About elementary operations in boost :: ublas

I find that boost :: ublas does not support stepwise operations and operations in the sequence are very good (but the efficiency is pretty high :)) I'm trying

D = A ^ 2. * B ^ 3. * C

where A, B, C are all square matrices of the same size, the operator ". *" denotes the operation "element by element", and ^ is the degree of the matrix. With boost: ublas, I wrote

for (int n=0; n<300; n++)
{
  for (int k=0; k<300; k++)
  {
    D(n, k) = pow(abs(A(n, k)), 2)*pow(abs(B(n, k)), 3)*C(n, k);
  }
}

In my program, I have many sequence operations, as shown above, anyway, can I get the same result, but using one line of code instead of a loop?

In addition, I notice that it seems impractical to assign a constant to all elements of a matrix or vector, for example

boost :: numeric :: ublas :: vector v (100); v = 0.2;

, , , ? , , . Armadillo, , ( 10 ).

+5
1

:

vector<double> v = scalar_vector<double>(100, 0.2);

( c_vector bounded_vector) :

vector<double> v(100, 0.2);

, , , . :):

namespace boost { namespace numeric { namespace ublas {
template<class M, class P>
struct scalar_power:
    public scalar_binary_functor<M, P> {
    typedef typename scalar_binary_functor<M, P>::argument1_type argument1_type;
    typedef typename scalar_binary_functor<M, P>::argument2_type argument2_type;
    typedef typename scalar_binary_functor<M, P>::result_type result_type;

    static BOOST_UBLAS_INLINE
    result_type apply (argument1_type t1, argument2_type t2) {
        return pow(abs(t1), t2);
    }
};

template<class M, class P>
BOOST_UBLAS_INLINE
typename enable_if< is_convertible<P, typename M::value_type>,
typename matrix_binary_scalar2_traits<M, const P, scalar_power<typename M::value_type, P> >::result_type
>::type
operator ^ (const matrix_expression<M> &m,
            const P &p) {
    typedef typename matrix_binary_scalar2_traits<M, const P, scalar_power<typename M::value_type, P> >::expression_type expression_type;
    return expression_type (m(), p);
}
}}}

:

D = element_prod(A^2, element_prod(B^3, C));
+3

All Articles