SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
Misc.java
1/*
2 * Class: Misc
3 * Description: Miscellaneous functions that are hard to classify.
4 * Environment: Java
5 * Software: SSJ
6 * Copyright (C) 2001 Pierre L'Ecuyer and Universite de Montreal
7 * Organization: DIRO, Universite de Montreal
8 * @author
9 * @since
10 *
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 *
24 */
25package umontreal.ssj.util;
26
33public class Misc {
34 private Misc() {
35 }
36
47 public static double quickSelect(double[] A, int n, int k) {
48 double[] U = new double[n];
49 double[] V = new double[n];
50 double p = A[k - 1];
51 int u = 0;
52 int v = 0;
53 int indV = 0;
54
55 for (int i = 0; i < n; i++) {
56 if (A[i] <= p) {
57 v++;
58 if (A[i] != p) {
59 U[u++] = A[i];
60 }
61 } else
62 V[indV++] = A[i];
63 }
64
65 if (k <= u)
66 return quickSelect(U, u, k);
67 else if (k > v)
68 return quickSelect(V, indV, k - v);
69 else
70 return p;
71 }
72
83 public static int quickSelect(int[] A, int n, int k) {
84 int[] U = new int[n];
85 int[] V = new int[n];
86 int p = A[k - 1];
87 int u = 0;
88 int v = 0;
89 int indV = 0;
90
91 for (int i = 0; i < n; i++) {
92 if (A[i] <= p) {
93 v++;
94 if (A[i] != p) {
95 U[u++] = A[i];
96 }
97 } else
98 V[indV++] = A[i];
99 }
100
101 if (k <= u)
102 return quickSelect(U, u, k);
103 else if (k > v)
104 return quickSelect(V, indV, k - v);
105 else
106 return p;
107 }
108
116 public static double getMedian(double[] A, int n) {
117 int k = (n + 1) / 2; // median index
118 double med = quickSelect(A, n, k);
119 double y;
120 if ((n & 1) == 0) {
121 y = quickSelect(A, n, k + 1);
122 med = (med + y) / 2.0;
123 }
124 return med;
125 }
126
134 public static double getMedian(int[] A, int n) {
135 int k = (n + 1) / 2; // median index
136 double med = quickSelect(A, n, k);
137 double y;
138 if ((n & 1) == 0) {
139 y = quickSelect(A, n, k + 1);
140 med = (med + y) / 2.0;
141 }
142 return med;
143 }
144
173 public static int getTimeInterval(double[] times, int start, int end, double t) {
174 if (start < 0)
175 throw new IllegalArgumentException("The starting index must not be negative");
176 int n = end - start;
177 if (n < 0)
178 throw new IllegalArgumentException("The ending index must be greater than or equal to the starting index");
179 if (t < times[start])
180 return -1;
181 if (t >= times[end])
182 return n;
183
184 int start0 = start;
185 // Perform binary search to find the interval index
186 int mid = (start + end) / 2;
187 // Test if t is inside the interval mid.
188 // The interval mid starts at times[mid],
189 // and the interval mid+1 starts at times[mid + 1].
190 while (t < times[mid] || t >= times[mid + 1]) {
191 if (start == end)
192 // Should not happen, safety check to avoid infinite loops.
193 throw new IllegalStateException();
194 if (t < times[mid])
195 // time corresponds to an interval before mid.
196 end = mid - 1;
197 else
198 // time corresponds to an interval after mid.
199 start = mid + 1;
200 mid = (start + end) / 2;
201 }
202 return mid - start0;
203 }
204
221 public static void interpol(int n, double[] X, double[] Y, double[] C) {
222 int j;
223 // Compute divided differences for the Newton interpolating polynomial
224 for (j = 0; j <= n; ++j)
225 C[j] = Y[j];
226 for (int i = 1; i <= n; ++i)
227 for (j = n; j >= i; --j) {
228 if (X[j] == X[j - i])
229 C[j] = 0;
230 else
231 C[j] = (C[j] - C[j - 1]) / (X[j] - X[j - i]);
232 }
233 }
234
248 public static double evalPoly(int n, double[] X, double[] C, double z) {
249 double v = C[n];
250 for (int j = n - 1; j >= 0; --j)
251 v = v * (z - X[j]) + C[j];
252 return v;
253 }
254
266 public static double evalPoly(double[] C, int n, double x) {
267 double v = C[n];
268 for (int j = n - 1; j >= 0; --j)
269 v = v * x + C[j];
270 return v;
271 }
272
273}
static double evalPoly(double[] C, int n, double x)
Evaluates the polynomial of degree with coefficients C[j] at :
Definition Misc.java:266
static int quickSelect(int[] A, int n, int k)
Returns the smallest item of the array of size.
Definition Misc.java:83
static double quickSelect(double[] A, int n, int k)
Returns the smallest item of the array of size.
Definition Misc.java:47
static void interpol(int n, double[] X, double[] Y, double[] C)
Computes the Newton interpolating polynomial.
Definition Misc.java:221
static double evalPoly(int n, double[] X, double[] C, double z)
Given , and as described in interpol(n, X, Y, C), this function returns the value of the interpolat...
Definition Misc.java:248
static int getTimeInterval(double[] times, int start, int end, double t)
Returns the index of the time interval corresponding to time t.
Definition Misc.java:173
static double getMedian(int[] A, int n)
Returns the median of the first elements of array .
Definition Misc.java:134
static double getMedian(double[] A, int n)
Returns the median of the first elements of array .
Definition Misc.java:116