diff --git a/CHANGELOG.md b/CHANGELOG.md index 39ae6bf8..e3e36896 100644 --- a/CHANGELOG.md +++ b/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`) diff --git a/lib/matrix.dart b/lib/matrix.dart index ff7e8c1c..5e2bab8d 100644 --- a/lib/matrix.dart +++ b/lib/matrix.dart @@ -66,12 +66,18 @@ abstract class Matrix implements Iterable> { 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 get rows; - /// Returns a generator of immutable column vectors of the matrix + /// Returns a lazy iterable of immutable column vectors of the matrix Iterable get columns; + /// Returns a lazy iterable of row indices + Iterable get rowIndices; + + /// Return a lazy iterable of column indices + Iterable get columnIndices; + /// Returns a number of matrix row int get rowsNum; diff --git a/lib/src/matrix/base_matrix.dart b/lib/src/matrix/base_matrix.dart index ee052464..856ab5cc 100644 --- a/lib/src/matrix/base_matrix.dart +++ b/lib/src/matrix/base_matrix.dart @@ -242,7 +242,13 @@ abstract class BaseMatrix with Iterable get rows => _dataManager.rowIndices.map(getRow); @override - Iterable get columns => _dataManager.colIndices.map(getColumn); + Iterable get columns => _dataManager.columnIndices.map(getColumn); + + @override + Iterable get rowIndices => _dataManager.rowIndices; + + @override + Iterable get columnIndices => _dataManager.columnIndices; @override Matrix fastMap(T mapper(T element)) { @@ -287,7 +293,7 @@ abstract class BaseMatrix with checkColumnsAndRowsNumber(this, matrix); final source = List(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; } diff --git a/lib/src/matrix/common/data_manager/data_manager.dart b/lib/src/matrix/common/data_manager/data_manager.dart index 89a63355..f6dfc100 100644 --- a/lib/src/matrix/common/data_manager/data_manager.dart +++ b/lib/src/matrix/common/data_manager/data_manager.dart @@ -5,7 +5,7 @@ abstract class DataManager { int get columnsNum; Iterator> get iterator; Iterable get rowIndices; - Iterable get colIndices; + Iterable get columnIndices; Vector getColumn(int index); Vector getRow(int index); List getValues(int index, int length); diff --git a/lib/src/matrix/common/data_manager/data_manager_impl.dart b/lib/src/matrix/common/data_manager/data_manager_impl.dart index 29903f06..4848df57 100644 --- a/lib/src/matrix/common/data_manager/data_manager_impl.dart +++ b/lib/src/matrix/common/data_manager/data_manager_impl.dart @@ -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(source.length), _colsCache = List(getLengthOfFirstOrZero(source)), @@ -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(getLengthOfFirstOrZero(source)), @@ -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(getLengthOfFirstOrZero(source)), _colsCache = source.toList(growable: false), _data = ByteData(source.length * @@ -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(rowsNum), _colsCache = List(colsNum), _data = ByteData(rowsNum * colsNum * bytesPerElement) { @@ -99,7 +99,7 @@ class DataManagerImpl implements DataManager { final Iterable rowIndices; @override - final Iterable colIndices; + final Iterable columnIndices; final List _rowsCache; final List _colsCache; diff --git a/pubspec.yaml b/pubspec.yaml index 133d4432..97242594 100644 --- a/pubspec.yaml +++ b/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 homepage: https://github.com/gyrdym/ml_linalg diff --git a/test/float32_matrix_test.dart b/test/float32_matrix_test.dart index be063070..f6f0a671 100644 --- a/test/float32_matrix_test.dart +++ b/test/float32_matrix_test.dart @@ -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); + }); + }); }