Clone wiki

function-tree / Home

Home | Basic Functions | Multi-variable functions | About


Welcome to function-tree, a Dart library that converts string representations of numerical functions into callable, function-like objects.

Basic use

import 'package:function_tree/function_tree.dart';
import 'dart:math' show PI;

main() {
  // A string representation of a function...
  String myFunction = "1.5 * sin(2 * (x - PI / 3)) + 2";

  // Create a function-like object from the string...
  var f = new FunctionOfX(myFunction);

  // We can now call f as if it were a true function...

  // And get a TeX representation of the function...

  // For multi-variable functions, we need to specify
  // the variable names...
  var ft = new FunctionTree(
    fromExpression: "1.5 * sin(2 * (x - PI / 3)^2) + y",
    withVariableNames: ["x", "y"]);

  // We can now call it like a function, specifying
  // variable substitutions using a map...
  print(ft({"x": 1, "y": 2}));

Supported syntax

The string expressions used to build the function-trees should be similar to expression that can be used in Dart programming after importing dart:math, such as 3 * sin(x) + 1. Several additions exist, however, including:

  • the ^ operator, to raise to a power
  • π, to represent π (PI can also be used)
  • the trigonometric functions sec, csc and cot are defined
  • the hyperbolic functions sinh, cosh and tanh are defined