ml_linalg 2.2.1

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • new61

Build Status

Linear algebra with Dart for machine learning

Table of contents

<a name="vectors"></a>Vectors

<a name="vectors_introduction"></a>A couple of words about the underlying vector architecture

All vector operations are supported by SIMD (single instruction, multiple data) computation architecture, so this library presents a high performance SIMD vector class, based on Float32x4 - Float32x4Vector. However, you cannot use it directly in your project. To create an instance of the vector, just import Float32x4VectorFactory and instantiate a vector via the factory. Most of operations in the vector are performed in four "threads". This kind of concurrency is reached by special 128-bit processor registers, which are used directly by program code. For better understanding of the topic please read the article.

<a name="vector_operations_examples"></a>Vector operations examples

At the present moment most common vector operations are implemented:

<a name="vector_addition"></a>Vectors sum
  import 'package:ml_linalg/linalg.dart';

  final vector1 = Float32x4VectorFactory.from([1.0, 2.0, 3.0, 4.0, 5.0]);
  final vector2 = Float32x4VectorFactory.from([2.0, 3.0, 4.0, 5.0, 6.0]);
  final result = vector1 + vector2;
  print(result.toList()); // [3.0, 5.0, 7.0, 9.0, 11.0]
<a name="vector_subtraction"></a>Vector subtraction
  import 'package:ml_linalg/linalg.dart';

  final vector1 = Float32x4VectorFactory.from([4.0, 5.0, 6.0, 7.0, 8.0]);
  final vector2 = Float32x4VectorFactory.from([2.0, 3.0, 2.0, 3.0, 2.0]);
  final result = vector1 - vector2;
  print(result.toList()); // [2.0, 2.0, 4.0, 4.0, 6.0]
<a name="vector_element_wise_mult"></a>Element wise vector by vector multiplication
  import 'package:ml_linalg/linalg.dart';

  final vector1 = Float32x4VectorFactory.from([1.0, 2.0, 3.0, 4.0, 5.0]);
  final vector2 = Float32x4VectorFactory.from([2.0, 3.0, 4.0, 5.0, 6.0]);
  final result = vector1 * vector2;
  print(result.toList()); // [2.0, 6.0, 12.0, 20.0, 30.0]
<a name="vector_element_wise_div"></a>Element wise vector by vector division
  import 'package:ml_linalg/linalg.dart';

  final vector1 = Float32x4VectorFactory.from([6.0, 12.0, 24.0, 48.0, 96.0]);
  final vector2 = Float32x4VectorFactory.from([3.0, 4.0, 6.0, 8.0, 12.0]);
  final result = vector1 / vector2;
  print(result.toList()); // [2.0, 3.0, 4.0, 6.0, 8.0]
<a name="euclidean_norm"></a>Euclidean norm
  import 'package:ml_linalg/linalg.dart';

  final vector1 = Float32x4VectorFactory.from([2.0, 3.0, 4.0, 5.0, 6.0]);
  final result = vector1.norm();
  print(result); // sqrt(2^2 + 3^2 + 4^2 + 5^2 + 6^2) = sqrt(90) ~~ 9.48
<a name="manhattan_norm"></a>Manhattan norm
  import 'package:ml_linalg/linalg.dart';

  final vector1 = Float32x4VectorFactory.from([2.0, 3.0, 4.0, 5.0, 6.0]);
  final result = vector1.norm(Norm.manhattan);
  print(result); // 2 + 3 + 4 + 5 + 6 = 20.0

<a name="mean_value"></a>

Mean value
  import 'package:linalg/ml_linalg.dart';

  final vector1 = Float32x4VectorFactory.from([2.0, 3.0, 4.0, 5.0, 6.0]);
  final result = vector1.mean();
  print(result); // (2 + 3 + 4 + 5 + 6) / 5 = 4.0
<a name="vector_sum"></a>Sum of all vector elements
  import 'package:ml_linalg/linalg.dart';

  final vector1 = Float32x4VectorFactory.from([2.0, 3.0, 4.0, 5.0, 6.0]);
  final result = vector1.sum();
  print(result); // 2 + 3 + 4 + 5 + 6 = 20.0 (equivalent to Manhattan norm)
<a name="vector_dot_product"></a>Dot product of two vectors
  import 'package:ml_linalg/linalg.dart';

  final vector1 = Float32x4VectorFactory.from([1.0, 2.0, 3.0, 4.0, 5.0]);
  final vector2 = Float32x4VectorFactory.from([2.0, 3.0, 4.0, 5.0, 6.0]);
  final result = vector1.dot(vector2);
  print(result); // 1.0 * 2.0 + 2.0 * 3.0 + 3.0 * 4.0 + 4.0 * 5.0 + 5.0 * 6.0 = 70.0
<a name="vector_scalar_add"></a>Sum of a vector and a scalar
  import 'package:linalg/ml_linalg.dart';

  final vector1 = Float32x4VectorFactory.from([1.0, 2.0, 3.0, 4.0, 5.0]);
  final scalar = 5.0;
  final result = vector1 + scalar;
  print(result.toList()); // [6.0, 7.0, 8.0, 9.0, 10.0]
<a name="vector_scalar_sub"></a>Subtraction of a scalar from a vector
  import 'package:ml_linalg/linalg.dart';

  final vector1 = Float32x4VectorFactory.from([1.0, 2.0, 3.0, 4.0, 5.0]);
  final scalar = 5.0;
  final result = vector1 - scalar;
  print(result.toList()); // [-4.0, -3.0, -2.0, -1.0, 0.0]
<a name="vector_scalar_mul"></a>Multiplication (scaling) of a vector by a scalar
  import 'package:ml_linalg/linalg.dart';

  final vector1 = Float32x4VectorFactory.from([1.0, 2.0, 3.0, 4.0, 5.0]);
  final scalar = 5.0;
  final result = vector1 * scalar;
  print(result.toList()); // [5.0, 10.0, 15.0, 20.0, 25.0]
<a name="vector_scalar_div"></a>Division (scaling) of a vector by a scalar value
  import 'package:ml_linalg/linalg.dart';

  final vector1 = Float32x4VectorFactory.from([25.0, 50.0, 75.0, 100.0, 125.0]);
  final scalar = 5.0;
  final result = vector1.scalarDiv(scalar);
  print(result.toList()); // [5.0, 10.0, 15.0, 20.0, 25.0]
<a name="vector_euclidean_dist"></a>Euclidean distance between two vectors
  import 'package:ml_linalg/linalg.dart';

  final vector1 = Float32x4VectorFactory.from([1.0, 2.0, 3.0, 4.0, 5.0]);
  final vector2 = Float32x4VectorFactory.from([2.0, 3.0, 4.0, 5.0, 6.0]);
  final result = vector1.distanceTo(vector2);
  print(result); // ~~2.23
<a name="vector_manhattan_dist"></a>Manhattan distance between two vectors
  import 'package:ml_linalg/linalg.dart';

  final vector1 = Float32x4VectorFactory.from([1.0, 2.0, 3.0, 4.0, 5.0]);
  final vector2 = Float32x4VectorFactory.from([2.0, 3.0, 4.0, 5.0, 6.0]);
  final result = vector1.distanceTo(vector2, Norm.manhattan);
  print(result); // 5.0

<a name="matrices"></a>Matrices

Also, a class for matrix is available. It is based on Float32x4 and Float32x4Vector types.

<a name="matrix_operations_examples"></a>Matrix operations examples

<a name="matrix_matrix_add"></a>Sum of a matrix and another matrix
import 'package:ml_linalg/linalg.dart';

final matrix1 = Float32x4Matrix.from([
  [1.0, 2.0, 3.0, 4.0],
  [5.0, 6.0, 7.0, 8.0],
  [9.0, .0, -2.0, -3.0],
]);
final matrix2 = Float32x4Matrix.from([
  [10.0, 20.0, 30.0, 40.0],
  [-5.0, 16.0, 2.0, 18.0],
  [2.0, -1.0, -2.0, -7.0],
]);
print(matrix1 + matrix2);
// [
//  [11.0, 22.0, 33.0, 44.0],
//  [0.0, 22.0, 9.0, 26.0],
//  [11.0, -1.0, -4.0, -10.0],
// ];
<a name="matrix_scalar_add"></a>Sum of a matrix and a scalar
import 'package:ml_linalg/linalg.dart';

final matrix = Float32x4Matrix.from([
  [1.0, 2.0, 3.0, 4.0],
  [5.0, 6.0, 7.0, 8.0],
  [9.0, .0, -2.0, -3.0],
]);
print(matrix + 7);
//  [
//    [8.0, 9.0, 10.0, 11.0],
//    [12.0, 13.0, 14.0, 15.0],
//    [16.0, 7.0, 5.0, 4.0],
//  ];
<a name="matrix_vector_mul"></a>Multiplication of a matrix and a vector
  import 'package:ml_linalg/linalg.dart';

  final matrix = Float32x4Matrix.from([
    [1.0, 2.0, 3.0, 4.0],
    [5.0, 6.0, 7.0, 8.0],
    [9.0, .0, -2.0, -3.0],
  ]);
  final vector = Float32x4Vector.from([2.0, 3.0, 4.0, 5.0]);
  final result = matrix * vector;
  print(result); 
  // a vector-column [
  //  [40],
  //  [96],
  //  [-5],
  //]
<a name="matrix_matrix_mul"></a>Multiplication of a matrix and another matrix
  import 'package:ml_linalg/linalg.dart';

  final matrix1 = Float32x4Matrix.from([
    [1.0, 2.0, 3.0, 4.0],
    [5.0, 6.0, 7.0, 8.0],
    [9.0, .0, -2.0, -3.0],
  ]);
  final matrix2 = Float32x4Matrix.from([
    [1.0, 2.0],
    [5.0, 6.0],
    [9.0, .0],
    [-9.0, 1.0],
  ]);
  final result = matrix1 * matrix2;
  print(result);
  //[
  // [2.0, 18.0],
  // [26.0, 54.0],
  // [18.0, 15.0],
  //]
<a name="matrix_scalar_mul"></a>Multiplication of a matrix and a scalar
import 'package:ml_linalg/linalg.dart';

final matrix = Float32x4Matrix.from([
  [1.0, 2.0, 3.0, 4.0],
  [5.0, 6.0, 7.0, 8.0],
  [9.0, .0, -2.0, -3.0],
]);
print(matrix * 3);
// [
//   [3.0, 6.0, 9.0, 12.0],
//   [15.0, 18.0, 21.0, 24.0],
//   [27.0, .0, -6.0, -9.0],
// ];
<a name="matrix_matrix_sub"></a>Element wise matrices subtraction
import 'package:ml_linalg/linalg.dart';

final matrix1 = Float32x4Matrix.from([
  [1.0, 2.0, 3.0, 4.0],
  [5.0, 6.0, 7.0, 8.0],
  [9.0, .0, -2.0, -3.0],
]);
final matrix2 = Float32x4Matrix.from([
  [10.0, 20.0, 30.0, 40.0],
  [-5.0, 16.0, 2.0, 18.0],
  [2.0, -1.0, -2.0, -7.0],
]);
print(matrix1 - matrix2);
// [
//   [-9.0, -18.0, -27.0, -36.0],
//   [10.0, -10.0, 5.0, -10.0],
//   [7.0, 1.0, .0, 4.0],
// ];
<a name="matrix_transpose"></a>Matrix transposition
  import 'package:ml_linalg/linalg.dart';
  
  final matrix = Float32x4Matrix.from([
    [1.0, 2.0, 3.0, 4.0],
    [5.0, 6.0, 7.0, 8.0],
    [9.0, .0, -2.0, -3.0],
  ]);
  final result = matrix.transpose();
  print(result);
  //[
  // [1.0, 5.0, 9.0],
  // [2.0, 6.0, .0],
  // [3.0, 7.0, -2.0],
  // [4.0, 8.0, -3.0],
  //]
<a name="matrix_row_reduce"></a>Matrix row wise reduce
  import 'package:ml_linalg/linalg.dart';

  final matrix = Float32x4MatrixFactory.from([
    [1.0, 2.0, 3.0, 4.0],
    [5.0, 6.0, 7.0, 8.0],
  ]); 
  final reduced = matrix.reduceRows((combine, row) => combine + row);
  print(reduced); // [6.0, 8.0, 10.0, 12.0]
<a name="matrix_col_reduce"></a>Matrix column wise reduce
  import 'package:ml_linalg/linalg.dart';

  final matrix = Float32x4Matrix.from([
    [11.0, 12.0, 13.0, 14.0],
    [15.0, 16.0, 17.0, 18.0],
    [21.0, 22.0, 23.0, 24.0],
  ]);
  final result = matrix.reduceColumns((combine, vector) => combine + vector);
  print(result); // [50, 66, 90]
<a name="matrix_submatrix"></a>Submatrix (taking a lower dimension matrix of the current matrix)
  import 'package:ml_linalg/linalg.dart';

  final matrix = Float32x4Matrix.from([
    [11.0, 12.0, 13.0, 14.0],
    [15.0, 16.0, 17.0, 18.0],
    [21.0, 22.0, 23.0, 24.0],
    [24.0, 32.0, 53.0, 74.0],
  ]);
  final submatrix = matrix.submatrix(rows: Range(0, 2));
  print(submatrix);
  // [
  //  [11.0, 12.0, 13.0, 14.0],
  //  [15.0, 16.0, 17.0, 18.0],
  //];

Changelog

2.2.1

  • Travis integration added

2.2.0

  • Support matrices in vector operations

2.1.0

  • Column and row vectors added

2.0.0

  • Unnecessary generic type argument removed from MLMatrix class
  • Matrix logic split into separate mixins

1.3.0

  • MLVectorMixin added, benchmark directory reorganized

1.2.0

  • Map functions added to matrix

1.1.0

  • Float32x4MatrixFactory extended

1.0.1

  • Readme updated

1.0.0

  • Library public release

example/main.dart

import 'package:ml_linalg/src/vector/float32/float32x4_vector.dart';
import 'package:ml_linalg/linalg.dart';

void main() {
  addition();
  subtraction();
  multiplication();
  division();
  euclideanNorm();
  manhattanNorm();
  mean();
  sum();
  dot();
  scalarAddition();
  scalarSubtraction();
  scalarMultiplication();
  scalarDivision();
  euclideanDistanceBetweenVectors();
  manhattanDistanceBetweenVectors();
}

void addition() {
  final vector1 = Float32x4Vector.from([1.0, 2.0, 3.0, 4.0, 5.0]);
  final vector2 = Float32x4Vector.from([2.0, 3.0, 4.0, 5.0, 6.0]);
  final result = vector1 + vector2;
  print(result); // [3.0, 5.0, 7.0, 9.0, 11.0]
}

void subtraction() {
  final vector1 = Float32x4Vector.from([4.0, 5.0, 6.0, 7.0, 8.0]);
  final vector2 = Float32x4Vector.from([2.0, 3.0, 2.0, 3.0, 2.0]);
  final result = vector1 - vector2;
  print(result); // [2.0, 2.0, 4.0, 4.0, 6.0]
}

void multiplication() {
  final vector1 = Float32x4Vector.from([1.0, 2.0, 3.0, 4.0, 5.0]);
  final vector2 = Float32x4Vector.from([2.0, 3.0, 4.0, 5.0, 6.0]);
  final result = vector1 * vector2;
  print(result); // [2.0, 6.0, 12.0, 20.0, 30.0]
}

void division() {
  final vector1 = Float32x4Vector.from([6.0, 12.0, 24.0, 48.0, 96.0]);
  final vector2 = Float32x4Vector.from([3.0, 4.0, 6.0, 8.0, 12.0]);
  final result = vector1 / vector2;
  print(result); // [2.0, 3.0, 4.0, 6.0, 8.0]
}

void euclideanNorm() {
  final vector1 = Float32x4Vector.from([2.0, 3.0, 4.0, 5.0, 6.0]);
  final result = vector1.norm();
  print(result); // sqrt(2^2 + 3^2 + 4^2 + 5^2 + 6^2) = sqrt(90) ~~ 9.48
}

void manhattanNorm() {
  final vector1 = Float32x4Vector.from([2.0, 3.0, 4.0, 5.0, 6.0]);
  final result = vector1.norm(Norm.manhattan);
  print(result); // 2 + 3 + 4 + 5 + 6 = 20.0
}

void mean() {
  final vector1 = Float32x4Vector.from([2.0, 3.0, 4.0, 5.0, 6.0]);
  final result = vector1.mean();
  print(result); // (2 + 3 + 4 + 5 + 6) / 5 = 4.0
}

void sum() {
  final vector1 = Float32x4Vector.from([2.0, 3.0, 4.0, 5.0, 6.0]);
  final result = vector1.sum();
  print(result); // 2 + 3 + 4 + 5 + 6 = 20.0 (equivalent to Manhattan norm)
}

void dot() {
  final vector1 = Float32x4Vector.from([1.0, 2.0, 3.0, 4.0, 5.0]);
  final vector2 = Float32x4Vector.from([2.0, 3.0, 4.0, 5.0, 6.0]);
  final result = vector1.dot(vector2);
  print(result); // 1.0 * 2.0 + 2.0 * 3.0 + 3.0 * 4.0 + 4.0 * 5.0 + 5.0 * 6.0 = 70.0
}

void scalarAddition() {
  final vector1 = Float32x4Vector.from([1.0, 2.0, 3.0, 4.0, 5.0]);
  final scalar = 5.0;
  final result = vector1 + scalar;
  print(result); // [6.0, 7.0, 8.0, 9.0, 10.0]
}

void scalarSubtraction() {
  final vector1 = Float32x4Vector.from([1.0, 2.0, 3.0, 4.0, 5.0]);
  final scalar = 5.0;
  final result = vector1 / scalar;
  print(result); // [-4.0, -3.0, -2.0, -1.0, 0.0]
}

void scalarMultiplication() {
  final vector1 = Float32x4Vector.from([1.0, 2.0, 3.0, 4.0, 5.0]);
  final scalar = 5.0;
  final result = vector1 * scalar;
  print(result); // [5.0, 10.0, 15.0, 20.0, 25.0]
}

void scalarDivision() {
  final vector1 = Float32x4Vector.from([25.0, 50.0, 75.0, 100.0, 125.0]);
  final scalar = 5.0;
  final result = vector1 / scalar;
  print(result); // [5.0, 10.0, 15.0, 20.0, 25.0]
}

void euclideanDistanceBetweenVectors() {
  final vector1 = Float32x4Vector.from([1.0, 2.0, 3.0, 4.0, 5.0]);
  final vector2 = Float32x4Vector.from([2.0, 3.0, 4.0, 5.0, 6.0]);
  final result = vector1.distanceTo(vector2);
  print(result); // ~~2.23
}

void manhattanDistanceBetweenVectors() {
  final vector1 = Float32x4Vector.from([1.0, 2.0, 3.0, 4.0, 5.0]);
  final vector2 = Float32x4Vector.from([2.0, 3.0, 4.0, 5.0, 6.0]);
  final result = vector1.distanceTo(vector2, Norm.manhattan);
  print(result); // 5.0
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  ml_linalg: ^2.2.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:ml_linalg/float32x4_matrix_factory.dart';
import 'package:ml_linalg/float32x4_vector_factory.dart';
import 'package:ml_linalg/linalg.dart';
import 'package:ml_linalg/matrix.dart';
import 'package:ml_linalg/norm.dart';
import 'package:ml_linalg/range.dart';
import 'package:ml_linalg/vector.dart';
import 'package:ml_linalg/vector_type.dart';
  
Version Uploaded Documentation Archive
2.2.1 Dec 11, 2018 Go to the documentation of ml_linalg 2.2.1 Download ml_linalg 2.2.1 archive
2.2.0 Dec 9, 2018 Go to the documentation of ml_linalg 2.2.0 Download ml_linalg 2.2.0 archive
2.1.0 Dec 7, 2018 Go to the documentation of ml_linalg 2.1.0 Download ml_linalg 2.1.0 archive
2.0.0 Dec 7, 2018 Go to the documentation of ml_linalg 2.0.0 Download ml_linalg 2.0.0 archive
1.3.0 Dec 6, 2018 Go to the documentation of ml_linalg 1.3.0 Download ml_linalg 1.3.0 archive
1.2.0 Dec 5, 2018 Go to the documentation of ml_linalg 1.2.0 Download ml_linalg 1.2.0 archive
1.1.0 Dec 4, 2018 Go to the documentation of ml_linalg 1.1.0 Download ml_linalg 1.1.0 archive
1.0.1 Dec 4, 2018 Go to the documentation of ml_linalg 1.0.1 Download ml_linalg 1.0.1 archive
1.0.0 Dec 3, 2018 Go to the documentation of ml_linalg 1.0.0 Download ml_linalg 1.0.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
23
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
61
Learn more about scoring.

We analyzed this package on Dec 11, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.7

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in libraries.

Health suggestions

Fix lib/vector.dart. (-0.50 points)

Analysis of lib/vector.dart reported 1 hint:

line 1 col 8: Unused import: 'package:ml_linalg/matrix.dart'.

Format lib/float32x4_vector_factory.dart.

Run dartfmt to format lib/float32x4_vector_factory.dart.

Format lib/matrix.dart.

Run dartfmt to format lib/matrix.dart.

Fix additional 14 files with analysis or formatting issues.

Additional issues in the following files:

  • lib/src/matrix/float32/float32_matrix_iterator.dart (Run dartfmt to format lib/src/matrix/float32/float32_matrix_iterator.dart.)
  • lib/src/matrix/float32/float32x4_matrix.dart (Run dartfmt to format lib/src/matrix/float32/float32x4_matrix.dart.)
  • lib/src/matrix/float32/float32x4_matrix_factory_mixin.dart (Run dartfmt to format lib/src/matrix/float32/float32x4_matrix_factory_mixin.dart.)
  • lib/src/matrix/ml_matrix_factory.dart (Run dartfmt to format lib/src/matrix/ml_matrix_factory.dart.)
  • lib/src/matrix/ml_matrix_mixin.dart (Run dartfmt to format lib/src/matrix/ml_matrix_mixin.dart.)
  • lib/src/matrix/ml_matrix_validatior.dart (Run dartfmt to format lib/src/matrix/ml_matrix_validatior.dart.)
  • lib/src/matrix/ml_matrix_validator_mixin.dart (Run dartfmt to format lib/src/matrix/ml_matrix_validator_mixin.dart.)
  • lib/src/vector/float32/float32_helper_mixin.dart (Run dartfmt to format lib/src/vector/float32/float32_helper_mixin.dart.)
  • lib/src/vector/float32/float32x4_data_store_mixin.dart (Run dartfmt to format lib/src/vector/float32/float32x4_data_store_mixin.dart.)
  • lib/src/vector/float32/float32x4_helper_mixin.dart (Run dartfmt to format lib/src/vector/float32/float32x4_helper_mixin.dart.)
  • lib/src/vector/float32/float32x4_vector.dart (Run dartfmt to format lib/src/vector/float32/float32x4_vector.dart.)
  • lib/src/vector/float32/float32x4_vector_factory_mixin.dart (Run dartfmt to format lib/src/vector/float32/float32x4_vector_factory_mixin.dart.)
  • lib/src/vector/ml_vector_factory.dart (Run dartfmt to format lib/src/vector/ml_vector_factory.dart.)
  • lib/src/vector/ml_vector_mixin.dart (Run dartfmt to format lib/src/vector/ml_vector_mixin.dart.)

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
Dev dependencies
benchmark_harness >=1.0.0 <2.0.0
build_runner ^0.10.0
build_test ^0.10.2
dartdoc any
mockito ^3.0.0
test ^1.2.0