2017-03-12 2 views
5

Ich habe ein Gleichungssystem wie folgt aus:Symbolische Lösung von Gleichungssystem Sympy Verwendung mit trivialen Lösungen abhängig von Symbolen

enter image description here

Für diesen spezifischen System, weiß ich, dass eine nicht-triviale Lösung (en) nur existiert wenn p1 == p2, das ist

enter image description here.

Wie kann ich dies jedoch im allgemeinen Fall mit Sympy feststellen?

Für dieses Beispiel meine Implementierung wie folgt:

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) 

Das Ergebnis

None 

ist Wenn ich

p2 = p1 

das Ergebnis gesetzt ist

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

Gibt es eine Möglichkeit, diese Anforderung automatisch zu entdecken?

Antwort

3

In Ihrem Beispielcode erwartet solve_linear_system ein erweitertes System, d. H. Wenn die rechte Seite Null ist, sollte die Matrix als Matrix.zeros(8,8) deklariert werden. Mit dieser Modifikation Code ergibt

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

die in der Tat ist eine Lösung, die aber nicht die interessanteste ...

Um dies zu beheben, kann man ausdrücklich verlangen, dass eine Komponente der Lösung soll normalisiert werden , sagen, 1. Also, wenn Sie so etwas wie folgendes tun:

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))) 

und lösen für die verbleibenden Variablen sowie die Parameter p1,p2, das Ergebnis ist:

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

was in der Tat die gewünschte Lösung ist.

4

Es scheint, dass sympy behandelt p1 und p2 als nicht gleich sind, was bedeutet, dass

top_matrix.nullspace() 

[]

, dass das System mit homogeous Matrixkoeffizienten ist, keine nicht-top_matrix hat triviale Lösung.

Sie haben hier zwei Möglichkeiten. Die erste besteht darin, das homogene System mit unbekannten Variablen zu behandeln, wobei beide Elemente des Vektors bundp2 behandelt werden, wobei p1 als fester Parameter behandelt wird.

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}] 

Beachten Sie, dass das System zwei Lösungen. Die triviale (mit beliebigen p2), und eine nicht triviale, wo es p2==p1 (mit beliebigen b7) hält.

Die zweite Option ist zu realisieren, dass das System A*b=0 eine nicht triviale Lösung hat, wenn das System A.T*A*b=0 eine nicht triviale Lösung hat. Letzteres ist möglich, wenn die Determinante A.T*A Null ist.Die Determinante A.T*A gleich

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

6*(p1 - p2)**2

sofort offenbart, dass es, um p1==p2 halten muss eine nicht-triviale Lösung.

Verwandte Themen