Skip to content

Commit

Permalink
dist reldist sqr
Browse files Browse the repository at this point in the history
  • Loading branch information
jtlap committed Aug 20, 2023
1 parent edcff8f commit 49841a7
Show file tree
Hide file tree
Showing 9 changed files with 258 additions and 49 deletions.
2 changes: 2 additions & 0 deletions include/kyosu/functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
//======================================================================================================================
#include <kyosu/functions/abs.hpp>
#include <kyosu/functions/conj.hpp>
#include <kyosu/functions/dist.hpp>
#include <kyosu/functions/if_else.hpp>
#include <kyosu/functions/ipart.hpp>
#include <kyosu/functions/jpart.hpp>
#include <kyosu/functions/kpart.hpp>
#include <kyosu/functions/purepart.hpp>
#include <kyosu/functions/real.hpp>
#include <kyosu/functions/reldist.hpp>
#include <kyosu/functions/sqr.hpp>
#include <kyosu/functions/sqr_abs.hpp>
44 changes: 0 additions & 44 deletions include/kyosu/functions/abs.hpp
Original file line number Diff line number Diff line change
@@ -1,47 +1,3 @@
//!
//! **Defined in Header**
//!
//! @code
//! #include <eve/module/core.hpp>
//! @endcode
//!
//! @groupheader{Callable Signatures}
//!
//! @code
//! namespace eve
//! {
//! template< eve::value T >
//! T abs(T x) noexcept; //1
//!
//! template< eve::floating_value T >
//! T abs(eve::as_complex<T> z) noexcept; //2
//!
//! }
//! @endcode
//!
//! **Parameters**
//!
//! * `x` : [real argument](@ref eve::value).
//! * `z` : [complex argument ](@ref eve::complex).
//!
//! **Return value**
//!
//! 1. value containing the [elementwise](@ref glossary_elementwise)
//! absolute value of `x` if it is representable in this type.
//!
//! More specifically, for signed integers : the absolute value of eve::valmin
//! is not representable and the result is undefined.
//!
//!
//! 2.
//!
//! @warning
//! abs is also a standard library function name and there possibly
//! exists a C macro version which may be called instead of the EVE version.
//! To avoid confusion, use the eve::abs notation.
//!


//======================================================================================================================
/*
Kyosu - Complex Without Complexes
Expand Down
79 changes: 79 additions & 0 deletions include/kyosu/functions/dist.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//======================================================================================================================
/*
Kyosu - Complex Without Complexes
Copyright : KYOSU Contributors & Maintainers
SPDX-License-Identifier: BSL-1.0
*/
//======================================================================================================================
#pragma once

#include <kyosu/details/invoke.hpp>

namespace kyosu::tags
{
struct callable_dist : eve::elementwise
{
using callable_tag_type = callable_dist;

KYOSU_DEFERS_CALLABLE(dist_);

static KYOSU_FORCEINLINE auto deferred_call(auto
, eve::ordered_value auto const& v0
, eve::ordered_value auto const& v1) noexcept
{
return eve::dist(v0, v1);
}

KYOSU_FORCEINLINE auto operator()(auto const& target0, auto const& target1 ) const noexcept
-> decltype(eve::tag_invoke(*this, target0, target1))
{
return eve::tag_invoke(*this, target0, target1);
}

template<typename... T>
eve::unsupported_call<callable_dist(T&&...)> operator()(T&&... x) const
requires(!requires { eve::tag_invoke(*this, KYOSU_FWD(x)...); }) = delete;
};
}

namespace kyosu
{
//======================================================================================================================
//! @addtogroup functions
//! @{
//! @var dist
//! @brief Computes the distance of the two parameters.
//!
//! **Defined in Header**
//!
//! @code
//! #include <kyosu/functions.hpp>
//! @endcode
//!
//! @groupheader{Callable Signatures}
//!
//! @code
//! namespace kyosu
//! {
//! template<kyosu::concepts::cayley_dickson T0, kyosu::concepts::cayley_dickson T1 > constexpr auto dist(T0 z0, T1, z1) noexcept;
//! template<eve::ordered_value T0, kyosu::concepts::cayley_dickson T1> > constexpr auto dist(T0 z0, T1, z1) noexcept;
//! template<kyosu::concepts::cayley_dickson T0, eve::ordered_value T1 > constexpr auto dist(T0 z0, T1, z1) noexcept;
//! template<eve::ordered_value T0, ordered_value T1> > constexpr auto dist(T0 z0, T1, z1) noexcept;
///! }
//! @endcode
//!
//! **Parameters**
//!
//! * `z0, z1` : Value to process.
//!
//! **Return value**
//!
//! Returns the absolute value of the arguments difference.
//!
//! @groupheader{Example}
//!
//! @godbolt{doc/dist.cpp}
//! @}
//======================================================================================================================
inline constexpr tags::callable_dist dist = {};
}
80 changes: 80 additions & 0 deletions include/kyosu/functions/reldist.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//======================================================================================================================
/*
Kyosu - Complex Without Complexes
Copyright : KYOSU Contributors & Maintainers
SPDX-License-Identifier: BSL-1.0
*/
//======================================================================================================================
#pragma once

#include <kyosu/details/invoke.hpp>

namespace kyosu::tags
{
struct callable_reldist : eve::elementwise
{
using callable_tag_type = callable_reldist;

KYOSU_DEFERS_CALLABLE(reldist_);


static KYOSU_FORCEINLINE auto deferred_call(auto
, eve::ordered_value auto const& v0
, eve::ordered_value auto const& v1) noexcept
{
return eve::reldist(v0, v1);
}

KYOSU_FORCEINLINE auto operator()(auto const& target0, auto const& target1 ) const noexcept
-> decltype(eve::tag_invoke(*this, target0, target1))
{
return eve::tag_invoke(*this, target0, target1);
}

template<typename... T>
eve::unsupported_call<callable_reldist(T&&...)> operator()(T&&... x) const
requires(!requires { eve::tag_invoke(*this, KYOSU_FWD(x)...); }) = delete;
};
}

namespace kyosu
{
//======================================================================================================================
//! @addtogroup functions
//! @{
//! @var reldist
//! @brief Computes the relative distance of the two parameters.
//!
//! **Defined in Header**
//!
//! @code
//! #include <kyosu/functions.hpp>
//! @endcode
//!
//! @groupheader{Callable Signatures}
//!
//! @code
//! namespace kyosu
//! {
//! template<kyosu::concepts::cayley_dickson T0, kyosu::concepts::cayley_dickson T1 > constexpr auto reldist(T0 z0, T1, z1) noexcept;
//! template<eve::ordered_value T0, kyosu::concepts::cayley_dickson T1> > constexpr auto reldist(T0 z0, T1, z1) noexcept;
//! template<kyosu::concepts::cayley_dickson T0, eve::ordered_value T1 > constexpr auto reldist(T0 z0, T1, z1) noexcept;
//! template<eve::ordered_value T0, ordered_value T1> > constexpr auto reldist(T0 z0, T1, z1) noexcept;
///! }
//! @endcode
//!
//! **Parameters**
//!
//! * `z0, z1` : Value to process.
//!
//! **Return value**
//!
//! Returns the absolute value of the arguments difference dived by the maximum of their absolute values and 1.
//!
//! @groupheader{Example}
//!
//! @godbolt{doc/reldist.cpp}
//! @}
//======================================================================================================================
inline constexpr tags::callable_reldist reldist = {};
}
15 changes: 15 additions & 0 deletions include/kyosu/types/impl/arithmetic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,19 @@ namespace kyosu::_
real(c) = 0;
return r+a*c;
}

template<typename C0, typename C1>
KYOSU_FORCEINLINE constexpr
auto dispatch(eve::tag_of<dist> const&, C0 const & c0, C1 const & c1) noexcept
{
return kyosu::abs(c0-c1);
}

template<typename C0, typename C1>
KYOSU_FORCEINLINE constexpr
auto dispatch(eve::tag_of<reldist> const&, C0 const & c0, C1 const & c1) noexcept
{
return kyosu::dist(c0, c1)/eve::max(kyosu::abs(c0), kyosu::abs(c1), eve::one(eve::as(abs(c0))));
}

}
38 changes: 38 additions & 0 deletions test/doc/dist.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <kyosu/kyosu.hpp>
#include <eve/wide.hpp>
#include <iostream>

int main()
{
using kyosu::dist;
using kyosu::complex;
using kyosu::quaternion;
using e_t = float;
using c_t = complex<float>;
using q_t = quaternion<float>;

std::cout << "Real: "<< "\n";
e_t e0(1);
e_t e1(2);
std::cout << e0 << ", " << e1 << " -> " << dist(e0, e1) << "\n";

std::cout << "Complex: "<< "\n";
c_t c0(1, 5);
c_t c1(5, 9);
std::cout << c0 << ", " << c1 << " -> " << dist(c0, c1) << "\n";



std::cout << "Quaternion: "<< "\n";
q_t q0(1, 5);
q_t q1(5, 9);
std::cout << q0 << ", " << q1 << " -> " << dist(q0, q1) << "\n";


// std::cout << "SIMD: ";
// using wc_t = eve::wide<complex<double>, eve::fixed<2>>;
// std::cout << wc_t(complex<double>(1.3,-3.7)) << " -> " << dist(wc_t(complex<double>(1.3,-3.7))) << "\n";
// std::cout << wc_t(complex<double>(1.3,-3.7)) << " -> " << (wc_t(complex<double>(1.3,-3.7)))*(wc_t(complex<double>(1.3,-3.7)))<< "\n";

return 0;
}
1 change: 1 addition & 0 deletions test/test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ int main(int argc, char const **argv)

namespace tts
{

template<kyosu::concepts::complex T> auto relative_distance(T const &l, T const &r)
{
auto [rl,il] = l;
Expand Down
39 changes: 39 additions & 0 deletions test/unit/function/dist.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <kyosu/kyosu.hpp>
#include <eve/wide.hpp>
#include <iostream>

int main()
{
using kyosu::dist;
using kyosu::complex;
using kyosu::quaternion;
using e_t = float;
using c_t = complex<float>;
using q_t = quaternion<float>;

std::cout << "Real: "<< "\n";
e_t e0(1);
e_t e1(2);
std::cout << e0 << ", " << e1 << " -> " << dist(e0, e1) << "\n";

std::cout << "Complex: "<< "\n";
c_t c0(1, 5);
c_t c1(4, 9);
std::cout << c0 << ", " << c1 << " -> " << dist(c0, c1) << "\n";



std::cout << "Quaternion: "<< "\n";
q_t q0(1, 5, 2, 3);
q_t q1(5, 9, 6, 7);
std::cout << q0 << ", " << q1 << " -> " << dist(q0, q1) << "\n";


std::cout << "SIMD: "<< "\n";
using wq_t = eve::wide<quaternion<double>, eve::fixed<2>>;
wq_t wq0(1, 5, 2, 3);
wq_t wq1(5, 9, 6, 7);
std::cout << wq0 << ", " << wq1 << " -> " << dist(wq0, wq1) << "\n";

return 0;
}
9 changes: 4 additions & 5 deletions test/unit/function/sqr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ TTS_CASE_WITH ( "Check kyosu::sqr over real"
)
(auto data)
{
TTS_EQUAL(kyosu::sqr(data), eve::sqr(data));
TTS_ULP_EQUAL(kyosu::sqr(data), eve::sqr(data), 0.5);
};

TTS_CASE_WITH ( "Check kyosu::sqr over complex"
Expand All @@ -24,7 +24,7 @@ TTS_CASE_WITH ( "Check kyosu::sqr over complex"
(auto r, auto i)
{
auto c = kyosu::to_complex(r,i);
TTS_EQUAL(kyosu::sqr(c), c*c);
TTS_EXPECT(eve::all(kyosu::reldist(kyosu::sqr(c), c*c) <= 0.0001));
};

TTS_CASE_WITH ( "Check kyosu::sqr over quaternion"
Expand All @@ -36,7 +36,6 @@ TTS_CASE_WITH ( "Check kyosu::sqr over quaternion"
<typename T>(T r, T i, T j, T k)
{
using type = kyosu::as_quaternion_t<T>;
auto o = type(r,i,j,k);

TTS_EQUAL(kyosu::sqr(o), o*o);
auto q = type(r,i,j,k);
TTS_EXPECT(eve::all(kyosu::reldist(kyosu::sqr(q), q*q) <= 0.0001));
};

0 comments on commit 49841a7

Please sign in to comment.