Finaquant’s Matrix and Vector Functions

Both of our .NET libraries finaquant® protos (non-commercial) and finaquant® calcs (commercial) contain Matrix and Vector functions in addition to Table Functions. In fact, table functions are higher level constructs that are based on matrix and vector functions of the library.

You may find examples for Matrix and Vector functions in the Visual Studio file FinaquantCalcsStarter (or FinaquantProtosStarter) that you can download at the corresponding product page (see related downloads).

Visual Studio file FinaquantCalcsStarter

Parallel to field types of tables (text attribute, numeric attribute, key figure) there are three types of matrices and vectors:

  1. KeyMatrix/KeyVector (double)
  2. NumMatrix/NumVector (integer)
  3. TextMatrix/TextVector (string)

Here are some examples for creating matrices, and making operations with them:

// create 2x3 matrix with element values
KeyMatrix M1 = KeyMatrix.CreateMatrixWithElements(2, 3,
	1.0, 2.0, 3.0,
	4.0, 5.0, 6.0);
// view matrix
System.Diagnostics.Debug.WriteLine("2x3 matrix with elements \n" + M1);
 
// access elements of matrix
System.Diagnostics.Debug.WriteLine("M(1, 1) = " + M1[0, 0]);   // first element
System.Diagnostics.Debug.WriteLine("M(2, 3) = " + M1[1, 2]);   // last element
 
// matrix properties
System.Diagnostics.Debug.WriteLine("Element count = " + M1.ElementCount);
System.Diagnostics.Debug.WriteLine("Number of rows = " + M1.nRows);
System.Diagnostics.Debug.WriteLine("Number of columns = " + M1.nCols);
System.Diagnostics.Debug.WriteLine("Is M1 empty matrix? " + M1.IsEmpty);
 
// transpose matrix
M2 = KeyMatrix.Transpose(M1);
 
// matrix multiplication
M3 = M1 * M2;
 
// matrix-scalar multiplication
M4 = 5.25 * M3;

Matrix and Vector function examples in FinaquantCalcsStarter:

Matrix Functions
//************************************************
// Matrix of type KeyMatrix (key figure)
//************************************************
 
// define variables
KeyMatrix kM1, kM2, kM3;
 
// Create 2x3 matrix with constant elements
kM1 = KeyMatrix.CreateConstantMatrix(nRows: 2, nCols: 3, ConstValue: 3.0);
 
// display matrix in immediate window
System.Diagnostics.Debug.WriteLine("2x3 constant matrix \n" + kM1);
 
// Create 3x3 identity matrix
kM1 = KeyMatrix.CreateIdentityMatrix(nRows: 3);
System.Diagnostics.Debug.WriteLine("3x3 identity matrix \n" + kM1);
 
// Create 2x3 matrix with element values
kM1 = KeyMatrix.CreateMatrixWithElements(2, 3,
	1.0, 2.0, 3.0,
	4.0, 5.0, 6.0);
System.Diagnostics.Debug.WriteLine("2x3 matrix with elements \n" + kM1);
 
// access elements of matrix
System.Diagnostics.Debug.WriteLine("M(1, 1) = " + kM1[0, 0]);   // first element
System.Diagnostics.Debug.WriteLine("M(2, 3) = " + kM1[1, 2]);   // last element
 
// matrix properties
System.Diagnostics.Debug.WriteLine("Element count = " + kM1.ElementCount);
System.Diagnostics.Debug.WriteLine("Number of rows = " + kM1.nRows);
System.Diagnostics.Debug.WriteLine("Number of columns = " + kM1.nCols);
System.Diagnostics.Debug.WriteLine("Is M empty matrix? " + kM1.IsEmpty);
 
// create 3x2 matrix with randon-valued elements between 0 and 1
kM1 = KeyMatrix.CreateRandomMatrix(nRows: 3, nCols: 2, nRandSeed: 100);
System.Diagnostics.Debug.WriteLine("3x2 matrix with random elements = \n" + kM1);
 
// transpose matrix
kM2 = KeyMatrix.Transpose(kM1);
System.Diagnostics.Debug.WriteLine("2x3 transposed random matrix = \n" + kM2);
 
// sum of all matrix elements
System.Diagnostics.Debug.WriteLine("Element sum = " + kM2.SumOfElements(RowColDirection.AllElements));
 
// Row-by-row sum of matrix elements
System.Diagnostics.Debug.WriteLine("Row-by-row sum = " + kM2.SumOfElements(RowColDirection.RowByRow));
 
// Create 2x2 matrix with sequentiel elements values
kM1 = KeyMatrix.CreateSequentielMatrix(nRows: 2, nCols: 2, StartValue: 1.0, Interval: 1.0);
System.Diagnostics.Debug.WriteLine("2x2 sequentiel matrix = \n" + kM1);
 
// determinant of 2x2 sequential matrix created above
System.Diagnostics.Debug.WriteLine("Determinant of 2x2 sequentiel matrix = \n" +
	KeyMatrix.Determinant(kM1));
 
// inverse matrix
kM2 = KeyMatrix.Inverse(kM1);
System.Diagnostics.Debug.WriteLine("Inverse matrix = \n" + kM2);
 
// matrix multiplication: M x inverse(M)
System.Diagnostics.Debug.WriteLine("M x inv(M) = \n" + KeyMatrix.MultiplyMatrices(kM1, kM2));
 
// multiply all elements of random matrix with scalar 100
kM1 = KeyMatrix.CreateRandomMatrix(nRows: 2, nCols: 3, nRandSeed: 100);
System.Diagnostics.Debug.WriteLine("2x3 random matrix R = \n" + kM1);
 
kM2 = KeyMatrix.MultiplyMatrixWithScalar(kM1, 100.0);
System.Diagnostics.Debug.WriteLine("R x 100 = \n" + kM2);
 
// matrix partitioning
NumVector RowIndices = NumVector.CreateVectorWithElements(0, 1);    // 1. and 2. rows
NumVector ColIndices = NumVector.CreateVectorWithElements(0, 2);    // 1. and 3. columns
 
kM2 = KeyMatrix.MatrixPartition(kM1, RowIndices, ColIndices);
System.Diagnostics.Debug.WriteLine("Submatrix M([1 2], [1 3]) = \n" + kM2);
 
// exponential of every matrix element
kM2 = KeyMatrix.OperationOnElements(kM1, Math.Exp);
System.Diagnostics.Debug.WriteLine("exponential(M) = \n" + kM2);
 
//************************************************
// Matrix of type NumMatrix (numeric attribute)
//************************************************
 
// define variables
NumMatrix nM1, nM2, nM3;
 
// Create 3x4 matrix with random elements between 10 and 99
nM1 = NumMatrix.CreateRandomMatrix(nRows: 3, nCols: 4, nRandSeed: 100, minVal: 10, maxVal: 99);
System.Diagnostics.Debug.WriteLine("Random matrix (10, 99) = \n" + nM1);
 
// Create 2x3 matrix with sequentiel valued elements from 0 to 5
nM1 = NumMatrix.CreateSequentielMatrix(nRows: 2, nCols: 3, StartValue: 0, Interval: 1);
System.Diagnostics.Debug.WriteLine("Sequentiel matrix = \n" + nM1);
 
//************************************************
// Matrix of type TextMatrix (text attribute)
//************************************************
 
// define variables
TextMatrix tM1, tM2, tM3;
 
// Create 2x3 matrix with element values
tM1 = TextMatrix.CreateMatrixWithElements(2, 3,
	"tiger", "lion", "leopard",
	"cheetah", "puma", "jaguar");
System.Diagnostics.Debug.WriteLine("2x3  cat matrix = \n" + tM1);
 
// generate matrix by combinations
TextVector colors = TextVector.CreateVectorWithElements("red", "blue", "green", "black");
TextVector cars = TextVector.CreateVectorWithElements("Audi", "Ford", "Toyota");
 
tM1 = TextMatrix.GenerateMatrixByCombinations(colors, cars);
System.Diagnostics.Debug.WriteLine("All possible combinations of colors & cars = \n" + tM1);
Vector Functions
//************************************************
// Vector of type KeyVector (key figure)
//************************************************
 
// define variables
KeyVector kV1, kV2, kV3;
 
// Create vector with element values
kV1 = KeyVector.CreateVectorWithElements(2, 4, 6, 9);
System.Diagnostics.Debug.WriteLine("Create vector with elements: " + kV1);
 
// Create a sequence vector
kV1 = KeyVector.CreateSequenceVector(StartValue: 1, Interval: 2, nLength: 5);
System.Diagnostics.Debug.WriteLine("Sequence vector: " + kV1);
 
// Create vector with a constant value for all elements
kV1 = KeyVector.CreateConstantVector(nLength: 6, ConstantValue: 5.2);
System.Diagnostics.Debug.WriteLine("Constant vector: " + kV1);
 
// Create vector with random values between 0 and 1
kV1 = KeyVector.CreateRandomVector_A(nLen: 4, nRandomSeed: 100);
System.Diagnostics.Debug.WriteLine("Random vector (0, 1): " + kV1);
 
// Create vector with random values between 10 and 99
kV1 = KeyVector.CreateRandomVector_B(nLen: 6, nRandomSeed: 100, LowerLimit: 10, UpperLimit: 99);
System.Diagnostics.Debug.WriteLine("Random vector (10, 99): " + kV1);
 
// Add two vectors: KeyVector.AddTwoVectors()
kV1 = KeyVector.CreateVectorWithElements(2, 4, 6, 8);
kV2 = KeyVector.CreateVectorWithElements(1, 2, 3, 4);
kV3 = kV1 + kV2;
System.Diagnostics.Debug.WriteLine("V1 + V2 = " + kV3);
 
// Apply math operation on every vector element
kV1 = KeyVector.CreateVectorWithElements(10, 100, 1000);
System.Diagnostics.Debug.WriteLine("V1 = " + kV1);
 
kV2 = KeyVector.OperationOnElements(kV1, Math.Log10);
System.Diagnostics.Debug.WriteLine("log10(V) = " + kV2);
 
//************************************************
// Vector of type NumVector (numeric attribute)
//************************************************
 
// define variables
NumVector nV1, nV2, nV3;
 
// Create a sequence vector
nV1 = NumVector.CreateSequenceVector(StartValue: 1, Interval: 2, nLength: 5);
System.Diagnostics.Debug.WriteLine("Sequence vector: " + nV1);
 
// Vector partition: Get subvector
NumVector PositionIndices = NumVector.CreateVectorWithElements(0, 1, 2, 0, 1, 2);
nV2 = NumVector.Partition(nV1, PositionIndices);
System.Diagnostics.Debug.WriteLine("Subvector: " + nV2);
 
// swap vector elements 1 and 5
NumVector.SwapVectorElements(nV1, ind1: 0, ind2: 4);
System.Diagnostics.Debug.WriteLine("Swap 1-5: " + nV1);
 
// reverse element order
System.Diagnostics.Debug.WriteLine("Reverse(V1): " + NumVector.Reverse(nV1));
 
// find element indices; find positions (indices) of 3 in vector
bool IfFound;
nV1 = NumVector.CreateVectorWithElements(3, 2, 3, 2, 3, 4, 5, 3);
 
NumVector.FindElements(nV1, ComparisonOption.Equal, SearchValue: 3,
	Vind: out nV2, Vval: out nV3, IfFound: out IfFound);
System.Diagnostics.Debug.WriteLine("Positions of 3 in vector: " + nV2);
 
//************************************************
// Vector of type TextVector (text attribute)
//************************************************
 
// define variables
TextVector tV1, tV2, tV3;
 
// Create vector with element values
tV1 = TextVector.CreateVectorWithElements("tiger", "lion", "leopard");
System.Diagnostics.Debug.WriteLine("Create vector with elements: " + tV1);
 
// find intersection (common elements) of two vectors
tV2 = TextVector.CreateVectorWithElements("tiger", "bear", "leopard", "jaguar");
tV3 = TextVector.Intersection(tV1, tV2);
System.Diagnostics.Debug.WriteLine("V1 INTERSECTION V2 = " + tV3);
 
// Sort vector
tV3 = TextVector.Sort(tV2, FinaquantCalcs.SortOrder.Ascending);
System.Diagnostics.Debug.WriteLine("Sort(V) = " + tV3);
You can find the complete list of matrix and vector functions in the user manuals of finaquant® protos and finaquant® calcs. Or you can get them listedthanks to intellisense help in Visual Studio:

List of Matrix Functions

This entry was posted in Calculation engine and tagged , . Bookmark the permalink.

Leave a Reply