Linear Algebra and the C Language/a00o
Install this file in your working directory.
/* ------------------------------------ */
/* Save as : vim2.h */
/* ------------------------------------ */
/* ---------------------------------------------------------------------------
Do : Dynamically allocate a multidimensional array.
(see : FAQ of the comp.lang.c group)
You can keep the array's contents contiguous,
r0 r1 r2 ... rn
R_000|C_xxx|0_xxx|...|0_xxx
R = Number of rows.
C = Number of columns.
The declaration of the sizes into the matrices, it is my work.
So be careful.
The zero row and the zero column are not used.
*********************
The size of the row of the matrix is into A[R_SIZE][C0] = A[0][0]
The size of the column of the matrix is into A[C_SIZE][C0] = A[1][0]
*********************
The first element of the matrix is A[1][1]
For a 10x10 matrix the last element is A[10][10]
*********************
-------------------------------------------------------------------------- */
/* ------------------------------------
Ex : double **A = i_mR(R3,C5);
------------------------------------ */
double **i_mR(
int r,
int c
)
{
double **A;
int ar;
int ac;
int i;
if(r<R1||c<C1)
{
printf(" The size of the matrix must be positive integers.\n\n");
printf(" double **i_mR(); \n\n");
fflush(stdout);
getchar();
exit(EXIT_FAILURE);
}
ar = r + R1;
ac = c + C1;
A = malloc(ar * sizeof(*A));
if(!A)
{
printf(" I was unable to allocate the memory you requested.\n\n");
printf(" double **i_mR(); \n\n");
printf(" **A = malloc(ar * sizeof(*A));\n\n");
fflush(stdout);
getchar();
exit(EXIT_FAILURE);
}
A[0] = malloc(ar * ac * sizeof(**A) );
if(!A[0])
{
printf(" I was unable to allocate the memory you requested.\n\n");
printf(" double **i_mR();\n\n");
printf(" A[0] = malloc(ar * ac * sizeof(**A) );\n\n");
fflush(stdout);
getchar();
exit(EXIT_FAILURE);
}
for(i=R1; i<ar; i++) A[i] = A[R0]+i*ac;
/* ------- Save the numbers of rows and columns -------- */
A[R_SIZE][C0] = ar;
A[C_SIZE][C0] = ac;
/* ----------- Put zero in the column zero ------------- */
for(r=R2; r<A[R_SIZE][C0]; r++)
A[r][C0] = 0.;
/* ----------- Index the columns ------------- */
for(c=C1; c<A[C_SIZE][C0]; c++)
A[R0][c] = c;
m0_mR(A);
return(A);
}
/* ------------------------------------
Ex : f_mR(A;
------------------------------------ */
void f_mR(
double **A
)
{
if(A) free(A[0]);
free(A);
}
/* ------------------------------------ */
/* ------------------------------------ */
/* Same as i_mR() but work with the size
in memory.
Ex : i_RC_mR(M[R_SIZE][C0],M[C_SIZE][C0] */
/* ------------------------------------ */
double **i_RC_mR(
int R,
int C
)
{
return(i_mR(--R,--C));
}
/* ------------------------------------ */
/* ------------------------------------ */
double **A = i_mR(Rx, Cx); allows you to construct a matrix of Rx rows and Cx columns.
f_mR(A); will free the space given to the matrix.
i_RC_mR( A[R_SIZE][C0], A[C_SIZE][C0]); must be used when you want to directly retrieve the size of the matrices in memory. (If you do not want to use rsize_R(); and csize_R();)