# 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?

edit retag close merge delete

Sort by ยป oldest newest most voted

The 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.

more