matrices 1.0.5 copy "matrices: ^1.0.5" to clipboard
matrices: ^1.0.5 copied to clipboard

Dart Matrix Computing Library

Matrices - A Matrix Computation Library in Dart

中文文档
Grayscale releases are marked with '!'
Unimplemented functions are marked with '*'


CONTENT


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. It is now in the very early age, and its API may change frequently. It is now written purely in Dart programming language and C-FFI might be introduced to lift its performance in the near future.


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. Importing 'Matrix.dart' is required before using methods listed below.

Illustrations of Matrix class #

The matrix class only has one public field - 'matrix' whose runtimetype is List<List

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 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.matrix[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

  mat.matrix[0] = [1, 4, 5, 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. Importing 'SquareMatrix.dart' is required before using methods listed below.

Illustrations of SquareMatrix class #

Createa a SquareMatrix instance #

Create a squarematrix

  /// 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]

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 squarematrix 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 squarematrix 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 #


Emoji method #

Providing frequently-used functions, like creating vectors, batch updating elements, etc. Importing 'Emoji.dart' is required before using methods listed below.

Create vectors ! * #

o_n

  /// List<double> o_n(int n)

l_n

  /// List<double> o_n(int n)

m_n

  /// List<double> o_n(int m, int n)

l_m_n

  /// List<double> o_n(int l, int m, int n)
10
likes
0
pub points
77%
popularity

Publisher

verified publisherabandoft.com

Dart Matrix Computing Library

Homepage

License

unknown (LICENSE)

More

Packages that depend on matrices