sympy.linsolve produces wrong result

Viewed 49

Consider the system of coupled, linear, algebraic equations: pic1

The solution set is infinite with: pic2

Attempting to solve this system using sympy gives an incorrect result of the form: pic3

Python code to reproduce incorrect result:

import numpy as np
import sympy

A = np.array([[1,1,0,1,1,-3,-1,1,],[0,0,0,-1,-1,0,-1,-2,],[0,0,0,0,0,1,1,1,]])
b = np.array([[0],[0],[0]])

variable_symbols = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']

#Convert to sympy matrix
A_sympy = sympy.Matrix(A)
b_sympy = sympy.Matrix(b)

#Make sympy symbols
syms = sympy.symbols(variable_symbols)
syms = list(syms)

#Solve system
sol_sympy = sympy.linsolve((A_sympy,b_sympy),syms)#Problem is here
print(sol_sympy)

The output is:

FiniteSet((-b - g - 2*h, b, c, -c - g - 2*h, e, -g - h, g, h))

Which gives the incorrect solution space of pic3.

The output should instead be:

FiniteSet((-b - g - 2*h, b, c, -e - g - 2*h, e, -g - h, g, h))

Edit: It was suggested that perhaps the solutions are identical, but written in different forms. But I don't think this is the case.

Consider a solution from the correct expression pic2 of the form b=0, c=1, e=0, g=0, h=0, This gives: a=0, b=0, c=1, d=0, e=0,f=0, g=0

NOTE: h,g,e,c,b are equal to alpha1,2,3,4,5 respectively.

Now consider obtaining the same solution, "a=0, b=0, c=1, d=0, e=0,f=0, g=0" but using the sympy expression pic3. It cannot be done.

Update & Solution: Error was produced using sympy 1.6.1. But an update to 1.7.1+ fixed the issue.

0 Answers
Related