8 #ifndef SIG_UTIL_BLAS_HPP
9 #define SIG_UTIL_BLAS_HPP
11 #include "../helper/maybe.hpp"
12 #include "../helper/container_helper.hpp"
13 #include "../helper/helper_modules.hpp"
15 #ifdef SIG_ENABLE_BOOST
17 #include <boost/numeric/ublas/vector.hpp>
18 #include <boost/numeric/ublas/matrix.hpp>
19 #include <boost/numeric/ublas/lu.hpp>
20 #include <boost/numeric/ublas/triangular.hpp>
28 using matrix_u = boost::numeric::ublas::matrix<T>;
49 using vector_u = boost::numeric::ublas::vector<T>;
114 template <
class V,
class R = std::vector<
typename V::value_type>>
119 for (uint i = 0; i < vec.size(); ++i){
127 template <
class M,
class R = std::vector<
typename M::value_type>>
132 for (uint i = 0; i < mat.size2(); ++i){
140 template <
class M,
class R = std::vector<std::vector<
typename M::value_type>>>
147 for (uint i=0; i < mat.size1(); ++i){
150 for (uint j = 0; j < mat.size2(); ++j){
160 template <class C, class T = typename impl::container_traits<C>::value_type>
165 for (uint i=0; i<vec.size(); ++i){
173 template <class CC, class T = typename impl::container_traits<typename impl::container_traits<CC>::value_type>::value_type>
178 const uint size1 = mat.size();
179 const uint size2 = mat.begin()->size();
182 for (uint i = 0; i < size1; ++i){
183 for (uint j = 0; j < size2; ++j){
184 dest(i, j) = mat[i][j];
202 class RT =
typename std::conditional<std::is_integral<T>::value, double, T>::type
207 using namespace boost::numeric::ublas;
209 permutation_matrix<> pm(mat.size1());
213 matrix<RT> result = identity_matrix<RT>(mat.size1());
215 lu_substitute(mat, pm, result);
217 return Just<matrix_u<RT>>(std::move(result));
221 class RT =
typename std::conditional<std::is_integral<T>::value, double, T>::type
234 class RT =
typename std::conditional<std::is_integral<T>::value, double, T>::type
239 using namespace boost::numeric::ublas;
241 permutation_matrix<> pm(mat.size1());
245 lu_substitute(mat, pm, vec);
247 return Just<vector_u<RT>>(std::move(vec));
251 class RT =
typename std::conditional<std::is_integral<T>::value, double, T>::type
263 class RT =
typename std::conditional<std::is_integral<T>::value, double, T>::type
274 class RT =
typename std::conditional<std::is_integral<T>::value, double, T>::type
286 template <
class F,
class V>
289 using namespace boost::numeric::ublas;
290 using RT = decltype(
impl::eval(std::forward<F>(func), std::forward<V>(vec)(0)));
294 for (uint i = 0, size = vec.size(); i < size; ++i){
295 result(i) = std::forward<F>(func)(std::forward<V>(vec)(i));
301 template <
class F,
class M>
304 using namespace boost::numeric::ublas;
305 using RT = decltype(
impl::eval(std::forward<F>(func), std::forward<M>(mat)(0,0)));
307 const uint size1 = mat.size1();
308 const uint size2 = mat.size2();
312 for (uint i = 0; i < size1; ++i){
313 for (uint j = 0; j < size2; ++j){
314 result(i, j) = std::forward<F>(func)(std::forward<M>(mat)(i, j));
322 template <
class F,
class V,
class... Vs>
325 const uint length =
sig::min(vec.size(), vecs.size()...);
330 template <
class F,
class M>
333 const uint size1 = mat.size1();
334 const uint size2 = mat.size2();
336 for (uint i = 0; i < size1; ++i){
337 for (uint j = 0; j < size2; ++j){
338 std::forward<F>(func)(mat(i, j));
344 template <
class F,
class M>
347 using namespace boost::numeric::ublas;
349 const uint size =
min(mat.size1(), mat.size2());
350 matrix_vector_range<M> r(mat, range(0, size), range(0, size));
352 for (
auto it = r.begin(),
end = r.end(); it !=
end; ++it){
353 std::forward<F>(func)(*it);
358 typename std::enable_if<std::is_floating_point<typename V::value_type>::value>::type*& =
enabler
363 for (
auto& e : data) e /=
sum;
auto to_matrix_ublas(CC const &mat) -> matrix_u< T >
STLのvectorの2次元配列 から ublas::matrix へ変換
auto end(C &&c) -> std::move_iterator< typename RC::iterator >
void for_diagonal(F &&func, M &mat)
行列の対角要素の対して、代入演算を行う関数を適用する
auto Nothing() -> typename impl::SameIf< T, void, typename boost::none_t, Maybe< T >>::type
値コンストラクタ
auto matrix_vector_solve(matrix_u< T > &&mat, vector_u< T > &&vec) -> Maybe< vector_u< RT >>
連立方程式を解く
auto map_v(F &&func, V &&vec)
ベクトルの全要素に対して、値を返す関数を適用し、その結果のベクトルを返す
bool normalize_dist_v(V &data)
boost::numeric::ublas::matrix< T > matrix_u
void iterative_assign(uint loop, F &&func, Its...iterators)
auto from_matrix_ublas(M const &mat, uint row) -> R
ublas::matrix の指定行を STLのvectorへ変換
auto map_m(F &&func, M &&mat)
auto invert_matrix(matrix_u< T > &&mat) -> Maybe< matrix_u< RT >>
逆行列を求める
auto eval(F &&f, Args &&...args) -> decltype(f(std::forward< Args >(args)...))
void for_each_v(F &&func, V &vec, Vs &&...vecs)
ベクトルの全要素に対して、関数を適用する
auto from_vector_ublas(V const &vec) -> R
ublas::vector から STLのvectorへ変換
boost::numeric::ublas::vector< T > vector_u
void for_each_m(F &&func, M &mat)
行列の全要素に対して、代入演算を行う関数を適用する
auto to_vector_ublas(C const &vec) -> vector_u< T >
STLのvector から ublas::vector へ変換
auto sum(C const &data) -> typename impl::SameIf< R, void, typename impl::container_traits< C >::value_type, R >::type
総和
auto begin(C &&c) -> std::move_iterator< typename RC::iterator >