# simple way solve equations form : y_1 = x_00*y_1 + x_01*y_2

Hi

is there a simplest way to get the cleaned solution below ?

startP=[1,2,3,4,5]
endP=[1,4,2,5,3]
n=len(startP)-1
varsX=var(["x_%d%d"%(c,r) for c in [0..n] for r in [0..n]])
varsY=var(["y_%d"%(c) for c in [0..n+1] ] )
#show(varsX)
#show(varsY)
eq0=y_1 == x_00*y_1 + x_01*y_2 + x_02*y_3 + x_03*y_4 + x_04*y_5
S=solve(eq0,varsX[0:5])
for s in S:
print(s)

##  get the solution x_00==1,x_01==0,x_02==0,x_03==0,x_04==0
cleanedS=[]
for s in S :
#print(s)
varsR=[]
for v in (s.variables()) :

if v not in  varsX+varsY :
#print('r : ',v)
varsR.append(v==0)
#print(varsR)
cleanedS.append(s.subs(varsR))
print('the cleaned solution : ',cleanedS)

edit retag close merge delete

why there is error if I add domain='real' , in the code above?

varsX=var(["x_%d%d"%(c,r) for c in [0..n] for r in [0..n]],domain='real')
varsY=var(["y_%d"%(c) for c in [0..n+1] ] ,domain='real')


why there is error if I add domain='real' , in the code above?

I don't get that. What Sage do you use ?

You can get an error if you re-run code creating assumptions already existing : Sage's assumption system (i. e. Maxima's) dislikes redundancies and abhorrs inconsistencies... Better to forget the assumptions ((or reset the variables).

yes I forgot to precise: Sagemath 10.0 ,Ubuntu 22.04.2 LTS ,WSL2 ,W11 sorry for that.

I understand for forget or reset assumption.

Thank you @Emmanuel Charpentier

Sort by » oldest newest most voted

The equation you are trying to solve is :

sage: eq0
y_1 == x_00*y_1 + x_01*y_2 + x_02*y_3 + x_03*y_4 + x_04*y_5


As you want to solve it, this is a linear equation with five unknowns (x_00, x_01, x_02, x_03, x_04) and five parameters (y_1 through y_5). This equation has for general solution a 4-dimensional vector space (see any linear algebra textbook, available in all good groceries and convenience stores...). That's what the initial solution states :

sage: S
[x_00 == -(r8*y_2 + r7*y_3 + r6*y_4 + r5*y_5 - y_1)/y_1,
x_01 == r8,
x_02 == r7,
x_03 == r6,
x_04 == r5]


meaning that x_01 through x_04 have arbitrary values.

Your "cleanup" arbitrarily replaces these arbitrary values with 0 ; as a consequence, the x values disappear from the (sole) equation defining your solution. You lose the meaning of this solution.

A better solution to understand the structure of this solution is to solve for just one variable (i. e. tour equation is now a linear equation of one unknown and nine parameters) ; for examples :

sage: solve(eq0, x_00)
[x_00 == -(x_01*y_2 + x_02*y_3 + x_03*y_4 + x_04*y_5 - y_1)/y_1]


(identical to Sage's choice of expression)

sage: solve(eq0, x_02)
[x_02 == -((x_00 - 1)*y_1 + x_01*y_2 + x_03*y_4 + x_04*y_5)/y_3]


(if you want to express x_02as a function of the others)

or to use Sympy, which returns (a) set of relations necessary an sufficient to define your solution space :

sage: solve(eq0, varsX[:5], algorithm="sympy")
[x_00 == -(x_01*y_2 + x_02*y_3 + x_03*y_4 + x_04*y_5 - y_1)/y_1]


The first choice lets you pick the variable you want to solve for ; the second one (and the initial solution) let Sage (i. e. Sympy) pick it.

HTH...

more