Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 13.12.9
- `Vector.fromList`:
- factory-constructor speed-up

## 13.12.8
- `README.md`: remove obsolete contacts

Expand Down
26 changes: 24 additions & 2 deletions lib/src/vector/float32x4_vector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ class Float32x4Vector with IterableMixin<double> implements Vector {

_buffer = list.buffer;

for (var i = 0; i < length; i++) {
list[i] = source[i].toDouble();
if (source is Float32List) {
_copyFromTypedList(list, source, length);
} else if (source is List<double>) {
_copyFromDoubleList(list, source, length);
} else {
_copyFromNumList(list, source, length);
}
}

Expand Down Expand Up @@ -111,6 +115,24 @@ class Float32x4Vector with IterableMixin<double> implements Vector {
static int _getNumOfBuckets(int length, int bucketSize) =>
(length / bucketSize).ceil();

static void _copyFromTypedList(
Float32List list, Float32List source, int length) {
list.setRange(0, length, source);
}

static void _copyFromDoubleList(
Float32List list, List<double> source, int length) {
for (var i = 0; i < length; i++) {
list[i] = source[i];
}
}

static void _copyFromNumList(Float32List list, List<num> source, int length) {
for (var i = 0; i < length; i++) {
list[i] = source[i].toDouble();
}
}

@override
final int length;

Expand Down
26 changes: 24 additions & 2 deletions lib/src/vector/float64x2_vector.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ class Float64x2Vector with IterableMixin<double> implements Vector {

_buffer = list.buffer;

for (var i = 0; i < length; i++) {
list[i] = source[i].toDouble();
if (source is Float64List) {
_copyFromTypedList(list, source, length);
} else if (source is List<double>) {
_copyFromDoubleList(list, source, length);
} else {
_copyFromNumList(list, source, length);
}
}

Expand Down Expand Up @@ -114,6 +118,24 @@ class Float64x2Vector with IterableMixin<double> implements Vector {
static int _getNumOfBuckets(int length, int bucketSize) =>
(length / bucketSize).ceil();

static void _copyFromTypedList(
Float64List list, Float64List source, int length) {
list.setRange(0, length, source);
}

static void _copyFromDoubleList(
Float64List list, List<double> source, int length) {
for (var i = 0; i < length; i++) {
list[i] = source[i];
}
}

static void _copyFromNumList(Float64List list, List<num> source, int length) {
for (var i = 0; i < length; i++) {
list[i] = source[i].toDouble();
}
}

@override
final int length;

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: ml_linalg
description: SIMD-based linear algebra and statistics, efficient manipulation with numeric data
version: 13.12.8
version: 13.12.9
homepage: https://github.com/gyrdym/ml_linalg

environment:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:typed_data';

import 'package:ml_linalg/dtype.dart';
import 'package:ml_linalg/vector.dart';
import 'package:test/test.dart';
Expand Down Expand Up @@ -80,5 +82,49 @@ void vectorFromListConstructorTestGroupFactory(DType dtype) =>
expect(vector.length, 5);
expect(vector.dtype, dtype);
});

test(
'should create a vector from a typed list, length is greater than 4',
() {
final source = dtype == DType.float32
? Float32List.fromList([1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
: Float64List.fromList([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
final vector = Vector.fromList(source, dtype: dtype);

expect(vector, equals([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]));
expect(vector.length, 6);
expect(vector.dtype, dtype);
});

test('should create a vector from a typed list, length is less than 4',
() {
final source = dtype == DType.float32
? Float32List.fromList([1.0, 2.0, 3.0])
: Float64List.fromList([1.0, 2.0, 3.0]);
final vector = Vector.fromList(source, dtype: dtype);

expect(vector, equals([1.0, 2.0, 3.0]));
expect(vector.length, 3);
expect(vector.dtype, dtype);
});

test('should keep SIMD padding zeros when created from a typed list',
() {
final source = dtype == DType.float32
? Float32List.fromList([1.0, 2.0, 3.0])
: Float64List.fromList([1.0, 2.0, 3.0]);
final vector = Vector.fromList(source, dtype: dtype);

expect(vector.sum(), 6.0);
});

test('should create a vector from a mixed List<num>', () {
final source = <num>[1, 2.5, 3, 4.25, 5];
final vector = Vector.fromList(source, dtype: dtype);

expect(vector, equals([1.0, 2.5, 3.0, 4.25, 5.0]));
expect(vector.length, 5);
expect(vector.dtype, dtype);
});
});
});
Loading