functionUtilsTest.frink

Download or view functionUtilsTest.frink in plain text format

/** These are tests for the functionUtils.frink library.   It demonstrates
    how to find derivatives and integrals of functions, including functions
    defined in Frink.
*/


use functionUtils.frink
use allTransforms.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["-----------------------------------\n\n"]


// Test with named function (sin) which uses built-in function.
// This needs to recognize that there's no Frink-defined function
// body and use any defined transformations to calculate its derivative.
// For example, the derivative of sin[x] is defined in derivatives.frink.
fSin = getFunction["sin", 1]
println["Function is:    " + inputForm[fSin]]
derivativeFunc1 = makeDerivative[fSin]
println["Before solve:   $derivativeFunc1"]
derivative1 = transformExpression[derivativeFunc1]
println["Derivative is:  $derivative1"]
integral1 = transformExpression[makeIntegral[fSin]]
println["Integral is:    $integral1"]
println["-----------------------------------\n\n"]


// 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["-----------------------------------\n\n"]


// 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]]
println["Going to solve:\t$solve"]
solutions = transformExpression[solve]

for sol = solutions
{
   // The solution comes back as something like x === 3 y + 2
   // So the right-hand-side (child 1) = (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"]
   retransformed = transformExpression[substituted]
   if ! structureEquals[substituted, retransformed]
      println["Re-Transformed:  $retransformed\n"]
   
   println["Evaluated solution: " + eval[retransformed] + "\n"]
   println[]
}


Download or view 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 19945 days, 16 hours, 3 minutes ago.