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

套件詳情v3.2.0

math_expressions

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

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
10.1萬
喜歡數
382
Pub 分數
160

安裝套件

flutter pub add math_expressions

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

一個用於解析與計算數學表達式的 Flutter/Dart 套件,支援實數、向量與基本區間算術。它能從字串動態計算數學公式,非常適合計算機、科學應用與資料分析工具。此套件輕量、跨平台,並可無縫整合至 Flutter 項目中。支援標準運算子、函數與自訂變數,對無效輸入提供明確的錯誤處理。對於需要靈活數學表達式求值卻不願從零建構解析器的開發者而言,是理想之選。

適用情境

  • 科學計算器
  • 動態公式求值
  • 應用中的資料分析
  • 教育類數學工具
  • 物理模擬

主要特色

  • 支援實數與向量
  • 基本區間算術
  • 自訂變數和函數定義
  • 跨平台相容性
  • 強大的錯誤處理
  • 基於字串的表示式解析

適合對象

  • 數學密集型應用
  • 教育軟體
  • 工程與物理應用
  • 計算機應用
  • 資料驅動的 UI

使用建議

  • 未針對高效率數值計算進行優化
  • 限制為基本區間算術
  • 需要仔細的輸入驗證以防止類似注入的問題
  • 不支援複數的內建支援

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

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.

所屬分類

依賴

依賴清單 2 項

原始碼儲存庫

使用此套件的專案

所在榜單