Ask Your Question

Cannot solve system of simultaneous equations

asked 2018-04-25 18:01:09 -0500

PatrickLewis gravatar image

I am trying to solve a particular problem, but my test solution for setting up the code isn't working. I have tried two ways - one, which just outputs the same equations, and the other, I'm unsure on how to solve this large list of equations which are unrealistic to type out.

First attempt

var('a b c d e f g h i')

This outputs $$\begin{pmatrix} ab+ac+bc & cd+ae+bf & cg+ah+bi \\ bd+ce+af & de+df+ef & fg+dh+ei \\ bg+ch+ai & eg+fh+di & gh+gi+hi \end{pmatrix}$$ I then make $L=X$, then type these equations element wise into the simultaneous equation solver;


The output:

 a*e + b*f == 1, d*e + d*f + e*f == 0, b*g + c*h + a*i == 1, c*g + a*h + b*i == 0, e*g + f*h + d*i == 0, f*g + d*h + e*i == 1, g*h + g*i + h*i == 0]

Second attempt;

for i in range(0,3):
    for j in range(0,3):

This gives me a list of the equations, I then try the solver again;


This just gives me an error:

(a, b, c, d, e, f, g, h, i)
Error in lines 13-13
Traceback (most recent call last):
  File "/cocalc/lib/python2.7/site-packages/smc_sagews/", line 1013, in execute
    exec compile(block+'\n', '', 'single') in namespace, locals
  File "", line 1, in <module>
  File "/ext/sage/sage-8.1/local/lib/python2.7/site-packages/sage/symbolic/", line 976, in solve
    raise TypeError("%s is not a valid variable." % repr(i))
TypeError: 2 is not a valid variable.

The hope is that I will be able to extend this to larger matrices, so autonomy is welcomed greatly. But I will happily take any advice given I'm at a completely lose end. It is worth noting that the solution to this problem; $$PXP^{\dagger}=X$ is the identity matrix. My understanding is that the first method cannot find a closed form? I have even tried specifying that a,b,c,d,e,f,g,h,i are all 0 or 1 and this has not helped. I believe my second method is more on the right track, however I cannot afford to manually type out the 9 non-linear equations that pop out from the matrix solver.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2018-04-26 21:13:55 -0500

dan_fulea gravatar image

The variable i was overwritten with $2$.

The error can be recovered as follows:

sage: var( 'h,i' );
sage: solve( [h+i == 7, h-i == 3], [h,i] )
[[h == 5, i == 2]]
sage: for i in range(3):    print "i = %s" % i
i = 0
i = 1
i = 2
sage: solve( [h+i == 7, h-i == 3], [h,i] )
TypeError                                 Traceback (most recent call last)
<ipython-input-628-48574179ae51> in <module>()
----> 1 solve( [h+i == Integer(7), h-i == Integer(3)], [h,i] )

/usr/lib/python2.7/site-packages/sage/symbolic/relation.pyc in solve(f, *args, **kwds)
    974         for i in x:
    975             if not isinstance(i, Expression):
--> 976                 raise TypeError("%s is not a valid variable." % repr(i))
    977     elif x is None:
    978         vars = f.variables()

TypeError: 2 is not a valid variable.
edit flag offensive delete link more

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


Asked: 2018-04-25 18:01:09 -0500

Seen: 58 times

Last updated: Apr 26