functionUtilsTest.frink

View or download functionUtilsTest.frink in plain text format


/** These are tests for the functionUtils.frink library.   It demonstrates
    how to find derivatives of functions. */


use functionUtils.frink
use derivatives.frink
use integrals.frink

symbolicMode[true]
showApproximations[false]


// Test with named function
squareFunc[x] := x^2
fSquare = getFunction["squareFunc", 1]
println["Function is:    " + inputForm[fSquare]]
derivativeFunc1 = makeDerivative[fSquare]
println["Before solve:   $derivativeFunc1"]
derivative1 = transformExpression[derivativeFunc1]
println["Derivative is:  $derivative1"]
integral1 = transformExpression[makeIntegral[fSquare]]
println["Integral is:    $integral1"]
println[]


// Test with anonymous function
anonFunc = { |x| x sin[x] }
println["Function is:    " + inputForm[anonFunc]]
derivativeFunc2 = makeDerivative[anonFunc]
println["Before solve:   $derivativeFunc2"]
derivative2 = transformExpression[derivativeFunc2]
println["Derivative is:  $derivative2"]
println[]


// Demonstrates solving an equation.
use solvingTransformations.frink

// The following four lines are essentially equivalent because the === is a special
// form of expression that is used for symbolic transformations and does not evaluate
// either side of the expression by default.
//
// solve = makeSolve[noEval[x], noEval[y^3 + 2 y^2 + 3 y + 4], noEval[y]]
// solve = makeSolve[noEval[x === y^3 + 2 y^2 + 3 y + 4], noEval[y]]
// solve = makeSolve[x === y^3 + 2 y^2 + 3 y + 4, noEval[y]]
solve = noEval[solve[x === y^3 + 2 y^2 + 3 y + 4, y]]

solutions = transformExpression[solve]
for sol = solutions
{
   // The solution comes back as something like x === 3 y + 2
   // So the right-hand-side (3 y + 2) will be the solution
   right = getChild[sol, 1]
   println["Solution:  $right\n"]
   substituted = substituteExpression[right, noEval[x], 2]  // Substitute x -> 2
   println["Substituted:  $substituted\n"]
   println[eval[substituted] + "\n"]
   retransformed = transformExpression[substituted]
   println["Re-Transformed:  $retransformed\n"]
   println["Evaluated solution: " + eval[retransformed] + "\n"]
   println[]
}


View or download functionUtilsTest.frink in plain text format


This is a program written in the programming language Frink.
For more information, view the Frink Documentation or see More Sample Frink Programs.

Alan Eliasen was born 17591 days, 17 hours, 44 minutes ago.