function_tree 0.2.6

  • Readme
  • Changelog
  • Example
  • Installing
  • 74

function_tree #

A library for parsing and evaluating numerical functions built from strings.

Usage #

A simple usage example:

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

main() {
  var f = FunctionOfX("1.5 * sin(2 * (x - pi / 3)) + 2");
  print(f(pi));

  var ft = FunctionTree(
    fromExpression: "1.5 * sin(2 * (x - pi / 3)^2) + y",
    withVariableNames: ["x", "y"]);

  print(ft({"x": 1, "y": 2}));
}

Features and bugs #

Please file feature requests and bugs at the issue tracker.

Change log #

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.dart

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

void main() {
  // FunctionOfX...
  var f = FunctionOfX("abs(sin(x)) + cos(abs(x)) / 2"),
      n = 50,
      xs = List.generate(n + 1, (i) => -pi + i * 2 * pi / n),
      ys = xs.map(f);

  // A rough sketch of the graph of this function over [-π, π].
  ys.map((y) => "|${" " * (y * 50).round()}+").forEach(print);

  // FunctionTree
  var tree = FunctionTree(
      fromExpression: "abs(sin(a) + cos(b))", withVariableNames: ["a", "b"]);

  print("+${"-" * xs.length}+");
  for (var a in xs) {
    print("|${xs.map((b) {
      var value = tree({"a": a, "b": b});
      if (value < 0.5) return " ";
      if (value < 1) return ":";
      if (value < 1.5) return "o";
      return "*";
    }).join()}|");
  }
  print("+${"-" * xs.length}+");
}

Use this package as a library

1. Depend on it

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


dependencies:
  function_tree: ^0.2.6

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]
63
Health:
Code health derived from static analysis. [more]
96
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
70
Overall:
Weighted score of the above. [more]
74
Learn more about scoring.

We analyzed this package on Nov 22, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.6.0
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:function_tree/function_tree.dart.

Health suggestions

Fix lib/src/parser.dart. (-1.99 points)

Analysis of lib/src/parser.dart reported 4 hints:

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

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

line 46 col 9: DO use curly braces for all flow control structures.

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

Fix lib/src/trees.dart. (-1.99 points)

Analysis of lib/src/trees.dart reported 4 hints:

line 64 col 36: Use = to separate a named parameter from its default value.

line 77 col 36: Use = to separate a named parameter from its default value.

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

line 89 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.

Maintenance suggestions

Package is getting outdated. (-29.86 points)

The package was last published 67 weeks ago.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0