该数据包为 Dart 提供了高效、轻量级的数据结构和算法,针对时间和空间进行了优化。它提供了数组、集合和映射等集合类型,具有增强的性能特征,以及用于常见计算任务的实用函数。专为数学和算法操作设计,支持在 Android、iOS、Web、macOS、Windows 和 Linux 等所有主要平台上的无缝集成。非常适合需要高性能数据处理且无需大型框架开销的应用程序。
适合场景
算法实现
高性能数据处理
数学计算
跨平台数据操作
应用中的高效内存使用
主要特点
针对时间和空间效率进行了优化
轻量级集合实现
支持多个平台
内置算法工具
最小依赖
适合对象
需要快速数据操作的开发者
数据负载较重的应用程序
跨平台项目
性能敏感的 Dart 代码
计算密集型应用
使用建议
不是对内置 Dart 集合的完整替代
最适合特定算法需求
可能需要学习新的 API 模式
仅限核心数据结构
不适用于用户界面或状态管理
原始 README
以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。
Dart Data
Dart Data is a fast and space efficient library to deal with data in Dart, Flutter and the web. As of today this mostly includes data structures and algorithms for vectors and matrices, but at some point might also include graphs and other mathematical structures.
This library is open source, stable and well tested. Development happens on GitHub. Feel free to report issues or create a pull-request there. General questions are best asked on StackOverflow.
Solve 'A * x = b', where 'A' is a matrix and 'b' a vector:
final a = Matrix<double>.fromRows(DataType.float64, [
[2, 1, 1],
[1, 3, 2],
[1, 0, 0],
]);
final b = Vector<double>.fromList(DataType.float64, [4, 5, 6]);
final x = a.solve(b.columnMatrix).column(0);
print(x.format(valuePrinter: FixedNumberPrinter())); // prints '6 15 -23'
How to find the eigenvalues of a matrix?
Find the eigenvalues of a matrix 'A':
final a = Matrix<double>.fromRows(DataType.float64, [
[1, 0, 0, -1],
[0, -1, 0, 0],
[0, 0, 1, -1],
[-1, 0, -1, 0],
]);
final decomposition = a.eigenvalue;
final eigenvalues = Vector<double>.fromList(
DataType.float64, decomposition.realEigenvalues);
print(eigenvalues.format(valuePrinter: FixedNumberPrinter(precision: 1))); // prints '-1.0 -1.0 1.0 2.0'
How to find all the roots of a polynomial?
To find the roots of x^5 + -8x^4 + -72x^3 + 242x^2 + 1847x + 2310:
To find the best fitting third degree polynomial through a list of points:
final height = [1.47, 1.50, 1.52, 1.55, 1.57, 1.60, 1.63, 1.65, 1.68, 1.70, 1.73, 1.75, 1.78, 1.80, 1.83].toVector();
final mass = [52.21, 53.12, 54.48, 55.84, 57.20, 58.57, 59.93, 61.29, 63.11, 64.47, 66.28, 68.10, 69.92, 72.19, 74.46].toVector();
final fitter = PolynomialRegression(degree: 2);
final result = fitter.fit(xs: height, ys: mass);
print(result.polynomial.format(valuePrinter: FixedNumberPrinter(precision: 3))); // 61.960x^2 + -143.162x + 128.813
How to numerically integrate a function?
In both examples we specify a custom depth, since these integrals are tricky at the upper bound (very steep for the first one, very flat for the second one).
// Compute the area of a circle by iterating over a quarter circle:final pi = 4 * integrate((x) => sqrt(1 - x * x), 0, 1, depth: 30);
print(pi); // 3.1415925673846368 ~ pi// Compute an improper integral:final one = integrate((x) => exp(-x), 0, double.infinity, depth: 30);
print(one); // 1.0000000904304227 ~ 1
Misc
Demos
This package includes a sandbox web dashboard in web/distributions to visualize, configure, and dynamically sample the supported continuous and discrete probability distributions in real-time.
To compile, serve, and explore the interactive distributions demo run:
dart pub global activate webdev
webdev serve --release