Loading [MathJax]/jax/output/HTML-CSS/jax.js

First time here? Check out the FAQ!

Ask Your Question
0

Extracting a matrix from linear expressions

asked 12 years ago

Pascal Ortiz gravatar image

updated 12 years ago

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.

Preview: (hide)

3 Answers

Sort by » oldest newest most voted
2

answered 12 years ago

achrzesz gravatar image
maxima('coefmatrix([a-b,b-c,c-a],[a,b,c])').sage()      
[ 1 -1  0]
[ 0  1 -1]
[-1  0  1]
Preview: (hide)
link
0

answered 12 years ago

ppurka gravatar image

updated 12 years ago

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()
Preview: (hide)
link
0

answered 12 years ago

DSM gravatar image

updated 12 years ago

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.

Preview: (hide)
link

Comments

So should we add one?

kcrisman gravatar imagekcrisman ( 12 years ago )
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 ( 12 years ago )

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: 12 years ago

Seen: 545 times

Last updated: Feb 13 '13