Linear Algebra and the C Language/a0el
Install and compile this file in your working directory.
/* ------------------------------------ */
/* Save as : c00a.c */
/* ------------------------------------ */
#include "v_a.h"
/*
The zero row and the zero column are not used!!!
In the row zero you have an index for the columns.
In the columns zero you have the size of the matrix.
see the pall_mR(); function.
*********************
The size of the rows is into A[R_SIZE][C0] = A[R0][C0]
The size of the columns is into A[C_SIZE][C0] = A[R1][C0]
*********************
The first element of the matrix is: A[R1 ][ C1]
For a 10x10 matrix the last element is: A[R10][C10]
*********************
*/
/* ------------------------------------ */
/* ------------------------------------ */
double **x_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; /* I add a row. The row zero */
ac = c + C1; /* I add a column. The column zero */
/* *************** */
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[0]+i*ac;
/* *************** */
A[R_SIZE][C0] = ar; /* I copy the number of lines */
A[C_SIZE][C0] = ac; /* I copy the number of columns */
for(r=R2; r<A[R_SIZE][C0]; r++) /* I set the zero column to zero */
/* without touching the size of */
A[r][C0] = 0.; /* the matrix. */
for(c=C1; c<A[C_SIZE][C0]; c++) /* I put the index in the row zero*/
A[R0][c] = c;
m0_mR(A); /* Set all coefficients to zero */
return(A);
}
/* ------------------------------------ */
/* ------------------------------------ */
void fun(int r,int c)
{
double **A = r_mR(x_i_mR(r,c),9);
clrscrn();
printf(" A[R%d,C%d]: (p_mR();)\n",rsize_R(A),csize_R(A));
p_mR(A, S4,P0,C8);
printf("\n\n\n");
printf(" A[R%d,C%d]: (pall_mR();)\n",rsize_R(A),csize_R(A));
pall_mR(A, S4,P0,C9);
f_mR(A);
}
/* ------------------------------------ */
int main(void)
{
time_t t;
srand(time(&t));
do
fun(rp_I(R4),rp_I(C8));
while(stop_w());
return 0;
}
/* ------------------------------------ */
/* ------------------------------------ */
Screen output example:
A[R3,C6]: (p_mR();)
+4 -7 -5 -4 +2 +1
+3 -5 -2 -3 +8 +6
+7 +7 -7 -7 +9 +4
A[R3,C6]: (pall_mR();)
+4 +1 +2 +3 +4 +5 +6
+7 +4 -7 -5 -4 +2 +1
+0 +3 -5 -2 -3 +8 +6
+0 +7 +7 -7 -7 +9 +4
Press return to continue
Press X return to stop