SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
CycleBasedPointSetBase2.java
1/*
2 * Class: CycleBasedPointSetBase2
3 * Description:
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.hups;
26
27import umontreal.ssj.util.PrintfFormat;
28import umontreal.ssj.rng.RandomStream;
29import cern.colt.list.*;
30
50public abstract class CycleBasedPointSetBase2 extends CycleBasedPointSet {
51
52// dim = Integer.MAX_VALUE; // Dimension is infinite.
53 private int[] digitalShift; // Digital shift, initially zero (null).
54 // Entry j is for dimension j.
55 protected int numBits; // Number of bits in stored values.
56 protected double normFactor; // To convert output to (0,1); 1/2^numBits.
57
58 public double getCoordinate(int i, int j) {
59 // Find cycle that contains point i, then index in cycle.
60 int l = 0; // Length of next cycle.
61 int n = 0; // Total length of cycles added so far.
62 int k;
63 for (k = 0; n <= i; k++)
64 n += l = ((AbstractList) cycles.get(k)).size();
65 AbstractList curCycle = (AbstractList) cycles.get(k - 1);
66 int[] curCycleI = ((IntArrayList) curCycle).elements();
67 int coordinate = (i - n + l + j) % curCycle.size();
68 int shift = 0;
69 if (digitalShift != null) {
70 shift = digitalShift[j];
71 return (shift ^ curCycleI[coordinate]) * normFactor + EpsilonHalf;
72 } else
73 return (shift ^ curCycleI[coordinate]) * normFactor;
74 }
75
79
86 public void addRandomShift(int d1, int d2, RandomStream stream) {
87 if (null == stream)
88 throw new IllegalArgumentException(PrintfFormat.NEWLINE + " Calling addRandomShift with null stream");
89 if (0 == d2)
90 d2 = Math.max(1, dim);
91 if (digitalShift == null) {
92 digitalShift = new int[d2];
93 capacityShift = d2;
94 } else if (d2 > capacityShift) {
95 int d3 = Math.max(4, capacityShift);
96 while (d2 > d3)
97 d3 *= 2;
98 int[] temp = new int[d3];
99 capacityShift = d3;
100 for (int i = 0; i < dimShift; i++)
101 temp[i] = digitalShift[i];
102 digitalShift = temp;
103 }
104 dimShift = d2;
105 int maxj;
106 if (numBits < 31) {
107 maxj = (1 << numBits) - 1;
108 } else {
109 maxj = 2147483647;
110 }
111 for (int i = d1; i < d2; i++)
112 digitalShift[i] = stream.nextInt(0, maxj);
113 shiftStream = stream;
114
115 }
116
120 public void clearRandomShift() {
121 super.clearRandomShift();
122 digitalShift = null;
123 }
124
125 public String formatPoints() {
126 StringBuffer sb = new StringBuffer(toString());
127 for (int c = 0; c < numCycles; c++) {
128 AbstractList curCycle = (AbstractList) cycles.get(c);
129 int[] cycle = ((IntArrayList) curCycle).elements();
130 sb.append(PrintfFormat.NEWLINE + "Cycle " + c + ": (");
131 boolean first = true;
132 for (int e = 0; e < curCycle.size(); e++) {
133 if (first)
134 first = false;
135 else
136 sb.append(", ");
137 sb.append(cycle[e]);
138 }
139 sb.append(")");
140 }
141 return sb.toString();
142 }
143
144 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
145
146 public class CycleBasedPointSetBase2Iterator extends CycleBasedPointSetIterator {
147
148 protected int[] curCycleI; // The array for current cycle
149
150 public CycleBasedPointSetBase2Iterator() {
151 super();
152 resetCurCycle(0);
153 }
154
155 protected void init() {
156 }
157
158 public void resetCurCycle(int index) {
159 curCycleIndex = index;
160 curCycle = (AbstractList) cycles.get(index);
161 curCycleI = ((IntArrayList) curCycle).elements();
162 }
163
164 public double nextCoordinate() {
165 // First, verify if there are still points....
167 outOfBounds();
168 int x = curCycleI[curCoordInCycle];
169 if (digitalShift != null) {
170 if (curCoordIndex >= dimShift) // Extend the shift.
172 x ^= digitalShift[curCoordIndex];
173 }
175 curCoordInCycle++;
176 if (curCoordInCycle >= curCycle.size())
177 curCoordInCycle = 0;
178 if (digitalShift == null)
179 return x * normFactor;
180 else
181 return x * normFactor + EpsilonHalf;
182 }
183
184 public void nextCoordinates(double p[], int dim) {
185 // First, verify if there are still points....
187 outOfBounds();
188 if (curCoordIndex + dim >= dimShift)
190 // int j = curCoordInCycle;
191 int maxj = curCycle.size();
192 int x;
193 for (int i = 0; i < dim; i++) {
194 x = curCycleI[curCoordInCycle++];
195 if (curCoordInCycle >= maxj)
196 curCoordInCycle = 0;
197 if (digitalShift == null)
198 p[i] = x * normFactor;
199 else
200 p[i] = (digitalShift[curCoordIndex + i] ^ x) * normFactor + EpsilonHalf;
201 }
203 }
204
205 public int nextPoint(double p[], int dim) {
207 outOfBounds();
208 curCoordIndex = 0;
209 curCoordInCycle = startPointInCycle;
212 return curPointIndex;
213 }
214 }
215}
Similar to CycleBasedPointSet, except that the successive values in the cycles are stored as integers...
PointSetIterator iterator()
Constructs and returns a point set iterator.
void addRandomShift(int d1, int d2, RandomStream stream)
Adds a random digital shift in base 2 to all the points of the point set, using stream stream to gene...
void clearRandomShift()
Erases the current digital random shift, if any.
String formatPoints()
Same as invoking formatPoints(n, d) with and equal to the number of points and the dimension of thi...
double getCoordinate(int i, int j)
Returns , the coordinate of the point .
int resetToNextPoint()
Resets the current point index to the next one and current coordinate to 0, and returns the new curre...
This abstract class provides the basic structures for storing and manipulating a point set defined by...
String toString()
Formats a string that contains information about the point set.
int curPointIndex
Index of the current point.
int curCoordIndex
Index of the current coordinate.
double EpsilonHalf
Default constant epsilon/2 added to the points after a random shift.
void outOfBounds()
Error message for index out of bounds.
int dimShift
Current dimension of the shift.
void addRandomShift()
Same as addRandomShift(0, dim), where dim is the dimension of the point set.
int numPoints
Number of points.
int getNumPoints()
Returns the number of points.
double EpsilonHalf
To avoid 0 for nextCoordinate when random shifting, we add this to each coordinate.
int capacityShift
Number of array elements in the shift vector, always >= dimShift.
int dim
Dimension of the points.
RandomStream shiftStream
Stream used to generate the random shifts.
This class acts like a StringBuffer which defines new types of append methods.
static final String NEWLINE
End-of-line symbol or line separator.
This is the interface for iterators that permit one to go through the points of a PointSet and the su...
This interface defines the basic structures to handle multiple streams of uniform (pseudo)random numb...
int nextInt(int i, int j)
Returns a (pseudo)random number from the discrete uniform distribution over the integers ,...