Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions include/xtl/xcomplex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,23 @@ namespace xtl
template <class E, class R = void>
using enable_scalar = std::enable_if_t<xtl::is_arithmetic<E>::value, R>;

/*************************
* select implementation *
*************************/

// Complex overload of xtl::select (see xfunctional.hpp, which covers
// scalars only): at least one of v1 / v2 is complex, the other may be a
// scalar.
template <class B, class T1, class T2,
XTL_REQUIRES(std::is_scalar<B>,
std::disjunction<is_gen_complex<T1>, is_gen_complex<T2>>,
std::disjunction<std::is_scalar<T1>, is_gen_complex<T1>>,
std::disjunction<std::is_scalar<T2>, is_gen_complex<T2>>)>
inline std::common_type_t<T1, T2> select(const B& cond, const T1& v1, const T2& v2) noexcept
{
return cond ? v1 : v2;
}

/*******************
* common_xcomplex *
*******************/
Expand Down
22 changes: 22 additions & 0 deletions test/test_xcomplex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,5 +328,27 @@ namespace xtl
EXPECT_COMPLEX_APPROX_EQ(b / x_closure, b / 5.0);
EXPECT_COMPLEX_APPROX_EQ(x_closure / b, 5.0 / b);
}

TEST(xcomplex, select)
{
std::complex<double> a(1., 2.);
std::complex<double> b(3., 4.);
EXPECT_EQ(select(true, a, b), a);
EXPECT_EQ(select(false, a, b), b);

// Mixed precision promotes to the common type.
std::complex<float> af(1.f, 2.f);
EXPECT_EQ(select(true, af, b), std::complex<double>(1., 2.));
EXPECT_EQ(select(false, af, b), b);

// Mixed real / complex.
EXPECT_EQ(select(true, 5., b), std::complex<double>(5., 0.));
EXPECT_EQ(select(false, 5., b), b);

xcomplex<double> xa(1., 2.);
xcomplex<double> xb(3., 4.);
EXPECT_EQ(select(true, xa, xb), xa);
EXPECT_EQ(select(false, xa, xb), xb);
}
}

Loading