FiniteDifferenceFormula.activatejuliafunction
— Functionverifyformula(n, points, k, m = 1)
or activatejuliafunction(n, points, k, m = 1)
Verify if a formula is valid. If it is valid, generate and activate its Julia function(s). If not, try to find a formula for the derivative using the points.
n: the n-th order derivative
points: in the format of a range, start : stop, or a list
k: a list of the coefficients in a formula
m: the coefficient in the denominator of a formula
Examples
import FiniteDifferenceFormula as fd
fd.verifyformula(1,[-1,2],[-3,4],5) # f'(x[i]) = (-3f(x[i-1])+4f(x[i+2]))/(5h)?
fd.verifyformula(2, -3:3, [2,-27,270,-490,270,-27,2], 18)
fd.activatejuliafunction(2, -3:3, [1/90,-3/20,3/2,-49/18,3/2,-3/20,1/90])
fd.verifyformula(2, -3:3, [1//90,-3//20,3//2,-49//18,3//2,-3//20,1//90])
FiniteDifferenceFormula.compute
— Functioncompute
(n, points, printformulaq = false)
Compute a formula for the nth order derivative using the given points.
n: the n-th order derivative to be found
points: in the format of a range, start : stop, or a vector
printformulaq: print the computed formula or not
points | The points/nodes to be used |
---|---|
0:2 | x[i], x[i+1], x[i+2] |
-2:2 | x[i-2], x[i-1], x[i], x[i+1], x[i+2] |
-3:2 | x[i-3], x[i-2], x[i-1], x[i], x[i+1], x[i+2] |
[1, 1, -1] | x[i-1], x[i+1] |
[1 0 1 -1] | x[i-1], x[i], x[i+1] |
A vector can be like [1, 0, 2] or [1 0 2]. It will be rearranged so that elements are ordered from lowest to highest with duplicate ones removed.
Examples
julia> import FiniteDifferenceFormula as fd
julia> fd.compute(2, [0 1 2 3])
julia> fd.compute(2, 0:3)
julia> fd.compute(2, [-5, -2, 1, 2, 4], true)
FiniteDifferenceFormula.decimalplaces
— Methoddecimalplaces
() or decimalplaces
(n)
Show present decimal places for generating Julia function(s) of computed formulas, if no argument is passed to decimalplaces
. Otherwise, set the decimal places to n.
Examples
import FiniteDifferenceFormula as fd
fd.compute(2,-3:3)
fd.formula() # by default, use 16 decimal places to generate a Julia function
fd.decimalplaces(4)
fd.formula() # now, use 4 decimal places to generate a Julia function
FiniteDifferenceFormula.find
— Functionfind
(n, points, printformulaq = false)
Compute a formula for the nth order derivative using the given points.
For the input, n
and points
(See [compute
]), there may not be formulas which use the two end points like -2 and 3 in -2 : 3 or [-2, -1, 0, 1, 2, 3]. In this case, find
tries to find a formula by shrinking the range to, first -1 : 3, then, -2 : 2, then -1 : 2, and so on, until a formula is found or no formulas can be found at all.
See also [compute
], [findbackward
], and [findforward
].
Examples
import FiniteDifferenceFormula as fd
fd.find(2, -10:9)
FiniteDifferenceFormula.findbackward
— Functionfindbackward
(n, points, printformulaq = false)
Compute a formula for the nth order derivative using the given points.
For the input, n
and points
(See [compute
]), there may not be formulas which use the two end points like -2 and 3 in -2 : 3 or [-2, -1, 0, 1, 2, 3]. In this case, findbackward
tries to find a formula by shrinking the range from the right endpoint to, first -2 : 2, then, -2 : 1, then -2 : 0, and so on, until a formula is found or no formulas can be found at all.
See also [compute
], [find
], and [findforward
].
Examples
import FiniteDifferenceFormula as fd
fd.compute(3,-100:50)
# output: ***** Warning: 3, -100:22 might be your input for which a formula is found.
fd.findbackward(3,-99:50)
# output: (3, -99:23, ...)
FiniteDifferenceFormula.findforward
— Functionfindforward
(n, points, printformulaq = false)
Compute a formula for the nth order derivative using the given points.
For the input, n
and points
(See [compute
]), there may not be formulas which use the two end points like -2 and 3 in -2 : 3 or [-2, -1, 0, 1, 2, 3]. In this case, findforward
tries to find a formula by shrinking the range from the left endpoint to, first -1 : 3, then, 0 : 3, then 1 : 3, and so on, until a formula is found or no formulas can be found at all.
See also [compute
], [find
], and [findbackward
].
Examples
import FiniteDifferenceFormula as fd
fd.findforward(2, -10:9)
FiniteDifferenceFormula.formula
— Methodformula
()
Generate and list:
k[1]*f(x[i+points[1]]) + k[2]*f(x[i+points[2]]) + ... + k[len]*f(x[i+points[len]]) = m*f^(n)(x[i]) + ..., m > 0
The formula for f^(n)(x[i]), including estimation of accuracy in the big-O notation.
Julia function(s) for f^(n)(x[i]).
Calling compute(n, points, true)
is the same as calling compute(n, points)
and then formula()
.
Even if no formula can be found, it still lists the computing results from which we can see why. For example, after compute(2,1:2)
, try formula()
.
FiniteDifferenceFormula.formulas
— Functionformulas
(orders = 1:3, minnumofpoints = 2, maxnumofpoints = 5)
By default, the function prints all forward, backward, and central finite difference formulas for the 1st, 2nd, and 3rd derivatives, using 2 to 5 points.
Examples
# The following examples show all forward, backward, and central finite
# difference formulas for the specified derivatives, using 4 to 11 points.
julia> import FiniteDifferenceFormula as fd
julia> fd.formulas(2:5, 4, 11) # the 2nd, 3rd, .., 5th derivatives
juliq> fd.formulas([2, 4, 7], 4, 11) # the 2nd, 4th, and 7th derivatives
julia> fd.formulas(3, 4, 11) # the 3rd derivative
FiniteDifferenceFormula.loadcomputingresults
— Methodloadcomputingresults
(results)
Input: 'results' is a tuple, (n, points, k[:], m). See compute(...).
Load computing results from the output of compute(...). After this command, formula(), activatepythonfunction(), truncationerror(0, etc., are available. It allows users to work on saved computing results (say, in a textfile). For example, when it takes hours to compute/find a formula, users may run commands like the following one from OS terminal
julia -e "import FiniteDifferenceFormula as fd; println(fd.compute(1, range(-200, 201)))" > data.txt
and then mannually load data from data.txt to this function later.
See also [compute].
FiniteDifferenceFormula.taylor
— Methodtaylor
()
- Print the first few nonzero terms of the Taylor series of the linear combination k[0]f(x[i+j0]) + k[1]f(x[i+j1]) + ... for the newly computed formula (even if failed).
taylor
(j, n = 10)
- Print the 1st n terms of Taylor series of f(x[i+j]) about x[i].
taylor
(coefs, n = 10), or
- Print the 1st n terms of Taylor series with coefficients in 'coefs'
taylor
(points, k, n::Int = 10)
- Prints the 1st n nonzero terms of the Taylor series of the linear combination: k[0]f(x[i+points[0]]) + k[1]f(x[i+points[1]]) + ...
The last two provide also another way to verify if a formula is mathematically valid or not.
See also [verifyformula
], [activatejuliafunction
], and [taylorcoefs
].
Examples
import FiniteDifferenceFormula as fd
fd.compute(1, [0, 1, 5, 8])
fd.taylor()
fd.taylor(2)
n = 50
coefs = 2*fd.tcoefs(0, n) - 6*fd.tcoefs(1, n) + 4*fd.tcoefs(2, n)
fd.taylor(coefs, n) # this n can be any positive integer
fd.taylor(-fd.tcoefs(0) + 3*fd.tcoefs(1) - 3*fd.tcoefs(2) + fd.tcoefs(3))
fd.taylor(0:3, [-1, 3, -3, 1], 6)
FiniteDifferenceFormula.taylorcoefs
— Functiontaylorcoefs
(j, n = 10))
Compute and return coefficients of the first n terms of the Taylor series of f(x[i + j]) = f(x[i] + jh) about x[i], where h is the increment in x.
Examples
import FiniteDifferenceFormula as fd
fd.taylorcoefs(-2)
fd.taylorcoefs(5, 4)
FiniteDifferenceFormula.tcoefs
— Functiontcoefs
(j, n = 10))
Same as taylorcoefs(j, n).
FiniteDifferenceFormula.truncationerror
— Methodtruncationerror
()
Show the truncation error of the last computed formula in the big_O notation.
Examples
import FiniteDifferenceFormula as fd
fd.compute(2,-3:3)
fd.truncationerror()
fd.find(3,[-2, 1, 2, 5, 7, 15])
fd.truncationerror()
FiniteDifferenceFormula.verifyformula
— Functionverifyformula
(n, points, k, m = 1) or activatejuliafunction
(n, points, k, m = 1)
Verify if a formula is valid. If it is valid, generate and activate its Julia function(s). If not, try to find a formula for the derivative using the points.
n: the n-th order derivative
points: in the format of a range, start : stop, or a vector
k: a list of the coefficients in a formula
m: the coefficient in the denominator of a formula
Examples
import FiniteDifferenceFormula as fd
fd.verifyformula(1,[-1,2],[-3,4],5) # f'(x[i]) = (-3f(x[i-1])+4f(x[i+2]))/(5h)?
fd.verifyformula(2, -3:3, [2,-27,270,-490,270,-27,2], 18)
fd.activatejuliafunction(2, -3:3, [1/90,-3/20,3/2,-49/18,3/2,-3/20,1/90])
fd.verifyformula(2, -3:3, [1//90,-3//20,3//2,-49//18,3//2,-3//20,1//90])