Skip to content

Commit 97b250d

Browse files
committed
Adds math_utils.hpp with complex comparison functions
1 parent acd1a60 commit 97b250d

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
//===------- math_utils.hpp - Implementation of math utils -------*-C++-*/===//
2+
//
3+
// Data Parallel Control (dpctl)
4+
//
5+
// Copyright 2020-2023 Intel Corporation
6+
//
7+
// Licensed under the Apache License, Version 2.0 (the "License");
8+
// you may not use this file except in compliance with the License.
9+
// You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing, software
14+
// distributed under the License is distributed on an "AS IS" BASIS,
15+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
// See the License for the specific language governing permissions and
17+
// limitations under the License.
18+
//
19+
//===----------------------------------------------------------------------===//
20+
///
21+
/// \file
22+
/// This file defines math utility functions.
23+
//===----------------------------------------------------------------------===//
24+
25+
#pragma once
26+
#include <cmath>
27+
#include <complex>
28+
29+
namespace dpctl
30+
{
31+
namespace tensor
32+
{
33+
namespace math_utils
34+
{
35+
36+
template <typename T>
37+
bool less_complex(const T &x1, const T &x2)
38+
{
39+
using realT = typename T::value_type;
40+
realT real1 = std::real(x1);
41+
realT real2 = std::real(x2);
42+
realT imag1 = std::imag(x1);
43+
realT imag2 = std::imag(x2);
44+
45+
return (real1 == real2)
46+
? (imag1 < imag2)
47+
: (real1 < real2 && !std::isnan(imag1) && !std::isnan(imag2));
48+
}
49+
50+
template <typename T> bool greater_complex(const T &x1, const T &x2)
51+
{
52+
using realT = typename T::value_type;
53+
realT real1 = std::real(x1);
54+
realT real2 = std::real(x2);
55+
realT imag1 = std::imag(x1);
56+
realT imag2 = std::imag(x2);
57+
58+
return (real1 == real2)
59+
? (imag1 > imag2)
60+
: (real1 > real2 && !std::isnan(imag1) && !std::isnan(imag2));
61+
}
62+
63+
template <typename T> bool less_equal_complex(const T &x1, const T &x2)
64+
{
65+
using realT = typename T::value_type;
66+
realT real1 = std::real(x1);
67+
realT real2 = std::real(x2);
68+
realT imag1 = std::imag(x1);
69+
realT imag2 = std::imag(x2);
70+
71+
return (real1 == real2)
72+
? (imag1 <= imag2)
73+
: (real1 < real2 && !std::isnan(imag1) && !std::isnan(imag2));
74+
}
75+
76+
template <typename T> bool greater_equal_complex(const T &x1, const T &x2)
77+
{
78+
using realT = typename T::value_type;
79+
realT real1 = std::real(x1);
80+
realT real2 = std::real(x2);
81+
realT imag1 = std::imag(x1);
82+
realT imag2 = std::imag(x2);
83+
84+
return (real1 == real2)
85+
? (imag1 >= imag2)
86+
: (real1 > real2 && !std::isnan(imag1) && !std::isnan(imag2));
87+
}
88+
89+
template <typename T> T max_complex(const T &x1, const T &x2)
90+
{
91+
using realT = typename T::value_type;
92+
realT real1 = std::real(x1);
93+
realT real2 = std::real(x2);
94+
realT imag1 = std::imag(x1);
95+
realT imag2 = std::imag(x2);
96+
97+
bool isnan_imag2 = std::isnan(imag2);
98+
bool gt = (real1 == real2)
99+
? (imag1 > imag2)
100+
: (real1 > real2 && !std::isnan(imag1) && !isnan_imag2);
101+
return (std::isnan(real1) || isnan_imag2 || gt) ? x1 : x2;
102+
}
103+
104+
template <typename T> T min_complex(const T &x1, const T &x2)
105+
{
106+
using realT = typename T::value_type;
107+
realT real1 = std::real(x1);
108+
realT real2 = std::real(x2);
109+
realT imag1 = std::imag(x1);
110+
realT imag2 = std::imag(x2);
111+
112+
bool isnan_imag2 = std::isnan(imag2);
113+
bool lt = (real1 == real2)
114+
? (imag1 < imag2)
115+
: (real1 < real2 && !std::isnan(imag1) && !isnan_imag2);
116+
return (std::isnan(real1) || isnan_imag2 || lt) ? x1 : x2;
117+
}
118+
119+
} // namespace math_utils
120+
} // namespace tensor
121+
} // namespace dpctl

0 commit comments

Comments
 (0)