SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
PointSet.java
1/*
2 * Class: PointSet
3 * Description: Base class of all point sets
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 java.util.NoSuchElementException;
28import umontreal.ssj.rng.RandomStream;
29import umontreal.ssj.util.Num;
30import umontreal.ssj.util.PrintfFormat;
31
99public abstract class PointSet {
100
107 protected static final int MAXBITS = 31; // Max number of usable bits.
108
113 protected double EpsilonHalf = 1.0 / Num.TWOEXP[55]; // 2^{-55}
114
118 protected int dim = 0;
119
123 protected int numPoints = 0;
124
129 // **Pierre:** Maybe this could be defined only in `CycleBasedPointSet` ???
130 protected int dimShift = 0;
131
135 protected int capacityShift = 0;
136
141 protected double[] shift;
142
151
159 public int getDimension() {
160 return dim;
161 }
162
170 public int getNumPoints() {
171 return numPoints;
172 }
173
177 public static int getMaxBits() {
178 return MAXBITS;
179 }
180
193 public abstract double getCoordinate(int i, int j);
194
204 return new DefaultPointSetIterator();
205 }
206
213 // @Deprecated
215 rand.randomize(this);
216 // Note that RandomShift.randomize(p) calls addRandomShift !
217 }
218
241 public void addRandomShift(int d1, int d2, RandomStream stream) {
242 if (d1 < 0 || d1 > d2)
243 throw new IllegalArgumentException("illegal parameter d1 or d2");
244 if (d2 > capacityShift) {
245 int d3 = Math.max(4, capacityShift);
246 while (d2 > d3)
247 d3 *= 2;
248 double[] temp = new double[d3];
249 capacityShift = d3;
250 for (int i = 0; i < d1; i++)
251 temp[i] = shift[i];
252 shift = temp;
253 }
254 shiftStream = stream;
255 dimShift = d2;
256 for (int i = d1; i < d2; i++)
257 shift[i] = shiftStream.nextDouble();
258 }
259
267 public void addRandomShift(RandomStream stream) {
268 addRandomShift(0, dim, stream);
269 }
270
275 public void addRandomShift(int d1, int d2) {
277 }
278
283 public void addRandomShift() {
285 }
286
290 public void clearRandomShift() {
291 capacityShift = 0;
292 dimShift = 0;
293// shiftStream = null;
294 }
295
301 public String toString() {
302 StringBuffer sb = new StringBuffer("Number of points: ");
303 int x = getNumPoints();
304 if (x == Integer.MAX_VALUE)
305 sb.append("infinite");
306 else
307 sb.append(x);
308 sb.append(PrintfFormat.NEWLINE + "Point set dimension: ");
309 x = getDimension();
310 if (x == Integer.MAX_VALUE)
311 sb.append("infinite");
312 else
313 sb.append(x);
314 return sb.toString();
315 }
316
327 public String formatPoints() {
328 PointSetIterator iter = iterator();
329 return formatPoints(iter);
330 }
331
346 public String formatPoints(int n, int d) {
347 PointSetIterator iter = iterator();
348 return formatPoints(iter, n, d);
349 }
350
362 public String formatPoints(PointSetIterator iter) {
363 int n = getNumPoints();
364 if (n == Integer.MAX_VALUE)
365 throw new UnsupportedOperationException("Number of points is infinite");
366 int d = getDimension();
367 if (d == Integer.MAX_VALUE)
368 throw new UnsupportedOperationException("Dimension is infinite");
369 return formatPoints(iter, n, d);
370 }
371
383 public String formatPoints(PointSetIterator iter, int n, int d) {
384 if (getNumPoints() < n)
385 n = getNumPoints();
386 if (getDimension() < d)
387 d = getDimension();
388 StringBuffer sb = new StringBuffer(toString());
389 sb.append(PrintfFormat.NEWLINE + PrintfFormat.NEWLINE + "Points of the point set:" + PrintfFormat.NEWLINE);
390 for (int i = 0; i < n; i++) {
391 for (int j = 0; j < d; j++) {
392 sb.append(" ");
393 sb.append(iter.nextCoordinate());
394 }
395 sb.append(PrintfFormat.NEWLINE);
396 iter.resetToNextPoint();
397 }
398 return sb.toString();
399 }
400
411 public String formatPointsBase(int b) {
412 PointSetIterator iter = iterator();
413 return formatPointsBase(iter, b);
414 }
415
426 public String formatPointsBase(int n, int d, int b) {
427 PointSetIterator iter = iterator();
428 return formatPointsBase(iter, n, d, b);
429 }
430
442 public String formatPointsBase(PointSetIterator iter, int b) {
443 int n = getNumPoints();
444 if (n == Integer.MAX_VALUE)
445 throw new UnsupportedOperationException("Number of points is infinite");
446 int d = getDimension();
447 if (d == Integer.MAX_VALUE)
448 throw new UnsupportedOperationException("Dimension is infinite");
449 return formatPointsBase(iter, n, d, b);
450 }
451
463 public String formatPointsBase(PointSetIterator iter, int n, int d, int b) {
464 if (getNumPoints() < n)
465 n = getNumPoints();
466 if (getDimension() < d)
467 d = getDimension();
468 StringBuffer sb = new StringBuffer(toString());
469 sb.append(PrintfFormat.NEWLINE + PrintfFormat.NEWLINE + "Points of the point set:" + PrintfFormat.NEWLINE);
470 double x;
471 int acc = 10;
472 if (b == 2)
473 acc = 20;
474 else if (b == 3)
475 acc = 13;
476 else
477 acc = 10;
478 int width = acc + 3;
479 String chaine;
480 for (int i = 0; i < n; i++) {
481 for (int j = 0; j < d; j++) {
482 sb.append(" ");
483 x = iter.nextCoordinate();
484 chaine = PrintfFormat.formatBase(-width, acc, b, x);
485 sb.append(chaine);
486 }
487 sb.append(PrintfFormat.NEWLINE);
488 iter.resetToNextPoint();
489 }
490 return sb.toString();
491 }
492
503 public String formatPointsNumbered() {
504 int n = getNumPoints();
505 if (n == Integer.MAX_VALUE)
506 throw new UnsupportedOperationException("Number of points is infinite");
507 int d = getDimension();
508 if (d == Integer.MAX_VALUE)
509 throw new UnsupportedOperationException("Dimension is infinite");
510 return formatPointsNumbered(n, d);
511 }
512
522 public String formatPointsNumbered(int n, int d) {
523 if (getNumPoints() < n)
524 n = getNumPoints();
525 if (getDimension() < d)
526 d = getDimension();
527 StringBuffer sb = new StringBuffer(toString());
529 sb.append(PrintfFormat.NEWLINE + PrintfFormat.NEWLINE + "Points of the point set:");
530 for (int i = 0; i < n; i++) {
531 sb.append(PrintfFormat.NEWLINE + "Point " +
532 // itr.getCurPointIndex() + " = (");
533 i + " = (");
534 boolean first = true;
535 for (int j = 0; j < d; j++) {
536 if (first)
537 first = false;
538 else
539 sb.append(", ");
540 sb.append(itr.nextCoordinate());
541 }
542 sb.append(")");
543 itr.resetToNextPoint();
544 }
545 return sb.toString();
546 }
547
548// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
556 protected class DefaultPointSetIterator implements PointSetIterator {
557
561 protected int curPointIndex = 0;
562
566 protected int curCoordIndex = 0;
567
571 protected double EpsilonHalf = 1.0 / Num.TWOEXP[55];
572
576 protected void outOfBounds() {
578 throw new NoSuchElementException("Not enough points available");
579 else
580 throw new NoSuchElementException("Not enough coordinates available");
581 }
582
586 public void setCurCoordIndex(int j) {
587 curCoordIndex = j;
588 }
589
593 public void resetCurCoordIndex() {
595 }
596
600 public int getCurCoordIndex() {
601 return curCoordIndex;
602 }
603
607 public boolean hasNextCoordinate() {
608 return getCurCoordIndex() < getDimension();
609 }
610
614 public double nextCoordinate() {
616 outOfBounds();
618 }
619
623 public void nextCoordinates(double p[], int d) {
624 if (getCurCoordIndex() + d > getDimension())
625 outOfBounds();
626 for (int j = 0; j < d; j++)
627 p[j] = nextCoordinate();
628 }
629
633 // This is called with i = numPoints when nextPoint generates the
634 // last point, so i = numPoints must be allowed.
635 // The "no more point" error will be raised if we ask for
636 // a new coordinate or point when i = numPoints.
637 public void setCurPointIndex(int i) {
638 curPointIndex = i;
640 }
641
645 public void resetCurPointIndex() {
647 }
648
653 public int resetToNextPoint() {
655 return curPointIndex;
656 }
657
661 public int getCurPointIndex() {
662 return curPointIndex;
663 }
664
668 public boolean hasNextPoint() {
669 return getCurPointIndex() < getNumPoints();
670 }
671
678 public int nextPoint(double p[], int fromDim, int d) {
679 setCurCoordIndex(fromDim);
680 nextCoordinates(p, d);
681 return resetToNextPoint();
682 }
683
687 public int nextPoint(double p[], int d) {
689 nextCoordinates(p, d);
690 return resetToNextPoint();
691 }
692
696 public void resetStartStream() {
698 }
699
703 public void resetStartSubstream() {
705 }
706
710 public void resetNextSubstream() { // Same as resetToNextPoint();
712 }
713
720 public void setAntithetic(boolean b) {
721 throw new UnsupportedOperationException();
722 }
723
727 public double nextDouble() {
728 return nextCoordinate();
729 }
730
735 public void nextArrayOfDouble(double[] u, int start, int n) {
736 if (n < 0)
737 throw new IllegalArgumentException("n must be positive.");
738 for (int i = start; i < start + n; i++)
739 u[i] = nextDouble();
740 }
741
746 public int nextInt(int i, int j) {
747 return (i + (int) (nextDouble() * (j - i + 1.0)));
748 }
749
754 public void nextArrayOfInt(int i, int j, int[] u, int start, int n) {
755 if (n < 0)
756 throw new IllegalArgumentException("n must be positive.");
757 for (int k = start; k < start + n; k++)
758 u[k] = nextInt(i, j);
759 }
760
765 public String formatState() {
766 return "Current point index: " + getCurPointIndex() + PrintfFormat.NEWLINE + "Current coordinate index: "
768 }
769 }
770}
This class implements a default point set iterator.
int curPointIndex
Index of the current point.
void resetCurPointIndex()
Resets both the current point index and the current coordinate to 0.
void resetNextSubstream()
Same as resetToNextPoint().
void setCurCoordIndex(int j)
Set current coordinate to j.
int nextPoint(double p[], int d)
Same as nextPoint(p, 0, d).
int curCoordIndex
Index of the current coordinate.
int resetToNextPoint()
Resets the current point index to the next one and current coordinate to 0, and returns the new curre...
double EpsilonHalf
Default constant epsilon/2 added to the points after a random shift.
void nextArrayOfDouble(double[] u, int start, int n)
Returns in p[start..start+n-1] a block of n successive coordinates of the current point,...
int nextInt(int i, int j)
Similar to nextDouble(), but returns an integer uniformly distributed in [i..j].
void resetStartSubstream()
Same as resetCurCoordIndex().
void nextArrayOfInt(int i, int j, int[] u, int start, int n)
Similar to nextArrayOfDouble but returns in u[start..start+n-1] a block of n integers uniformly distr...
void setCurPointIndex(int i)
Resets the current point index to i and current coordinate to 0.
void setAntithetic(boolean b)
Not implemented here, raises an exception.
int nextPoint(double p[], int fromDim, int d)
Returns in p a block of d successive coordinates of the current point, starting at coordinate fromDim...
void resetCurCoordIndex()
Set current coordinate to 0.
void resetStartStream()
Same as resetCurPointIndex().
void outOfBounds()
Error message for index out of bounds.
double nextDouble()
Same as nextCoordinate().
This abstract class represents a general point set.
Definition PointSet.java:99
static final int MAXBITS
Since Java has no unsigned type, the 32nd bit cannot be used efficiently, so we have only 31 bits.
int dimShift
Current dimension of the shift.
String formatPoints(PointSetIterator iter, int n, int d)
Same as invoking formatPoints(n, d), but prints the points by calling iter repeatedly.
String formatPoints()
Same as invoking formatPoints(n, d) with and equal to the number of points and the dimension of thi...
void addRandomShift()
Same as addRandomShift(0, dim), where dim is the dimension of the point set.
static int getMaxBits()
Returns the maximum number of usable bits.
String formatPointsNumbered(int n, int d)
Same as invoking formatPoints(n,d), except that the points are numbered.
void randomize(PointSetRandomization rand)
Randomizes this point set using the given rand.
String formatPointsBase(int b)
Similar to formatPoints(), but the points coordinates are printed in base .
PointSetIterator iterator()
Constructs and returns a point set iterator.
void addRandomShift(int d1, int d2)
Refreshes the random shift (generates new uniform values for the random shift coordinates) for coordi...
int numPoints
Number of points.
String formatPointsBase(int n, int d, int b)
Similar to formatPoints(n, d), but the points coordinates are printed in base .
int getNumPoints()
Returns the number of points.
double EpsilonHalf
To avoid 0 for nextCoordinate when random shifting, we add this to each coordinate.
double[] shift
This is the shift vector as a double[] array, which contains the current random shift in case we appl...
int capacityShift
Number of array elements in the shift vector, always >= dimShift.
void clearRandomShift()
Erases the current random shift, if any.
abstract double getCoordinate(int i, int j)
Returns , the coordinate of the point .
String formatPoints(PointSetIterator iter)
Same as invoking formatPoints(iter, n, d) with and equal to the number of points and the dimension,...
String formatPointsBase(PointSetIterator iter, int b)
Similar to formatPoints(iter), but the points coordinates are printed in base.
int getDimension()
Returns the dimension (number of available coordinates) of the points.
int dim
Dimension of the points.
String formatPointsNumbered()
Same as invoking formatPointsNumbered(n, d) with and equal to the number of points and the dimensio...
RandomStream shiftStream
Stream used to generate the random shifts.
void addRandomShift(int d1, int d2, RandomStream stream)
By default, this method generates a random shift in the protected double[] array shift,...
String formatPoints(int n, int d)
Formats a string that displays the same information as returned by toString, together with the first ...
String formatPointsBase(PointSetIterator iter, int n, int d, int b)
Similar to formatPoints(iter,n, d), but the points coordinates are printed in base .
void addRandomShift(RandomStream stream)
Same as addRandomShift(0, dim, stream), where dim is the dimension of the point set.
String toString()
Formats a string that contains information about the point set.
This class provides various constants and methods to compute numerical quantities such as factorials,...
Definition Num.java:35
static final double TWOEXP[]
Contains the precomputed positive powers of 2.
Definition Num.java:170
This class acts like a StringBuffer which defines new types of append methods.
static String formatBase(int b, long x)
Same as formatBase(0, b, x).
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...
int resetToNextPoint()
Increases the current point index by 1 and returns its new value.
double nextCoordinate()
Returns the current coordinate and advances to the next one.
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...