copy.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. static char rcsid[] = "$Id: copy.c,v 1.2 1994/01/13 05:37:14 des Exp $";
  25. #include <stdio.h>
  26. #include "matrix.h"
  27. /* _m_copy -- copies matrix into new area
  28. -- out(i0:m,j0:n) <- in(i0:m,j0:n) */
  29. #ifndef ANSI_C
  30. MAT *_m_copy(in,out,i0,j0)
  31. MAT *in,*out;
  32. unsigned int i0,j0;
  33. #else
  34. MAT *_m_copy(const MAT *in, MAT *out, unsigned int i0, unsigned int j0)
  35. #endif
  36. {
  37. unsigned int i /* ,j */;
  38. if ( in==MNULL )
  39. error(E_NULL,"_m_copy");
  40. if ( in==out )
  41. return (out);
  42. if ( out==MNULL || out->m < in->m || out->n < in->n )
  43. out = m_resize(out,in->m,in->n);
  44. for ( i=i0; i < in->m; i++ )
  45. MEM_COPY(&(in->me[i][j0]),&(out->me[i][j0]),
  46. (in->n - j0)*sizeof(Real));
  47. /* for ( j=j0; j < in->n; j++ )
  48. out->me[i][j] = in->me[i][j]; */
  49. return (out);
  50. }
  51. /* _v_copy -- copies vector into new area
  52. -- out(i0:dim) <- in(i0:dim) */
  53. #ifndef ANSI_C
  54. VEC *_v_copy(in,out,i0)
  55. VEC *in,*out;
  56. unsigned int i0;
  57. #else
  58. VEC *_v_copy(const VEC *in, VEC *out, unsigned int i0)
  59. #endif
  60. {
  61. /* unsigned int i,j; */
  62. if ( in==VNULL )
  63. error(E_NULL,"_v_copy");
  64. if ( in==out )
  65. return (out);
  66. if ( out==VNULL || out->dim < in->dim )
  67. out = v_resize(out,in->dim);
  68. MEM_COPY(&(in->ve[i0]),&(out->ve[i0]),(in->dim - i0)*sizeof(Real));
  69. /* for ( i=i0; i < in->dim; i++ )
  70. out->ve[i] = in->ve[i]; */
  71. return (out);
  72. }
  73. /* px_copy -- copies permutation 'in' to 'out'
  74. -- out is resized to in->size */
  75. #ifndef ANSI_C
  76. PERM *px_copy(in,out)
  77. PERM *in,*out;
  78. #else
  79. PERM *px_copy(const PERM *in, PERM *out)
  80. #endif
  81. {
  82. /* int i; */
  83. if ( in == PNULL )
  84. error(E_NULL,"px_copy");
  85. if ( in == out )
  86. return out;
  87. if ( out == PNULL || out->size != in->size )
  88. out = px_resize(out,in->size);
  89. MEM_COPY(in->pe,out->pe,in->size*sizeof(unsigned int));
  90. /* for ( i = 0; i < in->size; i++ )
  91. out->pe[i] = in->pe[i]; */
  92. return out;
  93. }
  94. /*
  95. The .._move() routines are for moving blocks of memory around
  96. within Meschach data structures and for re-arranging matrices,
  97. vectors etc.
  98. */
  99. /* m_move -- copies selected pieces of a matrix
  100. -- moves the m0 x n0 submatrix with top-left cor-ordinates (i0,j0)
  101. to the corresponding submatrix of out with top-left co-ordinates
  102. (i1,j1)
  103. -- out is resized (& created) if necessary */
  104. #ifndef ANSI_C
  105. MAT *m_move(in,i0,j0,m0,n0,out,i1,j1)
  106. MAT *in, *out;
  107. int i0, j0, m0, n0, i1, j1;
  108. #else
  109. MAT *m_move(const MAT *in, int i0,int j0, int m0,int n0,
  110. MAT *out, int i1, int j1)
  111. #endif
  112. {
  113. int i;
  114. if ( ! in )
  115. error(E_NULL,"m_move");
  116. if ( i0 < 0 || j0 < 0 || i1 < 0 || j1 < 0 || m0 < 0 || n0 < 0 ||
  117. i0+m0 > in->m || j0+n0 > in->n )
  118. error(E_BOUNDS,"m_move");
  119. if ( ! out )
  120. out = m_resize(out,i1+m0,j1+n0);
  121. else if ( i1+m0 > out->m || j1+n0 > out->n )
  122. out = m_resize(out,max(out->m,i1+m0),max(out->n,j1+n0));
  123. for ( i = 0; i < m0; i++ )
  124. MEM_COPY(&(in->me[i0+i][j0]),&(out->me[i1+i][j1]),
  125. n0*sizeof(Real));
  126. return out;
  127. }
  128. /* v_move -- copies selected pieces of a vector
  129. -- moves the length dim0 subvector with initial index i0
  130. to the corresponding subvector of out with initial index i1
  131. -- out is resized if necessary */
  132. #ifndef ANSI_C
  133. VEC *v_move(in,i0,dim0,out,i1)
  134. VEC *in, *out;
  135. int i0, dim0, i1;
  136. #else
  137. VEC *v_move(const VEC *in, int i0, int dim0,
  138. VEC *out, int i1)
  139. #endif
  140. {
  141. if ( ! in )
  142. error(E_NULL,"v_move");
  143. if ( i0 < 0 || dim0 < 0 || i1 < 0 ||
  144. i0+dim0 > in->dim )
  145. error(E_BOUNDS,"v_move");
  146. if ( (! out) || i1+dim0 > out->dim )
  147. out = v_resize(out,i1+dim0);
  148. MEM_COPY(&(in->ve[i0]),&(out->ve[i1]),dim0*sizeof(Real));
  149. return out;
  150. }
  151. /* mv_move -- copies selected piece of matrix to a vector
  152. -- moves the m0 x n0 submatrix with top-left co-ordinate (i0,j0) to
  153. the subvector with initial index i1 (and length m0*n0)
  154. -- rows are copied contiguously
  155. -- out is resized if necessary */
  156. #ifndef ANSI_C
  157. VEC *mv_move(in,i0,j0,m0,n0,out,i1)
  158. MAT *in;
  159. VEC *out;
  160. int i0, j0, m0, n0, i1;
  161. #else
  162. VEC *mv_move(const MAT *in, int i0,int j0, int m0, int n0,
  163. VEC *out, int i1)
  164. #endif
  165. {
  166. int dim1, i;
  167. if ( ! in )
  168. error(E_NULL,"mv_move");
  169. if ( i0 < 0 || j0 < 0 || m0 < 0 || n0 < 0 || i1 < 0 ||
  170. i0+m0 > in->m || j0+n0 > in->n )
  171. error(E_BOUNDS,"mv_move");
  172. dim1 = m0*n0;
  173. if ( (! out) || i1+dim1 > out->dim )
  174. out = v_resize(out,i1+dim1);
  175. for ( i = 0; i < m0; i++ )
  176. MEM_COPY(&(in->me[i0+i][j0]),&(out->ve[i1+i*n0]),n0*sizeof(Real));
  177. return out;
  178. }
  179. /* vm_move -- copies selected piece of vector to a matrix
  180. -- moves the subvector with initial index i0 and length m1*n1 to
  181. the m1 x n1 submatrix with top-left co-ordinate (i1,j1)
  182. -- copying is done by rows
  183. -- out is resized if necessary */
  184. #ifndef ANSI_C
  185. MAT *vm_move(in,i0,out,i1,j1,m1,n1)
  186. VEC *in;
  187. MAT *out;
  188. int i0, i1, j1, m1, n1;
  189. #else
  190. MAT *vm_move(const VEC *in, int i0,
  191. MAT *out, int i1, int j1, int m1, int n1)
  192. #endif
  193. {
  194. int dim0, i;
  195. if ( ! in )
  196. error(E_NULL,"vm_move");
  197. if ( i0 < 0 || i1 < 0 || j1 < 0 || m1 < 0 || n1 < 0 ||
  198. i0+m1*n1 > in->dim )
  199. error(E_BOUNDS,"vm_move");
  200. if ( ! out )
  201. out = m_resize(out,i1+m1,j1+n1);
  202. else
  203. out = m_resize(out,max(i1+m1,out->m),max(j1+n1,out->n));
  204. dim0 = m1*n1;
  205. for ( i = 0; i < m1; i++ )
  206. MEM_COPY(&(in->ve[i0+i*n1]),&(out->me[i1+i][j1]),n1*sizeof(Real));
  207. return out;
  208. }