Commit b2013e68 authored by Davis King's avatar Davis King

Added an overloaded operator/ to allow you to say things like 3.0/my_matrix

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%404112
parent 81d2baa2
......@@ -744,6 +744,42 @@ namespace dlib
return matrix_mul_scal_exp<EXP>(m.m,m.s/static_cast<type>(s));
}
// ----------------------------------------------------------------------------------------
template <typename M>
struct op_s_div_m : basic_op_m<M>
{
typedef typename M::type type;
op_s_div_m( const M& m_, const type& s_) : basic_op_m<M>(m_), s(s_){}
const type s;
const static long cost = M::cost+1;
typedef const typename M::type const_ret_type;
const_ret_type apply (long r, long c) const
{
return s/this->m(r,c);
}
};
template <
typename EXP,
typename S
>
const typename disable_if<is_matrix<S>, matrix_op<op_s_div_m<EXP> > >::type operator/ (
const S& val,
const matrix_exp<EXP>& m
)
{
typedef typename EXP::type type;
typedef op_s_div_m<EXP> op;
return matrix_op<op>(op(m.ref(), static_cast<type>(val)));
}
// ----------------------------------------------------------------------------------------
template <
typename EXP
>
......
......@@ -108,6 +108,17 @@ namespace dlib
scalar value. The resulting matrix will have the same dimensions as m.
!*/
template <typename T>
const matrix_exp operator/ (
const T& value,
const matrix_exp& m
);
/*!
ensures
- returns the result of dividing the given scalar value by all the elements
of matrix m. The resulting matrix will have the same dimensions as m.
!*/
template <typename T>
const matrix_exp operator+ (
const matrix_exp& m,
......
......@@ -595,6 +595,18 @@ namespace
DLIB_TEST(equal(pointwise_multiply((d1)*trans(reciprocal(d2)), m) , (diagm(d1))*(m*inv(diagm(d2)))));
DLIB_TEST(equal(pointwise_multiply((d1)*trans(reciprocal(d2)), m) , ((diagm(d1))*m)*inv(diagm(d2))));
}
{
for (int i = 0; i < 5; ++i)
{
matrix<double> m = randm(3,4) + 1;
DLIB_TEST(equal(1.0/m , reciprocal(m)));
DLIB_TEST(equal(0.0/m , zeros_matrix<double>(3,4)));
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment