SuanShu, a Java numerical and statistical library

com.numericalmethod.suanshu.matrix.doubles.matrixtype.dense
Class DenseData

java.lang.Object
  extended by com.numericalmethod.suanshu.matrix.doubles.matrixtype.dense.DenseData
All Implemented Interfaces:
DeepCopyable, MatrixData

public abstract class DenseData
extends java.lang.Object
implements MatrixData

This class provides an implementation of a dense matrix.

It stores the data of a 2D matrix as a 1D array for performance reason. In general computing index for a double[][] array is slower than that for a double[] array. Hence for most operations, e.g., element-by-element add, minus, this implementation will be faster.

To loop over the matrix element by rows, here is a code snippet.

 for (int row = 0; row < this.dataImpl.length; row += nCols()) {//loop over rows
      for (int col = 0; col < nCols(); col += 1) {//loop over cols
          int index = row + col;//A[row][col] = A[index]
          //do something useful
      }
 }
 

This class implements some common element-by-element operations so that its subclass implementation of a dense matrix can reuse these pieces of code.

For Java, an initialized data array (1D or 2D) seems to have all entries 0. We use this "assumption" to save an Arrays.fill.


Field Summary
protected  double[] data
          stores the values of matrix entries
 
Constructor Summary
DenseData(double[] data)
           
DenseData(double[] data, DoubleArrayOperation doubleArrayOperation)
           
DenseData(int length)
           
 
Method Summary
 double[] add(DenseData that)
          Add up the elements in this and that, element-by-element.
 boolean equals(java.lang.Object obj)
          Check if two DenseData are equal.
 int hashCode()
           
 double[] minus(DenseData that)
          Subtract the elements in this by that, element-by-element.
 double[] scaled(double scalar)
          Multiply the elements in this by a scalar, element-by-element.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.numericalmethod.suanshu.matrix.doubles.matrixtype.MatrixData
get, set
 
Methods inherited from interface com.numericalmethod.suanshu.DeepCopyable
deepCopy
 

Field Detail

data

protected double[] data
stores the values of matrix entries

Constructor Detail

DenseData

public DenseData(double[] data,
                 DoubleArrayOperation doubleArrayOperation)

DenseData

public DenseData(double[] data)

DenseData

public DenseData(int length)
Method Detail

add

public double[] add(DenseData that)
Add up the elements in this and that, element-by-element. The result is a new and independent double[] array.

Parameters:
that - a DenseData
Returns:
a double[], of which entries are the sum of this.data and that.data

minus

public double[] minus(DenseData that)
Subtract the elements in this by that, element-by-element. The result is a new and independent double[] array.

Parameters:
that - a DenseData
Returns:
a double[], of which entries are the difference of this.data and that.data

scaled

public double[] scaled(double scalar)
Multiply the elements in this by a scalar, element-by-element. The result is a new and independent double[] array.

Parameters:
scalar - the constant to multiply with
Returns:
a double[], of which entries are those of this multiplied by scalar

equals

public boolean equals(java.lang.Object obj)
Check if two DenseData are equal.

DoubleUtils.equal is used in place of Arrays.equal because we need to compare doubles.

Overrides:
equals in class java.lang.Object
Returns:
true iff two DenseData have the same values, element-by-element

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

SuanShu, a Java numerical and statistical library

Copyright © 2011 Numerical Method Inc. Ltd. All Rights Reserved.