Autowrap, ufuncify, and matrices

123 views
Skip to first unread message

James Crist

unread,
Aug 14, 2014, 6:11:17 PM8/14/14
to sy...@googlegroups.com
This is part of my continuing my work on making sympy capable of generating code for evaluating matrix functions. So far the codeprinters and codegen is done(ish); all that's left is autowrap.

Questions:

1. Should functions created by autowrap create matrices that would be in-out parameters in the resulting C code?

Suppose the C header is:

void func(double a, double b, double c, double mat[3][4])

Should `mat` be created in the wrapper code, so that the python function header is func(a, b, c)? The performance benefits vs pythonic-ness really have to do with the nature of the calculations. If `mat` is small, but has elements with lots of operations (as seen in sympy.physics.mechanics) then the performance hit is neglible. In contrast, if `mat` is big, but has simple elements, the hit is relatively large.

Perhaps a kwarg? Or maybe if autowrap is passed (Eq(x, mat)) it will set an input matrix `x` to `mat`, otherwise it will return matrix `mat`?

2. What should `ufuncify` do with matrices? Jason messaged me earlier with a thought on this, I'll just paste his use case here as it explains it fairly well.


A very common use case that I'm finding in all my dynamics work is to evaluate matrices in a tight loop for millions of iterations. It would be cool to pass in a sympy matrix and generate a function that would return a 3D numpy array when you evaluate the function with equal length arrays (or scalars) as args. For example:

expr = Matrix([[a, b], [c, d]])
f = ufuncify((a, b, c, d), expr)
out = f(rand(1e6), rand(1e6), rand(1e6), 1.5)
out.shape == (1e6, 4, 4)

This kind of broadcasting would be doable (but maybe complicated? I'm not sure.). The big question is, does this kind of behavior for `ufuncify` make sense?

Matthew Rocklin

unread,
Aug 14, 2014, 9:32:21 PM8/14/14
to sy...@googlegroups.com, Frédéric Bastien
1.  Maybe if mat is NULL then we malloc?  But probably the answer is what you suggest with an explicit kwarg

2.  Theano has a solution for this.  It may be worth looking at how they handle broadcasting.  While this is accessible through SymPy it looks like the SymPy documentation on this feature is sparse.  My apologies.


--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/771f1dfa-0a80-49ed-9a88-76a690f4aa96%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages