Ask Your Question
0

Extracting a matrix from linear expressions

asked 2013-02-12 03:06:29 +0100

Pascal Ortiz gravatar image

updated 2013-02-12 03:07:12 +0100

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 flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
2

answered 2013-02-12 06:17:18 +0100

achrzesz gravatar image
maxima('coefmatrix([a-b,b-c,c-a],[a,b,c])').sage()      
[ 1 -1  0]
[ 0  1 -1]
[-1  0  1]
edit flag offensive delete link more
0

answered 2013-02-13 21:32:49 +0100

ppurka gravatar image

updated 2013-02-13 21:34:56 +0100

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()
edit flag offensive delete link more
0

answered 2013-02-12 07:21:34 +0100

DSM gravatar image

updated 2013-02-12 07:27:46 +0100

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.

edit flag offensive delete link more

Comments

So should we add one?

kcrisman gravatar imagekcrisman ( 2013-02-12 11:16:05 +0100 )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.

ppurka gravatar imageppurka ( 2013-02-13 21:28:13 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2013-02-12 03:06:29 +0100

Seen: 482 times

Last updated: Feb 13 '13