Search code examples
sageequation-solving

Solving equations in Sagemath


I have to find a vector and a matrix that transform some input vectors into others. The input vectors are: (−3, 4), (0, 8) i (6, 6) and the output for each vector is (1, −4), (6, −4) i (8, 2).

That's what i've tried: `

var('a b c d e f')
p = matrix([[-3,4],[0,8],[6,6]]).T
p2 = matrix([[1,-4],[6,-4],[8,2]]).T
q = vector([a,b])
t = matrix([[c,d],[e,f]])

eqs = [p2[i,j] == q[i] + t[i,:]*p[:,j] for i in range(2) for j in range(2)]
show(eqs)

solve(eqs,[a,b,c,d,e,f])

But when i say show(eqs) the output is [False,False,False,False] instead of the actual equations. Why?

I've tried the code above. I expect some feedback that can make me understand the error in the code.


Solution

  • The problem is that you're adding scalars and matrices: p2[i,j] is a scalar, as is q[i], but t[i,:]*p[:,j] is a 1x1 matrix: type(p2[1,1]) will return <class 'sage.rings.integer.Integer'> and type(q[0]) will return <class 'sage.symbolic.expression.Expression'>, but type(t[i,:]*p[:,j]) will return <class 'sage.matrix.matrix_symbolic_dense.Matrix_symbolic_dense'>. Try using the only entry in that matrix instead, either by explicitly pulling out the [0,0] entry or by converting the row of t and the column of p2 to vectors and using a dot product: use

    eqs = [p2[i,j] == q[i] + (t[i,:]*p[:,j])[0,0] for i in range(2) for j in range(2)]
    

    or

    eqs = [p2[i,j] == q[i] + t[i]*p.transpose()[j] for i in range(2) for j in range(2)]