Skip to content

Commit

Permalink
Merge 720bbc8 into 9065366
Browse files Browse the repository at this point in the history
  • Loading branch information
gyrdym committed Sep 28, 2019
2 parents 9065366 + 720bbc8 commit f9efe07
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 11 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,10 @@
# Changelog

## 12.1.0
- `Matrix`:
- `rowIndices` field added to the interface
- `columnIndices` field added to the interface

## 12.0.2
- `xrange` 0.0.8 version supported (`integers` function used instead of `ZRange`)

Expand Down
10 changes: 8 additions & 2 deletions lib/matrix.dart
Expand Up @@ -66,12 +66,18 @@ abstract class Matrix implements Iterable<Iterable<double>> {

DType get dtype;

/// Returns a generator of immutable row vectors of the matrix
/// Returns a lazy iterable of immutable row vectors of the matrix
Iterable<Vector> get rows;

/// Returns a generator of immutable column vectors of the matrix
/// Returns a lazy iterable of immutable column vectors of the matrix
Iterable<Vector> get columns;

/// Returns a lazy iterable of row indices
Iterable<int> get rowIndices;

/// Return a lazy iterable of column indices
Iterable<int> get columnIndices;

/// Returns a number of matrix row
int get rowsNum;

Expand Down
10 changes: 8 additions & 2 deletions lib/src/matrix/base_matrix.dart
Expand Up @@ -242,7 +242,13 @@ abstract class BaseMatrix with
Iterable<Vector> get rows => _dataManager.rowIndices.map(getRow);

@override
Iterable<Vector> get columns => _dataManager.colIndices.map(getColumn);
Iterable<Vector> get columns => _dataManager.columnIndices.map(getColumn);

@override
Iterable<int> get rowIndices => _dataManager.rowIndices;

@override
Iterable<int> get columnIndices => _dataManager.columnIndices;

@override
Matrix fastMap<T>(T mapper(T element)) {
Expand Down Expand Up @@ -287,7 +293,7 @@ abstract class BaseMatrix with
checkColumnsAndRowsNumber(this, matrix);
final source = List<double>(rowsNum * matrix.columnsNum);
for (final i in _dataManager.rowIndices) {
for (final j in (matrix as BaseMatrix)._dataManager.colIndices) {
for (final j in (matrix as BaseMatrix)._dataManager.columnIndices) {
final element = getRow(i).dot(matrix.getColumn(j));
source[i * matrix.columnsNum + j] = element;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/matrix/common/data_manager/data_manager.dart
Expand Up @@ -5,7 +5,7 @@ abstract class DataManager {
int get columnsNum;
Iterator<Iterable<double>> get iterator;
Iterable<int> get rowIndices;
Iterable<int> get colIndices;
Iterable<int> get columnIndices;
Vector getColumn(int index);
Vector getRow(int index);
List<double> getValues(int index, int length);
Expand Down
10 changes: 5 additions & 5 deletions lib/src/matrix/common/data_manager/data_manager_impl.dart
Expand Up @@ -18,7 +18,7 @@ class DataManagerImpl implements DataManager {
rowsNum = source.length,
columnsNum = getLengthOfFirstOrZero(source),
rowIndices = integers(0, source.length, upperClosed: false),
colIndices = integers(0, getLengthOfFirstOrZero(source),
columnIndices = integers(0, getLengthOfFirstOrZero(source),
upperClosed: false),
_rowsCache = List<Vector>(source.length),
_colsCache = List<Vector>(getLengthOfFirstOrZero(source)),
Expand All @@ -37,7 +37,7 @@ class DataManagerImpl implements DataManager {
rowsNum = source.length,
columnsNum = getLengthOfFirstOrZero(source),
rowIndices = integers(0, source.length, upperClosed: false),
colIndices = integers(0, getLengthOfFirstOrZero(source),
columnIndices = integers(0, getLengthOfFirstOrZero(source),
upperClosed: false),
_rowsCache = source.toList(growable: false),
_colsCache = List<Vector>(getLengthOfFirstOrZero(source)),
Expand All @@ -57,7 +57,7 @@ class DataManagerImpl implements DataManager {
columnsNum = source.length,
rowIndices = integers(0, getLengthOfFirstOrZero(source),
upperClosed: false),
colIndices = integers(0, source.length, upperClosed: false),
columnIndices = integers(0, source.length, upperClosed: false),
_rowsCache = List<Vector>(getLengthOfFirstOrZero(source)),
_colsCache = source.toList(growable: false),
_data = ByteData(source.length *
Expand All @@ -77,7 +77,7 @@ class DataManagerImpl implements DataManager {
rowsNum = rowsNum,
columnsNum = colsNum,
rowIndices = integers(0, rowsNum, upperClosed: false),
colIndices = integers(0, colsNum, upperClosed: false),
columnIndices = integers(0, colsNum, upperClosed: false),
_rowsCache = List<Vector>(rowsNum),
_colsCache = List<Vector>(colsNum),
_data = ByteData(rowsNum * colsNum * bytesPerElement) {
Expand All @@ -99,7 +99,7 @@ class DataManagerImpl implements DataManager {
final Iterable<int> rowIndices;

@override
final Iterable<int> colIndices;
final Iterable<int> columnIndices;

final List<Vector> _rowsCache;
final List<Vector> _colsCache;
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
@@ -1,6 +1,6 @@
name: ml_linalg
description: SIMD-based linear algebra (1 operation on 4 float32 values, 1 operation on 2 float64 values)
version: 12.0.2
version: 12.1.0
author: Ilia Gyrdymov <ilgyrd@gmail.com>
homepage: https://github.com/gyrdym/ml_linalg

Expand Down
36 changes: 36 additions & 0 deletions test/float32_matrix_test.dart
Expand Up @@ -1321,4 +1321,40 @@ void main() {
expect(actual, expected);
});
});

group('rowIndices', () {
test('should contain zero-based ordered iterable of row indices', () {
final matrix = Float32Matrix.fromList([
[ 1, 2, 3, 4],
[ 10, 20, 30, 40],
[100, 200, 300, 400],
]);

expect(matrix.rowIndices, equals([0, 1, 2]));
});

test('should contain empty iterable if the matrix is empty', () {
final matrix = Float32Matrix.fromList([]);

expect(matrix.rowIndices, isEmpty);
});
});

group('columnIndices', () {
test('should contain zero-based iterable of ordered column indices', () {
final matrix = Float32Matrix.fromList([
[ 1, 2, 3, 4],
[ 10, 20, 30, 40],
[100, 200, 300, 400],
]);

expect(matrix.columnIndices, equals([0, 1, 2, 3]));
});

test('should contain empty iterable if the matrix is empty', () {
final matrix = Float32Matrix.fromList([]);

expect(matrix.columnIndices, isEmpty);
});
});
}

0 comments on commit f9efe07

Please sign in to comment.