I have the following class (which previously supported only an ordinary array type) converted to generic class. The code does the matrix multiplication but there is one problem. The line:
results in compile time error since generic type T cannot support +-*/ operators(since the class doesn't understand which value type the incoming parameter will be). So I have to define an interface named e.g. IOperatorSupport which will provide abstract prototypes of supporting those arithmetic operators. I'm gonna take a long road thinking and implementing such interface. Has anyone got such interface already defined? Or I'm gonna do it myself this night... Thanks
Here's the class itself:
Code:
MM[i, j] += matrixA[i, k] * matrixB[k, j];
results in compile time error since generic type T cannot support +-*/ operators(since the class doesn't understand which value type the incoming parameter will be). So I have to define an interface named e.g. IOperatorSupport which will provide abstract prototypes of supporting those arithmetic operators. I'm gonna take a long road thinking and implementing such interface. Has anyone got such interface already defined? Or I'm gonna do it myself this night... Thanks
Here's the class itself:
Code:
public class Lin_Alg<T>where T: struct, IOperatorSupport //note this interface
{
public static List<T>[,] MMult<T>(List<T>[,] matrixA, List<T>[,] matrixB)
{
int n = matrixA.GetLength(0);
int m = matrixA.GetLength(1);
int a = matrixB.GetLength(0);
int b = matrixB.GetLength(1);
List<T>[,] MM = new List<T>[n, b];
int k = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < b; j++)
{
while (k < m)
{
MM[i, j] += matrixA[i, k] * matrixB[k, j]; //offending line
k++;
}
k = 0;
}
}
return MM;
}
}