1 | /************************************************************************* |
---|
2 | * * |
---|
3 | * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith. * |
---|
4 | * All rights reserved. Email: russ@q12.org Web: www.q12.org * |
---|
5 | * * |
---|
6 | * This library is free software; you can redistribute it and/or * |
---|
7 | * modify it under the terms of EITHER: * |
---|
8 | * (1) The GNU Lesser General Public License as published by the Free * |
---|
9 | * Software Foundation; either version 2.1 of the License, or (at * |
---|
10 | * your option) any later version. The text of the GNU Lesser * |
---|
11 | * General Public License is included with this library in the * |
---|
12 | * file LICENSE.TXT. * |
---|
13 | * (2) The BSD-style license that is included with this library in * |
---|
14 | * the file LICENSE-BSD.TXT. * |
---|
15 | * * |
---|
16 | * This library is distributed in the hope that it will be useful, * |
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files * |
---|
19 | * LICENSE.TXT and LICENSE-BSD.TXT for more details. * |
---|
20 | * * |
---|
21 | *************************************************************************/ |
---|
22 | |
---|
23 | #include <ode/config.h> |
---|
24 | #include <ode/misc.h> |
---|
25 | #include <ode/matrix.h> |
---|
26 | |
---|
27 | //**************************************************************************** |
---|
28 | // random numbers |
---|
29 | |
---|
30 | static unsigned long seed = 0; |
---|
31 | |
---|
32 | unsigned long dRand() |
---|
33 | { |
---|
34 | seed = (1664525L*seed + 1013904223L) & 0xffffffff; |
---|
35 | return seed; |
---|
36 | } |
---|
37 | |
---|
38 | |
---|
39 | unsigned long dRandGetSeed() |
---|
40 | { |
---|
41 | return seed; |
---|
42 | } |
---|
43 | |
---|
44 | |
---|
45 | void dRandSetSeed (unsigned long s) |
---|
46 | { |
---|
47 | seed = s; |
---|
48 | } |
---|
49 | |
---|
50 | |
---|
51 | int dTestRand() |
---|
52 | { |
---|
53 | unsigned long oldseed = seed; |
---|
54 | int ret = 1; |
---|
55 | seed = 0; |
---|
56 | if (dRand() != 0x3c6ef35f || dRand() != 0x47502932 || |
---|
57 | dRand() != 0xd1ccf6e9 || dRand() != 0xaaf95334 || |
---|
58 | dRand() != 0x6252e503) ret = 0; |
---|
59 | seed = oldseed; |
---|
60 | return ret; |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | // adam's all-int straightforward(?) dRandInt (0..n-1) |
---|
65 | int dRandInt (int n) |
---|
66 | { |
---|
67 | // seems good; xor-fold and modulus |
---|
68 | const unsigned long un = n; |
---|
69 | unsigned long r = dRand(); |
---|
70 | |
---|
71 | // note: probably more aggressive than it needs to be -- might be |
---|
72 | // able to get away without one or two of the innermost branches. |
---|
73 | if (un <= 0x00010000UL) { |
---|
74 | r ^= (r >> 16); |
---|
75 | if (un <= 0x00000100UL) { |
---|
76 | r ^= (r >> 8); |
---|
77 | if (un <= 0x00000010UL) { |
---|
78 | r ^= (r >> 4); |
---|
79 | if (un <= 0x00000004UL) { |
---|
80 | r ^= (r >> 2); |
---|
81 | if (un <= 0x00000002UL) { |
---|
82 | r ^= (r >> 1); |
---|
83 | } |
---|
84 | } |
---|
85 | } |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | return (int) (r % un); |
---|
90 | } |
---|
91 | |
---|
92 | |
---|
93 | dReal dRandReal() |
---|
94 | { |
---|
95 | return ((dReal) dRand()) / ((dReal) 0xffffffff); |
---|
96 | } |
---|
97 | |
---|
98 | //**************************************************************************** |
---|
99 | // matrix utility stuff |
---|
100 | |
---|
101 | void dPrintMatrix (const dReal *A, int n, int m, char *fmt, FILE *f) |
---|
102 | { |
---|
103 | int i,j; |
---|
104 | int skip = dPAD(m); |
---|
105 | for (i=0; i<n; i++) { |
---|
106 | for (j=0; j<m; j++) fprintf (f,fmt,A[i*skip+j]); |
---|
107 | fprintf (f,"\n"); |
---|
108 | } |
---|
109 | } |
---|
110 | |
---|
111 | |
---|
112 | void dMakeRandomVector (dReal *A, int n, dReal range) |
---|
113 | { |
---|
114 | int i; |
---|
115 | for (i=0; i<n; i++) A[i] = (dRandReal()*REAL(2.0)-REAL(1.0))*range; |
---|
116 | } |
---|
117 | |
---|
118 | |
---|
119 | void dMakeRandomMatrix (dReal *A, int n, int m, dReal range) |
---|
120 | { |
---|
121 | int i,j; |
---|
122 | int skip = dPAD(m); |
---|
123 | dSetZero (A,n*skip); |
---|
124 | for (i=0; i<n; i++) { |
---|
125 | for (j=0; j<m; j++) A[i*skip+j] = (dRandReal()*REAL(2.0)-REAL(1.0))*range; |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | |
---|
130 | void dClearUpperTriangle (dReal *A, int n) |
---|
131 | { |
---|
132 | int i,j; |
---|
133 | int skip = dPAD(n); |
---|
134 | for (i=0; i<n; i++) { |
---|
135 | for (j=i+1; j<n; j++) A[i*skip+j] = 0; |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | |
---|
140 | dReal dMaxDifference (const dReal *A, const dReal *B, int n, int m) |
---|
141 | { |
---|
142 | int i,j; |
---|
143 | int skip = dPAD(m); |
---|
144 | dReal diff,max; |
---|
145 | max = 0; |
---|
146 | for (i=0; i<n; i++) { |
---|
147 | for (j=0; j<m; j++) { |
---|
148 | diff = dFabs(A[i*skip+j] - B[i*skip+j]); |
---|
149 | if (diff > max) max = diff; |
---|
150 | } |
---|
151 | } |
---|
152 | return max; |
---|
153 | } |
---|
154 | |
---|
155 | |
---|
156 | dReal dMaxDifferenceLowerTriangle (const dReal *A, const dReal *B, int n) |
---|
157 | { |
---|
158 | int i,j; |
---|
159 | int skip = dPAD(n); |
---|
160 | dReal diff,max; |
---|
161 | max = 0; |
---|
162 | for (i=0; i<n; i++) { |
---|
163 | for (j=0; j<=i; j++) { |
---|
164 | diff = dFabs(A[i*skip+j] - B[i*skip+j]); |
---|
165 | if (diff > max) max = diff; |
---|
166 | } |
---|
167 | } |
---|
168 | return max; |
---|
169 | } |
---|