Skip to content

Commit

Permalink
Merge pull request #59 from gyrdym/matrix-from-columns-constructor-em…
Browse files Browse the repository at this point in the history
…pty-list

Empty list-based source support for `Float32Matrix.rows` and `Float32Matrix.columns` added
  • Loading branch information
gyrdym committed May 27, 2019
2 parents 09a757e + 37af345 commit 4570517
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 18 deletions.
4 changes: 4 additions & 0 deletions 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

Expand Down
2 changes: 1 addition & 1 deletion lib/src/common/helper/get_length_of_first_or_zero.dart
@@ -1,2 +1,2 @@
int getLengthOfFirstOrZero(List<List<double>> collection) =>
int getLengthOfFirstOrZero<T extends Iterable<double>>(List<T> collection) =>
collection.isNotEmpty ? collection.first.length : 0;
32 changes: 17 additions & 15 deletions lib/src/matrix/common/data_manager/data_manager_impl.dart
Expand Up @@ -16,14 +16,14 @@ class DataManagerImpl implements DataManager {
this._typedListHelper,
) :
rowsNum = source.length,
columnsNum = getLengthOfFirstOrZero(source),
columnsNum = getLengthOfFirstOrZero<List<double>>(source),
_rowsIndicesRange = ZRange.closedOpen(0, source.length),
_colsIndicesRange = ZRange
.closedOpen(0, getLengthOfFirstOrZero(source)),
.closedOpen(0, getLengthOfFirstOrZero<List<double>>(source)),
_rowsCache = List<Vector>(source.length),
_colsCache = List<Vector>(getLengthOfFirstOrZero(source)),
_data = ByteData(source.length * getLengthOfFirstOrZero(source) *
bytesPerElement) {
_colsCache = List<Vector>(getLengthOfFirstOrZero<List<double>>(source)),
_data = ByteData(source.length *
getLengthOfFirstOrZero<List<double>>(source) * bytesPerElement) {
_updateByteDataBy2dimIterable(source, (i, j) => i * columnsNum + j,
bytesPerElement);
}
Expand All @@ -35,13 +35,14 @@ class DataManagerImpl implements DataManager {
this._typedListHelper,
) :
rowsNum = source.length,
columnsNum = source.first.length,
columnsNum = getLengthOfFirstOrZero<Vector>(source),
_rowsIndicesRange = ZRange.closedOpen(0, source.length),
_colsIndicesRange = ZRange.closedOpen(0, source.first.length),
_colsIndicesRange = ZRange
.closedOpen(0, getLengthOfFirstOrZero<Vector>(source)),
_rowsCache = source.toList(growable: false),
_colsCache = List<Vector>(source.first.length),
_data = ByteData(source.length * source.first.length *
bytesPerElement) {
_colsCache = List<Vector>(getLengthOfFirstOrZero<Vector>(source)),
_data = ByteData(source.length *
getLengthOfFirstOrZero<Vector>(source) * bytesPerElement) {
_updateByteDataBy2dimIterable(source, (i, j) => i * columnsNum + j,
bytesPerElement);
}
Expand All @@ -52,14 +53,15 @@ class DataManagerImpl implements DataManager {
this._dtype,
this._typedListHelper,
) :
rowsNum = source.first.length,
rowsNum = getLengthOfFirstOrZero<Vector>(source),
columnsNum = source.length,
_rowsIndicesRange = ZRange.closedOpen(0, source.first.length),
_rowsIndicesRange = ZRange
.closedOpen(0, getLengthOfFirstOrZero<Vector>(source)),
_colsIndicesRange = ZRange.closedOpen(0, source.length),
_rowsCache = List<Vector>(source.first.length),
_rowsCache = List<Vector>(getLengthOfFirstOrZero<Vector>(source)),
_colsCache = source.toList(growable: false),
_data = ByteData(source.length * source.first.length *
bytesPerElement) {
_data = ByteData(source.length *
getLengthOfFirstOrZero<Vector>(source) * bytesPerElement) {
_updateByteDataBy2dimIterable(source, (i, j) => j * columnsNum + i,
bytesPerElement);
}
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: 10.0.2
version: 10.0.3
author: Ilia Gyrdymov <ilgyrd@gmail.com>
homepage: https://github.com/gyrdym/ml_linalg

Expand Down
31 changes: 30 additions & 1 deletion test/float32_matrix_test.dart
Expand Up @@ -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 = <double>[];
expect(actual, equals(expected));
Expand All @@ -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 = <double>[];
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([
Expand All @@ -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 = <double>[];
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);
Expand All @@ -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 = <double>[];
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),
Expand Down

0 comments on commit 4570517

Please sign in to comment.