More Related Content Similar to Cranberries interval library 開発の話 Similar to Cranberries interval library 開発の話 (12) Cranberries interval library 開発の話2. 自己紹介
• Twitter : いなむ先生@_EnumHack
• 高校で化学・物理を教えている
• 趣味で数値解析の研究をしている
• プログラミング歴約6年、C++歴約3年
• ちょうど1週間前に「C++の会」というオンラインのコミュニティを立ち上げた
10. 実用例 – 区間最適化
• Levy3 関数の最小化
• 𝑓 𝑥1, 𝑥2 = 𝑖=1
5
𝑖cos 𝑖 − 1 𝑥1 + 𝑖 𝑖=1
5
𝑖cos 𝑖 + 1 𝑥2 + 𝑖
• −10 < 𝑥1, 𝑥2 < 10
• [-10,10]で関数値を区間演算すると
• ans = [ -54.5403, 54.5403 ]
• 真の値域は[-14.976,14.976]
• どうするか?
21. 比較演算子について
• これらの比較方法は4種類の関数として提供される
• bool operator<(interval<T> const&, interval<T> const&)
• はデフォルトではtotal_ordering
bool interval_ordering(interval<T> const&, interval<T> const&)
bool partial_ordering(interval<T> const&, interval<T> const&)
bool weak_ordering(interval<T> const&, interval<T> const&)
bool partial_ordering(interval<T> const&, interval<T> const&);
22. Semantics Switching
auto&& a = interval<>{ 1,3 };
auto&& b = interval<>{ 2,4 };
a < b; // defaultではtotal orderingなのでtrue
{
using namespace interval_ordering;
a < b; // interval ordering に意味が変わるのでfalseになる
}
23. 仕組み
inline namespace total_ordering_policy {
template < typename T, typename U >
inline constexpr bool operator<( interval<T> const& x, interval<U> const& y ){
return total_less( x, y ) ;
}
}
namespace interval_ordering_policy {
template < typename T, typename U>
constexpr bool operator<( T&& x, U&& y ) {
return interval_less( std::forward<T>( x ), std::forward<U>( y ) ) ;
}
}
24. 関数オブジェクトもある
template < order = order::Total >
struct less {
template < typename T, typename U >
constexpr bool operator()( interval<T> const& a, interval<U> const& b ) const {
return total_less( a, b ) ;
}
template < typename T, typename U >
constexpr bool operator()( U const& a, interval<T> const& b ) const {
return total_less( a, b ) ;
}
template < typename T, typename U >
constexpr bool operator()( interval<T> const& a, U const& b ) const {
return total_less( a, b ) ;
}
} ;
25. 関数オブジェクトの使いどころ
interval<double> a{1,2}, b{3,4}, c{5,6}, d{6,7} ;
vector<interval<double>> vec{a,b,c,d} ;
std::sort(vec.begin(),vec.end(),less<order::Weak>()) ;
32. 型変換の仕組み
• 異なるvalue type をもつ区間クラスの演算での型変換規則
template < typename L, typename R >
struct promotion {
typedef decltype( L{} + R{} ) type ;
} ;
template < typename L, typename R >
using promotion_t = typename promotion<L, R>::type ;
33. 次のリリース(予定)
auto&& a = interval<>{ 1,3 };
auto&& b = interval<>{ 2,4 };
{
using namespace Expression;
auto&& expr = a + b; // Expression Templateになる!
}