submat.c 5.1 KB

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