symengine.py icon indicating copy to clipboard operation
symengine.py copied to clipboard

Indexed object support

Open zaprice opened this issue 5 years ago • 2 comments

SymPy provides support for indexed symbolic objects, i.e. vector or matrix variables. This is often useful when, say, taking partials with respect to different elements of some vector argument.

Are there plans for adding something like IndexedBase to symengine.py?

zaprice avatar May 21 '20 23:05 zaprice

What use cases do you have for it? If it's just what you have mentioned above, then a vector of symbols should work.

isuruf avatar May 22 '20 01:05 isuruf

Hi, sorry for taking so long to get back to this. Agreed; in the simplest case of a symbolic vector variable x, you can make a vector of symbols x[0], x[1], ..., which I am doing.

It is more complicated if you have references to elements (or constants) from a matrix or DataFrame, something like: x[0] + x[1] + data[2,102]*x[2] + data[3,99] In SymPy, you can handle this easily by declaring an IndexedBase like data = sympy.IndexedBase("data")

To work around this in symengine, I ended up creating a mapping from the DataFrame references data[i,j] to some new vector d[k], in order to use the same vector trick described earlier. But, this means you have to substitute data[i,j] -> d[k] in the equations, then take partials, then re-substitute to get the original matrix/DataFrame references. Maybe there is a more straightforward implementation that I missed.

The speed of symengine is really indispensable for computing huge Jacobians, so I'm glad to have been able to use it at all! IndexedBase support would make the SymPy-to-symengine conversion a bit easier in some cases.

zaprice avatar Jan 15 '21 17:01 zaprice