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