# Extracting a matrix from linear expressions

Suppose I have a vector depending on symbolic parameters, say v=vector([a-b, b-c, c-a]). How to extract the matrix naturally associated with the vector v ? I mean the matrix M

1 -1 0
0 1 -1
1 0 -1


since vector([a, b, c]) * M == v. In fact I need the equivalent to the Maple command GenerateMatrix.

edit retag close merge delete

Sort by ยป oldest newest most voted
maxima('coefmatrix([a-b,b-c,c-a],[a,b,c])').sage()
[ 1 -1  0]
[ 0  1 -1]
[-1  0  1]

more

By the way, you can do this in one go by defining a symbolic function.

sage: f(a,b,c) = [a-b, b-c, c-a]
sage: f.derivative()
[ (a, b, c) |--> 1 (a, b, c) |--> -1  (a, b, c) |--> 0]
[ (a, b, c) |--> 0  (a, b, c) |--> 1 (a, b, c) |--> -1]
[(a, b, c) |--> -1  (a, b, c) |--> 0  (a, b, c) |--> 1]
sage: f.derivative()(a,b,c)
[ 1 -1  0]
[ 0  1 -1]
[-1  0  1]
sage: type(_)
<type 'sage.matrix.matrix_symbolic_dense.Matrix_symbolic_dense'>


If you started out with a vector in your computations then you can get a list by doing this

sage: a,b,c = var('a,b,c')
sage: v = vector([a-b, b-c, c-a])
sage: f(a,b,c) = v.list()

more

Alternatively, you can use the Matrix constructor:

sage: s = var("a b c")
sage: v = vector([a-b, b-c, c-a])
sage: Matrix(QQ, [v.derivative(x) for x in (a,b,c)]).T
[ 1 -1  0]
[ 0  1 -1]
[-1  0  1]
sage: Matrix(ZZ, 3, lambda i, j: v[i].coeff(s[j]))
[ 1 -1  0]
[ 0  1 -1]
[-1  0  1]


etc., depending on your v. I can't seem to find a Sage-side function which'll just do it.

more

( 2013-02-12 11:16:05 +0200 )edit
1

I don't think we should add one *unless* somewhere *Linear Expressions* are defined as an object in Sage. This makes sense only for linear expressions.

( 2013-02-13 21:28:13 +0200 )edit