# General solution for a matrix

I have a matrix named p and with the values

[-10*x1 - 4*x2 + 14,   -4*x1 - 2*x2 + 6]


When I try to solve the matrix with this line:

print(p(p.arguments()[0] == 0, p.arguments()[1] == 0))


I get this error:

ValueError: use named arguments, like EXPR(x=..., y=...)


In general i want to use something like this, the arguments are x1 and x2 and the x is a list of numbers that it will change on every loop

print(p([i == j for i,j in zip(f.arguments(), x)]))


But it reproduce the same error

edit retag close merge delete

( 2019-04-17 01:29:26 -0500 )edit

Can you provide the actual code you use to define p?

( 2019-04-17 01:30:03 -0500 )edit

Sort by » oldest newest most voted

To solve this system of two equations in two unknowns, one can either use solve or, since the equations are linear, one could use linear algebra.

Here is how to use solve:

sage: x1, x2 = SR.var('x1 x2')
sage: p = [-10*x1 - 4*x2 + 14, -4*x1 - 2*x2 + 6]
sage: solve(p, x1, x2)
[[x1 == 1, x2 == 1]]


Using linear algebra, the question translates as solving $a u = b$ for $u$, where $a = \begin{pmatrix}10 & 4 \newline 4 & 2 \newline \end{pmatrix}$ and $b = \begin{pmatrix}14 \newline 6\end{pmatrix}$.

Here is how to solve that:

sage: a = matrix([[10, 4], [4, 2]])
sage: b = vector([14, 6])
sage: a \ b
(1, 1)


Now it's easy to change the vector b.

sage: a \ vector([3, 8])
(-13/2, 17)


To solve in the general case $b = \begin{pmatrix}b_x \newline b_y\end{pmatrix}$:

sage: a = matrix(SR, [[10, 4], [4, 2]])
sage: bx, by = SR.var('bx by')
sage: b = vector([bx, by])
sage: a \ b
(1/2*bx - by, -bx + 5/2*by)


Note that this amounts to computing the inverse of a:

sage: a.inverse()
[1/2  -1]
[ -1 5/2]


more

Thank you but i did make a wrong use of the word solution I want to replace the x1 and x2 with numbers and use that result on something else So on the example before i want to get the result of [14, 6]

( 2019-04-17 02:03:35 -0500 )edit

Ok i also had this equation for p

p = Q*x - b


and i replace x in that equation and that it is how i got the answer that i wanted

I think that sagemath wants to use matrices with multiplication not as a place that you can throw a function

( 2019-04-17 03:21:22 -0500 )edit

Thank you, you give a good idea on how to solve my problem

( 2019-04-17 03:21:51 -0500 )edit