0

sympy.line.equation()

Same value and type, but what's the difference?"

How can I fix it?

What is the difference between z1 and z2?

from sympy import *
var('x y z1 z2')
z1=8*x+6*y+48
print("#z1",z1,type(z1))

z2=Line(Point(-6,0),Point(0,-8)).equation()
print("#z2",z2,type(z2))

if type(z1)==type(z2):
    print("#","type==")
else:
    print("#","type<>")
if z1==z2:
    print("#","==")
else:
    print("#","<>")
#z1 8*x + 6*y + 48 <class 'sympy.core.add.Add'>
#z2 8*x + 6*y + 48 <class 'sympy.core.add.Add'>
# type==
# <>

I try add .expand().simplify() 30 mins ago

from sympy import *
var('x y')
print("#z1#",solve(8*x+6*y+ 48                                                 ,y))
print("#   ",      Line(Point(-6,0),Point(0,-8)).equation().expand().simplify()   )
print("#z2#",solve(Line(Point(-6,0),Point(0,-8)).equation().expand().simplify(),y))
#z1# [-4*x/3 - 8]
#    8*x + 6*y + 48
#z2# []

Thank you.

from sympy import *
var('x y')
print("#z1#",solve(8*x+6*y+ 48                                           ,y))
print("#   ",                  Line(Point(-6,0),Point(0,-8)).equation()     )
print("#z2#",solve(sympify(str(Line(Point(-6,0),Point(0,-8)).equation())),y))
#z1# [-4*x/3 - 8]
#    8*x + 6*y + 48
#z2# [-4*x/3 - 8]

2 Answers 2

1

The equality operator (==) in SymPy tests whether expressions have identical form, not whether they are mathematically equivalent.

If you want to determine the mathematical equivalence of nontrivial expressions, you should apply a more advanced simplification routine to both sides of the equation. In the case of polynomials, expressions can be rewritten in a canonical form by expanding them fully. This is done using the .expand() method in SymPy - in your case:

print(bool(z1.expand()==z2.expand()))

or

print((z1-z2).expand())

In first case True will be resulted for equivalent expressions. In second case you will get 0 (zero) if expressions are equivalent. But you will have False and 8x - 8x - 6y + 6y instead.

If you try simplify(), which attempt more advanced transformations, you will get the same result:

print(simplify(z1-z2))

That means that your expressions has same type and 'value', but not mathematically equivalent. See detail here.

1
  • 1
    The equations are in identical form but they are using symbols with different assumptions, in this case.
    – smichr
    Commented Jul 16, 2022 at 12:11
0

When you don't tell equation what symbols to use, it creates its own symbols x and y with assumptions appropriate for a line (i.e. real). When you create a symbol with var the symbols are created with only commutative assumptions. Symbols that have different assumptions do not compare equal.

>>> from sympy import Line, oo
>>> Line((0,0),slope=oo).equation()
x
>>> _._assumptions
{'real': True, 'commutative': True, 'infinite': False, 'extended_real': True,
 'hermitian': True, 'complex': True, 'imaginary': False, 'finite': True}
>>> var('x')
x
>>> _._assumptions
{'commutative': True, 'zero': None}
>>> Line((0,0),slope=oo).equation(x)  # supply value of x to use
x
>>> _ == x
True
>>> Line((0,0),slope=oo).equation() == x  # no default given
False

So...if you want to compare expression built by Line and yourself, use the same symbols (i.e. provide the defaults to Line.equation. Otherwise the hidden assumptions will cause issues.

>>> from sympy.abc import y
>>> Line(Point(-6,0),Point(0,-8)).equation(x,y)
8*x + 6*y + 48
>>> _==8*x + 6*y + 48
True

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.