Skip to content
Open
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 0.1.0
- **Breaking change** svd returns a dart3 record (E, U, D) instead of a map {'E': E, 'U': U, 'D': D}
- Fix null safety issues.
# 0.0.29+1

- Fix percentile index error in `Percentile.value` getter ([zeshuaro](https://github.com/zeshuaro)).
Expand Down
8 changes: 4 additions & 4 deletions example/extended_math_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ void main() {
final v1 = Vector(<double>[1, 2, 3]);
final v2 = Vector(<double>[4, 5, 6]);
// Multiply vectors
final double res = v1.dot(v2);
final num res = v1.dot(v2);
// Add vectors
final Vector res1 = v1 + v2;

Expand All @@ -14,12 +14,12 @@ void main() {
print(v5.angleBetween(v6));

final v3 = SquareMatrix(<List<double>>[
<double>[9, 3, 5],
<double>[-6, -9, 7],
<double>[9, 3, 5],
<double>[-6, -9, 7],
<double>[-1, -8, 1]
]);
// Gets determinant of matrix
final double det = v3.determinant();
final num det = v3.determinant();

// Computes eigenvalues and eigenvectors of square matrix
final Map<num, Vector> result = v3.eigen();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class NewtonsMethod {

/// Counting Lowest limit for negative roots
num lowerLimit() {
final helpArray = List<num>(_equationCoef.length);
final helpArray = List<num>.filled(_equationCoef.length, 0);
for (var i = 0; i < _equationCoef.length; i++) {
if (_equationPower[i] % 2 == 0) {
helpArray[i] = _equationCoef[i];
Expand Down Expand Up @@ -113,8 +113,8 @@ class NewtonsMethod {

var point1 = lowLim;
var point2 = lowLim + delta;
var value1 = 0.0;
var value2 = 0.0;
num value1 = 0.0;
num value2 = 0.0;

while (point2 < upLim) {
value1 = _calcValue(point1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import 'dart:math';

import 'package:meta/meta.dart';

/// Generator of random numbers
///
/// It isn't extend `Random` of `dart:math`, but provide functionality
Expand All @@ -25,7 +23,7 @@ class NumbersGenerator {
///
/// Generates `integer` number in range [from] - [to] inclusively.
/// [from] should be less than [to].
Iterable<int> intIterableSync({@required int to, int from = 0}) sync* {
Iterable<int> intIterableSync({required int to, int from = 0}) sync* {
while (true) {
yield nextInt(to, from: from);
}
Expand Down
12 changes: 6 additions & 6 deletions lib/src/applied_mathematics/statistic/central_tendency.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ class CentralTendency {
///
/// If [weights] isn't `null` then the weighted arithmetic mean are computed.
/// If provided, [weights] must have the same shape as the number's set.
num arithmetic({TensorBase weights}) {
num arithmetic({TensorBase? weights}) {
if (_set.any((e) => e <= 0)) {
throw MeanException('All numbers in set must be greatest than zero!');
}

var set = _set.copy();
final w = weights ?? TensorBase.generate(set.shape, (_) => 1);

if (!mapsEqual(set.shape, w.shape)) {
if (!listsEqual(set.shape, w.shape)) {
throw ArgumentError.value(weights, 'weights',
'Items count of weights don\'t match set of numbers!');
}
Expand All @@ -43,15 +43,15 @@ class CentralTendency {
///
/// If [weights] isn't `null` then the weighted geometric mean are computed.
/// If provided, [weights] must have the same shape as the number's set.
num geometric({TensorBase weights}) {
num geometric({TensorBase? weights}) {
if (_set.any((e) => e <= 0)) {
throw MeanException('All numbers in set must be greatest than zero!');
}

final set = _set.copy();
final w = weights ?? TensorBase.generate(set.shape, (_) => 1);

if (!mapsEqual(set.shape, w.shape)) {
if (!listsEqual(set.shape, w.shape)) {
throw ArgumentError.value(weights, 'weights',
'Items count of weights don\'t match set of numbers!');
}
Expand All @@ -71,15 +71,15 @@ class CentralTendency {
///
/// If [weights] isn't `null` then the weighted harmonic mean are computed.
/// If provided, [weights] must have the same shape as the number's set.
num harmonic({TensorBase weights}) {
num harmonic({TensorBase? weights}) {
if (_set.any((e) => e <= 0)) {
throw MeanException('All numbers in set must be greatest than zero!');
}

final set = _set.copy();
final w = weights ?? TensorBase.generate(set.shape, (_) => 1);

if (!mapsEqual(set.shape, w.shape)) {
if (!listsEqual(set.shape, w.shape)) {
throw ArgumentError.value(weights, 'weights',
'Items count of weights don\'t match set of numbers!');
}
Expand Down
7 changes: 4 additions & 3 deletions lib/src/applied_mathematics/statistic/dispersion.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Dispersion {
///
/// By default means that all values have an equal propabilities if
/// it isn't so, you can provide your own.
Dispersion(this._values, {TensorBase probabilities})
Dispersion(this._values, {TensorBase? probabilities})
: _probabilities = probabilities ??
TensorBase.generate(_values.shape, (_) => 1 / _values.itemsCount);

Expand All @@ -28,7 +28,8 @@ class Dispersion {

/// Computes expected value for all possible [values] of a random number
/// for finite case with its probabilities
double expectedValue() => (values * probabilities).reduce((f, s) => f + s);
double expectedValue() =>
(values * probabilities).reduce((f, s) => f + s).toDouble();

/// Computes `population` or `sample` [type]s of standard deviation of
/// [values]
Expand All @@ -47,7 +48,7 @@ class Dispersion {
default:
final summ = (values.map((v) => pow(v, 2)) * probabilities)
.reduce((f, s) => f + s);
return summ - pow(mu, 2);
return (summ - pow(mu, 2)).toDouble();
}
}

Expand Down
10 changes: 5 additions & 5 deletions lib/src/complex_analysis/complex.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Complex with CopyableMixin<Complex> {
///
/// [other] can be either number or complex number.
Complex operator +(Object other) {
Complex c;
var c = Complex();
if (other is num) {
c = Complex(re: re + other, im: im);
} else if (other is Complex) {
Expand All @@ -50,7 +50,7 @@ class Complex with CopyableMixin<Complex> {
///
/// [other] can be either number or complex number.
Complex operator -(Object other) {
Complex c;
var c = Complex();
if (other is Complex) {
c = this + -other;
} else if (other is num) {
Expand All @@ -68,7 +68,7 @@ class Complex with CopyableMixin<Complex> {
///
/// [other] can be either number or complex number.
Complex operator *(Object other) {
Complex c;
var c = Complex();
if (other is num) {
final newRe = re * other;
final newIm = im * other;
Expand All @@ -89,7 +89,7 @@ class Complex with CopyableMixin<Complex> {
///
/// [other] can be either number or complex number.
Complex operator /(Object other) {
Complex c;
var c = Complex();
if (other is num) {
final down = m.pow(other, 2);
final newRe = (re * other) / down;
Expand Down Expand Up @@ -137,7 +137,7 @@ class Complex with CopyableMixin<Complex> {
}

// final rootModule = m.pow(module, 1 / root);
final rootModule = Double(module).rootOf(root).toDouble();
final rootModule = Double(module.toDouble()).rootOf(root).toDouble();

for (var i = 0; i < root; i++) {
final newRe = m.cos((argument + 2 * argument * i) / root) * rootModule;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ class Number extends TensorBase {
num get data => _value;

@override
Map<String, int> get shape =>
<String, int>{}; // Empty because numbers haven't shape
List<int> get shape => []; // Empty because numbers haven't shape

/// Gets nth root of this number
///
Expand Down Expand Up @@ -62,7 +61,7 @@ class Number extends TensorBase {
/// [other] can be either `num` or [Number].
@override
Number operator +(Object other) {
Number n;
var n = Number(0);
if (other is num) {
n = Number(_value + other);
} else if (other is Number) {
Expand All @@ -79,7 +78,7 @@ class Number extends TensorBase {
/// [other] can be either `num` or [Number].
@override
Number operator -(Object other) {
Number n;
var n = Number(0);
if (other is num) {
n = Number(_value - other);
} else if (other is Number) {
Expand All @@ -93,7 +92,7 @@ class Number extends TensorBase {
/// [other] can be either `num` or [Number].
@override
Number operator *(Object other) {
Number n;
var n = Number(0);
if (other is num) {
n = Number(_value * other);
} else if (other is Number) {
Expand All @@ -107,7 +106,7 @@ class Number extends TensorBase {
/// [other] can be either `num` or [Number].
@override
Double operator /(Object other) {
Double n;
var n = Double(0);
if (other is num) {
if (other == 0) {
throw DivisionByZeroException();
Expand All @@ -124,7 +123,7 @@ class Number extends TensorBase {

@override
bool operator ==(Object other) {
bool result;
var result = false;
if (other is num) {
result = _value == other;
} else if (other is Number) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,25 @@ abstract class TensorBase with CopyableMixin<TensorBase> {
/// - [2, 3] -> `Matrix` with 2 rows and 3 columns
/// - [4, 2, 5] -> `Tensor3` with 4 width, 2 length and 5 depth
factory TensorBase.generate(
Map<String, int> shape, num Function(num number) generator) {
List<int> shape, num Function(num number) generator) {
switch (shape.length) {
case 1:
return Vector(List<num>.generate(shape['width'], generator));
return Vector(List<num>.generate(shape[0], generator));
case 2:
final row = List<num>.generate(shape['width'], generator);
return Matrix(List<List<num>>.generate(shape['length'], (_) => row));
final row = List<num>.generate(shape[1], generator);
return Matrix(List<List<num>>.generate(shape[0], (_) => row));
case 3:
final depth = List<num>.generate(shape['depth'], generator);
final width = List<List<num>>.generate(shape['width'], (_) => depth);
final depth = List<num>.generate(shape[2], generator);
final width = List<List<num>>.generate(shape[1], (_) => depth);
return Tensor3(
List<List<List<num>>>.generate(shape['length'], (_) => width));
List<List<List<num>>>.generate(shape[0], (_) => width));
case 4:
final depth2 = List<num>.generate(shape['depth2'], generator);
final depth = List<List<num>>.generate(shape['depth'], (_) => depth2);
final depth2 = List<num>.generate(shape[3], generator);
final depth = List<List<num>>.generate(shape[2], (_) => depth2);
final width =
List<List<List<num>>>.generate(shape['width'], (_) => depth);
List<List<List<num>>>.generate(shape[1], (_) => depth);
return Tensor4(List<List<List<List<num>>>>.generate(
shape['length'], (_) => width));
shape[0], (_) => width));
default:
return Number(generator(1));
}
Expand All @@ -59,9 +59,10 @@ abstract class TensorBase with CopyableMixin<TensorBase> {

/// Gets shape of this tensor
///
/// [shape] may contain numbers that denote count of `width`(columns),
/// `length`(rows), `depth` and `depth2` in this order.
Map<String, int> get shape;
/// [shape] may contain numbers that denote count of entries in each
/// dimension of the tensor, with shape[0] being the count for the first
/// dimension shape[1] in the second and so on.
List<int> get shape;

/// Reduces data to number with provided [f] reduce function
num reduce(num Function(num prev, num next) f);
Expand All @@ -79,7 +80,7 @@ abstract class TensorBase with CopyableMixin<TensorBase> {
/// otherwise throws [TensorException]
Number toScalar() {
if (dimension == 0) {
return Number(data);
return Number(data as num);
} else {
throw TensorException('Tensor cannot be converted to Number, because '
'dimension of this tensor isn\'t equal to 0!');
Expand All @@ -90,7 +91,7 @@ abstract class TensorBase with CopyableMixin<TensorBase> {
/// otherwise throws [TensorException]
Vector toVector() {
if (dimension == 1) {
return Vector(data);
return Vector(data as List<num>);
} else {
throw TensorException('Tensor cannot be converted to Vector, because '
'dimension of this tensor isn\'t equal to 1!');
Expand All @@ -101,7 +102,7 @@ abstract class TensorBase with CopyableMixin<TensorBase> {
/// otherwise throws [TensorException]
Matrix toMatrix() {
if (dimension == 2) {
return Matrix(data);
return Matrix(data as List<List<num>>);
} else {
throw TensorException('Tensor cannot be converted to Matrix, because '
'dimension of this tensor isn\'t equal to 2!');
Expand All @@ -112,7 +113,7 @@ abstract class TensorBase with CopyableMixin<TensorBase> {
/// otherwise throws [TensorException]
Tensor3 toTensor3() {
if (dimension == 3) {
return Tensor3(data);
return Tensor3(data as List<List<List<num>>>);
} else {
throw TensorException('Tensor cannot be converted to Tensor3, because '
'dimension of this tensor isn\'t equal to 3!');
Expand All @@ -123,7 +124,7 @@ abstract class TensorBase with CopyableMixin<TensorBase> {
/// otherwise throws [TensorException]
Tensor4 toTensor4() {
if (dimension == 4) {
return Tensor4(data);
return Tensor4(data as List<List<List<List<num>>>>);
} else {
throw TensorException('Tensor cannot be converted to Tensor4, because '
'dimension of this tensor isn\'t equal to 4!');
Expand All @@ -137,7 +138,7 @@ abstract class TensorBase with CopyableMixin<TensorBase> {
/// of known data points [a] (alpha) may be in range from 0 to 1
/// inclusively. Otherwise throws [TensorException].
TensorBase lerp(TensorBase other, double a) {
if (dimension != other.dimension && !mapsEqual(shape, other.shape)) {
if (dimension != other.dimension && !listsEqual(shape, other.shape)) {
throw ArgumentError('Tensors aren\'t equals!');
}

Expand Down
Loading