lambdify function with array of parameters

88 views
Skip to first unread message

Nico Schlömer

unread,
Mar 19, 2018, 6:06:22 PM3/19/18
to sympy
Given the degrees in numerator and denominator I would like to represent a Padé polynomial à la
```
P(x) = (a[0] + a[1]*x + a[2]*x**2 + ...) / (1 + b[1]*x + b[2]*x**2 + ...)
```
in sympy and let it do the heavy lifting, e.g., computation of derivatives.

Operations that I'll need to do:
  * Replace a[] and b[] with actual numerical values (in both P and P')
  * Evaluate those P_{a,b} and P'_{a, b} for many x.

The first idea was to go
```
import numpy
import sympy

a = sympy.MatrixSymbol('a', 2, 1)
b = sympy.MatrixSymbol('b', 3, 1)

x = sympy.Symbol('x')

P = (a[0] + a[1]*x) / (1 + b[0]*x + b[1]*x**2 + b[2]*x**3)

val_a = numpy.random.rand(2)
P_a = P.evalf(subs={a: val_a})  
```
The last line however doesn't seem to do anything at all. Second attempt:
```
Pa = sympy.lambdify(a, P)(val_a)
```
This gives
```
IndexError: too many indices for array
```
Hm. Any hints of how to proceed here? Perhaps `MatrixSymbol` isn't what I'm looking for.

Leonid Kovalev

unread,
Mar 19, 2018, 6:24:59 PM3/19/18
to sympy
P_a = P.subs(dict(zip(a, val_a)))

returns 

(0.450033195586719*x + 0.829322314411828)/(x**3*b[2, 0] + x**2*b[1, 0] + x*b[0, 0] + 1)

You need to substitute individual MatrixElement objects like a[0], not the MatrixSymbol itself (which isn't even present in the expression P). 


Leonid Kovalev

unread,
Mar 19, 2018, 6:26:48 PM3/19/18
to sympy
Also, the conversion to dict was unnecessary, subs can take an iterator of tuples.   

P_a = P.subs(zip(a, val_a))




Nico Schlömer

unread,
Mar 19, 2018, 6:29:28 PM3/19/18
to sympy
I see!
I also noticed that I can use `DeferredVector`s instead of `MatrixSymbol`s to get avoid those dict shenanigans. This
```
import numpy
import sympy

a = sympy.DeferredVector('a')
b = sympy.DeferredVector('b')

x = sympy.Symbol('x')

P = (a[0] + a[1]*x) / (1 + b[0]*x + b[1]*x**2 + b[2]*x**3)

val_a = numpy.random.rand(2)
val_b = numpy.random.rand(3)

Pa = sympy.lambdify((a, b), P)(val_a, val_b)


print(P)
print(Pa)
```
works. It's a bit weird to create a lambdify only to call it straight away though. Also, I'm reading [1] that `DeferredVector` were once scheduled for removal? Probably outdated.

Leonid Kovalev

unread,
Mar 19, 2018, 6:49:33 PM3/19/18
to sy...@googlegroups.com
lambdify should not be needed to work with or create SymPy
expressions. Its purpose is to make a SymPy expression usable by other
libraries.

As I wrote, "dict shenanigans" were not needed. A MatrixSymbol is an
iterable. `zip(MatrixSymbol, array_of_values)` creates an iterator of
tuples, indicating what element is substituted by what value. A
natural thing to do in Python, I wouldn't look to replace it with
lambdify and DeferredVector (which apparently isn't removed because
everyone forgot about it).
> --
> 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/1cdc96e2-6dd7-4540-9c55-44814d9a01b3%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

Nico Schlömer

unread,
Mar 22, 2018, 11:45:00 AM3/22/18
to sy...@googlegroups.com
Alright, thanks for the explanation.

You received this message because you are subscribed to a topic in the Google Groups "sympy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sympy/1A2vJdNInfE/unsubscribe.
To unsubscribe from this group and all its topics, 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.
Reply all
Reply to author
Forward
0 new messages