Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F92131724
vector_checker.hpp
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sun, Nov 17, 15:32
Size
10 KB
Mime Type
text/x-c++
Expires
Tue, Nov 19, 15:32 (2 d)
Engine
blob
Format
Raw Data
Handle
22380742
Attached To
rSPECMICP SpecMiCP / ReactMiCP
vector_checker.hpp
View Options
/* =============================================================================
Copyright (c) 2014 - 2016
F. Georget <fabieng@princeton.edu> Princeton University
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
============================================================================= */
#ifndef SPECMICP_UTILS_VECTORCHECKER_HPP
#define SPECMICP_UTILS_VECTORCHECKER_HPP
#include "../types.hpp"
#include <cmath>
#include <utility>
#include <iostream>
namespace
specmicp
{
//! \namespace specmicp::vector_checker
//! \brief Check values in a vector
namespace
vector_checker
{
//! \brief Type for the
template
<
typename
ScalarT
>
using
VectorT
=
Eigen
::
template
Matrix
<
ScalarT
,
Eigen
::
Dynamic
,
1
>
;
//! \brief The base class of an expression
template
<
typename
Derived
>
struct
BaseBoolExpr
{
//! \brief Return the Derived class
const
Derived
&
self
()
const
{
return
static_cast
<
const
Derived
&>
(
*
this
);}
//! \brief Evaluate the expression
bool
operator
()
()
const
{
return
self
().
out_impl
();}
//! \brief Evalute the expression when assigned to a boolean
operator
bool
()
const
{
return
self
().
out_impl
();}
};
//! \brief Evaluate a logical AND expression
template
<
typename
Derived0
,
typename
Derived1
>
class
AndBoolExpr
:
public
BaseBoolExpr
<
AndBoolExpr
<
Derived0
,
Derived1
>>
{
using
Child0
=
Derived0
;
using
Child1
=
Derived1
;
public
:
AndBoolExpr
(
const
Child0
&
child_0
,
const
Child1
&
child_1
)
:
m_child_0
(
child_0
),
m_child_1
(
child_1
)
{}
AndBoolExpr
(
BaseBoolExpr
<
Derived0
>&&
child_0
,
BaseBoolExpr
<
Derived1
>&&
child_1
)
:
m_child_0
(
std
::
move
(
child_0
.
self
())),
m_child_1
(
std
::
move
(
child_1
.
self
()))
{}
bool
out_impl
()
const
{
if
(
m_child_0
())
return
m_child_1
();
return
false
;
}
private
:
Child0
m_child_0
;
Child1
m_child_1
;
};
//! \brief Evaluate a logical OR Expression
template
<
typename
Derived0
,
typename
Derived1
>
class
OrBoolExpr
:
public
BaseBoolExpr
<
OrBoolExpr
<
Derived0
,
Derived1
>>
{
using
Child0
=
Derived0
;
using
Child1
=
Derived1
;
public
:
OrBoolExpr
(
const
Child0
&
child_0
,
const
Child1
&
child_1
)
:
m_child_0
(
child_0
),
m_child_1
(
child_1
)
{}
OrBoolExpr
(
BaseBoolExpr
<
Derived0
>&&
child_0
,
BaseBoolExpr
<
Derived1
>&&
child_1
)
:
m_child_0
(
std
::
move
(
child_0
.
self
())),
m_child_1
(
std
::
move
(
child_1
.
self
()))
{}
bool
out_impl
()
const
{
if
(
m_child_0
())
return
true
;
return
m_child_1
();
}
private
:
Child0
m_child_0
;
Child1
m_child_1
;
};
//! \brief True if the underlying condition is true for all element
//!
//! Evaluate a condition for all element in a vector,
//! check if the condition formed by 'element BinaryOpTag rhs' is true
//! for all element
template
<
typename
BinaryOpTag
,
typename
ScalarT
>
class
AllExpr
:
public
BaseBoolExpr
<
AllExpr
<
BinaryOpTag
,
ScalarT
>>
{
public
:
AllExpr
(
const
VectorT
<
ScalarT
>&
vector
,
ScalarT
rhs
)
:
m_vector
(
vector
),
m_rhs
(
rhs
)
{}
bool
out_impl
()
const
{
bool
is_true
=
true
;
for
(
typename
VectorT
<
ScalarT
>::
Index
id
=
0
;
id
<
m_vector
.
rows
();
++
id
)
{
if
(
not
op
(
m_vector
(
id
),
m_rhs
))
{
is_true
=
false
;
break
;
}
}
return
is_true
;
}
private
:
BinaryOpTag
op
;
const
VectorT
<
ScalarT
>&
m_vector
;
ScalarT
m_rhs
;
};
//! \brief Wrapper around a vector for creating a 'All' condition
template
<
typename
ScalarT
>
class
All
{
public
:
All
(
const
VectorT
<
ScalarT
>&
vector
)
:
m_vector
(
vector
)
{}
const
VectorT
<
ScalarT
>&
operator
()
()
const
{
return
m_vector
;}
private
:
const
VectorT
<
ScalarT
>&
m_vector
;
};
//! \brief Create a 'All' wrapper from a vector
template
<
typename
ScalarT
>
All
<
ScalarT
>
all
(
const
VectorT
<
ScalarT
>&
vector
)
{
return
All
<
ScalarT
>
(
vector
);
}
//! \brief True if the underlying condition is true for at least one element
//!
//! Evaluate the condition 'element BinaryOpTag rhs' to see if it is true for
//! at least one element
template
<
typename
BinaryOpTag
,
typename
ScalarT
>
class
AnyExpr
:
public
BaseBoolExpr
<
AnyExpr
<
BinaryOpTag
,
ScalarT
>>
{
public
:
AnyExpr
(
const
VectorT
<
ScalarT
>&
vector
,
ScalarT
rhs
)
:
m_vector
(
vector
),
m_rhs
(
rhs
)
{}
bool
out_impl
()
const
{
bool
is_true
=
false
;
for
(
typename
VectorT
<
ScalarT
>::
Index
id
=
0
;
id
<
m_vector
.
rows
();
++
id
)
{
if
(
op
(
m_vector
(
id
),
m_rhs
))
{
is_true
=
true
;
break
;
}
}
return
is_true
;
}
private
:
BinaryOpTag
op
;
const
VectorT
<
ScalarT
>&
m_vector
;
ScalarT
m_rhs
;
};
//! \brief Wrapper around a vector to create a 'Any' condition
template
<
typename
ScalarT
>
class
Any
{
public
:
Any
(
const
VectorT
<
ScalarT
>&
vector
)
:
m_vector
(
vector
)
{}
const
VectorT
<
ScalarT
>&
operator
()
()
const
{
return
m_vector
;}
private
:
const
VectorT
<
ScalarT
>&
m_vector
;
};
//! \brief create a 'Any' wrapper
template
<
typename
ScalarT
>
Any
<
ScalarT
>
any
(
const
VectorT
<
ScalarT
>&
vector
)
{
return
Any
<
ScalarT
>
(
vector
);
}
//! \brief True if all elements of a vector are bounded
//!
//! Bounds must be finite
template
<
typename
ScalarT
>
class
BoundedExpr
:
public
BaseBoolExpr
<
BoundedExpr
<
ScalarT
>>
{
public
:
BoundedExpr
(
const
VectorT
<
ScalarT
>&
vector
,
ScalarT
lower_bound
,
ScalarT
upper_bound
)
:
m_vector
(
vector
),
m_lower
(
lower_bound
),
m_upper
(
upper_bound
)
{}
bool
out_impl
()
const
{
bool
is_true
=
true
;
for
(
typename
VectorT
<
ScalarT
>::
Index
id
=
0
;
id
<
m_vector
.
rows
();
++
id
)
{
const
ScalarT
&
value
=
m_vector
(
id
);
if
(
not
(
std
::
isfinite
(
value
)
&&
value
>=
m_lower
&&
value
<=
m_upper
)
)
{
is_true
=
false
;
break
;
}
}
return
is_true
;
}
private
:
const
VectorT
<
ScalarT
>&
m_vector
;
ScalarT
m_lower
;
ScalarT
m_upper
;
};
//! \brief create a 'Bounded' expression
template
<
typename
ScalarT
>
BoundedExpr
<
ScalarT
>
is_bounded
(
const
VectorT
<
ScalarT
>&
vector
,
ScalarT
lower
,
ScalarT
upper
)
{
return
BoundedExpr
<
ScalarT
>
(
vector
,
lower
,
upper
);
}
// Logical Operators
// =================
//! \brief Evaluate the logical AND between two expressions
template
<
typename
Derived0
,
typename
Derived1
>
AndBoolExpr
<
Derived0
,
Derived1
>
operator
&&
(
BaseBoolExpr
<
Derived0
>&&
child_0
,
BaseBoolExpr
<
Derived1
>&&
child_1
)
{
return
AndBoolExpr
<
Derived0
,
Derived1
>
(
std
::
forward
<
BaseBoolExpr
<
Derived0
>>
(
child_0
),
std
::
forward
<
BaseBoolExpr
<
Derived1
>>
(
child_1
)
);
}
//! \brief Evaluate the logical AND between two expressions
template
<
typename
Derived0
,
typename
Derived1
>
OrBoolExpr
<
Derived0
,
Derived1
>
operator
||
(
BaseBoolExpr
<
Derived0
>&&
child_0
,
BaseBoolExpr
<
Derived1
>&&
child_1
)
{
return
OrBoolExpr
<
Derived0
,
Derived1
>
(
std
::
forward
<
BaseBoolExpr
<
Derived0
>>
(
child_0
),
std
::
forward
<
BaseBoolExpr
<
Derived1
>>
(
child_1
)
);
}
// Comparison Operator
// ===================
#define make_tag_operator(name, op) \
struct name \
{ \
template <typename ScalarT> \
bool operator() (const ScalarT& lhs, const ScalarT& rhs) const { \
return (lhs op rhs); \
} \
};
#define make_operator_overloading_class(name, op, class_name) \
template <typename ScalarT> \
class_name ## Expr<name, ScalarT> operator op (class_name<ScalarT>&& lhs, ScalarT rhs) \
{ \
return class_name ## Expr<name, ScalarT>(lhs(), rhs); \
}
#define make_operator_overloading_all(name, op) \
make_operator_overloading_class(name, op, All)
#define make_operator_overloading_any(name, op) \
make_operator_overloading_class(name, op, Any)
#define make_operator(name, op) \
make_tag_operator(name, op) \
make_operator_overloading_all(name, op) \
make_operator_overloading_any(name, op)
make_operator
(
DifferentThan
,
!=
)
make_operator
(
EqualTo
,
==
)
make_operator
(
GreaterThan
,
>
)
make_operator
(
GreaterThanOrEqualTo
,
>=
)
make_operator
(
LessThan
,
<
)
make_operator
(
LessThanOrEqualTo
,
<=
)
#undef make_operator
#undef make_operator_overloading_any
#undef make_operator_overloading_all
#undef make_operator_overloading_class
#undef make_tag_operator
// special operator
struct
IsFinite
{
template
<
typename
ScalarT
>
bool
operator
()
(
const
ScalarT
&
lhs
,
const
ScalarT
&
_
)
const
{
return
(
std
::
isfinite
(
lhs
));
}
};
//! \brief check that all element of a vector is finite
template
<
typename
ScalarT
>
AllExpr
<
IsFinite
,
ScalarT
>
is_finite
(
const
VectorT
<
ScalarT
>&
vector
)
{
return
AllExpr
<
IsFinite
,
ScalarT
>
(
vector
,
0.0
);
}
//! \brief create a 'LowerBounded' expression
template
<
typename
ScalarT
>
AllExpr
<
GreaterThanOrEqualTo
,
ScalarT
>
is_lower_bounded
(
const
VectorT
<
ScalarT
>&
vector
,
ScalarT
lower
)
{
return
AllExpr
<
GreaterThanOrEqualTo
,
ScalarT
>
(
vector
,
lower
);
}
//! \brief create a 'UpperBounded' expression
template
<
typename
ScalarT
>
AllExpr
<
LessThanOrEqualTo
,
ScalarT
>
is_upper_bounded
(
const
VectorT
<
ScalarT
>&
vector
,
ScalarT
upper
)
{
return
AllExpr
<
LessThanOrEqualTo
,
ScalarT
>
(
vector
,
upper
);
}
}
//end namespace vector_checker
}
//end namespace specmicp
#endif
// SPECMICP_UTILS_VECTORCHECKER_HPP
Event Timeline
Log In to Comment