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

edit retag close merge delete

## Comments

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

( 2015-11-19 01:14:59 -0500 )edit

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

( 2015-11-19 13:56:05 -0500 )edit
1

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

( 2015-11-20 02:47:14 -0500 )edit

I 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)

( 2015-11-30 13:25:09 -0500 )edit