{
}
-
+
/**
- * Creates a new Matrix object. For example
+ * Creates a new Matrix object containing a copy of the supplied array values.
+ * For example
*
* <pre>
* new Matrix(new double[][] {{2, 3, 4}, {5, 6, 7})
{
this.rows = values.length;
this.cols = this.rows == 0 ? 0 : values[0].length;
- this.value = values;
+
+ /*
+ * make a copy of the values array, for immutability
+ */
+ this.value = new double[rows][];
+ int i = 0;
+ for (double[] row : values)
+ {
+ if (row != null)
+ {
+ value[i] = new double[row.length];
+ System.arraycopy(row, 0, value[i], 0, row.length);
+ }
+ i++;
+ }
}
/**
* Returns a new matrix which is the transpose of this one
*
- * @return DOCUMENT ME!
+ * @return
*/
@Override
public MatrixI transpose()
if (iter == maxIter)
{
throw new Exception(MessageManager.formatMessage(
- "exception.matrix_too_many_iteration", new String[] {
- "tqli", Integer.valueOf(maxIter).toString() }));
+ "exception.matrix_too_many_iteration", new String[]
+ { "tqli", Integer.valueOf(maxIter).toString() }));
}
else
{
{
f = getValue(k - 1, i);
setValue(k - 1, i, (s * getValue(k - 1, i - 1)) + (c * f));
- setValue(k - 1, i - 1, (c * getValue(k - 1, i - 1)) - (s * f));
+ setValue(k - 1, i - 1,
+ (c * getValue(k - 1, i - 1)) - (s * f));
}
}
if (iter == maxIter)
{
throw new Exception(MessageManager.formatMessage(
- "exception.matrix_too_many_iteration", new String[] {
- "tqli2", Integer.valueOf(maxIter).toString() }));
+ "exception.matrix_too_many_iteration", new String[]
+ { "tqli2", Integer.valueOf(maxIter).toString() }));
}
else
{
*
* @param ps
* DOCUMENT ME!
- * @param format TODO
+ * @param format
+ * TODO
*/
@Override
public void printE(PrintStream ps, String format)
{
return e;
}
-
+
@Override
- public int height() {
+ public int height()
+ {
return rows;
}
return row;
}
- @Override
- public double getMaxValue()
+ /**
+ * Returns a length 2 array of {minValue, maxValue} of all values in the
+ * matrix. Returns null if the matrix is null or empty.
+ *
+ * @return
+ */
+ double[] findMinMax()
{
if (value == null)
{
- return 0;
+ return null;
}
+ double min = Double.MAX_VALUE;
double max = -Double.MAX_VALUE;
+ boolean empty = true;
for (double[] row : value)
{
if (row != null)
{
for (double x : row)
{
+ empty = false;
if (x > max)
{
max = x;
}
+ if (x < min)
+ {
+ min = x;
+ }
}
}
}
- return max;
+ return empty ? null : new double[] { min, max };
}
+ /**
+ * {@inheritDoc}
+ */
@Override
- public void subtractAllFrom(double val)
+ public void reverseRange(boolean maxToZero)
{
if (value == null)
{
return;
}
+ double[] minMax = findMinMax();
+ if (minMax == null)
+ {
+ return; // empty matrix
+ }
+ double subtractFrom = maxToZero ? minMax[1] : minMax[0] + minMax[1];
for (double[] row : value)
{
int j = 0;
for (double x : row)
{
- row[j] = val - x;
+ row[j] = subtractFrom - x;
j++;
}
}
}
}
+
+ /**
+ * Multiplies every entry in the matrix by the given value.
+ *
+ * @param
+ */
+ @Override
+ public void multiply(double by)
+ {
+ for (double[] row : value)
+ {
+ if (row != null)
+ {
+ for (int i = 0; i < row.length; i++)
+ {
+ row[i] *= by;
+ }
+ }
+ }
+ }
}