Commit a0306d4a authored by Davis King's avatar Davis King

Added flipud() and fliplr() matrix functions. I also renamed

the scale_rows_columns.cpp test file to matrix4.cpp.

--HG--
rename : dlib/test/scale_rows_columns.cpp => dlib/test/matrix4.cpp
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%403896
parent bdd4bcbf
...@@ -3321,6 +3321,82 @@ namespace dlib ...@@ -3321,6 +3321,82 @@ namespace dlib
return matrix_op<op>(op(a.ref(),b.ref())); return matrix_op<op>(op(a.ref(),b.ref()));
} }
// ----------------------------------------------------------------------------------------
template <typename M>
struct op_fliplr
{
op_fliplr( const M& m_) : m(m_){}
const M& m;
const static long cost = M::cost;
const static long NR = M::NR;
const static long NC = M::NC;
typedef typename M::type type;
typedef typename M::const_ret_type const_ret_type;
typedef typename M::mem_manager_type mem_manager_type;
typedef typename M::layout_type layout_type;
const_ret_type apply (long r, long c) const { return m(r,m.nc()-c-1); }
long nr () const { return m.nr(); }
long nc () const { return m.nc(); }
template <typename U> bool aliases ( const matrix_exp<U>& item) const { return m.aliases(item); }
template <typename U> bool destructively_aliases ( const matrix_exp<U>& item) const { return m.aliases(item); }
};
template <
typename M
>
const matrix_op<op_fliplr<M> > fliplr (
const matrix_exp<M>& m
)
{
typedef op_fliplr<M> op;
return matrix_op<op>(op(m.ref()));
}
// ----------------------------------------------------------------------------------------
template <typename M>
struct op_flipud
{
op_flipud( const M& m_) : m(m_){}
const M& m;
const static long cost = M::cost;
const static long NR = M::NR;
const static long NC = M::NC;
typedef typename M::type type;
typedef typename M::const_ret_type const_ret_type;
typedef typename M::mem_manager_type mem_manager_type;
typedef typename M::layout_type layout_type;
const_ret_type apply (long r, long c) const { return m(m.nr()-r-1,c); }
long nr () const { return m.nr(); }
long nc () const { return m.nc(); }
template <typename U> bool aliases ( const matrix_exp<U>& item) const { return m.aliases(item); }
template <typename U> bool destructively_aliases ( const matrix_exp<U>& item) const { return m.aliases(item); }
};
template <
typename M
>
const matrix_op<op_flipud<M> > flipud (
const matrix_exp<M>& m
)
{
typedef op_flipud<M> op;
return matrix_op<op>(op(m.ref()));
}
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
} }
......
...@@ -338,6 +338,38 @@ namespace dlib ...@@ -338,6 +338,38 @@ namespace dlib
R( (r+R)%m.nr() , (c+C)%m.nc() ) == m(r,c) R( (r+R)%m.nr() , (c+C)%m.nc() ) == m(r,c)
!*/ !*/
// ----------------------------------------------------------------------------------------
const matrix_exp fliplr (
const matrix_exp& m
);
/*!
ensures
- flips the matrix m from left to right and returns the result.
I.e. reverses the order of the columns.
- returns a matrix M such that:
- M::type == the same type that was in m
- M has the same dimensions as m
- for all valid r and c:
M(r,c) == m(r, m.nc()-c-1)
!*/
// ----------------------------------------------------------------------------------------
const matrix_exp flipud (
const matrix_exp& m
);
/*!
ensures
- flips the matrix m from up to down and returns the result.
I.e. reverses the order of the rows.
- returns a matrix M such that:
- M::type == the same type that was in m
- M has the same dimensions as m
- for all valid r and c:
M(r,c) == m(m.nr()-r-1, c)
!*/
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
template < template <
......
...@@ -50,6 +50,7 @@ set (tests ...@@ -50,6 +50,7 @@ set (tests
map.cpp map.cpp
matrix2.cpp matrix2.cpp
matrix3.cpp matrix3.cpp
matrix4.cpp
matrix_chol.cpp matrix_chol.cpp
matrix.cpp matrix.cpp
matrix_eig.cpp matrix_eig.cpp
...@@ -67,7 +68,6 @@ set (tests ...@@ -67,7 +68,6 @@ set (tests
rand.cpp rand.cpp
read_write_mutex.cpp read_write_mutex.cpp
reference_counter.cpp reference_counter.cpp
scale_rows_columns.cpp
sequence.cpp sequence.cpp
serialize.cpp serialize.cpp
set.cpp set.cpp
......
...@@ -60,6 +60,7 @@ SRC += lz77_buffer.cpp ...@@ -60,6 +60,7 @@ SRC += lz77_buffer.cpp
SRC += map.cpp SRC += map.cpp
SRC += matrix2.cpp SRC += matrix2.cpp
SRC += matrix3.cpp SRC += matrix3.cpp
SRC += matrix4.cpp
SRC += matrix_chol.cpp SRC += matrix_chol.cpp
SRC += matrix.cpp SRC += matrix.cpp
SRC += matrix_eig.cpp SRC += matrix_eig.cpp
...@@ -77,7 +78,6 @@ SRC += queue.cpp ...@@ -77,7 +78,6 @@ SRC += queue.cpp
SRC += rand.cpp SRC += rand.cpp
SRC += read_write_mutex.cpp SRC += read_write_mutex.cpp
SRC += reference_counter.cpp SRC += reference_counter.cpp
SRC += scale_rows_columns.cpp
SRC += sequence.cpp SRC += sequence.cpp
SRC += serialize.cpp SRC += serialize.cpp
SRC += set.cpp SRC += set.cpp
......
...@@ -23,7 +23,7 @@ namespace ...@@ -23,7 +23,7 @@ namespace
using namespace dlib; using namespace dlib;
using namespace std; using namespace std;
logger dlog("test.scale_rows_columns"); logger dlog("test.matrix4");
void matrix_test ( void matrix_test (
) )
...@@ -160,23 +160,69 @@ namespace ...@@ -160,23 +160,69 @@ namespace
DLIB_TEST(equal( diagm(v2)*m , tmp(diagm(v2))*m )); DLIB_TEST(equal( diagm(v2)*m , tmp(diagm(v2))*m ));
DLIB_TEST(equal( scale_rows(m,v2) , tmp(diagm(v2))*m )); DLIB_TEST(equal( scale_rows(m,v2) , tmp(diagm(v2))*m ));
} }
}
void test_stuff()
{
print_spinner();
{
matrix<double> m(3,3), lr(3,3), ud(3,3);
m = 1,2,3,
4,5,6,
7,8,9;
lr = 3,2,1,
6,5,4,
9,8,7;
ud = 7,8,9,
4,5,6,
1,2,3;
DLIB_TEST(lr == fliplr(m));
DLIB_TEST(ud == flipud(m));
}
{
matrix<double> m(3,2), lr(3,2), ud(3,2);
m = 1,2,
3,4,
5,6;
lr = 2,1,
4,3,
6,5;
ud = 5,6,
3,4,
1,2;
DLIB_TEST(lr == fliplr(m));
DLIB_TEST(ud == flipud(m));
}
} }
class matrix_tester : public tester class matrix_tester : public tester
{ {
public: public:
matrix_tester ( matrix_tester (
) : ) :
tester ("test_scale_rows_columns", tester ("test_matrix4",
"Runs tests on the scale_rows and scale_columns functions.") "Runs tests on the scale_rows and scale_columns functions.")
{} {}
void perform_test ( void perform_test (
) )
{ {
test_stuff();
for (int i = 0; i < 10; ++i) for (int i = 0; i < 10; ++i)
matrix_test(); matrix_test();
} }
......
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