| 1 |
function SymbolicNthDerivative(f,n) = |
| 2 |
( |
| 3 |
local *; |
| 4 |
if not IsFunction(f) then |
| 5 |
(error("SymbolicNthDerivative: argument 1 must be a function");bailout) |
| 6 |
else if not IsNonNegativeInteger(n) then |
| 7 |
(error("SymbolicNthDerivative: argument 2 must be a non-negative integer");bailout); |
| 8 |
|
| 9 |
df = f; |
| 10 |
|
| 11 |
for k=1 to n do ( |
| 12 |
df = SymbolicDerivativeTry (df); |
| 13 |
if IsNull(df) then |
| 14 |
(error("SymbolicNthDerivative: Cannot differentiate function n times");bailout) |
| 15 |
); |
| 16 |
df |
| 17 |
) |
| 18 |
SetHelp("SymbolicNthDerivative","symbolic","Attempt to symbolically differentiate a function n times"); |
| 19 |
|
| 20 |
function SymbolicNthDerivativeTry(f,n) = |
| 21 |
( |
| 22 |
local *; |
| 23 |
if not IsFunction(f) then |
| 24 |
(error("SymbolicNthDerivativeTry: argument 1 must be a function");bailout) |
| 25 |
else if not IsNonNegativeInteger(n) then |
| 26 |
(error("SymbolicNthDerivativeTry: argument 2 must be a non-negative integer");bailout); |
| 27 |
|
| 28 |
df = f; |
| 29 |
|
| 30 |
for k=1 to n do ( |
| 31 |
df = SymbolicDerivativeTry (df); |
| 32 |
if IsNull(df) then |
| 33 |
return null |
| 34 |
); |
| 35 |
df |
| 36 |
) |
| 37 |
SetHelp("SymbolicNthDerivativeTry","symbolic","Attempt to symbolically differentiate a function n times quietly and return null on failure"); |
| 38 |
|
| 39 |
function SymbolicTaylorApproximationFunction(f,x0,n) = |
| 40 |
( |
| 41 |
local *; |
| 42 |
if not IsFunction(f) then |
| 43 |
(error("SymbolicTaylorApproximationFunction: argument 1 must be a function");bailout) |
| 44 |
else if not IsValue(x0) then |
| 45 |
(error("SymbolicTaylorApproximationFunction: argument 2 must be a value");bailout) |
| 46 |
else if not IsPositiveInteger(n) then |
| 47 |
(error("SymbolicTaylorApproximationFunction: argument 3 must be a positive integer");bailout); |
| 48 |
|
| 49 |
df = f; |
| 50 |
c = null; |
| 51 |
|
| 52 |
for k=0 to n do ( |
| 53 |
c@(k+1) = (df call (x0))/(k!); |
| 54 |
if k < n then ( |
| 55 |
df = SymbolicDerivative (df); |
| 56 |
if IsNull(df) then |
| 57 |
return null |
| 58 |
) |
| 59 |
); |
| 60 |
tp = PolyToFunction (c); |
| 61 |
`(x)[tp,x0] = (tp call (x - x0)) |
| 62 |
) |
| 63 |
SetHelp("SymbolicTaylorApproximationFunction","symbolic","Attempt to construct the Taylor approximation function around x0 to the nth degree."); |