ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 30 Nov 2015 13:25:09 -0600Late binding and lazy symbolic thence numeric mathhttp://ask.sagemath.org/question/30833/late-binding-and-lazy-symbolic-thence-numeric-math/Being a newbie to Sage, after growing weary of wxMaxima, perhaps this is already supported but bear with me...
It seems the programming principle of "late binding" should apply, in particular, to systems in which numeric and symbolic math are seamlessly integrated.
For example, let's say I:
from scipy.constants import epsilon_0, c
...define an identity like:
mu_0*epsilon_0=1/c^2
...and then define a function, say, for the vector potential of a Hertzian dipole:
norm(x,y,z)=sqrt(x*x+y*y+z*z)
Idipole(t)=I0*cos(omega*t)
A(x,y,z,t)=[0,0,(1/(4*pi*epsilon_0*c^2)*L*Idipole(t-norm(x,y,z)/c)/norm(x,y,z))]
If (after defining other symbols) I then demand a numeric value, say, in a plot:
plot(A(0,0,z,0),z,1,4)
It should be able to, in service of this demand, lazily invoke an optimization by first solving for mu_0 thence symbolic simplification to:
A(x,y,z,t)=[0,0,(mu_0/(4*pi)*L*Idipole(t-norm(x,y,z)/c)/norm(x,y,z))]
...prior to substituting numeric values or other presumptive substitutions.
Is this kind of late binding with lazy symbolic thence numeric math supported by Sage?
Wed, 18 Nov 2015 15:10:31 -0600http://ask.sagemath.org/question/30833/late-binding-and-lazy-symbolic-thence-numeric-math/Comment by A.P. for <p>Being a newbie to Sage, after growing weary of wxMaxima, perhaps this is already supported but bear with me...</p>
<p>It seems the programming principle of "late binding" should apply, in particular, to systems in which numeric and symbolic math are seamlessly integrated.</p>
<p>For example, let's say I:</p>
<pre><code>from scipy.constants import epsilon_0, c
</code></pre>
<p>...define an identity like:</p>
<pre><code>mu_0*epsilon_0=1/c^2
</code></pre>
<p>...and then define a function, say, for the vector potential of a Hertzian dipole:</p>
<pre><code>norm(x,y,z)=sqrt(x*x+y*y+z*z)
Idipole(t)=I0*cos(omega*t)
A(x,y,z,t)=[0,0,(1/(4*pi*epsilon_0*c^2)*L*Idipole(t-norm(x,y,z)/c)/norm(x,y,z))]
</code></pre>
<p>If (after defining other symbols) I then demand a numeric value, say, in a plot:</p>
<pre><code>plot(A(0,0,z,0),z,1,4)
</code></pre>
<p>It should be able to, in service of this demand, lazily invoke an optimization by first solving for mu_0 thence symbolic simplification to:</p>
<pre><code>A(x,y,z,t)=[0,0,(mu_0/(4*pi)*L*Idipole(t-norm(x,y,z)/c)/norm(x,y,z))]
</code></pre>
<p>...prior to substituting numeric values or other presumptive substitutions.</p>
<p>Is this kind of late binding with lazy symbolic thence numeric math supported by Sage?</p>
http://ask.sagemath.org/question/30833/late-binding-and-lazy-symbolic-thence-numeric-math/?comment=31213#post-id-31213I don't understand: what does lazy evaluation have to do with symbolic simplifications? By the way, your second line of code fails in my Sage
sage: from scipy.constants import epsilon_0, c
sage: mu_0*epsilon_0=1/c^2
File "<ipython-input-2-6e0cb446ba6f>", line 1
mu_0*epsilon_0=Integer(1)/c**Integer(2)
SyntaxError: can't assign to operator
although this works
sage: mu_0 = var('mu_0')
sage: mu_0*epsilon_0==1/c^2
(8.854187817620389e-12)*mu_0 == (1.1126500560536185e-17)Mon, 30 Nov 2015 13:25:09 -0600http://ask.sagemath.org/question/30833/late-binding-and-lazy-symbolic-thence-numeric-math/?comment=31213#post-id-31213Comment by nbruin for <p>Being a newbie to Sage, after growing weary of wxMaxima, perhaps this is already supported but bear with me...</p>
<p>It seems the programming principle of "late binding" should apply, in particular, to systems in which numeric and symbolic math are seamlessly integrated.</p>
<p>For example, let's say I:</p>
<pre><code>from scipy.constants import epsilon_0, c
</code></pre>
<p>...define an identity like:</p>
<pre><code>mu_0*epsilon_0=1/c^2
</code></pre>
<p>...and then define a function, say, for the vector potential of a Hertzian dipole:</p>
<pre><code>norm(x,y,z)=sqrt(x*x+y*y+z*z)
Idipole(t)=I0*cos(omega*t)
A(x,y,z,t)=[0,0,(1/(4*pi*epsilon_0*c^2)*L*Idipole(t-norm(x,y,z)/c)/norm(x,y,z))]
</code></pre>
<p>If (after defining other symbols) I then demand a numeric value, say, in a plot:</p>
<pre><code>plot(A(0,0,z,0),z,1,4)
</code></pre>
<p>It should be able to, in service of this demand, lazily invoke an optimization by first solving for mu_0 thence symbolic simplification to:</p>
<pre><code>A(x,y,z,t)=[0,0,(mu_0/(4*pi)*L*Idipole(t-norm(x,y,z)/c)/norm(x,y,z))]
</code></pre>
<p>...prior to substituting numeric values or other presumptive substitutions.</p>
<p>Is this kind of late binding with lazy symbolic thence numeric math supported by Sage?</p>
http://ask.sagemath.org/question/30833/late-binding-and-lazy-symbolic-thence-numeric-math/?comment=30924#post-id-30924plot would normally put the to-be-plotted expression through "fast_callable", which basically constructs a straight-line program to determine numerical values of the to-be-plotted expression. In the process of constructing that straight-line-program, the constant `mu_0/(4*pi)=1/(4*pi*epsilon_0*c^2)` would get folded.
There is RealLazyField which stores enough information to first construct an expression and then evaluate it numerically to get a certain number of digits.
Python is not a lazily evaluated programming language. So any lazy evaluation functionality needs to come from specifically implemented behaviour.Fri, 20 Nov 2015 02:47:14 -0600http://ask.sagemath.org/question/30833/late-binding-and-lazy-symbolic-thence-numeric-math/?comment=30924#post-id-30924Comment by jabowery for <p>Being a newbie to Sage, after growing weary of wxMaxima, perhaps this is already supported but bear with me...</p>
<p>It seems the programming principle of "late binding" should apply, in particular, to systems in which numeric and symbolic math are seamlessly integrated.</p>
<p>For example, let's say I:</p>
<pre><code>from scipy.constants import epsilon_0, c
</code></pre>
<p>...define an identity like:</p>
<pre><code>mu_0*epsilon_0=1/c^2
</code></pre>
<p>...and then define a function, say, for the vector potential of a Hertzian dipole:</p>
<pre><code>norm(x,y,z)=sqrt(x*x+y*y+z*z)
Idipole(t)=I0*cos(omega*t)
A(x,y,z,t)=[0,0,(1/(4*pi*epsilon_0*c^2)*L*Idipole(t-norm(x,y,z)/c)/norm(x,y,z))]
</code></pre>
<p>If (after defining other symbols) I then demand a numeric value, say, in a plot:</p>
<pre><code>plot(A(0,0,z,0),z,1,4)
</code></pre>
<p>It should be able to, in service of this demand, lazily invoke an optimization by first solving for mu_0 thence symbolic simplification to:</p>
<pre><code>A(x,y,z,t)=[0,0,(mu_0/(4*pi)*L*Idipole(t-norm(x,y,z)/c)/norm(x,y,z))]
</code></pre>
<p>...prior to substituting numeric values or other presumptive substitutions.</p>
<p>Is this kind of late binding with lazy symbolic thence numeric math supported by Sage?</p>
http://ask.sagemath.org/question/30833/late-binding-and-lazy-symbolic-thence-numeric-math/?comment=30891#post-id-30891The few symbols (formal elegance) and a few arithmetic operations (computational optimization) saved by the transformation are in principle applicable to a broad range of mathematics. A bit more thought on my part might have come up with better examples to bypass your query -- a query that misses the more fundamental point about lazy evaluation (which may be thought of as subsuming late binding, demand driven computation, just in time compilation, memoization, tabling, caching/voiding, incremental view maintenance etc.).Thu, 19 Nov 2015 13:56:05 -0600http://ask.sagemath.org/question/30833/late-binding-and-lazy-symbolic-thence-numeric-math/?comment=30891#post-id-30891Comment by nbruin for <p>Being a newbie to Sage, after growing weary of wxMaxima, perhaps this is already supported but bear with me...</p>
<p>It seems the programming principle of "late binding" should apply, in particular, to systems in which numeric and symbolic math are seamlessly integrated.</p>
<p>For example, let's say I:</p>
<pre><code>from scipy.constants import epsilon_0, c
</code></pre>
<p>...define an identity like:</p>
<pre><code>mu_0*epsilon_0=1/c^2
</code></pre>
<p>...and then define a function, say, for the vector potential of a Hertzian dipole:</p>
<pre><code>norm(x,y,z)=sqrt(x*x+y*y+z*z)
Idipole(t)=I0*cos(omega*t)
A(x,y,z,t)=[0,0,(1/(4*pi*epsilon_0*c^2)*L*Idipole(t-norm(x,y,z)/c)/norm(x,y,z))]
</code></pre>
<p>If (after defining other symbols) I then demand a numeric value, say, in a plot:</p>
<pre><code>plot(A(0,0,z,0),z,1,4)
</code></pre>
<p>It should be able to, in service of this demand, lazily invoke an optimization by first solving for mu_0 thence symbolic simplification to:</p>
<pre><code>A(x,y,z,t)=[0,0,(mu_0/(4*pi)*L*Idipole(t-norm(x,y,z)/c)/norm(x,y,z))]
</code></pre>
<p>...prior to substituting numeric values or other presumptive substitutions.</p>
<p>Is this kind of late binding with lazy symbolic thence numeric math supported by Sage?</p>
http://ask.sagemath.org/question/30833/late-binding-and-lazy-symbolic-thence-numeric-math/?comment=30870#post-id-30870Do you have a reason to assume that the constant
(1/(4*pi*epsilon_0*c^2)
is better computed via the formula
(mu_0/(4*pi)
and would sage have enough information to come to that conclusion as well? With a little bit of work it should be able to let sage rewrite one into the other, but without extra information I don't think you can expect a computer algebra system to decide that such a rewrite is advantageous.Thu, 19 Nov 2015 01:14:59 -0600http://ask.sagemath.org/question/30833/late-binding-and-lazy-symbolic-thence-numeric-math/?comment=30870#post-id-30870