NEURON
init.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 /*
29  This is a file of routines for zero-ing, and initialising
30  vectors, matrices and permutations.
31  This is to be included in the matrix.a library
32 */
33 
34 static char rcsid[] = "init.c,v 1.1 1997/12/04 17:55:25 hines Exp";
35 
36 #include <stdio.h>
37 #include "matrix.h"
38 
39 /* v_zero -- zero the vector x */
41 VEC *x;
42 {
43  if ( x == VNULL )
44  error(E_NULL,"v_zero");
45 
46  __zero__(x->ve,x->dim);
47  /* for ( i = 0; i < x->dim; i++ )
48  x->ve[i] = 0.0; */
49 
50  return x;
51 }
52 
53 
54 /* iv_zero -- zero the vector ix */
56 IVEC *ix;
57 {
58  int i;
59 
60  if ( ix == IVNULL )
61  error(E_NULL,"iv_zero");
62 
63  for ( i = 0; i < ix->dim; i++ )
64  ix->ive[i] = 0;
65 
66  return ix;
67 }
68 
69 
70 /* m_zero -- zero the matrix A */
72 MAT *A;
73 {
74  int i, A_m, A_n;
75  Real **A_me;
76 
77  if ( A == MNULL )
78  error(E_NULL,"m_zero");
79 
80  A_m = A->m; A_n = A->n; A_me = A->me;
81  for ( i = 0; i < A_m; i++ )
82  __zero__(A_me[i],A_n);
83  /* for ( j = 0; j < A_n; j++ )
84  A_me[i][j] = 0.0; */
85 
86  return A;
87 }
88 
89 /* mat_id -- set A to being closest to identity matrix as possible
90  -- i.e. A[i][j] == 1 if i == j and 0 otherwise */
92 MAT *A;
93 {
94  int i, size;
95 
96  if ( A == MNULL )
97  error(E_NULL,"m_ident");
98 
99  m_zero(A);
100  size = min(A->m,A->n);
101  for ( i = 0; i < size; i++ )
102  A->me[i][i] = 1.0;
103 
104  return A;
105 }
106 
107 /* px_ident -- set px to identity permutation */
109 PERM *px;
110 {
111  int i, px_size;
112  u_int *px_pe;
113 
114  if ( px == PNULL )
115  error(E_NULL,"px_ident");
116 
117  px_size = px->size; px_pe = px->pe;
118  for ( i = 0; i < px_size; i++ )
119  px_pe[i] = i;
120 
121  return px;
122 }
123 
124 /* Pseudo random number generator data structures */
125 /* Knuth's lagged Fibonacci-based generator: See "Seminumerical Algorithms:
126  The Art of Computer Programming" sections 3.2-3.3 */
127 
128 #ifdef ANSI_C
129 #ifndef LONG_MAX
130 #include <limits.h>
131 #endif
132 #endif
133 
134 #ifdef LONG_MAX
135 #define MODULUS LONG_MAX
136 #else
137 #define MODULUS 1000000000L /* assuming long's at least 32 bits long */
138 #endif
139 #define MZ 0L
140 
141 static long mrand_list[56];
142 static int started = FALSE;
143 static int inext = 0, inextp = 31;
144 
145 
146 /* mrand -- pseudo-random number generator */
147 #ifdef ANSI_C
148 double mrand(void)
149 #else
150 double mrand()
151 #endif
152 {
153  long lval;
154  static Real factor = 1.0/((Real)MODULUS);
155 
156  if ( ! started )
157  smrand(3127);
158 
159  inext = (inext >= 54) ? 0 : inext+1;
160  inextp = (inextp >= 54) ? 0 : inextp+1;
161 
163  if ( lval < 0L )
164  lval += MODULUS;
165  mrand_list[inext] = lval;
166 
167  return (double)lval*factor;
168 }
169 
170 /* mrandlist -- fills the array a[] with len random numbers */
171 void mrandlist(a, len)
172 Real a[];
173 int len;
174 {
175  int i;
176  long lval;
177  static Real factor = 1.0/((Real)MODULUS);
178 
179  if ( ! started )
180  smrand(3127);
181 
182  for ( i = 0; i < len; i++ )
183  {
184  inext = (inext >= 54) ? 0 : inext+1;
185  inextp = (inextp >= 54) ? 0 : inextp+1;
186 
188  if ( lval < 0L )
189  lval += MODULUS;
190  mrand_list[inext] = lval;
191 
192  a[i] = (Real)lval*factor;
193  }
194 }
195 
196 
197 /* smrand -- set seed for mrand() */
198 void smrand(seed)
199 int seed;
200 {
201  int i;
202 
203  mrand_list[0] = (123413*seed) % MODULUS;
204  for ( i = 1; i < 55; i++ )
205  mrand_list[i] = (123413*mrand_list[i-1]) % MODULUS;
206 
207  started = TRUE;
208 
209  /* run mrand() through the list sufficient times to
210  thoroughly randomise the array */
211  for ( i = 0; i < 55*55; i++ )
212  mrand();
213 }
214 #undef MODULUS
215 #undef MZ
216 #undef FAC
217 
218 /* v_rand -- initialises x to be a random vector, components
219  independently & uniformly ditributed between 0 and 1 */
221 VEC *x;
222 {
223  /* int i; */
224 
225  if ( ! x )
226  error(E_NULL,"v_rand");
227 
228  /* for ( i = 0; i < x->dim; i++ ) */
229  /* x->ve[i] = rand()/((Real)MAX_RAND); */
230  /* x->ve[i] = mrand(); */
231  mrandlist(x->ve,x->dim);
232 
233  return x;
234 }
235 
236 /* m_rand -- initialises A to be a random vector, components
237  independently & uniformly distributed between 0 and 1 */
239 MAT *A;
240 {
241  int i /* , j */;
242 
243  if ( ! A )
244  error(E_NULL,"m_rand");
245 
246  for ( i = 0; i < A->m; i++ )
247  /* for ( j = 0; j < A->n; j++ ) */
248  /* A->me[i][j] = rand()/((Real)MAX_RAND); */
249  /* A->me[i][j] = mrand(); */
250  mrandlist(A->me[i],A->n);
251 
252  return A;
253 }
254 
255 /* v_ones -- fills x with one's */
257 VEC *x;
258 {
259  int i;
260 
261  if ( ! x )
262  error(E_NULL,"v_ones");
263 
264  for ( i = 0; i < x->dim; i++ )
265  x->ve[i] = 1.0;
266 
267  return x;
268 }
269 
270 /* m_ones -- fills matrix with one's */
272 MAT *A;
273 {
274  int i, j;
275 
276  if ( ! A )
277  error(E_NULL,"m_ones");
278 
279  for ( i = 0; i < A->m; i++ )
280  for ( j = 0; j < A->n; j++ )
281  A->me[i][j] = 1.0;
282 
283  return A;
284 }
285 
286 /* v_count -- initialises x so that x->ve[i] == i */
288 VEC *x;
289 {
290  int i;
291 
292  if ( ! x )
293  error(E_NULL,"v_count");
294 
295  for ( i = 0; i < x->dim; i++ )
296  x->ve[i] = (Real)i;
297 
298  return x;
299 }
#define TRUE
Definition: err.c:57
#define FALSE
Definition: err.c:56
#define error(err_num, fn_name)
Definition: err.h:73
#define E_NULL
Definition: err.h:102
VEC * v_ones(VEC *x)
Definition: init.c:256
IVEC * iv_zero(IVEC *ix)
Definition: init.c:55
VEC * v_count(VEC *x)
Definition: init.c:287
static int inextp
Definition: init.c:143
double mrand(void)
Definition: init.c:148
MAT * m_ident(MAT *A)
Definition: init.c:91
MAT * m_rand(MAT *A)
Definition: init.c:238
static long mrand_list[56]
Definition: init.c:141
PERM * px_ident(PERM *px)
Definition: init.c:108
MAT * m_zero(MAT *A)
Definition: init.c:71
#define MODULUS
Definition: init.c:137
void smrand(int seed)
Definition: init.c:198
static int inext
Definition: init.c:143
VEC * v_zero(VEC *x)
Definition: init.c:40
static int started
Definition: init.c:142
VEC * v_rand(VEC *x)
Definition: init.c:220
MAT * m_ones(MAT *A)
Definition: init.c:271
void mrandlist(a, int len)
Definition: init.c:171
static char rcsid[]
Definition: init.c:34
void __zero__(Real *dp, int len)
Definition: machine.c:133
#define Real
Definition: machine.h:189
uint32_t u_int
Definition: machine.h:38
#define VNULL
Definition: matrix.h:631
#define PNULL
Definition: matrix.h:633
#define IVNULL
Definition: matrix.h:634
#define min(a, b)
Definition: matrix.h:157
#define MNULL
Definition: matrix.h:632
#define i
Definition: md1redef.h:12
#define A(i)
Definition: multisplit.cpp:63
size_t j
Definition: matrix.h:92
Definition: matrix.h:73
Definition: matrix.h:87
Definition: matrix.h:67