Ask Your Question
0

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

asked 2023-08-27 09:23:04 +0100

ortollj gravatar image

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])[0]
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 flag offensive close merge delete

Comments

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')
ortollj gravatar imageortollj ( 2023-08-27 09:31:32 +0100 )edit

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

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

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2023-08-27 14:37:05 +0100 )edit

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

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2023-08-27 15:07:04 +0100 )edit

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

ortollj gravatar imageortollj ( 2023-08-27 17:21:07 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-08-27 15:44:28 +0100

Emmanuel Charpentier gravatar image

updated 2023-08-27 15:45:26 +0100

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

edit flag offensive delete link more

Comments

Thanks again @Emmanuel Charpentier.

ortollj gravatar imageortollj ( 2023-08-27 17:21:55 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2023-08-27 09:23:04 +0100

Seen: 141 times

Last updated: Aug 27 '23