Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tools.Matrix

A Delphi record wrapper around IMatrix from MrMath by MrMath (Michael Rabat), adding operator overloading and a cleaner API.

Dependency

Requires MrMath — add Matrix.pas (and its dependencies) to your project.

What it adds

Feature Example
Arithmetic operators C := A + B, A * 2.0, -A, A / 3.0
Matrix multiplication C := A * B
Element-wise multiply C := A.MultiElementweise(B)
Implicit conversions TMatrix ↔ IMatrix ↔ TMatrixArray
Index access M[col, row] := 1.5

Quick start

uses tools.Matrix;

var
  A, B, C: TMatrix;
begin
  // Create and fill
  A := TMatrix.Create(2, 2);
  A[0,0] := 1;  A[1,0] := 2;
  A[0,1] := 3;  A[1,1] := 4;

  // Identity matrix
  B := TMatrix.CreateEye(2);

  // Operators
  C := A + B;          // addition
  C := A * 2.5;        // scalar multiply
  C := A * B;          // matrix multiply
  C := A.Transpose;    // transpose
end;

Solving a linear system

// 2x + y = 5
//  x + 3y = 10  →  x=1, y=3
var
  M: TMatrix;
  b, x: TMatrixArray;
  IsSingular: Boolean;
begin
  M := TMatrix.Create(2, 2);
  M[0,0] := 2;  M[1,0] := 1;
  M[0,1] := 1;  M[1,1] := 3;

  b := TMatrixArray.Create(5, 10);
  x := M.Solve(b, IsSingular);

  if not IsSingular then
    Writeln(x[0]:0:4, ' ', x[1]:0:4);  // 1.0000  3.0000
end;

Row/column manipulation

M.AddRow(0.0);            // append a row filled with 0
M.InsertCol(1, 1.0);      // insert column at index 1 filled with 1
M.DeleteRow(2);           // remove row at index 2
M.SetRow(0, 99.0);        // set entire row 0 to a constant

Symmetry

if not M.IsSymmetric then
  M := M.ToSymMatrix;     // mirror lower triangle to upper

Running the tests

Add tools.Matrix.Test.pas to a DUnitX console project and run. All 35 tests should pass.

License

Mozilla Public License 2.0 — same as the underlying MrMath library.

About

Delphi record wrapper for MrMath's IMatrix with operator overloading and a clean API

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages