Commit 28a8f3ad authored by Davis King's avatar Davis King

Added the reshape() and pointer_to_matrix() functions.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%403583
parent a8f9f2ea
......@@ -784,7 +784,7 @@ namespace dlib
const_ret_type operator() (
long r,
long c
) const { return OP::apply(s,r,c); }
) const { return OP::apply(s,r,c, nr_, nc_); }
const_ret_type operator() ( long i ) const
{ return matrix_exp<dynamic_matrix_scalar_unary_exp>::operator()(i); }
......
......@@ -349,7 +349,7 @@ namespace dlib
typedef typename memory_manager<char>::kernel_1a mem_manager_type;
typedef T type;
typedef const T& const_ret_type;
static const_ret_type apply (const T* val, long r, long )
static const_ret_type apply (const T* val, long r, long, long, long )
{ return val[r]; }
};
......@@ -370,6 +370,42 @@ namespace dlib
return exp(nr,1,ptr);
}
// ----------------------------------------------------------------------------------------
template <
typename T
>
struct op_pointer_to_mat : has_nondestructive_aliasing
{
const static long cost = 1;
const static long NR = 0;
const static long NC = 0;
typedef typename memory_manager<char>::kernel_1a mem_manager_type;
typedef T type;
typedef const T& const_ret_type;
static const_ret_type apply (const T* val, long r, long c, long , long nc )
{ return val[r*nc + c]; }
};
template <
typename T
>
const dynamic_matrix_scalar_unary_exp<const T*,op_pointer_to_mat<T> > pointer_to_matrix (
const T* ptr,
long nr,
long nc
)
{
DLIB_ASSERT(nr > 0 && nc > 0 ,
"\tconst matrix_exp pointer_to_matrix(ptr, nr, nc)"
<< "\n\t nr and nc must be bigger than 0"
<< "\n\t nr: " << nr
<< "\n\t nc: " << nc
);
typedef dynamic_matrix_scalar_unary_exp<const T*,op_pointer_to_mat<T> > exp;
return exp(nr,nc,ptr);
}
// ----------------------------------------------------------------------------------------
struct op_trans
......@@ -1545,7 +1581,7 @@ namespace dlib
typedef typename memory_manager<char>::kernel_1a mem_manager_type;
typedef T type;
typedef const T& const_ret_type;
static const_ret_type apply (const T& val, long , long )
static const_ret_type apply (const T& val, long , long, long, long )
{ return val; }
};
......@@ -1689,7 +1725,7 @@ namespace dlib
typedef typename memory_manager<char>::kernel_1a mem_manager_type;
typedef T type;
typedef const T const_ret_type;
static const_ret_type apply (const T&, long r, long c)
static const_ret_type apply (const T&, long r, long c, long, long)
{ return static_cast<type>(r == c); }
};
......@@ -2191,6 +2227,56 @@ namespace dlib
return exp(m.ref(),lower, upper);
}
// ----------------------------------------------------------------------------------------
struct op_reshape
{
template <typename EXP>
struct op : public has_destructive_aliasing
{
const static long cost = EXP::cost+1;
const static long NR = 0;
const static long NC = 0;
typedef typename EXP::type type;
typedef typename EXP::const_ret_type const_ret_type;
typedef typename EXP::mem_manager_type mem_manager_type;
template <typename M>
static const_ret_type apply ( const M& m, const long& , const long& cols, long r, long c)
{
const long idx = r*cols + c;
return m(idx/m.nc(), idx%m.nc());
}
template <typename M1 >
static long nr (const M1& , const long& rows, const long& ) { return rows; }
template <typename M1>
static long nc (const M1& , const long&, const long& cols ) { return cols; }
};
};
template <
typename EXP
>
const matrix_scalar_ternary_exp<EXP, long, op_reshape> reshape (
const matrix_exp<EXP>& m,
const long& rows,
const long& cols
)
{
DLIB_ASSERT(m.size() == rows*cols && rows > 0 && cols > 0,
"\tconst matrix_exp reshape(m, rows, cols)"
<< "\n\t The size of m must match the dimensions you want to reshape it into."
<< "\n\t m.size(): " << m.size()
<< "\n\t rows*cols: " << rows*cols
<< "\n\t rows: " << rows
<< "\n\t cols: " << cols
);
typedef matrix_scalar_ternary_exp<EXP, long, op_reshape> exp;
return exp(m.ref(), rows, cols);
}
// ----------------------------------------------------------------------------------------
template <
......
......@@ -364,6 +364,33 @@ namespace dlib
R(r, c) == array[r][c]
!*/
// ----------------------------------------------------------------------------------------
template <
typename T
>
const matrix_exp pointer_to_matrix (
const T* ptr,
long nr,
long nc
);
/*!
requires
- nr > 0
- nc > 0
- ptr == a pointer to at least nr*nc T objects
ensures
- returns a matrix M such that:
- M.nr() == nr
- m.nc() == nc
- for all valid r and c:
M(r,c) == ptr[r*nc + c]
(i.e. the pointer is interpreted as a matrix laid out in memory
in row major order)
- Note that the returned matrix doesn't take "ownership" of
the pointer and thus will not delete or free it.
!*/
// ----------------------------------------------------------------------------------------
template <
......@@ -387,6 +414,32 @@ namespace dlib
the pointer and thus will not delete or free it.
!*/
// ----------------------------------------------------------------------------------------
const matrix_exp reshape (
const matrix_exp& m,
long rows,
long cols
);
/*!
requires
- m.size() == rows*cols
- rows > 0
- cols > 0
ensures
- returns a matrix M such that:
- M.nr() == rows
- M.nc() == cols
- M.size() == m.size()
- for all valid r and c:
- let IDX = r*cols + c
- M(r,c) == m(IDX/m.nc(), IDX%m.nc())
- i.e. The matrix m is reshaped into a new matrix of rows by cols
dimension. Additionally, the elements of m are laid into M in row major
order.
!*/
// ----------------------------------------------------------------------------------------
const matrix_exp reshape_to_column_vector (
......@@ -400,7 +453,7 @@ namespace dlib
- for all valid r and c:
- m(r,c) == M(r*m.nc() + c)
- i.e. the matrix m is reshaped into a column vector. Note that
- i.e. The matrix m is reshaped into a column vector. Note that
the elements are pulled out in row major order.
!*/
......
......@@ -882,6 +882,62 @@ namespace
DLIB_TEST(join_rows(trans(a2),trans(a)) == trans(c))
}
{
matrix<int> a, b;
a.set_size(2,3);
a = 1, 2, 3,
4, 5, 6;
b.set_size(3,2);
b = 1, 2,
3, 4,
5, 6;
DLIB_TEST(reshape(a, 3, 2) == b);
b.set_size(2,3);
b = 1, 4, 2,
5, 3, 6;
DLIB_TEST(reshape(trans(a), 2, 3) == b);
}
{
matrix<int,2,3> a;
matrix<int> b;
a = 1, 2, 3,
4, 5, 6;
b.set_size(3,2);
b = 1, 2,
3, 4,
5, 6;
DLIB_TEST(reshape(a, 3, 2) == b);
b.set_size(2,3);
b = 1, 4, 2,
5, 3, 6;
DLIB_TEST(reshape(trans(a), 2, 3) == b);
}
{
std::vector<int> v(6);
for (unsigned long i = 0; i < v.size(); ++i)
v[i] = i;
matrix<int,2,3> a;
a = 0, 1, 2,
3, 4, 5;
DLIB_TEST(pointer_to_matrix(&v[0], 2, 3) == a);
}
}
......
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