Symbolic solution to an equation system using Sympy with trivial symbol-dependent solutions

I have a system of equations such as:

enter image description here

For this particular system, I know that a nontrivial solution exists only if p1 == p2, which

enter image description here .

However, how can I define this in general using Sympy?

In this example, my implementation is as follows:

from sympy import Matrix, symbols, pprint, lcm, latex from sympy.solvers import solve_linear_system top_matrix = Matrix.zeros(8,7) p1 = symbols("p1") p2 = symbols("p2") top_matrix[0,0] = 1 top_matrix[0,1] = -1 top_matrix[1,1] = (1-p1) top_matrix[1,2] = -1 top_matrix[2,2] = 1 top_matrix[2,4] = p2-1 top_matrix[3,1] = p1 top_matrix[3,3] = -1 top_matrix[4,3] = 1 top_matrix[4,4] = -p2 top_matrix[5,4] = 1 top_matrix[5,5] = -1 top_matrix[6,1] = -1 top_matrix[6,6] = 1 top_matrix[7,4] = -1 top_matrix[7,6] = 1 pprint(top_matrix) vars = symbols("a1, a2, a3, a4, a5, a6, a7, a8") print solve_linear_system(top_matrix, *vars) 

Result

 None 

If i installed

 p2 = p1 

result

 {a1: -1, a5: -1, a2: -1, a6: -1, a3: p1 - 1, a4: -p1} 

Is there a way to automatically detect this requirement?

+5
source share
2 answers

In your example code, solve_linear_system expects an extended system, i.e. if the right side is zero, then the matrix should be declared as Matrix.zeros(8,8) . With this modification, your code gives

 {a3: 0, a1: 0, a5: 0, a7: 0, a6: 0, a2: 0, a4: 0} 

which is really a solution, albeit not an interesting one ...

To fix this, you can explicitly request that one component of the solution be normalized, say 1. So, if you are doing something like the following:

 from sympy import Matrix, symbols, pprint, lcm, latex, solve top_matrix = Matrix.zeros(8,7) p1,p2 = symbols("p1, p2") top_matrix[0,0] = 1 top_matrix[0,1] = -1 top_matrix[1,1] = (1-p1) top_matrix[1,2] = -1 top_matrix[2,2] = 1 top_matrix[2,4] = p2-1 top_matrix[3,1] = p1 top_matrix[3,3] = -1 top_matrix[4,3] = 1 top_matrix[4,4] = -p2 top_matrix[5,4] = 1 top_matrix[5,5] = -1 top_matrix[6,1] = -1 top_matrix[6,6] = -1 top_matrix[7,4] = 1 top_matrix[7,6] = 1 pprint(top_matrix) a1,a2,a3,a4,a5,a6,a7 = list(symbols("a1, a2, a3, a4, a5, a6, a7")) B = Matrix([[1],[a2],[a3],[a4],[a5],[a6],[a7]]) C = top_matrix * B print(solve(C, (a2,a3,a4,a5,a6,a7,p1,p2))) 

and solve for the remaining variables, as well as the parameters p1,p2 , the result:

 [{a2: 1, a7: -1, a4: p2, a6: 1, a5: 1, p1: p2, a3: -p2 + 1}] 

what really is the solution you are looking for.

+3
source

It seems like sympy treats p1 and p2 as unequal, which means that

 top_matrix.nullspace() 

[]

those. homogeneous systems with top_matrix matrix coefficients top_matrix not have a nontrivial solution.

You have two options. First, consider a homogeneous system as having unknown variables as elements of the vector b and p2 , considering p1 as a fixed parameter.

 b = sp.Matrix(sp.symbols('b1:8')) #import sympy as sp sp.solve(top_matrix*b, (*b,p2)) 
 [{b1: 0, b2: 0, b3: 0, b4: 0, b5: 0, b6: 0, b7: 0}, {b1: b7, b2: b7, b3: b7*(-p1 + 1), b4: b7*p1, b5: b7, b6: b7, p2: p1}] 

Please note that the system has two solutions. Trivial (with arbitrary p2 ) and nontrivial, where it contains p2==p1 (with arbitrary b7 ).

The second option is to understand that the system A*b=0 has a nontrivial solution if the system AT*A*b=0 has a nontrivial solution. The latter is possible if the determinant AT*A is zero. The determinant AT*A is

 (top_matrix.T * top_matrix).det().factor() 

6*(p1 - p2)**2

immediately discovering that it must contain p1==p2 in order to have a non-trivial solution.

+4
source

Source: https://habr.com/ru/post/1265354/


All Articles