init.c 6.3 KB

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