Skip to content

Commit 81b1808

Browse files
committed
Adds math_utils.hpp with complex comparison functions
1 parent 79994c1 commit 81b1808

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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 func_utils
34+
{
35+
36+
template <T>
37+
bool less_complex(const std::complex<T> &in1, const std::complex<T> &in2)
38+
{
39+
T real1 = std::real(in1);
40+
T real2 = std::real(in2);
41+
T imag1 = std::imag(in1);
42+
43+
return (real1 == real2) ? (imag1 < std::imag(x2)) : real1 < real2;
44+
}
45+
46+
template <T>
47+
bool greater_complex(const std::complex<T> &in1, const std::complex<T> &in2)
48+
{
49+
T real1 = std::real(in1);
50+
T real2 = std::real(in2);
51+
T imag1 = std::imag(in1);
52+
53+
return (real1 == real2) ? (imag1 > std::imag(x2)) : real1 > real2;
54+
}
55+
56+
template <T>
57+
bool less_equal_complex(const std::complex<T> &in1, const std::complex<T> &in2)
58+
{
59+
T real1 = std::real(in1);
60+
T real2 = std::real(in2);
61+
T imag1 = std::imag(in1);
62+
63+
return (real1 == real2) ? (imag1 <= std::imag(x2)) : real1 <= real2;
64+
}
65+
66+
template <T>
67+
bool greater_equal_complex(const std::complex<T> &in1,
68+
const std::complex<T> &in2)
69+
{
70+
T real1 = std::real(in1);
71+
T real2 = std::real(in2);
72+
T imag1 = std::imag(in1);
73+
74+
return (real1 == real2) ? (imag1 >= std::imag(x2)) : real1 >= real2;
75+
}
76+
77+
template <T>
78+
std::complex<T> max_complex(const std::complex<T> &in1,
79+
const std::complex<T> &in2)
80+
{
81+
T real1 = std::real(in1);
82+
T real2 = std::real(in2);
83+
T imag1 = std::imag(in1);
84+
85+
bool is_greater =
86+
(real1 == real2) ? (imag1 > std::imag(x2)) : real1 > real2;
87+
return (std::isnan(real1) || std::isnan(imag1) || greater_c) ? in1 : in2;
88+
}
89+
90+
template <T>
91+
std::complex<T> min_complex(const std::complex<T> &in1,
92+
const std::complex<T> &in2)
93+
{
94+
T real1 = std::real(in1);
95+
T real2 = std::real(in2);
96+
T imag1 = std::imag(in1);
97+
98+
bool is_less = (real1 == real2) ? (imag1 < std::imag(x2)) : real1 < real2;
99+
return (std::isnan(real1) || std::isnan(imag1) || greater_c) ? in1 : in2;
100+
}
101+
102+
} // namespace func_utils
103+
} // namespace tensor
104+
} // namespace dpctl

0 commit comments

Comments
 (0)