n4ze3m / matrix2d

A package for mathematical 2D array functions and manipulations in Dart, similar to NumPy
https://pub.dev/packages/matrix2d
MIT License
16 stars 3 forks source link
dart dartlang matrix numpy

Matrix 2D 🧮

pub version Build Status License: MIT

With Matrix 2D, you can perform matrix operations such as addition, subtraction, and multiplication in Dart. It is a simple and easy-to-use library inspired by Python's NumPy library.

Operations

Examples

shape

List list = [[1, 2],[1, 2]];
print(list.shape);

ouput:

[2,2]

flatten

List list = [[1, 2],[1, 2]];
print(list.flatten);

ouput:

[1,2,1,2]

transpose

List list = [[1, 2],[1, 2]];
print(list.transpose);

ouput:

[[1,1],[2,2]]

addition

List list1 = [[1, 1],[1, 1]];
List list2 = [[2, 2],[2, 2]];
var addition = m2d.addition(list1,list2);
print(addition);

ouput:

[[3,3],[3,3]]

subtraction

List list1 = [[1, 1],[1, 1]];
List list2 = [[2, 2],[2, 2]];
var subtraction = m2d.subtraction(list1,list2);
print(subtraction);

ouput:

[[-1,-1],[-1,-1]]

division

List list1 = [[1, 1],[1, 1]];
List list2 = [[2, 2],[0, 2]];
var division = m2d.subtraction(division,list2);
print(division);

ouput:

[[0.5,Infinity],[0.5,0.5]]

dot operation

List list1 = [[1,2],[3,4]];
List list2 = [[11,12],[13,14]];
var dot = m2d.dot(division,list2);
print(dot);

ouput:

[[37, 40], [85, 92]]

arange

var arange = m2d.arange(8);
print(arange);

ouput:

[[0,1,2,3,4,5,6,7,8]]

sum

var list = [[2,2],[2,2]];
var sum = m2d.sum(list);
print(sum);

ouput:

8

reshape

List list = [[0, 1, 2, 3, 4, 5, 6, 7]];
list = list.reshape(2,4);
print(list);

ouput:

[[0, 1, 2, 3], [4, 5, 6, 7]]

linspace

var linspace = m2d.linspace(2, 3, 5);
print(linspace);

ouput:

[2.0, 2.25, 2.5, 2.75, 3.0]

diagonal

List list = [[1,1,1],[2,2,2],[3,3,3]];
print(list.diagonal);

ouput:

[1,2,3]

compare

var list = [[1,1,1],[2,2,2],[3,3,3]];
var compare = m2d.compare(list,'>',2);
print(compare);

ouput:

[[false, false, false], [false, false, false], [true, true, true]]

concatenate

axis 0
final l1 = [
    [1, 1, 1],
    [1, 1, 1],
    [1, 1, 1]
  ];
  final l2 = [
    [0, 0, 0],
    [0, 0, 0],
    [0, 0, 0],
    [0, 0, 0],
  ];
  final l3 = m2d.concatenate(l1, l2);
  print(l3);

ouput:

[[1, 1, 1],
[1, 1, 1],
[1, 1, 1],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]
axis 1
final a1 = [
    [1, 1, 1, 1],
    [1, 1, 1, 1],
    [1, 1, 1, 1]
  ];
  final a2 = [
    [0, 0, 0, 0, 0,0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0,0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0,0, 0, 0, 0, 0]
  ];

  final a3 = m2d.concatenate(a2, a1, axis: 1);
  print(a3);

ouput:

[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1]]

zeros,ones and fill

var zeros = m2d.zeros(2,2);
print(zeros);

var ones = m2d.ones(2,2);
print(ones);

var anything = m2d.fill(2,2,'i love dart');
print(anything);

ouput:

[[0,0],[1,1]]

[[1,1],[1,1]]

[['i love dart','i love dart'],['i love dart','i love dart']]

min max

 final numbers = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
  ];
  print(numbers.min());
  print(numbers.min(axis: 1));
  print(numbers.min(axis: 0));
  print(numbers.max());
  print(numbers.max(axis: 1));
  print(numbers.max(axis: 0));

ouput:

[1]

[1, 4, 7]

[1, 2, 3]

[9]

[3, 6, 9]

[7, 8, 9]

slice


 var sliceArray = [
    [1, 2, 3, 4, 5],
    [6, 7, 8, 9, 10]
  ];

  var newArray = m2d.slice(sliceArray, [0, 2], [1, 4]);
  print(newArray);

ouput:

[[2, 3, 4],[7, 8, 9]]

Contribution

If you want to contribute to this project, you are always welcome! Just make a pull request and I will review it as soon as possible.

Features and bugs

If you have any problems or suggestions, please open an issue here.