ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 13 Feb 2013 14:32:49 -0600Extracting a matrix from linear expressionshttp://ask.sagemath.org/question/9756/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**.
Mon, 11 Feb 2013 20:06:29 -0600http://ask.sagemath.org/question/9756/extracting-a-matrix-from-linear-expressions/Answer by DSM for <p>Suppose I have a vector depending on symbolic parameters, say <code>v=vector([a-b, b-c, c-a])</code>. How to extract the matrix naturally associated with the vector v ? I mean the matrix M</p>
<pre><code>1 -1 0
0 1 -1
1 0 -1
</code></pre>
<p>since <code>vector([a, b, c]) * M == v</code>. In fact I need the equivalent to the Maple command <strong>GenerateMatrix</strong>.</p>
http://ask.sagemath.org/question/9756/extracting-a-matrix-from-linear-expressions/?answer=14540#post-id-14540Alternatively, 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.
Tue, 12 Feb 2013 00:21:34 -0600http://ask.sagemath.org/question/9756/extracting-a-matrix-from-linear-expressions/?answer=14540#post-id-14540Comment by kcrisman for <p>Alternatively, you can use the <code>Matrix</code> constructor:</p>
<pre><code>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]
</code></pre>
<p>etc., depending on your <code>v</code>. I can't seem to find a Sage-side function which'll just do it.</p>
http://ask.sagemath.org/question/9756/extracting-a-matrix-from-linear-expressions/?comment=18245#post-id-18245So should we add one?Tue, 12 Feb 2013 04:16:05 -0600http://ask.sagemath.org/question/9756/extracting-a-matrix-from-linear-expressions/?comment=18245#post-id-18245Comment by ppurka for <p>Alternatively, you can use the <code>Matrix</code> constructor:</p>
<pre><code>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]
</code></pre>
<p>etc., depending on your <code>v</code>. I can't seem to find a Sage-side function which'll just do it.</p>
http://ask.sagemath.org/question/9756/extracting-a-matrix-from-linear-expressions/?comment=18234#post-id-18234I 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.Wed, 13 Feb 2013 14:28:13 -0600http://ask.sagemath.org/question/9756/extracting-a-matrix-from-linear-expressions/?comment=18234#post-id-18234Answer by achrzesz for <p>Suppose I have a vector depending on symbolic parameters, say <code>v=vector([a-b, b-c, c-a])</code>. How to extract the matrix naturally associated with the vector v ? I mean the matrix M</p>
<pre><code>1 -1 0
0 1 -1
1 0 -1
</code></pre>
<p>since <code>vector([a, b, c]) * M == v</code>. In fact I need the equivalent to the Maple command <strong>GenerateMatrix</strong>.</p>
http://ask.sagemath.org/question/9756/extracting-a-matrix-from-linear-expressions/?answer=14539#post-id-14539 maxima('coefmatrix([a-b,b-c,c-a],[a,b,c])').sage()
[ 1 -1 0]
[ 0 1 -1]
[-1 0 1]
Mon, 11 Feb 2013 23:17:18 -0600http://ask.sagemath.org/question/9756/extracting-a-matrix-from-linear-expressions/?answer=14539#post-id-14539Answer by ppurka for <p>Suppose I have a vector depending on symbolic parameters, say <code>v=vector([a-b, b-c, c-a])</code>. How to extract the matrix naturally associated with the vector v ? I mean the matrix M</p>
<pre><code>1 -1 0
0 1 -1
1 0 -1
</code></pre>
<p>since <code>vector([a, b, c]) * M == v</code>. In fact I need the equivalent to the Maple command <strong>GenerateMatrix</strong>.</p>
http://ask.sagemath.org/question/9756/extracting-a-matrix-from-linear-expressions/?answer=14548#post-id-14548By 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()
Wed, 13 Feb 2013 14:32:49 -0600http://ask.sagemath.org/question/9756/extracting-a-matrix-from-linear-expressions/?answer=14548#post-id-14548