I am trying to calculate eigenvalues of a matrix using a downloaded
tqli algorithm
There is no way to get a double**
pointer by trying to cast double[4][4]
.
You have to use different approach to supply required double **
pointer for the tqli
function.
You can pass your data using one of the method below.
First method:
1) Allocate memory for pointers to pointers double **aa
and initialize them with pointers to row vectors in the a
array.
Second method:
2) Allocate memory for pointers to matrix rows (as above). Allocate memory for the content of the rows. Copy all element values from your ze
.
The testing program:
#include <stdlib.h>
#include <stdio.h>
#define M_SIZE 4 // size of the nxn matrix
void tqli(double d[], double e[], int n, double **z)
{
int i,j;
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
double d = z[i][j];
printf(" z[%i][%i] = %f \n", i, j, d );
}
printf("\n");
}
}
int main()
{
int i,j;
double ze[M_SIZE][M_SIZE];
double **zepointer;
ze[0][0] = 1;
ze[1][1] = 1;
ze[2][2] = 1;
ze[3][3] = 1;
ze[0][1] = 0;
ze[1][0] = 0;
ze[0][2] = 0;
ze[2][0] = 0;
ze[0][3] = 0;
ze[3][0] = 0;
ze[1][2] = 0;
ze[2][1] = 0;
ze[1][3] = 0;
ze[3][1] = 0;
ze[2][3] = 0;
ze[3][2] = 0;
double de[M_SIZE] = {0};
double ee[M_SIZE] = {0};
// 1.---------------------
printf(" First array is { {6,7,8,9}, {1,6,1,5}, {6,2,2,4}, {1,2,3,4} } \n");
double a[M_SIZE][M_SIZE]={ {6,7,8,9}, {1,6,1,5}, {6,2,2,4}, {1,2,3,4} };
double **aa = malloc( M_SIZE * sizeof(double*)); // allocate memory to hold the pointers to all rows
for(i=0; i<M_SIZE; i++)
aa[i] = a[i]; // initialize pointers to the `a` rows
tqli(de,ee,M_SIZE,aa); // test
free(aa); // free memory for row pointers
// 2.--------------------
printf(" Second array is { {1,0,0,0}, {0,1,0,0}, {0,0,1,0}, {0,0,0,1} } \n");
zepointer = malloc( M_SIZE * sizeof(double*)); // allocate memory to hold the pointers to all rows
// allocate memory for every row to hold column elements
for(i=0; i<M_SIZE; i++)
zepointer[i] = malloc (M_SIZE * sizeof (double )) ;
// assign values from your ze[][] matrix
for(i=0; i<M_SIZE; i++)
for(j=0; j<M_SIZE; j++)
zepointer[i][j] = ze[i][j];
tqli(de,ee,M_SIZE,zepointer); // test
// cleanup
for(i=0; i<M_SIZE; i++) // free memory allocated for row elements
free(zepointer[i]) ;
free(zepointer); // free memory for the row pointers
return 0;
}
Output:
First method: array is { {6,7,8,9}, {1,6,1,5}, {6,2,2,4}, {1,2,3,4} }
z[0][0] = 6.000000
z[0][1] = 7.000000
z[0][2] = 8.000000
z[0][3] = 9.000000
z[1][0] = 1.000000
z[1][1] = 6.000000
z[1][2] = 1.000000
z[1][3] = 5.000000
z[2][0] = 6.000000
z[2][1] = 2.000000
z[2][2] = 2.000000
z[2][3] = 4.000000
z[3][0] = 1.000000
z[3][1] = 2.000000
z[3][2] = 3.000000
z[3][3] = 4.000000
Second method: array is { {1,0,0,0}, {0,1,0,0}, {0,0,1,0}, {0,0,0,1} }
z[0][0] = 1.000000
z[0][1] = 0.000000
z[0][2] = 0.000000
z[0][3] = 0.000000
z[1][0] = 0.000000
z[1][1] = 1.000000
z[1][2] = 0.000000
z[1][3] = 0.000000
z[2][0] = 0.000000
z[2][1] = 0.000000
z[2][2] = 1.000000
z[2][3] = 0.000000
z[3][0] = 0.000000
z[3][1] = 0.000000
z[3][2] = 0.000000
z[3][3] = 1.000000