mirror of
https://github.com/davisking/dlib.git
synced 2024-11-01 10:14:53 +08:00
General code cleanup. Most of the changes are due to me simplifying
the way a bunch of requirements were written. I also added missing assert statements to the randomize_samples() functions. --HG-- extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%403040
This commit is contained in:
parent
111f19c65d
commit
98fa067eeb
@ -1191,13 +1191,12 @@ namespace dlib
|
||||
<< "\n\tnc(): " << nc()
|
||||
<< "\n\tthis: " << this
|
||||
);
|
||||
DLIB_ASSERT( ((nc() == 1 && i < nr()) || (nr() == 1 && i < nc())) && i >= 0,
|
||||
DLIB_ASSERT( 0 <= i && i < size(),
|
||||
"\tconst type matrix::operator(i)"
|
||||
<< "\n\tYou must give a valid row/column number"
|
||||
<< "\n\ti: " << i
|
||||
<< "\n\tnr(): " << nr()
|
||||
<< "\n\tnc(): " << nc()
|
||||
<< "\n\tthis: " << this
|
||||
<< "\n\ti: " << i
|
||||
<< "\n\tsize(): " << size()
|
||||
<< "\n\tthis: " << this
|
||||
);
|
||||
return data(i);
|
||||
}
|
||||
@ -1216,13 +1215,12 @@ namespace dlib
|
||||
<< "\n\tnc(): " << nc()
|
||||
<< "\n\tthis: " << this
|
||||
);
|
||||
DLIB_ASSERT( ((nc() == 1 && i < nr()) || (nr() == 1 && i < nc())) && i >= 0,
|
||||
DLIB_ASSERT( 0 <= i && i < size(),
|
||||
"\tconst type matrix::operator(i)"
|
||||
<< "\n\tYou must give a valid row/column number"
|
||||
<< "\n\ti: " << i
|
||||
<< "\n\tnr(): " << nr()
|
||||
<< "\n\tnc(): " << nc()
|
||||
<< "\n\tthis: " << this
|
||||
<< "\n\ti: " << i
|
||||
<< "\n\tsize(): " << size()
|
||||
<< "\n\tthis: " << this
|
||||
);
|
||||
return data(i);
|
||||
}
|
||||
|
@ -483,10 +483,7 @@ namespace dlib
|
||||
/*!
|
||||
requires
|
||||
- nc() == 1 || nr() == 1 (i.e. this must be a column or row vector)
|
||||
- if (nc() == 1) then
|
||||
- 0 <= i < nr()
|
||||
- else
|
||||
- 0 <= i < nc()
|
||||
- 0 <= i < size()
|
||||
ensures
|
||||
- if (nc() == 1) then
|
||||
- returns a reference to (*this)(i,0)
|
||||
@ -500,10 +497,7 @@ namespace dlib
|
||||
/*!
|
||||
requires
|
||||
- nc() == 1 || nr() == 1 (i.e. this must be a column or row vector)
|
||||
- if (nc() == 1) then
|
||||
- 0 <= i < nr()
|
||||
- else
|
||||
- 0 <= i < nc()
|
||||
- 0 <= i < size()
|
||||
ensures
|
||||
- if (nc() == 1) then
|
||||
- returns a reference to (*this)(i,0)
|
||||
|
@ -20,6 +20,23 @@
|
||||
namespace dlib
|
||||
{
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
template <typename EXP>
|
||||
inline bool is_row_vector (
|
||||
const matrix_exp<EXP>& m
|
||||
) { return m.nr() == 1; }
|
||||
|
||||
template <typename EXP>
|
||||
inline bool is_col_vector (
|
||||
const matrix_exp<EXP>& m
|
||||
) { return m.nc() == 1; }
|
||||
|
||||
template <typename EXP>
|
||||
inline bool is_vector (
|
||||
const matrix_exp<EXP>& m
|
||||
) { return is_row_vector(m) || is_col_vector(m); }
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
template <
|
||||
@ -62,7 +79,7 @@ namespace dlib
|
||||
const matrix_exp<EXP>& m
|
||||
)
|
||||
{
|
||||
DLIB_ASSERT(m.size() > 0 && (m.nr() == 1 || m.nc() == 1),
|
||||
DLIB_ASSERT(m.size() > 0 && is_vector(m) == true,
|
||||
"\tlong index_of_max(const matrix_exp& m)"
|
||||
<< "\n\tm must be a row or column matrix"
|
||||
<< "\n\tm.size(): " << m.size()
|
||||
@ -94,7 +111,7 @@ namespace dlib
|
||||
const matrix_exp<EXP>& m
|
||||
)
|
||||
{
|
||||
DLIB_ASSERT(m.size() > 0 && (m.nr() == 1 || m.nc() == 1),
|
||||
DLIB_ASSERT(m.size() > 0 && is_vector(m),
|
||||
"\tlong index_of_min(const matrix_exp& m)"
|
||||
<< "\n\tm must be a row or column matrix"
|
||||
<< "\n\tm.size(): " << m.size()
|
||||
@ -184,7 +201,7 @@ namespace dlib
|
||||
const matrix_exp<EXP>& m
|
||||
)
|
||||
{
|
||||
DLIB_ASSERT(m.nr() == 1 || m.nc() == 1,
|
||||
DLIB_ASSERT(is_vector(m) == true,
|
||||
"\ttype length(const matrix_exp& m)"
|
||||
<< "\n\tm must be a row or column vector"
|
||||
<< "\n\tm.nr(): " << m.nr()
|
||||
@ -202,7 +219,7 @@ namespace dlib
|
||||
const matrix_exp<EXP>& m
|
||||
)
|
||||
{
|
||||
DLIB_ASSERT(m.nr() == 1 || m.nc() == 1,
|
||||
DLIB_ASSERT(is_vector(m) == true,
|
||||
"\ttype length_squared(const matrix_exp& m)"
|
||||
<< "\n\tm must be a row or column vector"
|
||||
<< "\n\tm.nr(): " << m.nr()
|
||||
@ -698,7 +715,7 @@ namespace dlib
|
||||
{
|
||||
// You can only make a diagonal matrix out of a row or column vector
|
||||
COMPILE_TIME_ASSERT(EXP::NR == 0 || EXP::NR == 1 || EXP::NC == 1 || EXP::NC == 0);
|
||||
DLIB_ASSERT(m.nr() == 1 || m.nc() == 1,
|
||||
DLIB_ASSERT(is_vector(m),
|
||||
"\tconst matrix_exp diagm(const matrix_exp& m)"
|
||||
<< "\n\tYou can only apply diagm() to a row or column matrix"
|
||||
<< "\n\tm.nr(): " << m.nr()
|
||||
@ -966,7 +983,7 @@ namespace dlib
|
||||
// perform static checks to make sure the matrices contained in m are column vectors
|
||||
COMPILE_TIME_ASSERT(EXP::type::NC == 1 || EXP::type::NC == 0 );
|
||||
|
||||
DLIB_ASSERT(m.nr() > 1 && m.nc() == 1,
|
||||
DLIB_ASSERT(m.size() > 1 && is_col_vector(m),
|
||||
"\tconst matrix covariance(const matrix_exp& m)"
|
||||
<< "\n\tYou can only apply covariance() to a column matrix"
|
||||
<< "\n\tm.nr(): " << m.nr()
|
||||
@ -975,12 +992,12 @@ namespace dlib
|
||||
#ifdef ENABLE_ASSERTS
|
||||
for (long i = 0; i < m.nr(); ++i)
|
||||
{
|
||||
DLIB_ASSERT(m(0).nr() == m(i).nr() && m(i).nr() > 0 && m(i).nc() == 1,
|
||||
DLIB_ASSERT(m(0).size() == m(i).size() && m(i).size() > 0 && is_col_vector(m(i)),
|
||||
"\tconst matrix covariance(const matrix_exp& m)"
|
||||
<< "\n\tYou can only apply covariance() to a column matrix of column matrices"
|
||||
<< "\n\tm(0).nr(): " << m(0).nr()
|
||||
<< "\n\tm(i).nr(): " << m(i).nr()
|
||||
<< "\n\tm(i).nc(): " << m(i).nc()
|
||||
<< "\n\tm(0).size(): " << m(0).size()
|
||||
<< "\n\tm(i).size(): " << m(i).size()
|
||||
<< "\n\tis_col_vector(m(i)): " << (is_col_vector(m(i)) ? "true" : "false")
|
||||
<< "\n\ti: " << i
|
||||
);
|
||||
}
|
||||
@ -1700,7 +1717,7 @@ namespace dlib
|
||||
COMPILE_TIME_ASSERT(EXP2::NC == 1 || EXP2::NC == 0);
|
||||
COMPILE_TIME_ASSERT(EXP1::NC == EXP2::NR || EXP1::NC == 0 || EXP2::NR == 0);
|
||||
|
||||
DLIB_ASSERT(v.nc() == 1 && v.nr() == m.nc(),
|
||||
DLIB_ASSERT(is_col_vector(v) == true && v.size() == m.nc(),
|
||||
"\tconst matrix_exp scale_columns(m, v)"
|
||||
<< "\n\tv must be a column vector and its length must match the number of columns in m"
|
||||
<< "\n\tm.nr(): " << m.nr()
|
||||
@ -1738,7 +1755,7 @@ namespace dlib
|
||||
COMPILE_TIME_ASSERT(NC2 == 1 || NC2 == 0);
|
||||
COMPILE_TIME_ASSERT(NC == NR2 || NC == 0 || NR2 == 0);
|
||||
|
||||
DLIB_ASSERT(v.nc() == 1 && v.nr() == m.nc(),
|
||||
DLIB_ASSERT(is_col_vector(v) == true && v.size() == m.nc(),
|
||||
"\tconst matrix_exp sort_columns(m, v)"
|
||||
<< "\n\tv must be a column vector and its length must match the number of columns in m"
|
||||
<< "\n\tm.nr(): " << m.nr()
|
||||
@ -1785,7 +1802,7 @@ namespace dlib
|
||||
COMPILE_TIME_ASSERT(NC2 == 1 || NC2 == 0);
|
||||
COMPILE_TIME_ASSERT(NC == NR2 || NC == 0 || NR2 == 0);
|
||||
|
||||
DLIB_ASSERT(v.nc() == 1 && v.nr() == m.nc(),
|
||||
DLIB_ASSERT(is_col_vector(v) == true && v.size() == m.nc(),
|
||||
"\tconst matrix_exp rsort_columns(m, v)"
|
||||
<< "\n\tv must be a column vector and its length must match the number of columns in m"
|
||||
<< "\n\tm.nr(): " << m.nr()
|
||||
|
@ -34,7 +34,8 @@ namespace dlib
|
||||
);
|
||||
/*!
|
||||
requires
|
||||
- m is a row or column matrix
|
||||
- is_vector(m) == true
|
||||
(i.e. m is a row or column matrix)
|
||||
ensures
|
||||
- returns a square matrix M such that:
|
||||
- diag(M) == m
|
||||
@ -253,8 +254,8 @@ namespace dlib
|
||||
ensures
|
||||
- returns a matrix M such that:
|
||||
- M::type == double
|
||||
- M.nr() == 1
|
||||
- M.nc() == num
|
||||
- is_row_vector(M) == true
|
||||
- M.size() == num
|
||||
- M == a row vector with num linearly spaced values beginning with start
|
||||
and stopping with end.
|
||||
- M(num-1) == end
|
||||
@ -275,8 +276,8 @@ namespace dlib
|
||||
ensures
|
||||
- returns a matrix M such that:
|
||||
- M::type == double
|
||||
- M.nr() == 1
|
||||
- M.nc() == num
|
||||
- is_row_vector(M) == true
|
||||
- M.size() == num
|
||||
- M == a row vector with num logarithmically spaced values beginning with
|
||||
10^start and stopping with 10^end.
|
||||
(i.e. M == pow(10, linspace(start, end, num)))
|
||||
@ -318,8 +319,8 @@ namespace dlib
|
||||
- returns a reference to vector
|
||||
- else
|
||||
- returns a matrix R such that:
|
||||
- R.nr() == vector.size()
|
||||
- R.nc() == 1
|
||||
- is_col_vector(R) == true
|
||||
- R.size() == vector.size()
|
||||
- for all valid r:
|
||||
R(r) == vector[r]
|
||||
!*/
|
||||
@ -610,8 +611,8 @@ namespace dlib
|
||||
);
|
||||
/*!
|
||||
requires
|
||||
- v.nc() == 1 (i.e. v is a column vector)
|
||||
- v.nr() == m.nc()
|
||||
- is_col_vector(v) == true
|
||||
- v.size() == m.nc()
|
||||
- m and v both contain the same type of element
|
||||
ensures
|
||||
- returns a matrix R such that:
|
||||
@ -632,8 +633,8 @@ namespace dlib
|
||||
);
|
||||
/*!
|
||||
requires
|
||||
- v.nc() == 1 (i.e. v is a column vector)
|
||||
- v.nr() == m.nc()
|
||||
- is_col_vector(v) == true
|
||||
- v.size() == m.nc()
|
||||
- m and v both contain the same type of element
|
||||
ensures
|
||||
- the dimensions for m and v are not changed
|
||||
@ -654,8 +655,8 @@ namespace dlib
|
||||
);
|
||||
/*!
|
||||
requires
|
||||
- v.nc() == 1 (i.e. v is a column vector)
|
||||
- v.nr() == m.nc()
|
||||
- is_col_vector(v) == true
|
||||
- v.size() == m.nc()
|
||||
- m and v both contain the same type of element
|
||||
ensures
|
||||
- the dimensions for m and v are not changed
|
||||
@ -674,8 +675,7 @@ namespace dlib
|
||||
);
|
||||
/*!
|
||||
requires
|
||||
- m.nr() == 1 || m.nc() == 1
|
||||
(i.e. m must be a vector)
|
||||
- is_vector(m) == true
|
||||
ensures
|
||||
- returns sum(squared(m))
|
||||
(i.e. returns the square of the length of the vector m)
|
||||
@ -688,13 +688,47 @@ namespace dlib
|
||||
);
|
||||
/*!
|
||||
requires
|
||||
- m.nr() == 1 || m.nc() == 1
|
||||
(i.e. m must be a vector)
|
||||
- is_vector(m) == true
|
||||
ensures
|
||||
- returns sqrt(sum(squared(m)))
|
||||
(i.e. returns the length of the vector m)
|
||||
!*/
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
bool is_row_vector (
|
||||
const matrix_exp& m
|
||||
);
|
||||
/*!
|
||||
ensures
|
||||
- if (m.nr() == 1) then
|
||||
- return true
|
||||
- else
|
||||
- returns false
|
||||
!*/
|
||||
|
||||
bool is_col_vector (
|
||||
const matrix_exp& m
|
||||
);
|
||||
/*!
|
||||
ensures
|
||||
- if (m.nc() == 1) then
|
||||
- return true
|
||||
- else
|
||||
- returns false
|
||||
!*/
|
||||
|
||||
bool is_vector (
|
||||
const matrix_exp& m
|
||||
);
|
||||
/*!
|
||||
ensures
|
||||
- if (is_row_vector(m) || is_col_vector(m)) then
|
||||
- return true
|
||||
- else
|
||||
- returns false
|
||||
!*/
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
// ----------------------------------------------------------------------------------------
|
||||
// Statistics
|
||||
@ -747,7 +781,7 @@ namespace dlib
|
||||
);
|
||||
/*!
|
||||
requires
|
||||
- m.nr() == 1 || m.nc() == 1 (i.e. m must be a row or column vector)
|
||||
- is_vector(m) == true
|
||||
- m.size() > 0
|
||||
ensures
|
||||
- returns the index of the largest element in m.
|
||||
@ -761,7 +795,7 @@ namespace dlib
|
||||
);
|
||||
/*!
|
||||
requires
|
||||
- m.nr() == 1 || m.nc() == 1 (i.e. m must be a row or column vector)
|
||||
- is_vector(m) == true
|
||||
- m.size() > 0
|
||||
ensures
|
||||
- returns the index of the smallest element in m.
|
||||
@ -818,12 +852,12 @@ namespace dlib
|
||||
/*!
|
||||
requires
|
||||
- matrix_exp::type == a dlib::matrix object
|
||||
- m.nr() > 1
|
||||
- m.nc() == 1 (i.e. m is a column vector)
|
||||
- is_col_vector(m) == true
|
||||
- m.size() > 1
|
||||
- for all valid i, j:
|
||||
- m(i).nr() > 0
|
||||
- m(i).nc() == 1
|
||||
- m(i).nr() == m(j).nr()
|
||||
- is_col_vector(m(i)) == true
|
||||
- m(i).size() > 0
|
||||
- m(i).size() == m(j).size()
|
||||
- i.e. m contains only column vectors and all the column vectors
|
||||
have the same non-zero length
|
||||
ensures
|
||||
|
@ -808,9 +808,19 @@ namespace dlib
|
||||
U& u
|
||||
)
|
||||
{
|
||||
// make sure requires clause is not broken
|
||||
DLIB_ASSERT(is_vector(t) && is_vector(u) && u.size() == t.size(),
|
||||
"\t randomize_samples(t,u)"
|
||||
<< "\n\t invalid inputs were given to this function"
|
||||
<< "\n\t t.size(): " << t.size()
|
||||
<< "\n\t u.size(): " << u.size()
|
||||
<< "\n\t is_vector(t): " << (is_vector(t)? "true" : "false")
|
||||
<< "\n\t is_vector(u): " << (is_vector(u)? "true" : "false")
|
||||
);
|
||||
|
||||
rand::kernel_1a r;
|
||||
|
||||
long n = t.nr()-1;
|
||||
long n = t.size()-1;
|
||||
while (n > 0)
|
||||
{
|
||||
// put a random integer into idx
|
||||
@ -838,6 +848,14 @@ namespace dlib
|
||||
U& u
|
||||
)
|
||||
{
|
||||
// make sure requires clause is not broken
|
||||
DLIB_ASSERT(u.size() == t.size(),
|
||||
"\t randomize_samples(t,u)"
|
||||
<< "\n\t invalid inputs were given to this function"
|
||||
<< "\n\t t.size(): " << t.size()
|
||||
<< "\n\t u.size(): " << u.size()
|
||||
);
|
||||
|
||||
rand::kernel_1a r;
|
||||
|
||||
long n = t.size()-1;
|
||||
@ -866,9 +884,16 @@ namespace dlib
|
||||
T& t
|
||||
)
|
||||
{
|
||||
// make sure requires clause is not broken
|
||||
DLIB_ASSERT(is_vector(t),
|
||||
"\t randomize_samples(t)"
|
||||
<< "\n\t invalid inputs were given to this function"
|
||||
<< "\n\t is_vector(t): " << (is_vector(t)? "true" : "false")
|
||||
);
|
||||
|
||||
rand::kernel_1a r;
|
||||
|
||||
long n = t.nr()-1;
|
||||
long n = t.size()-1;
|
||||
while (n > 0)
|
||||
{
|
||||
// put a random integer into idx
|
||||
|
@ -376,11 +376,13 @@ namespace dlib
|
||||
);
|
||||
/*!
|
||||
requires
|
||||
- T == a matrix object that contains a swappable type
|
||||
- U == a matrix object that contains a swappable type
|
||||
- samples.nc() == 1
|
||||
- labels.nc() == 1
|
||||
- samples.nr() == labels.nr()
|
||||
- T == a matrix object or an object compatible with std::vector that contains
|
||||
a swappable type.
|
||||
- U == a matrix object or an object compatible with std::vector that contains
|
||||
a swappable type.
|
||||
- if samples or labels are matrix objects then is_vector(samples) == true and
|
||||
is_vector(labels) == true
|
||||
- samples.size() == labels.size()
|
||||
ensures
|
||||
- randomizes the order of the samples and labels but preserves
|
||||
the pairing between each sample and its label
|
||||
@ -399,46 +401,9 @@ namespace dlib
|
||||
);
|
||||
/*!
|
||||
requires
|
||||
- T == a matrix object that contains a swappable type
|
||||
- samples.nc() == 1
|
||||
ensures
|
||||
- randomizes the order of the elements inside samples
|
||||
!*/
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
template <
|
||||
typename T,
|
||||
typename U
|
||||
>
|
||||
void randomize_samples (
|
||||
T& samples,
|
||||
U& labels
|
||||
);
|
||||
/*!
|
||||
requires
|
||||
- T == an object compatible with std::vector that contains a swappable type
|
||||
- U == an object compatible with std::vector that contains a swappable type
|
||||
- samples.size() == labels.size()
|
||||
ensures
|
||||
- randomizes the order of the samples and labels but preserves
|
||||
the pairing between each sample and its label
|
||||
- for all valid i:
|
||||
- let r == the random index samples[i] was moved to. then:
|
||||
- #labels[r] == labels[i]
|
||||
!*/
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
template <
|
||||
typename T
|
||||
>
|
||||
void randomize_samples (
|
||||
T& samples
|
||||
);
|
||||
/*!
|
||||
requires
|
||||
- T == an object compatible with std::vector that contains a swappable type
|
||||
- T == a matrix object or an object compatible with std::vector that contains
|
||||
a swappable type.
|
||||
- if samples is a matrix then is_vector(samples) == true
|
||||
ensures
|
||||
- randomizes the order of the elements inside samples
|
||||
!*/
|
||||
|
@ -236,7 +236,7 @@ namespace
|
||||
q.enqueue(a);
|
||||
}
|
||||
|
||||
while (q.move_next());
|
||||
while (q.move_next()) ;
|
||||
|
||||
DLIB_TEST(q.at_start() == false);
|
||||
|
||||
|
@ -234,7 +234,7 @@ namespace dlib
|
||||
) { return tuple_helpers::get_helper<idx,tuple>::get(*this); }
|
||||
|
||||
template < class Q>
|
||||
const long index (
|
||||
long index (
|
||||
) const { return tuple_helpers::get_index<Q>(*this); }
|
||||
|
||||
template <class Q>
|
||||
|
Loading…
Reference in New Issue
Block a user