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, 12 Nov 2020 14:27:12 +0100assignment vs. subs()https://ask.sagemath.org/question/54215/assignment-vs-subs/What am I missing? I can assign "t=H" but subs(t=H) errors out.
reset('t')
I4=4*identity_matrix(5)
t=3
var('t')
t2 = t^2 #random formula example
t=I4
display(t2,t^2)
a1=t2.subs(t=I4)Wed, 11 Nov 2020 16:49:34 +0100https://ask.sagemath.org/question/54215/assignment-vs-subs/Comment by rburing for <p>What am I missing? I can assign "t=H" but subs(t=H) errors out.</p>
<pre><code>reset('t')
I4=4*identity_matrix(5)
t=3
var('t')
t2 = t^2 #random formula example
t=I4
display(t2,t^2)
a1=t2.subs(t=I4)
</code></pre>
https://ask.sagemath.org/question/54215/assignment-vs-subs/?comment=54217#post-id-54217The assignment `t=I4` overwrites the variable `t` so that it no longer refers to a symbolic variable but rather to the concrete matrix `I4`, hence `t^2` does give the squared matrix (and no symbolic variables are used in this computation).Wed, 11 Nov 2020 17:44:13 +0100https://ask.sagemath.org/question/54215/assignment-vs-subs/?comment=54217#post-id-54217Answer by rburing for <p>What am I missing? I can assign "t=H" but subs(t=H) errors out.</p>
<pre><code>reset('t')
I4=4*identity_matrix(5)
t=3
var('t')
t2 = t^2 #random formula example
t=I4
display(t2,t^2)
a1=t2.subs(t=I4)
</code></pre>
https://ask.sagemath.org/question/54215/assignment-vs-subs/?answer=54216#post-id-54216There is no implementation for substituting a matrix into a symbolic expression, because the operation is not well-defined in general. (For example, what should happen when you substitute a matrix into `exp(-1/t)`?)
Of course it is well-defined for polynomials. This substitution *is* implemented, but only for polynomials as members of a polynomial ring (rather than symbolic expressions), so you have to do a conversion:
sage: t2.polynomial(QQ).subs(t=I4)
[16 0 0 0 0]
[ 0 16 0 0 0]
[ 0 0 16 0 0]
[ 0 0 0 16 0]
[ 0 0 0 0 16]
It is easier (in life in general) to avoid symbolic expressions altogether, and to define `t` as a generator of a polynomial ring (instead of a symbolic variable), so that substitutions into polynomials in `t` work immediately:
sage: t = polygen(QQ, name='t')
sage: t^2
t^2
sage: (t^2).subs(t=I4)
[16 0 0 0 0]
[ 0 16 0 0 0]
[ 0 0 16 0 0]
[ 0 0 0 16 0]
[ 0 0 0 0 16]Wed, 11 Nov 2020 17:33:57 +0100https://ask.sagemath.org/question/54215/assignment-vs-subs/?answer=54216#post-id-54216Comment by rrogers for <p>There is no implementation for substituting a matrix into a symbolic expression, because the operation is not well-defined in general. (For example, what should happen when you substitute a matrix into <code>exp(-1/t)</code>?)</p>
<p>Of course it is well-defined for polynomials. This substitution <em>is</em> implemented, but only for polynomials as members of a polynomial ring (rather than symbolic expressions), so you have to do a conversion:</p>
<pre><code>sage: t2.polynomial(QQ).subs(t=I4)
[16 0 0 0 0]
[ 0 16 0 0 0]
[ 0 0 16 0 0]
[ 0 0 0 16 0]
[ 0 0 0 0 16]
</code></pre>
<p>It is easier (in life in general) to avoid symbolic expressions altogether, and to define <code>t</code> as a generator of a polynomial ring (instead of a symbolic variable), so that substitutions into polynomials in <code>t</code> work immediately:</p>
<pre><code>sage: t = polygen(QQ, name='t')
sage: t^2
t^2
sage: (t^2).subs(t=I4)
[16 0 0 0 0]
[ 0 16 0 0 0]
[ 0 0 16 0 0]
[ 0 0 0 16 0]
[ 0 0 0 0 16]
</code></pre>
https://ask.sagemath.org/question/54215/assignment-vs-subs/?comment=54228#post-id-54228I should add that Jordan_form transforms of polynomials are not Taylor/polynomial representable. Which, to me, is still a mystery.Thu, 12 Nov 2020 14:27:12 +0100https://ask.sagemath.org/question/54215/assignment-vs-subs/?comment=54228#post-id-54228Comment by rrogers for <p>There is no implementation for substituting a matrix into a symbolic expression, because the operation is not well-defined in general. (For example, what should happen when you substitute a matrix into <code>exp(-1/t)</code>?)</p>
<p>Of course it is well-defined for polynomials. This substitution <em>is</em> implemented, but only for polynomials as members of a polynomial ring (rather than symbolic expressions), so you have to do a conversion:</p>
<pre><code>sage: t2.polynomial(QQ).subs(t=I4)
[16 0 0 0 0]
[ 0 16 0 0 0]
[ 0 0 16 0 0]
[ 0 0 0 16 0]
[ 0 0 0 0 16]
</code></pre>
<p>It is easier (in life in general) to avoid symbolic expressions altogether, and to define <code>t</code> as a generator of a polynomial ring (instead of a symbolic variable), so that substitutions into polynomials in <code>t</code> work immediately:</p>
<pre><code>sage: t = polygen(QQ, name='t')
sage: t^2
t^2
sage: (t^2).subs(t=I4)
[16 0 0 0 0]
[ 0 16 0 0 0]
[ 0 0 16 0 0]
[ 0 0 0 16 0]
[ 0 0 0 0 16]
</code></pre>
https://ask.sagemath.org/question/54215/assignment-vs-subs/?comment=54222#post-id-54222Okay, but I am doing things that I don't know will fit in QQ. For instance:
gp_tmp=logM(identity_matrix(dimM)-H)
gp_tmp.jordan_form(transformation=True)
Where H is lower triangular singular; (i.e.) the creation matrix. Substituting H for t in a variety of formulas; in particular "Scheffer sequence" generating functions.Wed, 11 Nov 2020 23:22:14 +0100https://ask.sagemath.org/question/54215/assignment-vs-subs/?comment=54222#post-id-54222