Skip to content

Commit

Permalink
Merge 5595e60 into 71089b6
Browse files Browse the repository at this point in the history
  • Loading branch information
gyrdym committed May 28, 2020
2 parents 71089b6 + 5595e60 commit 150cd6a
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,11 @@
# Changelog

## 12.12.0
- `Matrix`:
- `Matrix.exp` method added
- `Vector`:
- `Vector.exp` method added

## 12.11.0
- `Matrix`:
- `Matrix.pow` method added
Expand Down
29 changes: 29 additions & 0 deletions README.md
Expand Up @@ -22,6 +22,7 @@
- [Mean value](#mean-value)
- [Sum of all vector elements](#sum-of-all-vector-elements)
- [Element-wise power](#element-wise-power)
- [Element-wise exp](#element-wise-exp)
- [Dot product](#dot-product-of-two-vectors)
- [Sum of a vector and a scalar](#sum-of-a-vector-and-a-scalar)
- [Subtraction of a scalar from a vector](#subtraction-of-a-scalar-from-a-vector)
Expand Down Expand Up @@ -55,6 +56,7 @@
- [Getting max value of the matrix](#getting-max-value-of-the-matrix)
- [Getting min value of the matrix](#getting-min-value-of-the-matrix)
- [Matrix element-wise power](#matrix-element-wise-power)
- [Matrix element-wise exp](#matrix-element-wise-exp)
- [Matrix indexing and sampling](#matrix-indexing-and-sampling)
- [Add new columns to a matrix](#add-new-columns-to-a-matrix)
- [Matrix serialization/deserialization](#matrix-serializationdeserialization)
Expand Down Expand Up @@ -249,6 +251,16 @@ the difference is significant.
print(result); // [2 ^ 3 = 8.0, 3 ^ 3 = 27.0, 4 ^ 3 = 64.0, 5 ^3 = 125.0, 6 ^ 3 = 216.0]
````

### Element-wise exp
````Dart
import 'package:ml_linalg/linalg.dart';
final vector = Vector.fromList([2.0, 3.0, 4.0, 5.0, 6.0]);
final result = vector.exp();
print(result); // [e ^ 2, e ^ 3, e ^ 4, e ^ 5, e ^ 6]
````

#### Dot product of two vectors
````Dart
import 'package:ml_linalg/linalg.dart';
Expand Down Expand Up @@ -730,6 +742,23 @@ print(matrix1 - matrix2);
// [7 ^ 3 = 343, 8 ^ 3 = 512, 9 ^ 3 = 729]
````

#### Matrix element-wise exp
````Dart
import 'package:ml_linalg/linalg.dart';
final matrix = Matrix.fromList([
[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0],
[7.0, 8.0, 9.0],
]);
final result = matrix.exp();
print(result);
// [e ^ 1, e ^ 2, e ^ 3]
// [e ^ 4, e ^ 5, e ^ 6]
// [e ^ 7, e ^ 8, e ^ 9]
````

#### Matrix indexing and sampling
    To access a certain row vector of the matrix one may use `[]` operator:

Expand Down
@@ -0,0 +1,37 @@
// Approx. 20 seconds (MacBook Air mid 2017)

import 'package:benchmark_harness/benchmark_harness.dart';
import 'package:ml_linalg/dtype.dart';
import 'package:ml_linalg/vector.dart';

const amountOfElements = 10000000;

class Float32x4VectorExpBenchmark extends BenchmarkBase {
Float32x4VectorExpBenchmark()
: super('Vector `exp` method; $amountOfElements elements');

Vector vector;

static void main() {
Float32x4VectorExpBenchmark().report();
}

@override
void run() {
vector.exp();
}

@override
void setup() {
vector = Vector.randomFilled(amountOfElements,
seed: 1,
min: -1000,
max: 1000,
dtype: DType.float32,
);
}
}

void main() {
Float32x4VectorExpBenchmark.main();
}
4 changes: 4 additions & 0 deletions lib/matrix.dart
Expand Up @@ -452,6 +452,10 @@ abstract class Matrix implements Iterable<Iterable<double>> {
/// power, since it is a slow operation
Matrix pow(num exponent);

/// Creates a new [Matrix] composed of Euler's numbers raised to powers which
/// are the elements of this [Matrix]
Matrix exp();

/// Returns a serializable map
Map<String, dynamic> toJson();
}
7 changes: 7 additions & 0 deletions lib/src/matrix/matrix_impl.dart
Expand Up @@ -336,6 +336,13 @@ class MatrixImpl with IterableMixin<Iterable<double>>, MatrixValidatorMixin
: Matrix.fromColumns(columns.map(
(column) => column.pow(exponent)).toList(), dtype: dtype);

@override
Matrix exp() => _dataManager.areAllRowsCached
? Matrix.fromRows(rows.map(
(row) => row.exp()).toList(), dtype: dtype)
: Matrix.fromColumns(columns.map(
(column) => column.exp()).toList(), dtype: dtype);

@override
Map<String, dynamic> toJson() => matrixToJson(this);

Expand Down
10 changes: 10 additions & 0 deletions lib/src/vector/float32x4_vector.dart
Expand Up @@ -245,6 +245,16 @@ class Float32x4Vector with IterableMixin<double> implements Vector {
@override
Vector pow(num exponent) => _elementWisePow(exponent);

@override
Vector exp() {
final source = Float32List(length);
for (int i = 0; i < length; i++) {
source[i] = math.exp(_innerTypedList[i]);
}

return Vector.fromList(source, dtype: dtype);
}

@override
@deprecated
Vector toIntegerPower(int power) => pow(power);
Expand Down
10 changes: 10 additions & 0 deletions lib/src/vector/float64x2_vector.dart
Expand Up @@ -247,6 +247,16 @@ class Float64x2Vector with IterableMixin<double> implements Vector {
@override
Vector pow(num exponent) => _elementWisePow(exponent);

@override
Vector exp() {
final source = Float64List(length);
for (int i = 0; i < length; i++) {
source[i] = math.exp(_innerTypedList[i]);
}

return Vector.fromList(source, dtype: dtype);
}

@override
@deprecated
Vector toIntegerPower(int power) => pow(power);
Expand Down
4 changes: 4 additions & 0 deletions lib/vector.dart
Expand Up @@ -309,6 +309,10 @@ abstract class Vector implements Iterable<double> {
/// a slow operation
Vector pow(num exponent);

/// Creates a new [Vector] composed of Euler's numbers raised to powers which
/// are the elements of this [Vector]
Vector exp();

/// Returns a new vector where the elements are absolute values of the
/// original vector's elements
Vector abs({bool skipCaching = false});
Expand Down
8 changes: 8 additions & 0 deletions test/integration_test/matrix/methods/exp/exp_test.dart
@@ -0,0 +1,8 @@
import 'package:ml_linalg/dtype.dart';

import 'exp_test_group_factory.dart';

void main() {
matrixExpTestGroupFactory(DType.float32);
matrixExpTestGroupFactory(DType.float64);
}
@@ -0,0 +1,26 @@
import 'dart:math' as math;
import 'package:ml_linalg/dtype.dart';
import 'package:ml_linalg/linalg.dart';
import 'package:ml_tech/unit_testing/matchers/iterable_2d_almost_equal_to.dart';
import 'package:test/test.dart';

import '../../../../dtype_to_title.dart';

void matrixExpTestGroupFactory(DType dtype) =>
group(dtypeToMatrixTestTitle[dtype], () {
group('exp method', () {
test('should raise euler\'s numbers to elements of matrix', () {
final matrix = Matrix.fromList([
[1, 2, 3, 4],
[5, 6, 7, 8],
], dtype: dtype);
final result = matrix.exp();

expect(result, iterable2dAlmostEqualTo([
[math.exp(1), math.exp(2), math.exp(3), math.exp(4)],
[math.exp(5), math.exp(6), math.exp(7), math.exp(8)],
], 1e-3));
expect(result.dtype, dtype);
});
});
});
8 changes: 8 additions & 0 deletions test/integration_test/vector/methods/exp/exp_test.dart
@@ -0,0 +1,8 @@
import 'package:ml_linalg/dtype.dart';

import 'exp_test_group_factory.dart';

void main() {
vectorExpTestGroupFactory(DType.float32);
vectorExpTestGroupFactory(DType.float64);
}
@@ -0,0 +1,20 @@
import 'package:ml_linalg/dtype.dart';
import 'package:ml_linalg/vector.dart';
import 'package:ml_tech/unit_testing/matchers/iterable_almost_equal_to.dart';
import 'package:test/test.dart';

import '../../../../dtype_to_title.dart';

void vectorExpTestGroupFactory(DType dtype) =>
group(dtypeToVectorTestTitle[dtype], () {
group('exp method', () {
test('should raise euler\'s numbers to elements of vector', () {
final vector = Vector.fromList([1.0, 2.0, 3.0, 4.0, 5.0],
dtype: dtype);
final result = vector.exp();

expect(result, iterableAlmostEqualTo([
2.7182, 7.3890, 20.08553, 54.5981, 148.4131], 1e-3));
});
});
});

0 comments on commit 150cd6a

Please sign in to comment.