24package umontreal.ssj.stat.matrix;
26import umontreal.ssj.util.PrintfFormat;
27import umontreal.ssj.stat.StatProbe;
28import cern.colt.matrix.DoubleMatrix2D;
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;
61 private List<MatrixOfObservationListener> listeners =
new ArrayList<MatrixOfObservationListener>();
62 protected boolean collect =
true;
63 protected boolean broadcast =
false;
64 protected String name;
67 private int numColumns;
68 private int modCount = 0;
84 createProbes(numRows, numColumns);
99 createProbes(numRows, numColumns);
103 private void createProbes(
int numRows,
int numColumns) {
105 throw new NegativeArraySizeException(
"The number of rows must not be negative");
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;
166 throw new NegativeArraySizeException(
"The given number of rows is negative");
167 if (
rows() == newRows)
170 int m = Math.
min(
rows(), newRows);
171 System.arraycopy(probes, 0, newProbes, 0, m *
columns());
187 throw new IllegalArgumentException(
"The given number of columns is negative");
193 for (
int r = 0; r <
rows(); r++)
194 System.arraycopy(probes,
columns() * r, newProbes, newColumns * r, m);
196 numColumns = newColumns;
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];
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;
249 for (
int r = 0; r <
rows; r++)
250 for (
int c = 0; c <
columns; c++)
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());
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++) {
274 m.setQuick(r, c, probe ==
null ? Double.NaN : probe.
sum());
290 if (m.rows() !=
rows())
291 throw new IllegalArgumentException(
292 "Invalid number of rows in the given matrix: required " +
rows() +
" but found " + m.rows());
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++) {
299 m.setQuick(r, c, probe ==
null ? Double.NaN : probe.
average());
352 throw new NullPointerException();
353 if (!listeners.contains(l))
383 final int nl = listeners.size();
384 for (
int i = 0; i < nl; i++)
385 listeners.get(i).newMatrixOfObservations(
this, x);
397 return new MyList<E>(
this,
ListType.ROW, r);
409 return new MyList<E>(
this,
ListType.COLUMN, c);
444 }
catch (CloneNotSupportedException cne) {
445 throw new IllegalStateException(
"CloneNotSupportedException for a class implementing Cloneable");
448 sm.probes = (E[]) probes.clone();
452 private class MyIterator
implements Iterator<E> {
453 private int index = 0;
454 private int expectedModCount = modCount;
456 public boolean hasNext() {
457 if (modCount != expectedModCount)
458 throw new ConcurrentModificationException();
459 return (index < probes.length - 1);
464 throw new NoSuchElementException();
465 return probes[index++];
468 public void remove() {
469 throw new UnsupportedOperationException(
"Can not remove an element in a matrix");
473 public Iterator<E> iterator() {
474 return new MyIterator();
477 private static class MyList<E
extends StatProbe> extends AbstractList<E> implements RandomAccess {
478 private MatrixOfStatProbes<E> matrix;
479 private ListType type;
482 public MyList(MatrixOfStatProbes<E> matrix, ListType type,
int index) {
483 this.matrix = matrix;
488 public E
get(
int index) {
489 if (type == ListType.ROW) {
490 return matrix.get(this.index, index);
492 return matrix.get(index, this.index);
497 if (type == ListType.ROW) {
498 return matrix.numColumns;
500 return matrix.numRows;
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 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 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.