NEURON
copy.c
Go to the documentation of this file.
1 #include <../../nrnconf.h>
2 
3 /**************************************************************************
4 **
5 ** Copyright (C) 1993 David E. Steward & Zbigniew Leyk, all rights reserved.
6 **
7 ** Meschach Library
8 **
9 ** This Meschach Library is provided "as is" without any express
10 ** or implied warranty of any kind with respect to this software.
11 ** In particular the authors shall not be liable for any direct,
12 ** indirect, special, incidental or consequential damages arising
13 ** in any way from use of the software.
14 **
15 ** Everyone is granted permission to copy, modify and redistribute this
16 ** Meschach Library, provided:
17 ** 1. All copies contain this copyright notice.
18 ** 2. All modified copies shall carry a notice stating who
19 ** made the last modification and the date of such modification.
20 ** 3. No charge is made for this software or works derived from it.
21 ** This clause shall not be construed as constraining other software
22 ** distributed on the same medium as this software, nor is a
23 ** distribution fee considered a charge.
24 **
25 ***************************************************************************/
26 
27 
28 static char rcsid[] = "copy.c,v 1.1 1997/12/04 17:55:17 hines Exp";
29 #include <stdio.h>
30 #include "matrix.h"
31 
32 
33 
34 /* _m_copy -- copies matrix into new area */
35 MAT *_m_copy(in,out,i0,j0)
36 MAT *in,*out;
37 u_int i0,j0;
38 {
39  u_int i /* ,j */;
40 
41  if ( in==MNULL )
42  error(E_NULL,"_m_copy");
43  if ( in==out )
44  return (out);
45  if ( out==MNULL || out->m < in->m || out->n < in->n )
46  out = m_resize(out,in->m,in->n);
47 
48  for ( i=i0; i < in->m; i++ )
49  MEM_COPY(&(in->me[i][j0]),&(out->me[i][j0]),
50  (in->n - j0)*sizeof(Real));
51  /* for ( j=j0; j < in->n; j++ )
52  out->me[i][j] = in->me[i][j]; */
53 
54  return (out);
55 }
56 
57 /* _v_copy -- copies vector into new area */
58 VEC *_v_copy(in,out,i0)
59 VEC *in,*out;
60 u_int i0;
61 {
62  /* u_int i,j; */
63 
64  if ( in==VNULL )
65  error(E_NULL,"_v_copy");
66  if ( in==out )
67  return (out);
68  if ( out==VNULL || out->dim < in->dim )
69  out = v_resize(out,in->dim);
70 
71  MEM_COPY(&(in->ve[i0]),&(out->ve[i0]),(in->dim - i0)*sizeof(Real));
72  /* for ( i=i0; i < in->dim; i++ )
73  out->ve[i] = in->ve[i]; */
74 
75  return (out);
76 }
77 
78 /* px_copy -- copies permutation 'in' to 'out' */
79 PERM *px_copy(in,out)
80 PERM *in,*out;
81 {
82  /* int i; */
83 
84  if ( in == PNULL )
85  error(E_NULL,"px_copy");
86  if ( in == out )
87  return out;
88  if ( out == PNULL || out->size != in->size )
89  out = px_resize(out,in->size);
90 
91  MEM_COPY(in->pe,out->pe,in->size*sizeof(u_int));
92  /* for ( i = 0; i < in->size; i++ )
93  out->pe[i] = in->pe[i]; */
94 
95  return out;
96 }
97 
98 /*
99  The .._move() routines are for moving blocks of memory around
100  within Meschach data structures and for re-arranging matrices,
101  vectors etc.
102 */
103 
104 /* m_move -- copies selected pieces of a matrix
105  -- moves the m0 x n0 submatrix with top-left cor-ordinates (i0,j0)
106  to the corresponding submatrix of out with top-left co-ordinates
107  (i1,j1)
108  -- out is resized (& created) if necessary */
109 MAT *m_move(in,i0,j0,m0,n0,out,i1,j1)
110 MAT *in, *out;
111 int i0, j0, m0, n0, i1, j1;
112 {
113  int i;
114 
115  if ( ! in )
116  error(E_NULL,"m_move");
117  if ( i0 < 0 || j0 < 0 || i1 < 0 || j1 < 0 || m0 < 0 || n0 < 0 ||
118  i0+m0 > in->m || j0+n0 > in->n )
119  error(E_BOUNDS,"m_move");
120 
121  if ( ! out )
122  out = m_resize(out,i1+m0,j1+n0);
123  else if ( i1+m0 > out->m || j1+n0 > out->n )
124  out = m_resize(out,max(out->m,i1+m0),max(out->n,j1+n0));
125 
126  for ( i = 0; i < m0; i++ )
127  MEM_COPY(&(in->me[i0+i][j0]),&(out->me[i1+i][j1]),
128  n0*sizeof(Real));
129 
130  return out;
131 }
132 
133 /* v_move -- copies selected pieces of a vector
134  -- moves the length dim0 subvector with initial index i0
135  to the corresponding subvector of out with initial index i1
136  -- out is resized if necessary */
137 VEC *v_move(in,i0,dim0,out,i1)
138 VEC *in, *out;
139 int i0, dim0, i1;
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 
147  if ( (! out) || i1+dim0 > out->dim )
148  out = v_resize(out,i1+dim0);
149 
150  MEM_COPY(&(in->ve[i0]),&(out->ve[i1]),dim0*sizeof(Real));
151 
152  return out;
153 }
154 
155 /* mv_move -- copies selected piece of matrix to a vector
156  -- moves the m0 x n0 submatrix with top-left co-ordinate (i0,j0) to
157  the subvector with initial index i1 (and length m0*n0)
158  -- rows are copied contiguously
159  -- out is resized if necessary */
160 VEC *mv_move(in,i0,j0,m0,n0,out,i1)
161 MAT *in;
162 VEC *out;
163 int i0, j0, m0, n0, i1;
164 {
165  int dim1, i;
166 
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 
173  dim1 = m0*n0;
174  if ( (! out) || i1+dim1 > out->dim )
175  out = v_resize(out,i1+dim1);
176 
177  for ( i = 0; i < m0; i++ )
178  MEM_COPY(&(in->me[i0+i][j0]),&(out->ve[i1+i*n0]),n0*sizeof(Real));
179 
180  return out;
181 }
182 
183 /* vm_move -- copies selected piece of vector to a matrix
184  -- moves the subvector with initial index i0 and length m1*n1 to
185  the m1 x n1 submatrix with top-left co-ordinate (i1,j1)
186  -- copying is done by rows
187  -- out is resized if necessary */
188 MAT *vm_move(in,i0,out,i1,j1,m1,n1)
189 VEC *in;
190 MAT *out;
191 int i0, i1, j1, m1, n1;
192 {
193  int dim0, i;
194 
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 
201  if ( ! out )
202  out = m_resize(out,i1+m1,j1+n1);
203  else
204  out = m_resize(out,max(i1+m1,out->m),max(j1+n1,out->n));
205 
206  dim0 = m1*n1;
207  for ( i = 0; i < m1; i++ )
208  MEM_COPY(&(in->ve[i0+i*n1]),&(out->me[i1+i][j1]),n1*sizeof(Real));
209 
210  return out;
211 }
#define PNULL
Definition: matrix.h:633
double max(double a, double b)
Definition: geometry3d.cpp:22
VEC * mv_move(MAT *in, int i0, int j0, int m0, int n0, VEC *out, int i1)
Definition: copy.c:160
#define Real
Definition: machine.h:189
static Object ** v_resize(void *v)
Definition: ivocvect.cpp:1297
PERM * px_copy(PERM *in, PERM *out)
Definition: copy.c:79
u_int n
Definition: matrix.h:74
Real * ve
Definition: matrix.h:69
Definition: matrix.h:67
uint32_t u_int
Definition: machine.h:38
#define MNULL
Definition: matrix.h:632
PERM * px_resize(PERM *, int)
Definition: memory.c:399
u_int dim
Definition: matrix.h:68
#define E_BOUNDS
Definition: err.h:96
void MEM_COPY(char *from, char *to, int len)
Definition: extras.c:37
VEC * _v_copy(VEC *in, VEC *out, u_int i0)
Definition: copy.c:58
#define E_NULL
Definition: err.h:102
static char rcsid[]
Definition: copy.c:28
MAT * vm_move(VEC *in, int i0, MAT *out, int i1, int j1, int m1, int n1)
Definition: copy.c:188
VEC * v_move(VEC *in, int i0, int dim0, VEC *out, int i1)
Definition: copy.c:137
MAT * _m_copy(MAT *in, MAT *out, u_int i0, u_int j0)
Definition: copy.c:35
static Object ** m_resize(void *v)
Definition: matrix.cpp:190
#define i
Definition: md1redef.h:12
#define error(err_num, fn_name)
Definition: err.h:73
u_int m
Definition: matrix.h:74
#define VNULL
Definition: matrix.h:631
Definition: matrix.h:87
MAT * m_move(MAT *in, int i0, int j0, int m0, int n0, MAT *out, int i1, int j1)
Definition: copy.c:109
Real ** me
Definition: matrix.h:76
Definition: matrix.h:73