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

cern.colt.matrix.tdcomplex.DComplexMatrix2D 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.tdcomplex;

import java.util.ArrayList;
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.AbstractMatrix2D;
import cern.colt.matrix.tdouble.DoubleMatrix2D;
import cern.jet.math.tdcomplex.DComplex;
import edu.emory.mathcs.utils.ConcurrencyUtils;

/**
 * Abstract base class for 2-d matrices holding complex elements.
 * 
 * A matrix has a number of rows and columns, which are assigned upon instance
 * construction - The matrix's size is then rows()*columns(). Elements
 * are accessed via [row,column] coordinates. Legal coordinates range
 * from [0,0] to [rows()-1,columns()-1]. Any attempt to access
 * an element at a coordinate
 * column<0 || column>=columns() || row<0 || row>=rows()
 * will throw an IndexOutOfBoundsException.
 * 

* Note that this implementation is not synchronized. * * @author Piotr Wendykier ([email protected]) * */ public abstract class DComplexMatrix2D extends AbstractMatrix2D { private static final long serialVersionUID = 1L; /** * Makes this class non instantiable, but still let's others inherit from * it. */ protected DComplexMatrix2D() { } /** * Applies a function to each cell and aggregates the results. * * @param aggr * an aggregation function taking as first argument the current * aggregation and as second argument the transformed current * cell value. * @param f * a function transforming the current cell value. * @return the aggregated measure. * @see cern.jet.math.tdcomplex.DComplexFunctions */ public double[] aggregate(final cern.colt.function.tdcomplex.DComplexDComplexDComplexFunction aggr, final cern.colt.function.tdcomplex.DComplexDComplexFunction f) { double[] b = new double[2]; if (size() == 0) { b[0] = Double.NaN; b[1] = Double.NaN; return b; } double[] 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 double[] call() throws Exception { double[] a = f.apply(getQuick(firstRow, 0)); int d = 1; for (int r = firstRow; r < lastRow; r++) { for (int c = d; c < columns; c++) { a = aggr.apply(a, f.apply(getQuick(r, c))); } d = 0; } return a; } }); } a = ConcurrencyUtils.waitForCompletion(futures, aggr); } else { a = f.apply(getQuick(0, 0)); 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(getQuick(r, c))); } d = 0; } } return a; } /** * Applies a function to each corresponding cell of two matrices and * aggregates the results. * * @param aggr * an aggregation function taking as first argument the current * aggregation and as second argument the transformed current * cell values. * @param f * a function transforming the current cell values. * @return the aggregated measure. * @throws IllegalArgumentException * if * columns() != other.columns() || rows() != other.rows() * @see cern.jet.math.tdcomplex.DComplexFunctions */ public double[] aggregate(final DComplexMatrix2D other, final cern.colt.function.tdcomplex.DComplexDComplexDComplexFunction aggr, final cern.colt.function.tdcomplex.DComplexDComplexDComplexFunction f) { checkShape(other); double[] b = new double[2]; if (size() == 0) { b[0] = Double.NaN; b[1] = Double.NaN; return b; } double[] 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 double[] call() throws Exception { double[] a = f.apply(getQuick(firstRow, 0), other.getQuick(firstRow, 0)); int d = 1; for (int r = firstRow; r < lastRow; r++) { for (int c = d; c < columns; c++) { a = aggr.apply(a, f.apply(getQuick(r, c), other.getQuick(r, c))); } d = 0; } return a; } }); } a = ConcurrencyUtils.waitForCompletion(futures, aggr); } else { a = f.apply(getQuick(0, 0), other.getQuick(0, 0)); 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(getQuick(r, c), other.getQuick(r, c))); } d = 0; } } return a; } /** * Assigns the result of a function to each cell; * * @param f * a function object taking as argument the current cell's value. * @return this (for convenience only). * @see cern.jet.math.tdcomplex.DComplexFunctions */ public DComplexMatrix2D assign(final cern.colt.function.tdcomplex.DComplexDComplexFunction f) { 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++) { for (int c = 0; c < columns; c++) { setQuick(r, c, f.apply(getQuick(r, c))); } } } }); } ConcurrencyUtils.waitForCompletion(futures); } else { for (int r = 0; r < rows; r++) { for (int c = 0; c < columns; c++) { setQuick(r, c, f.apply(getQuick(r, c))); } } } return this; } /** * Assigns the result of a function to all cells that satisfy a condition. * * @param cond * a condition. * * @param f * a function object. * @return this (for convenience only). * @see cern.jet.math.tdcomplex.DComplexFunctions */ public DComplexMatrix2D assign(final cern.colt.function.tdcomplex.DComplexProcedure cond, final cern.colt.function.tdcomplex.DComplexDComplexFunction f) { 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() { double[] elem; for (int r = firstRow; r < lastRow; r++) { for (int c = 0; c < columns; c++) { elem = getQuick(r, c); if (cond.apply(elem) == true) { setQuick(r, c, f.apply(elem)); } } } } }); } ConcurrencyUtils.waitForCompletion(futures); } else { double[] elem; for (int r = 0; r < rows; r++) { for (int c = 0; c < columns; c++) { elem = getQuick(r, c); if (cond.apply(elem) == true) { setQuick(r, c, f.apply(elem)); } } } } return this; } /** * Assigns a value to all cells that satisfy a condition. * * @param cond * a condition. * * @param value * a value (re=value[0], im=value[1]). * @return this (for convenience only). * */ public DComplexMatrix2D assign(final cern.colt.function.tdcomplex.DComplexProcedure cond, final double[] value) { 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() { double[] elem; for (int r = firstRow; r < lastRow; r++) { for (int c = 0; c < columns; c++) { elem = getQuick(r, c); if (cond.apply(elem) == true) { setQuick(r, c, value); } } } } }); } ConcurrencyUtils.waitForCompletion(futures); } else { double[] elem; for (int r = 0; r < rows; r++) { for (int c = 0; c < columns; c++) { elem = getQuick(r, c); if (cond.apply(elem) == true) { setQuick(r, c, value); } } } } return this; } /** * Assigns the result of a function to the real part of the receiver. The * imaginary part of the receiver is reset to zero. * * @param f * a function object taking as argument the current cell's value. * @return this (for convenience only). * @see cern.jet.math.tdcomplex.DComplexFunctions */ public DComplexMatrix2D assign(final cern.colt.function.tdcomplex.DComplexRealFunction f) { 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++) { for (int c = 0; c < columns; c++) { double re = f.apply(getQuick(r, c)); setQuick(r, c, re, 0); } } } }); } ConcurrencyUtils.waitForCompletion(futures); } else { for (int r = 0; r < rows; r++) { for (int c = 0; c < columns; c++) { double re = f.apply(getQuick(r, c)); setQuick(r, c, re, 0); } } } 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 other * the source matrix to copy from (may be identical to the * receiver). * @return this (for convenience only). * @throws IllegalArgumentException * if * columns() != other.columns() || rows() != other.rows() */ public DComplexMatrix2D assign(DComplexMatrix2D other) { if (other == this) return this; checkShape(other); final DComplexMatrix2D otherLoc; if (haveSharedCells(other)) { otherLoc = other.copy(); } else { otherLoc = other; } 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++) { for (int c = 0; c < columns; c++) { setQuick(r, c, otherLoc.getQuick(r, c)); } } } }); } ConcurrencyUtils.waitForCompletion(futures); } else { for (int r = 0; r < rows; r++) { for (int c = 0; c < columns; c++) { setQuick(r, c, otherLoc.getQuick(r, c)); } } } return this; } /** * Assigns the result of a function to each cell. * * @param y * the secondary matrix to operate on. * @param f * 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.tdcomplex.DComplexFunctions */ public DComplexMatrix2D assign(final DComplexMatrix2D y, final cern.colt.function.tdcomplex.DComplexDComplexDComplexFunction f) { checkShape(y); 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++) { for (int c = 0; c < columns; c++) { setQuick(r, c, f.apply(getQuick(r, c), y.getQuick(r, c))); } } } }); } ConcurrencyUtils.waitForCompletion(futures); } else { for (int r = 0; r < rows; r++) { for (int c = 0; c < columns; c++) { setQuick(r, c, f.apply(getQuick(r, c), y.getQuick(r, c))); } } } return this; } /** * Assigns the result of a function to all cells with a given indexes * * @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, * @param rowList * row indexes. * @param columnList * column indexes. * * @return this (for convenience only). * @throws IllegalArgumentException * if * columns() != other.columns() || rows() != other.rows() * @see cern.jet.math.tdouble.DoubleFunctions */ public DComplexMatrix2D assign(final DComplexMatrix2D y, final cern.colt.function.tdcomplex.DComplexDComplexDComplexFunction function, IntArrayList rowList, IntArrayList columnList) { checkShape(y); final int size = rowList.size(); final int[] rowElements = rowList.elements(); final int[] columnElements = columnList.elements(); int nthreads = ConcurrencyUtils.getNumberOfThreads(); if ((nthreads > 1) && (size >= ConcurrencyUtils.getThreadsBeginN_2D())) { nthreads = Math.min(nthreads, rows); 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() { for (int i = firstIdx; i < lastIdx; i++) { setQuick(rowElements[i], columnElements[i], function.apply(getQuick(rowElements[i], columnElements[i]), y.getQuick(rowElements[i], columnElements[i]))); } } }); } ConcurrencyUtils.waitForCompletion(futures); } else { for (int i = 0; i < size; i++) { setQuick(rowElements[i], columnElements[i], function.apply(getQuick(rowElements[i], columnElements[i]), y.getQuick(rowElements[i], columnElements[i]))); } } return this; } /** * Sets all cells to the state specified by re and im. * * @param re * the real part of the value to be filled into the cells. * @param im * the imaginary part of the value to be filled into the cells. * * @return this (for convenience only). */ public DComplexMatrix2D assign(final double re, final double im) { 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++) { for (int c = 0; c < columns; c++) { setQuick(r, c, re, im); } } } }); } ConcurrencyUtils.waitForCompletion(futures); } else { for (int r = 0; r < rows; r++) { for (int c = 0; c < columns; c++) { setQuick(r, c, re, im); } } } return this; } /** * Sets all cells to the state specified by values. values * is required to have the form * re = values[row*rowStride+column*columnStride]; im = values[row*rowStride+column*columnStride+1] * 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()*2*columns(). */ public DComplexMatrix2D assign(final double[] values) { if (values.length != rows * 2 * columns) throw new IllegalArgumentException("Must have same length: length=" + values.length + "rows()*2*columns()=" + rows() * 2 * columns()); 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 = firstRow * columns * 2; for (int r = firstRow; r < lastRow; r++) { for (int c = 0; c < columns; c++) { setQuick(r, c, values[idx], values[idx + 1]); idx += 2; } } } }); } ConcurrencyUtils.waitForCompletion(futures); } else { int idx = 0; for (int r = 0; r < rows; r++) { for (int c = 0; c < columns; c++) { setQuick(r, c, values[idx], values[idx + 1]); idx += 2; } } } return this; } /** * Sets all cells to the state specified by values. values * is required to have the form * re = values[row][2*column]; im = values[row][2*column+1] 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 != 2*columns() * . */ public DComplexMatrix2D assign(final double[][] values) { int nthreads = ConcurrencyUtils.getNumberOfThreads(); if (values.length != rows) throw new IllegalArgumentException("Must have same number of rows: rows=" + values.length + "rows()=" + rows()); 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++) { double[] currentRow = values[r]; if (currentRow.length != 2 * columns) throw new IllegalArgumentException( "Must have same number of columns in every row: columns=" + currentRow.length + "2*columns()=" + 2 * columns()); for (int c = 0; c < columns; c++) { setQuick(r, c, currentRow[2 * c], currentRow[2 * c + 1]); } } } }); } ConcurrencyUtils.waitForCompletion(futures); } else { for (int r = 0; r < rows; r++) { double[] currentRow = values[r]; if (currentRow.length != 2 * columns) throw new IllegalArgumentException("Must have same number of columns in every row: columns=" + currentRow.length + "2*columns()=" + 2 * columns()); for (int c = 0; c < columns; c++) { setQuick(r, c, currentRow[2 * c], currentRow[2 * c + 1]); } } } return this; } /** * Sets all cells to the state specified by values. values * is required to have the form * re = values[row*rowStride+column*columnStride]; im = values[row*rowStride+column*columnStride+1] * 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()*2*columns(). */ public DComplexMatrix2D assign(final float[] values) { if (values.length != rows * 2 * columns) throw new IllegalArgumentException("Must have same length: length=" + values.length + "rows()*2*columns()=" + rows() * 2 * columns()); 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 = firstRow * columns * 2; for (int r = firstRow; r < lastRow; r++) { for (int c = 0; c < columns; c++) { setQuick(r, c, values[idx], values[idx + 1]); idx += 2; } } } }); } ConcurrencyUtils.waitForCompletion(futures); } else { int idx = 0; for (int r = 0; r < rows; r++) { for (int c = 0; c < columns; c++) { setQuick(r, c, values[idx], values[idx + 1]); idx += 2; } } } return this; } /** * Replaces imaginary part of the receiver with the values of another real * matrix. The real part of the receiver remains unchanged. Both matrices * must have the same size. * * @param other * the source matrix to copy from * @return this (for convenience only). * @throws IllegalArgumentException * if size() != other.size(). */ public DComplexMatrix2D assignImaginary(final DoubleMatrix2D other) { checkShape(other); 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++) { for (int c = 0; c < columns; c++) { double re = getQuick(r, c)[0]; double im = other.getQuick(r, c); setQuick(r, c, re, im); } } } }); } ConcurrencyUtils.waitForCompletion(futures); } else { for (int r = 0; r < rows; r++) { for (int c = 0; c < columns; c++) { double re = getQuick(r, c)[0]; double im = other.getQuick(r, c); setQuick(r, c, re, im); } } } return this; } /** * Replaces real part of the receiver with the values of another real * matrix. The imaginary part of the receiver remains unchanged. Both * matrices must have the same size. * * @param other * the source matrix to copy from * @return this (for convenience only). * @throws IllegalArgumentException * if size() != other.size(). */ public DComplexMatrix2D assignReal(final DoubleMatrix2D other) { checkShape(other); 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++) { for (int c = 0; c < columns; c++) { double re = other.getQuick(r, c); double im = getQuick(r, c)[1]; setQuick(r, c, re, im); } } } }); } ConcurrencyUtils.waitForCompletion(futures); } else { for (int r = 0; r < rows; r++) { for (int c = 0; c < columns; c++) { double re = other.getQuick(r, c); double im = getQuick(r, c)[1]; setQuick(r, c, re, im); } } } return this; } /** * Returns the number of cells having non-zero values; ignores tolerance. * * @return the number of cells having non-zero values. */ public int cardinality() { int cardinality = 0; int nthreads = ConcurrencyUtils.getNumberOfThreads(); if ((nthreads > 1) && (size() >= ConcurrencyUtils.getThreadsBeginN_2D())) { nthreads = Math.min(nthreads, rows); Future[] futures = new Future[nthreads]; Integer[] results = new Integer[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 Integer call() throws Exception { int cardinality = 0; double[] tmp = new double[2]; for (int r = firstRow; r < lastRow; r++) { for (int c = 0; c < columns; c++) { tmp = getQuick(r, c); if ((tmp[0] != 0.0) || (tmp[1] != 0.0)) cardinality++; } } return Integer.valueOf(cardinality); } }); } try { for (int j = 0; j < nthreads; j++) { results[j] = (Integer) futures[j].get(); } cardinality = results[0].intValue(); for (int j = 1; j < nthreads; j++) { cardinality += results[j].intValue(); } } catch (ExecutionException ex) { ex.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } else { double[] tmp = new double[2]; for (int r = 0; r < rows; r++) { for (int c = 0; c < columns; c++) { tmp = getQuick(r, c); if (tmp[0] != 0 || tmp[1] != 0) cardinality++; } } } return cardinality; } /** * Constructs and returns a deep copy of the receiver. *

* Note that the returned matrix is an independent deep copy. The * returned matrix is not backed by this matrix, so changes in the returned * matrix are not reflected in this matrix, and vice-versa. * * @return a deep copy of the receiver. */ public DComplexMatrix2D copy() { return like().assign(this); } /** * Returns whether all cells are equal to the given value. * * @param value * the value to test against. * @return true if all cells are equal to the given value, * false otherwise. */ public boolean equals(double[] value) { return cern.colt.matrix.tdcomplex.algo.DComplexProperty.DEFAULT.equals(this, value); } /** * Compares this object against the specified object. The result is * true if and only if the argument is not null * and is at least a DoubleMatrix2D object that has the same * number of columns and rows as the receiver and has exactly the same * values at the same coordinates. * * @param obj * the object to compare with. * @return true if the objects are the same; false * otherwise. */ public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (!(obj instanceof DComplexMatrix2D)) return false; return cern.colt.matrix.tdcomplex.algo.DComplexProperty.DEFAULT.equals(this, (DComplexMatrix2D) obj); } /** * Assigns the result of a function to each non-zero cell. Use this * method for fast special-purpose iteration. If you want to modify another * matrix instead of this (i.e. work in read-only mode), simply * return the input value unchanged. * * Parameters to function are as follows: first==row, * second==column, third==nonZeroValue. * * @param function * a function object taking as argument the current non-zero * cell's row, column and value. * @return this (for convenience only). */ public DComplexMatrix2D forEachNonZero(final cern.colt.function.tdcomplex.IntIntDComplexFunction function) { 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++) { for (int c = 0; c < columns; c++) { double[] value = getQuick(r, c); if (value[0] != 0 || value[1] != 0) { double[] v = function.apply(r, c, value); setQuick(r, c, v); } } } } }); } ConcurrencyUtils.waitForCompletion(futures); } else { for (int r = 0; r < rows; r++) { for (int c = 0; c < columns; c++) { double[] value = getQuick(r, c); if (value[0] != 0 || value[1] != 0) { double[] v = function.apply(r, c, value); setQuick(r, c, v); } } } } return this; } /** * Returns the matrix cell value at coordinate [row,column]. * * @param row * the index of the row-coordinate. * @param column * the index of the column-coordinate. * @return the value of the specified cell. * @throws IndexOutOfBoundsException * if * column<0 || column>=columns() || row<0 || row>=rows() */ public double[] get(int row, int column) { if (column < 0 || column >= columns || row < 0 || row >= rows) throw new IndexOutOfBoundsException("row:" + row + ", column:" + column); return getQuick(row, column); } /** * Returns a new matrix that is a complex conjugate of this matrix. If * unconjugated complex transposition is needed, one should use viewDice() * method. This method creates a new object (not a view), so changes in the * returned matrix are NOT reflected in this matrix. * * @return a complex conjugate matrix */ public DComplexMatrix2D getConjugateTranspose() { final DComplexMatrix2D transpose = this.viewDice().copy(); 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 firstRow = j * k; final int lastRow = (j == nthreads - 1) ? columns : firstRow + k; futures[j] = ConcurrencyUtils.submit(new Runnable() { public void run() { double[] tmp = new double[2]; for (int r = firstRow; r < lastRow; r++) { for (int c = 0; c < rows; c++) { tmp = transpose.getQuick(r, c); tmp[1] = -tmp[1]; transpose.setQuick(r, c, tmp); } } } }); } ConcurrencyUtils.waitForCompletion(futures); } else { double[] tmp = new double[2]; for (int r = 0; r < columns; r++) { for (int c = 0; c < rows; c++) { tmp = transpose.getQuick(r, c); tmp[1] = -tmp[1]; transpose.setQuick(r, c, tmp); } } } return transpose; } /** * Returns the elements of this matrix. * * @return the elements */ public abstract Object elements(); /** * Returns the imaginary part of this matrix * * @return the imaginary part */ public abstract DoubleMatrix2D getImaginaryPart(); /** * Fills the coordinates and values of cells having non-zero values into the * specified lists. Fills into the lists, starting at index 0. After this * call returns the specified lists all have a new size, the number of * non-zero values. *

* In general, fill order is unspecified. This implementation fills * like for (row = 0..rows-1) for (column = 0..columns-1) do ... . * However, subclasses are free to us any other order, even an order that * may change over time as cell values are changed. (Of course, result lists * indexes are guaranteed to correspond to the same cell). * * @param rowList * the list to be filled with row indexes, can have any size. * @param columnList * the list to be filled with column indexes, can have any size. * @param valueList * the list to be filled with values, can have any size. */ public void getNonZeros(final IntArrayList rowList, final IntArrayList columnList, final ArrayList valueList) { rowList.clear(); columnList.clear(); valueList.clear(); for (int r = 0; r < rows; r++) { for (int c = 0; c < columns; c++) { double[] value = getQuick(r, c); if (value[0] != 0 || value[1] != 0) { rowList.add(r); columnList.add(c); valueList.add(value); } } } } /** * 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 abstract double[] getQuick(int row, int column); /** * Returns the real part of this matrix * * @return the real part */ public abstract DoubleMatrix2D getRealPart(); /** * Construct and returns a new empty matrix of the same dynamic type * as the receiver, having the same number of rows and columns. For example, * if the receiver is an instance of type DenseComplexMatrix2D the * new matrix must also be of type DenseComplexMatrix2D. In * general, the new matrix should have internal parametrization as similar * as possible. * * @return a new empty matrix of the same dynamic type. */ public DComplexMatrix2D like() { return like(rows, columns); } /** * 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 * DenseComplexMatrix2D the new matrix must also be of type * DenseComplexMatrix2D. 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 abstract DComplexMatrix2D like(int rows, int 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 DenseComplexMatrix2D the new * matrix must be of type DenseComplexMatrix1D. * * @param size * the number of cells the matrix shall have. * @return a new matrix of the corresponding dynamic type. */ public abstract DComplexMatrix1D like1D(int size); /** * Sets the matrix cell at coordinate [row,column] to the specified * value. * * @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. * @throws IndexOutOfBoundsException * if * column<0 || column>=columns() || row<0 || row>=rows() */ public void set(int row, int column, double[] value) { if (column < 0 || column >= columns || row < 0 || row >= rows) throw new IndexOutOfBoundsException("row:" + row + ", column:" + column); setQuick(row, column, value); } /** * Sets the matrix cell at coordinate [row,column] to the specified * value. * * @param row * the index of the row-coordinate. * @param column * the index of the column-coordinate. * @param re * the real part of the value to be filled into the specified * cell. * @param im * the imaginary part of the value to be filled into the * specified cell. * @throws IndexOutOfBoundsException * if * column<0 || column>=columns() || row<0 || row>=rows() */ public void set(int row, int column, double re, double im) { if (column < 0 || column >= columns || row < 0 || row >= rows) throw new IndexOutOfBoundsException("row:" + row + ", column:" + column); setQuick(row, column, re, im); } /** * 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 re * the real part of the value to be filled into the specified * cell. * @param im * the imaginary part of the value to be filled into the * specified cell. * */ public abstract void setQuick(int row, int column, double re, double im); /** * 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 abstract void setQuick(int row, int column, double[] value); /** * Constructs and returns a 2-dimensional array containing the cell values. * The returned array values has the form * re = values[row][2*column]; im = values[row][2*column+1] and has * 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. * * @return an array filled with the values of the cells. */ public double[][] toArray() { final double[][] values = new double[rows][2 * columns]; 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() { double[] tmp; for (int r = firstRow; r < lastRow; r++) { for (int c = 0; c < columns; c++) { tmp = getQuick(r, c); values[r][2 * c] = tmp[0]; values[r][2 * c + 1] = tmp[1]; } } } }); } ConcurrencyUtils.waitForCompletion(futures); } else { double[] tmp; for (int r = 0; r < rows; r++) { for (int c = 0; c < columns; c++) { tmp = getQuick(r, c); values[r][2 * c] = tmp[0]; values[r][2 * c + 1] = tmp[1]; } } } return values; } /** * Returns a string representation using default formatting ("%.4f"). * * @return a string representation of the matrix. */ public String toString() { return toString("%.4f"); } /** * Returns a string representation using using given format * * @param format * @return a string representation of the matrix. * */ public String toString(String format) { StringBuffer s = new StringBuffer(String.format("ComplexMatrix2D: %d rows, %d columns\n\n", rows, columns)); double[] elem = new double[2]; for (int r = 0; r < rows; r++) { for (int c = 0; c < columns; c++) { elem = getQuick(r, c); if (elem[1] == 0) { s.append(String.format(format + "\t", elem[0])); continue; } if (elem[0] == 0) { s.append(String.format(format + "i\t", elem[1])); continue; } if (elem[1] < 0) { s.append(String.format(format + " - " + format + "i\t", elem[0], -elem[1])); continue; } s.append(String.format(format + " + " + format + "i\t", elem[0], elem[1])); } s.append("\n"); } return s.toString(); } /** * Returns a vector obtained by stacking the columns of this matrix on top * of one another. * * @return a vector of columns of this matrix. */ public abstract DComplexMatrix1D vectorize(); /** * Constructs and returns a new slice view representing the rows of * the given column. The returned view is backed by this matrix, so changes * in the returned view are reflected in this matrix, and vice-versa. To * obtain a slice view on subranges, construct a sub-ranging view ( * viewPart(...)), then apply this method to the sub-range view. * * @param column * the column to fix. * @return a new slice view. * @throws IndexOutOfBoundsException * if column < 0 || column >= columns(). * @see #viewRow(int) */ public DComplexMatrix1D viewColumn(int column) { checkColumn(column); int viewSize = this.rows; int viewZero = (int) index(0, column); int viewStride = this.rowStride; return like1D(viewSize, viewZero, viewStride); } /** * Constructs and returns a new flip view along the column axis. What * used to be column 0 is now column columns()-1, ..., * what used to be column columns()-1 is now column 0. The * returned view is backed by this matrix, so changes in the returned view * are reflected in this matrix, and vice-versa. * * @return a new flip view. * @see #viewRowFlip() */ public DComplexMatrix2D viewColumnFlip() { return (DComplexMatrix2D) (view().vColumnFlip()); } /** * Constructs and returns a new dice (transposition) view; Swaps * axes; example: 3 x 4 matrix --> 4 x 3 matrix. The view has both * dimensions exchanged; what used to be columns become rows, what used to * be rows become columns. This is a zero-copy transposition, taking O(1), * i.e. constant time. The returned view is backed by this matrix, so * changes in the returned view are reflected in this matrix, and * vice-versa. Use idioms like result = viewDice(A).copy() to * generate an independent transposed matrix. * * @return a new dice view. */ public DComplexMatrix2D viewDice() { return (DComplexMatrix2D) (view().vDice()); } /** * Constructs and returns a new sub-range view that is a * height x width sub matrix starting at [row,column]. * * Operations on the returned view can only be applied to the restricted * range. Any attempt to access coordinates not contained in the view will * throw an IndexOutOfBoundsException. *

* Note that the view is really just a range restriction: The * returned matrix is backed by this matrix, so changes in the returned * matrix are reflected in this matrix, and vice-versa. *

* The view contains the cells from [row,column] to * [row+height-1,column+width-1], all inclusive. and has * view.rows() == height; view.columns() == width;. A view's legal * coordinates are again zero based, as usual. In other words, legal * coordinates of the view range from [0,0] to * [view.rows()-1==height-1,view.columns()-1==width-1]. As usual, * any attempt to access a cell at a coordinate * column<0 || column>=view.columns() || row<0 || row>=view.rows() * will throw an IndexOutOfBoundsException. * * @param row * The index of the row-coordinate. * @param column * The index of the column-coordinate. * @param height * The height of the box. * @param width * The width of the box. * @throws IndexOutOfBoundsException * if * column<0 || width<0 || column+width>columns() || row<0 || height<0 || row+height>rows() * @return the new view. * */ public DComplexMatrix2D viewPart(int row, int column, int height, int width) { return (DComplexMatrix2D) (view().vPart(row, column, height, width)); } /** * Constructs and returns a new slice view representing the columns * of the given row. The returned view is backed by this matrix, so changes * in the returned view are reflected in this matrix, and vice-versa. To * obtain a slice view on subranges, construct a sub-ranging view ( * viewPart(...)), then apply this method to the sub-range view. * * @param row * the row to fix. * @return a new slice view. * @throws IndexOutOfBoundsException * if row < 0 || row >= rows(). * @see #viewColumn(int) */ public DComplexMatrix1D viewRow(int row) { checkRow(row); int viewSize = this.columns; int viewZero = (int) index(row, 0); int viewStride = this.columnStride; return like1D(viewSize, viewZero, viewStride); } /** * Constructs and returns a new flip view along the row axis. What * used to be row 0 is now row rows()-1, ..., what used to * be row rows()-1 is now row 0. The returned view is * backed by this matrix, so changes in the returned view are reflected in * this matrix, and vice-versa. * * @return a new flip view. * @see #viewColumnFlip() */ public DComplexMatrix2D viewRowFlip() { return (DComplexMatrix2D) (view().vRowFlip()); } /** * Constructs and returns a new selection view that is a matrix * holding all rows matching the given condition. Applies the * condition to each row and takes only those row where * condition.apply(viewRow(i)) yields true. To match * columns, use a dice view. * * @param condition * The condition to be matched. * @return the new view. */ public DComplexMatrix2D viewSelection(DComplexMatrix1DProcedure condition) { IntArrayList matches = new IntArrayList(); for (int i = 0; i < rows; i++) { if (condition.apply(viewRow(i))) matches.add(i); } matches.trimToSize(); return viewSelection(matches.elements(), null); // take all columns } /** * Constructs and returns a new selection view that is a matrix * holding the indicated cells. There holds * view.rows() == rowIndexes.length, view.columns() == columnIndexes.length * and view.get(i,j) == this.get(rowIndexes[i],columnIndexes[j]). * Indexes can occur multiple times and can be in arbitrary order. * * Note that modifying the index arguments after this call has returned has * no effect on the view. The returned view is backed by this matrix, so * changes in the returned view are reflected in this matrix, and * vice-versa. *

* To indicate "all" rows or "all columns", simply set the respective * parameter * * @param rowIndexes * The rows of the cells that shall be visible in the new view. * To indicate that all rows shall be visible, simply set * this parameter to null. * @param columnIndexes * The columns of the cells that shall be visible in the new * view. To indicate that all columns shall be visible, * simply set this parameter to null. * @return the new view. * @throws IndexOutOfBoundsException * if !(0 <= rowIndexes[i] < rows()) for any * i=0..rowIndexes.length()-1. * @throws IndexOutOfBoundsException * if !(0 <= columnIndexes[i] < columns()) for any * i=0..columnIndexes.length()-1. */ public DComplexMatrix2D viewSelection(int[] rowIndexes, int[] columnIndexes) { // check for "all" if (rowIndexes == null) { rowIndexes = new int[rows]; for (int i = 0; i < rows; i++) rowIndexes[i] = i; } if (columnIndexes == null) { columnIndexes = new int[columns]; for (int i = 0; i < columns; i++) columnIndexes[i] = i; } checkRowIndexes(rowIndexes); checkColumnIndexes(columnIndexes); int[] rowOffsets = new int[rowIndexes.length]; int[] columnOffsets = new int[columnIndexes.length]; for (int i = 0; i < rowIndexes.length; i++) { rowOffsets[i] = _rowOffset(_rowRank(rowIndexes[i])); } for (int i = 0; i < columnIndexes.length; i++) { columnOffsets[i] = _columnOffset(_columnRank(columnIndexes[i])); } return viewSelectionLike(rowOffsets, columnOffsets); } /** * Constructs and returns a new stride view which is a sub matrix * consisting of every i-th cell. More specifically, the view has * this.rows()/rowStride rows and * this.columns()/columnStride columns holding cells * this.get(i*rowStride,j*columnStride) for all * i = 0..rows()/rowStride - 1, j = 0..columns()/columnStride - 1. * The returned view is backed by this matrix, so changes in the returned * view are reflected in this matrix, and vice-versa. * * @param rowStride * the row step factor. * @param columnStride * the column step factor. * @return a new view. * @throws IndexOutOfBoundsException * if rowStride<=0 || columnStride<=0. */ public DComplexMatrix2D viewStrides(int rowStride, int columnStride) { return (DComplexMatrix2D) (view().vStrides(rowStride, columnStride)); } /** * Linear algebraic matrix-vector multiplication; z = A * y; * Equivalent to return A.zMult(y,z,1,0); * * @param y * the source vector. * @param z * the vector where results are to be stored. Set this parameter * to null to indicate that a new result vector shall be * constructed. * @return z (for convenience only). */ public DComplexMatrix1D zMult(DComplexMatrix1D y, DComplexMatrix1D z) { return zMult(y, z, new double[] { 1, 0 }, (z == null ? new double[] { 1, 0 } : new double[] { 0, 0 }), false); } /** * Linear algebraic matrix-vector multiplication; * z = alpha * A * y + beta*z. Where A == this.
* Note: Matrix shape conformance is checked after potential * transpositions. * * @param y * the source vector. * @param z * the vector where results are to be stored. Set this parameter * to null to indicate that a new result vector shall be * constructed. * @return z (for convenience only). * * @throws IllegalArgumentException * if A.columns() != y.size() || A.rows() > z.size()). */ public DComplexMatrix1D zMult(final DComplexMatrix1D y, DComplexMatrix1D z, final double[] alpha, final double[] beta, boolean transposeA) { if (transposeA) return getConjugateTranspose().zMult(y, z, alpha, beta, false); final DComplexMatrix1D zz; if (z == null) { zz = y.like(this.rows); } else { zz = z; } if (columns != y.size() || rows > zz.size()) throw new IllegalArgumentException("Incompatible args: " + toStringShort() + ", " + y.toStringShort() + ", " + zz.toStringShort()); 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() { double[] s = new double[2]; for (int r = firstRow; r < lastRow; r++) { s[0] = 0; s[1] = 0; for (int c = 0; c < columns; c++) { s = DComplex.plus(s, DComplex.mult(getQuick(r, c), y.getQuick(c))); } zz.setQuick(r, DComplex.plus(DComplex.mult(s, alpha), DComplex.mult(zz.getQuick(r), beta))); } } }); } ConcurrencyUtils.waitForCompletion(futures); } else { double[] s = new double[2]; for (int r = 0; r < rows; r++) { s[0] = 0; s[1] = 0; for (int c = 0; c < columns; c++) { s = DComplex.plus(s, DComplex.mult(getQuick(r, c), y.getQuick(c))); } zz.setQuick(r, DComplex.plus(DComplex.mult(s, alpha), DComplex.mult(zz.getQuick(r), beta))); } } return zz; } /** * Linear algebraic matrix-matrix multiplication; C = A x B; * Equivalent to A.zMult(B,C,1,0,false,false). * * @param B * the second source matrix. * @param C * the matrix where results are to be stored. Set this parameter * to null to indicate that a new result matrix shall be * constructed. * @return C (for convenience only). */ public DComplexMatrix2D zMult(DComplexMatrix2D B, DComplexMatrix2D C) { return zMult(B, C, new double[] { 1, 0 }, (C == null ? new double[] { 1, 0 } : new double[] { 0, 0 }), false, false); } /** * Linear algebraic matrix-matrix multiplication; * C = alpha * A x B + beta*C. Matrix shapes: * A(m x n), B(n x p), C(m x p).
* Note: Matrix shape conformance is checked after potential * transpositions. * * @param B * the second source matrix. * @param C * the matrix where results are to be stored. Set this parameter * to null to indicate that a new result matrix shall be * constructed. * @return C (for convenience only). * * @throws IllegalArgumentException * if B.rows() != A.columns(). * @throws IllegalArgumentException * if * C.rows() != A.rows() || C.columns() != B.columns(). * @throws IllegalArgumentException * if A == C || B == C. */ public DComplexMatrix2D zMult(final DComplexMatrix2D B, DComplexMatrix2D C, final double[] alpha, final double[] beta, boolean transposeA, boolean transposeB) { if (transposeA) return getConjugateTranspose().zMult(B, C, alpha, beta, false, transposeB); if (transposeB) return this.zMult(B.getConjugateTranspose(), C, alpha, beta, transposeA, false); final int m = rows; final int n = columns; final int p = B.columns; final DComplexMatrix2D CC; if (C == null) { CC = like(m, p); } else { CC = C; } if (B.rows != n) throw new IllegalArgumentException("Matrix2D inner dimensions must agree:" + toStringShort() + ", " + B.toStringShort()); if (CC.rows != m || CC.columns != p) throw new IllegalArgumentException("Incompatibe result matrix: " + toStringShort() + ", " + B.toStringShort() + ", " + CC.toStringShort()); if (this == CC || B == CC) throw new IllegalArgumentException("Matrices must not be identical"); int nthreads = ConcurrencyUtils.getNumberOfThreads(); if ((nthreads > 1) && (size() >= ConcurrencyUtils.getThreadsBeginN_2D())) { nthreads = Math.min(nthreads, p); Future[] futures = new Future[nthreads]; int k = p / nthreads; for (int j = 0; j < nthreads; j++) { final int firstIdx = j * k; final int lastIdx = (j == nthreads - 1) ? p : firstIdx + k; futures[j] = ConcurrencyUtils.submit(new Runnable() { public void run() { double[] s = new double[2]; for (int a = firstIdx; a < lastIdx; a++) { for (int b = 0; b < m; b++) { s[0] = 0; s[1] = 0; for (int c = 0; c < n; c++) { s = DComplex.plus(s, DComplex.mult(getQuick(b, c), B.getQuick(c, a))); } CC.setQuick(b, a, DComplex.plus(DComplex.mult(s, alpha), DComplex.mult(CC .getQuick(b, a), beta))); } } } }); } ConcurrencyUtils.waitForCompletion(futures); } else { double[] s = new double[2]; for (int a = 0; a < p; a++) { for (int b = 0; b < m; b++) { s[0] = 0; s[1] = 0; for (int c = 0; c < n; c++) { s = DComplex.plus(s, DComplex.mult(getQuick(b, c), B.getQuick(c, a))); } CC.setQuick(b, a, DComplex.plus(DComplex.mult(s, alpha), DComplex.mult(CC.getQuick(b, a), beta))); } } } return CC; } /** * Returns the sum of all cells. * * @return the sum. */ public double[] zSum() { if (size() == 0) return new double[] { 0, 0 }; return aggregate(cern.jet.math.tdcomplex.DComplexFunctions.plus, cern.jet.math.tdcomplex.DComplexFunctions.identity); } /** * Returns the content of this matrix if it is a wrapper; or this * otherwise. Override this method in wrappers. * * @return this */ protected DComplexMatrix2D getContent() { return this; } /** * Returns true if both matrices share at least one identical cell. * * @param other * matrix * @return true if both matrices share at least one identical cell. */ protected boolean haveSharedCells(DComplexMatrix2D other) { if (other == null) return false; if (this == other) return true; return getContent().haveSharedCellsRaw(other.getContent()); } /** * Always returns false * * @param other * matrix * @return false */ protected boolean haveSharedCellsRaw(DComplexMatrix2D other) { return false; } /** * 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 DenseComplexMatrix2D the new matrix must be of * type DenseComplexMatrix1D. * * @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 abstract DComplexMatrix1D like1D(int size, int zero, int stride); /** * Constructs and returns a new view equal to the receiver. The view is a * shallow clone. Calls clone() and casts the result. *

* Note that the view is not a deep copy. The returned matrix is * backed by this matrix, so changes in the returned matrix are reflected in * this matrix, and vice-versa. *

* Use {@link #copy()} to construct an independent deep copy rather than a * new view. * * @return a new view of the receiver. */ protected DComplexMatrix2D view() { return (DComplexMatrix2D) clone(); } /** * 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 abstract DComplexMatrix2D viewSelectionLike(int[] rowOffsets, int[] columnOffsets); }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy