All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cern.colt.matrix.tfloat.impl.DenseLargeFloatMatrix2D Maven / Gradle / Ivy

Go to download

Parallel Colt is a multithreaded version of Colt - a library for high performance scientific computing in Java. It contains efficient algorithms for data analysis, linear algebra, multi-dimensional arrays, Fourier transforms, statistics and histogramming.

The newest version!
/*
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.tfloat.impl;

import java.util.concurrent.Future;

import cern.colt.matrix.tfcomplex.impl.DenseLargeFComplexMatrix2D;
import cern.colt.matrix.tfloat.FloatMatrix1D;
import cern.colt.matrix.tfloat.FloatMatrix2D;
import edu.emory.mathcs.jtransforms.dct.FloatDCT_1D;
import edu.emory.mathcs.jtransforms.dct.FloatDCT_2D;
import edu.emory.mathcs.jtransforms.dht.FloatDHT_1D;
import edu.emory.mathcs.jtransforms.dht.FloatDHT_2D;
import edu.emory.mathcs.jtransforms.dst.FloatDST_1D;
import edu.emory.mathcs.jtransforms.dst.FloatDST_2D;
import edu.emory.mathcs.jtransforms.fft.FloatFFT_1D;
import edu.emory.mathcs.jtransforms.fft.FloatFFT_2D;
import edu.emory.mathcs.utils.ConcurrencyUtils;

/**
 * Dense 2-d matrix holding float 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 DenseLargeFloatMatrix2D extends WrapperFloatMatrix2D { private static final long serialVersionUID = 1L; private float[][] elements; private FloatFFT_2D fft2; private FloatDCT_2D dct2; private FloatDST_2D dst2; private FloatDHT_2D dht2; private FloatFFT_1D fftRows; private FloatFFT_1D fftColumns; private FloatDCT_1D dctRows; private FloatDCT_1D dctColumns; private FloatDST_1D dstRows; private FloatDST_1D dstColumns; private FloatDHT_1D dhtRows; private FloatDHT_1D dhtColumns; public DenseLargeFloatMatrix2D(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 float[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 FloatDCT_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 FloatDCT_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() { float[] column; for (int c = firstCol; c < lastCol; c++) { column = (float[]) viewColumn(c).copy().elements(); dctColumns.forward(column, scale); viewColumn(c).assign(column); } } }); } ConcurrencyUtils.waitForCompletion(futures); ConcurrencyUtils.resetThreadsBeginN_FFT(); } else { float[] column; for (int c = 0; c < columns; c++) { column = (float[]) 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 FloatDCT_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 FloatDHT_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 FloatDHT_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() { float[] column; for (int c = firstCol; c < lastCol; c++) { column = (float[]) viewColumn(c).copy().elements(); dhtColumns.forward(column); viewColumn(c).assign(column); } } }); } ConcurrencyUtils.waitForCompletion(futures); ConcurrencyUtils.resetThreadsBeginN_FFT(); } else { float[] column; for (int c = 0; c < columns; c++) { column = (float[]) 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 FloatDHT_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 FloatDST_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 FloatDST_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() { float[] column; for (int c = firstCol; c < lastCol; c++) { column = (float[]) viewColumn(c).copy().elements(); dstColumns.forward(column, scale); viewColumn(c).assign(column); } } }); } ConcurrencyUtils.waitForCompletion(futures); ConcurrencyUtils.resetThreadsBeginN_FFT(); } else { float[] column; for (int c = 0; c < columns; c++) { column = (float[]) 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 FloatDST_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 FloatFFT_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 DenseLargeFComplexMatrix2D getFft2() { int oldNthreads = ConcurrencyUtils.getNumberOfThreads(); ConcurrencyUtils.setNumberOfThreads(ConcurrencyUtils.nextPow2(oldNthreads)); if (fft2 == null) { fft2 = new FloatFFT_2D(rows, columns); } DenseLargeFComplexMatrix2D C = new DenseLargeFComplexMatrix2D(rows, columns); final float[][] 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 DenseLargeFComplexMatrix2D getFftColumns() { if (fftColumns == null) { fftColumns = new FloatFFT_1D(rows); } int oldNthreads = ConcurrencyUtils.getNumberOfThreads(); ConcurrencyUtils.setNumberOfThreads(ConcurrencyUtils.nextPow2(oldNthreads)); final DenseLargeFComplexMatrix2D C = new DenseLargeFComplexMatrix2D(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++) { float[] column = new float[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++) { float[] column = new float[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 DenseLargeFComplexMatrix2D getFftRows() { if (fftRows == null) { fftRows = new FloatFFT_1D(columns); } int oldNthreads = ConcurrencyUtils.getNumberOfThreads(); ConcurrencyUtils.setNumberOfThreads(ConcurrencyUtils.nextPow2(oldNthreads)); final DenseLargeFComplexMatrix2D C = new DenseLargeFComplexMatrix2D(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++) { float[] row = new float[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++) { float[] row = new float[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 DenseLargeFComplexMatrix2D getIfft2(boolean scale) { int oldNthreads = ConcurrencyUtils.getNumberOfThreads(); ConcurrencyUtils.setNumberOfThreads(ConcurrencyUtils.nextPow2(oldNthreads)); DenseLargeFComplexMatrix2D C = new DenseLargeFComplexMatrix2D(rows, columns); final float[][] elementsC = (C).elements(); 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() { 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 FloatFFT_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 DenseLargeFComplexMatrix2D getIfftColumns(final boolean scale) { if (fftColumns == null) { fftColumns = new FloatFFT_1D(rows); } int oldNthreads = ConcurrencyUtils.getNumberOfThreads(); ConcurrencyUtils.setNumberOfThreads(ConcurrencyUtils.nextPow2(oldNthreads)); final DenseLargeFComplexMatrix2D C = new DenseLargeFComplexMatrix2D(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++) { float[] column = new float[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++) { float[] column = new float[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 DenseLargeFComplexMatrix2D getIfftRows(final boolean scale) { if (fftRows == null) { fftRows = new FloatFFT_1D(columns); } int oldNthreads = ConcurrencyUtils.getNumberOfThreads(); ConcurrencyUtils.setNumberOfThreads(ConcurrencyUtils.nextPow2(oldNthreads)); final DenseLargeFComplexMatrix2D C = new DenseLargeFComplexMatrix2D(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++) { float[] row = new float[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++) { float[] row = new float[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 float 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 FloatDCT_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 FloatDCT_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() { float[] column; for (int c = firstColumn; c < lastColumn; c++) { column = (float[]) viewColumn(c).copy().elements(); dctColumns.inverse(column, scale); viewColumn(c).assign(column); } } }); } ConcurrencyUtils.waitForCompletion(futures); ConcurrencyUtils.resetThreadsBeginN_FFT(); } else { float[] column; for (int c = 0; c < columns; c++) { column = (float[]) 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 FloatDCT_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 FloatDHT_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 FloatDHT_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() { float[] column; for (int c = firstColumn; c < lastColumn; c++) { column = (float[]) viewColumn(c).copy().elements(); dhtColumns.inverse(column, scale); viewColumn(c).assign(column); } } }); } ConcurrencyUtils.waitForCompletion(futures); ConcurrencyUtils.resetThreadsBeginN_FFT(); } else { float[] column; for (int c = 0; c < columns; c++) { column = (float[]) 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 FloatDHT_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 FloatDST_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 FloatDST_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() { float[] column; for (int c = firstColumn; c < lastColumn; c++) { column = (float[]) viewColumn(c).copy().elements(); dstColumns.inverse(column, scale); viewColumn(c).assign(column); } } }); } ConcurrencyUtils.waitForCompletion(futures); ConcurrencyUtils.resetThreadsBeginN_FFT(); } else { float[] column; for (int c = 0; c < columns; c++) { column = (float[]) 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 FloatDST_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 FloatFFT_2D(rows, columns); } fft2.realInverse(elements, scale); ConcurrencyUtils.setNumberOfThreads(oldNthreads); } public void setQuick(int row, int column, float value) { elements[row][column] = value; } public float[][] elements() { return elements; } protected FloatMatrix2D getContent() { return this; } public FloatMatrix2D like(int rows, int columns) { return new DenseLargeFloatMatrix2D(rows, columns); } public FloatMatrix1D like1D(int size) { return new DenseFloatMatrix1D(size); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy