Skip to content

Commit f709514

Browse files
committed
Simplify descending complex comparator via operand negation
Implement `ExtendedComplexFPGreater` as `ExtendedComplexFPLess{}(-v1, -v2)` instead of duplicating the comparison logic with operands swapped. Negation preserves NaN-ness, so the NaN-based grouping (and its ordering to the end) is unchanged while the finite-component comparison is reversed.
1 parent 490c858 commit f709514

1 file changed

Lines changed: 3 additions & 30 deletions

File tree

dpnp/tensor/libtensor/include/utils/rich_comparisons.hpp

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -107,38 +107,11 @@ struct ExtendedComplexFPLess
107107
template <typename cT>
108108
struct ExtendedComplexFPGreater
109109
{
110-
/* [(R, R), (R, nan), (nan, R), (nan, nan)] — NaN-containing values keep
111-
the same trailing groups as ascending order; only the finite-component
112-
comparison within a group is reversed */
110+
/* Negating both operands reverses the finite comparison but preserves
111+
NaN-ness, so NaN groups stay ordered to the end. */
113112
bool operator()(const cT &v1, const cT &v2) const
114113
{
115-
using realT = typename cT::value_type;
116-
117-
const realT real1 = std::real(v1);
118-
const realT real2 = std::real(v2);
119-
120-
const bool r1_nan = std::isnan(real1);
121-
const bool r2_nan = std::isnan(real2);
122-
123-
const realT imag1 = std::imag(v1);
124-
const realT imag2 = std::imag(v2);
125-
126-
const bool i1_nan = std::isnan(imag1);
127-
const bool i2_nan = std::isnan(imag2);
128-
129-
const int idx1 = ((r1_nan) ? 2 : 0) + ((i1_nan) ? 1 : 0);
130-
const int idx2 = ((r2_nan) ? 2 : 0) + ((i2_nan) ? 1 : 0);
131-
132-
const bool res =
133-
!(r1_nan && i1_nan) &&
134-
((idx1 < idx2) ||
135-
((idx1 == idx2) &&
136-
((r1_nan && !i1_nan && (imag2 < imag1)) ||
137-
(!r1_nan && i1_nan && (real2 < real1)) ||
138-
(!r1_nan && !i1_nan &&
139-
((real2 < real1) || (!(real1 < real2) && (imag2 < imag1)))))));
140-
141-
return res;
114+
return ExtendedComplexFPLess<cT>{}(-v1, -v2);
142115
}
143116
};
144117

0 commit comments

Comments
 (0)