home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / mesch12a.zip / submat.c < prev    next >
C/C++ Source or Header  |  1994-01-13  |  5KB  |  179 lines

  1.  
  2. /**************************************************************************
  3. **
  4. ** Copyright (C) 1993 David E. Steward & Zbigniew Leyk, all rights reserved.
  5. **
  6. **                 Meschach Library
  7. ** 
  8. ** This Meschach Library is provided "as is" without any express 
  9. ** or implied warranty of any kind with respect to this software. 
  10. ** In particular the authors shall not be liable for any direct, 
  11. ** indirect, special, incidental or consequential damages arising 
  12. ** in any way from use of the software.
  13. ** 
  14. ** Everyone is granted permission to copy, modify and redistribute this
  15. ** Meschach Library, provided:
  16. **  1.  All copies contain this copyright notice.
  17. **  2.  All modified copies shall carry a notice stating who
  18. **      made the last modification and the date of such modification.
  19. **  3.  No charge is made for this software or works derived from it.  
  20. **      This clause shall not be construed as constraining other software
  21. **      distributed on the same medium as this software, nor is a
  22. **      distribution fee considered a charge.
  23. **
  24. ***************************************************************************/
  25.  
  26.  
  27. /* 1.2 submat.c 11/25/87 */
  28.  
  29. #include    <stdio.h>
  30. #include    "matrix.h"
  31.  
  32. static    char    rcsid[] = "$Id: submat.c,v 1.2 1994/01/13 05:28:12 des Exp $";
  33.  
  34.  
  35. /* get_col -- gets a specified column of a matrix and retruns it as a vector */
  36. VEC    *get_col(mat,col,vec)
  37. u_int    col;
  38. MAT    *mat;
  39. VEC    *vec;
  40. {
  41.    u_int    i;
  42.    
  43.    if ( mat==(MAT *)NULL )
  44.      error(E_NULL,"get_col");
  45.    if ( col >= mat->n )
  46.      error(E_RANGE,"get_col");
  47.    if ( vec==(VEC *)NULL || vec->dim<mat->m )
  48.      vec = v_resize(vec,mat->m);
  49.    
  50.    for ( i=0; i<mat->m; i++ )
  51.      vec->ve[i] = mat->me[i][col];
  52.    
  53.    return (vec);
  54. }
  55.  
  56. /* get_row -- gets a specified row of a matrix and retruns it as a vector */
  57. VEC    *get_row(mat,row,vec)
  58. u_int    row;
  59. MAT    *mat;
  60. VEC    *vec;
  61. {
  62.    u_int    i;
  63.    
  64.    if ( mat==(MAT *)NULL )
  65.      error(E_NULL,"get_row");
  66.    if ( row >= mat->m )
  67.      error(E_RANGE,"get_row");
  68.    if ( vec==(VEC *)NULL || vec->dim<mat->n )
  69.      vec = v_resize(vec,mat->n);
  70.    
  71.    for ( i=0; i<mat->n; i++ )
  72.      vec->ve[i] = mat->me[row][i];
  73.    
  74.    return (vec);
  75. }
  76.  
  77. /* _set_col -- sets column of matrix to values given in vec (in situ) */
  78. MAT    *_set_col(mat,col,vec,i0)
  79. MAT    *mat;
  80. VEC    *vec;
  81. u_int    col,i0;
  82. {
  83.    u_int    i,lim;
  84.    
  85.    if ( mat==(MAT *)NULL || vec==(VEC *)NULL )
  86.      error(E_NULL,"_set_col");
  87.    if ( col >= mat->n )
  88.      error(E_RANGE,"_set_col");
  89.    lim = min(mat->m,vec->dim);
  90.    for ( i=i0; i<lim; i++ )
  91.      mat->me[i][col] = vec->ve[i];
  92.    
  93.    return (mat);
  94. }
  95.  
  96. /* _set_row -- sets row of matrix to values given in vec (in situ) */
  97. MAT    *_set_row(mat,row,vec,j0)
  98. MAT    *mat;
  99. VEC    *vec;
  100. u_int    row,j0;
  101. {
  102.    u_int    j,lim;
  103.    
  104.    if ( mat==(MAT *)NULL || vec==(VEC *)NULL )
  105.      error(E_NULL,"_set_row");
  106.    if ( row >= mat->m )
  107.      error(E_RANGE,"_set_row");
  108.    lim = min(mat->n,vec->dim);
  109.    for ( j=j0; j<lim; j++ )
  110.      mat->me[row][j] = vec->ve[j];
  111.    
  112.    return (mat);
  113. }
  114.  
  115. /* sub_mat -- returns sub-matrix of old which is formed by the rectangle
  116.    from (row1,col1) to (row2,col2)
  117.    -- Note: storage is shared so that altering the "new"
  118.    matrix will alter the "old" matrix */
  119. MAT    *sub_mat(old,row1,col1,row2,col2,new)
  120. MAT    *old,*new;
  121. u_int    row1,col1,row2,col2;
  122. {
  123.    u_int    i;
  124.    
  125.    if ( old==(MAT *)NULL )
  126.      error(E_NULL,"sub_mat");
  127.    if ( row1 > row2 || col1 > col2 || row2 >= old->m || col2 >= old->n )
  128.      error(E_RANGE,"sub_mat");
  129.    if ( new==(MAT *)NULL || new->m < row2-row1+1 )
  130.    {
  131.       new = NEW(MAT);
  132.       new->me = NEW_A(row2-row1+1,Real *);
  133.       if ( new==(MAT *)NULL || new->me==(Real **)NULL )
  134.     error(E_MEM,"sub_mat");
  135.       else if (mem_info_is_on()) {
  136.      mem_bytes(TYPE_MAT,0,sizeof(MAT)+
  137.               (row2-row1+1)*sizeof(Real *));
  138.       }
  139.       
  140.    }
  141.    new->m = row2-row1+1;
  142.    
  143.    new->n = col2-col1+1;
  144.    
  145.    new->base = (Real *)NULL;
  146.    
  147.    for ( i=0; i < new->m; i++ )
  148.      new->me[i] = (old->me[i+row1]) + col1;
  149.    
  150.    return (new);
  151. }
  152.  
  153.  
  154. /* sub_vec -- returns sub-vector which is formed by the elements i1 to i2
  155.    -- as for sub_mat, storage is shared */
  156. VEC    *sub_vec(old,i1,i2,new)
  157. VEC    *old, *new;
  158. int    i1, i2;
  159. {
  160.    if ( old == (VEC *)NULL )
  161.      error(E_NULL,"sub_vec");
  162.    if ( i1 > i2 || old->dim < i2 )
  163.      error(E_RANGE,"sub_vec");
  164.    
  165.    if ( new == (VEC *)NULL )
  166.      new = NEW(VEC);
  167.    if ( new == (VEC *)NULL )
  168.      error(E_MEM,"sub_vec");
  169.    else if (mem_info_is_on()) {
  170.       mem_bytes(TYPE_VEC,0,sizeof(VEC));
  171.    }
  172.    
  173.    
  174.    new->dim = i2 - i1 + 1;
  175.    new->ve = &(old->ve[i1]);
  176.    
  177.    return new;
  178. }
  179.