Skip to content

Commit

Permalink
Fixes around constants generation
Browse files Browse the repository at this point in the history
* as_real<T> is available
* Add tests for as_real and constants
  • Loading branch information
jfalcou authored Nov 9, 2023
1 parent fe69e00 commit 2df86a7
Show file tree
Hide file tree
Showing 16 changed files with 224 additions and 150 deletions.
4 changes: 2 additions & 2 deletions include/kyosu/functions/abs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ namespace kyosu
//! @code
//! namespace kyosu
//! {
//! template<kyosu::concepts::cayley_dickson T> constexpr as_real_t<T> abs(T z) noexcept;
//! template<eve::floating_ordered_value T> constexpr T abs(T z) noexcept;
//! template<kyosu::concepts::cayley_dickson T> constexpr as_real_type_t<T> abs(T z) noexcept;
//! template<eve::floating_ordered_value T> constexpr T abs(T z) noexcept;
//! }
//! @endcode
//!
Expand Down
4 changes: 2 additions & 2 deletions include/kyosu/functions/radinpi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ namespace kyosu
//! @code
//! namespace kyosu
//! {
//! template<kyosu::concepts::cayley_dickson T> constexpr as_real_t<T> radinpi(T z) noexcept;
//! template<eve::floating_ordered_value T> constexpr T radinpi(T z) noexcept;
//! template<kyosu::concepts::cayley_dickson T> constexpr as_real_type_t<T> radinpi(T z) noexcept;
//! template<eve::floating_ordered_value T> constexpr T radinpi(T z) noexcept;
//! }
//! @endcode
//!
Expand Down
4 changes: 2 additions & 2 deletions include/kyosu/functions/sqr_abs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ namespace kyosu
//! @code
//! namespace kyosu
//! {
//! template<kyosu::concepts::cayley_dickson T> constexpr auto sqr_abs(T z) noexcept;
//! template<eve::floating_ordered_value T> constexpr auto sqr_abs(T z) noexcept;
//! template<kyosu::concepts::cayley_dickson T> constexpr as_real_type_t<T> sqr_abs(T z) noexcept;
//! template<eve::floating_ordered_value T> constexpr auto sqr_abs(T z) noexcept;
//! }
//! @endcode
//!
Expand Down
39 changes: 21 additions & 18 deletions include/kyosu/types/cayley_dickson.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ namespace kyosu
template<std::size_t I, typename T, unsigned int N>
constexpr auto get(cayley_dickson<T,N> const& c) noexcept { return kumi::get<I>(c.contents); }

//==================================================================================================================
//====================================================================================================================
// Main function dispatchers
//==================================================================================================================
//====================================================================================================================
template<typename... T>
requires(concepts::cayley_dickson<T> || ... )
KYOSU_FORCEINLINE constexpr auto tag_invoke(eve::callable auto const& f, auto, T&&... c) noexcept
Expand All @@ -133,9 +133,9 @@ namespace kyosu
return _::dispatch(f, KYOSU_FWD(c)...);
}

//==================================================================================================================
//====================================================================================================================
// Tag invoke override for if_else - Outside so it can properly deals with the complicated parameters of if_else
//==================================================================================================================
//====================================================================================================================
template<typename T, typename F>
requires(concepts::cayley_dickson<T> || concepts::cayley_dickson<F>)
KYOSU_FORCEINLINE constexpr auto tag_invoke( eve::tag_of<kyosu::if_else> const& fn, auto, auto m
Expand All @@ -155,9 +155,9 @@ namespace kyosu
return _::dispatch(f, c, tgt);
}

//==================================================================================================================
//====================================================================================================================
// Tag invoke override for parts extraction - Outside so they can see get<I>(c)
//==================================================================================================================
//====================================================================================================================
template<concepts::extractor T, concepts::cayley_dickson C>
KYOSU_FORCEINLINE constexpr decltype(auto)
tag_invoke(T const&, auto, C&& c) noexcept
Expand All @@ -167,7 +167,7 @@ namespace kyosu
if constexpr(T::minimum_valid_index == T::maximum_valid_index)
{
if constexpr(sz > T::minimum_valid_index) return get<T::minimum_valid_index>(EVE_FWD(c));
else return as_real_t<std::remove_cvref_t<C>>{0};
else return as_real_type_t<std::remove_cvref_t<C>>{0};
}
else
{
Expand All @@ -183,11 +183,22 @@ namespace kyosu
}

// TODO: Move to tag_invoke when EVE catch up on this front
template<typename Tag, eve::floating_scalar_value Type, unsigned int N>
KYOSU_FORCEINLINE constexpr auto tagged_dispatch(Tag const&, eve::as<cayley_dickson<Type,N>> const&) noexcept
template<typename Tag, concepts::cayley_dickson T>
KYOSU_FORCEINLINE constexpr auto tagged_dispatch(Tag const&, eve::as<T> const&) noexcept
{
eve::detail::callable_object<Tag> cst;
return cayley_dickson<Type,N>( cst(eve::as<Type>{}) );
auto val = cst( eve::as<as_real_type_t<T>>{} );
using val_t = std::remove_cvref_t<decltype(val)>;

if constexpr(!eve::floating_value<val_t>) return val;
else return as_cayley_dickson_n_t<eve::element_type_t<T>::static_size,val_t>(val);
}

template<typename Tag, typename T>
KYOSU_FORCEINLINE constexpr auto tagged_dispatch(Tag const&, as_real<T> const&) noexcept
{
eve::detail::callable_object<Tag> cst;
return cst(typename as_real<T>::target_type{});
}

//====================================================================================================================
Expand Down Expand Up @@ -217,14 +228,6 @@ struct std::tuple_element<I, kyosu::cayley_dickson<T,N>>
using type = T;
};

template<typename Wrapper, typename T, unsigned int N>
struct eve::supports_like<Wrapper,kyosu::cayley_dickson<T,N>>
: std::bool_constant< kyosu::concepts::cayley_dickson<Wrapper>
|| std::same_as<T, eve::element_type_t<Wrapper>>
|| plain_scalar_value<Wrapper>
>
{
};
#endif

#include <kyosu/types/impl/io.hpp>
Expand Down
4 changes: 2 additions & 2 deletions include/kyosu/types/impl/arithmetic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ namespace kyosu::_
KYOSU_FORCEINLINE constexpr
auto dispatch(eve::tag_of<kyosu::proj> const&, C c) noexcept
{
using real_t = eve::as<as_real_t<C>>;
using real_t = eve::as<as_real_type_t<C>>;
constexpr auto P = kyosu::dimension_v<C>;
if constexpr (P == 2)
return if_else(is_infinite(c)
Expand Down Expand Up @@ -417,7 +417,7 @@ namespace kyosu::_
{
r_t x = convert(xx, eve::as<r_t>());
auto dfma = /*d*/(fma);
r_t that(eve::zero(eve::as<as_real_t<r_t>>()));
r_t that(eve::zero(eve::as<as_real_type_t<r_t>>()));
auto next = [&](auto that, auto arg) { return dfma(x, that, arg); };
((that = next(that, cs)), ...);
return that;
Expand Down
4 changes: 2 additions & 2 deletions include/kyosu/types/impl/compounds.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ namespace kyosu
auto ria = kumi::split(self ,kumi::index<sz>);
auto rib = kumi::split(other,kumi::index<sz>);

using cd_t = as_cayley_dickson_n_t<sz,as_real_t<Self>,as_real_t<Other>>;
using cd_t = as_cayley_dickson_n_t<sz,as_real_type_t<Self>,as_real_type_t<Other>>;
cd_t ra{get<0>(ria)}, ia{get<1>(ria)}
, rb{get<0>(rib)}, ib{get<1>(rib)};

Expand All @@ -91,7 +91,7 @@ namespace kyosu
constexpr auto idx = kumi::index<sz>;
auto ria = kumi::split(self,idx);

using cd_t = as_cayley_dickson_n_t<sz,as_real_t<Self>,as_real_t<Other>>;
using cd_t = as_cayley_dickson_n_t<sz,as_real_type_t<Self>,as_real_type_t<Other>>;
cd_t ra{get<0>(ria)}, ia{get<1>(ria)};
self = as_cayley_dickson_t<Self,Other>{kumi::cat(ra * other,ia * conj(other))};
}
Expand Down
2 changes: 1 addition & 1 deletion include/kyosu/types/impl/detail/besselin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace kyosu::_
{
if constexpr(concepts::complex<Z> )
{
using e_t = as_real_t<Z>;
using e_t = as_real_type_t<Z>;
auto miton = [](N n){
if (n%4 == 0) return complex(eve::one(eve::as<e_t>()));
else if (n%4 == 1) return complex(eve::zero(eve::as<e_t>()), eve::mone(eve::as<e_t>()));
Expand Down
4 changes: 2 additions & 2 deletions include/kyosu/types/impl/detail/besselj0.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace kyosu::_
{
if constexpr(concepts::complex<Z> )
{
using e_t = as_real_t<Z>;
using e_t = as_real_type_t<Z>;
using u_t = eve::underlying_type_t<e_t>;
auto saz = kyosu::sqr_abs(z);

Expand Down Expand Up @@ -110,7 +110,7 @@ namespace kyosu::_

if( eve::any(notdone) )
{
notdone = next_interval(ascending_series_cyl_j0, notdone, saz <= as_real_t<Z>(144), r, z);
notdone = next_interval(ascending_series_cyl_j0, notdone, saz <= as_real_type_t<Z>(144), r, z);
if( eve::any(notdone) )
{
last_interval(semiconvergent_series_cyl_j0, notdone, r, z);
Expand Down
4 changes: 2 additions & 2 deletions include/kyosu/types/impl/detail/besselj1.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace kyosu::_
{
if constexpr(concepts::complex<Z> )
{
using e_t = as_real_t<Z>;
using e_t = as_real_type_t<Z>;
using u_t = eve::underlying_type_t<e_t>;
auto saz = kyosu::sqr_abs(z);

Expand Down Expand Up @@ -111,7 +111,7 @@ namespace kyosu::_

if( eve::any(notdone) )
{
notdone = next_interval(ascending_series_cyl_j1, notdone, saz <= as_real_t<Z>(144), r, z);
notdone = next_interval(ascending_series_cyl_j1, notdone, saz <= as_real_type_t<Z>(144), r, z);
if( eve::any(notdone) )
{
last_interval(semiconvergent_series_cyl_j1, notdone, r, z);
Expand Down
2 changes: 1 addition & 1 deletion include/kyosu/types/impl/detail/besseljn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace kyosu::_
}
else
{
using e_t = as_real_t<Z>;
using e_t = as_real_type_t<Z>;
using u_t = eve::underlying_type_t<e_t>;
auto n = u_t(eve::abs(nn));
auto az = kyosu::abs(z);
Expand Down
2 changes: 1 addition & 1 deletion include/kyosu/types/impl/faddeeva.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace kyosu::_
{
if constexpr(concepts::complex<Z> )
{
using v_t = as_real_t<Z>;
using v_t = as_real_type_t<Z>;
using real_t = eve::element_type_t<v_t>;
auto const sqrtpi = eve::sqrt_pi(eve::as<real_t>());
auto const iosqrtpi = complex(real_t(0), eve::rec(sqrtpi));
Expand Down
8 changes: 4 additions & 4 deletions include/kyosu/types/impl/special.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace kyosu::_
// 15 sig. digits for 0<=real(z)<=171
// coeffs should sum to about g*g/2+23/24
//
using r_t = eve::element_type_t<as_real_t<Z>>;
using r_t = eve::element_type_t<as_real_type_t<Z>>;
auto g=r_t(607)/r_t(128);
// best results when 4<=g<=5
constexpr int N = 15;
Expand Down Expand Up @@ -98,7 +98,7 @@ namespace kyosu::_
// 15 sig. digits for 0<=real(z)<=171
// coeffs should sum to about g*g/2+23/24
//
using r_t = eve::element_type_t<as_real_t<Z>>;
using r_t = eve::element_type_t<as_real_type_t<Z>>;
auto g=r_t(607)/r_t(128);
// best results when 4<=g<=5
constexpr int N = 15;
Expand Down Expand Up @@ -165,7 +165,7 @@ namespace kyosu::_
// 15 sig. digits for 0<=real(z)<=171
// coeffs should sum to about g*g/2+23/24
//
using r_t = eve::element_type_t<as_real_t<Z>>;
using r_t = eve::element_type_t<as_real_type_t<Z>>;
auto g=r_t(607)/r_t(128);
// best results when 4<=g<=5
constexpr int N = 15;
Expand Down Expand Up @@ -263,7 +263,7 @@ namespace kyosu::_
// 15 sig. digits for 0<=real(z)<=171
// coeffs should sum to about g*g/2+23/24
//
using v_t = kyosu::as_real_t<Z>;
using v_t = kyosu::as_real_type_t<Z>;
using real_t = eve::element_type_t<v_t>;
auto k = real_t(kk);
auto [rz, iz] = z;
Expand Down
31 changes: 26 additions & 5 deletions include/kyosu/types/traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//======================================================================================================================
#pragma once

#include <eve/as.hpp>
#include <bit>

namespace kyosu
Expand Down Expand Up @@ -63,11 +64,11 @@ namespace kyosu
template<concepts::cayley_dickson T>
inline constexpr auto dimension_v<T> = eve::element_type_t<std::remove_cvref_t<T>>::static_size;

template<typename T> struct as_real { using type = T; };
template<typename T,unsigned int Dim> struct as_real<cayley_dickson<T,Dim>> { using type = T; };
template<typename T,typename N> struct as_real<eve::wide<T,N>>
template<typename T> struct as_real_type { using type = T; };
template<typename T,unsigned int Dim> struct as_real_type<cayley_dickson<T,Dim>> { using type = T; };
template<typename T,typename N> struct as_real_type<eve::wide<T,N>>
{
using type = eve::wide<typename as_real<T>::type,N>;
using type = eve::wide<typename as_real_type<T>::type,N>;
};

//====================================================================================================================
Expand All @@ -76,7 +77,7 @@ namespace kyosu
//! @tparam T Type to convert to a real type.
//====================================================================================================================
template<typename T>
using as_real_t = typename as_real<T>::type;
using as_real_type_t = typename as_real_type<T>::type;

template<unsigned int Dim, typename... Ts>
struct as_cayley_dickson_n;
Expand Down Expand Up @@ -127,6 +128,26 @@ namespace kyosu
template<typename... Ts>
using as_cayley_dickson_t = typename as_cayley_dickson<Ts...>::type;

using eve::as;

//====================================================================================================================
//! @struct as_real
//! @brief Lightweight type-wrapper of real value type
//!
//! Wraps the real type associed to `T` into a constexpr, trivially constructible empty class to optimize passing type
//! parameters via object instead of via template parameters.
//!
//! @tparam T Type to wrap
//====================================================================================================================
template<typename T> struct as_real
{
using type = as_real_type_t<T>;
using target_type = eve::as<type>;

constexpr as_real() noexcept {}
constexpr as_real(T const&) noexcept {}
};

//====================================================================================================================
//! @}
//====================================================================================================================
Expand Down
Loading

0 comments on commit 2df86a7

Please sign in to comment.