SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
CachedPointSet.java
1/*
2 * Class: CachedPointSet
3 * Description:
4 * Environment: Java
5 * Software: SSJ
6 * Copyright (C) 2001--2018 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 java.util.Arrays;
29import umontreal.ssj.util.multidimsort.DoubleArrayComparator;
30import umontreal.ssj.util.multidimsort.MultiDimSort;
31import umontreal.ssj.rng.RandomStream;
32
52public class CachedPointSet extends PointSet {
53 protected PointSet P; // Original PointSet which is cached in the present object.
54 protected double x[][]; // Cached points.
55 int fromPoint = 0; // Number of skipped points (usually 0).
56 int fromDim = 0; // Number of skipped coordinates (usually 0).
57 // int numPoints; // Number of retained points, inherited from PointSet.
58 // int dim; // Dimension of the *retained* points, inherited from PointSet.
59 boolean randomizeParent = true;
60
61 protected CachedPointSet() {
62 } // Constructs an empty cache for a point set.
63
78 public CachedPointSet(PointSet p, int fromPoint, int toPoint, int fromDim, int toDim) {
79 if (p.getNumPoints() < toPoint - fromPoint)
80 throw new IllegalArgumentException("Cannot cache more points than in point set p.");
81 if (p.getDimension() < toDim - fromDim)
82 throw new IllegalArgumentException("Attempt to cache points using more coordinates than the dimension.");
83 if (toPoint == Integer.MAX_VALUE)
84 throw new IllegalArgumentException("Cannot cache infinite number of points");
85 if (toDim == Integer.MAX_VALUE)
86 throw new IllegalArgumentException("Cannot cache infinite dimensional points");
87 this.fromPoint = fromPoint;
88 numPoints = toPoint - fromPoint;
89 this.fromDim = fromDim;
90 dim = toDim - fromDim;
91 this.P = p;
92 x = new double[numPoints][dim];
93 fillCache(fromDim, dim);
94 }
95
100 protected void fillCache(int fromDim, int dim) {
101 PointSetIterator itr = P.iterator();
102 if (fromPoint > 0)
103 itr.setCurPointIndex(fromPoint);
104 for (int i = 0; i < numPoints; i++)
105 itr.nextPoint(x[i], fromDim, dim);
106 }
107
112 public CachedPointSet(PointSet p, int n, int dim) {
113 this(p, 0, n, 0, dim);
114 }
115
124 this(p, 0, p.getNumPoints(), 0, p.getDimension());
125 }
126
132 return new CachedPointSetIterator();
133 }
134
141 public void setRandomizeParent(boolean randomizeParent) {
142 this.randomizeParent = randomizeParent;
143 }
144
153 public void addRandomShift(int d1, int d2, RandomStream stream) {
154 P.addRandomShift(d1, d2, stream);
155 fillCache(fromDim, dim);
156 }
157
166 if (randomizeParent) {
167 rand.randomize(P);
168 fillCache(fromDim, dim);
169 } else {
170 rand.randomize(this);
171 }
172 }
173
180 public void sortByCoordinate(int j) {
181 Arrays.sort(x, new DoubleArrayComparator(j));
182 }
183
190 public <T> void sort(MultiDimSort<T> sort) {
191 sort.sort(x);
192 // sort.sort (P); init(); ? No.
193 }
194
200 public void stripCoordinates(int d) {
201 for (int i = 0; i < numPoints; i++)
202 for (int j = 0; j < d; j++)
203 x[i][j] = x[i][j + d];
204 dim = dim - d;
205 }
206
207 public String toString() {
208 StringBuffer sb = new StringBuffer("Cached point set" + PrintfFormat.NEWLINE);
209 sb.append(super.toString());
210 sb.append(PrintfFormat.NEWLINE + "Cached point set information {" + PrintfFormat.NEWLINE);
211 sb.append(P.toString());
212 sb.append(PrintfFormat.NEWLINE + "}");
213 return sb.toString();
214 }
215
216 public double getCoordinate(int i, int j) {
217 return x[i][j];
218 }
219
220 public double[][] getArray() {
221 return x;
222 }
223
228 return P;
229 }
230
231// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
232//
233
240
241 public double nextCoordinate() {
243 outOfBounds();
244 return x[curPointIndex][curCoordIndex++];
245 }
246
247 public void nextCoordinates(double p[], int d) {
248 if (getCurCoordIndex() + d > getDimension())
249 outOfBounds();
250 for (int j = 0; j < d; j++)
251 p[j] = x[curPointIndex][curCoordIndex++];
252 }
253
254 }
255
256}
This class implements a CachedPointSet iterator which takes the values directly in the array x in whi...
CachedPointSet(PointSet p)
Creates a new PointSet object that contains an array storing the points of p.
void randomize(PointSetRandomization rand)
Randomizes the underlying point set using rand and re-caches the points.
void sortByCoordinate(int j)
Sorts the cached points by increasing order of coordinate j.
void fillCache(int fromDim, int dim)
Caches the points, skipping fromDim coordinates and taking the next dim ones.
void addRandomShift(int d1, int d2, RandomStream stream)
Generates a random shift, adds it to the contained point set, and re-caches the points.
PointSetIterator iterator()
Constructs and returns a point set iterator that gets the values directly from the cached array.
void stripCoordinates(int d)
Removes the first d coordinates of each cached point.
double getCoordinate(int i, int j)
Returns , the coordinate of the point .
CachedPointSet(PointSet p, int n, int dim)
Same as CachedPointSet(p, 0,n, 0, dim).
String toString()
Formats a string that contains information about the point set.
PointSet getParentPointSet()
Returns the reference point set that was passed to the constructor.
CachedPointSet(PointSet p, int fromPoint, int toPoint, int fromDim, int toDim)
Creates a new PointSet object that contains an array storing the coordinates fromDim (inclusive) to t...
public< T > void sort(MultiDimSort< T > sort)
Sorts the cached points (only) with the given.
void setRandomizeParent(boolean randomizeParent)
If randomizeParent is true, calls to randomize() will be defered to the parent point set (this is the...
This class implements a default point set iterator.
int curPointIndex
Index of the current point.
int curCoordIndex
Index of the current coordinate.
void outOfBounds()
Error message for index out of bounds.
This abstract class represents a general point set.
Definition PointSet.java:99
int numPoints
Number of points.
int getNumPoints()
Returns the number of points.
int getDimension()
Returns the dimension (number of available coordinates) of the points.
int dim
Dimension of the points.
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 provides an implementation of Comparator in which arrays of double in dimensions are compared b...
This is the interface for iterators that permit one to go through the points of a PointSet and the su...
void setCurPointIndex(int i)
Resets the current point index to and the current coordinate index to zero.
int nextPoint(double[] p, int fromDim, int d)
Returns in p the next d coordinates of the current point, starting at coordinate fromDim (i....
This interface is for a randomization that can be used to randomize a umontreal.ssj....
void randomize(PointSet p)
This method must randomize p.
This interface defines the basic structures to handle multiple streams of uniform (pseudo)random numb...
This interface is meant to be implemented by certain multivariate sorting algorithms that sort object...