ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 22 Jun 2017 11:36:17 +0200Define map with vector argument(s)https://ask.sagemath.org/question/38049/define-map-with-vector-arguments/(Is it possible to use LaTeX code here in order to get a nice output?)
Let's say I want to put the map phi: Q^3 x Q^3 -> Q defined by (x,y) -> x1*y1 - x2*y2 + x3*y3 into Sage. How do I do this?
I want to have later for example:
sage: x=vector((1,2,3)); y=vector((2,3,1))
sage: phi(x,y)
Then the output should be the value phi(x,y).
Is it only possible via the `def` command (with`x[1]` etc.)? Or how can you specify the domain of a map? How would you do this?Thu, 22 Jun 2017 00:14:46 +0200https://ask.sagemath.org/question/38049/define-map-with-vector-arguments/Answer by eric_g for <p>(Is it possible to use LaTeX code here in order to get a nice output?)</p>
<p>Let's say I want to put the map phi: Q^3 x Q^3 -> Q defined by (x,y) -> x1<em>y1 - x2</em>y2 + x3*y3 into Sage. How do I do this?
I want to have later for example:</p>
<pre><code>sage: x=vector((1,2,3)); y=vector((2,3,1))
sage: phi(x,y)
</code></pre>
<p>Then the output should be the value phi(x,y).</p>
<p>Is it only possible via the <code>def</code> command (with<code>x[1]</code> etc.)? Or how can you specify the domain of a map? How would you do this?</p>
https://ask.sagemath.org/question/38049/define-map-with-vector-arguments/?answer=38053#post-id-38053Assuming `phi` is bilinear, one possibility is
sage: Q3 = FiniteRankFreeModule(QQ, 3, 'Q^3', start_index=1)
sage: e = Q3.basis('e') # Q^3 canonical basis
sage: phi = Q3.tensor((0,2))
sage: phi[1,1], phi[2,2], phi[3,3] = 1, -1, 1
sage: phi
Type-(0,2) tensor on the 3-dimensional vector space Q^3 over the Rational Field
sage: phi.display()
e^1*e^1 - e^2*e^2 + e^3*e^3
sage: x = Q3((1,2,3)); x
Element of the 3-dimensional vector space Q^3 over the Rational Field
sage: y = Q3((2,3,1))
sage: phi(x,y)
-1
sage: phi(x,y) == x[1]*y[1] - x[2]*y[2] + x[3]*y[3]
TrueThu, 22 Jun 2017 11:05:57 +0200https://ask.sagemath.org/question/38049/define-map-with-vector-arguments/?answer=38053#post-id-38053Answer by dan_fulea for <p>(Is it possible to use LaTeX code here in order to get a nice output?)</p>
<p>Let's say I want to put the map phi: Q^3 x Q^3 -> Q defined by (x,y) -> x1<em>y1 - x2</em>y2 + x3*y3 into Sage. How do I do this?
I want to have later for example:</p>
<pre><code>sage: x=vector((1,2,3)); y=vector((2,3,1))
sage: phi(x,y)
</code></pre>
<p>Then the output should be the value phi(x,y).</p>
<p>Is it only possible via the <code>def</code> command (with<code>x[1]</code> etc.)? Or how can you specify the domain of a map? How would you do this?</p>
https://ask.sagemath.org/question/38049/define-map-with-vector-arguments/?answer=38054#post-id-38054The `inner_product` is the implemented method in the case of the `phi` associated to the $(1,1,1)$ diagonal matrix, for instance:
sage: x=vector((1,2,3)); y=vector((2,3,11))
sage: x.inner_product(y)
41
(That `11` instead of the one in the postshould make the result bigger and avoid "coincidences".)
An explicit check:
sage: sum( [x[k]*y[k] for k in range(len(x)) ] )
41
(The `range(len(x))` could have been simply `(0,1,2)` in this simple case.)
In the more general case the matrix giving the bilinear form can be inserted, we have in the same spirit:
sage: PHI = diagonal_matrix( 3, [1,-1,1] )
sage: x * PHI * y.column()
(29)
sage: sum( [ (-1)^k*x[k]*y[k] for k in range(len(x)) ] )
29
sage: ( x*PHI ).inner_product(y)
29
Above, `y.column()` is the column vector related to `y`.
sage: y.column()
[ 2]
[ 3]
[11]Thu, 22 Jun 2017 11:36:17 +0200https://ask.sagemath.org/question/38049/define-map-with-vector-arguments/?answer=38054#post-id-38054