[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Type conversion of octave_value to/from Python representations
From: |
David Grundberg |
Subject: |
Type conversion of octave_value to/from Python representations |
Date: |
Thu, 18 Sep 2008 16:57:31 +0300 |
User-agent: |
Thunderbird 1.5.0.14ubu (X11/20080306) |
Hi,
I'm working on a Python-to-Octave bridge in order to feval Octave
functions, like this:
print = pytave.feval("sin", (Numeric.ones([3, 3]),))
I've gotten this far for real 2d matrices. I'm writing a Python module
in C++, linking against Octave 3.0.2 and Python 2.4. However, I'm having
trouble with the conversions back and forth Octave values and Python
objects.
*Converting Python objects to octave-value:s*
Integers, floats and strings are converted with the octave_value
constructor. Numeric arrays are copied to a Array<T> subclass of closest
matching nature, and then wrapped into an octave_value. (i.e. support
for int8, int16 etc) Any 3d double matrices are converted to NDArray
instead of Matrix as a special case. The 1d cases are treated like 2d
cases, i.e. the vector subclasses are not used at all.
I'm having trouble with creating any octave-value with a dimension
higher than 3, how do I do that?
*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")
Oh, there is so much wrong with this type of checking, I don't know
where to begin. If someone has a pointer as how to find the type, or a
section of Octave code that does, or any hint on accomplishing the
conversion, please let me know.
David
PS: Anyone interested in the module can download the early-stage code
(with Autotools configuration) from here:
http://www.cs.umu.se/~c04dgg/pytave-0.1.tar.gz
- Type conversion of octave_value to/from Python representations,
David Grundberg <=