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.Sat, 24 Aug 2013 03:58:41 +0200arithmetic with matrices of formal functionshttps://ask.sagemath.org/question/10457/arithmetic-with-matrices-of-formal-functions/If I have two plain formal functions, like
sage: var('q k')
sage: function('u')
sage: function('v')
it seems I can always get more 'involved' formal functions, like the following h(k,q), by symbolic operations
sage: h(k,q)=u(k)*v(q)
sage: h(17,sin(k))
u(17)*v(sin(k)) # OK.
My naive hope was, that this would straightforwardly apply also to matrix construction from formal functions, like so
sage: B(k,q) = matrix([[u(k),v(q)],[v(q),u(k)]])
# but
sage: B(17,sin(q))
[u(k) v(q)]
[v(q) u(k)] # :(
So, B(k,q) does not substitute the variables. After some attempts, it seems that this is a valid way to construct a callable symbolic matrix from formal functions
sage: B=matrix(CallableSymbolicExpressionRing((k,q)),[[u(k),v(q)],[v(q),u(k)]])
# at least
sage: B(x^2,cos(q))
[ u(x^2) v(cos(q))]
[v(cos(q)) u(x^2)] # OK.
My **1st question** is, if there is not a 'less cryptic' way to construct B from u and v?
My **2nd question** concerns doing matrix arithmetic with B. If I try to do something similar to that for the plain formal function arithmetic for h(k,q) with my formal matrix function B it 'does not work'. Eg.:
sage: H(k,q)=B(k,q)*B(q,q)
sage: H(sin(17),pi)
[ u(k)*u(q) + v(q)^2 u(k)*v(q) + u(q)*v(q)]
[u(k)*v(q) + u(q)*v(q) u(k)*u(q) + v(q)^2] # :(
I.e., again, upon matrix multiplication, the object B(k,q)*B(q,q) seems to not substitute the arguments into the formal functions anymore.
However, if I do
sage: H=matrix(CallableSymbolicExpressionRing((k,q)),B(k,q)*B(q,q))
Then
sage: H(sin(17),pi)
[ u(pi)*u(sin(17)) + v(pi)^2 u(pi)*v(pi) + u(sin(17))*v(pi)]
[u(pi)*v(pi) + u(sin(17))*v(pi) u(pi)*u(sin(17)) + v(pi)^2] # OK.
But it seems awkward if one would have to do matrix arithmetic like that. So, how does one do proper matrix operations with matrices made from formal functions?
Mon, 19 Aug 2013 15:31:38 +0200https://ask.sagemath.org/question/10457/arithmetic-with-matrices-of-formal-functions/Answer by nbruin for <p>If I have two plain formal functions, like</p>
<pre><code>sage: var('q k')
sage: function('u')
sage: function('v')
</code></pre>
<p>it seems I can always get more 'involved' formal functions, like the following h(k,q), by symbolic operations</p>
<pre><code>sage: h(k,q)=u(k)*v(q)
sage: h(17,sin(k))
u(17)*v(sin(k)) # OK.
</code></pre>
<p>My naive hope was, that this would straightforwardly apply also to matrix construction from formal functions, like so</p>
<pre><code>sage: B(k,q) = matrix([[u(k),v(q)],[v(q),u(k)]])
# but
sage: B(17,sin(q))
[u(k) v(q)]
[v(q) u(k)] # :(
</code></pre>
<p>So, B(k,q) does not substitute the variables. After some attempts, it seems that this is a valid way to construct a callable symbolic matrix from formal functions</p>
<pre><code>sage: B=matrix(CallableSymbolicExpressionRing((k,q)),[[u(k),v(q)],[v(q),u(k)]])
# at least
sage: B(x^2,cos(q))
[ u(x^2) v(cos(q))]
[v(cos(q)) u(x^2)] # OK.
</code></pre>
<p>My <strong>1st question</strong> is, if there is not a 'less cryptic' way to construct B from u and v?</p>
<p>My <strong>2nd question</strong> concerns doing matrix arithmetic with B. If I try to do something similar to that for the plain formal function arithmetic for h(k,q) with my formal matrix function B it 'does not work'. Eg.:</p>
<pre><code>sage: H(k,q)=B(k,q)*B(q,q)
sage: H(sin(17),pi)
[ u(k)*u(q) + v(q)^2 u(k)*v(q) + u(q)*v(q)]
[u(k)*v(q) + u(q)*v(q) u(k)*u(q) + v(q)^2] # :(
</code></pre>
<p>I.e., again, upon matrix multiplication, the object B(k,q)*B(q,q) seems to not substitute the arguments into the formal functions anymore.
However, if I do</p>
<pre><code>sage: H=matrix(CallableSymbolicExpressionRing((k,q)),B(k,q)*B(q,q))
</code></pre>
<p>Then</p>
<pre><code>sage: H(sin(17),pi)
[ u(pi)*u(sin(17)) + v(pi)^2 u(pi)*v(pi) + u(sin(17))*v(pi)]
[u(pi)*v(pi) + u(sin(17))*v(pi) u(pi)*u(sin(17)) + v(pi)^2] # OK.
</code></pre>
<p>But it seems awkward if one would have to do matrix arithmetic like that. So, how does one do proper matrix operations with matrices made from formal functions? </p>
https://ask.sagemath.org/question/10457/arithmetic-with-matrices-of-formal-functions/?answer=15373#post-id-15373The problem arises from the fact that matrices with symbolic expressions are not "symbolic expressions" themselves:
sage: B = matrix([[u(k),v(q)],[v(q),u(k)]])
sage: type(B)
sage.matrix.matrix_symbolic_dense.Matrix_symbolic_dense
sage: type(u)
sage.symbolic.function_factory.NewSymbolicFunction
sage: type(u(q))
sage.symbolic.expression.Expression
and hence, trying to turn it into a CallableSymbolicExpression by itself fails.
Possibly the easiest thing is to just live with `B`. You can still substitute values, as long as you're explicit about the substitutions:
sage: B(k=17,q=sin(q))
[ u(17) v(sin(q))]
[v(sin(q)) u(17)]
The nice thing is that you can do matrix algebra with these:
sage: H=B(k=k,q=q)*B(k=q,q=q)
sage: H
[ u(k)*u(q) + v(q)^2 u(k)*v(q) + u(q)*v(q)]
[u(k)*v(q) + u(q)*v(q) u(k)*u(q) + v(q)^2]
sage: H(k=sin(17),q=pi)
[ u(pi)*u(sin(17)) + v(pi)^2 u(pi)*v(pi) + u(sin(17))*v(pi)]
[u(pi)*v(pi) + u(sin(17))*v(pi) u(pi)*u(sin(17)) + v(pi)^2]
Presently, "callable" symbolic expressions and "symbolic" matrices are two extensions built on top of symbolic expressions and they don't mix very well.Sat, 24 Aug 2013 03:58:41 +0200https://ask.sagemath.org/question/10457/arithmetic-with-matrices-of-formal-functions/?answer=15373#post-id-15373