NumDart
Arrays
Array, Array2d and ArrayComplex, element-wise math and evenly spaced samples.
Like numpy.ndarray, numpy.linspace, numpy.arange
SciDart has three containers:
Array: a list ofdouble, the equivalent of a 1-D NumPy array.Array2d: a list ofArrayrows, used as a matrix.ArrayComplex: a list ofComplex, used by the FFT.
All of them are real Dart lists, so map, where, length and for loops work as usual.
Element-wise operations
+, -, * and / between two arrays of the same length work element by element. Operations with a scalar have their own functions, such as arrayMultiplyToScalar and arrayAddToScalar. Math functions follow the same naming: arraySin, arrayCos, arrayLog, arraySqrt, arrayPow.
import 'package:scidart/numdart.dart';
void main() {
var a = Array([1, 2, 3, 4]);
var b = Array([10, 20, 30, 40]);
print((a + b).toList()); // element-wise sum
print((a * b).toList()); // element-wise product
print(arrayMultiplyToScalar(a, 0.5).toList());
print(arrayCumSum(a).toList());
print('sum: ${arraySum(a)}, max: ${arrayMax(b)}, argmax: ${arrayArgMax(b)}');
}
[11.0, 22.0, 33.0, 44.0] [10.0, 40.0, 90.0, 160.0] [0.5, 1.0, 1.5, 2.0] [1.0, 3.0, 6.0, 10.0] sum: 10.0, max: 40.0, argmax: 3
Reductions return a double or an index: arraySum, arrayMax, arrayMin, arrayArgMax, arrayNorm. arrayCumSum and arrayDiff return a new array.
Creating arrays
import 'package:scidart/numdart.dart';
void main() {
print(createArrayRange(start: 0, stop: 10, step: 2).toList());
print(linspace(0, 1, num: 5).toList());
print(linspace(0, 1, num: 4, endpoint: false).toList());
print(ones(3).toList());
print(zeros(3).toList());
print(Array2d.fixed(2, 3, initialValue: 7).map((r) => r.toList()).toList());
}
[0.0, 2.0, 4.0, 6.0, 8.0] [0.0, 0.25, 0.5, 0.75, 1.0] [0.0, 0.25, 0.5, 0.75] [1.0, 1.0, 1.0] [0.0, 0.0, 0.0] [[7.0, 7.0, 7.0], [7.0, 7.0, 7.0]]
linspacereturnsnumsamples betweenstartandstop, includingstopunlessendpointis false.createArrayRangeworks likenumpy.arange, with integer steps.ones,zeros,Array.fixedandArray2d.fixedcreate filled arrays.logSpacegives samples evenly spaced on a log scale.
Copying
Arrays are mutable lists. Use Array.fromArray (or copy()) when a function must not change the caller's data.