cern.colt.matrix
Class ObjectMatrix1D

java.lang.Object
  extended by cern.colt.PersistentObject
      extended by cern.colt.matrix.impl.AbstractMatrix
          extended by cern.colt.matrix.impl.AbstractMatrix1D
              extended by cern.colt.matrix.ObjectMatrix1D
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable
Direct Known Subclasses:
DenseObjectMatrix1D, SparseObjectMatrix1D

public abstract class ObjectMatrix1D
extends AbstractMatrix1D

Abstract base class for 1-d matrices (aka vectors) holding Object elements. First see the package summary and javadoc tree view to get the broad picture.

A matrix has a number of cells (its size), which are assigned upon instance construction. Elements are accessed via zero based indexes. Legal indexes are of the form [0..size()-1]. Any attempt to access an element at a coordinate index<0 || index>=size() will throw an IndexOutOfBoundsException.

Author:
wolfgang.hoschek@cern.ch
See Also:
Serialized Form

Field Summary
 
Fields inherited from class cern.colt.matrix.impl.AbstractMatrix1D
size, stride, zero
 
Fields inherited from class cern.colt.matrix.impl.AbstractMatrix
isNoView
 
Fields inherited from class cern.colt.PersistentObject
serialVersionUID
 
Constructor Summary
protected ObjectMatrix1D()
          Makes this class non instantiable, but still let's others inherit from it.
 
Method Summary
 java.lang.Object aggregate(ObjectMatrix1D other, ObjectObjectFunction aggr, ObjectObjectFunction f)
          Applies a function to each corresponding cell of two matrices and aggregates the results.
 java.lang.Object aggregate(ObjectObjectFunction aggr, ObjectFunction f)
          Applies a function to each cell and aggregates the results.
 ObjectMatrix1D assign(java.lang.Object value)
          Sets all cells to the state specified by value.
 ObjectMatrix1D assign(java.lang.Object[] values)
          Sets all cells to the state specified by values.
 ObjectMatrix1D assign(ObjectFunction function)
          Assigns the result of a function to each cell; x[i] = function(x[i]).
 ObjectMatrix1D assign(ObjectMatrix1D other)
          Replaces all cell values of the receiver with the values of another matrix.
 ObjectMatrix1D assign(ObjectMatrix1D y, ObjectObjectFunction function)
          Assigns the result of a function to each cell; x[i] = function(x[i],y[i]).
 int cardinality()
          Returns the number of cells having non-zero values; ignores tolerance.
 ObjectMatrix1D copy()
          Constructs and returns a deep copy of the receiver.
 boolean equals(java.lang.Object otherObj)
          Compares the specified Object with the receiver for equality.
 boolean equals(java.lang.Object otherObj, boolean testForEquality)
          Compares the specified Object with the receiver for equality.
 java.lang.Object get(int index)
          Returns the matrix cell value at coordinate index.
protected  ObjectMatrix1D getContent()
          Returns the content of this matrix if it is a wrapper; or this otherwise.
 void getNonZeros(IntArrayList indexList, ObjectArrayList valueList)
          Fills the coordinates and values of cells having non-zero values into the specified lists.
abstract  java.lang.Object getQuick(int index)
          Returns the matrix cell value at coordinate index.
protected  boolean haveSharedCells(ObjectMatrix1D other)
          Returns true if both matrices share at least one identical cell.
protected  boolean haveSharedCellsRaw(ObjectMatrix1D other)
          Returns true if both matrices share at least one identical cell.
 ObjectMatrix1D like()
          Construct and returns a new empty matrix of the same dynamic type as the receiver, having the same size.
abstract  ObjectMatrix1D like(int size)
          Construct and returns a new empty matrix of the same dynamic type as the receiver, having the specified size.
abstract  ObjectMatrix2D like2D(int rows, int columns)
          Construct and returns a new 2-d matrix of the corresponding dynamic type, entirelly independent of the receiver.
 void set(int index, java.lang.Object value)
          Sets the matrix cell at coordinate index to the specified value.
abstract  void setQuick(int index, java.lang.Object value)
          Sets the matrix cell at coordinate index to the specified value.
 void swap(ObjectMatrix1D other)
          Swaps each element this[i] with other[i].
 java.lang.Object[] toArray()
          Constructs and returns a 1-dimensional array containing the cell values.
 void toArray(java.lang.Object[] values)
          Fills the cell values into the specified 1-dimensional array.
 java.lang.String toString()
          Returns a string representation using default formatting.
protected  ObjectMatrix1D view()
          Constructs and returns a new view equal to the receiver.
 ObjectMatrix1D viewFlip()
          Constructs and returns a new flip view.
 ObjectMatrix1D viewPart(int index, int width)
          Constructs and returns a new sub-range view that is a width sub matrix starting at index.
 ObjectMatrix1D viewSelection(int[] indexes)
          Constructs and returns a new selection view that is a matrix holding the indicated cells.
 ObjectMatrix1D viewSelection(ObjectProcedure condition)
          Constructs and returns a new selection view that is a matrix holding the cells matching the given condition.
protected abstract  ObjectMatrix1D viewSelectionLike(int[] offsets)
          Construct and returns a new selection view.
 ObjectMatrix1D viewSorted()
          Sorts the vector into ascending order, according to the natural ordering.
 ObjectMatrix1D viewStrides(int stride)
          Constructs and returns a new stride view which is a sub matrix consisting of every i-th cell.
 
Methods inherited from class cern.colt.matrix.impl.AbstractMatrix1D
_offset, _rank, checkIndex, checkIndexes, checkRange, checkSize, checkSize, index, setUp, setUp, size, stride, toStringShort, vFlip, vPart, vStrides
 
Methods inherited from class cern.colt.matrix.impl.AbstractMatrix
ensureCapacity, isView, trimToSize
 
Methods inherited from class cern.colt.PersistentObject
clone
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ObjectMatrix1D

protected ObjectMatrix1D()
Makes this class non instantiable, but still let's others inherit from it.

Method Detail

aggregate

public java.lang.Object aggregate(ObjectObjectFunction aggr,
                                  ObjectFunction f)
Applies a function to each cell and aggregates the results. Returns a value v such that v==a(size()) where a(i) == aggr( a(i-1), f(get(i)) ) and terminators are a(1) == f(get(0)), a(0)==null.

Example:

cern.jet.math.Functions F = cern.jet.math.Functions.functions;
matrix = 0 1 2 3 

// Sum( x[i]*x[i] ) 
matrix.aggregate(F.plus,F.square);
--> 14
For further examples, see the package doc.

Parameters:
aggr - an aggregation function taking as first argument the current aggregation and as second argument the transformed current cell value.
f - a function transforming the current cell value.
Returns:
the aggregated measure.

aggregate

public java.lang.Object aggregate(ObjectMatrix1D other,
                                  ObjectObjectFunction aggr,
                                  ObjectObjectFunction f)
Applies a function to each corresponding cell of two matrices and aggregates the results. Returns a value v such that v==a(size()) where a(i) == aggr( a(i-1), f(get(i),other.get(i)) ) and terminators are a(1) == f(get(0),other.get(0)), a(0)==null.

Example:

cern.jet.math.Functions F = cern.jet.math.Functions.functions;
x = 0 1 2 3 
y = 0 1 2 3 

// Sum( x[i]*y[i] )
x.aggregate(y, F.plus, F.mult);
--> 14

// Sum( (x[i]+y[i])^2 )
x.aggregate(y, F.plus, F.chain(F.square,F.plus));
--> 56
For further examples, see the package doc.

Parameters:
aggr - an aggregation function taking as first argument the current aggregation and as second argument the transformed current cell values.
f - a function transforming the current cell values.
Returns:
the aggregated measure.
Throws:
java.lang.IllegalArgumentException - if size() != other.size().

assign

public ObjectMatrix1D assign(java.lang.Object[] values)
Sets all cells to the state specified by values. values is required to have the same number of cells as the receiver.

The values are copied. So subsequent changes in values are not reflected in the matrix, and vice-versa.

Parameters:
values - the values to be filled into the cells.
Returns:
this (for convenience only).
Throws:
java.lang.IllegalArgumentException - if values.length != size().

assign

public ObjectMatrix1D assign(ObjectFunction function)
Assigns the result of a function to each cell; x[i] = function(x[i]). (Iterates downwards from [size()-1] to [0]).

Example:

// change each cell to its sine
matrix =   0.5      1.5      2.5       3.5 
matrix.assign(cern.jet.math.Functions.sin);
-->
matrix ==  0.479426 0.997495 0.598472 -0.350783
For further examples, see the package doc.

Parameters:
function - a function object taking as argument the current cell's value.
Returns:
this (for convenience only).
See Also:
Functions

assign

public ObjectMatrix1D assign(ObjectMatrix1D other)
Replaces all cell values of the receiver with the values of another matrix. Both matrices must have the same size. If both matrices share the same cells (as is the case if they are views derived from the same matrix) and intersect in an ambiguous way, then replaces as if using an intermediate auxiliary deep copy of other.

Parameters:
other - the source matrix to copy from (may be identical to the receiver).
Returns:
this (for convenience only).
Throws:
java.lang.IllegalArgumentException - if size() != other.size().

assign

public ObjectMatrix1D assign(ObjectMatrix1D y,
                             ObjectObjectFunction function)
Assigns the result of a function to each cell; x[i] = function(x[i],y[i]).

Example:

// assign x[i] = x[i]y[i]
m1 = 0 1 2 3;
m2 = 0 2 4 6;
m1.assign(m2, cern.jet.math.Functions.pow);
-->
m1 == 1 1 16 729
For further examples, see the package doc.

Parameters:
y - the secondary matrix to operate on.
function - a function object taking as first argument the current cell's value of this, and as second argument the current cell's value of y,
Returns:
this (for convenience only).
Throws:
java.lang.IllegalArgumentException - if size() != y.size().
See Also:
Functions

assign

public ObjectMatrix1D assign(java.lang.Object value)
Sets all cells to the state specified by value.

Parameters:
value - the value to be filled into the cells.
Returns:
this (for convenience only).

cardinality

public int cardinality()
Returns the number of cells having non-zero values; ignores tolerance.


copy

public ObjectMatrix1D copy()
Constructs and returns a deep copy of the receiver.

Note that the returned matrix is an independent deep copy. The returned matrix is not backed by this matrix, so changes in the returned matrix are not reflected in this matrix, and vice-versa.

Returns:
a deep copy of the receiver.

equals

public boolean equals(java.lang.Object otherObj)
Compares the specified Object with the receiver for equality. Equivalent to equals(otherObj,true).

Overrides:
equals in class java.lang.Object
Parameters:
otherObj - the Object to be compared for equality with the receiver.
Returns:
true if the specified Object is equal to the receiver.

equals

public boolean equals(java.lang.Object otherObj,
                      boolean testForEquality)
Compares the specified Object with the receiver for equality. Returns true if and only if the specified Object is also at least an ObjectMatrix1D, both matrices have the same size, and all corresponding pairs of cells in the two matrices are the same. In other words, two matrices are defined to be equal if they contain the same cell values in the same order. Tests elements for equality or identity as specified by testForEquality. When testing for equality, two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).)

Parameters:
otherObj - the Object to be compared for equality with the receiver.
testForEquality - if true -> tests for equality, otherwise for identity.
Returns:
true if the specified Object is equal to the receiver.

get

public java.lang.Object get(int index)
Returns the matrix cell value at coordinate index.

Parameters:
index - the index of the cell.
Returns:
the value of the specified cell.
Throws:
java.lang.IndexOutOfBoundsException - if index<0 || index>=size().

getContent

protected ObjectMatrix1D getContent()
Returns the content of this matrix if it is a wrapper; or this otherwise. Override this method in wrappers.


getNonZeros

public void getNonZeros(IntArrayList indexList,
                        ObjectArrayList valueList)
Fills the coordinates and values of cells having non-zero values into the specified lists. Fills into the lists, starting at index 0. After this call returns the specified lists all have a new size, the number of non-zero values.

In general, fill order is unspecified. This implementation fills like: for (index = 0..size()-1) do ... . However, subclasses are free to us any other order, even an order that may change over time as cell values are changed. (Of course, result lists indexes are guaranteed to correspond to the same cell).

Example:

0, 0, 8, 0, 7
-->
indexList  = (2,4)
valueList  = (8,7)
In other words, get(2)==8, get(4)==7.

Parameters:
indexList - the list to be filled with indexes, can have any size.
valueList - the list to be filled with values, can have any size.

getQuick

public abstract java.lang.Object getQuick(int index)
Returns the matrix cell value at coordinate index.

Provided with invalid parameters this method may return invalid objects without throwing any exception. You should only use this method when you are absolutely sure that the coordinate is within bounds. Precondition (unchecked): index<0 || index>=size().

Parameters:
index - the index of the cell.
Returns:
the value of the specified cell.

haveSharedCells

protected boolean haveSharedCells(ObjectMatrix1D other)
Returns true if both matrices share at least one identical cell.


haveSharedCellsRaw

protected boolean haveSharedCellsRaw(ObjectMatrix1D other)
Returns true if both matrices share at least one identical cell.


like

public ObjectMatrix1D like()
Construct and returns a new empty matrix of the same dynamic type as the receiver, having the same size. For example, if the receiver is an instance of type DenseObjectMatrix1D the new matrix must also be of type DenseObjectMatrix1D, if the receiver is an instance of type SparseObjectMatrix1D the new matrix must also be of type SparseObjectMatrix1D, etc. In general, the new matrix should have internal parametrization as similar as possible.

Returns:
a new empty matrix of the same dynamic type.

like

public abstract ObjectMatrix1D like(int size)
Construct and returns a new empty matrix of the same dynamic type as the receiver, having the specified size. For example, if the receiver is an instance of type DenseObjectMatrix1D the new matrix must also be of type DenseObjectMatrix1D, if the receiver is an instance of type SparseObjectMatrix1D the new matrix must also be of type SparseObjectMatrix1D, etc. In general, the new matrix should have internal parametrization as similar as possible.

Parameters:
size - the number of cell the matrix shall have.
Returns:
a new empty matrix of the same dynamic type.

like2D

public abstract ObjectMatrix2D like2D(int rows,
                                      int columns)
Construct and returns a new 2-d matrix of the corresponding dynamic type, entirelly independent of the receiver. For example, if the receiver is an instance of type DenseObjectMatrix1D the new matrix must be of type DenseObjectMatrix2D, if the receiver is an instance of type SparseObjectMatrix1D the new matrix must be of type SparseObjectMatrix2D, etc.

Parameters:
rows - the number of rows the matrix shall have.
columns - the number of columns the matrix shall have.
Returns:
a new matrix of the corresponding dynamic type.

set

public void set(int index,
                java.lang.Object value)
Sets the matrix cell at coordinate index to the specified value.

Parameters:
index - the index of the cell.
value - the value to be filled into the specified cell.
Throws:
java.lang.IndexOutOfBoundsException - if index<0 || index>=size().

setQuick

public abstract void setQuick(int index,
                              java.lang.Object value)
Sets the matrix cell at coordinate index to the specified value.

Provided with invalid parameters this method may access illegal indexes without throwing any exception. You should only use this method when you are absolutely sure that the coordinate is within bounds. Precondition (unchecked): index<0 || index>=size().

Parameters:
index - the index of the cell.
value - the value to be filled into the specified cell.

swap

public void swap(ObjectMatrix1D other)
Swaps each element this[i] with other[i].

Throws:
java.lang.IllegalArgumentException - if size() != other.size().

toArray

public java.lang.Object[] toArray()
Constructs and returns a 1-dimensional array containing the cell values. The values are copied. So subsequent changes in values are not reflected in the matrix, and vice-versa. The returned array values has the form
for (int i=0; i < size(); i++) values[i] = get(i);

Returns:
an array filled with the values of the cells.

toArray

public void toArray(java.lang.Object[] values)
Fills the cell values into the specified 1-dimensional array. The values are copied. So subsequent changes in values are not reflected in the matrix, and vice-versa. After this call returns the array values has the form
for (int i=0; i < size(); i++) values[i] = get(i);

Throws:
java.lang.IllegalArgumentException - if values.length < size().

toString

public java.lang.String toString()
Returns a string representation using default formatting.

Overrides:
toString in class java.lang.Object
See Also:
Formatter

view

protected ObjectMatrix1D view()
Constructs and returns a new view equal to the receiver. The view is a shallow clone. Calls clone() and casts the result.

Note that the view is not a deep copy. The returned matrix is backed by this matrix, so changes in the returned matrix are reflected in this matrix, and vice-versa.

Use copy() to construct an independent deep copy rather than a new view.

Returns:
a new view of the receiver.

viewFlip

public ObjectMatrix1D viewFlip()
Constructs and returns a new flip view. What used to be index 0 is now index size()-1, ..., what used to be index size()-1 is now index 0. The returned view is backed by this matrix, so changes in the returned view are reflected in this matrix, and vice-versa.

Returns:
a new flip view.

viewPart

public ObjectMatrix1D viewPart(int index,
                               int width)
Constructs and returns a new sub-range view that is a width sub matrix starting at index. Operations on the returned view can only be applied to the restricted range. Any attempt to access coordinates not contained in the view will throw an IndexOutOfBoundsException.

Note that the view is really just a range restriction: The returned matrix is backed by this matrix, so changes in the returned matrix are reflected in this matrix, and vice-versa.

The view contains the cells from index..index+width-1. and has view.size() == width. A view's legal coordinates are again zero based, as usual. In other words, legal coordinates of the view are 0 .. view.size()-1==width-1. As usual, any attempt to access a cell at other coordinates will throw an IndexOutOfBoundsException.

Parameters:
index - The index of the first cell.
width - The width of the range.
Returns:
the new view.
Throws:
java.lang.IndexOutOfBoundsException - if index<0 || width<0 || index+width>size().

viewSelection

public ObjectMatrix1D viewSelection(int[] indexes)
Constructs and returns a new selection view that is a matrix holding the indicated cells. There holds view.size() == indexes.length and view.get(i) == this.get(indexes[i]). Indexes can occur multiple times and can be in arbitrary order.

Example:

this     = (0,0,8,0,7)
indexes  = (0,2,4,2)
-->
view     = (0,8,7,8)
Note that modifying indexes after this call has returned has no effect on the view. The returned view is backed by this matrix, so changes in the returned view are reflected in this matrix, and vice-versa.

Parameters:
indexes - The indexes of the cells that shall be visible in the new view. To indicate that all cells shall be visible, simply set this parameter to null.
Returns:
the new view.
Throws:
java.lang.IndexOutOfBoundsException - if !(0 <= indexes[i] < size()) for any i=0..indexes.length()-1.

viewSelection

public ObjectMatrix1D viewSelection(ObjectProcedure condition)
Constructs and returns a new selection view that is a matrix holding the cells matching the given condition. Applies the condition to each cell and takes only those cells where condition.apply(get(i)) yields true.

Example:

// extract and view all cells with even value
matrix = 0 1 2 3 
matrix.viewSelection( 
   new ObjectProcedure() {
      public final boolean apply(Object a) { return a % 2 == 0; }
   }
);
-->
matrix ==  0 2
For further examples, see the package doc. The returned view is backed by this matrix, so changes in the returned view are reflected in this matrix, and vice-versa.

Parameters:
condition - The condition to be matched.
Returns:
the new view.

viewSelectionLike

protected abstract ObjectMatrix1D viewSelectionLike(int[] offsets)
Construct and returns a new selection view.

Parameters:
offsets - the offsets of the visible elements.
Returns:
a new view.

viewSorted

public ObjectMatrix1D viewSorted()
Sorts the vector into ascending order, according to the natural ordering. This sort is guaranteed to be stable. For further information, see Sorting.sort(ObjectMatrix1D). For more advanced sorting functionality, see Sorting.

Returns:
a new sorted vector (matrix) view.

viewStrides

public ObjectMatrix1D viewStrides(int stride)
Constructs and returns a new stride view which is a sub matrix consisting of every i-th cell. More specifically, the view has size this.size()/stride holding cells this.get(i*stride) for all i = 0..size()/stride - 1.

Parameters:
stride - the step factor.
Returns:
the new view.
Throws:
java.lang.IndexOutOfBoundsException - if stride <= 0.