[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Type conversion of octave_value to/from Python representations
From: |
John W. Eaton |
Subject: |
Type conversion of octave_value to/from Python representations |
Date: |
Thu, 18 Sep 2008 11:23:52 -0400 |
On 18-Sep-2008, David Grundberg wrote:
| I'm having trouble with creating any octave-value with a dimension
| higher than 3, how do I do that?
Create an NDArray using a dim_vector object to specify the
dimensions.
| *Converting octave-value:s to Python objects*
|
| This is when I really got stuck. I'm having trouble figuring out how to
| properly access the actual storage type (Array<T> subclass, or
| primitive, or?). I'm doing this right now (kind of):
|
| octave_value ov = fromsomewhere;
| if (ov.is_bool_scalar)) {
| pyobj = object(ov.bool_value());
| } if // etc..
| } else if (ov.is_matrix_type()) { // try to convert to numeric array
| if (ov.is_nd_real_array()) {
| // Try to use instantiate an NDArray with octave-value-argument
| constructor
| }
| else if (ov.is_double_type()) {
| // Try to use instantiate a Matrix with octave-value-argument
| constructor
| }
| }
|
| And this won't work, since octave-values with NDArray:s won't return
| true on is_nd_real_array(). The Matrix(ov) constructor won't accept a 3d
| real ov either. ("fatal: invalid conversion of NDArray to Matrix")
Doesn't something like
if (ov.is_single_type ())
{
if (ov.is_complex_type ())
{
FloatComplexNDArray tmp = ov.float_complex_array_value ();
...
}
else if (ov.is_real_type ())
{
FloatNDArray tmp = ov.float_array_value ();
...
}
else
error (???);
}
else if (ov.is_double_type ())
{
if (ov.is_complex_type ())
{
ComplexNDArray tmp = ov.complex_array_value ();
...
}
else if (ov.is_real_type ())
{
NDArray tmp = ov.array_value ();
...
}
else
error (???);
}
else ...
work? I'm assuming you are working with the development version of
Octave, which has the single type. Does Python have single precision
numeric values?
| Oh, there is so much wrong with this type of checking, I don't know
| where to begin.
Pick a spot and start griping, I guess.
How would you like to be able to write this conversion?
jwe