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