搜尋套件、儲存庫與 APP 分類

GitHub 儲存庫fkleon/math-expressions

math-expressions

一個用於解析和評估數學表達式的程式庫,支援實數、向量和基本區間算術。

math-expressions 專案封面
星標
114
分支
40
最近推送(UTC)
2026年7月31日
專案狀態
未封存

原始 README

以下為英文專案原文快照,最新內容請造訪 GitHub。

math_expressions

A library for parsing and evaluating mathematical expressions.

  • Performs evaluations in real, vector, and interval contexts.
  • Supports expression simplification and differentiation.

math_expressions is inspired by mathExpr for Java and distributed under the MIT license.

Features

  • Evaluation of expressions in various modes: Real, Vector and Interval.
  • Parsing, simplification and differentiation of mathematical expressions.
  • Supporting most basic math functions out of the box.
  • Extensible through custom function definitions in code.
  • Well documented and tested.

This package contains a very simple command-line interpreter for real numbers:

dart pub run math_expressions:interpreter
What's not working yet?
  • Some evaluations in vector and interval space (especially functions).
  • N-dimensional vectors. Curently no more than four dimensions are supported.
  • The parser only works for real numbers.
  • Complex numbers.

Suggestions and pull requests are always welcome!

Usage

Below are two basic examples of how to use this library. There also is some additional example code available.

1. Expression creation and evaluation

This example shows how to evaluate

$\frac{(x^2+\cos y)}{3}$

for $x=2, y=\pi$

Build the expression

You can either create an mathematical expression programmatically or parse a string.

  • Create the expression programmatically:
  var x = Variable('x'), y = Variable('y');
  var xSquare = Power(x, 2);
  var yCos = Cos(y);
  var three = Number(3.0);
  Expression exp = (xSquare + yCos) / three;
  • Create the expression via the parser:
  ExpressionParser p = GrammarParser();
  Expression exp = p.parse("(x^2 + cos(y)) / 3");
Evaluate the expression
  • Bind variables and evaluate the expression as real number:
  // Bind variables:
  var context = ContextModel()
    ..bindVariableName('x', Number(2.0));
    ..bindVariableName('y', Number(math.pi));

  // Evaluate expression:
  var evaluator = RealEvaluator(context);
  num eval = evaluator.evaluate(exp);

  print(eval) // = 1.0
2. Expression simplification and differentiation

This example shows how to simplify and differentiate

$x \cdot 1 - (-5)$

  • Expressions can be simplified and differentiated with respect to a given variable:
  Expression exp = p.parse("x*1 - (-5)");

  print(exp);            // = ((x * 1.0) - -(5.0))
  print(exp.simplify()); // = (x + 5.0)

  Expression expDerived = exp.derive('x');

  print(expDerived);            // = (((x * 0.0) + (1.0 * 1.0)) - -(0.0))
  print(expDerived.simplify()); // = 1.0

Alternatives

Here are some other Dart libraries that implement similar functionality to math_expression: parsing and evaluating mathematical expressions.

  • expressions: an elegant and small library to parse and evaluate simple expressions.
  • function_tree: a library for parsing, evaluating and plotting single- and multi-variables numerical functions.

To the author's knowledge math_expressions is currently the only library supporting interval arithmetics.

Frederik Leonhardt GitHub avatar
GitHub 作者

Frederik Leonhardt ↗

@catalystWellington, New Zealand官方網站 ↗
語言Dart

分類

此儲存庫發佈的套件

使用的依賴

依賴清單 4 項

如何選擇適合專案的儲存庫?

先依使用情境篩選,再比較專案活躍情況,最後到原始儲存庫確認使用要求。

  1. 透過儲存庫名稱、作者或描述搜尋,再依技術分類縮小範圍。
  2. 綜合星標、分支與最近推送比較專案;星標反映社群關注,不保證品質。
  3. 使用前在 GitHub 確認 README、授權條款與 Issues。

來源與維護

“A repository is the most basic element of GitHub.”

GitHub Docs — About repositories ↗