Commit 4d95091d authored by Davis King's avatar Davis King

Added the length and length_squared functions. Also cleaned up

the sigmoid function's code and spec

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402453
parent 278c4bcf
......@@ -67,9 +67,7 @@ DLIB_MATRIX_SIMPLE_STD_FUNCTION(atan)
template <typename M>
static type apply ( const M& m, long r, long c)
{
const double e = 2.718281828459045235360287471352;
double temp = std::pow(e,-m(r,c));
return static_cast<type>(1.0/(1.0 + temp));
return static_cast<type>(1/(1 + std::exp(-m(r,c))));
}
};
};
......
......@@ -129,7 +129,7 @@ namespace dlib
- R::type == the same type that was in m
- R has the same dimensions as m
- for all valid r and c:
R(r,c) == 1/(1 + pow(e,-m(r,c)))
R(r,c) == 1/(1 + exp(-m(r,c)))
!*/
// ----------------------------------------------------------------------------------------
......
......@@ -220,6 +220,42 @@ namespace dlib
return val;
}
// ----------------------------------------------------------------------------------------
template <
typename EXP
>
const typename matrix_exp<EXP>::type length (
const matrix_exp<EXP>& m
)
{
DLIB_ASSERT(m.nr() == 1 || m.nc() == 1,
"\ttype length(const matrix_exp& m)"
<< "\n\tm must be a row or column vector"
<< "\n\tm.nr(): " << m.nr()
<< "\n\tm.nc(): " << m.nc()
);
return std::sqrt(sum(squared(m)));
}
// ----------------------------------------------------------------------------------------
template <
typename EXP
>
const typename matrix_exp<EXP>::type length_squared (
const matrix_exp<EXP>& m
)
{
DLIB_ASSERT(m.nr() == 1 || m.nc() == 1,
"\ttype length_squared(const matrix_exp& m)"
<< "\n\tm must be a row or column vector"
<< "\n\tm.nr(): " << m.nr()
<< "\n\tm.nc(): " << m.nc()
);
return sum(squared(m));
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
......
......@@ -595,6 +595,34 @@ namespace dlib
the corresponding scalar in v.
!*/
// ----------------------------------------------------------------------------------------
const matrix_exp::type length_squared (
const matrix_exp& m
);
/*!
requires
- m.nr() == 1 || m.nc() == 1
(i.e. m must be a vector)
ensures
- returns sum(squared(m))
(i.e. returns the square of the length of the vector m)
!*/
// ----------------------------------------------------------------------------------------
const matrix_exp::type length (
const matrix_exp& m
);
/*!
requires
- m.nr() == 1 || m.nc() == 1
(i.e. m must be a vector)
ensures
- returns sqrt(sum(squared(m)))
(i.e. returns the length of the vector m)
!*/
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// Linear algebra functions
......
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