Matrix algebra is essential in solving different practical problems starting from structural engineering to creating geometries. GenerativeComponents has some inbuilt functions to solve basic linear algebra problems.
These functions belong to Bentley Geometry Library. We can access these functions from Expression Builders in Script Editor.
However, we can only use these functions for a maximum up to a 4×4 matrix. For higher-order, we need custom functions. In this article, we have provided some basic functions to deal with higher-order matrices.
Using GC List
Before creating the custom function, let’s discuss how we have used the GC list for fundamental calculations. GC List is replicable. Which makes Scalar multiplication and Matrix addition very straightforward.
Matrix Algebra Functions
1. Matrix Multiplication
Function Signature
double[] MatrixMultiplication(double[][] MatrixValueA, double[][] MatrixValueB)
Full Code
double[] MatrixMultiplication(double[][] MatrixValueA, double[][] MatrixValueB)
//Multiply two matrices
{
if (IsConformable(MatrixValueA, MatrixValueB))
{
double MatrixAB = FilledList2d(MatrixValueA.Count, MatrixValueB[0].Count,0);
for (int i = 0; i < MatrixValueA.Count; ++i)
{
for (int j = 0; j < MatrixValueB[0].Count; ++j)
{
for (int k = 0; k < MatrixValueA[0].Count; ++k)
{
MatrixAB[i][j] = MatrixAB[i][j] + MatrixValueA[i][k]*MatrixValueB[k][j];
breakpoint;
}
}
}
return MatrixAB;
}
else
{
return {{null}};
}
}
bool IsConformable(object[] Matrix_A, object[] Matrix_B)
{
//Check if matrices are conformable for multiplication
//breakpoint;
if(IsRectangle(Matrix_A) && IsRectangle(Matrix_B))
{
if (Matrix_A[0].Count == Matrix_B.Count)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
2. Determinant
Function signature
double Determinant(object[] SquareMatrix)
Full Code
double Determinant(object[] SquareMatrix)
//Calculate determinant of a given square matrix
{
double Sum, DetCofac;
if (SquareMatrix.Count == 2)
{
return SquareMatrix[0][0]*SquareMatrix[1][1] - SquareMatrix[0][1]*SquareMatrix[1][0];
}
if (SquareMatrix.Count == 1)
{
return SquareMatrix[0][0];
}
if (SquareMatrix.Count > 2)
{
Sum = 0;
for (int i = 0; i < SquareMatrix[0].Count; ++i)
{
DetCofac = Determinant(Cofactors(SquareMatrix, 0, i));
Sum = Sum + Pow(-1,i)*SquareMatrix[0][i]*DetCofac;
}
return Sum;
}
}
double[] Cofactors(object[] Matrix, int i, int j)
//returns co-factors of a given matrix of a given position
{
Matrix = Matrix.RemoveAt(i);
for (int k = 0; k < Matrix.Count; ++k)
{
Matrix[k] = Matrix[k].RemoveAt(j);
}
return Matrix;
}
3. Matrix Inverse
Gauss-Jordan Elimination method is used for calculating Matrix Inversion
Function Signature
double[] MatrixInverse(object[] MatrixToBeInversed)
Full Code
double[] MatrixInverse(object[] MatrixToBeInversed)
//Inverts a Matrix
{
double IdentityMatrixRow;
int SizeOfMatrix = MatrixToBeInversed.Count;
//Augmenting Identity Matrix
for (int i = 0; i < MatrixToBeInversed.Count; ++i)
{
IdentityMatrixRow = FilledList(MatrixToBeInversed.Count, 0);
IdentityMatrixRow[i] = 1;
MatrixToBeInversed[i] = MatrixToBeInversed[i].Append(IdentityMatrixRow);
MatrixToBeInversed[i] = MatrixToBeInversed[i].Flatten();
}
//Gauss-Transformation operations
for (int j = 0; j < MatrixToBeInversed.Count; ++j)
{
MatrixToBeInversed[j] = MatrixToBeInversed[j]/MatrixToBeInversed[j][j];
//breakpoint;
for (int k = 0; k <MatrixToBeInversed.Count; ++k)
{
if (k != j )
{
MatrixToBeInversed[k] = MatrixToBeInversed[k] - MatrixToBeInversed[j]*MatrixToBeInversed[k][j];
//breakpoint;
}
}
}
//return the inverse
return from double val in MatrixToBeInversed select val.TakeLast(SizeOfMatrix) ;
}
Further Scope of Work
The above functions are very elementary we can further add functions for
- Solving Linear Equations
- Rank of Matrix
- Eigen Value and Eigen Vector