On Wed, Jul 14, 2010 at 7:09 PM, Robert Bradshaw
<[email protected]> wrote:
> Still
> waiting for someone to put up a wiki page on what they like from weave
> and what should go into Cython.

Ahem :)

A simple example of the blitz expressions which are nice:

In [2]: import numpy as np

In [3]: from scipy import weave

In [4]:

In [5]: a = np.arange(10)

In [6]: x = np.empty_like(a)

In [7]: weave.blitz('x=a+2*a*a')
creating 
/tmp/fperez/python26_intermediate/compiler_8dc7dab30c62f7787b818e8ffdc19f8c

In [8]: print x
[  0   3  10  21  36  55  78 105 136 171]

In [9]: print x-(a+2*a*a)
[0 0 0 0 0 0 0 0 0 0]


Interactively, weave gives you a feel of 'interactive C++' thanks to
its tying of the module name to the code hash:

In [17]: src = 'std::cout << "a is " << a << std::endl;'

In [18]: a = 10

In [19]: inline(src, ['a'])  # compilation happens here
a is 10

In [20]: a = 4.99

In [21]: inline(src, ['a'])  #compiling again, for a float
a is 4.99

In [22]: a = 12

In [23]: inline(src, ['a']) # no compilation, code for a int is reused
a is 12


Some examples that use the blitz indexing support:

# Returning a scalar quantity computed from an array.
def trace(mat):
    """Return the trace of a matrix.
    """
    nrow,ncol = mat.shape
    code = \
"""
double tr=0.0;

for(int i=0;i<nrow;++i)
    tr += mat(i,i);
return_val = tr;
"""
    return inline(code,['mat','nrow','ncol'],
                  type_converters = converters.blitz)

# In-place operations on arrays in general work without any problems
def in_place_mult(num,mat):
    """In-place multiplication of a matrix by a scalar.
    """
    nrow,ncol = mat.shape
    code = \
"""
for(int i=0;i<nrow;++i)
    for(int j=0;j<ncol;++j)
        mat(i,j) *= num;

"""
    inline(code,['num','mat','nrow','ncol'],
           type_converters = converters.blitz)


But at least I don't care if this *specific syntax* survives, I only
care about the ability to produce fast code to manipulate numpy arrays
with clean syntax.  Weave with inline/blitz helps really well in
practice on this front, and if we can get these abilities with Cython,
that would be great.

One more use case I have had in the past: I've auto-generated complex
C++ codes with dimension-specific numbers of loops and other
parameters of a problem, to produce objects which, once instantiated,
had a few key methods run very fast.  This proved quite easy to do
with weave, because I could compute the code as a string with nice
python code at object initialization time (when the
dimensions/parameters became known), and then  during the object's
life it would always use the compiled version.  So while 'coding in
strings' is in most situations awful, in this case it actually was
very handy to have the flexibility to use string-generated code and
weave handle all the gory details for me.


I hope this is useful, sorry for not having provided these details before.

Cheers,

f
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to