On 02/05/2020 14:20, Oscar Benjamin wrote:
I guess I haven't explained this very well. To be clear your example
sould work fine with Equation:
In [2]: a, b, k = symbols('a, b, k')
In [3]: Equation(a+k,b+k)-k
Out[3]: a = b
There needs to be a distinction between a python expression like a+b
and sympy expression like Add(a, b). I'll use lower-case expression
for a python expression and Expr for a sympy expression,
A python expression need not involve sympy types and can do all sorts
of different things e.g.:
In [9]: a = 'hel'
In [10]: b = 'lo'
In [11]: a+b
Out[11]: 'hello'
However if a and b are sympy Exprs than a python expression involving
them will create another sympy Expr so e.g. a+b becomes Add(a, b):
In [12]: a, b = symbols('a, b')
In [13]: c = a+b
In [14]: c
Out[14]: a + b
In [15]: type(c)
Out[15]: sympy.core.add.Add
I propose to have a new Equation type which is not Expr since it does
not represent a mathematical expression. It could however be used in
*python* expressions involving either Equation or Expr with the result
always being an Equation:
In [19]: eq = Equation(a, b)
In [20]: eq
Out[20]: a = b
In [21]: eq + 1
Out[21]: a + 1 = b + 1
In [22]: type(eq + 1)
Out[22]: __main__.Equation
The idea then is that we create an Equation with two Expr:
Equation(Expr, Expr) -> Equation
Then the following arithmetic operations with + would work for Equation/Expr:
Equation + Equation -> Equation
Equation + Expr -> Equation
Expr + Equation -> Equation
Expr + Expr -> Expr
Although the arithmetic operations would work the equation can not be
part of a*sympy* Expr so e.g. Add(eq, 1) should be an error if eq is
an Equation (that should only be valid where eq is an Expr).
Other useful functions to go with Equation would be a solve method e.g.:
In [23]: eq = Equation(2*x, y)
In [24]: eq
Out[24]: 2*x = y
In [25]: eq.solve(x)
Out[24]: x = y/2
This solve method would be for simple manipulations and would raise an
exception if there is not a unique solution (I guess it would have a
force option).
There should be a way to use Equation with a subs-like function e.g.:
In [26]: expr = x+y**2
In [27]: expr
Out[27]:
2
x + y
In [28]: subseq(expr, Equation(x, 2))
2
2 + y
Then functions like dsolve that currently return Equality could return
Equation and those could be used directly for substitutions.
Thanks for that great explanation. I think my confusion was that I
assumed that if you typed in the expression
Equation(a+k,b+k)+k
Then the first thing that would happen would be to transform the
expression to Add(Equation(add(a,k),k) and then try to evaluate the
result. If that had been correct than the outer Add() expression could
not be created and would generate an error.
David
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/sympy/ea32f6ce-35c1-6ad2-ddd5-902a703dd1ef%40dbailey.co.uk.