MathematicalFunctions[Evalf]
Add
numerically evaluate a formula depending on a parameter n, or two of them n and m, adding from n = 0, also from m = 0 if there are two parameters, until the result converges for the current value of Digits
Calling Sequence
Parameters
Description
Examples
Compatibility
Add(formula_procedure, N)
formula_procedure
-
procedure depending on one or two parameters - say n or n and m - such that when they assume non-negative values the formula can be numerically evaluated
N
(optional) range of non-negative integers, or just a non-negative integer, indicating the summation range or the upper limit of a sum that starts at 0, for one or the two parameters n and m. The default is infinity.
The Add command is used to numerically add a formula, that is passed as a procedure that depends on a single non-negative integer parameter, say n, a summation index, or two of them, say n and m. The summation over these parameters starts at 0 and is performed until the result converges (up to Digits). If the formula does not converge, the sum is interrupted when the number of terms added reaches 10000⁢Digits, in which case a WARNING message explaining the situation is displayed on the screen.
When a second argument N is passed, if it is a non-negative integer, the summation, that starts at n=0, or n=0 and m=0 in the case of two parameters, is performed until the result converges (up to Digits) or n=N. This second argument can also be passed as a range, say n0..nf, in which case the summation will start at n0 until the result converges or nf terms were added.
The case of two parameters n and m is handled by rewriting the double sum from 0 to ∞ as a single sum in only one parameter k from 0 to ∞ according to
∑n=0∞⁡∑m=0∞⁡f⁡m,n=∑k=0∞⁡∑m=0k⁡f⁡k,m+∑n=0k−1⁡f⁡n,k
Add uses option hfloat, which means that, depending on the mathematical contents of the formula_procedure passed, the numerical evaluation will be performed using hardware floating-point values. Depending on the operations performed, this can significantly speed up the execution of the procedure.
Add is mainly useful to numerically evaluate mathematical expressions or functions that can be represented as an infinite sum. In these cases, with Add you can do:
a fast implementation of a numerical evaluation procedure for the expression (see the Examples section);
fast experimentation with different formulas, for instance comparing their performance for numerically evaluating the expression.
Initialization: Load the package and set the display of special functions in output to typeset mathematical notation (textbook notation):
withMathematicalFunctions:-Evalf;Typesetting:-EnableTypesetRuleTypesetting:-SpecialFunctionRules:
Add,Evalb,Zoom,QuadrantNumbers,Singularities,GenerateRecurrence,PairwiseSummation
Basically all mathematical functions can be represented by a infinite sum. You can query about this sum form of a mathematical function using the FunctionAdvisor. As an example, consider the infinite sum representation for the spherical harmonic SphericalY function
FunctionAdvisorsum, SphericalYlambda, mu, theta, phi
* Partial match of "sum" against topic "sum_form".
Yλμ⁡θ,φ=−1μ⁢2⁢λ+1π⁢λ−μ!⁢ⅇI⁢φ⁢μ⁢cos⁡θ+1μ2⁢∑_k1=0∞⁡−λ_k1⁢λ+1_k1⁢12−cos⁡θ2_k1_k1!⁢1−μ_k12⁢λ+μ!⁢cos⁡θ−1μ2⁢Γ⁡1−μ,¬λ+μ::ℤ−∧¬λ−μ::ℤ−∧¬1−μ::ℤ0,−
How could we use this formula to numerically evaluate the function, for example for λ=12,μ=32+I,φ=π7,θ=13+I5? First put all the operands within the summation
combine⁡op⁡1,
Yλμ⁡θ,φ=∑_k1=0∞⁡λ−μ!⁢cos⁡θ+1μ2⁢cos⁡θ−1−μ2⁢2⁢λ+1π⁢ⅇI⁢φ⁢μ⁢−1μ⁢−λ_k1⁢λ+1_k1⁢12−cos⁡θ2_k12⁢λ+μ!⁢Γ⁡1−μ⁢_k1!⁢1−μ_k1
Take now the summand and substitute in it the values for the function's parameters indicated
summand≔op⁡1,rhs⁡
summand≔λ−μ!⁢cos⁡θ+1μ2⁢cos⁡θ−1−μ2⁢2⁢λ+1π⁢ⅇI⁢φ⁢μ⁢−1μ⁢−λ_k1⁢λ+1_k1⁢12−cos⁡θ2_k12⁢λ+μ!⁢Γ⁡1−μ⁢_k1!⁢1−μ_k1
subslambda = 12, mu = 32+I, phi =Pi7, theta = 13+I5, summand
−1−I!⁢cos⁡13+I5+134+I2⁢cos⁡13+I5−1−34−I2⁢2⁢1π⁢ⅇ−17+3⁢I14⁢π⁢−132+I⁢−12_k1⁢32_k1⁢12−cos⁡13+I52_k12⁢2+I!⁢Γ⁡−12−I⁢_k1!⁢−12−I_k1
Construct a formula_procedure with this summand as a function of the summation index _k1
formula_procedure≔unapply⁡,_k1
formula_procedure≔_k1↦−1−I!⁢cos⁡13+I5+134+I2⁢cos⁡13+I5−1−34−I2⁢2⁢1π⁢ⅇ−17+3⁢I14⁢π⁢−132+I⁢−12_k1⁢32_k1⁢12−cos⁡13+I52_k12⁢2+I!⁢Γ⁡−12−I⁢_k1!⁢−12−I_k1
You can now numerically evaluate SphericalY⁡12,32+I,13+I5,π7
Add⁡formula_procedure
0.05489806051−0.01484631132⁢I
To transform this formula into a procedure for the numerical evaluation of SphericalY for arbitrary arguments, construct with the summand a generic formula, depending on all the parameters, and as a function of the summation index, then use the extension mechanism of evalf, so define `evalf/Y` as shown below, where Y will represent the spherical harmonics function
general_formula≔unapply⁡summand,_k1
general_formula≔_k1↦λ−μ!⁢cos⁡θ+1μ2⁢cos⁡θ−1−μ2⁢2⁢λ+1π⁢ⅇI⁢φ⁢μ⁢−1μ⁢−λ_k1⁢λ+1_k1⁢12−cos⁡θ2_k12⁢λ+μ!⁢Γ⁡1−μ⁢_k1!⁢1−μ_k1
`evalf/Y` ≔ lambda, mu, theta, phi → Addsubs':-lambda = lambda, :-mu = mu, :-theta = theta, :-phi = phi', evalgeneral_formula
evalf/Y≔λ,μ,θ,φ↦Add⁡subs⁡:−λ=λ,:−μ=μ,:−θ=θ,:−φ=φ,eval⁡general_formula
Note the use of :-, necessary for the replacement to happen within general_formula. Alternatively, you could also write `evalf/Y` with the contents of general_formula explicitly present and in that way avoid entirely the need of the substitution of parameters by their actual values. Now, if you have not assigned a procedure to Y, it just echoes the function on the screen (i.e. the function returns unevaluated)
Y12, 32+I, 13+I5, Pi7
Y⁡12,32+I,13+I5,π7
But because of having assigned evalf/Y, however, you can numerically evaluate this function using evalf
evalf⁡
A plot of the real part of Y for θ from 0 to π
plotReY12, 32+I, 13+I5, theta, theta = 0 .. Pi
Compare with the same plot constructed using the standard Maple SphericalY function
plotReSphericalY12, 32+I, 13+I5, theta, theta = 0 .. Pi
As an example using an optional argument to specify the upper limit of the summation, consider the sum forms for the exponential-integral Ei function
FunctionAdvisorsum, Eia, z
Eia⁡z=∑_k1=0∞⁡−z_k1Γ⁡_k1+1⁢−1+a−_k1+za−1⁢Γ⁡1−a,a::¬ℤ+,Eia⁡z=∑_k1=0∞⁡−1a+2⁢_k1⁢−Ψ⁡_k1+1+ln⁡z⁢za−1+_k1Γ⁡_k1+1⁢Γ⁡a⁢ⅇz+∑_k1=0−2+a⁡−1_k1⁢Γ⁡−1+a−_k1⁢z_k1Γ⁡a⁢ⅇz,a::ℤ+∧z≠0,Eia⁡z=∑_k1=0−a⁡−a!⁢za−1+_k1ⅇz⁢_k1!,a::ℤ−∧z≠0
Get the summand of the last sum in one go
summand≔op1,rhs−11
summand≔−a!⁢za−1+_k1ⅇz⁢_k1!
Construct now a general formula-procedure as a function of the summation index, then a numerical evaluation procedure as done above for the function Y representing the spherical harmonics; call EI this function representing the exponential-integral
general_formula≔_k1↦−a!⁢za−1+_k1ⅇz⁢_k1!
`evalf/EI` ≔ a,z→Add⁡subs⁡':-a=a,:-z=z',eval⁡general_formula
evalf/EI≔a,z↦Add⁡subs⁡:−a=a,:−z=z,eval⁡general_formula
Note that the sum form we are using requires a to be a negative integer and also that the upper summation limit is equal to −a. In the `evalf/EI` procedure above, however, we have not specified either of these things. So, the numerical evaluation will be performed adding terms until the result converges. To see how many, turn on the infolevel for Add
infolevelAdd ≔ 5
infolevelAdd≔5
Enter now the EI function with some parameters and evaluate it numerically using evalf
EI⁡−4,2150−3⁢I10
-> entering Add with: 'formula' = 24/exp(21/50-3/10*I)/k!*(21/50-3/10*I)^(-5+k) <- exiting Add with -654.669541617+26.4271294535*I; after adding 17 terms
−654.6695416+26.42712945⁢I
We see that not just 5 but 14 terms were added and at that point the result already converged. Check the value using the Maple standard Ei function
Ei−4,2150−3 I10
Ei−4⁡2150−3⁢I10
−654.8063012+26.39258365⁢I
The two results obtained are close to each other but the level of accuracy is poor. The formula, in fact, for a=−4 is valid if we add only 5 terms. Redefine now `evalf/Ei` taking into account both that it expects a to be a negative integer and also that the upper summation limit is equal to −a
`evalf/EI` ≔ a::negint,z→Add⁡subs⁡':-a=a,:-z=z',eval⁡general_formula,−a
evalf/EI≔a::ℤ−,z↦Add⁡subs⁡:−a=a,:−z=z,eval⁡general_formula,−a
Clear the remember table of evalf and compute the value of Ei(-4, 21/50 - 3*I/10) again, checking the number of terms used: for a=−4 we expect 5 terms added
forget⁡evalf
-> entering Add with: 'formula' = 24/exp(21/50-3/10*I)/k!*(21/50-3/10*I)^(-5+k)
<- exiting Add with -654.806301169+26.3925836556*I; after adding 5 terms
−654.8063012+26.39258366⁢I
So now only 5 terms got added, as expected, and the accuracy of the result is much better. Also, if we pass a value of a that is not a negative integer, for instance a=1, the numerical evaluation is halted with an error message
evalf⁡EI⁡1,2150−3⁢I10
Error, invalid input: `evalf/EI` expects its 1st argument, a, to be of type negint, but received 1
As an example of a double sum, consider the AppellF2 function
FunctionAdvisordefinition, AppellF2a, b1, b2, c1, c2, z1, z2
F2⁡a,b1,b2,c1,c2,z1,z2=∑_k1=0∞⁡∑_k2=0∞⁡a_k1+_k2⁢b1_k1⁢b2_k2⁢z1_k1⁢z2_k2c1_k1⁢c2_k2⁢_k1!⁢_k2!,z1+z2<1
Following the same steps outlined before, get the summand and construct a formula-procedure this time depending on 2 parameters m and n
summand≔op⁡1,1,rhs⁡1
summand≔a_k1+_k2⁢b1_k1⁢b2_k2⁢z1_k1⁢z2_k2c1_k1⁢c2_k2⁢_k1!⁢_k2!
general_formula≔unapply⁡summand,_k1,_k2
general_formula≔_k1,_k2↦a_k1+_k2⁢b1_k1⁢b2_k2⁢z1_k1⁢z2_k2c1_k1⁢c2_k2⁢_k1!⁢_k2!
from where a routine for the numerical evaluation of AppellF2, when the condition z1+z2<1 shown in the definition is satisfied, is
`evalf/AF2` ≔ a,b1,b2,c1,c2,z1,z2→Add⁡subs⁡':-a=a,:-b1=b1,:-b2=b2,:-c1=c1,:-c2=c2,:-z1=z1,:-z2=z2',eval⁡general_formula
evalf/AF2≔a,b1,b2,c1,c2,z1,z2↦Add⁡subs⁡:−a=a,:−b1=b1,:−b2=b2,:−c1=c1,:−c2=c2,:−z1=z1,:−z2=z2,eval⁡general_formula
where we are calling AF2 this representation of AppellF2. Consider now for instance some numerical complex values of the parameters a,b1,b2,c1,c2,z1,z2 and numerically evaluate this AF2 representation at those values
AF2⁡12,27,−14,2,32+I,14⁢I,14
AF2⁡12,27,−14,2,32+I,I4,14
-> entering Add with: 'formula' = add(pochhammer(1/2,k+m)*pochhammer(2/7,k)*pochhammer(-1/4,m)*(1/4*I)^k*(1/4)^m/pochhammer(2,k)/pochhammer(3/2+I,m)/k!/m!,m = 0 .. k)+add(pochhammer(1/2,n+k)*pochhammer(2/7,n)*pochhammer(-1/4,k)*(1/4*I)^n*(1/4)^k/pochhammer(2,n)/pochhammer(3/2+I,k)/n!/k!,n = 0 .. k-1)
<- exiting Add with .983152249087+.272658733829e-1*I; after adding 19 terms
0.9831522491+0.02726587338⁢I
Compare with the numerical value obtained using the standard Maple AppellF2 function
subs⁡AF2=AppellF2,
F2⁡12,27,−14,2,32+I,I4,14
The MathematicalFunctions[Evalf][Add] command was introduced in Maple 2017.
For more information on Maple 2017 changes, see Updates in Maple 2017.
See Also
Ei
evalf
Evalf command
Evalf package
forget
infolevel
MathematicalFunctions
plot
remember
SphericalY
unapply
Download Help Document