Skip to content

Commit

Permalink
matrix max/min value getting
Browse files Browse the repository at this point in the history
  • Loading branch information
gyrdym committed Feb 18, 2019
1 parent def14a9 commit 52fb26c
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 120 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,8 @@
# Changelog

## 5.1.0
- `max` and `min` methods added for matrix

## 5.0.1
- Travis integration added
- `dartfmt` task added
Expand Down
32 changes: 32 additions & 0 deletions README.md
Expand Up @@ -39,6 +39,8 @@
- [Matrix row wise reduce](#matrix-row-wise-reduce)
- [Matrix column wise reduce](#matrix-column-wise-reduce)
- [Submatrix](#submatrix-taking-a-lower-dimension-matrix-of-the-current-matrix)
- [Getting max value of the matrix](#getting-max-value-of-the-matrix)
- [Getting min value of the matrix](#getting-min-value-of-the-matrix)
+ [Vectorized non-mathematical matrix methods](#vectorized-non-mathematical-matrix-methods)
- [Matrix indexing](#matrix-indexing)
+ [Contacts](#contacts)
Expand Down Expand Up @@ -417,6 +419,36 @@ print(matrix1 - matrix2);
//];
````

##### Getting max value of the matrix
````Dart
import 'package:ml_linalg/linalg.dart';
final matrix = MLMatrix.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 maxValue = matrix.max();
print(maxValue);
// 74.0
````

##### Getting min value of the matrix
````Dart
import 'package:ml_linalg/linalg.dart';
final matrix = MLMatrix.from([
[11.0, 12.0, 13.0, 14.0],
[15.0, 16.0, 0.0, 18.0],
[21.0, 22.0, -23.0, 24.0],
[24.0, 32.0, 53.0, 74.0],
]);
final minValue = matrix.min();
print(minValue);
// -23.0
````

### Vectorized non-mathematical matrix methods

#### Matrix indexing
Expand Down
23 changes: 17 additions & 6 deletions lib/matrix.dart
Expand Up @@ -48,10 +48,12 @@ abstract class MLMatrix {
/// Performs sum of the matrix and a matrix/ a vector/ a scalar/ whatever
MLMatrix operator +(Object value);

/// Performs subtraction of the matrix and a matrix/ a vector/ a scalar/ whatever
/// Performs subtraction of the matrix and a matrix/ a vector/ a scalar/
/// whatever
MLMatrix operator -(Object value);

/// Performs multiplication of the matrix and a matrix/ a vector/ a scalar/ whatever
/// Performs multiplication of the matrix and a matrix/ a vector/ a scalar/
/// whatever
MLMatrix operator *(Object value);

/// Performs transposition of the matrix
Expand All @@ -77,11 +79,20 @@ abstract class MLMatrix {
MLVector reduceRows(MLVector combiner(MLVector combine, MLVector vector),
{MLVector initValue});

/// Creates a new matrix, efficiently iterating through all the matrix elements (several floating point elements in a
/// time) and applying the [mapper] function
/// Creates a new matrix, efficiently iterating through all the matrix
/// elements (several floating point elements in a time) and applying the
/// [mapper] function
MLMatrix fastMap<E>(E mapper(E columnElement));

/// Tries to convert the matrix to a vector. It fails, if the matrix's both numbers of columns and rows are greater
/// than 1
/// Tries to convert the matrix to a vector.
///
/// It fails, if the matrix's both numbers of columns and rows are greater
/// than `1`
MLVector toVector({bool mutable = false});

/// Returns max value of the matrix
double max();

/// Return min value of the matrix
double min();
}
1 change: 1 addition & 0 deletions lib/norm.dart
@@ -1,3 +1,4 @@
/// Vector norm types
///
/// Actually, vector norm defines a metric for considered space
enum Norm { euclidean, manhattan }
27 changes: 23 additions & 4 deletions lib/src/matrix/ml_matrix_mixin.dart
Expand Up @@ -41,8 +41,11 @@ abstract class MLMatrixMixin<E, S extends List<E>>
}

/// Mathematical matrix multiplication
/// The main rule: let N be a number of columns, so the multiplication is available only for
/// XxN * NxY matrices, that causes XxY matrix
///
/// The main rule:
///
/// let `N` be a number of columns, so the multiplication is
/// available only for X by N * N by Y matrices, that causes X by Y matrix
@override
MLMatrix operator *(Object value) {
if (value is MLVector) {
Expand Down Expand Up @@ -173,12 +176,27 @@ abstract class MLMatrixMixin<E, S extends List<E>>
break;
}
result =
'$result${row.take(columnsLimit).toString().replaceAll(RegExp(r'\)$'), '')}$eol\n';
'$result${row.take(columnsLimit).toString()
.replaceAll(RegExp(r'\)$'), '')}$eol\n';
i++;
}
return result;
}

@override
double max() => _findExtrema((MLVector row) => row.max());

@override
double min() => _findExtrema((MLVector row) => row.min());

double _findExtrema(double callback(MLVector vector)) {
int i = 0;
return callback(reduceRows((MLVector result, MLVector row) {
result[i++] = callback(row);
return result;
}, initValue: MLVector.zero(rowsNum, isMutable: true)));
}

MLVector _reduce(
MLVector Function(MLVector combine, MLVector vector) combiner,
int length,
Expand All @@ -195,7 +213,8 @@ abstract class MLMatrixMixin<E, S extends List<E>>
MLMatrix _matrixVectorMul(MLVector vector) {
if (vector.length != columnsNum) {
throw Exception(
'The dimension of the vector ${vector} and the columns number of matrix ${this} mismatch');
'The dimension of the vector ${vector} and the columns number of '
'matrix ${this} mismatch');
}
final generateElementFn = (int i) => vector.dot(getRow(i));
final source = List<double>.generate(rowsNum, generateElementFn);
Expand Down
38 changes: 25 additions & 13 deletions lib/vector.dart
Expand Up @@ -6,9 +6,11 @@ import 'norm.dart';

/// An algebraic vector (ordered set of elements).
abstract class MLVector implements Iterable<double> {
/// Creates a vector from a collection [source]. It converts the collection of [double]-type elements into a
/// collection of [Float32x4] elements. If [isMutable] is true, one can alter the vector, for example, via `[]=`
/// operator
/// Creates a vector from a collection [source].
///
/// It converts the collection of [double]-type elements into a collection of
/// [Float32x4] elements. If [isMutable] is true, one can alter the vector,
/// for example, via `[]=` operator
factory MLVector.from(Iterable<double> source,
{bool isMutable, Type dtype = Float32x4}) {
switch (dtype) {
Expand All @@ -19,8 +21,10 @@ abstract class MLVector implements Iterable<double> {
}
}

/// Creates a vector of length, equal to [length], filled with [value]. If [isMutable] is true, one can alter the
/// vector, for example, via `[]=` operator
/// Creates a vector of length, equal to [length], filled with [value].
///
/// If [isMutable] is true, one can alter the vector, for example, via `[]=`
/// operator
factory MLVector.filled(int length, double value,
{bool isMutable, Type dtype = Float32x4}) {
switch (dtype) {
Expand All @@ -31,8 +35,10 @@ abstract class MLVector implements Iterable<double> {
}
}

/// Creates a vector of length, equal to [length], filled with zeroes. If [isMutable] is true, one can alter the
/// vector, for example, via `[]=` operator
/// Creates a vector of length, equal to [length], filled with zeroes.
///
/// If [isMutable] is true, one can alter the vector, for example, via `[]=`
/// operator
factory MLVector.zero(int length, {bool isMutable, Type dtype = Float32x4}) {
switch (dtype) {
case Float32x4:
Expand All @@ -42,8 +48,11 @@ abstract class MLVector implements Iterable<double> {
}
}

/// Creates a vector of length, equal to [length], filled with random values, generated from randomizer with seed,
/// equal to [seed]. If [isMutable] is true, one can alter the vector, for example, via `[]=` operator
/// Creates a vector of length, equal to [length], filled with random values,
/// generated from randomizer with seed, equal to [seed].
///
/// If [isMutable] is true, one can alter the vector, for example, via `[]=`
/// operator
factory MLVector.randomFilled(int length,
{int seed, bool isMutable, Type dtype = Float32x4}) {
switch (dtype) {
Expand All @@ -55,7 +64,7 @@ abstract class MLVector implements Iterable<double> {
}
}

/// Can someone mutate the vector e.g. via []= operator
/// Can someone mutate the vector, e.g. via []= operator, or not
bool get isMutable;

/// Indexed access to a vector's element
Expand All @@ -76,7 +85,8 @@ abstract class MLVector implements Iterable<double> {
/// Element-wise division
MLVector operator /(Object value);

/// Creates a new [MLVector] containing elements of this [MLVector] raised to the integer [power]
/// Creates a new [MLVector] containing elements of this [MLVector] raised to
/// the integer [power]
MLVector toIntegerPower(int power);

/// Returns a vector with absolute value of each vector element
Expand All @@ -85,7 +95,8 @@ abstract class MLVector implements Iterable<double> {
/// Returns a dot (inner) product of [this] and [vector]
double dot(MLVector vector);

/// Returns a distance between [this] and [vector] with vector norm type considering
/// Returns a distance between [this] and [vector] with vector norm type
/// considering
double distanceTo(MLVector vector, [Norm norm = Norm.euclidean]);

/// Returns a mean value of [this] vector
Expand All @@ -103,7 +114,8 @@ abstract class MLVector implements Iterable<double> {
/// Returns maximum element
double min();

/// Returns a vector composed of elements which are located on the passed indexes
/// Returns a vector composed of elements which are located on the passed
/// indexes
MLVector query(Iterable<int> indexes);

/// Returns a vector composed of unique vector's elements
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
@@ -1,6 +1,6 @@
name: ml_linalg
description: SIMD-based linear algebra with dart for machine learning purposes
version: 5.0.1
version: 5.1.0
author: Ilia Gyrdymov <ilgyrd@gmail.com>
homepage: https://github.com/gyrdym/ml_linalg

Expand Down

0 comments on commit 52fb26c

Please sign in to comment.