LISTING 7 - Comprehensive Test Program for Matrix.library /* TestMatLib.c Here is a test program that calls all of the functions in Matrix.library using #pragmas. This code is FREELY DISTRIBUTABLE but NOT PUBLIC DOMAIN (written by Randy Finch) */ #include "Test.h" /*--------------- main() -----------------*/ void main(argc,argv) int argc; char **argv; { LONG i, j, k; LONG *lv[3]; LONG **lm[3]; DOUBLE *dv[3]; DOUBLE **dm[3]; DOUBLE **inverse; puts("\nOpening Matrix.library\n"); Matrix = OpenLibrary("Matrix.library",1); printf("\nMatrix = %x\n",Matrix); if(!Matrix) { printf("\nCould not open Matrix.library!\n"); exit(0); } for (i=0 ; i<3 ; i++) { if(!(lv[i] = (LONG *)AllocLVector(4))) { printf("\nCould not allocate vector %d.\n",i); exit(0); } } printf("\nLong vectors allotted.\n"); for (i=0 ; i<3 ; i++) { if(!(dv[i] = (DOUBLE *)AllocDVector(4))) { printf("\nCould not allocate vector %d.\n",i); exit(0); } } printf("\nDouble vectors allotted.\n"); for (i=0 ; i<3 ; i++) { if(!(lm[i] = (LONG **)AllocLMatrix(4,4))) { printf("\nCould not allocate matrix %d.\n",i); exit(0); } } printf("\nLong matrices allotted.\n"); for (i=0 ; i<3 ; i++) { if(!(dm[i] = (DOUBLE **)AllocDMatrix(4,4))) { printf("\nCould not allocate matrix %d.\n",i); exit(0); } } printf("\nDouble matrices allotted.\n"); for (i=0 ; i<3 ; i++) { for (j=0 ; j<=3 ; j++) { /* assign values to vector elements */ (lv[i])[j] = (LONG)(10.0*drand48()); (dv[i])[j] = 10.0*drand48(); for (k=0 ; k<=3 ; k++) { /* assign values to matrix elements */ ((lm[i])[j])[k] = (LONG)(10.0*drand48()); ((dm[i])[j])[k] = 10.0*drand48(); } } } /* for i=0 */ printf("\nVectors and matrices assigned.\n"); AddLVectors(lv[0],lv[1],lv[2],4); printf("\nLong vectors added.\n"); DisplayLVector(lv[0],4); DisplayLVector(lv[1],4); DisplayLVector(lv[2],4); SubLVectors(lv[0],lv[1],lv[2],4); printf("\nLong vectors subtracted.\n"); DisplayLVector(lv[0],4); DisplayLVector(lv[1],4); DisplayLVector(lv[2],4); AddLMatrices(lm[0],lm[1],lm[2],4,4); printf("\nLong matrices added.\n"); DisplayLMatrix(lm[0],4,4); DisplayLMatrix(lm[1],4,4); DisplayLMatrix(lm[2],4,4); SubLMatrices(lm[0],lm[1],lm[2],4,4); printf("\nLong matrices subtracted.\n"); DisplayLMatrix(lm[0],4,4); DisplayLMatrix(lm[1],4,4); DisplayLMatrix(lm[2],4,4); MultLMatrices(lm[0],lm[1],lm[2],4,4,4); printf("\nLong matrices multiplied.\n"); DisplayLMatrix(lm[0],4,4); DisplayLMatrix(lm[1],4,4); DisplayLMatrix(lm[2],4,4); MultLVectorMatrix(lv[0],lm[0],lv[2],4,4); printf("\nLong vector and matrix multiplied.\n"); DisplayLVector(lv[0],4); DisplayLMatrix(lm[0],4,4); DisplayLVector(lv[2],4); MultLMatrixVector(lm[0],lv[0],lv[2],4,4); printf("\nLong matrix and vector multiplied.\n"); DisplayLMatrix(lm[0],4,4); DisplayLVector(lv[0],4); DisplayLVector(lv[2],4); lm[2] = TransposeLMatrix(lm[0],lm[2],4,4); printf("\nLong matrix transposed.\n"); DisplayLMatrix(lm[0],4,4); DisplayLMatrix(lm[2],4,4); AddDVectors(dv[0],dv[1],dv[2],4); printf("\nDouble vectors added.\n"); DisplayDVector(dv[0],4); DisplayDVector(dv[1],4); DisplayDVector(dv[2],4); SubDVectors(dv[0],dv[1],dv[2],4); printf("\nDouble vectors subtracted.\n"); DisplayDVector(dv[0],4); DisplayDVector(dv[1],4); DisplayDVector(dv[2],4); AddDMatrices(dm[0],dm[1],dm[2],4,4); printf("\nDouble matrices added.\n"); DisplayDMatrix(dm[0],4,4); DisplayDMatrix(dm[1],4,4); DisplayDMatrix(dm[2],4,4); SubDMatrices(dm[0],dm[1],dm[2],4,4); printf("\nDouble matrices subtracted.\n"); DisplayDMatrix(dm[0],4,4); DisplayDMatrix(dm[1],4,4); DisplayDMatrix(dm[2],4,4); MultDMatrices(dm[0],dm[1],dm[2],4,4,4); printf("\nDouble matrices multiplied.\n"); DisplayDMatrix(dm[0],4,4); DisplayDMatrix(dm[1],4,4); DisplayDMatrix(dm[2],4,4); MultDVectorMatrix(dv[0],dm[0],dv[2],4,4); printf("\nDouble vector and matrix multiplied.\n"); DisplayDVector(dv[0],4); DisplayDMatrix(dm[0],4,4); DisplayDVector(dv[2],4); MultDMatrixVector(dm[0],dv[0],dv[2],4,4); printf("\nDouble matrix and vector multiplied.\n"); DisplayDMatrix(dm[0],4,4); DisplayDVector(dv[0],4); DisplayDVector(dv[2],4); dm[2] = TransposeDMatrix(dm[0],dm[2],4,4); printf("\nDouble matrix transposed.\n"); DisplayDMatrix(dm[0],4,4); DisplayDMatrix(dm[2],4,4); inverse = InvertDMatrix(dm[0],NULL,NULL,4); if (inverse) { printf("\nDouble matrix inverted.\n"); MultDMatrices(dm[0],inverse,dm[2],4,4,4); DisplayDMatrix(dm[0],4,4); DisplayDMatrix(inverse,4,4); DisplayDMatrix(dm[2],4,4); } else { printf("\nDouble matrix inversion failed.\n"); } for (i=0 ; i<3 ; i++) { FreeLVector(lv[i],4); FreeDVector(dv[i],4); FreeLMatrix(lm[i],4,4); FreeDMatrix(dm[i],4,4); } if (inverse) FreeDMatrix(inverse,4,4); printf("\nVectors and Matrices freed.\n"); CloseLibrary(Matrix); } /* main */