function_tree 0.5.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 76

Welcome to function_tree, a simple library for parsing strings into callable function-trees.

Parsing strings as mathematical expressions #

At the simplest (and least efficient) level, we can interpret strings as mathematical expressions.

Example: #

 final expressions = [
   '2 + 2',
   '(3 + 2)^3',
   '3 * pi / 4',
   '3 * sin(5 * pi / 6)',
   'e^(-1)'
 ];
 for (final expression in expressions) {
   print("'$expression' -> ${expression.interpret()}");
 }

Output: #

'2 + 2' -> 4
'(3 + 2)^3' -> 125
'3 * pi / 4' -> 2.356194490192345
'3 * sin(5 * pi / 6)' -> 1.5000000000000009
'e^(-1)' -> 0.36787944117144233

Function Trees #

The library supports two types of callable, function-trees, namely SingleVariableFunction and MultiVariableFunction.

Single variable functions #

We can create a single variable function from a string either by constructing a SingleVariableFunction instance or by calling the toSingleVariableFunction string extension directly on a string, as in the following example:

Example: #

 final f = '20 * (sin(x) + 1)'.toSingleVariableFunction(),
     pi = 'pi'.interpret();
 for (var x = 0.0; x < 2 * pi; x += pi / 20) {
   print('|' + ' ' * f(x).round() + '*');
 }

Output: #

|                    *
|                       *
|                          *
|                             *
|                                *
|                                  *
|                                    *
|                                      *
|                                       *
|                                        *
|                                        *
|                                        *
|                                       *
|                                      *
|                                    *
|                                  *
|                                *
|                             *
|                          *
|                       *
|                    *
|                 *
|              *
|           *
|        *
|      *
|    *
|  *
| *
|*
|*
|*
| *
|  *
|    *
|      *
|        *
|           *
|              *
|                 *

Multi-variable functions #

Similarly, we can construct a MultiVariableFunction instance or call the toMultiVariableFunction string extension to create a multi-variable functional tree, as in the following example.

Example: #

 final times = 'a * b'.toMultiVariableFunction(['a', 'b']),
     values = [1, 2, 3, 4, 5];
 for (final a in values) {
   final sb = StringBuffer();
   for (final b in values) {
     sb..write(times({'a': a, 'b': b}))..write('\t');
   }
   print(sb);
 }

Output: #

1	2	3	4	5	
2	4	6	8	10	
3	6	9	12	15	
4	8	12	16	20	
5	10	15	20	25	

TeX expressions #

Function tree instances have a tex property for TeX expressions.

Example: #

 final f = 'x * cos(y) + y * sin(x)'.toMultiVariableFunction(['x', 'y']);
 print(f.tex);

Output: #

x \cdot \cos\left( y \right) + y \cdot \sin\left( x \right) 

Interpreter #

The interpreter has support for the following:

Functions #

abs     acos    asin    atan    ceil
cos     cosh    cot     coth    csc
csch    exp     floor   ln      log
round   sec     sech    sin     sinh
sqrt    tan     tanh

Constants #

e       pi      ln2     ln10    log2e
log10e  sqrt1_2 sqrt2

Operations #

+   -   *   /   %   ^

Thanks! #

Thanks for your interest in this library. Please file any bugs or requests here.

Change log #

0.5.0 #

  • Dusted out some cobwebs.
  • Renamed FunctionTree MultiVariableFunction and FunctionOfX SingleVariableFunction, which is more expressive.
  • Removed zero, derivative and integral estimates, which I feel were tangential.

0.2.6 #

Fixed ambiguity issue: if the user chooses a variable name in an expression that happens to be a special constant name (such as 'e'), the parser will take occurrences in the expression to mean that the variable.

0.2.5 #

  • Marked as ready for Dart 2.
  • dartfmt-ed some of the files.
  • Added support for the % operator.
  • Added support for floor, ceil and round.
  • Added come timing comparisons between evaluating native Dart functions, function tree instances and repeated string parsing in test/.

0.2.0 #

  • Re-wrote the parsing engine: code much cleaner and easier to read.
  • Cleared out deprecated dart:math constants.
  • Updated code for Dart 2.
  • Added support for abs.

0.1.1 #

  • Basic numerical differentiation.
  • Basic numerical integration (Simpson).
  • Basic zero-finding (Newton).
  • Very much proof of concept.

0.1.0 #

  • Initial version, proof of concept.
  • Very little checking of input at this stage.

example/example.md

A rough plot of $\left| \left(\sin\left( x \right) \right) \right| + \frac{\cos\left( \left| \left(x \right) \right| \right) }{2 }$:

|+
|+
|+
|+
|  +
|         +
|                +
|                       +
|                             +
|                                   +
|                                        +
|                                            +
|                                                +
|                                                   +
|                                                      +
|                                                       +
|                                                        +
|                                                        +
|                                                      +
|                                                    +
|                                                  +
|                                              +
|                                          +
|                                     +
|                               +
|                         +
|                               +
|                                     +
|                                          +
|                                              +
|                                                  +
|                                                    +
|                                                      +
|                                                        +
|                                                        +
|                                                       +
|                                                      +
|                                                   +
|                                                +
|                                            +
|                                        +
|                                   +
|                             +
|                       +
|                +
|         +
|  +
|+
|+
|+
|+

A rough plot of $\left| \left(\sin\left( a \right) + \cos\left( b \right) \right) \right|$:

+---------------------------------------------------+
|o::::::::        :::::::::::::::::        ::::::::o|
|ooooo:::::        :::::::::::::::        :::::ooooo|
|oooooo:::::         :::::::::::         :::::oooooo|
|oooooooo::::         :::::::::         ::::oooooooo|
|ooooooooo::::           :::           ::::ooooooooo|
|****oooooo::::                       ::::oooooo****|
|*****ooooo::::                       ::::ooooo*****|
|******ooooo::::                     ::::ooooo******|
|*******ooooo::::                   ::::ooooo*******|
|********oooo::::                   ::::oooo********|
|********ooooo::::                 ::::ooooo********|
|*********oooo::::                 ::::oooo*********|
|*********oooo::::                 ::::oooo*********|
|*********oooo::::                 ::::oooo*********|
|*********oooo::::                 ::::oooo*********|
|********ooooo::::                 ::::ooooo********|
|********oooo::::                   ::::oooo********|
|*******ooooo::::                   ::::ooooo*******|
|******ooooo::::                     ::::ooooo******|
|*****ooooo::::                       ::::ooooo*****|
|****oooooo::::                       ::::oooooo****|
|ooooooooo::::           :::           ::::ooooooooo|
|oooooooo::::         :::::::::         ::::oooooooo|
|oooooo:::::         :::::::::::         :::::oooooo|
|ooooo:::::        :::::::::::::::        :::::ooooo|
|o::::::::        ::::::::o::::::::        ::::::::o|
|::::::::        :::::ooooooooo:::::        ::::::::|
|::::::         :::::ooooooooooo:::::         ::::::|
|:::::         ::::ooooooooooooooo::::         :::::|
|::           ::::ooooooooooooooooo::::           ::|
|            ::::oooooo*******oooooo::::            |
|            ::::ooooo*********ooooo::::            |
|           ::::ooooo***********ooooo::::           |
|          ::::ooooo*************ooooo::::          |
|          ::::oooo***************oooo::::          |
|         ::::ooooo***************ooooo::::         |
|         ::::oooo*****************oooo::::         |
|         ::::oooo*****************oooo::::         |
|         ::::oooo*****************oooo::::         |
|         ::::oooo*****************oooo::::         |
|         ::::ooooo***************ooooo::::         |
|          ::::oooo***************oooo::::          |
|          ::::ooooo*************ooooo::::          |
|           ::::ooooo***********ooooo::::           |
|            ::::ooooo*********ooooo::::            |
|            ::::oooooo*******oooooo::::            |
|::           ::::ooooooooooooooooo::::           ::|
|:::::         ::::ooooooooooooooo::::         :::::|
|::::::         :::::ooooooooooo:::::         ::::::|
|::::::::        :::::ooooooooo:::::        ::::::::|
|:::::::::        ::::::::o::::::::        :::::::::|
+---------------------------------------------------+

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  function_tree: ^0.5.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:function_tree/function_tree.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
53
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
76
Learn more about scoring.

We analyzed this package on Mar 30, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6

Health suggestions

Fix lib/src/interpreter.dart. (-0.50 points)

Analysis of lib/src/interpreter.dart reported 1 hint:

line 109 col 7: DO use curly braces for all flow control structures.

Fix lib/src/leaves.dart. (-0.50 points)

Analysis of lib/src/leaves.dart reported 1 hint:

line 4 col 29: Use ; instead of {} for empty constructor bodies.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0