MathematicalFunctions[Evalf] - Maple Programming Help

Online Help

All Products    Maple    MapleSim


Home : Support : Online Help : Mathematics : Special Functions : MathematicalFunctions : Evalf : MathematicalFunctions/Evalf/Add

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

Calling Sequence

Add(formula_procedure, N)

Parameters

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.

Description

• 

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 10000Digits, 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=0m=0fm,n=k=0m=0kfk,m+n=0k1fn,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.

Examples

  

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

(1)

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_k112cosθ2_k1_k1!1μ_k12λ+μ!cosθ1μ2Γ1μ,¬λ+μ::'negint'¬λμ::'negint'¬1μ::'nonposint'

(2)

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

combineop1,

Yλμθ,φ=_k1=0λμ!cosθ+1μ2cosθ1μ22λ+1πⅇIφμ−1μλ_k1λ+1_k112cosθ2_k12λ+μ!Γ1μ_k1!1μ_k1

(3)

Take now the summand and substitute in it the values for the function's parameters indicated

summandop1,rhs

summandλμ!cosθ+1μ2cosθ1μ22λ+1πⅇIφμ−1μλ_k1λ+1_k112cosθ2_k12λ+μ!Γ1μ_k1!1μ_k1

(4)

subslambda = 12, mu = 32+I, phi =Pi7, theta = 13+I5, summand

−1I!cos13+I5+134+I2cos13+I5134I221πⅇ17+3I14π−132+I12_k132_k112cos13+I52_k122+I!Γ12I_k1!12I_k1

(5)

Construct a formula_procedure with this summand as a function of the summation index _k1

formula_procedureunapply,_k1

formula_procedure_k1−1I!cos13+I5+134+I2cos13+I5134I221πⅇ17+3I14π−132+I12_k132_k112cos13+I52_k122+I!Γ12I_k1!12I_k1

(6)

You can now numerically evaluate SphericalY12,32+I,13+I5,π7

Addformula_procedure

0.054898060510.01484631132I

(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

general_formulaunapplysummand,_k1

general_formula_k1λμ!cosθ+1μ2cosθ1μ22λ+1πⅇIφμ−1μλ_k1λ+1_k112cosθ2_k12λ+μ!Γ1μ_k1!1μ_k1

(8)

`evalf/Y`  lambda, mu, theta, phi  Addsubs':-lambda = lambda, :-mu = mu, :-theta = theta, :-phi = phi', evalgeneral_formula

evalf/Yλ,μ,θ,φAddsubs:−λ=λ,:−μ=μ,:−θ=θ,:−φ=φ,evalgeneral_formula

(9)

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

Y12,32+I,13+I5,π7

(10)

But because of having assigned evalf/Y, however, you can numerically evaluate this function using evalf

evalf

0.054898060510.01484631132I

(11)

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

* Partial match of "sum" against topic "sum_form".

Eiaz=_k1=0z_k1Γ_k1+11+a_k1+za1Γ1a,a::¬'posint',Eiaz=_k1=0−1a+2_k1Ψ_k1+1+lnzza1+_k1Γ_k1+1Γaⅇz+_k1=02+a−1_k1Γ1+a_k1z_k1Γaⅇz,a::'posint'z0,Eiaz=_k1=0aa!za1+_k1ⅇz_k1!,a::'negint'z0

(12)

Get the summand of the last sum in one go

summandop1,rhs11

summanda!za1+_k1ⅇz_k1!

(13)

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_formulaunapplysummand,_k1

general_formula_k1a!za1+_k1ⅇz_k1!

(14)

`evalf/EI`a,z→Addsubs':-a=a,:-z=z',evalgeneral_formula

evalf/EIa,zAddsubs:−a=a,:−z=z,evalgeneral_formula

(15)

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

infolevelAdd5

(16)

Enter now the EI function with some parameters and evaluate it numerically using evalf

EI4,21503I10

EI−4,21503I10

(17)

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

−654.6695416+26.42712945I

(18)

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

Ei4&comma;21503 I10

Ei−421503I10

(19)

evalf

−654.8063012+26.39258365I

(20)

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&comma;z&rarr;Addsubs&apos;:-a&equals;a&comma;:-z&equals;z&apos;&comma;evalgeneral_formula&comma;a

evalf/EIa::&apos;negint&apos;&comma;zAddsubs:−a=a&comma;:−z=z&comma;evalgeneral_formula&comma;a

(21)

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

forgetevalf

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.39258366I

(22)

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

evalfEI1&comma;21503I10

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&comma; AppellF2a&comma; b1&comma; b2&comma; c1&comma; c2&comma; z1&comma; z2

F2a&comma;b1&comma;b2&comma;c1&comma;c2&comma;z1&comma;z2=_k1=0_k2=0a_k1+_k2b1_k1b2_k2z1_k1z2_k2c1_k1c2_k2_k1!_k2!&comma;z1+z2<1

(23)

Following the same steps outlined before, get the summand and construct a formula-procedure this time depending on 2 parameters m and n

summandop1&comma;1&comma;rhs1

summanda_k1+_k2b1_k1b2_k2z1_k1z2_k2c1_k1c2_k2_k1!_k2!

(24)

general_formulaunapplysummand&comma;_k1&comma;_k2

general_formula_k1&comma;_k2a_k1+_k2b1_k1b2_k2z1_k1z2_k2c1_k1c2_k2_k1!_k2!

(25)

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&comma;b1&comma;b2&comma;c1&comma;c2&comma;z1&comma;z2&rarr;Addsubs&apos;:-a&equals;a&comma;:-b1&equals;b1&comma;:-b2&equals;b2&comma;:-c1&equals;c1&comma;:-c2&equals;c2&comma;:-z1&equals;z1&comma;:-z2&equals;z2&apos;&comma;evalgeneral_formula

evalf/AF2a&comma;b1&comma;b2&comma;c1&comma;c2&comma;z1&comma;z2Addsubs:−a=a&comma;:−b1=b1&comma;:−b2=b2&comma;:−c1=c1&comma;:−c2=c2&comma;:−z1=z1&comma;:−z2=z2&comma;evalgeneral_formula

(26)

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

AF212&comma;27&comma;14&comma;2&comma;32+I&comma;14I&comma;14

AF212&comma;27&comma;14&comma;2&comma;32+I&comma;I4&comma;14

(27)

evalf

    -> 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.02726587338I

(28)

Compare with the numerical value obtained using the standard Maple AppellF2 function

subsAF2=AppellF2&comma;

F212&comma;27&comma;14&comma;2&comma;32+I&comma;I4&comma;14

(29)

evalf

0.9831522491+0.02726587338I

(30)

Compatibility

• 

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