Ask Your Question

Revision history [back]

Since you know that the variables are x and y, grab their coefficients in each entry of A.

Use list comprehension to build a list of lists out of that.

Then create a matrix from this list of list.

The variables:

sage: x, y = SR.var('x, y')

The matrix A:

sage: A = matrix([[x], [x - 2*y]])
sage: A
[      x]
[x - 2*y]

The vector of variables:

sage: b = matrix([[x], [y]])

The matrix of coefficients:

sage: C = matrix([[e.coefficient(x), e.coefficient(y)] for row in A for e in row])

Check:

sage: C, b, C*b, A
(
[ 1  0]  [x]  [      x]  [      x]
[ 1 -2], [y], [x - 2*y], [x - 2*y]
)