Skip to content

Commit 0ee44af

Browse files
committed
Adds math_utils.hpp with complex comparison functions
1 parent acd1a60 commit 0ee44af

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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 <T>
37+
bool less_complex(const std::complex<T> &x1, const std::complex<T> &x2)
38+
{
39+
T real1 = std::real(x1);
40+
T real2 = std::real(x2);
41+
T imag1 = std::imag(x1);
42+
T imag2 = std::imag(x2);
43+
44+
return (real1 == real2)
45+
? (imag1 < imag2)
46+
: (real1 < real2 && !std::isnan(imag1) && !std::isnan(imag2));
47+
}
48+
49+
template <T>
50+
bool greater_complex(const std::complex<T> &x1, const std::complex<T> &x2)
51+
{
52+
T real1 = std::real(x1);
53+
T real2 = std::real(x2);
54+
T imag1 = std::imag(x1);
55+
T imag2 = std::imag(x2);
56+
57+
return (real1 == real2)
58+
? (imag1 > imag2)
59+
: (real1 > real2 && !std::isnan(imag1) && !std::isnan(imag2));
60+
}
61+
62+
template <T>
63+
bool less_equal_complex(const std::complex<T> &x1, const std::complex<T> &x2)
64+
{
65+
T real1 = std::real(x1);
66+
T real2 = std::real(x2);
67+
T imag1 = std::imag(x1);
68+
T imag2 = std::imag(x2);
69+
70+
return (real1 == real2)
71+
? (imag1 <= imag2)
72+
: (real1 < real2 && !std::isnan(imag1) && !std::isnan(imag2));
73+
}
74+
75+
template <T>
76+
bool greater_equal_complex(const std::complex<T> &x1, const std::complex<T> &x2)
77+
{
78+
T real1 = std::real(x1);
79+
T real2 = std::real(x2);
80+
T imag1 = std::imag(x1);
81+
T imag2 = std::imag(x2);
82+
83+
return (real1 == real2)
84+
? (imag1 >= imag2)
85+
: (real1 > real2 && !std::isnan(imag1) && !std::isnan(imag2));
86+
}
87+
88+
template <T>
89+
std::complex<T> max_complex(const std::complex<T> &x1,
90+
const std::complex<T> &x2)
91+
{
92+
T real1 = std::real(x1);
93+
T real2 = std::real(x2);
94+
T imag1 = std::imag(x1);
95+
T imag2 = std::imag(x2);
96+
97+
bool isnan_imag2 = std::isnan(imag2);
98+
bool gt = (real1 == real2) ? (imag1 > imag2)
99+
: (real1 > real2 && !std::isnan(imag1) &&
100+
!isnan_imag2) return (std::isnan(real1) || isnan_imag2 || gt)
101+
? x1
102+
: x2;
103+
}
104+
105+
template <T>
106+
std::complex<T> min_complex(const std::complex<T> &x1,
107+
const std::complex<T> &x2)
108+
{
109+
T real1 = std::real(x1);
110+
T real2 = std::real(x2);
111+
T imag1 = std::imag(x1);
112+
T imag2 = std::imag(x2);
113+
114+
bool isnan_imag2 = std::isnan(imag2);
115+
bool lt = (real1 == real2) ? (imag1 < imag2)
116+
: (real1 < real2 && !std::isnan(imag1) &&
117+
!isnan_imag2) return (std::isnan(real1) || isnan_imag2 || lt)
118+
? x1
119+
: x2;
120+
}
121+
122+
} // namespace math_utils
123+
} // namespace tensor
124+
} // namespace dpctl

0 commit comments

Comments
 (0)