-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
matrix.dart
609 lines (546 loc) · 16.2 KB
/
matrix.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
import 'dart:typed_data';
import 'package:ml_linalg/axis.dart';
import 'package:ml_linalg/dtype.dart';
import 'package:ml_linalg/matrix_norm.dart';
import 'package:ml_linalg/sort_direction.dart';
import 'package:ml_linalg/src/common/cache_manager/cache_manager_factory.dart';
import 'package:ml_linalg/src/di/dependencies.dart';
import 'package:ml_linalg/src/matrix/data_manager/float64_matrix_data_manager.dart';
import 'package:ml_linalg/src/matrix/matrix_cache_keys.dart';
import 'package:ml_linalg/src/matrix/matrix_impl.dart';
import 'package:ml_linalg/src/matrix/data_manager/float32_matrix_data_manager.dart';
import 'package:ml_linalg/vector.dart';
final _cacheManagerFactory = dependencies.getDependency<CacheManagerFactory>();
/// An algebraic matrix with extended functionality, adapted for data science
/// applications
abstract class Matrix implements Iterable<Iterable<double>> {
/// Creates a matrix from a two dimensional list, every nested list is a
/// source for a matrix row.
///
/// There is no check of nested lists length in the [source] due to
/// performance, keep it in mind, don't create a matrix from nested lists of
/// different length
///
/// A simple usage example:
///
/// ````dart
/// import 'package:ml_linalg/matrix.dart';
///
/// void main() {
/// final matrix = Matrix.fromList([
/// [1, 2, 3, 4, 5],
/// [6, 7, 8, 9, 0],
/// ]);
///
/// print(matrix);
/// }
/// ````
///
/// The output:
///
/// ```
/// Matrix 2 x 5:
/// (1.0, 2.0, 3.0, 4.0, 5.0)
/// (6.0, 7.0, 8.0, 9.0, 0.0)
/// ```
factory Matrix.fromList(List<List<double>> source,
{DType dtype = DType.float32}) {
switch (dtype) {
case DType.float32:
return MatrixImpl(
Float32MatrixDataManager.fromList(source),
_cacheManagerFactory.create(matrixCacheKeys),
);
case DType.float64:
return MatrixImpl(
Float64MatrixDataManager.fromList(source),
_cacheManagerFactory.create(matrixCacheKeys),
);
default:
throw UnimplementedError('Matrix of type $dtype is not implemented yet');
}
}
/// Creates a matrix with predefined row vectors
///
/// There is no check of nested vectors length in the [source] due to
/// performance, keep it in mind, don't create a matrix from vectors lists of
/// different length
///
/// A simple usage example:
///
/// ````dart
/// import 'package:ml_linalg/matrix.dart';
/// import 'package:ml_linalg/vector.dart';
///
/// void main() {
/// final matrix = Matrix.fromRows([
/// Vector.fromList([1, 2, 3, 4, 5]),
/// Vector.fromList([6, 7, 8, 9, 0]),
/// ]);
///
/// print(matrix);
/// }
/// ````
///
/// The output:
///
/// ```
/// Matrix 2 x 5:
/// (1.0, 2.0, 3.0, 4.0, 5.0)
/// (6.0, 7.0, 8.0, 9.0, 0.0)
/// ```
factory Matrix.fromRows(List<Vector> source, {DType dtype = DType.float32}) {
switch (dtype) {
case DType.float32:
return MatrixImpl(
Float32MatrixDataManager.fromRows(source),
_cacheManagerFactory.create(matrixCacheKeys),
);
case DType.float64:
return MatrixImpl(
Float64MatrixDataManager.fromRows(source),
_cacheManagerFactory.create(matrixCacheKeys),
);
default:
throw UnimplementedError('Matrix of type $dtype is not implemented yet');
}
}
/// Creates a matrix with predefined column vectors
///
/// There is no check of nested vectors length in the [source] due to
/// performance, keep it in mind, don't create a matrix from nested vectors of
/// different length
///
/// A simple usage example:
///
/// ````dart
/// import 'package:ml_linalg/matrix.dart';
/// import 'package:ml_linalg/vector.dart';
///
/// void main() {
/// final matrix = Matrix.fromColumns([
/// Vector.fromList([1, 2, 3, 4, 5]),
/// Vector.fromList([6, 7, 8, 9, 0]),
/// ]);
///
/// print(matrix);
/// }
/// ````
///
/// The output:
///
/// ```
/// Matrix 5 x 2:
/// (1.0, 6.0)
/// (2.0, 7.0)
/// (3.0, 8.0)
/// (4.0, 9.0)
/// (5.0, 0.0)
/// ```
factory Matrix.fromColumns(List<Vector> source,
{DType dtype = DType.float32}) {
switch (dtype) {
case DType.float32:
return MatrixImpl(
Float32MatrixDataManager.fromColumns(source),
_cacheManagerFactory.create(matrixCacheKeys),
);
case DType.float64:
return MatrixImpl(
Float64MatrixDataManager.fromColumns(source),
_cacheManagerFactory.create(matrixCacheKeys),
);
default:
throw UnimplementedError('Matrix of type $dtype is not implemented yet');
}
}
/// Creates a matrix of shape 0 x 0 (no rows, no columns)
///
/// A simple usage example:
///
/// ````dart
/// import 'package:ml_linalg/matrix.dart';
///
/// void main() {
/// final matrix = Matrix.empty();
///
/// print(matrix);
/// }
/// ````
///
/// The output:
///
/// ```
/// Matrix 0 x 0
/// ```
factory Matrix.empty({DType dtype = DType.float32}) {
switch (dtype) {
case DType.float32:
return MatrixImpl(
Float32MatrixDataManager.fromList([]),
_cacheManagerFactory.create(matrixCacheKeys),
);
case DType.float64:
return MatrixImpl(
Float64MatrixDataManager.fromList([]),
_cacheManagerFactory.create(matrixCacheKeys),
);
default:
throw UnimplementedError('Matrix of type $dtype is not implemented yet');
}
}
/// Creates a matrix from flattened list of length equal to
/// [rowsNum] * [columnsNum]
///
/// A simple usage example:
///
/// ````dart
/// import 'package:ml_linalg/matrix.dart';
///
/// void main() {
/// final source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
///
/// final matrix = Matrix.fromFlattenedList(source, 2, 5);
///
/// print(matrix);
/// }
/// ````
///
/// The output:
///
/// ```
/// Matrix 2 x 5:
/// (1.0, 2.0, 3.0, 4.0, 5.0)
/// (6.0, 7.0, 8.0, 9.0, 0.0)
/// ```
factory Matrix.fromFlattenedList(List<double> source, int rowsNum,
int columnsNum, {DType dtype = DType.float32}) {
switch (dtype) {
case DType.float32:
return MatrixImpl(
Float32MatrixDataManager.fromFlattened(source, rowsNum, columnsNum),
_cacheManagerFactory.create(matrixCacheKeys),
);
case DType.float64:
return MatrixImpl(
Float64MatrixDataManager.fromFlattened(source, rowsNum, columnsNum),
_cacheManagerFactory.create(matrixCacheKeys),
);
default:
throw UnimplementedError('Matrix of type $dtype is not implemented yet');
}
}
/// Creates a matrix, where elements from [source] are the elements for the
/// matrix main diagonal, the rest of the elements are zero
///
/// ````dart
/// import 'package:ml_linalg/matrix.dart';
///
/// void main() {
/// final matrix = Matrix.diagonal([1, 2, 3, 4, 5]);
///
/// print(matrix);
/// }
/// ````
///
/// The output:
///
/// ```
/// Matrix 5 x 5:
/// (1.0, 0.0, 0.0, 0.0, 0.0)
/// (0.0, 2.0, 0.0, 0.0, 0.0)
/// (0.0, 0.0, 3.0, 0.0, 0.0)
/// (0.0, 0.0, 0.0, 4.0, 0.0)
/// (0.0, 0.0, 0.0, 0.0, 5.0)
/// ```
factory Matrix.diagonal(List<double> source, {DType dtype = DType.float32}) {
switch (dtype) {
case DType.float32:
return MatrixImpl(
Float32MatrixDataManager.diagonal(source),
_cacheManagerFactory.create(matrixCacheKeys),
);
case DType.float64:
return MatrixImpl(
Float64MatrixDataManager.diagonal(source),
_cacheManagerFactory.create(matrixCacheKeys),
);
default:
throw UnimplementedError('Matrix of type $dtype is not implemented yet');
}
}
/// Creates a matrix of [size] * [size] dimension, where all the main
/// diagonal elements are equal to [scalar], the rest of the elements are 0
///
/// ````dart
/// import 'package:ml_linalg/matrix.dart';
///
/// void main() {
/// final matrix = Matrix.scalar(3, 5);
///
/// print(matrix);
/// }
/// ````
///
/// The output:
///
/// ```
/// Matrix 5 x 5:
/// (3.0, 0.0, 0.0, 0.0, 0.0)
/// (0.0, 3.0, 0.0, 0.0, 0.0)
/// (0.0, 0.0, 3.0, 0.0, 0.0)
/// (0.0, 0.0, 0.0, 3.0, 0.0)
/// (0.0, 0.0, 0.0, 0.0, 3.0)
/// ```
factory Matrix.scalar(double scalar, int size, {DType dtype = DType.float32}) {
switch (dtype) {
case DType.float32:
return MatrixImpl(
Float32MatrixDataManager.scalar(scalar, size),
_cacheManagerFactory.create(matrixCacheKeys),
);
case DType.float64:
return MatrixImpl(
Float64MatrixDataManager.scalar(scalar, size),
_cacheManagerFactory.create(matrixCacheKeys),
);
default:
throw UnimplementedError('Matrix of type $dtype is not implemented yet');
}
}
/// Creates a matrix of [size] * [size] dimension, where all the main
/// diagonal elements are equal to 1, the rest of the elements are 0
///
/// ````dart
/// import 'package:ml_linalg/matrix.dart';
///
/// void main() {
/// final matrix = Matrix.identity(5);
///
/// print(matrix);
/// }
/// ````
///
/// The output:
///
/// ```
/// Matrix 5 x 5:
/// (1.0, 0.0, 0.0, 0.0, 0.0)
/// (0.0, 1.0, 0.0, 0.0, 0.0)
/// (0.0, 0.0, 1.0, 0.0, 0.0)
/// (0.0, 0.0, 0.0, 1.0, 0.0)
/// (0.0, 0.0, 0.0, 0.0, 1.0)
/// ```
factory Matrix.identity(int size, {DType dtype = DType.float32}) {
switch (dtype) {
case DType.float32:
return MatrixImpl(
Float32MatrixDataManager.scalar(1.0, size),
_cacheManagerFactory.create(matrixCacheKeys),
);
case DType.float64:
return MatrixImpl(
Float64MatrixDataManager.scalar(1.0, size),
_cacheManagerFactory.create(matrixCacheKeys),
);
default:
throw UnimplementedError('Matrix of type $dtype is not implemented yet');
}
}
/// Creates a matrix, consisting of just one row (aka `Row matrix`)
///
/// ````dart
/// import 'package:ml_linalg/matrix.dart';
///
/// void main() {
/// final matrix = Matrix.row([1, 2, 3, 4, 5]);
///
/// print(matrix);
/// }
/// ````
///
/// The output:
///
/// ```
/// Matrix 1 x 5:
/// (1.0, 2.0, 3.0, 4.0, 5.0)
/// ```
factory Matrix.row(List<double> source, {DType dtype = DType.float32}) {
switch (dtype) {
case DType.float32:
return MatrixImpl(
Float32MatrixDataManager
.fromRows([Vector.fromList(source, dtype: dtype)]),
_cacheManagerFactory.create(matrixCacheKeys),
);
case DType.float64:
return MatrixImpl(
Float64MatrixDataManager
.fromRows([Vector.fromList(source, dtype: dtype)]),
_cacheManagerFactory.create(matrixCacheKeys),
);
default:
throw UnimplementedError(
'Matrix of type $dtype is not implemented yet');
}
}
/// Creates a matrix, consisting of just one column (aka `Column matrix`)
///
/// ````dart
/// import 'package:ml_linalg/matrix.dart';
///
/// void main() {
/// final matrix = Matrix.column([1, 2, 3, 4, 5]);
///
/// print(matrix);
/// }
/// ````
///
/// The output:
///
/// ```
/// Matrix 5 x 1:
/// (1.0)
/// (2.0)
/// (3.0)
/// (4.0)
/// (5.0)
/// ```
factory Matrix.column(List<double> source, {DType dtype = DType.float32}) {
switch (dtype) {
case DType.float32:
return MatrixImpl(
Float32MatrixDataManager
.fromColumns(([Vector.fromList(source, dtype: dtype)])),
_cacheManagerFactory.create(matrixCacheKeys),
);
case DType.float64:
return MatrixImpl(
Float64MatrixDataManager
.fromColumns(([Vector.fromList(source, dtype: dtype)])),
_cacheManagerFactory.create(matrixCacheKeys),
);
default:
throw UnimplementedError(
'Matrix of type $dtype is not implemented yet');
}
}
/// A data type of [Matrix] elements
DType get dtype;
/// Returns a lazy iterable of row vectors of the matrix
Iterable<Vector> get rows;
/// Returns a lazy iterable of column vectors of the matrix
Iterable<Vector> get columns;
/// Returns a lazy iterable of row indices
Iterable<int> get rowIndices;
/// Return a lazy iterable of column indices
Iterable<int> get columnIndices;
/// Returns a number of matrix row
int get rowsNum;
/// Returns a number of matrix columns
int get columnsNum;
/// Returns `true` if the [Matrix] is not empty. Use it instead of `isEmpty`
/// getter from [Iterable] interface, since the latter may return falsy true
bool get hasData;
/// Returns a matrix row on an [index] (the operator is an alias for
/// [getRow] method)
Vector operator [](int index);
/// Performs sum of the matrix and a matrix/ a vector/ a scalar/ whatever
Matrix operator +(Object value);
/// Performs subtraction of the matrix and a matrix/ a vector/ a scalar/
/// whatever
Matrix operator -(Object value);
/// Performs multiplication of the matrix and a matrix/ a vector/ a scalar/
/// whatever
Matrix operator *(Object value);
/// Performs division of the matrix by a matrix/ a vector/ a scalar/
/// whatever
Matrix operator /(Object value);
/// Performs transposition of the matrix
Matrix transpose();
/// Samples a new [Matrix] from parts of this [Matrix]
Matrix sample({
Iterable<int> rowIndices,
Iterable<int> columnIndices,
});
/// Returns a column of the matrix on [index]
Vector getColumn(int index);
/// Returns a row of the matrix on [index]
Vector getRow(int index);
/// Reduces all the matrix columns to only column, using [combiner] function
Vector reduceColumns(Vector combiner(Vector combine, Vector vector),
{Vector initValue});
/// Reduces all the matrix rows to only row, using [combiner] function
Vector reduceRows(Vector combiner(Vector combine, Vector vector),
{Vector initValue});
/// Performs column-wise mapping of this [Matrix] to a new one via passed
/// [mapper] function
Matrix mapColumns(Vector mapper(Vector column));
/// Performs row-wise mapping of this [Matrix] to a new one via passed
/// [mapper] function
Matrix mapRows(Vector mapper(Vector row));
/// Creates a new matrix, efficiently iterating through all the matrix
/// elements (several floating point elements in a time) and applying the
/// [mapper] function
///
/// Type [E] should be either [Float32x4] or [Float64x2], depends on [dtype]
/// value
Matrix fastMap<E>(E mapper(E columnElement));
/// Tries to convert the [Matrix] to a vector:
///
/// ````dart
/// import 'package:ml_linalg/matrix.dart';
///
/// void main() {
/// final matrix = Matrix.column([1, 2, 3, 4, 5]);
/// final vector = matrix.toVector();
///
/// print(vector);
/// }
/// ````
///
/// The output:
///
/// ```
/// (1.0, 2.0, 3.0, 4.0, 5.0)
/// ```
///
/// It fails, if both [columnsNum] and [rowsNum] are greater than `1`:
///
/// ````dart
/// import 'package:ml_linalg/matrix.dart';
///
/// void main() {
/// final matrix = Matrix.fromList([
/// [1.0, 2.0, 3.0, 4.0],
/// [5.0, 6.0, 7.0, 8.0],
/// ]);
///
/// final vector = matrix.toVector();
/// }
/// ````
///
/// The output:
///
/// ```
/// Exception: Cannot convert 2 x 4 matrix into a vector
/// ```
Vector toVector();
/// Returns maximal value of the matrix
double max();
/// Return minimal value of the matrix
double min();
/// Returns a norm of a matrix
double norm([MatrixNorm norm]);
/// Returns a new matrix with inserted [columns]
Matrix insertColumns(int index, List<Vector> columns);
/// Extracts non-repeated matrix rows and pack them into matrix
Matrix uniqueRows();
/// Returns mean values of matrix column/rows
Vector mean([Axis axis = Axis.columns]);
/// Returns standard deviation values of matrix column/rows
Vector deviation([Axis axis = Axis.columns]);
/// Returns a new matrix with sorted elements from this [Matrix]
Matrix sort(double selectSortValue(Vector vector), [Axis axis = Axis.rows,
SortDirection sortDir = SortDirection.asc]);
}