On Sat, 2 May 2020 at 12:12, David Bailey <[email protected]> wrote:
>
> On 02/05/2020 00:07, Oscar Benjamin wrote:
>
> There is a difference between having an object as part of an
> expression and having an object that supports arithmetic operations.
> Objects of type Expr support arithmetic operations and by default the
> result creates an expression involving the original object so e.g. a+b
> becomes Add(a, b). That's what happens with Eq in sympy 1.5 because in
> 1.5 Eq is a subclass of Expr so Eq(a, b)+1 becomes Add(Eq(a, b), 1)
> which is nonsensical. In 1.6 Eq does not subclass Expr and also Expr
> is fussier about what types it can interact with through arithmetic
> operations so this gives an error.
>
> The point of Equation is that it can support some arithmetic
> operations and the result will always be an Equation and never an
> expression. It should never be a part of an expression though so e.g.
> Add(Equation(x, y), z) is an error because Add is of type Expr and its
> arguments need to be of type Expr but Equation is not of type Expr.
> That does not preclude Equation(x,y)+1 giving an Equation though.
>
> Doesn't that feel a bit arbitrary, I mean you seem to be saying saying that
>
> Equation(a+k,b+k)-k
>
> where a,b, and k are SymPy symbols
>
> would fail?
>
> If that is the case, the new ability to add/subtract things from Equations
> doesn't seem that powerful.
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.
--
Oscar
--
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/CAHVvXxT37_GEtnRs-SE1K1LV1AeYGyzQdqcAq9Z5OWCHQ3LsVg%40mail.gmail.com.