SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
TableFormat.java
1/*
2 * Class: TableFormat
3 * Description: Provides methods to format arrays into String's
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.util;
26
35public class TableFormat {
36 private TableFormat() {
37 }
38
42
46 public static final int PLAIN = 0;
47
51 public static final int MATHEMATICA = 1;
52
56 public static final int MATLAB = 2;
57
61
65
79 public static String format(int V[], int n1, int n2, int k, int p) {
80 int i;
81 StringBuffer sb = new StringBuffer();
82 if (k > 1) {
83 sb.append("Elements " + n1 + " to " + n2 + PrintfFormat.NEWLINE + PrintfFormat.NEWLINE);
84 for (i = n1; i <= n2; i++) {
85 sb.append(PrintfFormat.d(p, V[i]));
86 if (((i + 1 - n1) % k) == 0)
87 sb.append(PrintfFormat.NEWLINE);
88 }
89 sb.append(PrintfFormat.NEWLINE);
90 } else {
91 sb.append(PrintfFormat.NEWLINE + " Index Element" + PrintfFormat.NEWLINE);
92 for (i = n1; i <= n2; i++)
93 sb.append(PrintfFormat.d(6, i) + " " + PrintfFormat.d(12, V[i]) + PrintfFormat.NEWLINE);
94 }
95 sb.append(PrintfFormat.NEWLINE);
96 return sb.toString();
97 }
98
113 public static String format(double V[], int n1, int n2, int k, int p1, int p2, int p3) {
114 int i;
115 StringBuffer sb = new StringBuffer();
116 if (k > 1) {
117 sb.append("Elements " + n1 + " to " + n2 + PrintfFormat.NEWLINE + PrintfFormat.NEWLINE);
118 for (i = n1; i <= n2; i++) {
119 sb.append(PrintfFormat.format(p1, p2, p3, V[i]));
120 if (((i + 1 - n1) % k) == 0)
121 sb.append(PrintfFormat.NEWLINE);
122 }
123 sb.append(PrintfFormat.NEWLINE);
124
125 } else {
126 sb.append(PrintfFormat.NEWLINE + " Index Element" + PrintfFormat.NEWLINE);
127 for (i = n1; i <= n2; i++)
128 sb.append(PrintfFormat.d(6, i) + " " + PrintfFormat.format(p1, p2, p3, V[i]) + PrintfFormat.NEWLINE);
129 }
130 sb.append(PrintfFormat.NEWLINE);
131 return sb.toString();
132 }
133
134 private static int Style = PLAIN;
135
136 private static char OuvrantMat = ' '; // Matrix delimitors
137 private static char FermantMat = ' ';
138
139 private static char OuvrantVec = ' '; // Vector delimitors
140 private static char FermantVec = ' ';
141
142 private static char SepareVec = ' '; // Element separators
143 private static char SepareElem = ' ';
144
145 private static void fixeDelim(int style) {
146 /*
147 * Fixe les delimiteurs pour imprimer une matrice selon un format approprie
148 */
149 Style = style;
150 switch (style) {
151 case MATHEMATICA:
152 OuvrantMat = '{';
153 FermantMat = '}';
154 OuvrantVec = '{';
155 FermantVec = '}';
156 SepareVec = ',';
157 SepareElem = ',';
158 break;
159 case MATLAB:
160 OuvrantMat = '[';
161 FermantMat = ']';
162 OuvrantVec = ' ';
163 FermantVec = ' ';
164 SepareVec = ' ';
165 SepareElem = ' ';
166 break;
167 default:
168 OuvrantMat = ' ';
169 FermantMat = ' ';
170 OuvrantVec = ' ';
171 FermantVec = ' ';
172 SepareVec = ' ';
173 SepareElem = ' ';
174 break;
175 }
176 }
177
178 @Deprecated
179 public static String format(int[][] Mat, int i1, int i2, int j1, int j2, int w, int p, int style, String Name) {
180 return format(Mat, i1, i2, j1, j2, w, style, Name);
181 }
182
205 public static String format(double[][] Mat, int i1, int i2, int j1, int j2, int w, int p, int style, String Name) {
206 int k;
207 int j;
208 int i;
209 double x;
210 String S;
211
212 fixeDelim(style);
213 StringBuffer sb = new StringBuffer();
214 if (Name.length() > 0)
215 sb.append(Name + " = ");
216
217 double prec = Math.pow(10.0, (double) p);
218 sb.append(OuvrantMat + PrintfFormat.NEWLINE);
219 for (i = i1; i <= i2; i++) {
220 sb.append(OuvrantVec);
221 for (j = j1; j <= j2; j++) {
222 sb.append(' ');
223 switch (style) {
224 case MATHEMATICA:
225 x = Mat[i][j];
226 if (((x != 0.0) && (Math.abs(x) < 0.1)) || (Math.abs(x) > prec)) {
227 S = PrintfFormat.G(0, p, x);
228 int exppos = S.indexOf('E');
229 if (exppos != -1)
230 S = S.substring(0, exppos) + "*10^(" + S.substring(exppos + 1) + ")";
231 } else
232 S = PrintfFormat.f(0, p, x);
233 S = PrintfFormat.s(w, S);
234 break;
235 default:
236 // MATLAB, Default */
237 sb.append(PrintfFormat.G(w, p, Mat[i][j]));
238 break;
239 }
240 if (j < j2)
241 sb.append(SepareElem);
242 }
243 sb.append(FermantVec);
244 if (i < i2)
245 sb.append(SepareVec + PrintfFormat.NEWLINE);
246 }
247 sb.append(FermantMat + PrintfFormat.NEWLINE);
248 return sb.toString();
249 }
250
265 public static String format(int[][] Mat, int i1, int i2, int j1, int j2, int w, int style, String Name) {
266 int i;
267 int j;
268
269 fixeDelim(style);
270 StringBuffer sb = new StringBuffer();
271 if (Name.length() > 0)
272 sb.append(Name + " = ");
273
274 sb.append(OuvrantMat + PrintfFormat.NEWLINE);
275 for (i = i1; i <= i2; i++) {
276 sb.append(OuvrantVec);
277 for (j = j1; j <= j2; j++) {
278 sb.append(PrintfFormat.d(w, Mat[i][j]));
279 if (j < j2)
280 sb.append(SepareElem);
281 }
282 sb.append(FermantVec);
283 if (i < i2)
284 sb.append(SepareVec + PrintfFormat.NEWLINE);
285 }
286 sb.append(FermantMat + PrintfFormat.NEWLINE + PrintfFormat.NEWLINE);
287 return sb.toString();
288 }
289}
290
This class acts like a StringBuffer which defines new types of append methods.
static String s(String str)
Same as s(0, str).
static String d(long x)
Same as d(0, 1, x).
static String f(double x)
Same as f(0, 6, x).
static final String NEWLINE
End-of-line symbol or line separator.
static String format(long x)
Same as d(0, 1, x).
static String G(double x)
Same as G(0, 6, x).
static final int MATLAB
Matlab matrix printing style.
static String format(int[][] Mat, int i1, int i2, int j1, int j2, int w, int style, String Name)
Similar to the previous method, but for a matrix of int’s.
static String format(double V[], int n1, int n2, int k, int p1, int p2, int p3)
Similar to the previous method, but for an array of double’s.
static final int MATHEMATICA
Mathematica matrix printing style.
static final int PLAIN
Plain text matrix printing style.
static String format(int V[], int n1, int n2, int k, int p)
Formats a String containing the elements n1 to n2 (inclusive) of table V, k elements per line,...
static String format(double[][] Mat, int i1, int i2, int j1, int j2, int w, int p, int style, String Name)
Formats the submatrix with lines i1 i2 and columns j1 j2 of the matrix Mat, using the formatting st...