您可以很容易地将常量分配给向量或矩阵:
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));