Definition of matrices. How do I do a matrix declaration, matrix memory allocation, and matrix initialization as a three step process?


(Cristian Jimenez) #1

Hi everyone, my question is the following:

How do I do a matrix declaration, matrix memory allocation, and matrix initialization as a three step process? The examples provided do it like:

Matrix<double> m = Matrix<double>.Build.Random(3, 4); 

But in the example above all the three steps (declaration, memory allocation, and initialization) are done at once.

Other way they do it is:

var A = Matrix<double>.Build.Dense(5,5);

But here again all the three steps are done (declaration, memory allocation, and initialization) as one step.

Thanks for your help.


(Christoph Rüegg) #2

What would you like to achieve with this separation? Some levels of separation are possible, but not entirely along these lines - by design.


(Cristian Jimenez) #3

I would like to define a matrix in a struct so the matrix can be returned as a member of the struct. This maybe possible with a class (no sure):


struct CalculationOut
{
List<double> variable; //Every element here is a function of x and y
List<double> value; //Every element here is a function of x and y
matrix <double> QMatrixOpt; //Every element of the matrix is a function of variable and value

}

 //The method bellow calculates the struct every time
calculationOut method1 (double x, double y,int size) //This method calculates the struct
{
CalculationOut struct1 = new CalculationOut();

	for (int i=0; i<size; i++)
	{
	struct1.value.add(5*x*x*y*y*y+12*y*x-15*y)
	struct1.variable.add(9*x*x*y+15*x+150*y*y*y)
	}
	
	struct1.QMatrixOpt.Build.Dense(size,size); //If this is possible
	for (int i=0; i<size; i++)
	{
		for (int j=0; j<size; j++)
			{
			struct1.QMatrixOpt[i.j]= 5 *struct1.value[i]-19*struct1.variable[j];
			}
	}
return(struct1);
}

//This method uses the values of the struct to calculate y given the condition that determinant of the matrix is zero with
// a given x and size
double MethodForCalculationOfY()
{
double x;
double y;
int size;
CalculationOut struct2 = new CalculationOut();
size=11;
x=3.14159564;
y=0;

struct2 = calculationOut method1 ( x, y, size);

	while (0.000001> Math.Abs(struct2.QMatrixOpt.Determinant()))
	{
		if(0.000001> struct2.QMatrixOpt.Determinant())
		{
		y=y+1;
		}
		else
		{
		y=y-1;
		}
		struct2 = calculationOut method1 ( x, y, size);
	}
}


(Christoph Rüegg) #4

Note that both List<double> and Matrix<double> are reference types. While you can use reference types in a structure (which is a value type, not a reference type), the structure then effectively just contains a reference to these objects, which live on the heap. As a consequence, you can just overwrite this reference to point to whatever instance you want.

Instead of:

struct1.QMatrixOpt.Build.Dense(size,size); //If this is possible

you can therefore just create and initialize the matrix object and make the reference in the struct point to it:

struct1.QMatrixOpt = Matrix<double>.Build.Dense(size, size);

Note that the loop before that will not work that way: the two list don’t exist yet (the struct1.value and struct1.variable references still point to nowhere), you’d also first have to create the list objects and then point the references to these objects, e.g. struct1.value = new List<double>();

Does that help?


(Cristian Jimenez) #5

@cdrnet It did work. Thank you so much for your collaboration. Sorry I forgot to do the memory allocation for the arrays. But you were right. Thanks again.