I need to multiply two relatively large matrices and do it many times (in a loop). However, the format (type of objects) how these matrices are stored in the memory should be chosen by user. There are three possibilities:
double [,] M1; double [,] M2;
( two-dimensional arrays)double[][] M1; double [][]M2;
( jagged arrays).Matrix <double> M1;Matrix <double> M2;
( Math Net Numerics format).
Each format suits a corresponding method ( taken from a numerical library as you may have already guessed). I need to avoid boxing and unboxing at all cost ( since its expensive) so the following code will not work for me ( unless you tell me that boxing/unboxing is cheaper than fast matrix multiplication):
object M1;
object M2;
switch case
{
case 1:
M1 = new double[rows,columns];
.../etc
}
One way to solve it is very straightforward: just to declare 6 matrices( 6 variables) and at runtime initialize two of them. But this looks ugly and will make the code much more complicated.
My question: is there any elegant solution of this problem? I am new to C# but the searching over the site did not help me much.