Commit 485ac90e authored by Davis King's avatar Davis King

Added an overload of set_subm() that can operate on ranges.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402700
parent c3496c64
......@@ -78,7 +78,7 @@ namespace dlib
{
for (long c = 0; c < src.nc(); ++c)
{
dest(r+row_offset,c+col_offset) = make_exp(lhs*rhs)(r,c);
dest(r+row_offset,c+col_offset) = src(r,c);
}
}
}
......
......@@ -2156,8 +2156,8 @@ convergence:
private:
const M m;
EXPr rows;
EXPc cols;
const EXPr rows;
const EXPc cols;
};
template <
......@@ -2388,6 +2388,99 @@ convergence:
// ----------------------------------------------------------------------------------------
template <typename T, long NR, long NC, typename mm, typename EXPr, typename EXPc>
class assignable_sub_range_matrix
{
public:
assignable_sub_range_matrix(
matrix<T,NR,NC,mm>& m_,
const EXPr& rows_,
const EXPc& cols_
) : m(m_), rows(rows_), cols(cols_) {}
template <typename EXP>
assignable_sub_range_matrix& operator= (
const matrix_exp<EXP>& exp
)
{
DLIB_ASSERT( exp.nr() == rows.size() && exp.nc() == cols.size(),
"\tassignable_matrix_expression set_subm(matrix& m, const matrix_exp rows, const matrix_exp cols)"
<< "\n\tYou have tried to assign to this object using a matrix that isn't the right size"
<< "\n\texp.nr() (source matrix): " << exp.nr()
<< "\n\texp.nc() (source matrix): " << exp.nc()
<< "\n\trows.size() (target matrix): " << rows.size()
<< "\n\tcols.size() (target matrix): " << cols.size()
);
if (exp.destructively_aliases(m) == false)
{
for (long r = 0; r < rows.size(); ++r)
{
for (long c = 0; c < cols.size(); ++c)
{
m(rows(r),cols(c)) = exp(r,c);
}
}
}
else
{
// make a temporary copy of the matrix we are going to assign to m to
// avoid aliasing issues during the copy
this->operator=(tmp(exp));
}
return *this;
}
assignable_sub_range_matrix& operator= (
const T& value
)
{
for (long r = 0; r < rows.size(); ++r)
{
for (long c = 0; c < cols.size(); ++c)
{
m(rows(r),cols(c)) = value;
}
}
return *this;
}
private:
matrix<T,NR,NC,mm>& m;
const EXPr rows;
const EXPc cols;
};
template <typename T, long NR, long NC, typename mm, typename EXPr, typename EXPc>
assignable_sub_range_matrix<T,NR,NC,mm,matrix_exp<EXPr>,matrix_exp<EXPc> > set_subm (
matrix<T,NR,NC,mm>& m,
const matrix_exp<EXPr>& rows,
const matrix_exp<EXPc>& cols
)
{
DLIB_ASSERT(0 <= min(rows) && max(rows) < m.nr() && 0 <= min(cols) && max(cols) < m.nc() &&
(rows.nr() == 1 || rows.nc() == 1) && (cols.nr() == 1 || cols.nc() == 1),
"\tassignable_matrix_expression set_subm(matrix& m, const matrix_exp& rows, const matrix_exp& cols)"
<< "\n\tYou have specified invalid sub matrix dimensions"
<< "\n\tm.nr(): " << m.nr()
<< "\n\tm.nc(): " << m.nc()
<< "\n\tmin(rows): " << min(rows)
<< "\n\tmax(rows): " << max(rows)
<< "\n\tmin(cols): " << min(cols)
<< "\n\tmax(cols): " << max(cols)
<< "\n\trows.nr(): " << rows.nr()
<< "\n\trows.nc(): " << rows.nc()
<< "\n\tcols.nr(): " << cols.nr()
<< "\n\tcols.nc(): " << cols.nc()
);
return assignable_sub_range_matrix<T,NR,NC,mm,matrix_exp<EXPr>,matrix_exp<EXPc> >(m,rows,cols);
}
// ----------------------------------------------------------------------------------------
template <typename T, long NR, long NC, typename mm>
class assignable_col_matrix
......
......@@ -402,6 +402,33 @@ namespace dlib
- subm(m,rect) == uniform_matrix<matrix::type>(nr,nc,scalar_value).
!*/
// ----------------------------------------------------------------------------------------
assignable_matrix_expression set_subm (
matrix& m,
const matrix_exp& rows,
const matrix_exp& cols
);
/*!
requires
- rows and cols contain elements of type long
- 0 <= min(rows) && max(rows) < m.nr()
- 0 <= min(cols) && max(cols) < m.nc()
- rows.nr() == 1 || rows.nc() == 1
- cols.nr() == 1 || cols.nc() == 1
(i.e. rows and cols must be vectors)
ensures
- statements of the following form:
- set_subm(m,rows,cols) = some_matrix;
result in it being the case that:
- subm(m,rows,cols) == some_matrix.
- statements of the following form:
- set_subm(m,rows,cols) = scalar_value;
result in it being the case that:
- subm(m,rows,cols) == uniform_matrix<matrix::type>(nr,nc,scalar_value).
!*/
// ----------------------------------------------------------------------------------------
assignable_matrix_expression set_rowm (
......
......@@ -1333,6 +1333,52 @@ namespace
DLIB_CASSERT((equal(round_zeros(cos(exp(array_to_matrix(m)))*mi,0.000001) , identity_matrix<double,5>())),"");
}
{
matrix<long,5,5> m1, res;
matrix<long,2,2> m2;
set_all_elements(m1,0);
long res_vals[] = {
9, 9, 9, 9, 9,
0, 1, 1, 0, 0,
0, 1, 1, 0, 2,
0, 0, 2, 2, 2,
0, 0, 2, 2, 0
};
res = res_vals;
set_all_elements(m2, 1);
set_subm(m1, range(1,2), range(1,2)) = subm(m2,0,0,2,2);
set_all_elements(m2, 2);
set_subm(m1, 3,2,2,2) = m2;
set_colm(m1,4) = trans(rowm(m1,4));
set_rowm(m1,0) = 9;
DLIB_CASSERT(m1 == res, "m1: \n" << m1 << "\nres: \n" << res);
set_subm(m1,0,0,5,5) = m1*m1;
DLIB_CASSERT(m1 == res*res, "m1: \n" << m1 << "\nres*res: \n" << res*res);
m1 = res;
set_subm(m1,1,1,2,2) = subm(m1,0,0,2,2);
long res_vals2[] = {
9, 9, 9, 9, 9,
0, 9, 9, 0, 0,
0, 0, 1, 0, 2,
0, 0, 2, 2, 2,
0, 0, 2, 2, 0
};
res = res_vals2;
DLIB_CASSERT(m1 == res, "m1: \n" << m1 << "\nres: \n" << res);
}
{
matrix<long,5,5> m1, res;
......@@ -1752,6 +1798,28 @@ namespace
}
{
matrix<double,4,5> m;
set_subm(m, range(0,3), range(0,4)) = 4;
DLIB_CASSERT(min(m) == max(m) && min(m) == 4,"");
set_subm(m,range(1,1),range(0,4)) = 7;
DLIB_CASSERT((rowm(m,0) == uniform_matrix<double>(1,5, 4)),"");
DLIB_CASSERT((rowm(m,1) == uniform_matrix<double>(1,5, 7)),"");
DLIB_CASSERT((rowm(m,2) == uniform_matrix<double>(1,5, 4)),"");
DLIB_CASSERT((rowm(m,3) == uniform_matrix<double>(1,5, 4)),"");
set_subm(m, range(0,2,3), range(0,2,4)) = trans(subm(m,0,0,3,2));
DLIB_CASSERT(m(0,2) == 7,"");
DLIB_CASSERT(m(2,2) == 7,"");
DLIB_CASSERT(sum(m) == 7*5+ 7+7 + 4*(4*5 - 7),"");
}
}
......
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