agi.foundation.coordinates
(agi.foundation.core-2019r1.jar)

## Class DenseMatrix

• ### Method Summary

All Methods
Modifier and Type Method and Description
`protected boolean` `checkForSameDefinition(DenseMatrix other)`
Checks to determine if another instance has the same definition as this instance and returns `true` if it does.
`protected boolean` `checkForSameDefinition(Matrix other)`
Checks to determine if another instance has the same definition as this instance and returns `true` if it does.
`void` `clear()`
Sets the value of every value in the matrix to zero.
`Object` `clone(CopyContext context)`
Clones this object using the specified context.
`protected int` `computeCurrentDefinitionHashCode()`
Computes a hash code based on the current properties of this object.
`boolean` `equals(Object obj)`
Indicates whether another object is exactly equal to this instance.
`double` ```get(int rowIndex, int columnIndex)```
Gets the value at the specified location in the matrix.
`int` `hashCode()`
Returns a hash code for this instance, which is suitable for use in hashing algorithms and data structures like a hash table.
`static DenseMatrix` ```identity(int m, int n)```
Generate identity matrix
`static double[]` ```multiply(DenseMatrix matrix, double[] vector)```
Returns the result of the multiplication of a matrix and a vector.
`Matrix` `multiply(double factor)`
Creates a new `Matrix` which has the value of the current matrix multiplied by the given factor.
`double[]` `multiply(double[] vector)`
Returns the result of the multiplication of this matrix and a vector.
`DenseMatrix` `multiplyAssign(double s)`
Multiply a matrix by a scalar in place, A = s*A
`void` ```set(int rowIndex, int columnIndex, double value)```
Sets the value at the specified location in the matrix.
`double[]` `solve(double[] vectorB)`
Solve A*X = B
`Matrix` `transpose()`
Creates a new `Matrix` which is the transpose of the current matrix.
• ### Methods inherited from class agi.foundation.coordinates.Matrix

`add, add, add, add, add, addAssign, checkForSameDefinition, elementDivide, elementDivide, elementDivide, elementDivide, elementDivide, elementDivideAssign, elementMultiply, elementMultiply, elementMultiply, elementMultiply, elementMultiply, elementMultiplyAssign, equals, equalsEpsilon, getColumnDimension, getFrozenCopy, getMatrix, getMatrix, getMatrix, getMatrix, getMatrix, getMatrix, getMatrix, getMatrix, getRowDimension, multiply, multiply, multiply, multiply, multiply, multiply, multiply, multiplyAssign, setMatrix, setMatrix, setMatrix, setMatrix, setMatrix, subtract, subtract, subtract, subtract, subtract, subtractAssign, toString`
• ### Methods inherited from class agi.foundation.infrastructure.DefinitionalObject

`areSameDefinition, areSameDefinition, areSameDefinition, areSameDefinition, areSameDefinition, collectionItemsAreSameDefinition, collectionItemsAreSameDefinition, dictionaryItemsAreSameDefinition, enumerateDependencies, freeze, freezeAggregatedObjects, getCollectionHashCode, getDefinitionHashCode, getDefinitionHashCode, getDefinitionHashCode, getDefinitionHashCode, getDefinitionHashCode, getDefinitionHashCode, getDictionaryHashCode, getIsFrozen, isSameDefinition, throwIfFrozen`
• ### Methods inherited from class java.lang.Object

`clone, finalize, getClass, notify, notifyAll, wait, wait, wait`
• ### Constructor Detail

• #### DenseMatrix

```public DenseMatrix(int rowDimension,
int columnDimension)```
Initializes a new `DenseMatrix` with the specified row and column dimensions. All elements of the matrix will be set to zero.
Parameters:
`rowDimension` - The number of rows in the matrix.
`columnDimension` - The number of columns in the matrix.
Throws:
`IllegalStateException` - Thrown if the `rowDimension` or `columnDimension` are less than zero.
• #### DenseMatrix

```public DenseMatrix(int rowDimension,
int columnDimension,
double elementValue)```
Initializes a new `DenseMatrix` with the specified row and column dimensions. All elements of the matrix will be set to the provided double.
Parameters:
`rowDimension` - The number of rows in the matrix.
`columnDimension` - The number of columns in the matrix.
`elementValue` - The value that all elements in the matrix will be set to.
Throws:
`IllegalStateException` - Thrown if the `rowDimension` or `columnDimension` are less than zero.
• #### DenseMatrix

`public DenseMatrix(double[][] array)`
Initialize a new `DenseMatrix` from a 2-D array.
Parameters:
`array` - Two-dimensional array of doubles.
Throws:
`IllegalStateException` - Throw if all rows are not the same length.
• #### DenseMatrix

```public DenseMatrix(double[][] array,
int rowDimension,
int columnDimension)```
Construct a matrix quickly without validating arguments.
Parameters:
`array` - Rectangular two-dimensional array of doubles.
`rowDimension` - Number of rows, this must match the first dimension of the given array.
`columnDimension` - Number of columns, this must match the second dimensions of the given array.
• #### DenseMatrix

```public DenseMatrix(double[] elements,
int rowDimension)```
Initialize a new `DenseMatrix` from a one-dimensional packed array
Parameters:
`elements` - One-dimensional array of doubles containing the values of all of the elements.
`rowDimension` - The number of rows in the matrix.
Throws:
`IllegalStateException` - Thrown if the `rowDimension` is less than zero.
`IllegalStateException` - Thrown if the `elements` array length is not a multiple of the column dimension.
• ### Method Detail

• #### clone

`public Object clone(CopyContext context)`
Clones this object using the specified context.

This method should be implemented to call a copy constructor on the class of the object being cloned. The copy constructor should take the `CopyContext` as a parameter in addition to the existing instance to copy. The copy constructor should first call `CopyContext.addObjectMapping(T, T)` to identify the newly constructed instance as a copy of the existing instance. It should then copy all fields, using `CopyContext.updateReference(T)` to copy any reference fields.

A typical implementation of `ICloneWithContext`:

``````public static class MyClass implements ICloneWithContext {
public MyClass(MyClass existingInstance, CopyContext context) {
someReference = context.updateReference(existingInstance.someReference);
}

@Override
public final Object clone(CopyContext context) {
return new MyClass(this, context);
}

private Object someReference;
}``````

In general, all fields that are reference types should be copied with a call to `CopyContext.updateReference(T)`. There are a couple of exceptions:

1. Fields that are aggregate parts of the object should always be copied. A referenced object is an aggregate if properties or methods on the object being copied can modify the externally-visible value of the referenced object.
2. If the semantics of the referenced object require that it have a single parent, owner, context, etc., and the object being copied is that parent, owner, or context, then the referenced object should always be copied.
If one of these exceptions applies, the `CopyContext` should be given an opportunity to update the reference before the reference is copied explicitly. Use `CopyContext.updateReference(T)` to update the reference. If `CopyContext.updateReference(T)` returns the original object (that is, if the reference some field is equal to the reference of the same field of another instance)) then copy the object manually by invoking a Clone method, a copy constructor, or by manually constructing a new instance and copying the values.

``````alwaysCopy = context.updateReference(existingInstance.alwaysCopy);
if (existingInstance.alwaysCopy != null && alwaysCopy == existingInstance.alwaysCopy) {
alwaysCopy = (AlwaysCopy) existingInstance.alwaysCopy.clone(context);
}``````

If you are implementing an evaluator (a class that implements `IEvaluator`), the `IEvaluator.updateEvaluatorReferences(agi.foundation.infrastructure.CopyContext)` method shares some responsibilities with the copy context constructor. Code duplication can be avoided by doing the following:

1. For references to other evaluators ONLY, simply assign the reference in the constructor instead of calling `CopyContext.updateReference(T)`. You should still call `CopyContext.updateReference(T)` on any references to non-evaluators.
2. Call `IEvaluator.updateEvaluatorReferences(agi.foundation.infrastructure.CopyContext)` as the last line in the constructor and pass it the same `CopyContext` passed to the constructor.
3. Implement `IEvaluator.updateEvaluatorReferences(agi.foundation.infrastructure.CopyContext)` as normal. See the reference documentation for `IEvaluator.updateEvaluatorReferences(agi.foundation.infrastructure.CopyContext)` for more information on implementing that method.

``````public MyClass(MyClass existingInstance, CopyContext context) {
super(existingInstance, context);
someReference = context.updateReference(existingInstance.someReference);
evaluatorReference = existingInstance.evaluatorReference;
updateEvaluatorReferences(context);
}

@Override
public void updateEvaluatorReferences(CopyContext context) {
evaluatorReference = context.updateReference(evaluatorReference);
}

@Override
public Object clone(CopyContext context) {
return new MyClass(this, context);
}

private Object someReference;
private IEvaluator evaluatorReference;``````
Specified by:
`clone` in interface `ICloneWithContext`
Specified by:
`clone` in class `DefinitionalObject`
Parameters:
`context` - The context to use to perform the copy.
Returns:
A new instance which is a copy of this object.
• #### checkForSameDefinition

`protected final boolean checkForSameDefinition(Matrix other)`
Checks to determine if another instance has the same definition as this instance and returns `true` if it does. Derived classes MUST override this method and check all new fields introduced by the derived class for definitional equivalence. It is NOT necessary to check base class fields because the base class will already have done that. When overriding this method, you should NOT call the base implementation because it will return `false` for all derived-class instances. Derived classes should check the type of `other` to preserve the symmetric nature of `IEquatableDefinition.isSameDefinition(java.lang.Object)`.
Specified by:
`checkForSameDefinition` in class `Matrix`
Parameters:
`other` - The other instance to compare to this one.
Returns:
`true` if the two objects are defined equivalently, otherwise `false`.
• #### checkForSameDefinition

`protected boolean checkForSameDefinition(DenseMatrix other)`
Checks to determine if another instance has the same definition as this instance and returns `true` if it does. Derived classes MUST override this method and check all new fields introduced by the derived class for definitional equivalence. It is NOT necessary to check base class fields because the base class will already have done that. When overriding this method, you should NOT call the base implementation because it will return `false` for all derived-class instances. Derived classes should check the type of `other` to preserve the symmetric nature of `IEquatableDefinition.isSameDefinition(java.lang.Object)`.
Parameters:
`other` - The other instance to compare to this one.
Returns:
`true` if the two objects are defined equivalently, otherwise `false`.
• #### computeCurrentDefinitionHashCode

`protected int computeCurrentDefinitionHashCode()`
Computes a hash code based on the current properties of this object. Derived classes MUST override this method and compute a hash code that combines: a unique hash code seed, the base implementation result, and the hash codes of all new fields introduced by the derived class which are used in the `DenseMatrix.checkForSameDefinition(agi.foundation.coordinates.Matrix)` method.
Overrides:
`computeCurrentDefinitionHashCode` in class `Matrix`
Returns:
The computed hash code.
• #### set

```public void set(int rowIndex,
int columnIndex,
double value)```
Sets the value at the specified location in the matrix.
Specified by:
`set` in class `Matrix`
Parameters:
`rowIndex` - The row index of the element being queried.
`columnIndex` - The column index of the element being queried.
Throws:
`IndexOutOfBoundsException` - Thrown if `rowIndex` is greater than `RowDimension` (`get`), `columnIndex` is greater than `ColumnDimension` (`get`), or either are less than zero.
• #### get

```public double get(int rowIndex,
int columnIndex)```
Gets the value at the specified location in the matrix.
Specified by:
`get` in class `Matrix`
Parameters:
`rowIndex` - The row index of the element being queried.
`columnIndex` - The column index of the element being queried.
Returns:
The value in the matrix.
Throws:
`IndexOutOfBoundsException` - Thrown if `rowIndex` is greater than `RowDimension` (`get`), `columnIndex` is greater than `ColumnDimension` (`get`), or either are less than zero.
• #### equals

`public boolean equals(Object obj)`
Indicates whether another object is exactly equal to this instance.
Overrides:
`equals` in class `Object`
Parameters:
`obj` - The object to compare to this instance.
Returns:
`true` if `obj` is an instance of this type and represents the same value as this instance; otherwise, `false`.
`Object.hashCode()`, `HashMap`
• #### clear

`public final void clear()`
Sets the value of every value in the matrix to zero.
• #### transpose

`public Matrix transpose()`
Creates a new `Matrix` which is the transpose of the current matrix.
Specified by:
`transpose` in class `Matrix`
Returns:
The new matrix.
• #### multiply

`public Matrix multiply(double factor)`
Creates a new `Matrix` which has the value of the current matrix multiplied by the given factor.
Specified by:
`multiply` in class `Matrix`
Parameters:
`factor` - The 'scalar' to multiply by the current Matrix.
Returns:
The new matrix.
• #### multiplyAssign

```@Nonnull
public final DenseMatrix multiplyAssign(double s)```
Multiply a matrix by a scalar in place, A = s*A
Parameters:
`s` - The scalar.
Returns:
A reference to this matrix, for convenience in performing multiple operations in a single line of code.
• #### multiply

`public final double[] multiply(double[] vector)`
Returns the result of the multiplication of this matrix and a vector.
Parameters:
`vector` - The vector to be multiplied.
Returns:
The resultant vector.
• #### multiply

```public static double[] multiply(DenseMatrix matrix,
double[] vector)```
Returns the result of the multiplication of a matrix and a vector.
Parameters:
`matrix` - The matrix to multiply by the vector.
`vector` - The vector being multiplied.
Returns:
The array of doubles which is the product of the given matrix and vector.
• #### solve

`public final double[] solve(double[] vectorB)`
Solve A*X = B
Parameters:
`vectorB` - right hand side
Returns:
solution if A is square, least squares solution otherwise
• #### identity

```public static DenseMatrix identity(int m,
int n)```
Generate identity matrix
Parameters:
`m` - Number of rows.
`n` - Number of columns.
Returns:
An m-by-n matrix with ones on the diagonal and zeros elsewhere.