python interface to c++ class
M.-A. Lemburg
mal at lemburg.com
Wed Apr 14 17:32:31 EDT 1999
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/ :
---------------------------------------------------------
More information about the Python-list
mailing list