搜索插件、代码仓与 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 项

源码仓库

使用此插件的项目

所在榜单