SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
MatrixOfStatProbes.java
1/*
2 * Class: MatrixOfStatProbes
3 * Description: Matrix of statistical probes
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 Éric Buist
9 * @since 2006
10
11 * SSJ is free software: you can redistribute it and/or modify it under
12 * the terms of the GNU General Public License (GPL) as published by the
13 * Free Software Foundation, either version 3 of the License, or
14 * any later version.
15
16 * SSJ is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20
21 * A copy of the GNU General Public License is available at
22 <a href="http://www.gnu.org/licenses">GPL licence site</a>.
23 */
24package umontreal.ssj.stat.matrix;
25
26import umontreal.ssj.util.PrintfFormat;
27import umontreal.ssj.stat.StatProbe;
28import cern.colt.matrix.DoubleMatrix2D;
29import java.util.List;
30import java.util.ArrayList;
31import java.util.AbstractList;
32import java.util.Iterator;
33import java.util.ConcurrentModificationException;
34import java.util.NoSuchElementException;
35import java.util.Collections;
36import java.util.RandomAccess;
37
60public class MatrixOfStatProbes<E extends StatProbe> implements Cloneable, Iterable<E> {
61 private List<MatrixOfObservationListener> listeners = new ArrayList<MatrixOfObservationListener>();
62 protected boolean collect = true;
63 protected boolean broadcast = false;
64 protected String name;
65 private E[] probes;
66 private int numRows;
67 private int numColumns;
68 private int modCount = 0;
69
70 private static enum ListType {
71 ROW, COLUMN
72 }
73
83 public MatrixOfStatProbes(int numRows, int numColumns) {
84 createProbes(numRows, numColumns);
85 }
86
97 public MatrixOfStatProbes(String name, int numRows, int numColumns) {
98 this.name = name;
99 createProbes(numRows, numColumns);
100 }
101
102 // @SuppressWarnings("unchecked")
103 private void createProbes(int numRows, int numColumns) {
104 if (numRows < 0)
105 throw new NegativeArraySizeException("The number of rows must not be negative");
106 if (numColumns < 0)
107 throw new NegativeArraySizeException("The number of columns must not be negative");
108 this.numRows = numRows;
109 this.numColumns = numColumns;
110 int length = numRows * numColumns;
111
112 probes = (E[]) new StatProbe[length];
113 }
114
120 public String getName() {
121 return name;
122 }
123
129 public void setName(String name) {
130 this.name = name;
131 }
132
138 public int rows() {
139 return numRows;
140 }
141
147 public int columns() {
148 return numColumns;
149 }
150
164 public void setRows(int newRows) {
165 if (newRows < 0)
166 throw new NegativeArraySizeException("The given number of rows is negative");
167 if (rows() == newRows)
168 return;
169 E[] newProbes = (E[]) new StatProbe[newRows * columns()];
170 int m = Math.min(rows(), newRows);
171 System.arraycopy(probes, 0, newProbes, 0, m * columns());
172 probes = newProbes;
173 numRows = newRows;
174 ++modCount;
175 }
176
185 public void setColumns(int newColumns) {
186 if (newColumns < 0)
187 throw new IllegalArgumentException("The given number of columns is negative");
188 if (columns() == newColumns)
189 return;
190 // E[] newProbes = (E[])new StatProbe[probeClass, rows()*newColumns];
191 E[] newProbes = (E[]) new StatProbe[rows() * newColumns];
192 int m = Math.min(columns(), newColumns);
193 for (int r = 0; r < rows(); r++)
194 System.arraycopy(probes, columns() * r, newProbes, newColumns * r, m);
195 probes = newProbes;
196 numColumns = newColumns;
197 ++modCount;
198 }
199
213 public E get(int r, int c) {
214 if (r < 0 || r >= numRows)
215 throw new ArrayIndexOutOfBoundsException("Row index out of bounds: " + r);
216 if (c < 0 || c >= numColumns)
217 throw new ArrayIndexOutOfBoundsException("Column index out of bounds: " + c);
218 return probes[numColumns * r + c];
219 }
220
233 public void set(int r, int c, E probe) {
234 if (r < 0 || r >= numRows)
235 throw new ArrayIndexOutOfBoundsException("Row index out of bounds: " + r);
236 if (c < 0 || c >= numColumns)
237 throw new ArrayIndexOutOfBoundsException("Column index out of bounds: " + c);
238 probes[numColumns * r + c] = probe;
239 ++modCount;
240 }
241
246 public void init() {
247 int rows = rows();
248 int columns = columns();
249 for (int r = 0; r < rows; r++)
250 for (int c = 0; c < columns; c++)
251 get(r, c).init();
252 }
253
264 public void sum(DoubleMatrix2D m) {
265 if (m.rows() != rows())
266 throw new IllegalArgumentException(
267 "Invalid number of rows in the given matrix: required " + rows() + " but found " + m.rows());
268 if (m.columns() != columns())
269 throw new IllegalArgumentException(
270 "Invalid number of columns in the given matrix: required " + columns() + " but found " + m.columns());
271 for (int r = 0; r < rows(); r++)
272 for (int c = 0; c < columns(); c++) {
273 StatProbe probe = get(r, c);
274 m.setQuick(r, c, probe == null ? Double.NaN : probe.sum());
275 }
276 }
277
289 public void average(DoubleMatrix2D m) {
290 if (m.rows() != rows())
291 throw new IllegalArgumentException(
292 "Invalid number of rows in the given matrix: required " + rows() + " but found " + m.rows());
293 if (m.columns() != columns())
294 throw new IllegalArgumentException(
295 "Invalid number of columns in the given matrix: required " + columns() + " but found " + m.columns());
296 for (int r = 0; r < rows(); r++)
297 for (int c = 0; c < columns(); c++) {
298 StatProbe probe = get(r, c);
299 m.setQuick(r, c, probe == null ? Double.NaN : probe.average());
300 }
301 }
302
309 public boolean isCollecting() {
310 return collect;
311 }
312
319 public void setCollecting(boolean c) {
320 collect = c;
321 }
322
329 public boolean isBroadcasting() {
330 return broadcast;
331 }
332
339 public void setBroadcasting(boolean b) {
340 broadcast = b;
341 }
342
351 if (l == null)
352 throw new NullPointerException();
353 if (!listeners.contains(l))
354 listeners.add(l);
355 }
356
364 listeners.remove(l);
365 }
366
372 listeners.clear();
373 }
374
379 public void notifyListeners(DoubleMatrix2D x) {
380 if (!broadcast)
381 return;
382 // We could also use the enhanced for loop here, but this is less efficient.
383 final int nl = listeners.size();
384 for (int i = 0; i < nl; i++)
385 listeners.get(i).newMatrixOfObservations(this, x);
386 }
387
396 public List<E> viewRow(int r) {
397 return new MyList<E>(this, ListType.ROW, r);
398 }
399
408 public List<E> viewColumn(int c) {
409 return new MyList<E>(this, ListType.COLUMN, c);
410 }
411
420 public String rowReport(int r) {
421 return StatProbe.report(getName(), viewRow(r));
422 }
423
432 public String columnReport(int c) {
433 return StatProbe.report(getName(), viewColumn(c));
434 }
435
442 try {
443 sm = (MatrixOfStatProbes<E>) super.clone();
444 } catch (CloneNotSupportedException cne) {
445 throw new IllegalStateException("CloneNotSupportedException for a class implementing Cloneable");
446 }
447 if (probes != null)
448 sm.probes = (E[]) probes.clone();
449 return sm;
450 }
451
452 private class MyIterator implements Iterator<E> {
453 private int index = 0;
454 private int expectedModCount = modCount;
455
456 public boolean hasNext() {
457 if (modCount != expectedModCount)
458 throw new ConcurrentModificationException();
459 return (index < probes.length - 1);
460 }
461
462 public E next() {
463 if (!hasNext())
464 throw new NoSuchElementException();
465 return probes[index++];
466 }
467
468 public void remove() {
469 throw new UnsupportedOperationException("Can not remove an element in a matrix");
470 }
471 }
472
473 public Iterator<E> iterator() {
474 return new MyIterator();
475 }
476
477 private static class MyList<E extends StatProbe> extends AbstractList<E> implements RandomAccess {
478 private MatrixOfStatProbes<E> matrix;
479 private ListType type;
480 private int index;
481
482 public MyList(MatrixOfStatProbes<E> matrix, ListType type, int index) {
483 this.matrix = matrix;
484 this.type = type;
485 this.index = index;
486 }
487
488 public E get(int index) {
489 if (type == ListType.ROW) {
490 return matrix.get(this.index, index);
491 } else {
492 return matrix.get(index, this.index);
493 }
494 }
495
496 public int size() {
497 if (type == ListType.ROW) {
498 return matrix.numColumns;
499 } else {
500 return matrix.numRows;
501 }
502 }
503 }
504
505}
The objects of this class are statistical probes or collectors, which are elementary devices for coll...
abstract double average()
Returns the average for this collector.
double min()
Returns the smallest value taken by the variable since the last initialization of this probe.
abstract String report()
Returns a string containing a report for this statistical collector.
double sum()
Returns the sum cumulated so far for this probe.
void removeMatrixOfObservationListener(MatrixOfObservationListener l)
Removes the observation listener l from the list of observers of this matrix of statistical probes.
void setCollecting(boolean c)
Sets the status of the statistical collecting mechanism to c.
boolean isBroadcasting()
Determines if this matrix of statistical probes is broadcasting values to registered observers.
int rows()
Returns the number of rows in this matrix.
void setName(String name)
Sets the global name of this matrix to name.
int columns()
Returns the number of columns in this matrix.
void average(DoubleMatrix2D m)
For each statistical probe in the matrix, computes the average by calling umontreal....
List< E > viewRow(int r)
Returns a list representing a view on row r of this matrix of statistical probe.
List< E > viewColumn(int c)
Returns a list representing a view on column c of this matrix of statistical probe.
void setColumns(int newColumns)
Similar to setRows(int), for setting the number of columns.
MatrixOfStatProbes(int numRows, int numColumns)
Constructs a new unnamed matrix of statistical probes with numRows rows, and numColumns columns,...
String rowReport(int r)
Formats a report for the row&#160;r of the statistical probe matrix.
boolean isCollecting()
Determines if this matrix of statistical probes is collecting values.
void setBroadcasting(boolean b)
Sets the status of the observation broadcasting mechanism to b.
void clearMatrixOfObservationListeners()
Removes all observation listeners from the list of observers of this matrix of statistical probes.
void setRows(int newRows)
Sets the number of rows of this matrix of statistical probes to newRows, adding or removing cells as ...
void init()
Initializes this matrix of statistical probes by calling StatProbe.init on each element.
void notifyListeners(DoubleMatrix2D x)
Notifies the observation x to all registered observers if broadcasting is ON.
MatrixOfStatProbes< E > clone()
Clones this object.
String columnReport(int c)
Formats a report for the column&#160;c of the statistical probe matrix.
void sum(DoubleMatrix2D m)
For each probe in the matrix, computes the sum by calling umontreal.ssj.stat.StatProbe....
MatrixOfStatProbes(String name, int numRows, int numColumns)
Constructs a new matrix of statistical probes with name name, numRows rows, and numColumns columns,...
String getName()
Returns the global name of this matrix of statistical probes.
void addMatrixOfObservationListener(MatrixOfObservationListener l)
Adds the observation listener l to the list of observers of this matrix of statistical probes.
Represents an object that can listen to observations broadcast by matrices of statistical probes.