1 /* -*- C++ -*- ------------------------------------------------------------
3 Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
5 The Configurable Math Library (CML) is distributed under the terms of the
6 Boost Software License, v1.0 (see cml/LICENSE for details).
8 *-----------------------------------------------------------------------*/
12 * @todo The matrix and matrix order operators could probably be combined
13 * into a single templated implementation, since the only thing that is
14 * different is the access method.
17 #ifndef matrix_comparison_h
18 #define matrix_comparison_h
20 #include <cml/core/cml_assert.h>
21 #include <cml/et/size_checking.h>
22 #include <cml/et/scalar_ops.h>
24 /* This is used below to create a more meaningful compile-time error when
25 * matrix_comparison is not provided with matrix or MatrixExpr arguments:
27 struct matrix_comparison_expects_matrix_args_error
;
29 #define CML_MAT_MAT_ORDER(_order_, _op_, _OpT_) \
30 template<typename E1, class AT1, typename L1, \
31 typename E2, class AT2, typename L2, typename BO> \
34 const matrix<E1,AT1,L1,BO>& left, \
35 const matrix<E2,AT2,L2,BO>& right) \
37 return detail::matrix_##_order_ (left, right, _OpT_ <E1,E2>()); \
40 #define CML_MAT_MATXPR_ORDER(_order_, _op_, _OpT_) \
41 template<typename E, class AT, typename L, typename BO, class XprT> \
44 const matrix<E,AT,L,BO>& left, \
45 MATXPR_ARG_TYPE right) \
47 return detail::matrix_##_order_ (left, right, \
48 _OpT_ <E, typename XprT::value_type>()); \
51 #define CML_MATXPR_MAT_ORDER(_order_, _op_, _OpT_) \
52 template<class XprT, typename E, class AT, typename L, typename BO> \
55 MATXPR_ARG_TYPE left, \
56 const matrix<E,AT,L,BO>& right) \
58 return detail::matrix_##_order_ (left, right, \
59 _OpT_ <typename XprT::value_type, E>()); \
62 #define CML_MATXPR_MATXPR_ORDER(_order_, _op_, _OpT_) \
63 template<class XprT1, class XprT2> \
66 MATXPR_ARG_TYPE_N(1) left, \
67 MATXPR_ARG_TYPE_N(2) right) \
69 return detail::matrix_##_order_ (left, right, \
71 typename XprT1::value_type, \
72 typename XprT2::value_type>()); \
79 /** Matrix strict weak ordering relationship.
81 * OpT must implement a strict weak order on the matrix element type.
82 * operator< and operator> on integer and floating-point types are
85 template<typename LeftT
, typename RightT
, typename OpT
>
87 matrix_weak_order(const LeftT
& left
, const RightT
& right
, OpT
)
90 typedef et::ExprTraits
<LeftT
> left_traits
;
91 typedef et::ExprTraits
<RightT
> right_traits
;
93 /* matrix_comparison() requires matrix expressions: */
95 (et::MatrixExpressions
<LeftT
,RightT
>::is_true
),
96 matrix_comparison_expects_matrix_args_error
);
97 /* Note: parens are required here so that the preprocessor ignores the
101 typedef typename
et::MatrixPromote
<
102 typename
left_traits::result_type
,
103 typename
right_traits::result_type
105 typedef typename
result_type::size_tag size_tag
;
107 /* Verify expression size: */
108 matrix_size N
= et::CheckedSize(left
,right
,size_tag());
109 for(ssize_t i
= 0; i
< N
.first
; ++ i
) {
110 for(ssize_t j
= 0; j
< N
.second
; ++ j
) {
112 left_traits().get(left
,i
,j
),
113 right_traits().get(right
,i
,j
)
116 /* If weak order (a < b) is satisfied, return true: */
118 } else if(OpT().apply(
119 right_traits().get(right
,i
,j
),
120 left_traits().get(left
,i
,j
)
123 /* If !(b < a), then return false: */
127 /* Have !(a < b) && !(b < a) <=> (a >= b && b >= a)
128 * <=> (a == b). so need to test next element:
134 /* XXX Can this be unrolled in any reasonable way? */
136 /* If we get here, then left == right: */
140 /** Matrix total order relationship.
142 * OpT must implement a total order on the matrix element type. operator<=
143 * and operator>= on integer and floating-point types are examples.
145 template<typename LeftT
, typename RightT
, typename OpT
>
147 matrix_total_order(const LeftT
& left
, const RightT
& right
, OpT
)
150 typedef et::ExprTraits
<LeftT
> left_traits
;
151 typedef et::ExprTraits
<RightT
> right_traits
;
153 /* matrix_comparison() requires matrix expressions: */
154 CML_STATIC_REQUIRE_M(
155 (et::MatrixExpressions
<LeftT
,RightT
>::is_true
),
156 matrix_comparison_expects_matrix_args_error
);
157 /* Note: parens are required here so that the preprocessor ignores the
161 typedef typename
et::MatrixPromote
<
162 typename
left_traits::result_type
,
163 typename
right_traits::result_type
165 typedef typename
result_type::size_tag size_tag
;
167 /* Verify expression size: */
168 matrix_size N
= et::CheckedSize(left
,right
,size_tag());
169 for(ssize_t i
= 0; i
< N
.first
; ++ i
) {
170 for(ssize_t j
= 0; j
< N
.second
; ++ j
) {
172 /* Test total order: */
174 left_traits().get(left
,i
,j
),
175 right_traits().get(right
,i
,j
)
178 /* Automatically true if weak order (a <= b) && !(b <= a)
179 * <=> (a <= b) && (b > a) <=> (a < b) is satisfied:
182 right_traits().get(right
,i
,j
),
183 left_traits().get(left
,i
,j
)
187 /* Otherwise, have equality (a <= b) && (b <= a), so
188 * continue to next element:
195 /* Total order isn't satisfied (a > b), so return false: */
200 /* XXX Can this be unrolled in any reasonable way? */
202 /* Total (==) or weak (<) order was satisfied, so return true: */
210 /* XXX There is a better way to handle these with operator traits... */
212 CML_MAT_VEC_ORDER( total_order
, operator==, et::OpEqual
)
213 CML_MATXPR_MAT_ORDER( total_order
, operator==, et::OpEqual
)
214 CML_MAT_MATXPR_ORDER( total_order
, operator==, et::OpEqual
)
215 CML_MATXPR_VECXPR_ORDER( total_order
, operator==, et::OpEqual
)
217 CML_MAT_VEC_ORDER( weak_order
, operator!=, et::OpNotEqual
)
218 CML_MATXPR_MAT_ORDER( weak_order
, operator!=, et::OpNotEqual
)
219 CML_MAT_MATXPR_ORDER( weak_order
, operator!=, et::OpNotEqual
)
220 CML_MATXPR_VECXPR_ORDER( weak_order
, operator!=, et::OpNotEqual
)
222 CML_MAT_VEC_ORDER( weak_order
, operator<, et::OpLess
)
223 CML_MATXPR_MAT_ORDER( weak_order
, operator<, et::OpLess
)
224 CML_MAT_MATXPR_ORDER( weak_order
, operator<, et::OpLess
)
225 CML_MATXPR_VECXPR_ORDER( weak_order
, operator<, et::OpLess
)
227 CML_MAT_VEC_ORDER( weak_order
, operator>, et::OpGreater
)
228 CML_MATXPR_MAT_ORDER( weak_order
, operator>, et::OpGreater
)
229 CML_MAT_MATXPR_ORDER( weak_order
, operator>, et::OpGreater
)
230 CML_MATXPR_VECXPR_ORDER( weak_order
, operator>, et::OpGreater
)
232 CML_MAT_VEC_ORDER( total_order
, operator<=, et::OpLessEqual
)
233 CML_MATXPR_MAT_ORDER( total_order
, operator<=, et::OpLessEqual
)
234 CML_MAT_MATXPR_ORDER( total_order
, operator<=, et::OpLessEqual
)
235 CML_MATXPR_VECXPR_ORDER( total_order
, operator<=, et::OpLessEqual
)
237 CML_MAT_VEC_ORDER( total_order
, operator>=, et::OpGreaterEqual
)
238 CML_MATXPR_MAT_ORDER( total_order
, operator>=, et::OpGreaterEqual
)
239 CML_MAT_MATXPR_ORDER( total_order
, operator>=, et::OpGreaterEqual
)
240 CML_MATXPR_VECXPR_ORDER( total_order
, operator>=, et::OpGreaterEqual
)
244 // -------------------------------------------------------------------------