Mailing List Archive

python interface to c++ class
Hello, everyone.
I am now working on building Python interface
to my C++ matrix library.

for example In C++,we can enjoy following
operations.
(f_matrix: matrix class for float, d_matrix: for
double)

1. f_matrix x1,x2; float y; x2=y*x1; (operation
between different types)
2. x1(1,2)=3.4; (substitution for matrix
element)
3. d_matrix z; z=x1;
(auto conversion from float matrix to another
type(d_matrix))

Is it possible by "PyNumberMethods" ?
I would like to know how to implement above with
Python.

Thanks for reading.


Thooney Millennier.
python interface to c++ class [ In reply to ]
Thooney Millennier wrote:
>
> Hello, everyone.
> I am now working on building Python interface
> to my C++ matrix library.

Maybe you could use the NumPy extension as basis for
this... I guess integrating your lib there should be
doable (though they already have an multi-dimensional array).

> for example In C++,we can enjoy following
> operations.
> (f_matrix: matrix class for float, d_matrix: for
> double)
>
> 1. f_matrix x1,x2; float y; x2=y*x1; (operation
> between different types)

Implementing mixed type numerics is no fun when done in
C (there's no real support for it; I have a patch though,
that I would like to see in Python1.6 and which I will
update now that 1.5.2 is out).

In Python it's a piece of cake: just use the __op__
and __rop__ methods.

So if you are using SWIG to wrap your C++ classes
with shadow classes, you're lucky. To see how the
current C implementation can be tweaked to do
mixed number operations look at the source code of
mxDateTime (available from the link below).

> 2. x1(1,2)=3.4; (substitution for matrix
> element)

You can do this with __set/getitem__:

class C:
def __getitem__(self,what):
print 'get',repr(what),type(what)
def __setitem__(self,what,to):
print 'set',repr(what),type(what),to


o = C()

o[1,2]
o[1,...,3]

o[1,2] = 3
o[1,2,3] = 4
o[1,...,3] = 5

gives:

get (1, 2) <type 'tuple'>
get (1, Ellipsis, 3) <type 'tuple'>
set (1, 2) <type 'tuple'> 3
set (1, 2, 3) <type 'tuple'> 4
set (1, Ellipsis, 3) <type 'tuple'> 5

> 3. d_matrix z; z=x1;
> (auto conversion from float matrix to another
> type(d_matrix))

Python uses name bindings. z=x1 will only bind
the name 'z' to the object referenced as 'x1'.
You'll have to use explicit conversion, e.g.
z = toDMatrix(x1).

--
Marc-Andre Lemburg Y2000: 261 days left
---------------------------------------------------------------------
: Python Pages >>> http://starship.skyport.net/~lemburg/ :
---------------------------------------------------------
python interface to c++ class [ In reply to ]
> for example In C++,we can enjoy following
> operations.
> (f_matrix: matrix class for float, d_matrix: for
> double)
>
> 1. f_matrix x1,x2; float y; x2=y*x1; (operation
> between different types)

The Numeric module has these types of object defined (but extended to N-D
arrays). Operations between different types is supported by
sophisticated broadcasting rules.

> 2. x1(1,2)=3.4; (substitution for matrix
> element)

With Numeric one can substitute for array (matrix) elements or array
slices (start, stop, step).

> 3. d_matrix z; z=x1;
> (auto conversion from float matrix to another
> type(d_matrix))
>

Since Python is not a staticly typed language, typing z = x1 when x1 is a
Numeric multiarray object will just bind the name z to the object x1. If
x1 is a Float array and you want a double array, you say.

z = x1.astype('d')


It sounds like you should really check out the Numeric module and see if
it doesn't fit your needs. It is quite a useful tool.


Best,

Travis