From 37af34563be95a688e02b695b4ac4647e93918f0 Mon Sep 17 00:00:00 2001 From: Ilya Gyrdymov Date: Mon, 27 May 2019 23:10:49 +0300 Subject: [PATCH] empty list as a source supported for matrix (columns and rows constructors), unit tests for empty list based source added --- CHANGELOG.md | 4 +++ .../helper/get_length_of_first_or_zero.dart | 2 +- .../data_manager/data_manager_impl.dart | 32 ++++++++++--------- pubspec.yaml | 2 +- test/float32_matrix_test.dart | 31 +++++++++++++++++- 5 files changed, 53 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ab7238b1..81ca303a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 10.0.3 +- `Float32Matrix.columns`: empty list supported as a source +- `Float32Matrix.rows`: empty list supported as a source + ## 10.0.2 - `Float32Matrix.fromList`: empty list supported as a source diff --git a/lib/src/common/helper/get_length_of_first_or_zero.dart b/lib/src/common/helper/get_length_of_first_or_zero.dart index 787c48dd..b820be79 100644 --- a/lib/src/common/helper/get_length_of_first_or_zero.dart +++ b/lib/src/common/helper/get_length_of_first_or_zero.dart @@ -1,2 +1,2 @@ -int getLengthOfFirstOrZero(List> collection) => +int getLengthOfFirstOrZero>(List collection) => collection.isNotEmpty ? collection.first.length : 0; 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 117f1a53..fcd7b818 100644 --- a/lib/src/matrix/common/data_manager/data_manager_impl.dart +++ b/lib/src/matrix/common/data_manager/data_manager_impl.dart @@ -16,14 +16,14 @@ class DataManagerImpl implements DataManager { this._typedListHelper, ) : rowsNum = source.length, - columnsNum = getLengthOfFirstOrZero(source), + columnsNum = getLengthOfFirstOrZero>(source), _rowsIndicesRange = ZRange.closedOpen(0, source.length), _colsIndicesRange = ZRange - .closedOpen(0, getLengthOfFirstOrZero(source)), + .closedOpen(0, getLengthOfFirstOrZero>(source)), _rowsCache = List(source.length), - _colsCache = List(getLengthOfFirstOrZero(source)), - _data = ByteData(source.length * getLengthOfFirstOrZero(source) * - bytesPerElement) { + _colsCache = List(getLengthOfFirstOrZero>(source)), + _data = ByteData(source.length * + getLengthOfFirstOrZero>(source) * bytesPerElement) { _updateByteDataBy2dimIterable(source, (i, j) => i * columnsNum + j, bytesPerElement); } @@ -35,13 +35,14 @@ class DataManagerImpl implements DataManager { this._typedListHelper, ) : rowsNum = source.length, - columnsNum = source.first.length, + columnsNum = getLengthOfFirstOrZero(source), _rowsIndicesRange = ZRange.closedOpen(0, source.length), - _colsIndicesRange = ZRange.closedOpen(0, source.first.length), + _colsIndicesRange = ZRange + .closedOpen(0, getLengthOfFirstOrZero(source)), _rowsCache = source.toList(growable: false), - _colsCache = List(source.first.length), - _data = ByteData(source.length * source.first.length * - bytesPerElement) { + _colsCache = List(getLengthOfFirstOrZero(source)), + _data = ByteData(source.length * + getLengthOfFirstOrZero(source) * bytesPerElement) { _updateByteDataBy2dimIterable(source, (i, j) => i * columnsNum + j, bytesPerElement); } @@ -52,14 +53,15 @@ class DataManagerImpl implements DataManager { this._dtype, this._typedListHelper, ) : - rowsNum = source.first.length, + rowsNum = getLengthOfFirstOrZero(source), columnsNum = source.length, - _rowsIndicesRange = ZRange.closedOpen(0, source.first.length), + _rowsIndicesRange = ZRange + .closedOpen(0, getLengthOfFirstOrZero(source)), _colsIndicesRange = ZRange.closedOpen(0, source.length), - _rowsCache = List(source.first.length), + _rowsCache = List(getLengthOfFirstOrZero(source)), _colsCache = source.toList(growable: false), - _data = ByteData(source.length * source.first.length * - bytesPerElement) { + _data = ByteData(source.length * + getLengthOfFirstOrZero(source) * bytesPerElement) { _updateByteDataBy2dimIterable(source, (i, j) => j * columnsNum + i, bytesPerElement); } diff --git a/pubspec.yaml b/pubspec.yaml index d832d994..951f9c6f 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: 10.0.2 +version: 10.0.3 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 ef10f7e4..055103b4 100644 --- a/test/float32_matrix_test.dart +++ b/test/float32_matrix_test.dart @@ -23,7 +23,8 @@ void main() { expect(actual.columnsNum, 5); }); - test('should create an instance based on an empty list', () { + test('should create an instance based on an empty list (`fromList` ' + 'constructor)', () { final actual = Float32Matrix.fromList([]); final expected = []; expect(actual, equals(expected)); @@ -46,6 +47,15 @@ void main() { expect(actual.columnsNum, 5); }); + test('should create an instance based on an empty list (`rows` ' + 'constructor)', () { + final actual = Float32Matrix.rows([]); + final expected = []; + expect(actual, equals(expected)); + expect(actual.rowsNum, 0); + expect(actual.columnsNum, 0); + }); + test('should create an instance with predefined vectors as matrix columns', () { final actual = Float32Matrix.columns([ @@ -64,6 +74,15 @@ void main() { expect(actual.columnsNum, 2); }); + test('should create an instance based on an empty list (`columns` ' + 'constructor)', () { + final actual = Float32Matrix.columns([]); + final expected = []; + expect(actual, equals(expected)); + expect(actual.rowsNum, 0); + expect(actual.columnsNum, 0); + }); + test('should create an instance from flattened collection', () { final actual = Float32Matrix.flattened([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], 2, 3); @@ -76,6 +95,16 @@ void main() { expect(actual, equals(expected)); }); + test('should create an instance based on an empty list (`flattened` ' + 'constructor)', () { + final actual = + Float32Matrix.flattened([], 0, 0); + final expected = []; + expect(actual.rowsNum, 0); + expect(actual.columnsNum, 0); + expect(actual, equals(expected)); + }); + test('should throw an error if one tries to create a matrix from flattened ' 'collection and with unproper specified dimensions', () { expect(() => Float32Matrix.flattened([1.0, 2.0, 3.0, 4.0, 5.0], 2, 3),