cern.colt.matrix.tdouble.impl.DenseLargeDoubleMatrix2D Maven / Gradle / Ivy
Show all versions of parallelcolt Show documentation
/*
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.tdouble.impl;
import java.util.concurrent.Future;
import cern.colt.matrix.tdcomplex.impl.DenseLargeDComplexMatrix2D;
import cern.colt.matrix.tdouble.DoubleMatrix1D;
import cern.colt.matrix.tdouble.DoubleMatrix2D;
import edu.emory.mathcs.jtransforms.dct.DoubleDCT_1D;
import edu.emory.mathcs.jtransforms.dct.DoubleDCT_2D;
import edu.emory.mathcs.jtransforms.dht.DoubleDHT_1D;
import edu.emory.mathcs.jtransforms.dht.DoubleDHT_2D;
import edu.emory.mathcs.jtransforms.dst.DoubleDST_1D;
import edu.emory.mathcs.jtransforms.dst.DoubleDST_2D;
import edu.emory.mathcs.jtransforms.fft.DoubleFFT_1D;
import edu.emory.mathcs.jtransforms.fft.DoubleFFT_2D;
import edu.emory.mathcs.utils.ConcurrencyUtils;
/**
* Dense 2-d matrix holding double elements. First see the package summary and javadoc tree view to get the broad picture.
*
* Implementation:
*
* This data structure allows to store more than 2^31 elements. Internally holds
* one two-dimensional array, elements[rows][columns]. Note that this
* implementation is not synchronized.
*
* Time complexity:
*
* O(1) (i.e. constant time) for the basic operations get,
* getQuick, set, setQuick and size.
*
* @author Piotr Wendykier ([email protected])
*
*/
public class DenseLargeDoubleMatrix2D extends WrapperDoubleMatrix2D {
private static final long serialVersionUID = 1L;
private double[][] elements;
private DoubleFFT_2D fft2;
private DoubleDCT_2D dct2;
private DoubleDST_2D dst2;
private DoubleDHT_2D dht2;
private DoubleFFT_1D fftRows;
private DoubleFFT_1D fftColumns;
private DoubleDCT_1D dctRows;
private DoubleDCT_1D dctColumns;
private DoubleDST_1D dstRows;
private DoubleDST_1D dstColumns;
private DoubleDHT_1D dhtRows;
private DoubleDHT_1D dhtColumns;
public DenseLargeDoubleMatrix2D(int rows, int columns) {
super(null);
try {
setUp(rows, columns);
} catch (IllegalArgumentException exc) { // we can hold rows*columns>Integer.MAX_VALUE cells !
if (!"matrix too large".equals(exc.getMessage()))
throw exc;
}
elements = new double[rows][columns];
content = this;
}
/**
* Computes the 2D discrete cosine transform (DCT-II) of this matrix.
*
* @param scale
* if true then scaling is performed
*
*/
public void dct2(boolean scale) {
int oldNthreads = ConcurrencyUtils.getNumberOfThreads();
ConcurrencyUtils.setNumberOfThreads(ConcurrencyUtils.nextPow2(oldNthreads));
if (dct2 == null) {
dct2 = new DoubleDCT_2D(rows, columns);
}
dct2.forward(elements, scale);
ConcurrencyUtils.setNumberOfThreads(oldNthreads);
}
/**
* Computes the discrete cosine transform (DCT-II) of each column of this
* matrix.
*
* @param scale
* if true then scaling is performed
*
*/
public void dctColumns(final boolean scale) {
if (dctColumns == null) {
dctColumns = new DoubleDCT_1D(rows);
}
int nthreads = ConcurrencyUtils.getNumberOfThreads();
if ((nthreads > 1) && (size() >= ConcurrencyUtils.getThreadsBeginN_2D())) {
ConcurrencyUtils.setThreadsBeginN_1D_FFT_2Threads(Integer.MAX_VALUE);
ConcurrencyUtils.setThreadsBeginN_1D_FFT_4Threads(Integer.MAX_VALUE);
nthreads = Math.min(nthreads, columns);
Future>[] futures = new Future[nthreads];
int k = columns / nthreads;
for (int j = 0; j < nthreads; j++) {
final int firstCol = j * k;
final int lastCol = (j == nthreads - 1) ? columns : firstCol + k;
futures[j] = ConcurrencyUtils.submit(new Runnable() {
public void run() {
double[] column;
for (int c = firstCol; c < lastCol; c++) {
column = (double[]) viewColumn(c).copy().elements();
dctColumns.forward(column, scale);
viewColumn(c).assign(column);
}
}
});
}
ConcurrencyUtils.waitForCompletion(futures);
ConcurrencyUtils.resetThreadsBeginN_FFT();
} else {
double[] column;
for (int c = 0; c < columns; c++) {
column = (double[]) viewColumn(c).copy().elements();
dctColumns.forward(column, scale);
viewColumn(c).assign(column);
}
}
}
/**
* Computes the discrete cosine transform (DCT-II) of each row of this
* matrix.
*
* @param scale
* if true then scaling is performed
*
*/
public void dctRows(final boolean scale) {
if (dctRows == null) {
dctRows = new DoubleDCT_1D(columns);
}
int nthreads = ConcurrencyUtils.getNumberOfThreads();
if ((nthreads > 1) && (size() >= ConcurrencyUtils.getThreadsBeginN_2D())) {
ConcurrencyUtils.setThreadsBeginN_1D_FFT_2Threads(Integer.MAX_VALUE);
ConcurrencyUtils.setThreadsBeginN_1D_FFT_4Threads(Integer.MAX_VALUE);
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() {
for (int r = firstRow; r < lastRow; r++) {
dctRows.forward(elements[r], scale);
}
}
});
}
ConcurrencyUtils.waitForCompletion(futures);
ConcurrencyUtils.resetThreadsBeginN_FFT();
} else {
for (int r = 0; r < rows; r++) {
dctRows.forward(elements[r], scale);
}
}
}
/**
* Computes the 2D discrete Hartley transform (DHT) of this matrix.
*
*/
public void dht2() {
int oldNthreads = ConcurrencyUtils.getNumberOfThreads();
ConcurrencyUtils.setNumberOfThreads(ConcurrencyUtils.nextPow2(oldNthreads));
if (dht2 == null) {
dht2 = new DoubleDHT_2D(rows, columns);
}
dht2.forward(elements);
ConcurrencyUtils.setNumberOfThreads(oldNthreads);
}
/**
* Computes the discrete Hartley transform (DHT) of each column of this
* matrix.
*
*/
public void dhtColumns() {
if (dhtColumns == null) {
dhtColumns = new DoubleDHT_1D(rows);
}
int nthreads = ConcurrencyUtils.getNumberOfThreads();
if ((nthreads > 1) && (size() >= ConcurrencyUtils.getThreadsBeginN_2D())) {
ConcurrencyUtils.setThreadsBeginN_1D_FFT_2Threads(Integer.MAX_VALUE);
ConcurrencyUtils.setThreadsBeginN_1D_FFT_4Threads(Integer.MAX_VALUE);
nthreads = Math.min(nthreads, columns);
Future>[] futures = new Future[nthreads];
int k = columns / nthreads;
for (int j = 0; j < nthreads; j++) {
final int firstCol = j * k;
final int lastCol = (j == nthreads - 1) ? columns : firstCol + k;
futures[j] = ConcurrencyUtils.submit(new Runnable() {
public void run() {
double[] column;
for (int c = firstCol; c < lastCol; c++) {
column = (double[]) viewColumn(c).copy().elements();
dhtColumns.forward(column);
viewColumn(c).assign(column);
}
}
});
}
ConcurrencyUtils.waitForCompletion(futures);
ConcurrencyUtils.resetThreadsBeginN_FFT();
} else {
double[] column;
for (int c = 0; c < columns; c++) {
column = (double[]) viewColumn(c).copy().elements();
dhtColumns.forward(column);
viewColumn(c).assign(column);
}
}
}
/**
* Computes the discrete Hartley transform (DHT) of each row of this matrix.
*
*/
public void dhtRows() {
if (dhtRows == null) {
dhtRows = new DoubleDHT_1D(columns);
}
int nthreads = ConcurrencyUtils.getNumberOfThreads();
if ((nthreads > 1) && (size() >= ConcurrencyUtils.getThreadsBeginN_2D())) {
ConcurrencyUtils.setThreadsBeginN_1D_FFT_2Threads(Integer.MAX_VALUE);
ConcurrencyUtils.setThreadsBeginN_1D_FFT_4Threads(Integer.MAX_VALUE);
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() {
for (int r = firstRow; r < lastRow; r++) {
dhtRows.forward(elements[r]);
}
}
});
}
ConcurrencyUtils.waitForCompletion(futures);
ConcurrencyUtils.resetThreadsBeginN_FFT();
} else {
for (int r = 0; r < rows; r++) {
dhtRows.forward(elements[r]);
}
}
}
/**
* Computes the 2D discrete sine transform (DST-II) of this matrix.
*
* @param scale
* if true then scaling is performed
*
*/
public void dst2(boolean scale) {
int oldNthreads = ConcurrencyUtils.getNumberOfThreads();
ConcurrencyUtils.setNumberOfThreads(ConcurrencyUtils.nextPow2(oldNthreads));
if (dst2 == null) {
dst2 = new DoubleDST_2D(rows, columns);
}
dst2.forward(elements, scale);
ConcurrencyUtils.setNumberOfThreads(oldNthreads);
}
/**
* Computes the discrete sine transform (DST-II) of each column of this
* matrix.
*
* @param scale
* if true then scaling is performed
*
*/
public void dstColumns(final boolean scale) {
if (dstColumns == null) {
dstColumns = new DoubleDST_1D(rows);
}
int nthreads = ConcurrencyUtils.getNumberOfThreads();
if ((nthreads > 1) && (size() >= ConcurrencyUtils.getThreadsBeginN_2D())) {
ConcurrencyUtils.setThreadsBeginN_1D_FFT_2Threads(Integer.MAX_VALUE);
ConcurrencyUtils.setThreadsBeginN_1D_FFT_4Threads(Integer.MAX_VALUE);
nthreads = Math.min(nthreads, columns);
Future>[] futures = new Future[nthreads];
int k = columns / nthreads;
for (int j = 0; j < nthreads; j++) {
final int firstCol = j * k;
final int lastCol = (j == nthreads - 1) ? columns : firstCol + k;
futures[j] = ConcurrencyUtils.submit(new Runnable() {
public void run() {
double[] column;
for (int c = firstCol; c < lastCol; c++) {
column = (double[]) viewColumn(c).copy().elements();
dstColumns.forward(column, scale);
viewColumn(c).assign(column);
}
}
});
}
ConcurrencyUtils.waitForCompletion(futures);
ConcurrencyUtils.resetThreadsBeginN_FFT();
} else {
double[] column;
for (int c = 0; c < columns; c++) {
column = (double[]) viewColumn(c).copy().elements();
dstColumns.forward(column, scale);
viewColumn(c).assign(column);
}
}
}
/**
* Computes the discrete sine transform (DST-II) of each row of this matrix.
*
* @param scale
* if true then scaling is performed
*
*/
public void dstRows(final boolean scale) {
if (dstRows == null) {
dstRows = new DoubleDST_1D(columns);
}
int nthreads = ConcurrencyUtils.getNumberOfThreads();
if ((nthreads > 1) && (size() >= ConcurrencyUtils.getThreadsBeginN_2D())) {
ConcurrencyUtils.setThreadsBeginN_1D_FFT_2Threads(Integer.MAX_VALUE);
ConcurrencyUtils.setThreadsBeginN_1D_FFT_4Threads(Integer.MAX_VALUE);
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() {
for (int r = firstRow; r < lastRow; r++) {
dstRows.forward(elements[r], scale);
}
}
});
}
ConcurrencyUtils.waitForCompletion(futures);
ConcurrencyUtils.resetThreadsBeginN_FFT();
} else {
for (int r = 0; r < rows; r++) {
dstRows.forward(elements[r], scale);
}
}
}
/**
* Computes the 2D discrete Fourier transform (DFT) of this matrix. The
* physical layout of the output data is as follows:
*
*
* this[k1][2*k2] = Re[k1][k2] = Re[rows-k1][columns-k2],
* this[k1][2*k2+1] = Im[k1][k2] = -Im[rows-k1][columns-k2],
* 0<k1<rows, 0<k2<columns/2,
* this[0][2*k2] = Re[0][k2] = Re[0][columns-k2],
* this[0][2*k2+1] = Im[0][k2] = -Im[0][columns-k2],
* 0<k2<columns/2,
* this[k1][0] = Re[k1][0] = Re[rows-k1][0],
* this[k1][1] = Im[k1][0] = -Im[rows-k1][0],
* this[rows-k1][1] = Re[k1][columns/2] = Re[rows-k1][columns/2],
* this[rows-k1][0] = -Im[k1][columns/2] = Im[rows-k1][columns/2],
* 0<k1<rows/2,
* this[0][0] = Re[0][0],
* this[0][1] = Re[0][columns/2],
* this[rows/2][0] = Re[rows/2][0],
* this[rows/2][1] = Re[rows/2][columns/2]
*
*
* This method computes only half of the elements of the real transform. The
* other half satisfies the symmetry condition. If you want the full real
* forward transform, use getFft2
. To get back the original
* data, use ifft2
.
*
* @throws IllegalArgumentException
* if the row size or the column size of this matrix is not a
* power of 2 number.
*
*/
public void fft2() {
int oldNthreads = ConcurrencyUtils.getNumberOfThreads();
ConcurrencyUtils.setNumberOfThreads(ConcurrencyUtils.nextPow2(oldNthreads));
if (fft2 == null) {
fft2 = new DoubleFFT_2D(rows, columns);
}
fft2.realForward(elements);
ConcurrencyUtils.setNumberOfThreads(oldNthreads);
}
/**
* Returns new complex matrix which is the 2D discrete Fourier transform
* (DFT) of this matrix.
*
* @return the 2D discrete Fourier transform (DFT) of this matrix.
*
*/
public DenseLargeDComplexMatrix2D getFft2() {
int oldNthreads = ConcurrencyUtils.getNumberOfThreads();
ConcurrencyUtils.setNumberOfThreads(ConcurrencyUtils.nextPow2(oldNthreads));
if (fft2 == null) {
fft2 = new DoubleFFT_2D(rows, columns);
}
DenseLargeDComplexMatrix2D C = new DenseLargeDComplexMatrix2D(rows, columns);
final double[][] elementsC = (C).elements();
int nthreads = ConcurrencyUtils.getNumberOfThreads();
if ((nthreads > 1) && (size() >= ConcurrencyUtils.getThreadsBeginN_2D())) {
nthreads = Math.min(nthreads, columns);
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() {
for (int r = firstRow; r < lastRow; r++) {
System.arraycopy(elements[r], 0, elementsC[r], 0, columns);
}
}
});
}
ConcurrencyUtils.waitForCompletion(futures);
} else {
for (int r = 0; r < rows; r++) {
System.arraycopy(elements[r], 0, elementsC[r], 0, columns);
}
}
fft2.realForwardFull(elementsC);
ConcurrencyUtils.setNumberOfThreads(oldNthreads);
return C;
}
/**
* Returns new complex matrix which is the discrete Fourier transform (DFT)
* of each column of this matrix.
*
* @return the discrete Fourier transform (DFT) of each column of this
* matrix.
*/
public DenseLargeDComplexMatrix2D getFftColumns() {
if (fftColumns == null) {
fftColumns = new DoubleFFT_1D(rows);
}
int oldNthreads = ConcurrencyUtils.getNumberOfThreads();
ConcurrencyUtils.setNumberOfThreads(ConcurrencyUtils.nextPow2(oldNthreads));
final DenseLargeDComplexMatrix2D C = new DenseLargeDComplexMatrix2D(rows, columns);
int nthreads = ConcurrencyUtils.getNumberOfThreads();
if ((nthreads > 1) && (size() >= ConcurrencyUtils.getThreadsBeginN_2D())) {
ConcurrencyUtils.setThreadsBeginN_1D_FFT_2Threads(Integer.MAX_VALUE);
ConcurrencyUtils.setThreadsBeginN_1D_FFT_4Threads(Integer.MAX_VALUE);
nthreads = Math.min(nthreads, columns);
Future>[] futures = new Future[nthreads];
int k = columns / nthreads;
for (int j = 0; j < nthreads; j++) {
final int firstCol = j * k;
final int lastColumn = (j == nthreads - 1) ? columns : firstCol + k;
futures[j] = ConcurrencyUtils.submit(new Runnable() {
public void run() {
for (int c = firstCol; c < lastColumn; c++) {
double[] column = new double[2 * rows];
System.arraycopy(viewColumn(c).copy().elements(), 0, column, 0, rows);
fftColumns.realForwardFull(column);
C.viewColumn(c).assign(column);
}
}
});
}
ConcurrencyUtils.waitForCompletion(futures);
ConcurrencyUtils.resetThreadsBeginN_FFT();
} else {
for (int c = 0; c < columns; c++) {
double[] column = new double[2 * rows];
System.arraycopy(viewColumn(c).copy().elements(), 0, column, 0, rows);
fftColumns.realForwardFull(column);
C.viewColumn(c).assign(column);
}
}
ConcurrencyUtils.setNumberOfThreads(oldNthreads);
return C;
}
/**
* Returns new complex matrix which is the discrete Fourier transform (DFT)
* of each row of this matrix.
*
* @return the discrete Fourier transform (DFT) of each row of this matrix.
*/
public DenseLargeDComplexMatrix2D getFftRows() {
if (fftRows == null) {
fftRows = new DoubleFFT_1D(columns);
}
int oldNthreads = ConcurrencyUtils.getNumberOfThreads();
ConcurrencyUtils.setNumberOfThreads(ConcurrencyUtils.nextPow2(oldNthreads));
final DenseLargeDComplexMatrix2D C = new DenseLargeDComplexMatrix2D(rows, columns);
int nthreads = ConcurrencyUtils.getNumberOfThreads();
if ((nthreads > 1) && (size() >= ConcurrencyUtils.getThreadsBeginN_2D())) {
ConcurrencyUtils.setThreadsBeginN_1D_FFT_2Threads(Integer.MAX_VALUE);
ConcurrencyUtils.setThreadsBeginN_1D_FFT_4Threads(Integer.MAX_VALUE);
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() {
for (int r = firstRow; r < lastRow; r++) {
double[] row = new double[2 * columns];
System.arraycopy(elements[r], 0, row, 0, columns);
fftRows.realForwardFull(row);
C.viewRow(r).assign(row);
}
}
});
}
ConcurrencyUtils.waitForCompletion(futures);
ConcurrencyUtils.resetThreadsBeginN_FFT();
} else {
for (int r = 0; r < rows; r++) {
double[] row = new double[2 * columns];
System.arraycopy(elements[r], 0, row, 0, columns);
fftRows.realForwardFull(row);
C.viewRow(r).assign(row);
}
}
ConcurrencyUtils.setNumberOfThreads(oldNthreads);
return C;
}
/**
* Returns new complex matrix which is the 2D inverse of the discrete
* Fourier transform (IDFT) of this matrix.
*
* @return the 2D inverse of the discrete Fourier transform (IDFT) of this
* matrix.
*/
public DenseLargeDComplexMatrix2D getIfft2(boolean scale) {
int oldNthreads = ConcurrencyUtils.getNumberOfThreads();
ConcurrencyUtils.setNumberOfThreads(ConcurrencyUtils.nextPow2(oldNthreads));
DenseLargeDComplexMatrix2D C = new DenseLargeDComplexMatrix2D(rows, columns);
final double[][] elementsC = (C).elements();
int nthreads = ConcurrencyUtils.getNumberOfThreads();
if ((nthreads > 1) && (size() >= ConcurrencyUtils.getThreadsBeginN_2D())) {
nthreads = Math.min(nthreads, columns);
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() {
for (int r = firstRow; r < lastRow; r++) {
System.arraycopy(elements[r], 0, elementsC[r], 0, columns);
}
}
});
}
ConcurrencyUtils.waitForCompletion(futures);
} else {
for (int r = 0; r < rows; r++) {
System.arraycopy(elements[r], 0, elementsC[r], 0, columns);
}
}
if (fft2 == null) {
fft2 = new DoubleFFT_2D(rows, columns);
}
fft2.realInverseFull(elementsC, scale);
ConcurrencyUtils.setNumberOfThreads(oldNthreads);
return C;
}
/**
* Returns new complex matrix which is the inverse of the discrete Fourier
* transform (IDFT) of each column of this matrix.
*
* @return the inverse of the discrete Fourier transform (IDFT) of each
* column of this matrix.
*/
public DenseLargeDComplexMatrix2D getIfftColumns(final boolean scale) {
if (fftColumns == null) {
fftColumns = new DoubleFFT_1D(rows);
}
int oldNthreads = ConcurrencyUtils.getNumberOfThreads();
ConcurrencyUtils.setNumberOfThreads(ConcurrencyUtils.nextPow2(oldNthreads));
final DenseLargeDComplexMatrix2D C = new DenseLargeDComplexMatrix2D(rows, columns);
int nthreads = ConcurrencyUtils.getNumberOfThreads();
if ((nthreads > 1) && (size() >= ConcurrencyUtils.getThreadsBeginN_2D())) {
ConcurrencyUtils.setThreadsBeginN_1D_FFT_2Threads(Integer.MAX_VALUE);
ConcurrencyUtils.setThreadsBeginN_1D_FFT_4Threads(Integer.MAX_VALUE);
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;
futures[j] = ConcurrencyUtils.submit(new Runnable() {
public void run() {
for (int c = firstColumn; c < lastColumn; c++) {
double[] column = new double[2 * rows];
System.arraycopy(viewColumn(c).copy().elements(), 0, column, 0, rows);
fftColumns.realInverseFull(column, scale);
C.viewColumn(c).assign(column);
}
}
});
}
ConcurrencyUtils.waitForCompletion(futures);
ConcurrencyUtils.resetThreadsBeginN_FFT();
} else {
for (int c = 0; c < columns; c++) {
double[] column = new double[2 * rows];
System.arraycopy(viewColumn(c).copy().elements(), 0, column, 0, rows);
fftColumns.realInverseFull(column, scale);
C.viewColumn(c).assign(column);
}
}
ConcurrencyUtils.setNumberOfThreads(oldNthreads);
return C;
}
/**
* Returns new complex matrix which is the inverse of the discrete Fourier
* transform (IDFT) of each row of this matrix.
*
* @return the inverse of the discrete Fourier transform (IDFT) of each row
* of this matrix.
*/
public DenseLargeDComplexMatrix2D getIfftRows(final boolean scale) {
if (fftRows == null) {
fftRows = new DoubleFFT_1D(columns);
}
int oldNthreads = ConcurrencyUtils.getNumberOfThreads();
ConcurrencyUtils.setNumberOfThreads(ConcurrencyUtils.nextPow2(oldNthreads));
final DenseLargeDComplexMatrix2D C = new DenseLargeDComplexMatrix2D(rows, columns);
int nthreads = ConcurrencyUtils.getNumberOfThreads();
if ((nthreads > 1) && (size() >= ConcurrencyUtils.getThreadsBeginN_2D())) {
ConcurrencyUtils.setThreadsBeginN_1D_FFT_2Threads(Integer.MAX_VALUE);
ConcurrencyUtils.setThreadsBeginN_1D_FFT_4Threads(Integer.MAX_VALUE);
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() {
for (int r = firstRow; r < lastRow; r++) {
double[] row = new double[2 * columns];
System.arraycopy(elements[r], 0, row, 0, columns);
fftRows.realInverseFull(row, scale);
C.viewRow(r).assign(row);
}
}
});
}
ConcurrencyUtils.waitForCompletion(futures);
ConcurrencyUtils.resetThreadsBeginN_FFT();
} else {
for (int r = 0; r < rows; r++) {
double[] row = new double[2 * columns];
System.arraycopy(elements[r], 0, row, 0, columns);
fftRows.realInverseFull(row, scale);
C.viewRow(r).assign(row);
}
}
ConcurrencyUtils.setNumberOfThreads(oldNthreads);
return C;
}
public double getQuick(int row, int column) {
return elements[row][column];
}
/**
* Computes the 2D inverse of the discrete cosine transform (DCT-III) of
* this matrix.
*
* @param scale
* if true then scaling is performed
*
*/
public void idct2(boolean scale) {
int oldNthreads = ConcurrencyUtils.getNumberOfThreads();
ConcurrencyUtils.setNumberOfThreads(ConcurrencyUtils.nextPow2(oldNthreads));
if (dct2 == null) {
dct2 = new DoubleDCT_2D(rows, columns);
}
dct2.inverse(elements, scale);
ConcurrencyUtils.setNumberOfThreads(oldNthreads);
}
/**
* Computes the inverse of the discrete cosine transform (DCT-III) of each
* column of this matrix.
*
* @param scale
* if true then scaling is performed
*
*/
public void idctColumns(final boolean scale) {
if (dctColumns == null) {
dctColumns = new DoubleDCT_1D(rows);
}
int nthreads = ConcurrencyUtils.getNumberOfThreads();
if ((nthreads > 1) && (size() >= ConcurrencyUtils.getThreadsBeginN_2D())) {
ConcurrencyUtils.setThreadsBeginN_1D_FFT_2Threads(Integer.MAX_VALUE);
ConcurrencyUtils.setThreadsBeginN_1D_FFT_4Threads(Integer.MAX_VALUE);
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;
futures[j] = ConcurrencyUtils.submit(new Runnable() {
public void run() {
double[] column;
for (int c = firstColumn; c < lastColumn; c++) {
column = (double[]) viewColumn(c).copy().elements();
dctColumns.inverse(column, scale);
viewColumn(c).assign(column);
}
}
});
}
ConcurrencyUtils.waitForCompletion(futures);
ConcurrencyUtils.resetThreadsBeginN_FFT();
} else {
double[] column;
for (int c = 0; c < columns; c++) {
column = (double[]) viewColumn(c).copy().elements();
dctColumns.inverse(column, scale);
viewColumn(c).assign(column);
}
}
}
/**
* Computes the inverse of the discrete cosine transform (DCT-III) of each
* row of this matrix.
*
* @param scale
* if true then scaling is performed
*
*/
public void idctRows(final boolean scale) {
if (dctRows == null) {
dctRows = new DoubleDCT_1D(columns);
}
int nthreads = ConcurrencyUtils.getNumberOfThreads();
if ((nthreads > 1) && (size() >= ConcurrencyUtils.getThreadsBeginN_2D())) {
ConcurrencyUtils.setThreadsBeginN_1D_FFT_2Threads(Integer.MAX_VALUE);
ConcurrencyUtils.setThreadsBeginN_1D_FFT_4Threads(Integer.MAX_VALUE);
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() {
for (int r = firstRow; r < lastRow; r++) {
dctRows.inverse(elements[r], scale);
}
}
});
}
ConcurrencyUtils.waitForCompletion(futures);
ConcurrencyUtils.resetThreadsBeginN_FFT();
} else {
for (int r = 0; r < rows; r++) {
dctRows.inverse(elements[r], scale);
}
}
}
/**
* Computes the 2D inverse of the discrete Hartley transform (IDHT) of this
* matrix.
*
* @param scale
* if true then scaling is performed
*
*/
public void idht2(boolean scale) {
int oldNthreads = ConcurrencyUtils.getNumberOfThreads();
ConcurrencyUtils.setNumberOfThreads(ConcurrencyUtils.nextPow2(oldNthreads));
if (dht2 == null) {
dht2 = new DoubleDHT_2D(rows, columns);
}
dht2.inverse(elements, scale);
ConcurrencyUtils.setNumberOfThreads(oldNthreads);
}
/**
* Computes the inverse of the discrete Hartley transform (IDHT) of each
* column of this matrix.
*
* @param scale
* if true then scaling is performed
*
*/
public void idhtColumns(final boolean scale) {
if (dhtColumns == null) {
dhtColumns = new DoubleDHT_1D(rows);
}
int nthreads = ConcurrencyUtils.getNumberOfThreads();
if ((nthreads > 1) && (size() >= ConcurrencyUtils.getThreadsBeginN_2D())) {
ConcurrencyUtils.setThreadsBeginN_1D_FFT_2Threads(Integer.MAX_VALUE);
ConcurrencyUtils.setThreadsBeginN_1D_FFT_4Threads(Integer.MAX_VALUE);
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;
futures[j] = ConcurrencyUtils.submit(new Runnable() {
public void run() {
double[] column;
for (int c = firstColumn; c < lastColumn; c++) {
column = (double[]) viewColumn(c).copy().elements();
dhtColumns.inverse(column, scale);
viewColumn(c).assign(column);
}
}
});
}
ConcurrencyUtils.waitForCompletion(futures);
ConcurrencyUtils.resetThreadsBeginN_FFT();
} else {
double[] column;
for (int c = 0; c < columns; c++) {
column = (double[]) viewColumn(c).copy().elements();
dhtColumns.inverse(column, scale);
viewColumn(c).assign(column);
}
}
}
/**
* Computes the inverse of the discrete Hartley transform (IDHT) of each row
* of this matrix.
*
* @param scale
* if true then scaling is performed
*
*/
public void idhtRows(final boolean scale) {
if (dhtRows == null) {
dhtRows = new DoubleDHT_1D(columns);
}
int nthreads = ConcurrencyUtils.getNumberOfThreads();
if ((nthreads > 1) && (size() >= ConcurrencyUtils.getThreadsBeginN_2D())) {
ConcurrencyUtils.setThreadsBeginN_1D_FFT_2Threads(Integer.MAX_VALUE);
ConcurrencyUtils.setThreadsBeginN_1D_FFT_4Threads(Integer.MAX_VALUE);
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() {
for (int r = firstRow; r < lastRow; r++) {
dhtRows.inverse(elements[r], scale);
}
}
});
}
ConcurrencyUtils.waitForCompletion(futures);
ConcurrencyUtils.resetThreadsBeginN_FFT();
} else {
for (int r = 0; r < rows; r++) {
dhtRows.inverse(elements[r], scale);
}
}
}
/**
* Computes the 2D inverse of the discrete sine transform (DST-III) of this
* matrix.
*
* @param scale
* if true then scaling is performed
*
*/
public void idst2(boolean scale) {
int oldNthreads = ConcurrencyUtils.getNumberOfThreads();
ConcurrencyUtils.setNumberOfThreads(ConcurrencyUtils.nextPow2(oldNthreads));
if (dst2 == null) {
dst2 = new DoubleDST_2D(rows, columns);
}
dst2.inverse(elements, scale);
ConcurrencyUtils.setNumberOfThreads(oldNthreads);
}
/**
* Computes the inverse of the discrete sine transform (DST-III) of each
* column of this matrix.
*
* @param scale
* if true then scaling is performed
*
*/
public void idstColumns(final boolean scale) {
if (dstColumns == null) {
dstColumns = new DoubleDST_1D(rows);
}
int nthreads = ConcurrencyUtils.getNumberOfThreads();
if ((nthreads > 1) && (size() >= ConcurrencyUtils.getThreadsBeginN_2D())) {
ConcurrencyUtils.setThreadsBeginN_1D_FFT_2Threads(Integer.MAX_VALUE);
ConcurrencyUtils.setThreadsBeginN_1D_FFT_4Threads(Integer.MAX_VALUE);
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;
futures[j] = ConcurrencyUtils.submit(new Runnable() {
public void run() {
double[] column;
for (int c = firstColumn; c < lastColumn; c++) {
column = (double[]) viewColumn(c).copy().elements();
dstColumns.inverse(column, scale);
viewColumn(c).assign(column);
}
}
});
}
ConcurrencyUtils.waitForCompletion(futures);
ConcurrencyUtils.resetThreadsBeginN_FFT();
} else {
double[] column;
for (int c = 0; c < columns; c++) {
column = (double[]) viewColumn(c).copy().elements();
dstColumns.inverse(column, scale);
viewColumn(c).assign(column);
}
}
}
/**
* Computes the inverse of the discrete sine transform (DST-III) of each row
* of this matrix.
*
* @param scale
* if true then scaling is performed
*
*/
public void idstRows(final boolean scale) {
if (dstRows == null) {
dstRows = new DoubleDST_1D(columns);
}
int nthreads = ConcurrencyUtils.getNumberOfThreads();
if ((nthreads > 1) && (size() >= ConcurrencyUtils.getThreadsBeginN_2D())) {
ConcurrencyUtils.setThreadsBeginN_1D_FFT_2Threads(Integer.MAX_VALUE);
ConcurrencyUtils.setThreadsBeginN_1D_FFT_4Threads(Integer.MAX_VALUE);
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() {
for (int r = firstRow; r < lastRow; r++) {
dstRows.inverse(elements[r], scale);
}
}
});
}
ConcurrencyUtils.waitForCompletion(futures);
ConcurrencyUtils.resetThreadsBeginN_FFT();
} else {
for (int r = 0; r < rows; r++) {
dstRows.inverse(elements[r], scale);
}
}
}
/**
* Computes the 2D inverse of the discrete Fourier transform (IDFT) of this
* matrix. The physical layout of the input data has to be as follows:
*
*
* this[k1][2*k2] = Re[k1][k2] = Re[rows-k1][columns-k2],
* this[k1][2*k2+1] = Im[k1][k2] = -Im[rows-k1][columns-k2],
* 0<k1<rows, 0<k2<columns/2,
* this[0][2*k2] = Re[0][k2] = Re[0][columns-k2],
* this[0][2*k2+1] = Im[0][k2] = -Im[0][columns-k2],
* 0<k2<columns/2,
* this[k1][0] = Re[k1][0] = Re[rows-k1][0],
* this[k1][1] = Im[k1][0] = -Im[rows-k1][0],
* this[rows-k1][1] = Re[k1][columns/2] = Re[rows-k1][columns/2],
* this[rows-k1][0] = -Im[k1][columns/2] = Im[rows-k1][columns/2],
* 0<k1<rows/2,
* this[0][0] = Re[0][0],
* this[0][1] = Re[0][columns/2],
* this[rows/2][0] = Re[rows/2][0],
* this[rows/2][1] = Re[rows/2][columns/2]
*
*
* This method computes only half of the elements of the real transform. The
* other half satisfies the symmetry condition. If you want the full real
* inverse transform, use getIfft2
.
*
* @throws IllegalArgumentException
* if the row size or the column size of this matrix is not a
* power of 2 number.
*
* @param scale
* if true then scaling is performed
*
*/
public void ifft2(boolean scale) {
int oldNthreads = ConcurrencyUtils.getNumberOfThreads();
ConcurrencyUtils.setNumberOfThreads(ConcurrencyUtils.nextPow2(oldNthreads));
if (fft2 == null) {
fft2 = new DoubleFFT_2D(rows, columns);
}
fft2.realInverse(elements, scale);
ConcurrencyUtils.setNumberOfThreads(oldNthreads);
}
public void setQuick(int row, int column, double value) {
elements[row][column] = value;
}
public double[][] elements() {
return elements;
}
protected DoubleMatrix2D getContent() {
return this;
}
public DoubleMatrix2D like(int rows, int columns) {
return new DenseLargeDoubleMatrix2D(rows, columns);
}
public DoubleMatrix1D like1D(int size) {
return new DenseDoubleMatrix1D(size);
}
}