Assigning to variables with autowrap

17 views
Skip to first unread message

Nathaniel Virgo

unread,
Oct 4, 2018, 12:49:59 AM10/4/18
to sympy
This would probably be a better solution to the problem in my previous mail, if it can be done. I've posted the same text as a question on Stack Overflow at https://stackoverflow.com/questions/52638892/using-assignment-with-autowrap-in-sympy.

I am writing some code that uses sympy to construct a complicated mathematical expression, then uses the autowrap function to compile it and evaluate it repeatedly.

Unfortunately, my expressions are becoming exponentially large in some cases. To prevent this, I want to generate expressions that contain assignment statements. That is, I want to end up with something like (pseudocode)

x1 = f(a,a);
x2 = f(x1,x1);
x3 = f(x2,x2);
x4 = f(x3,x3);

instead of

f(f(f(a,a),f(a,a)),f(f(a,a),f(a,a))),f(f(a,a),f(a,a)),f(f(a,a),f(a,a))))

in which every subexpression has been substituted twice.

There seems to be a mechanism for this, in the form of sympy.codegen.ast.CodeBlock. However, I can't seem to work out how to get this to work with autowrap, if indeed it can.

If I attempt to do

>>> f = autowrap(ast.CodeBlock(ast.Assignment(y,sym.sin(x))),backend='cython')

then I get a long stack trace ending with

wrapped_code_12.c:5:22: error: use of undeclared identifier 'y'
   autofunc_result = y = sin(x);
                     ^
1 error generated.
error: command '/usr/bin/clang' failed with exit status 1

which suggests to me that using a CodeBlock directly inside autowrap isn't the right way to do it. (I get a similar error using the Fortran backend.)

The question is, is there a right way to do it? Can I use CodeBlock in autowrap, or is there some other way that I can generate intermediate variables with autowrap rather than always having my sympy expressions fully expanded out?


Best regards

Nathaniel


Aaron Meurer

unread,
Oct 4, 2018, 1:53:29 AM10/4/18
to sy...@googlegroups.com
autowrap doesn't understand CodeBlock yet.

Ideally your last element in the CodeBlock would be an expression, and
autowrap would assign that (and it would give a better error message
if it isn't). I opened https://github.com/sympy/sympy/issues/15336 for
this.

I'm not sure if is a more proper way to do what you want. autowrap is
fairly complicated, so I can't figure out how to do it, but I suspect
it is possible.

Aaron Meurer
> --
> 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 https://groups.google.com/group/sympy.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/265d7681-3377-4dbe-8997-7d12fd662b19%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages