home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / c / lpc05b.zip / MATCREAT.C < prev    next >
C/C++ Source or Header  |  1992-05-22  |  4KB  |  193 lines

  1. /*
  2. *-----------------------------------------------------------------------------
  3. *    file:    matcreat.c
  4. *    desc:    matrix mathematics - object creation
  5. *    by:    ko shu pui, patrick
  6. *    date:    24 nov 91 v0.1
  7. *    revi:    14 may 92 v0.2
  8. *        21 may 92 v0.3
  9. *    ref:
  10. *       [1] Mary L.Boas, "Mathematical Methods in the Physical Sciene,"
  11. *    John Wiley & Sons, 2nd Ed., 1983. Chap 3.
  12. *
  13. *    [2] Kendall E.Atkinson, "An Introduction to Numberical Analysis,"
  14. *    John Wiley & Sons, 1978.
  15. *
  16. *-----------------------------------------------------------------------------
  17. */
  18. #include <stdio.h>
  19.  
  20. #ifdef    __TURBOC__
  21. #include <alloc.h>
  22. #else
  23. #include <malloc.h>
  24. #endif
  25.  
  26. #include "matrix.h"
  27.  
  28. MATRIX    _mat_creat( row, col )
  29. int row, col;
  30. {
  31.     MATBODY    *mat;
  32.     int     i, j;
  33.  
  34.     if ((mat = (MATBODY *)malloc( sizeof(MATHEAD) + sizeof(double *) * row)) == NULL)
  35.         return (mat_error( MAT_MALLOC ));
  36.  
  37.     for (i=0; i<row; i++)
  38.     {
  39.     if ((*((double **)(&mat->matrix) + i) = (double *)malloc(sizeof(double) * col)) == NULL)
  40.         return (mat_error( MAT_MALLOC ));
  41.     }
  42.  
  43.     mat->head.row = row;
  44.     mat->head.col = col;
  45.  
  46.     return (&(mat->matrix));
  47. }
  48.  
  49. /*
  50. *-----------------------------------------------------------------------------
  51. *    funct:    mat_creat
  52. *    desct:    create a matrix
  53. *    given:  row, col = dimension, type = which kind of matrix
  54. *    retrn:    allocated matrix (use mat_free() to free memory)
  55. *-----------------------------------------------------------------------------
  56. */
  57. MATRIX    mat_creat( row, col, type )
  58. int row, col, type;
  59. {
  60.     MATRIX    A;
  61.  
  62.     if ((A =_mat_creat( row, col )) != NULL)
  63.         {
  64.         return (mat_fill(A, type));
  65.         }
  66.     else
  67.         return (NULL);
  68. }
  69.  
  70. /*
  71. *-----------------------------------------------------------------------------
  72. *    funct:    mat_fill
  73. *    desct:    form a special matrix
  74. *    given:  A = matrix, type = which kind of matrix
  75. *    retrn:    A
  76. *-----------------------------------------------------------------------------
  77. */
  78. MATRIX mat_fill( A, type )
  79. MATRIX A;
  80. int type;
  81. {
  82.     int    i, j;
  83.  
  84.     switch (type)
  85.         {
  86.         case UNDEFINED:
  87.             break;
  88.         case ZERO_MATRIX:
  89.         case UNIT_MATRIX:
  90.             for (i=0; i<MatRow(A); i++)
  91.             for (j=0; j<MatCol(A); j++)
  92.                 {
  93.                 if (type == UNIT_MATRIX)
  94.                     {
  95.                     if (i==j) A[i][j] = 1.0;
  96.                     continue;
  97.                     }
  98.                 A[i][j] = 0.0;
  99.                 }
  100.             break;
  101.         }
  102.     return (A);
  103. }
  104.  
  105.  
  106. /*
  107. *-----------------------------------------------------------------------------
  108. *    funct:    mat_free
  109. *    desct:    free an allocated matrix
  110. *    given:  A = matrix
  111. *    retrn:    nothing
  112. *-----------------------------------------------------------------------------
  113. */
  114. int mat_free( A )
  115. MATRIX A;
  116. {
  117.     int i;
  118.  
  119.     for (i=0; i<MatRow(A); i++)
  120.         {
  121.         free( A[i] );
  122.         }
  123.     free( Mathead(A) );
  124. }
  125.  
  126. /*
  127. *-----------------------------------------------------------------------------
  128. *    funct:    mat_copy
  129. *    desct:    duplicate a matrix
  130. *    given:    A = matrice to duplicated
  131. *    retrn:    C = A
  132. *    comen:
  133. *-----------------------------------------------------------------------------
  134. */
  135. MATRIX mat_copy( A )
  136. MATRIX A;
  137. {
  138.     int    i, j;
  139.     MATRIX    C;
  140.  
  141.     if ((C = mat_creat( MatRow(A), MatCol(A), UNDEFINED )) == NULL)
  142.         return (NULL);
  143.  
  144.     for (i=0; i<MatRow(A); i++)
  145.     for (j=0; j<MatCol(A); j++)
  146.         {
  147.         C[i][j] = A[i][j];
  148.         }
  149.     return (C);
  150. }
  151.  
  152.  
  153. MATRIX mat_colcopy1( A, B, cola, colb )
  154. MATRIX A, B;
  155. int cola, colb;
  156. {
  157.     int    i, n;
  158.  
  159.     n = MatRow(A);
  160.     for (i=0; i<n; i++)
  161.         {
  162.         A[i][cola] = B[i][colb];
  163.         }
  164.     return (A);
  165. }
  166.  
  167. int fgetmat( A, fp )
  168. MATRIX A;
  169. FILE *fp;
  170. {
  171.     int     i, j, k=0;
  172.  
  173.     for (i=0; i<MatRow(A); i++)
  174.     for (j=0; j<MatCol(A); j++)
  175.         {
  176. /*
  177. *    to avoid a bug in TC
  178. */
  179. #ifdef    __TURBOC__
  180.         {
  181.         double    temp;
  182.         k += fscanf( fp, "%lf", &temp );
  183.         A[i][j] = temp;
  184.         }
  185. #else
  186.         k += fscanf( fp, "%lf", &A[i][j] );
  187. #endif
  188.  
  189.         }
  190.  
  191.     return (k);
  192. }
  193.