搜索插件、代码仓与 APP 分类

插件详情v1.0.5

n_dimensional_array

允许在 Dart 中创建和轻松操作 n 维数组。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
405
喜欢数
23
Pub 分数
160

安装插件

flutter pub add n_dimensional_array

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

n_dimensional_array 包使 Dart 中能够创建和高效操作多维数组,支持任意数量的维度。它提供了一个简洁直观的 API,用于在各种平台上初始化、访问和修改数组元素。非常适合需要结构化数据存储的数学计算、数据处理和科学应用。该包轻量且可无缝集成到 Flutter 项目中。

适合场景

  • 科学计算
  • 数据分析
  • 游戏开发
  • 机器学习预处理
  • 数学建模

主要特点

  • 支持任意维度
  • 高效的元素访问
  • 简易初始化语法
  • 跨平台兼容性
  • 不可变和可变数组变体

适合对象

  • 开发人员使用矩阵和张量
  • 数学算法实现
  • 需要结构化多维数据的应用程序

使用建议

  • 未针对超大数组进行优化
  • 有限的内置数学运算
  • 需要手动处理数组边界
  • 无内置序列化支持

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

n-Dimensional Array

Multidimensional array graphic

The n-Dimensional Array package is a powerful tool that empowers Flutter developers to create and work with multi-dimensional arrays. With this package, developers can easily define and manipulate n-dimensional arrays in their applications. This package offers a simple and intuitive interface to handle arrays of various dimensions efficiently.

Installation

In the pubspec.yaml of your flutter project, add the following dependency:

dependencies:
  n_dimensional_array: ^1.0.4

Import it to each file you use it in:

import 'package:n_dimensional_array/n_dimensional_array.dart';

Usage

Example 1 - Create multi-dimensional array

This example shows how to create a multi-dimensional array using three different methods.

Note: Each method creates the same end product.

List myList = [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]];

// Method 1
NdArray ndArray1 = NdArray.fromList(myList);

// Method 2
NdArray ndArray2 = NdArray(3);

ndArray2.data = myList;

// Method 3
NdArray ndArray3 = NdArray(3);

ndArray3.reshape([3, 2, 2]);

ndArray3[0][0][0] = 1;
ndArray3[0][0][1] = 2;
ndArray3[0][1][0] = 3;
ndArray3[0][1][1] = 4;
ndArray3[1][0][0] = 5;
ndArray3[1][0][1] = 6;
ndArray3[1][1][0] = 7;
ndArray3[1][1][1] = 8;
ndArray3[2][0][0] = 9;
ndArray3[2][0][1] = 10;
ndArray3[2][1][0] = 11;
ndArray3[2][1][1] = 12;
Example 2 - Reshape

Just like with a normal list in Dart, you cannot add elements beyond the length of the list. For a list, you would use the add() method. Expanding a multi-dimensional list is a bit more complicated; so, the reshape method was created.

// The list from Example 1
List myList = [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]];

NdArray ndArray = NdArray.fromList(myList);

// The current shape is [3, 2, 2]
List<int> shape = ndArray.shape;

// Add one more element to the 3rd dimension
ndArray.reshape([3, 2, 3]);

// Note: Since we are only changing the third
// dimension, we could alternatively call:
// ndArray.reshape([-1, -1, 3]);
// The `-1` counts as a placeholder, and the
// original shape value is used.

// The list now looks like:
// [[[1, 2, null], [3, 4, null]], [[5, 6, null], [7, 8, null]], [[9, 10, null], [11, 12, null]]];

// Shrink the array down.
ndArray.reshape([1, 1, 2]);

// The list now looks like:
// [[[1, 2]]]
// Note: The truncated data is lost. Be careful
// when shrinking your arrays. Make sure that is
// really what you want to do.
Example 3 - Getting and Setting values

You can get or set a value very much like a normal list, where you would call myList[i].

// 2D list is easier to follow
List myList = [[1, 2], [3, 4]];

NdArray ndArray = NdArray.fromList(myList);

// Some current values
int value1 = ndArray[0][1]; // 2
int value2 = ndArray[1][1]; // 4

// Set a value
ndArray[0][1] = 8;

value1 = ndArray[0][1]; // 8

// The list now looks like:
// [[1, 8], [3, 4]]
Example 4 - Copy

When you use the copy() method, you create a new NdArray that is identical to the original. Any modifications to one does not affect the other.

Note: Any non-primitive data types that use references (like custom objects) might not create new versions of themselves when copied. If this is the case, changes to one of those objects will appear in both NdArrays.

// 2D list is easier to follow
List myList = [[1, 2], [3, 4]];

NdArray ndArray = NdArray.fromList(myList);
NdArray ndArrayCopy = ndArray.copy();

// Change only the copy
ndArrayCopy[0][0] = 99;

// The lists now look like:
// ndArray.data == [[1, 2], [3, 4]]
// ndArrayCopy.data == [[99, 2], [3, 4]]

If you found this helpful, please consider donating. Thanks!

buy me a coffee      paypal      venmo



所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目

暂无相关数据。