/*
Copyright (C) 1999 CERN - European Organization for Nuclear Research.
Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose
is hereby granted without fee, provided that the above copyright notice appear in all copies and
that both that copyright notice and this permission notice appear in supporting documentation.
CERN makes no representations about the suitability of this software for any purpose.
It is provided "as is" without expressed or implied warranty.
*/
package cern.colt.matrix.tobject.impl;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import cern.colt.list.tint.IntArrayList;
import cern.colt.matrix.tobject.ObjectMatrix1D;
import cern.colt.matrix.tobject.ObjectMatrix2D;
import edu.emory.mathcs.utils.ConcurrencyUtils;
/**
* Dense 2-d matrix holding Object elements. First see the package summary and javadoc tree view to get the broad picture.
*
* Implementation:
*
* Internally holds one single contigous one-dimensional array, addressed in row
* major. Note that this implementation is not synchronized.
*
* Memory requirements:
*
* memory [bytes] = 8*rows()*columns() . Thus, a 1000*1000 matrix uses 8
* MB.
*
* Time complexity:
*
* O(1) (i.e. constant time) for the basic operations get ,
* getQuick , set , setQuick and size ,
*
* Cells are internally addressed in row-major. Applications demanding utmost
* speed can exploit this fact. Setting/getting values in a loop row-by-row is
* quicker than column-by-column. Thus
*
*
* for (int row = 0; row < rows; row++) {
* for (int column = 0; column < columns; column++) {
* matrix.setQuick(row, column, someValue);
* }
* }
*
*
* is quicker than
*
*
* for (int column = 0; column < columns; column++) {
* for (int row = 0; row < rows; row++) {
* matrix.setQuick(row, column, someValue);
* }
* }
*
*
* @author [email protected]
* @version 1.0, 09/24/99
*/
public class DenseObjectMatrix2D extends ObjectMatrix2D {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* The elements of this matrix. elements are stored in row major, i.e.
* index==row*columns + column columnOf(index)==index%columns
* rowOf(index)==index/columns i.e. {row0 column0..m}, {row1 column0..m},
* ..., {rown column0..m}
*/
protected Object[] elements;
/**
* Constructs a matrix with a copy of the given values. values is
* required to have the form values[row][column] and have exactly
* the same number of columns in every row.
*
* The values are copied. So subsequent changes in values are not
* reflected in the matrix, and vice-versa.
*
* @param values
* The values to be filled into the new matrix.
* @throws IllegalArgumentException
* if
* for any 1 <= row < values.length: values[row].length != values[row-1].length
* .
*/
public DenseObjectMatrix2D(Object[][] values) {
this(values.length, values.length == 0 ? 0 : values[0].length);
assign(values);
}
/**
* Constructs a matrix with a given number of rows and columns. All entries
* are initially 0 .
*
* @param rows
* the number of rows the matrix shall have.
* @param columns
* the number of columns the matrix shall have.
* @throws IllegalArgumentException
* if
* rows<0 || columns<0 || (double)columns*rows > Integer.MAX_VALUE
* .
*/
public DenseObjectMatrix2D(int rows, int columns) {
setUp(rows, columns);
this.elements = new Object[rows * columns];
}
/**
* Constructs a view with the given parameters.
*
* @param rows
* the number of rows the matrix shall have.
* @param columns
* the number of columns the matrix shall have.
* @param elements
* the cells.
* @param rowZero
* the position of the first element.
* @param columnZero
* the position of the first element.
* @param rowStride
* the number of elements between two rows, i.e.
* index(i+1,j)-index(i,j) .
* @param columnStride
* the number of elements between two columns, i.e.
* index(i,j+1)-index(i,j) .
* @param isView
* if true then a matrix view is constructed
* @throws IllegalArgumentException
* if
* rows<0 || columns<0 || (double)columns*rows > Integer.MAX_VALUE
* or flip's are illegal.
*/
protected DenseObjectMatrix2D(int rows, int columns, Object[] elements, int rowZero, int columnZero, int rowStride,
int columnStride, boolean isView) {
setUp(rows, columns, rowZero, columnZero, rowStride, columnStride);
this.elements = elements;
this.isNoView = !isView;
}
public Object aggregate(final cern.colt.function.tobject.ObjectObjectFunction aggr,
final cern.colt.function.tobject.ObjectFunction f) {
if (size() == 0)
throw new IllegalArgumentException("size == 0");
final int zero = (int) index(0, 0);
Object a = null;
int nthreads = ConcurrencyUtils.getNumberOfThreads();
if ((nthreads > 1) && (size() >= ConcurrencyUtils.getThreadsBeginN_2D())) {
nthreads = Math.min(nthreads, rows);
Future>[] futures = new Future[nthreads];
int k = rows / nthreads;
for (int j = 0; j < nthreads; j++) {
final int firstRow = j * k;
final int lastRow = (j == nthreads - 1) ? rows : firstRow + k;
futures[j] = ConcurrencyUtils.submit(new Callable() {
public Object call() throws Exception {
Object a = f.apply(elements[zero + firstRow * rowStride]);
int d = 1;
for (int r = firstRow; r < lastRow; r++) {
for (int c = d; c < columns; c++) {
a = aggr.apply(a, f.apply(elements[zero + r * rowStride + c * columnStride]));
}
d = 0;
}
return a;
}
});
}
a = ConcurrencyUtils.waitForCompletion(futures, aggr);
} else {
a = f.apply(elements[zero]);
int d = 1; // first cell already done
for (int r = 0; r < rows; r++) {
for (int c = d; c < columns; c++) {
a = aggr.apply(a, f.apply(elements[zero + r * rowStride + c * columnStride]));
}
d = 0;
}
}
return a;
}
public Object aggregate(final cern.colt.function.tobject.ObjectObjectFunction aggr,
final cern.colt.function.tobject.ObjectFunction f, final cern.colt.function.tobject.ObjectProcedure cond) {
if (size() == 0)
throw new IllegalArgumentException("size == 0");
final int zero = (int) index(0, 0);
Object a = null;
int nthreads = ConcurrencyUtils.getNumberOfThreads();
if ((nthreads > 1) && (size() >= ConcurrencyUtils.getThreadsBeginN_2D())) {
nthreads = Math.min(nthreads, rows);
Future>[] futures = new Future[nthreads];
int k = rows / nthreads;
for (int j = 0; j < nthreads; j++) {
final int firstRow = j * k;
final int lastRow = (j == nthreads - 1) ? rows : firstRow + k;
futures[j] = ConcurrencyUtils.submit(new Callable() {
public Object call() throws Exception {
Object elem = elements[zero + firstRow * rowStride];
Object a = 0;
if (cond.apply(elem) == true) {
a = f.apply(elem);
}
int d = 1;
for (int r = firstRow; r < lastRow; r++) {
for (int c = d; c < columns; c++) {
elem = elements[zero + r * rowStride + c * columnStride];
if (cond.apply(elem) == true) {
a = aggr.apply(a, f.apply(elem));
}
}
d = 0;
}
return a;
}
});
}
a = ConcurrencyUtils.waitForCompletion(futures, aggr);
} else {
Object elem = elements[zero];
if (cond.apply(elem) == true) {
a = f.apply(elements[zero]);
}
int d = 1; // first cell already done
for (int r = 0; r < rows; r++) {
for (int c = d; c < columns; c++) {
elem = elements[zero + r * rowStride + c * columnStride];
if (cond.apply(elem) == true) {
a = aggr.apply(a, f.apply(elem));
}
}
d = 0;
}
}
return a;
}
public Object aggregate(final cern.colt.function.tobject.ObjectObjectFunction aggr,
final cern.colt.function.tobject.ObjectFunction f, final IntArrayList rowList, final IntArrayList columnList) {
if (size() == 0)
throw new IllegalArgumentException("size == 0");
final int zero = (int) index(0, 0);
final int size = rowList.size();
final int[] rowElements = rowList.elements();
final int[] columnElements = columnList.elements();
Object a = null;
int nthreads = ConcurrencyUtils.getNumberOfThreads();
if ((nthreads > 1) && (size >= ConcurrencyUtils.getThreadsBeginN_2D())) {
nthreads = Math.min(nthreads, size);
Future>[] futures = new Future[nthreads];
int k = size / nthreads;
for (int j = 0; j < nthreads; j++) {
final int firstIdx = j * k;
final int lastIdx = (j == nthreads - 1) ? size : firstIdx + k;
futures[j] = ConcurrencyUtils.submit(new Callable() {
public Object call() throws Exception {
Object a = f.apply(elements[zero + rowElements[firstIdx] * rowStride + columnElements[firstIdx]
* columnStride]);
Object elem;
for (int i = firstIdx + 1; i < lastIdx; i++) {
elem = elements[zero + rowElements[i] * rowStride + columnElements[i] * columnStride];
a = aggr.apply(a, f.apply(elem));
}
return a;
}
});
}
a = ConcurrencyUtils.waitForCompletion(futures, aggr);
} else {
Object elem;
a = f.apply(elements[zero + rowElements[0] * rowStride + columnElements[0] * columnStride]);
for (int i = 1; i < size; i++) {
elem = elements[zero + rowElements[i] * rowStride + columnElements[i] * columnStride];
a = aggr.apply(a, f.apply(elem));
}
}
return a;
}
public Object aggregate(final ObjectMatrix2D other, final cern.colt.function.tobject.ObjectObjectFunction aggr,
final cern.colt.function.tobject.ObjectObjectFunction f) {
if (!(other instanceof DenseObjectMatrix2D)) {
return super.aggregate(other, aggr, f);
}
checkShape(other);
if (size() == 0)
throw new IllegalArgumentException("size == 0");
final int zero = (int) index(0, 0);
final int zeroOther = (int) other.index(0, 0);
final int rowStrideOther = other.rowStride();
final int colStrideOther = other.columnStride();
final Object[] elemsOther = (Object[]) other.elements();
Object a = null;
int nthreads = ConcurrencyUtils.getNumberOfThreads();
if ((nthreads > 1) && (size() >= ConcurrencyUtils.getThreadsBeginN_2D())) {
nthreads = Math.min(nthreads, rows);
Future>[] futures = new Future[nthreads];
int k = rows / nthreads;
for (int j = 0; j < nthreads; j++) {
final int firstRow = j * k;
final int lastRow = (j == nthreads - 1) ? rows : firstRow + k;
futures[j] = ConcurrencyUtils.submit(new Callable() {
public Object call() throws Exception {
Object a = f.apply(elements[zero + firstRow * rowStride], elemsOther[zeroOther + firstRow
* rowStrideOther]);
int d = 1;
for (int r = firstRow; r < lastRow; r++) {
for (int c = d; c < columns; c++) {
a = aggr.apply(a, f.apply(elements[zero + r * rowStride + c * columnStride],
elemsOther[zeroOther + r * rowStrideOther + c * colStrideOther]));
}
d = 0;
}
return a;
}
});
}
a = ConcurrencyUtils.waitForCompletion(futures, aggr);
} else {
int d = 1; // first cell already done
a = f.apply(elements[zero], elemsOther[zeroOther]);
for (int r = 0; r < rows; r++) {
for (int c = d; c < columns; c++) {
a = aggr.apply(a, f.apply(elements[zero + r * rowStride + c * columnStride], elemsOther[zeroOther
+ r * rowStrideOther + c * colStrideOther]));
}
d = 0;
}
}
return a;
}
public ObjectMatrix2D assign(final Object value) {
final Object[] elems = this.elements;
final int zero = (int) index(0, 0);
int nthreads = ConcurrencyUtils.getNumberOfThreads();
if ((nthreads > 1) && (size() >= ConcurrencyUtils.getThreadsBeginN_2D())) {
nthreads = Math.min(nthreads, rows);
Future>[] futures = new Future[nthreads];
int k = rows / nthreads;
for (int j = 0; j < nthreads; j++) {
final int firstRow = j * k;
final int lastRow = (j == nthreads - 1) ? rows : firstRow + k;
futures[j] = ConcurrencyUtils.submit(new Runnable() {
public void run() {
int idx = zero + firstRow * rowStride;
for (int r = firstRow; r < lastRow; r++) {
for (int i = idx, c = 0; c < columns; c++) {
elems[i] = value;
i += columnStride;
}
idx += rowStride;
}
}
});
}
ConcurrencyUtils.waitForCompletion(futures);
} else {
int idx = zero;
for (int r = 0; r < rows; r++) {
for (int i = idx, c = 0; c < columns; c++) {
elems[i] = value;
i += columnStride;
}
idx += rowStride;
}
}
return this;
}
/**
* Sets all cells to the state specified by values . values
* is required to have the form values[row][column] and have
* exactly the same number of rows and columns as the receiver.
*
* The values are copied. So subsequent changes in values are not
* reflected in the matrix, and vice-versa.
*
* @param values
* the values to be filled into the cells.
* @return this (for convenience only).
* @throws IllegalArgumentException
* if
* values.length != rows() || for any 0 <= row < rows(): values[row].length != columns()
* .
*/
public ObjectMatrix2D assign(final Object[][] values) {
if (values.length != rows)
throw new IllegalArgumentException("Must have same number of rows: rows=" + values.length + "rows()="
+ rows());
int nthreads = ConcurrencyUtils.getNumberOfThreads();
if (this.isNoView) {
if ((nthreads > 1) && (size() >= ConcurrencyUtils.getThreadsBeginN_2D())) {
nthreads = Math.min(nthreads, rows);
Future>[] futures = new Future[nthreads];
int k = rows / nthreads;
for (int j = 0; j < nthreads; j++) {
final int firstRow = j * k;
final int lastRow = (j == nthreads - 1) ? rows : firstRow + k;
futures[j] = ConcurrencyUtils.submit(new Runnable() {
public void run() {
int i = firstRow * rowStride;
for (int r = firstRow; r < lastRow; r++) {
Object[] currentRow = values[r];
if (currentRow.length != columns)
throw new IllegalArgumentException(
"Must have same number of columns in every row: columns="
+ currentRow.length + "columns()=" + columns());
System.arraycopy(currentRow, 0, elements, i, columns);
i += columns;
}
}
});
}
try {
for (int j = 0; j < nthreads; j++) {
futures[j].get();
}
} catch (ExecutionException ex) {
ex.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
int i = 0;
for (int r = 0; r < rows; r++) {
Object[] currentRow = values[r];
if (currentRow.length != columns)
throw new IllegalArgumentException("Must have same number of columns in every row: columns="
+ currentRow.length + "columns()=" + columns());
System.arraycopy(currentRow, 0, this.elements, i, columns);
i += columns;
}
}
} else {
final int zero = (int) index(0, 0);
if ((nthreads > 1) && (size() >= ConcurrencyUtils.getThreadsBeginN_2D())) {
nthreads = Math.min(nthreads, rows);
Future>[] futures = new Future[nthreads];
int k = rows / nthreads;
for (int j = 0; j < nthreads; j++) {
final int firstRow = j * k;
final int lastRow = (j == nthreads - 1) ? rows : firstRow + k;
futures[j] = ConcurrencyUtils.submit(new Runnable() {
public void run() {
int idx = zero + firstRow * rowStride;
for (int r = firstRow; r < lastRow; r++) {
Object[] currentRow = values[r];
if (currentRow.length != columns)
throw new IllegalArgumentException(
"Must have same number of columns in every row: columns="
+ currentRow.length + "columns()=" + columns());
for (int i = idx, c = 0; c < columns; c++) {
elements[i] = currentRow[c];
i += columnStride;
}
idx += rowStride;
}
}
});
}
try {
for (int j = 0; j < nthreads; j++) {
futures[j].get();
}
} catch (ExecutionException ex) {
ex.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
int idx = zero;
for (int r = 0; r < rows; r++) {
Object[] currentRow = values[r];
if (currentRow.length != columns)
throw new IllegalArgumentException("Must have same number of columns in every row: columns="
+ currentRow.length + "columns()=" + columns());
for (int i = idx, c = 0; c < columns; c++) {
elements[i] = currentRow[c];
i += columnStride;
}
idx += rowStride;
}
}
return this;
}
return this;
}
/**
* Assigns the result of a function to each cell;
* x[row,col] = function(x[row,col]) .
*
* Example:
*
*
* matrix = 2 x 2 matrix
* 0.5 1.5
* 2.5 3.5
*
* // change each cell to its sine
* matrix.assign(cern.jet.math.Functions.sin);
* -->
* 2 x 2 matrix
* 0.479426 0.997495
* 0.598472 -0.350783
*
*
*
* For further examples, see the package doc .
*
* @param function
* a function object taking as argument the current cell's value.
* @return this (for convenience only).
* @see cern.jet.math.tdouble.DoubleFunctions
*/
public ObjectMatrix2D assign(final cern.colt.function.tobject.ObjectFunction function) {
final Object[] elems = this.elements;
if (elems == null)
throw new InternalError();
final int zero = (int) index(0, 0);
int nthreads = ConcurrencyUtils.getNumberOfThreads();
if ((nthreads > 1) && (size() >= ConcurrencyUtils.getThreadsBeginN_2D())) {
nthreads = Math.min(nthreads, rows);
Future>[] futures = new Future[nthreads];
int k = rows / nthreads;
for (int j = 0; j < nthreads; j++) {
final int firstRow = j * k;
final int lastRow = (j == nthreads - 1) ? rows : firstRow + k;
futures[j] = ConcurrencyUtils.submit(new Runnable() {
public void run() {
int idx = zero + firstRow * rowStride;
// the general case x[i] = f(x[i])
for (int r = firstRow; r < lastRow; r++) {
for (int i = idx, c = 0; c < columns; c++) {
elems[i] = function.apply(elems[i]);
i += columnStride;
}
idx += rowStride;
}
}
});
}
ConcurrencyUtils.waitForCompletion(futures);
} else {
int idx = zero;
for (int r = 0; r < rows; r++) {
for (int i = idx, c = 0; c < columns; c++) {
elems[i] = function.apply(elems[i]);
i += columnStride;
}
idx += rowStride;
}
}
return this;
}
public ObjectMatrix2D assign(final cern.colt.function.tobject.ObjectProcedure cond,
final cern.colt.function.tobject.ObjectFunction function) {
final int zero = (int) index(0, 0);
int nthreads = ConcurrencyUtils.getNumberOfThreads();
if ((nthreads > 1) && (size() >= ConcurrencyUtils.getThreadsBeginN_2D())) {
nthreads = Math.min(nthreads, rows);
Future>[] futures = new Future[nthreads];
int k = rows / nthreads;
for (int j = 0; j < nthreads; j++) {
final int firstRow = j * k;
final int lastRow = (j == nthreads - 1) ? rows : firstRow + k;
futures[j] = ConcurrencyUtils.submit(new Runnable() {
public void run() {
Object elem;
int idx = zero + firstRow * rowStride;
for (int r = firstRow; r < lastRow; r++) {
for (int i = idx, c = 0; c < columns; c++) {
elem = elements[i];
if (cond.apply(elem) == true) {
elements[i] = function.apply(elem);
}
i += columnStride;
}
idx += rowStride;
}
}
});
}
ConcurrencyUtils.waitForCompletion(futures);
} else {
Object elem;
int idx = zero;
for (int r = 0; r < rows; r++) {
for (int i = idx, c = 0; c < columns; c++) {
elem = elements[i];
if (cond.apply(elem) == true) {
elements[i] = function.apply(elem);
}
i += columnStride;
}
idx += rowStride;
}
}
return this;
}
public ObjectMatrix2D assign(final cern.colt.function.tobject.ObjectProcedure cond, final Object value) {
final int zero = (int) index(0, 0);
int nthreads = ConcurrencyUtils.getNumberOfThreads();
if ((nthreads > 1) && (size() >= ConcurrencyUtils.getThreadsBeginN_2D())) {
nthreads = Math.min(nthreads, rows);
Future>[] futures = new Future[nthreads];
int k = rows / nthreads;
for (int j = 0; j < nthreads; j++) {
final int firstRow = j * k;
final int lastRow = (j == nthreads - 1) ? rows : firstRow + k;
futures[j] = ConcurrencyUtils.submit(new Runnable() {
public void run() {
Object elem;
int idx = zero + firstRow * rowStride;
for (int r = firstRow; r < lastRow; r++) {
for (int i = idx, c = 0; c < columns; c++) {
elem = elements[i];
if (cond.apply(elem) == true) {
elements[i] = value;
}
i += columnStride;
}
idx += rowStride;
}
}
});
}
ConcurrencyUtils.waitForCompletion(futures);
} else {
Object elem;
int idx = zero;
for (int r = 0; r < rows; r++) {
for (int i = idx, c = 0; c < columns; c++) {
elem = elements[i];
if (cond.apply(elem) == true) {
elements[i] = value;
}
i += columnStride;
}
idx += rowStride;
}
}
return this;
}
/**
* Replaces all cell values of the receiver with the values of another
* matrix. Both matrices must have the same number of rows and columns. 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 .
*
* @param source
* the source matrix to copy from (may be identical to the
* receiver).
* @return this (for convenience only).
* @throws IllegalArgumentException
* if
* columns() != source.columns() || rows() != source.rows()
*/
public ObjectMatrix2D assign(ObjectMatrix2D source) {
// overriden for performance only
if (!(source instanceof DenseObjectMatrix2D)) {
return super.assign(source);
}
final DenseObjectMatrix2D other_final = (DenseObjectMatrix2D) source;
if (other_final == this)
return this; // nothing to do
checkShape(other_final);
int nthreads = ConcurrencyUtils.getNumberOfThreads();
if (this.isNoView && other_final.isNoView) { // quickest
System.arraycopy(other_final.elements, 0, this.elements, 0, this.elements.length);
return this;
}
DenseObjectMatrix2D other = (DenseObjectMatrix2D) source;
if (haveSharedCells(other)) {
ObjectMatrix2D c = other.copy();
if (!(c instanceof DenseObjectMatrix2D)) { // should not happen
super.assign(other);
return this;
}
other = (DenseObjectMatrix2D) c;
}
final Object[] elemsOther = other.elements;
if (elements == null || elemsOther == null)
throw new InternalError();
final int zeroOther = (int) other.index(0, 0);
final int zero = (int) index(0, 0);
final int columnStrideOther = other.columnStride;
final int rowStrideOther = other.rowStride;
if ((nthreads > 1) && (size() >= ConcurrencyUtils.getThreadsBeginN_2D())) {
nthreads = Math.min(nthreads, rows);
Future>[] futures = new Future[nthreads];
int k = rows / nthreads;
for (int j = 0; j < nthreads; j++) {
final int firstRow = j * k;
final int lastRow = (j == nthreads - 1) ? rows : firstRow + k;
futures[j] = ConcurrencyUtils.submit(new Runnable() {
public void run() {
int idx = zero + firstRow * rowStride;
int idxOther = zeroOther + firstRow * rowStrideOther;
for (int r = firstRow; r < lastRow; r++) {
for (int i = idx, j = idxOther, c = 0; c < columns; c++) {
elements[i] = elemsOther[j];
i += columnStride;
j += columnStrideOther;
}
idx += rowStride;
idxOther += rowStrideOther;
}
}
});
}
ConcurrencyUtils.waitForCompletion(futures);
} else {
int idx = zero;
int idxOther = zeroOther;
for (int r = 0; r < rows; r++) {
for (int i = idx, j = idxOther, c = 0; c < columns; c++) {
elements[i] = elemsOther[j];
i += columnStride;
j += columnStrideOther;
}
idx += rowStride;
idxOther += rowStrideOther;
}
}
return this;
}
/**
* Assigns the result of a function to each cell;
* x[row,col] = function(x[row,col],y[row,col]) .
*
* @param y
* the secondary matrix to operate on.
* @param 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 ,
* @return this (for convenience only).
* @throws IllegalArgumentException
* if
* columns() != other.columns() || rows() != other.rows()
* @see cern.jet.math.tdouble.DoubleFunctions
*/
public ObjectMatrix2D assign(final ObjectMatrix2D y, final cern.colt.function.tobject.ObjectObjectFunction function) {
// overriden for performance only
if (!(y instanceof DenseObjectMatrix2D)) {
return super.assign(y, function);
}
DenseObjectMatrix2D other = (DenseObjectMatrix2D) y;
checkShape(y);
final Object[] elemsOther = other.elements();
if (elements == null || elemsOther == null)
throw new InternalError();
final int zeroOther = (int) other.index(0, 0);
final int zero = (int) index(0, 0);
final int columnStrideOther = other.columnStride;
final int rowStrideOther = other.rowStride;
int nthreads = ConcurrencyUtils.getNumberOfThreads();
if ((nthreads > 1) && (size() >= ConcurrencyUtils.getThreadsBeginN_2D())) {
nthreads = Math.min(nthreads, rows);
Future>[] futures = new Future[nthreads];
int k = rows / nthreads;
for (int j = 0; j < nthreads; j++) {
final int firstRow = j * k;
final int lastRow = (j == nthreads - 1) ? rows : firstRow + k;
futures[j] = ConcurrencyUtils.submit(new Runnable() {
public void run() {
int idx;
int idxOther;
idx = zero + firstRow * rowStride;
idxOther = zeroOther + firstRow * rowStrideOther;
for (int r = firstRow; r < lastRow; r++) {
for (int i = idx, j = idxOther, c = 0; c < columns; c++) {
elements[i] = function.apply(elements[i], elemsOther[j]);
i += columnStride;
j += columnStrideOther;
}
idx += rowStride;
idxOther += rowStrideOther;
}
}
});
}
ConcurrencyUtils.waitForCompletion(futures);
} else {
int idx;
int idxOther;
idx = zero;
idxOther = zeroOther;
for (int r = 0; r < rows; r++) {
for (int i = idx, j = idxOther, c = 0; c < columns; c++) {
elements[i] = function.apply(elements[i], elemsOther[j]);
i += columnStride;
j += columnStrideOther;
}
idx += rowStride;
idxOther += rowStrideOther;
}
}
return this;
}
public ObjectMatrix2D assign(final Object[] values) {
if (values.length != size())
throw new IllegalArgumentException("Must have same length: length=" + values.length + " rows()*columns()="
+ rows() * columns());
int nthreads = ConcurrencyUtils.getNumberOfThreads();
if (this.isNoView) {
System.arraycopy(values, 0, this.elements, 0, values.length);
} else {
final int zero = (int) index(0, 0);
if ((nthreads > 1) && (size() >= ConcurrencyUtils.getThreadsBeginN_2D())) {
nthreads = Math.min(nthreads, rows);
Future>[] futures = new Future[nthreads];
int k = rows / nthreads;
for (int j = 0; j < nthreads; j++) {
final int firstRow = j * k;
final int lastRow = (j == nthreads - 1) ? rows : firstRow + k;
futures[j] = ConcurrencyUtils.submit(new Runnable() {
public void run() {
int idxOther = firstRow * columns;
int idx = zero + firstRow * rowStride;
for (int r = firstRow; r < lastRow; r++) {
for (int i = idx, c = 0; c < columns; c++) {
elements[i] = values[idxOther++];
i += columnStride;
}
idx += rowStride;
}
}
});
}
ConcurrencyUtils.waitForCompletion(futures);
} else {
int idxOther = 0;
int idx = zero;
for (int r = 0; r < rows; r++) {
for (int i = idx, c = 0; c < columns; c++) {
elements[i] = values[idxOther++];
i += columnStride;
}
idx += rowStride;
}
}
}
return this;
}
public ObjectMatrix2D assign(final ObjectMatrix2D y, final cern.colt.function.tobject.ObjectObjectFunction function,
IntArrayList rowList, IntArrayList columnList) {
checkShape(y);
final int size = rowList.size();
final int[] rowElements = rowList.elements();
final int[] columnElements = columnList.elements();
final Object[] elemsOther = (Object[]) y.elements();
final int zeroOther = (int) y.index(0, 0);
final int zero = (int) index(0, 0);
final int columnStrideOther = y.columnStride();
final int rowStrideOther = y.rowStride();
int nthreads = ConcurrencyUtils.getNumberOfThreads();
if ((nthreads > 1) && (size >= ConcurrencyUtils.getThreadsBeginN_2D())) {
nthreads = Math.min(nthreads, size);
Future>[] futures = new Future[nthreads];
int k = size / nthreads;
for (int j = 0; j < nthreads; j++) {
final int firstIdx = j * k;
final int lastIdx = (j == nthreads - 1) ? size : firstIdx + k;
futures[j] = ConcurrencyUtils.submit(new Runnable() {
public void run() {
int idx;
int idxOther;
for (int i = firstIdx; i < lastIdx; i++) {
idx = zero + rowElements[i] * rowStride + columnElements[i] * columnStride;
idxOther = zeroOther + rowElements[i] * rowStrideOther + columnElements[i]
* columnStrideOther;
elements[idx] = function.apply(elements[idx], elemsOther[idxOther]);
}
}
});
}
ConcurrencyUtils.waitForCompletion(futures);
} else {
int idx;
int idxOther;
for (int i = 0; i < size; i++) {
idx = zero + rowElements[i] * rowStride + columnElements[i] * columnStride;
idxOther = zeroOther + rowElements[i] * rowStrideOther + columnElements[i] * columnStrideOther;
elements[idx] = function.apply(elements[idx], elemsOther[idxOther]);
}
}
return this;
}
public Object[] elements() {
return elements;
}
/**
* Returns the matrix cell value at coordinate [row,column] .
*
*
* 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):
* 0 <= column < columns() && 0 <= row < rows() .
*
* @param row
* the index of the row-coordinate.
* @param column
* the index of the column-coordinate.
* @return the value at the specified coordinate.
*/
public Object getQuick(int row, int column) {
// if (debug) if (column<0 || column>=columns || row<0 || row>=rows)
// throw new IndexOutOfBoundsException("row:"+row+", column:"+column);
// return elements[index(row,column)];
// manually inlined:
return elements[rowZero + row * rowStride + columnZero + column * columnStride];
}
/**
* Returns true if both matrices share common cells. More formally,
* returns true if other != null and at least one of the
* following conditions is met
*
* the receiver is a view of the other matrix
* the other matrix is a view of the receiver
* this == other
*
*/
protected boolean haveSharedCellsRaw(ObjectMatrix2D other) {
if (other instanceof SelectedDenseObjectMatrix2D) {
SelectedDenseObjectMatrix2D otherMatrix = (SelectedDenseObjectMatrix2D) other;
return this.elements == otherMatrix.elements;
} else if (other instanceof DenseObjectMatrix2D) {
DenseObjectMatrix2D otherMatrix = (DenseObjectMatrix2D) other;
return this.elements == otherMatrix.elements;
}
return false;
}
/**
* Returns the position of the given coordinate within the (virtual or
* non-virtual) internal 1-dimensional array.
*
* @param row
* the index of the row-coordinate.
* @param column
* the index of the column-coordinate.
*/
public long index(int row, int column) {
// return super.index(row,column);
// manually inlined for speed:
return rowZero + row * rowStride + columnZero + column * columnStride;
}
/**
* Construct and returns a new empty matrix of the same dynamic type
* as the receiver, having the specified number of rows and columns. For
* example, if the receiver is an instance of type
* DenseObjectMatrix2D the new matrix must also be of type
* DenseObjectMatrix2D , if the receiver is an instance of type
* SparseObjectMatrix2D the new matrix must also be of type
* SparseObjectMatrix2D , etc. In general, the new matrix should
* have internal parametrization as similar as possible.
*
* @param rows
* the number of rows the matrix shall have.
* @param columns
* the number of columns the matrix shall have.
* @return a new empty matrix of the same dynamic type.
*/
public ObjectMatrix2D like(int rows, int columns) {
return new DenseObjectMatrix2D(rows, columns);
}
/**
* Construct and returns a new 1-d matrix of the corresponding dynamic
* type , entirelly independent of the receiver. For example, if the
* receiver is an instance of type DenseObjectMatrix2D the new
* matrix must be of type DenseObjectMatrix1D , if the receiver is
* an instance of type SparseObjectMatrix2D the new matrix must be
* of type SparseObjectMatrix1D , etc.
*
* @param size
* the number of cells the matrix shall have.
* @return a new matrix of the corresponding dynamic type.
*/
public ObjectMatrix1D like1D(int size) {
return new DenseObjectMatrix1D(size);
}
/**
* Construct and returns a new 1-d matrix of the corresponding dynamic
* type , sharing the same cells. For example, if the receiver is an
* instance of type DenseObjectMatrix2D the new matrix must be of
* type DenseObjectMatrix1D , if the receiver is an instance of type
* SparseObjectMatrix2D the new matrix must be of type
* SparseObjectMatrix1D , etc.
*
* @param size
* the number of cells the matrix shall have.
* @param zero
* the index of the first element.
* @param stride
* the number of indexes between any two elements, i.e.
* index(i+1)-index(i) .
* @return a new matrix of the corresponding dynamic type.
*/
protected ObjectMatrix1D like1D(int size, int zero, int stride) {
return new DenseObjectMatrix1D(size, this.elements, zero, stride, true);
}
/**
* Sets the matrix cell at coordinate [row,column] 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):
* 0 <= column < columns() && 0 <= row < rows() .
*
* @param row
* the index of the row-coordinate.
* @param column
* the index of the column-coordinate.
* @param value
* the value to be filled into the specified cell.
*/
public void setQuick(int row, int column, Object value) {
// if (debug) if (column<0 || column>=columns || row<0 || row>=rows)
// throw new IndexOutOfBoundsException("row:"+row+", column:"+column);
// elements[index(row,column)] = value;
// manually inlined:
elements[rowZero + row * rowStride + columnZero + column * columnStride] = value;
}
public ObjectMatrix1D vectorize() {
final DenseObjectMatrix1D v = new DenseObjectMatrix1D((int) size());
final int zero = (int) index(0, 0);
final int zeroOther = (int) v.index(0);
final int strideOther = v.stride();
final Object[] elemsOther = (Object[])v.elements();
int nthreads = ConcurrencyUtils.getNumberOfThreads();
if ((nthreads > 1) && (size() >= ConcurrencyUtils.getThreadsBeginN_2D())) {
nthreads = Math.min(nthreads, columns);
Future>[] futures = new Future[nthreads];
int k = columns / nthreads;
for (int j = 0; j < nthreads; j++) {
final int firstColumn = j * k;
final int lastColumn = (j == nthreads - 1) ? columns : firstColumn + k;
final int startidx = j * k * rows;
futures[j] = ConcurrencyUtils.submit(new Runnable() {
public void run() {
int idx = 0;
int idxOther = zeroOther + startidx * strideOther;
for (int c = firstColumn; c < lastColumn; c++) {
idx = zero + c * columnStride;
for (int r = 0; r < rows; r++) {
elemsOther[idxOther] = elements[idx];
idx += rowStride;
idxOther += strideOther;
}
}
}
});
}
ConcurrencyUtils.waitForCompletion(futures);
} else {
int idx = zero;
int idxOther = zeroOther;
for (int c = 0; c < columns; c++) {
idx = zero + c * columnStride;
for (int r = 0; r < rows; r++) {
elemsOther[idxOther] = elements[idx];
idx += rowStride;
idxOther += strideOther;
}
}
}
return v;
}
/**
* Construct and returns a new selection view.
*
* @param rowOffsets
* the offsets of the visible elements.
* @param columnOffsets
* the offsets of the visible elements.
* @return a new view.
*/
protected ObjectMatrix2D viewSelectionLike(int[] rowOffsets, int[] columnOffsets) {
return new SelectedDenseObjectMatrix2D(this.elements, rowOffsets, columnOffsets, 0);
}
}