Solve indexed symbol

66 views
Skip to first unread message

Will

unread,
Apr 19, 2017, 7:52:46 PM4/19/17
to sympy
Dear all,

So I'm trying to use Sympy to solve a simple system with indexed symbols, something like this:

    Eq 1.            y[i] = sqrt(x[i])
    Eq 2.            m = Sum(z[i] * y[i], (i, 0, n)) / Sum(x[i], (i, 0, n))

where, x, y, z, i, n and m are symbols (x, y and z are IndexedBase and i is Idx, n and m are Symbol).

Now given each x[i], n and z[i], I'd like to lambdify a callable function to evaluate m, how can I achieve this?

Issue with my attempt: I tried using solve([Eq1, Eq2]) to generate a symbolic solution for m first, but it seems that y[i] is not resolved as sqrt(x[i]) but kept in its original form y[i].

Any help is appreciated!

Aaron Meurer

unread,
Apr 20, 2017, 3:28:25 PM4/20/17
to sy...@googlegroups.com
The code generation functionality needed to make this sort of thing automatic isn't implemented yet. Ideally, you'd be able to lambdify the two statements as a single block of code. Also, for solve(), elimination is not implemented yet (https://github.com/sympy/sympy/issues/2720). For now, your best bet is to simply substitute the one expression in the other (m.subs(y[i], sqrt(x[i]))). 

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+unsubscribe@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/470f6d1b-534e-47ad-87a3-acdab0362b3e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Will

unread,
Apr 21, 2017, 4:32:15 PM4/21/17
to sympy
Hi Aaron,

Thanks for helping!

In this case I'll probably implement a wrapper parser to roll out the indexes first and then feed the generated equations to sympy.

Any plan on automating this in future release?

Best,
Will


On Thursday, April 20, 2017 at 12:28:25 PM UTC-7, Aaron Meurer wrote:
The code generation functionality needed to make this sort of thing automatic isn't implemented yet. Ideally, you'd be able to lambdify the two statements as a single block of code. Also, for solve(), elimination is not implemented yet (https://github.com/sympy/sympy/issues/2720). For now, your best bet is to simply substitute the one expression in the other (m.subs(y[i], sqrt(x[i]))). 

Aaron Meurer
On Wed, Apr 19, 2017 at 7:08 PM, Will <cuiweil...@gmail.com> wrote:
Dear all,

So I'm trying to use Sympy to solve a simple system with indexed symbols, something like this:

    Eq 1.            y[i] = sqrt(x[i])
    Eq 2.            m = Sum(z[i] * y[i], (i, 0, n)) / Sum(x[i], (i, 0, n))

where, x, y, z, i, n and m are symbols (x, y and z are IndexedBase and i is Idx, n and m are Symbol).

Now given each x[i], n and z[i], I'd like to lambdify a callable function to evaluate m, how can I achieve this?

Issue with my attempt: I tried using solve([Eq1, Eq2]) to generate a symbolic solution for m first, but it seems that y[i] is not resolved as sqrt(x[i]) but kept in its original form y[i].

Any help is appreciated!

--
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.

Aaron Meurer

unread,
Apr 21, 2017, 4:41:51 PM4/21/17
to sy...@googlegroups.com
We'd like to make it so that you can pass a block of statements to
lambdify and it will generate one function that runs them all. Once we
have that, you will be able to pass in your expressions as you have
them directly. We already have a way to represent a block of
statements (CodeBlock in sympy.codegen.ast), but we need to add
support to lambdify.

Aaron Meurer
> https://groups.google.com/d/msgid/sympy/422ff8d1-83a7-472b-9556-e5da4c74168c%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages