Commit c154949a authored by Andi Braimllari's avatar Andi Braimllari Committed by David Frank
Browse files

#105 Add real() and imag() free function

For a complex DataContainer real() and imag() return the real
and imaginary part of it respectively. The return type is a
real valued DataContainer.

Note, similar to the standard library (std::real and std::imag),
non-complex DataContainers are treated as complex with zero
imaginary component.
parent 13334211
Pipeline #703793 passed with stages
in 11 minutes and 28 seconds
......@@ -691,6 +691,32 @@ namespace elsa
return Expression{Callables{log, logGPU}, operand};
#else
return Expression{log, operand};
#endif
}
/// Element-wise real parts of the Operand
template <typename Operand, typename = std::enable_if_t<isDcOrExpr<Operand>>>
auto real(Operand const& operand)
{
auto real = [](auto const& operand) { return (operand.array().real()).matrix(); };
#ifdef ELSA_CUDA_VECTOR
auto realGPU = [](auto const& operand, bool) { return quickvec::real(operand); };
return Expression{Callables{real, realGPU}, operand};
#else
return Expression{real, operand};
#endif
}
/// Element-wise imaginary parts of the Operand
template <typename Operand, typename = std::enable_if_t<isDcOrExpr<Operand>>>
auto imag(Operand const& operand)
{
auto imag = [](auto const& operand) { return (operand.array().imag()).matrix(); };
#ifdef ELSA_CUDA_VECTOR
auto imagGPU = [](auto const& operand, bool) { return quickvec::imag(operand); };
return Expression{Callables{imag, imagGPU}, operand};
#else
return Expression{imag, operand};
#endif
}
} // namespace elsa
......@@ -304,6 +304,20 @@ TEST_CASE_TEMPLATE_DEFINE("DataContainer: Testing element-wise access", TestType
DataContainer dcLog = log(dcSquare);
for (index_t i = 0; i < dc.getSize(); ++i)
REQUIRE_UNARY(checkApproxEq(dcLog[i], randVec.array().square().log()[i]));
DataContainer dcReal = real(dc);
for (index_t i = 0; i < dc.getSize(); ++i)
REQUIRE_UNARY(checkApproxEq(dcReal[i], randVec.array().real()[i]));
DataContainer dcImag = imag(dc);
if constexpr (isComplex<data_t>) {
for (index_t i = 0; i < dc.getSize(); ++i)
REQUIRE_UNARY(checkApproxEq(dcImag[i], randVec.array().imag()[i]));
} else {
for (index_t i = 0; i < dc.getSize(); ++i)
REQUIRE_UNARY(checkApproxEq(dcImag[i], 0));
}
}
auto scalar = static_cast<data_t>(923.41f);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment