MtxVec expressions

It was Delphi 2006 that first introduced the ability to overload operators on records. This feature enabled MtxVec to add high-performance support for vectors, matrices and complex numbers in expressions.

Operator overloading

Vectors, matrices, complex numbers and scalars can be mixed in Delphi code. It is sufficient to declare a variable as Vector or Matrix, without having to worry about object creation or destruction: the compiler automatically wraps the freeing of objects in try/finally blocks. Internally, object and memory allocations are cached, ensuring best-in-class performance. The syntax is best demonstrated by an example:

uses MtxExpr, Math387;

procedure TMainForm.Button1Click(Sender: TObject);
var am,bm: Matrix;
    av: Vector;
    ac: TCplx;
    arr: TDoubleArray;
    i: integer;
begin
    ac := '1+2i';  //Convert from string to complex number
    am := RandGauss(5,5,true); //5x5 complex matrix with Gaussian noise
    av := Ramp(25);    //real vector = [0, 1, 2,...., 23, 24]

    bm := am*av + ac + 2;  // (*,/) treat Matrices as vectors
                           // Same as: ./  and .* operators (not linear algebra)

    //To make linear algebra multiplication and divison use functions

    bm := Divide(am,bm) + 2;  //matrix divison
    bm := Mul(am,bm) + 2;  //matrix multiply

    //of course you can mix matrices and vectors

    av.Resize(5);
    av := Mul(av,bm) + 2;  //vector from left and matrix multiply
    av.CopyToArray(arr);  //copy data to array of double
    av := Vector(arr);  //copy data from array to vector

    for i := 0 to av.Length-1 do  //standard loop example
    begin
        av[i] := av[i]*2;   //work with vector as an array
    end;
end;

end.

MtxVec expressions are available for Delphi and C++Builder 2006 and later. A further set of features was added in 2022:

procedure TMainForm.VectorMatrixButtonClick(Sender: TObject);
var a,b,c: Vector;
    am, bm: Matrix;
begin
    a := [0,1,2,3,4,5,6,7,8,9];

    c.Size(10);
    c.Select[5,5] := a.Select[0,5] + a.Select[5,5];
    // same as: c(6:10)= a(1:5) + a(6:10)
    // Select property has two parameters: Index and Len

    // c = [ , , , , ,5,7,9,11,13]

    c.Select[0,5] := c.Select[5,5];

    // c = [ 5,7,9,11,13 ,5,7,9,11,13]

    // OR

    c.Size(10);
    c.SetZero;

    // SelectIndex property has three parameters: StartIndex, Step, StopIndex
    c.SelectIndex[5,1,9] := a.SelectIndex[0,1,4] + a.SelectIndex[5,1,9];
    // same as: c(6:1:10)= a(1:1:5) + a(6:1:10)

    // c = [ , , , , ,5,7,9,11,13]

    c.SelectIndex[0,1,4] := c.SelectIndex[5,1,9];
    // same as: c(0:4)= c(5:9)

    // c = [ 5,7,9,11,13 ,5,7,9,11,13]

    am := [[1,2,3,4],
           [3,3,3,3],
           [2,2,2,2],
           [4,3,2,1]];

    am.Select2D[0, 0, 3, 3] := am.Select2D[0, 0, 3, 3] - 2;

    //    am becomes:
    //    am  = [[-1,0,1,4],
    //           [ 1,1,1,3],
    //           [ 0,0,0,2],
    //           [ 4,3,2,1]];

    bm := am.Select2D[0, 0, 3, 3];

    //matrix multiply and transpose:
    am.Select2D[0, 0, 3, 3] := Mul(bm, Transp(bm));
    //same as: am(1:3, 1:3) = am(1:3,1:3)*am(1:3,1:3)'
end;

The code is, of course, still (much) faster than code using native Delphi arrays, as long as the vectors and matrices are big enough (more than 10 elements). The Select property returns a view, a subranged vector; in the case of matrices, it makes a copy. This type of syntax is useful for quick prototyping and debugging.