SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
Rank1Lattice.java
1/*
2 * Class: Rank1Lattice
3 * Description: Rank-1 lattice
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;
29
45public class Rank1Lattice extends PointSet {
46
47 protected int[] genAs; // Lattice generator: a[i]
48 protected double[] v; // Lattice vector: v[i] = a[i]/n
49 protected double normFactor; // 1/n.
50
51 private void initN(int n) {
52 numPoints = n;
53 normFactor = 1.0 / (double) n;
54 for (int j = 0; j < dim; j++) {
55 int amod = (genAs[j] % n) + (genAs[j] < 0 ? n : 0);
56 v[j] = normFactor * amod;
57 }
58 }
59
68 public Rank1Lattice(int n, int[] a, int s) {
69 dim = s;
70 v = new double[s];
71 genAs = new int[s];
72 for (int j = 0; j < s; j++) {
73 genAs[j] = a[j];
74 }
75 initN(n);
76 }
77
83 public void setNumPoints(int n) {
84 initN(n);
85 }
86
92 public int[] getAs() {
93 return genAs;
94 }
95
106 public void addRandomShift(int d1, int d2, RandomStream stream) {
107 if (null == stream)
108 throw new IllegalArgumentException(PrintfFormat.NEWLINE + " Calling addRandomShift with null stream");
109 if (0 == d2)
110 d2 = Math.max(1, dim);
111 if (shift == null) {
112 shift = new double[d2];
113 capacityShift = d2;
114 } else if (d2 > capacityShift) {
115 int d3 = Math.max(4, capacityShift);
116 while (d2 > d3)
117 d3 *= 2;
118 double[] temp = new double[d3];
119 capacityShift = d3;
120 for (int i = 0; i < d1; i++)
121 temp[i] = shift[i];
122 shift = temp;
123 }
124 dimShift = d2;
125 for (int i = d1; i < d2; i++)
126 shift[i] = stream.nextDouble();
127 shiftStream = stream;
128 }
129
133 public void clearRandomShift() {
134 super.clearRandomShift();
135 shift = null;
136 }
137
138 public String toString() {
139 StringBuffer sb = new StringBuffer("Rank1Lattice:" + PrintfFormat.NEWLINE);
140 sb.append(super.toString());
141 return sb.toString();
142 }
143
144 public double getCoordinate(int i, int j) {
145 double x = (v[j] * i) % 1.0;
146 if (shift != null) {
147 if (j >= dimShift) // Extend the shift.
149 x += shift[j];
150 if (x >= 1.0)
151 x -= 1.0;
152 if (x <= 0.0)
153 x = EpsilonHalf; // avoid x = 0
154 }
155 return x;
156 }
157
158 // Recursive method that computes a^e mod m.
159 protected long modPower(long a, int e, int m) {
160 // If parameters a and m == numPoints could be omitted, then
161 // the routine would run much faster due to reduced stack usage.
162 // Note that a can be larger than m, e.g. in lattice sequences !
163
164 if (e == 0)
165 return 1;
166 else if (e == 1)
167 return a % m;
168 else if ((e & 1) == 1)
169 return (a * modPower(a, e - 1, m)) % m;
170 else {
171 long p = modPower(a, e / 2, m);
172 return (p * p) % m;
173 }
174 }
175
176 protected double radicalInverse(int base, int i) {
177 double digit, radical, inverse;
178 digit = radical = 1.0 / (double) base;
179 for (inverse = 0.0; i > 0; i /= base) {
180 inverse += digit * (double) (i % base);
181 digit *= radical;
182 }
183 return inverse;
184 }
185
187 return new Rank1LatticeIterator();
188 }
189
190// ************************************************************************
191
193 public double nextCoordinate() {
194 // I tried with long's and with double's. The double version is
195 // 4.5 times faster than the long version.
197 outOfBounds();
198// return (curPointIndex * v[curCoordIndex++]) % 1.0;
199 double x = (curPointIndex * v[curCoordIndex]) % 1.0;
200 if (shift != null) {
201 if (curCoordIndex >= dimShift) // Extend the shift.
203 x += shift[curCoordIndex];
204 if (x >= 1.0)
205 x -= 1.0;
206 if (x <= 0.0)
207 x = EpsilonHalf; // avoid x = 0
208 }
210 return x;
211 }
212 }
213}
This class implements a default point set iterator.
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.
This abstract class represents a general point set.
Definition PointSet.java:99
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.
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.
int dim
Dimension of the points.
RandomStream shiftStream
Stream used to generate the random shifts.
Rank1Lattice(int n, int[] a, int s)
Instantiates a Rank1Lattice with points and lattice vector of dimension .
void clearRandomShift()
Clears the random shift.
void setNumPoints(int n)
Resets the number of points of the lattice to .
double getCoordinate(int i, int j)
Returns , the coordinate of the point .
String toString()
Formats a string that contains information about the point set.
PointSetIterator iterator()
Constructs and returns a point set iterator.
int[] getAs()
Returns the generator of the lattice.
void addRandomShift(int d1, int d2, RandomStream stream)
Adds a random shift to all the points of the point set, using stream stream to generate the random nu...
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...
double nextDouble()
Returns a (pseudo)random number from the uniform distribution over the interval , using this stream,...