matrices 1.1.4 matrices: ^1.1.4 copied to clipboard
A matrix computing and linear algebra library for Dart.
Matrices - Matrix Computing and Linear Algebra Library for Dart
中文文档 中文文档(Gitee)
Grayscale releases are marked with '!'
Unimplemented functions are marked with '*'
Usage: https://pub.dev/packages/matrices
CONTENTS
- Matrices introduction
- Matrix class
- SquareMatrix class
- Donate ❤
Matrices introduction #
Matrices is a matrix library written purely in Dart. It is developed to provide support for projects like Astable which requires mathematical computation. Importing 'matrix.dart' is required before using it.
Matrix class #
The matrix class provides all the methods shared by any types of matrix like vector, square matrix or non-square matrix. For example, methods are provided to create a matrix, to query or update element at certain position, to append a row or column to the existing matrix, etc. However, it does not provides methods special for a square matrix.
Illustrations of Matrix class #
The Matrix class only has one public field - 'Matrix' whose runtimetype is List<List< double >>. Thus, the operation of updating the matrix is the same as the operation on a 'list' instance. We do not provide the concept of vectors, please use the List< double > type instead. However, you are NOT ALLOWED to initialize a Matrix instance through Matrix() conctructor. Instead, a few named constructors are provided for initialization of a Matrix instance.
Create a Matrix instance #
Create a matrix
/// Matrix.fromList(List<List<double>> lists)
var mat = Matrix.fromList([
[2, 3, 3, 3],
[9, 9, 8, 6],
[1, 1, 2, 9]
]);
print( mat );
// Matrix: 3x4
// [2.0, 3.0, 3.0, 3.0]
// [9.0, 9.0, 8.0, 6.0]
// [1.0, 1.0, 2.0, 9.0]
Matrix of zeros
/// Matrix.zero(int row, int column)
var mat = Matrix.zero(2, 3);
print( mat );
// Matrix: 2x3
// [0.0, 0.0, 0.0]
// [0.0, 0.0, 0.0]
Matrix of ones
/// Matrix.one(int row, int column)
var mat = Matrix.one(2, 3);
print( mat );
// Matrix: 2x3
// [1.0, 1.0, 1.0]
// [1.0, 1.0, 1.0]
Matrix of a certain number
/// Matrix.number(double number, int row, int column)
var mat = Matrix.number(9, 2, 3);
print( mat );
// Matrix: 2x3
// [9.0, 9.0, 9.0]
// [9.0, 9.0, 9.0]
Matrix from a list
// Matrix.fromFlattenedList(List<double> list, int row, int column)
// If the number of list elements is insufficient, the matrix is completed with zeros.
var mat1 = Matrix.fromFlattenedList([1, 2, 3, 4], 2, 2);
var mat2 = Matrix.fromFlattenedList([1, 2, 3, 4], 2, 3);
print(mat1);
print(mat2);
// Matrix: 2x2
// [1.0, 2.0]
// [3.0, 4.0]
// Matrix: 2x3
// [1.0, 2.0, 3.0]
// [4.0, 0.0, 0.0]
Matrix of random numbers
/// Matrix.random(int row, int column)
var mat = Matrix.random(2, 3);
print( mat );
// Matrix: 2x3
// [0.38224693703597046, 0.5412146597032305, 0.6424342222644003]
// [0.8491145735932242, 0.6236773300386973, 0.25269555696856316]
Select #
Taking the matrix 'mat' as example.
Matrix mat 3x4
[2, 3, 3, 3]
[9, 9, 8, 6]
[1, 1, 2, 9]
Access item
print( mat.matrix[1][2] );
// 8.0
Access row
print( mat.row(0) );
// [2.0, 3.0, 3.0, 3.0]
Access column
/// List<double> column(int column)
print( mat.column(0) );
// [2.0, 9.0, 1.0]
Transpose
/// Matrix transpose()
print( mat.transpose() );
// Matrix: 4x3
// [2.0, 9.0, 1.0]
// [3.0, 9.0, 1.0]
// [3.0, 8.0, 2.0]
// [3.0, 6.0, 9.0]
Row-echelon form
/// Matrix rowEchelonForm()
print( mat.rowEchelonForm() );
// Matrix: 3x4
// [1.0, 0.0, 0.0, 1.5]
// [0.0, 1.0, 0.0, -7.5]
// [0.0, 0.0, 1.0, 7.5]
Rank
/// int rank()
print( mat.rank() );
// 3
Update #
Taking the matrix 'mat' as example.
Matrix mat 3x4
[2, 3, 3, 3]
[9, 9, 8, 6]
[1, 1, 2, 9]
Update item
mat.matrix[0][0] = 3;
print( mat.matrix[0] );
// [3.0, 3.0, 3.0, 3.0]
Update row
/// setRow(List<double> list, int row)
mat.setRow([1, 4, 5, 0], 0);
print( mat.matrix[0] );
// [1.0, 4.0, 5.0, 0.0]
Update column
/// setColumn(List<double> list, int column)
mat.setColumn([1, 4, 5], 0);
print( mat );
// Matrix: 3x4
// [1.0, 3.0, 3.0, 3.0]
// [4.0, 9.0, 8.0, 6.0]
// [5.0, 1.0, 2.0, 9.0]
Insert #
Taking the matrix 'mat' as example.
Matrix mat 3x4
[2, 3, 3, 3]
[9, 9, 8, 6]
[1, 1, 2, 9]
Insert row
/// addRow(List<double> list, int index)
mat.addRow([8,8,8,8], 0);
print( mat );
// Matrix: 4x4
// [8.0, 8.0, 8.0, 8.0]
// [2.0, 3.0, 3.0, 3.0]
// [9.0, 9.0, 8.0, 6.0]
// [1.0, 1.0, 2.0, 9.0]
Insert column
/// addColumn(List<double> list, int index)
mat.addColumn([8, 8, 8], 4);
print( mat );
// Matrix: 3x5
// [2.0, 3.0, 3.0, 3.0, 8.0]
// [9.0, 9.0, 8.0, 6.0, 8.0]
// [1.0, 1.0, 2.0, 9.0, 8.0]
Append rows
/// appendRows(Matrix injected)
var tail = Matrix.fromList([
[8, 8, 8, 8],
[8, 8, 8, 8]
]);
mat.appendRows(tail);
print( mat );
// Matrix: 5x4
// [2.0, 3.0, 3.0, 3.0]
// [9.0, 9.0, 8.0, 6.0]
// [1.0, 1.0, 2.0, 9.0]
// [8.0, 8.0, 8.0, 8.0]
// [8.0, 8.0, 8.0, 8.0]
Append columns
/// appendColumns(Matrix injected)
var tail = Matrix.fromList([
[8, 8],
[8, 8],
[8, 8]
]);
mat.appendColumns(tail);
print( mat );
// Matrix: 3x6
// [2.0, 3.0, 3.0, 3.0, 8.0, 8.0]
// [9.0, 9.0, 8.0, 6.0, 8.0, 8.0]
// [1.0, 1.0, 2.0, 9.0, 8.0, 8.0]
Delete #
Taking the matrix 'mat' as example.
Matrix mat 3x4
[2, 3, 3, 3]
[9, 9, 8, 6]
[1, 1, 2, 9]
Delete row
/// deleteRow(int index)
mat.deleteRow(0);
print( mat );
// Matrix: 2x4
// [9.0, 9.0, 8.0, 6.0]
// [1.0, 1.0, 2.0, 9.0]
Delete column
/// deleteColumn(int index)
mat.deleteColumn(0);
print( mat );
// Matrix: 3x3
// [3.0, 3.0, 3.0]
// [9.0, 8.0, 6.0]
// [1.0, 2.0, 9.0]
Delete rows
PS: While deleting rows, the indexs MUST be put in order FROM SMALLEST TO LARGEST.
/// deleteRows(List<int> list)
mat.deleteRows([0, 1]);
print( mat );
// Matrix: 1x4
// [1.0, 1.0, 2.0, 9.0]
Delete columns
PS: While deleting columns, the indexs MUST be put in order FROM SMALLEST TO LARGEST.
/// deleteColumns(List<int> list)
mat.deleteColumns([0, 2]);
print( mat );
// Matrix: 3x2
// [3.0, 3.0]
// [9.0, 6.0]
// [1.0, 9.0]
Arithmetic operations #
Addition
var mat = Matrix.fromList([
[2, 3, 3],
[1, 2, 9]
]);
print( mat + 3 );
print( mat + mat );
Subtraction
var mat = Matrix.fromList([
[2, 3, 3],
[1, 2, 9]
]);
print( mat - 3 );
Multiplication
// Scalar matrix multiplication (the scalar number MUST be in the right position)
var mat = Matrix.fromList([
[2, 3, 3],
[1, 2, 9]
]);
print( mat * 3 );
// Matrix: 2x3
// [6.0, 9.0, 9.0]
// [3.0, 6.0, 27.0]
// Matrix multiplication
var multi = Matrix.fromList([
[2, 3.5],
[1, -2],
[-4, 0.5]
]);
print( mat * multi );
// Matrix: 2x2
// [-5.0, 2.5]
// [-32.0, 4.0]
Caution! #
Follow-up plan #
SquareMatrix class #
The SquareMatrix class has methods special for square matrix, like creating a squarematrix instance, calculating the determinant, the inverse, the eigen values and the eigen vectors, etc.
Illustrations of SquareMatrix class #
Createa a SquareMatrix instance #
Create a square matrix
/// SquareMatrix.fromList(List<List<double>> lists)
var mat = SquareMatrix.fromList([
[2, 3, 3],
[1, 2, 9],
[9, 8, 6]
]);
print( mat );
// Matrix: 3x3
// [6.0, 9.0, 9.0]
// [3.0, 6.0, 27.0]
// [27.0, 24.0, 18.0]
Create an identity matrix
// SquareMatrix.identity(int row)
var mat = SquareMatrix.identity(3);
print(mat);
// SquareMatrix: 3x3
// [1.0, 0.0, 0.0]
// [0.0, 1.0, 0.0]
// [0.0, 0.0, 1.0]
SquareMatrix of zeros
/// SquareMatrix.zero(int row)
var mat = SquareMatrix.zero(3);
print( mat );
// SquareMatrix: 3x3
// [0.0, 0.0, 0.0]
// [0.0, 0.0, 0.0]
// [0.0, 0.0, 0.0]
SquareMatrix of ones
/// SquareMatrix.one(int row)
var mat = SquareMatrix.one(3);
print( mat );
// SquareMatrix: 3x3
// [1.0, 1.0, 1.0]
// [1.0, 1.0, 1.0]
// [1.0, 1.0, 1.0]
SquareMatrix of a certain number
/// SquareMatrix.number(double number, int row)
var mat = SquareMatrix.number(2, 3);
print( mat );
// SquareMatrix: 3x3
// [2.0, 2.0, 2.0]
// [2.0, 2.0, 2.0]
// [2.0, 2.0, 2.0]
SquareMatrix of random numbers
/// SquareMatrix.random(int row)
var mat = SquareMatrix.random(3);
print( mat );
// SquareMatrix: 3x3
// [0.2666268534804871, 0.607635195280705, 0.581392153915932]
// [0.3590813328566256, 0.8098420655296595, 0.015949886001227154]
// [0.6670401483574298, 0.3054732396044414, 0.3113168618967712]
Create diagonal square matrix from list
/// SquareMatrix.diagonal(List<double> list)
var mat = SquareMatrix.diagonal([1,2,3]);
print( mat );
// SquareMatrix: 3x3
// [1.0, 0.0, 0.0]
// [0.0, 2.0, 0.0]
// [0.0, 0.0, 3.0]
Create diagonal square matrix from number
/// SquareMatrix.diagonalFromNumber(double num, int row)
var mat = SquareMatrix.diagonalFromNumber(2,3);
print( mat );
// SquareMatrix: 3x3
// [2.0, 0.0, 0.0]
// [0.0, 2.0, 0.0]
// [0.0, 0.0, 2.0]
Special properties of square matrix #
Determinant
/// double determinant()
var dett = SquareMatrix.fromList([
[1, 2],
[1, 1]
]);
print(dett.determinant());
// -1.0
Inverse
/// SquareMatrix inverse()
var dett = SquareMatrix.fromList([
[1, 2],
[1, 1]
]);
print(dett.inverse());
// SquareMatrix: 2x2
// [1.0, -1.0]
// [1.0, -1.0]
Eigenvalues *
/// double eigenvalue()
Eigenvectors *
/// Matrix eigenvector()
Caution! #
Follow-up plan #
Donate ❤ #
You can click PayPal or scan the QRcode to sponsor us :