home *** CD-ROM | disk | FTP | other *** search
/ vis-ftp.cs.umass.edu / vis-ftp.cs.umass.edu.tar / vis-ftp.cs.umass.edu / pub / CMU / cmu_files / cal / matrix.c < prev    next >
C/C++ Source or Header  |  1990-12-11  |  5KB  |  225 lines

  1. /* matrix.c -- library routines for constructing dynamic matrices
  2.  * with arbitrary bounds using Iliffe vectors
  3.  ****************************************************************
  4.  * HISTORY
  5.  * 25-Nov-80  David Smith (drs) at Carnegie-Mellon University
  6.  * Changed virtual base address name to "el" for all data
  7.  * types (Previously vali, vald, ...)  This was possible due to the
  8.  * compiler enhancement which keeps different structure declarations
  9.  * separate.
  10.  *
  11.  * 30-Oct-80  David Smith (drs) at Carnegie-Mellon University
  12.  *    Rewritten for record-style matrices
  13.  *
  14.  * 28-Oct-80  David Smith (drs) at Carnegie-Mellon University
  15.  *    Written.
  16.  *
  17.  */
  18.  
  19. #include "matrix.h"
  20. char *malloc();
  21.  
  22. cmat newcmat(rs, re, cs, ce, error)
  23.     int rs, re, cs, ce, *error;
  24.     {
  25.     register char *p, **b;
  26.     register int r, rows, cols;
  27.     cmat matrix;
  28.  
  29.     rows = re-rs+1;
  30.     cols = ce-cs+1;
  31.     if (rows<=0 || cols<=0) {*error=1; return matrix;}
  32.     matrix.lb1 = rs;
  33.     matrix.ub1 = re;
  34.     matrix.lb2 = cs;
  35.     matrix.ub2 = ce;
  36.     b = (char **)
  37.         malloc(rows*sizeof(char *) + rows*cols*sizeof(char));
  38.     if (b==0) {*error=1; return(matrix);}
  39.     matrix.mat_sto = (char *) b;
  40.     *error=0;
  41.     p = ((char *) &b[rows]) - cs;
  42.     matrix.el = b -= rs;
  43.     for (r=rs; r<=re; r++)
  44.         {
  45.         b[r] = p;
  46.         p += cols;
  47.         }
  48.     return matrix;
  49.     }
  50.  
  51. ucmat newucmat(rs, re, cs, ce, error)
  52.     int rs, re, cs, ce, *error;
  53.     {
  54.     register unsigned char *p, **b;
  55.     register int r, rows, cols;
  56.     ucmat matrix;
  57.  
  58.     rows = re-rs+1;
  59.     cols = ce-cs+1;
  60.     if (rows<=0 || cols<=0) {*error=1; return matrix;}
  61.     matrix.lb1 = rs;
  62.     matrix.ub1 = re;
  63.     matrix.lb2 = cs;
  64.     matrix.ub2 = ce;
  65.     b = (unsigned char **)
  66.         malloc(rows*sizeof(unsigned char *)
  67.                 +rows*cols*sizeof(unsigned char));
  68.     if (b==0) {*error=1; return(matrix);}
  69.     matrix.mat_sto = (char *) b;
  70.     *error=0;
  71.     p = ((unsigned char *) &b[rows]) - cs;
  72.     matrix.el = b -= rs;
  73.     for (r=rs; r<=re; r++)
  74.         {
  75.         b[r] = p;
  76.         p += cols;
  77.         }
  78.     return matrix;
  79.     }
  80.  
  81. smat newsmat(rs, re, cs, ce, error)
  82.     int rs, re, cs, ce, *error;
  83.     {
  84.     register short *p, **b;
  85.     register int r, rows, cols;
  86.     smat matrix;
  87.  
  88.     rows = re-rs+1;
  89.     cols = ce-cs+1;
  90.     if (rows<=0 || cols<=0) {*error=1; return matrix;}
  91.     matrix.lb1 = rs;
  92.     matrix.ub1 = re;
  93.     matrix.lb2 = cs;
  94.     matrix.ub2 = ce;
  95.     b = (short **)
  96.         malloc(rows*sizeof(short *) + rows*cols*sizeof(short));
  97.     if (b==0) {*error=1; return(matrix);}
  98.     matrix.mat_sto = (char *) b;
  99.     *error=0;
  100.     p = ((short *) &b[rows]) - cs;
  101.     matrix.el = b -= rs;
  102.     for (r=rs; r<=re; r++)
  103.         {
  104.         b[r] = p;
  105.         p += cols;
  106.         }
  107.     return matrix;
  108.     }
  109.  
  110. imat newimat(rs, re, cs, ce, error)
  111.     int rs, re, cs, ce, *error;
  112.     {
  113.     register int *p, **b;
  114.     register int r, rows, cols;
  115.     imat matrix;
  116.  
  117.     rows = re-rs+1;
  118.     cols = ce-cs+1;
  119.     if (rows<=0 || cols<=0) {*error=1; return matrix;}
  120.     matrix.lb1 = rs;
  121.     matrix.ub1 = re;
  122.     matrix.lb2 = cs;
  123.     matrix.ub2 = ce;
  124.     b = (int **)
  125.         malloc(rows*sizeof(int *) + rows*cols*sizeof(int));
  126.     if (b==0) {*error=1; return(matrix);}
  127.     matrix.mat_sto = (char *) b;
  128.     *error=0;
  129.     p = ((int *) &b[rows]) - cs;
  130.     matrix.el = b -= rs;
  131.     for (r=rs; r<=re; r++)
  132.         {
  133.         b[r] = p;
  134.         p += cols;
  135.         }
  136.     return matrix;
  137.     }
  138.  
  139. lmat newlmat(rs, re, cs, ce, error)
  140.     int rs, re, cs, ce, *error;
  141.     {
  142.     register long *p, **b;
  143.     register int r, rows, cols;
  144.     lmat matrix;
  145.  
  146.     rows = re-rs+1;
  147.     cols = ce-cs+1;
  148.     if (rows<=0 || cols<=0) {*error=1; return matrix;}
  149.     matrix.lb1 = rs;
  150.     matrix.ub1 = re;
  151.     matrix.lb2 = cs;
  152.     matrix.ub2 = ce;
  153.     b = (long **)
  154.         malloc(rows*sizeof(long *) + rows*cols*sizeof(long));
  155.     if (b==0) {*error=1; return(matrix);}
  156.     matrix.mat_sto = (char *) b;
  157.     *error=0;
  158.     p = ((long *) &b[rows]) - cs;
  159.     matrix.el = b -= rs;
  160.     for (r=rs; r<=re; r++)
  161.         {
  162.         b[r] = p;
  163.         p += cols;
  164.         }
  165.     return matrix;
  166.     }
  167.  
  168. fmat newfmat(rs, re, cs, ce, error)
  169.     int rs, re, cs, ce, *error;
  170.     {
  171.     register float *p, **b;
  172.     register int r, rows, cols;
  173.     fmat matrix;
  174.  
  175.     rows = re-rs+1;
  176.     cols = ce-cs+1;
  177.     if (rows<=0 || cols<=0) {*error=1; return matrix;}
  178.     matrix.lb1 = rs;
  179.     matrix.ub1 = re;
  180.     matrix.lb2 = cs;
  181.     matrix.ub2 = ce;
  182.     b = (float **)
  183.         malloc(rows*sizeof(float *) + rows*cols*sizeof(float));
  184.     if (b==0) {*error=1; return(matrix);}
  185.     matrix.mat_sto = (char *) b;
  186.     *error=0;
  187.     p = ((float *) &b[rows]) - cs;
  188.     matrix.el = b -= rs;
  189.     for (r=rs; r<=re; r++)
  190.         {
  191.         b[r] = p;
  192.         p += cols;
  193.         }
  194.     return matrix;
  195.     }
  196.  
  197. dmat newdmat(rs, re, cs, ce, error)
  198.     int rs, re, cs, ce, *error;
  199.     {
  200.     register double *p, **b;
  201.     register int r, rows, cols;
  202.     dmat matrix;
  203.  
  204.     rows = re-rs+1;
  205.     cols = ce-cs+1;
  206.     if (rows<=0 || cols<=0) {*error=1; return matrix;}
  207.     matrix.lb1 = rs;
  208.     matrix.ub1 = re;
  209.     matrix.lb2 = cs;
  210.     matrix.ub2 = ce;
  211.     b = (double **)
  212.         malloc(rows*sizeof(double *) + rows*cols*sizeof(double));
  213.     if (b==0) {*error=1; return(matrix);}
  214.     matrix.mat_sto = (char *) b;
  215.     *error=0;
  216.     p = ((double *) &b[rows]) - cs;
  217.     matrix.el = b -= rs;
  218.     for (r=rs; r<=re; r++)
  219.         {
  220.         b[r] = p;
  221.         p += cols;
  222.         }
  223.     return matrix;
  224.     }
  225.