SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
DigitalNetBase2FromFile.java
1/*
2 * Class: DigitalNetBase2FromFile
3 * Description: read the parameters defining a digital net in base 2
4 from a file or from a URL address
5 * Environment: Java
6 * Software: SSJ
7 * Copyright (C) 2001 Pierre L'Ecuyer and Universite de Montreal
8 * Organization: DIRO, Universite de Montreal
9 * @author
10 * @since
11 *
12 *
13 * Licensed under the Apache License, Version 2.0 (the "License");
14 * you may not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS,
21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
24 *
25 */
26package umontreal.ssj.hups;
27
28import java.io.*;
29import java.net.MalformedURLException;
30import umontreal.ssj.util.PrintfFormat;
31
111 private String filename;
112
113 // s is the effective dimension if > 0, otherwise it is dim
114 private void readData(Reader re, int r1, int s1) throws IOException, NumberFormatException {
115 try {
116 StreamTokenizer st = new StreamTokenizer(re);
117 if (st == null)
118 return;
119 st.eolIsSignificant(false);
120 st.slashSlashComments(true);
121
122 int i = st.nextToken();
123 if (i != StreamTokenizer.TT_NUMBER)
124 throw new NumberFormatException();
125 b = (int) st.nval;
126 st.nextToken();
127 numCols = (int) st.nval;
128 st.nextToken();
129 numRows = (int) st.nval;
130 st.nextToken();
131 numPoints = (int) st.nval;
132 st.nextToken();
133 dim = (int) st.nval;
134 if (dim < 1) {
135 System.err.println(PrintfFormat.NEWLINE + "DigitalNetBase2FromFile: dimension dim <= 0");
136 throw new IllegalArgumentException("dimension dim <= 0");
137 }
138 if (r1 > numRows)
139 throw new IllegalArgumentException("DigitalNetBase2FromFile: One must have r1 <= Max num rows");
140 if (s1 > dim) {
141 throw new IllegalArgumentException("s1 is too large");
142 }
143 if (s1 > 0)
144 dim = s1;
145 if (r1 > 0)
146 numRows = r1;
147
148 if (b != 2) {
149 System.err.println("***** DigitalNetBase2FromFile: only base 2 allowed");
150 throw new IllegalArgumentException("only base 2 allowed");
151 }
152 genMat = new int[dim * numCols];
153 for (i = 0; i < dim; i++)
154 for (int c = 0; c < numCols; c++) {
155 st.nextToken();
156 genMat[i * numCols + c] = (int) st.nval;
157 }
158
159 } catch (NumberFormatException e) {
160 System.err.println(" DigitalNetBase2FromFile: not a number " + e);
161 throw e;
162 }
163 }
164
165 private void maskRows(int r, int w) {
166 // Keep only the r most significant bits and set the others to 0.
167 int mask = (int) ((1L << r) - 1);
168 mask <<= MAXBITS - r;
169 for (int i = 0; i < dim; i++)
170 for (int c = 0; c < numCols; c++) {
171 genMat[i * numCols + c] &= mask;
172 genMat[i * numCols + c] >>= MAXBITS - w;
173 }
174 }
175
190 public DigitalNetBase2FromFile(String filename, int r1, int w, int s1) throws IOException, MalformedURLException {
191 super();
192 if (w < r1 || w > MAXBITS)
193 throw new IllegalArgumentException(" Must have numRows <= w <= 31");
194
195 BufferedReader input;
196 if (filename.startsWith("http:") || filename.startsWith("ftp:"))
197 input = DigitalNetFromFile.openURL(filename);
198 else
199 input = DigitalNetFromFile.openFile(filename);
200
201 try {
202 readData(input, r1, s1);
203 } catch (NumberFormatException e) {
204 System.err.println(" DigitalNetBase2FromFile: cannot read from " + filename);
205 throw e;
206
207 } catch (IOException e) {
208 System.err.println(" DigitalNetBase2FromFile: cannot read from " + filename);
209 throw e;
210 }
211 input.close();
212 maskRows(numRows, w);
213 outDigits = w;
214 if (numCols >= MAXBITS)
215 throw new IllegalArgumentException(" Must have numCols < 31");
216
217 this.filename = filename;
218 int x = (1 << numCols);
219 if (x != numPoints) {
220 System.out.println("numPoints != 2^k");
221 throw new IllegalArgumentException("numPoints != 2^k");
222 }
223 // Compute the normalization factors.
224 normFactor = 1.0 / ((double) (1L << (outDigits)));
225
226 }
227
236 public DigitalNetBase2FromFile(String filename, int s1) throws IOException, MalformedURLException {
237 this(filename, -1, 31, s1);
238 }
239
240 public String toString() {
241 StringBuffer sb = new StringBuffer("File: " + filename + PrintfFormat.NEWLINE);
242 sb.append(super.toString());
243 return sb.toString();
244 }
245
251 public String toStringDetailed() {
252 StringBuffer sb = new StringBuffer(toString() + PrintfFormat.NEWLINE);
253 sb.append("dim = " + dim + PrintfFormat.NEWLINE);
254 for (int i = 0; i < dim; i++) {
255 sb.append(PrintfFormat.NEWLINE + "// dim = " + (1 + i) + PrintfFormat.NEWLINE);
256 for (int c = 0; c < numCols; c++)
257 sb.append(genMat[i * numCols + c] + PrintfFormat.NEWLINE);
258 }
259 sb.append("--------------------------------" + PrintfFormat.NEWLINE);
260 return sb.toString();
261 }
262
270 public static String listDir(String dirname) throws IOException {
271 return DigitalNetFromFile.listDir(dirname);
272 }
273
274}
String toStringDetailed()
Writes the parameters and the generating matrices of this digital net to a string.
DigitalNetBase2FromFile(String filename, int r1, int w, int s1)
Constructs a digital net in base 2 after reading its parameters from file filename.
String toString()
Formats a string that contains information on this digital net.
static String listDir(String dirname)
Lists all files (or directories) in directory dirname.
DigitalNetBase2FromFile(String filename, int s1)
Same as DigitalNetBase2FromFile(filename,r, 31, s1) where s1 is the dimension and r is given in data ...
A special case of DigitalNet for the base .
This class allows us to read the parameters defining a digital net either from a file,...
static String listDir(String dirname)
Lists all files (or directories) in directory dirname.
static final int MAXBITS
Since Java has no unsigned type, the 32nd bit cannot be used efficiently, so we have only 31 bits.
int numPoints
Number of 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.