zcopy.c 5.7 KB

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