//# Array2Math.cc: Arithmetic functions defined on Arrays //# Copyright (C) 1993,1994,1995,1996,1999,2000,2001,2002 //# Associated Universities, Inc. Washington DC, USA. //# //# This library is free software; you can redistribute it and/or modify it //# under the terms of the GNU Library General Public License as published by //# the Free Software Foundation; either version 2 of the License, or (at your //# option) any later version. //# //# This library is distributed in the hope that it will be useful, but WITHOUT //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public //# License for more details. //# //# You should have received a copy of the GNU Library General Public License //# along with this library; if not, write to the Free Software Foundation, //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA. //# //# Correspondence concerning AIPS++ should be addressed as follows: //# Internet email: aips2-request@nrao.edu. //# Postal address: AIPS++ Project Office //# National Radio Astronomy Observatory //# 520 Edgemont Road //# Charlottesville, VA 22903-2475 USA //# //# $Id$ #include "ArrayMath.h" #include "ArrayError.h" #include "Matrix.h" #include namespace casacore { //# NAMESPACE CASACORE - BEGIN //# We could use macros to considerably reduce the number of lines, however //# that makes it harder to debug, understand, etc. Array> conj(const Array> &carray) { return arrayTransformResult (carray, [](std::complex v) { return std::conj(v); }); } Array> conj(const Array> &carray) { return arrayTransformResult (carray, [](std::complex v) { return std::conj(v); }); } Matrix> conj(const Matrix> &carray) { return Matrix>(conj ((const Array>&)carray)); } Matrix> conj(const Matrix> &carray) { return Matrix>(conj ((const Array>&)carray)); } void conj(Array> &rarray, const Array> &carray) { checkArrayShapes (carray, rarray, "conj"); arrayTransform (carray, rarray, [](std::complex v) { return std::conj(v); }); } void conj(Array> &rarray, const Array> &carray) { checkArrayShapes (carray, rarray, "conj"); arrayTransform (carray, rarray, [](std::complex v) { return std::conj(v); }); } void real(Array &rarray, const Array> &carray) { checkArrayShapes (carray, rarray, "real"); // std::real is only a template since c++14 :( arrayTransform (carray, rarray, [](std::complex v) { return std::real(v); }); } void real(Array &rarray, const Array> &carray) { checkArrayShapes (carray, rarray, "real"); arrayTransform (carray, rarray, [](std::complex v) { return std::real(v); }); } void imag(Array &rarray, const Array> &carray) { checkArrayShapes (carray, rarray, "imag"); arrayTransform (carray, rarray, [](std::complex v) { return std::imag(v); }); } void imag(Array &rarray, const Array> &carray) { checkArrayShapes (carray, rarray, "imag"); arrayTransform (carray, rarray, [](std::complex v) { return std::imag(v); }); } void amplitude(Array &rarray, const Array> &carray) { checkArrayShapes (carray, rarray, "amplitude"); arrayTransform (carray, rarray, std::abs); } void amplitude(Array &rarray, const Array> &carray) { checkArrayShapes (carray, rarray, "amplitude"); arrayTransform (carray, rarray, std::abs); } void phase(Array &rarray, const Array> &carray) { checkArrayShapes (carray, rarray, "pahse"); arrayTransform (carray, rarray, [](std::complex v) { return std::arg(v); }); } void phase(Array &rarray, const Array> &carray) { checkArrayShapes (carray, rarray, "phase"); arrayTransform (carray, rarray, [](std::complex v) { return std::arg(v); }); } Array real(const Array> &carray) { Array rarray(carray.shape()); real(rarray, carray); return rarray; } Array real(const Array> &carray) { Array rarray(carray.shape()); real(rarray, carray); return rarray; } Array imag(const Array> &carray) { Array rarray(carray.shape()); imag(rarray, carray); return rarray; } Array imag(const Array> &carray) { Array rarray(carray.shape()); imag(rarray, carray); return rarray; } Array amplitude(const Array> &carray) { Array rarray(carray.shape()); amplitude(rarray, carray); return rarray; } Array amplitude(const Array> &carray) { Array rarray(carray.shape()); amplitude(rarray, carray); return rarray; } Array phase(const Array> &carray) { Array rarray(carray.shape()); phase(rarray, carray); return rarray; } Array phase(const Array> &carray) { Array rarray(carray.shape()); phase(rarray, carray); return rarray; } // // ArrayError // void ComplexToReal(Array &rarray, const Array> &carray) { if (rarray.nelements() != 2*carray.nelements()) { throw(ArrayError("::ComplexToReal(Array &rarray, const " "Array> &carray) - rarray.nelements() != " "2*carray.nelements()")); } if (rarray.contiguousStorage() && carray.contiguousStorage()) { memcpy (const_cast(rarray.data()), carray.data(), rarray.nelements() * sizeof(float)); } else { Array>::const_iterator citer=carray.begin(); Array::iterator rend = rarray.end(); for (Array::iterator riter = rarray.begin(); riter!=rend; ++riter, ++citer) { *riter = real(*citer); ++riter; *riter = imag(*citer); } } } // // ArrayError // void ComplexToReal(Array &rarray, const Array> &carray) { if (rarray.nelements() != 2*carray.nelements()) { throw(ArrayError("::ComplexToReal(Array &rarray, const " "Array> &carray) - rarray.nelements() != " "2*carray.nelements()")); } if (rarray.contiguousStorage() && carray.contiguousStorage()) { memcpy (const_cast(rarray.data()), carray.data(), rarray.nelements() * sizeof(double)); } else { Array>::const_iterator citer=carray.begin(); Array::iterator rend = rarray.end(); for (Array::iterator riter = rarray.begin(); riter!=rend; ++riter, ++citer) { *riter = real(*citer); ++riter; *riter = imag(*citer); } } } Array ComplexToReal(const Array> &carray) { IPosition shape = carray.shape(); shape(0) *= 2; Array retval(shape); ComplexToReal(retval, carray); return retval; } Array ComplexToReal(const Array> &carray) { IPosition shape = carray.shape(); shape(0) *= 2; Array retval(shape); ComplexToReal(retval, carray); return retval; } // // ArrayError // void RealToComplex(Array> &carray, const Array &rarray) { if (rarray.nelements() != 2*carray.nelements()) { throw(ArrayError("::RealToComplex(Array> &carray, const " "Array &rarray) - rarray.nelements() != " "2*carray.nelements()")); } if (rarray.contiguousStorage() && carray.contiguousStorage()) { std::copy_n(rarray.data(), rarray.nelements(), reinterpret_cast(const_cast*>(carray.data()))); } else { Array>::iterator citer=carray.begin(); Array::const_iterator rend = rarray.end(); for (Array::const_iterator riter = rarray.begin(); riter!=rend; ++riter, ++citer) { float r = *riter; ++riter; *citer = std::complex(r, *riter); } } } // // ArrayError // void RealToComplex(Array> &carray, const Array &rarray) { if (rarray.nelements() != 2*carray.nelements()) { throw(ArrayError("::RealToComplex(Array> &carray, const " "Array &rarray) - rarray.nelements() != " "2*carray.nelements()")); } if (rarray.contiguousStorage() && carray.contiguousStorage()) { std::copy_n(rarray.data(), rarray.nelements(), reinterpret_cast(const_cast*>(carray.data()))); } else { Array>::iterator citer=carray.begin(); Array::const_iterator rend = rarray.end(); for (Array::const_iterator riter = rarray.begin(); riter!=rend; ++riter, ++citer) { double r = *riter; ++riter; *citer = std::complex(r, *riter); } } } // // ArrayError // Array> RealToComplex(const Array &rarray) { IPosition shape = rarray.shape(); if (shape(0) %2 == 1) { // Odd size throw(ArrayError("Array> RealToComplex(const Array &" "rarray) - rarray.shape()(0) not even")); } shape(0) /= 2; Array> retval(shape); RealToComplex(retval, rarray); return retval; } // // ArrayError // Array> RealToComplex(const Array &rarray) { IPosition shape = rarray.shape(); if (shape(0) %2 == 1) { // Odd size throw(ArrayError("Array> RealToComplex(const Array &" "rarray) - rarray.shape()(0) not even")); } shape(0) /= 2; Array> retval(shape); RealToComplex(retval, rarray); return retval; } IPosition checkExpandArray (IPosition& mult, IPosition& inshp, const IPosition& inShape, const IPosition& outShape, const IPosition& alternate) { if (inShape.size() == 0) { throw ArrayError("expandArray: input array cannot be empty"); } mult.resize (outShape.size()); inshp.resize (outShape.size()); inshp = 1; // missing axes have length 1 IPosition alt(outShape.size(), 0); for (size_t i=0; i outShape[i] || outShape[i] % inshp[i] != 0) { throw ArrayError("expandArray: length of each input array axis must " "be <= output axis and divide evenly"); } // Note that for an input length 1, linear and alternate come to the same. // Linear is faster, so use that if possible. if (i < alternate.size() && inshp[i] > 1) { alt[i] = alternate[i]; } mult[i] = outShape[i] / inshp[i]; } return alt; } } //# NAMESPACE CASACORE - END