ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 25 Jun 2020 12:42:38 -0500Multiplying a vector to a listhttps://ask.sagemath.org/question/52213/multiplying-a-vector-to-a-list/Let's say I have a list of forms given by
U = Manifold(4, 'U')
X.<x, y, z, w> = U.chart()
f = U.diff_form(3, 'f')
f[0, 1, 2] = x^2
g = U.diff_form(3, 'g')
g[0, 1, 2] = y^2
h = U.diff_form(3, 'g')
h[0, 1, 2] = z^3
List = [f, g, h]
So I have a list of 3 elements, each a 3-form.
Let's assume I have a list of vectors given by
ListofVectors = [(0, 1, 1), (0, 2, 1), (1, 2, 3)]
I want to do a dot product but the problem is Sage won't
let me make `List` into a vector so I can do like
List.dot_product(ListofVectors[i]) for i < len(ListforVectors)
Basically, I want a new list with `[g + h, 2*h + g, f + 2*g + 3*h]`. Is there a way to dot product a list of forms with a list of vectors? Again, I think the main issue is I can't turn List into a vector for the dot product function to make sense.Wed, 24 Jun 2020 19:16:50 -0500https://ask.sagemath.org/question/52213/multiplying-a-vector-to-a-list/Answer by John Palmieri for <p>Let's say I have a list of forms given by</p>
<pre><code>U = Manifold(4, 'U')
X.<x, y, z, w> = U.chart()
f = U.diff_form(3, 'f')
f[0, 1, 2] = x^2
g = U.diff_form(3, 'g')
g[0, 1, 2] = y^2
h = U.diff_form(3, 'g')
h[0, 1, 2] = z^3
List = [f, g, h]
</code></pre>
<p>So I have a list of 3 elements, each a 3-form.</p>
<p>Let's assume I have a list of vectors given by</p>
<pre><code>ListofVectors = [(0, 1, 1), (0, 2, 1), (1, 2, 3)]
</code></pre>
<p>I want to do a dot product but the problem is Sage won't
let me make <code>List</code> into a vector so I can do like</p>
<pre><code>List.dot_product(ListofVectors[i]) for i < len(ListforVectors)
</code></pre>
<p>Basically, I want a new list with <code>[g + h, 2*h + g, f + 2*g + 3*h]</code>. Is there a way to dot product a list of forms with a list of vectors? Again, I think the main issue is I can't turn List into a vector for the dot product function to make sense.</p>
https://ask.sagemath.org/question/52213/multiplying-a-vector-to-a-list/?answer=52215#post-id-52215The command
S = [sum(a*b for (a,b) in zip(List, v)) for v in ListofVectors]
will return what you want, although it won't be printed how you want:
sage: S
[3-form on the 4-dimensional differentiable manifold U,
3-form on the 4-dimensional differentiable manifold U,
3-form on the 4-dimensional differentiable manifold U]
sage: S[0] == g + h
True
sage: S[1] == 2*g + h
TrueWed, 24 Jun 2020 21:04:51 -0500https://ask.sagemath.org/question/52213/multiplying-a-vector-to-a-list/?answer=52215#post-id-52215Comment by whatupmatt for <p>The command</p>
<pre><code>S = [sum(a*b for (a,b) in zip(List, v)) for v in ListofVectors]
</code></pre>
<p>will return what you want, although it won't be printed how you want:</p>
<pre><code>sage: S
[3-form on the 4-dimensional differentiable manifold U,
3-form on the 4-dimensional differentiable manifold U,
3-form on the 4-dimensional differentiable manifold U]
sage: S[0] == g + h
True
sage: S[1] == 2*g + h
True
</code></pre>
https://ask.sagemath.org/question/52213/multiplying-a-vector-to-a-list/?comment=52222#post-id-52222I think from my previous questions, the command `show(LatexExpr('[' + ','.join(latex(f.display()) for f in S) + ']'))` works.Thu, 25 Jun 2020 12:42:38 -0500https://ask.sagemath.org/question/52213/multiplying-a-vector-to-a-list/?comment=52222#post-id-52222Comment by Florentin Jaffredo for <p>The command</p>
<pre><code>S = [sum(a*b for (a,b) in zip(List, v)) for v in ListofVectors]
</code></pre>
<p>will return what you want, although it won't be printed how you want:</p>
<pre><code>sage: S
[3-form on the 4-dimensional differentiable manifold U,
3-form on the 4-dimensional differentiable manifold U,
3-form on the 4-dimensional differentiable manifold U]
sage: S[0] == g + h
True
sage: S[1] == 2*g + h
True
</code></pre>
https://ask.sagemath.org/question/52213/multiplying-a-vector-to-a-list/?comment=52219#post-id-52219For a more Sage oriented way, I tried to build the module generated by `f`, `g` and `h`. using `U.diff_form_module(3).basis('e', from_family=[f, g, h])`, but it returns a `KeyError` in a cached method. Is it not a valid approach?Thu, 25 Jun 2020 06:32:23 -0500https://ask.sagemath.org/question/52213/multiplying-a-vector-to-a-list/?comment=52219#post-id-52219