matrix_utils 0.0.1
matrix_utils: ^0.0.1 copied to clipboard
A Dart library that provides an easy-to-use Matrix class for performing various matrix operations.
Matrix Library for Dart #
A Dart library that provides an easy-to-use Matrix class for performing various matrix operations.
Features #
- Matrix creation (zero, ones, eye, diagonal, from list, etc.)
- Matrix operation (addition, subtraction, multiplication, etc.)
- Matrix manipulation (removeRow, removeRows,removeCol,removeCols, reshape, etc. )
- Statistics on matrix (min, max, sum, rank)
- LU decomposition and Guassian elimination method
- Solving linear systems of equations
- Submatrix extraction
- Swapping rows and columns
Usage #
Import the library #
import 'matrix.dart';
Create a Matrix #
You can create a Matrix object in different ways:
// Create a 2x2 Matrix from string
Matrix a Matrix("1 2; 3 4");
// Create a matrix from a list of lists
Matrix b = Matrix([
[1, 2],
[3, 4]
]);
// Create a matrix from a list of diagonal objects
Matrix d = Matrix.fromDiagonal([1, 2, 3]);
print(d);
// Output:
// 1 0 0
// 0 2 0
// 0 0 3
// Create a from a list of lists
Matrix c = [[1, '2', true],[3, '4', false]].toMatrix()
// Create a 2x2 matrix with all zeros
Matrix zeros = Matrix.zeros(2, 2);
// Create a 2x2 matrix with all ones
Matrix ones = Matrix.ones(2, 2);
// Create a 2x2 identity matrix
Matrix identity = Matrix.eye(2);
// Create a matrix that is filled with same object
Matrix e = Matrix.fill(2, 3, 7);
print(e);
// Output:
// 7 7 7
// 7 7 7
// Create a matrix from linspace and create a diagonal matrix
Matrix f = Matrix.linespace(0, 10, 3);
print(Matrix.fromDiagonal(f.toList()));
// Create from a range or arrange at a step
var m = Matrix.range(6, 1, 2); // Matrix.arrange(6, 1, 2)
print(m);
// Output:
// 1 3 5
Matrix Operations #
Perform matrix arithmetic operations:
Matrix a = Matrix([
[1, 2],
[3, 4]
]);
Matrix b = Matrix([
[5, 6],
[7, 8]
]);
// Addition
Matrix sum = a + b;
// Subtraction
Matrix difference = a - b;
// Matrix Scaler multiplication
Matrix product = a * 2;
// Matrix division
Matrix division = b / 2;
// Matrix exponent
Matrix expo = b ^ 2;
// Negate Matrix
Matrix expo = -a;
// Element-wise operation with function
var result = a.elementWise(b, (x, y) => x * y);
print(result);
// Output:
// 2 6
// 12 20
var matrix = Matrix([[-1, 2], [3, -4]]);
var abs = matrix.abs();
print(abs);
// Output:
// 1 2
// 3 4
// Matrix Reciprocal
var matrix = Matrix([[1, 2], [3, 4]]);
var reciprocal = matrix.reciprocal();
print(reciprocal);
// Output:
// 1.0 0.5
// 0.3333333333333333 0.25
// Matrix dot product
var matrixB = Matrix([[2, 0], [1, 2]]);
var result = matrix.dot(matrixB);
print(result);
// Output:
// 4 4
// 10 8
// Determinant of a matrix
var determinant = matrix.determinant();
print(determinant); // Output: -2
// Inverse of Matrix
var inverse = matrix.inverse();
print(inverse);
// Output:
// -2.0 1.0
// 1.5 -0.5
// Transpose of a matrix
var transpose = matrix.transpose();
print(transpose);
// Output:
// 1 3
// 2 4
// Find the normalized matrix
var normalize = matrix.normalize();
print(normalize);
// Output:
// 0.25 0.5
// 0.75 1.0
// Norm of a matrix
var norm = matrix.norm();
print(norm); // Output: 5.477225575051661
// Sum of all the elements in a matrix
var sum = matrix.sum();
print(sum); // Output: 10
// determine the trace of a matrix
var trace = matrix.trace();
print(trace); // Output: 5
More operations are available
var v = Matrix([
[1, 2, 3],
[4, 5, 6],
[1, 3, 5]
]);
var b = Matrix([
[7, 8, 9],
[4, 6, 8],
[1, 2, 3]
]);
var r = Row([7, 8, 9]);
var c = Column([7, 4, 1]);
var d = Diagonal([1, 2, 3]);
print(d);
/// Output:
/// 1 0 0
/// 0 2 0
/// 0 0 3
v[1][2] = 0;
var u = v[1][2] + r[0][1];
print(u); // 9
var z = v[0][0] + c[0][0];
print(z); // 8
var y = v[1][2] + b[1][1];
print(y); // 9
var a = Matrix();
a.copyFrom(y); // Copy another matrix
var k = v.row(1); // Get all elements in row 1
print(k); // [1,2,3]
var n = v.column(1); // Get all elements in column 1
print(n); // [1,4,1]
Partition of Matrix #
// create a matrix
Matrix m = Matrix([
[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[5, 7, 8, 9, 10]
]);
// Extract a submatrix with rows 1 to 2 and columns 1 to 2
Matrix submatrix = m.submatrix(rowRange: "1:2", colRange: "0:1");
Matrix submatrix = m.submatrix(rowStart: 1, rowEnd: 2, colStart: 0, colEnd: 1);
// submatrix will be:
// [
// [6]
// ]
var sub = m.slice(0, 3, 2, 3);
// submatrix will be:
// [
// [3],
// [8],
// [8]
// ]
// Get a submatrix
Matrix subMatrix = m.subset("1:2", "1:2");
Manipulating the matrix #
Manipulate the matrices
// concatenate
// axis 0
var l1 = Matrix([
[1, 1, 1],
[1, 1, 1],
[1, 1, 1]
]);
var l2 = Matrix([
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
]);
var l3 = Matrix().concatenate([l1, l2]);
print(l3);
// axis 1
var a1 = Matrix([
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]
]);
var a2 = Matrix([
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]);
var a3 = Matrix().concatenate([a2, a1], axis: 1);
print(a3);
a3 = a2.concatenate([a1], axis: 1);
print(a3);
// Reshape the matrix
var matrix = Matrix([[1, 2], [3, 4]]);
var reshaped = matrix.reshape(1, 4);
print(reshaped);
// Output:
// 1 2 3 4
// Sort the elements in a matrix
var matrix = Matrix([[3], [1], [4]]);
var sortedMatrix = matrix.sort(ascending: false);
print(sortedMatrix);
// Output:
// 4
// 3
// 1
// Remove a row
var matrixWithoutRow = matrix.removeRow(1);
print(matrixWithoutRow);
// Output:
// 1 2
// 5 6
Solving Linear Systems of Equations #
Use the solve method to solve a linear system of equations:
Matrix a = Matrix([
[1, 2],
[3, 4]
]);
Matrix b = Matrix([
[5, 6],
[7, 8]
]);
// Solve the linear system Ax = b
Matrix x = a.solve(b);
Boolean Operations #
Some functions in the library that results in boolean values
// Check contain or not
var matrix1 = Matrix([[1, 2], [3, 4]]);
var matrix2 = Matrix([[5, 6], [7, 8]]);
var matrix3 = Matrix([[1, 2, 3], [3, 4, 5], [5, 6, 7]]);
var targetMatrix = Matrix([[1, 2], [3, 4]]);
print(targetMatrix.containsIn([matrix1, matrix2])); // Output: true
print(targetMatrix.containsIn([matrix2, matrix3])); // Output: false
print(targetMatrix.notIn([matrix2, matrix3])); // Output: true
print(targetMatrix.notIn([matrix1, matrix2])); // Output: false
print(targetMatrix.isSubMatrix(matrix3)); // Output: true
Check Equality of Matrix
var m1 = Matrix([[1, 2], [3, 4]]);
var m2 = Matrix([[1, 2], [3, 4]]);
print(m1 == m2); // Output: true
print(m1.notEqual(m2)); // Output: true
Compare elements of Matrix
var m = Matrix([[1, 2], [3, 4]]);
var result = Matrix.compare(m, '>', 2);
print(result);
// Output:
// false false
// true true
Other Functions #
The Matrix class provides various other functions for matrix manipulation and analysis.
// Swap rows
var matrix = Matrix([[1, 2], [3, 4]]);
matrix.swapRows(0, 1);
print(matrix);
// Output:
// 3 4
// 1 2
// Swap columns
matrix.swapColumns(0, 1);
print(matrix);
// Output:
// 2 1
// 4 3
// Index (row,column) of an element in the matrix
var index = m.indexOf(3);
print(index);
// Output: [1, 0]
// Get the leading diagonal of the matrix
var m = Matrix([[1, 2], [3, 4]]);
var diag = m.diagonal();
print(diag);
// Output: [1, 4]
// Iterate through elements in the matrix using map function
var doubled = m.map((x) => x * 2);
print(doubled);
// Output:
// 2 4
// 6 8
// // Iterate through elements in the matrix using foreach method
var m = Matrix([[1, 2], [3, 4]]);
m.forEach((x) => print(x));
// Output:
// 1
// 2
// 3
// 4
Testing #
Tests are located in the test directory. To run tests, execute dart test in the project root.
License #
This library is provided under the MIT License.