Skip to content

Refactor complex comparison functions #1320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 10, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <cstdint>
#include <type_traits>

#include "utils/math_utils.hpp"
#include "utils/offset_utils.hpp"
#include "utils/type_dispatch.hpp"
#include "utils/type_utils.hpp"
Expand Down Expand Up @@ -67,12 +68,8 @@ template <typename argT1, typename argT2, typename resT> struct GreaterFunctor
tu_ns::is_complex<argT2>::value)
{
static_assert(std::is_same_v<argT1, argT2>);
using realT = typename argT1::value_type;
realT real1 = std::real(in1);
realT real2 = std::real(in2);

return (real1 == real2) ? (std::imag(in1) > std::imag(in2))
: real1 > real2;
using dpctl::tensor::math_utils::greater_complex;
return greater_complex<argT1>(in1, in2);
}
else {
return (in1 > in2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <cstdint>
#include <type_traits>

#include "utils/math_utils.hpp"
#include "utils/offset_utils.hpp"
#include "utils/type_dispatch.hpp"
#include "utils/type_utils.hpp"
Expand Down Expand Up @@ -68,12 +69,8 @@ struct GreaterEqualFunctor
tu_ns::is_complex<argT2>::value)
{
static_assert(std::is_same_v<argT1, argT2>);
using realT = typename argT1::value_type;
realT real1 = std::real(in1);
realT real2 = std::real(in2);

return (real1 == real2) ? (std::imag(in1) >= std::imag(in2))
: real1 >= real2;
using dpctl::tensor::math_utils::greater_equal_complex;
return greater_equal_complex<argT1>(in1, in2);
}
else {
return (in1 >= in2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <cstdint>
#include <type_traits>

#include "utils/math_utils.hpp"
#include "utils/offset_utils.hpp"
#include "utils/type_dispatch.hpp"
#include "utils/type_utils.hpp"
Expand Down Expand Up @@ -66,12 +67,8 @@ template <typename argT1, typename argT2, typename resT> struct LessFunctor
tu_ns::is_complex<argT2>::value)
{
static_assert(std::is_same_v<argT1, argT2>);
using realT = typename argT1::value_type;
realT real1 = std::real(in1);
realT real2 = std::real(in2);

return (real1 == real2) ? (std::imag(in1) < std::imag(in2))
: real1 < real2;
using dpctl::tensor::math_utils::less_complex;
return less_complex<argT1>(in1, in2);
}
else {
return (in1 < in2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <cstdint>
#include <type_traits>

#include "utils/math_utils.hpp"
#include "utils/offset_utils.hpp"
#include "utils/type_dispatch.hpp"
#include "utils/type_utils.hpp"
Expand Down Expand Up @@ -67,12 +68,8 @@ template <typename argT1, typename argT2, typename resT> struct LessEqualFunctor
tu_ns::is_complex<argT2>::value)
{
static_assert(std::is_same_v<argT1, argT2>);
using realT = typename argT1::value_type;
realT real1 = std::real(in1);
realT real2 = std::real(in2);

return (real1 == real2) ? (std::imag(in1) <= std::imag(in2))
: real1 <= real2;
using dpctl::tensor::math_utils::less_equal_complex;
return less_equal_complex<argT1>(in1, in2);
}
else {
return (in1 <= in2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <cstdint>
#include <type_traits>

#include "utils/math_utils.hpp"
#include "utils/offset_utils.hpp"
#include "utils/type_dispatch.hpp"
#include "utils/type_utils.hpp"
Expand Down Expand Up @@ -65,16 +66,8 @@ template <typename argT1, typename argT2, typename resT> struct MaximumFunctor
tu_ns::is_complex<argT2>::value)
{
static_assert(std::is_same_v<argT1, argT2>);
using realT = typename argT1::value_type;
realT real1 = std::real(in1);
realT real2 = std::real(in2);
realT imag1 = std::imag(in1);
realT imag2 = std::imag(in2);

bool gt = (real1 == real2) ? (imag1 > imag2)
: (real1 > real2 && !std::isnan(imag1) &&
!std::isnan(imag2));
return (std::isnan(real1) || std::isnan(imag1) || gt) ? in1 : in2;
using dpctl::tensor::math_utils::max_complex;
return max_complex<argT1>(in1, in2);
}
else if constexpr (std::is_floating_point_v<argT1> ||
std::is_same_v<argT1, sycl::half>)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <cstdint>
#include <type_traits>

#include "utils/math_utils.hpp"
#include "utils/offset_utils.hpp"
#include "utils/type_dispatch.hpp"
#include "utils/type_utils.hpp"
Expand Down Expand Up @@ -65,16 +66,8 @@ template <typename argT1, typename argT2, typename resT> struct MinimumFunctor
tu_ns::is_complex<argT2>::value)
{
static_assert(std::is_same_v<argT1, argT2>);
using realT = typename argT1::value_type;
realT real1 = std::real(in1);
realT real2 = std::real(in2);
realT imag1 = std::imag(in1);
realT imag2 = std::imag(in2);

bool lt = (real1 == real2) ? (imag1 < imag2)
: (real1 < real2 && !std::isnan(imag1) &&
!std::isnan(imag2));
return (std::isnan(real1) || std::isnan(imag1) || lt) ? in1 : in2;
using dpctl::tensor::math_utils::min_complex;
return min_complex<argT1>(in1, in2);
}
else if constexpr (std::is_floating_point_v<argT1> ||
std::is_same_v<argT1, sycl::half>)
Expand Down
120 changes: 120 additions & 0 deletions dpctl/tensor/libtensor/include/utils/math_utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
//===------- math_utils.hpp - Implementation of math utils -------*-C++-*/===//
//
// Data Parallel Control (dpctl)
//
// Copyright 2020-2023 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file defines math utility functions.
//===----------------------------------------------------------------------===//

#pragma once
#include <cmath>
#include <complex>

namespace dpctl
{
namespace tensor
{
namespace math_utils
{

template <typename T> bool less_complex(const T &x1, const T &x2)
{
using realT = typename T::value_type;
realT real1 = std::real(x1);
realT real2 = std::real(x2);
realT imag1 = std::imag(x1);
realT imag2 = std::imag(x2);

return (real1 == real2)
? (imag1 < imag2)
: (real1 < real2 && !std::isnan(imag1) && !std::isnan(imag2));
}

template <typename T> bool greater_complex(const T &x1, const T &x2)
{
using realT = typename T::value_type;
realT real1 = std::real(x1);
realT real2 = std::real(x2);
realT imag1 = std::imag(x1);
realT imag2 = std::imag(x2);

return (real1 == real2)
? (imag1 > imag2)
: (real1 > real2 && !std::isnan(imag1) && !std::isnan(imag2));
}

template <typename T> bool less_equal_complex(const T &x1, const T &x2)
{
using realT = typename T::value_type;
realT real1 = std::real(x1);
realT real2 = std::real(x2);
realT imag1 = std::imag(x1);
realT imag2 = std::imag(x2);

return (real1 == real2)
? (imag1 <= imag2)
: (real1 < real2 && !std::isnan(imag1) && !std::isnan(imag2));
}

template <typename T> bool greater_equal_complex(const T &x1, const T &x2)
{
using realT = typename T::value_type;
realT real1 = std::real(x1);
realT real2 = std::real(x2);
realT imag1 = std::imag(x1);
realT imag2 = std::imag(x2);

return (real1 == real2)
? (imag1 >= imag2)
: (real1 > real2 && !std::isnan(imag1) && !std::isnan(imag2));
}

template <typename T> T max_complex(const T &x1, const T &x2)
{
using realT = typename T::value_type;
realT real1 = std::real(x1);
realT real2 = std::real(x2);
realT imag1 = std::imag(x1);
realT imag2 = std::imag(x2);

bool isnan_imag1 = std::isnan(imag1);
bool gt = (real1 == real2)
? (imag1 > imag2)
: (real1 > real2 && !isnan_imag1 && !std::isnan(imag2));
return (std::isnan(real1) || isnan_imag1 || gt) ? x1 : x2;
}

template <typename T> T min_complex(const T &x1, const T &x2)
{
using realT = typename T::value_type;
realT real1 = std::real(x1);
realT real2 = std::real(x2);
realT imag1 = std::imag(x1);
realT imag2 = std::imag(x2);

bool isnan_imag1 = std::isnan(imag1);
bool lt = (real1 == real2)
? (imag1 < imag2)
: (real1 < real2 && !isnan_imag1 && !std::isnan(imag2));
return (std::isnan(real1) || isnan_imag1 || lt) ? x1 : x2;
}

} // namespace math_utils
} // namespace tensor
} // namespace dpctl