SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
BitMatrix.java
1/*
2 * Class: BitMatrix
3 * Description: implements matrices of bits and their operations
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
27import java.io.Serializable;
28
36public class BitMatrix implements Serializable, Cloneable {
37
38 static final long serialVersionUID = 2472769969919959608L;
39
40 private BitVector[] rows; // rows vectors
41
42 private int r, c; // number of rows / columns
43
50 public BitMatrix(int r, int c) {
51 rows = new BitVector[r];
52 for (int i = 0; i < r; i++)
53 rows[i] = new BitVector(c);
54 this.r = r;
55 this.c = c;
56 }
57
64 public BitMatrix(BitVector[] rows) {
65 this.rows = new BitVector[rows.length];
66 for (int i = 0; i < rows.length; i++)
67 this.rows[i] = new BitVector(rows[i]);
68 this.r = rows.length;
69 this.c = r > 0 ? rows[0].size() : 0;
70 }
71
83 public BitMatrix(int[][] data, int r, int c) {
84 this.rows = new BitVector[r];
85 for (int i = 0; i < r; i++)
86 this.rows[i] = new BitVector(data[i], c);
87 this.r = r;
88 this.c = c;
89 }
90
96 public BitMatrix(BitMatrix that) {
97 this.r = that.r;
98 this.c = that.c;
99 this.rows = new BitVector[this.r];
100 for (int i = 0; i < this.r; i++)
101 this.rows[i] = new BitVector(that.rows[i]);
102 }
103
109 public Object clone() {
110 try {
111 BitMatrix c = (BitMatrix) super.clone();
112 c.rows = (BitVector[]) rows.clone();
113 for (int i = 0; i < rows.length; i++)
114 c.rows[i] = (BitVector) rows[i].clone();
115 return c;
116 } catch (CloneNotSupportedException e) {
117 IllegalStateException ne = new IllegalStateException();
118 ne.initCause(e);
119 throw ne;
120 }
121 }
122
130 public boolean equals(BitMatrix that) {
131 if (this.r != that.r || this.c != that.c)
132 return false;
133 for (int i = 0; i < r; i++)
134 if (!this.rows[i].equals(that.rows[i]))
135 return false;
136 return true;
137 }
138
147 public String toString() {
148 StringBuffer sb = new StringBuffer();
149
150 // on affiche les bits sur les lignes dans l'order inverse de l'ordre
151 // affiche pour le BitVector pour que le bit a (0,0) soit en haut
152 // a gauche
153
154 sb.append("{ ");
155 for (int i = 0; i < rows.length - 1; i++) {
156 for (int j = 0; j < rows[i].size(); j++)
157 sb.append(rows[i].getBool(j) ? "1" : "0");
158 sb.append(PrintfFormat.NEWLINE + " ");
159 }
160 if (r > 0) {
161 for (int j = 0; j < c; j++)
162 sb.append(rows[r - 1].getBool(j) ? "1" : "0");
163 sb.append(" }");
164 } else
165 sb.append(" }");
166
167 return sb.toString();
168 }
169
179 public String printData() {
180 StringBuffer sb = new StringBuffer();
181
182 sb.append("{ ");
183 for (int i = 0; i < r; i++) {
184 sb.append("{");
185 for (int j = 0; j < (c + 31) / 32; j++) {
186 sb.append(rows[i].getInt(j));
187 if (j != (c - 1) / 32)
188 sb.append(", ");
189 }
190 sb.append("}");
191 if (i != r - 1)
192 sb.append("," + PrintfFormat.NEWLINE + " ");
193 }
194 sb.append(" }");
195
196 return sb.toString();
197 }
198
204 public int numRows() {
205 return r;
206 }
207
213 public int numColumns() {
214 return c;
215 }
216
228 public boolean getBool(int row, int column) {
229 if (row >= r || column >= c)
230 throw new IndexOutOfBoundsException();
231
232 return rows[row].getBool(column);
233 }
234
245 public void setBool(int row, int column, boolean value) {
246 if (row >= r || column >= c)
247 throw new IndexOutOfBoundsException();
248
249 rows[row].setBool(column, value);
250 }
251
258 BitMatrix result = new BitMatrix(c, r);
259
260 for (int i = 0; i < r; i++)
261 for (int j = 0; j < c; j++)
262 result.rows[j].setBool(i, rows[i].getBool(j));
263
264 return result;
265 }
266
274 public BitMatrix not() {
275 BitMatrix result = new BitMatrix(this);
276 for (int i = 0; i < r; i++)
277 result.rows[i].selfNot();
278 return result;
279 }
280
292 public BitMatrix and(BitMatrix that) {
293 if (this.c != that.c || this.r != that.r)
294 throw new IncompatibleDimensionException("Both matrices must have the same dimension. " + "this is a " + this.r
295 + "x" + this.c + " matrix " + "while that is a " + that.r + "x" + that.c + " matrix.");
296 BitMatrix result = new BitMatrix(this);
297 for (int i = 0; i < r; i++)
298 result.rows[i].selfAnd(that.rows[i]);
299 return result;
300 }
301
313 public BitMatrix or(BitMatrix that) {
314 if (this.c != that.c || this.r != that.r)
315 throw new IncompatibleDimensionException("Both matrices must have the same dimension. " + "this is a " + this.r
316 + "x" + this.c + " matrix " + "while that is a " + that.r + "x" + that.c + " matrix.");
317 BitMatrix result = new BitMatrix(this);
318 for (int i = 0; i < r; i++)
319 result.rows[i].selfOr(that.rows[i]);
320 return result;
321 }
322
334 public BitMatrix xor(BitMatrix that) {
335 if (this.c != that.c || this.r != that.r)
336 throw new IncompatibleDimensionException("Both matrices must have the same dimension. " + "this is a " + this.r
337 + "x" + this.c + " matrix " + "while that is a " + that.r + "x" + that.c + " matrix.");
338 BitMatrix result = new BitMatrix(this);
339 for (int i = 0; i < r; i++)
340 result.rows[i].selfXor(that.rows[i]);
341 return result;
342 }
343
353 BitVector res = new BitVector(r);
354
355 for (int i = 0; i < r; i++)
356 res.setBool(i, rows[i].scalarProduct(vect));
357
358 return res;
359 }
360
370 public int multiply(int vect) {
371 BitVector temp = new BitVector(new int[] { vect });
372
373 return multiply(temp).getInt(0);
374 }
375
390 if (this.c != that.r)
391 throw new IncompatibleDimensionException("The number of columns of this (" + this.c
392 + ") must be equal to the number of rows of that (" + that.r + ").");
393
394 /*
395 * BitVector[] res = new BitVector[this.r];
396 *
397 * for(int i = 0; i < this.r; i++) { res[i] = new BitVector(that.c);
398 *
399 * for(int j = 0; j < that.c; j++) { temp = false; for(int k = 0; k < this.c;
400 * k++) if(this.rows[i].getBool(k) && that.rows[k].getBool(j)) temp = !temp;
401 * res[i].setBool(j,temp); } }
402 *
403 * return BitMatrix(res);
404 */
405
406 // methode plus efficace
407
408 BitMatrix res = new BitMatrix(this.r, that.c);
409
410 for (int i = 0; i < res.r; i++)
411 for (int j = 0; j < res.c; j++)
412 if (this.rows[i].getBool(j))
413 res.rows[i].selfXor(that.rows[j]);
414
415 return res;
416 }
417
427 public BitMatrix power(long p) {
428 if (c != r)
429 throw new IncompatibleDimensionException("Only square matrices can be raised to a power.");
430
431 if (p < 0)
432 throw new IllegalArgumentException("Only non-negative powers are accepted.");
433
434 if (p == 0) {
435 // the identity matrix
436 BitMatrix bm = new BitMatrix(r, r);
437 for (int i = 0; i < r; i++)
438 bm.setBool(i, i, true);
439 return bm;
440 }
441
442 if (p == 1)
443 return this;
444
445 if (p % 2 == 0) {
446 BitMatrix temp = this.power(p / 2);
447 return temp.multiply(temp);
448 } else
449 return this.multiply(this.power(p - 1));
450 }
451
460 public BitMatrix power2e(int e) {
461 if (c != r)
462 throw new IncompatibleDimensionException("Only square matrices can be raised to a power.");
463 BitMatrix result = this;
464
465 for (int i = 0; i < e; i++)
466 result = result.multiply(result);
467
468 return result;
469 }
470
474
479 public class IncompatibleDimensionException extends RuntimeException {
480 private IncompatibleDimensionException() {
481 super();
482 }
483
484 private IncompatibleDimensionException(String message) {
485 super(message);
486 }
487 }
488
489}
490
Runtime exception raised when the dimensions of the BitMatrix are not appropriate for the operation.
BitMatrix(BitMatrix that)
Copy constructor.
BitMatrix power(long p)
Raises the BitMatrix to the power p.
BitMatrix(int r, int c)
Creates a new BitMatrix with r rows and c columns filled with 0’s.
String printData()
Creates a String containing all the data of the BitMatrix.
Object clone()
Creates a copy of the BitMatrix.
int numColumns()
Returns the number of columns of the BitMatrix.
boolean equals(BitMatrix that)
Verifies that this and that are strictly identical.
void setBool(int row, int column, boolean value)
Changes the value of the bit in the specified row and column.
int numRows()
Returns the number of rows of the BitMatrix.
BitVector multiply(BitVector vect)
Multiplies the column BitVector by a BitMatrix and returns the result.
BitMatrix power2e(int e)
Raises the BitMatrix to power .
BitMatrix(BitVector[] rows)
Creates a new BitMatrix using the data in rows.
BitMatrix not()
Returns the BitMatrix resulting from the application of the not operator on the original BitMatrix.
BitMatrix(int[][] data, int r, int c)
Creates a new BitMatrix with r rows and c columns using the data in data.
BitMatrix xor(BitMatrix that)
Returns the BitMatrix resulting from the application of the xor operator on the original BitMatrix an...
BitMatrix multiply(BitMatrix that)
Multiplies two BitMatrix’s together.
String toString()
Creates a String containing all the data of the BitMatrix.
boolean getBool(int row, int column)
Returns the value of the bit in the specified row and column.
int multiply(int vect)
Multiplies vect, seen as a column BitVector, by a BitMatrix.
BitMatrix or(BitMatrix that)
Returns the BitMatrix resulting from the application of the or operator on the original BitMatrix and...
BitMatrix and(BitMatrix that)
Returns the BitMatrix resulting from the application of the and operator on the original BitMatrix an...
BitMatrix transpose()
Returns the transposed matrix.
This class implements vectors of bits and the operations needed to use them.
int getInt(int pos)
Returns an int containing all the bits in the interval.
BitVector selfNot()
Applies the not operator on the current BitVector and returns it.
BitVector selfXor(BitVector that)
Applies the xor operator on this with that.
void setBool(int pos, boolean value)
Sets the value of the bit in position pos.
BitVector selfOr(BitVector that)
Applies the or operator on this with that.
BitVector selfAnd(BitVector that)
Applies the and operator on this with that.
This class acts like a StringBuffer which defines new types of append methods.
static final String NEWLINE
End-of-line symbol or line separator.