It's not clear what exactly you want to do. Here are some
suggestions, though, that may or may not be what you want.
- To parse an expression given in string form into a SymPy object, us
the sympify() function:
In [12]: sympify("a*12 + 50*c")
Out[12]: 12⋅a + 50⋅c
Note that I did not have a or c defined at all before doing this.
sympify() does that automatically.
- To replace symbols with numerical values, use .subs():
In [17]: (a * 12 + 50*c - (456 + b)).subs({a:10, b:20})
Out[17]: 50⋅c - 356
You can also define them as Python variables as you did above too, but
this gives you more flexibility, as you can symbolically manipulate
the expression with a and b before substituting the values, and it
will be easier to put in different values later this way.
- There are two ways to do a == b in SymPy. You can do as I did above
and subtract one side from the other. Or, you can use the Eq() class:
In [18]: solve(Eq(a * 12 + 50*c, 456 + b), c)
Out[18]:
⎡ 6⋅a b 228⎤
⎢- ─── + ── + ───⎥
⎣ 25 50 25⎦
The lhs - rhs method is a little better, because it will play nicer
with other operations (e.g., right now, to add something to both sides
of an Equation, you have to do Eq(obj.lhs + expr, obj.rhs + expr)).
- Right now, sympify() doesn't support converting == to Eq() or lhs -
rhs. I created http://code.google.com/p/sympy/issues/detail?id=2966
for it. It should not be hard to do it by hand, though. You could
use the re module to replace (.*) == (.*) with Eq(\1, \2) or \1 - (\2)
(note the parentheses are necessary in the second one to make sure the
- applies over the whole right-hand side.
Do any of these answer your question?
Aaron Meurer
On Fri, Jan 6, 2012 at 4:07 AM, Bjorn <[email protected]> wrote:
> Hi, I am new to sympy and I have a basic question.
> I would like to use sympy to sovle simple sets of equations in a text
> editor.
>
> like given the following as plain text:
> a = 10
> b = 20
> a * 12 + 50*c == 456 + b
>
> and an instruction to solve for c, then ptorduce:
>
> c = 7.12
>
> I know sympy comes with a solver that can solve systems of f(x)=0,
> but in the problem above the third equation needs to be rewritten for
> this to work.
> To me, this seems as something that might have been done before?
> Id be grateful for directing me to a resource or telling me how or why
> this is harder than it seems.
>
> Sincerely,
> Bjorn
>
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/sympy?hl=en.
>
--
You received this message because you are subscribed to the Google Groups
"sympy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sympy?hl=en.