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
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
(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
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):
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".
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
Take now the summand and substitute in it the values for the function's parameters indicated
subslambda = 12, mu = 32+I, phi =Pi7, theta = 13+I5, summand
Construct a formula_procedure with this summand as a function of the summation index _k1
You can now numerically evaluate SphericalY⁡12,32+I,13+I5,π7
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
`evalf/Y` ≔ lambda, mu, theta, phi → Addsubs':-lambda = lambda, :-mu = mu, :-theta = theta, :-phi = phi', evalgeneral_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
But because of having assigned evalf/Y, however, you can numerically evaluate this function using 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
Get the summand of the last sum in one go
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
`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
Enter now the EI function with some parameters and evaluate it numerically using evalf
-> 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
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
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
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
-> 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
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
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
Following the same steps outlined before, get the summand and construct a formula-procedure this time depending on 2 parameters m and n
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
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
-> 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
Compare with the numerical value obtained using the standard Maple AppellF2 function
The MathematicalFunctions[Evalf][Add] command was introduced in Maple 2017.
For more information on Maple 2017 changes, see Updates in Maple 2017.
Download Help Document
What kind of issue would you like to report? (Optional)