Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
dlib
Commits
e3307db4
Commit
e3307db4
authored
Mar 10, 2012
by
Davis King
Browse files
Added some more convenient overloads of the zeros_matrix(), ones_matrix(),
and identity_matrix() functions.
parent
17565cfb
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
102 additions
and
0 deletions
+102
-0
dlib/matrix/matrix_utilities.h
dlib/matrix/matrix_utilities.h
+58
-0
dlib/matrix/matrix_utilities_abstract.h
dlib/matrix/matrix_utilities_abstract.h
+39
-0
dlib/test/matrix.cpp
dlib/test/matrix.cpp
+5
-0
No files found.
dlib/matrix/matrix_utilities.h
View file @
e3307db4
...
@@ -1882,6 +1882,26 @@ namespace dlib
...
@@ -1882,6 +1882,26 @@ namespace dlib
return
matrix_op
<
op
>
(
op
(
nr
,
nc
,
0
));
return
matrix_op
<
op
>
(
op
(
nr
,
nc
,
0
));
}
}
// ----------------------------------------------------------------------------------------
template
<
typename
EXP
>
const
matrix_op
<
op_uniform_matrix_3
<
typename
EXP
::
type
>
>
zeros_matrix
(
const
matrix_exp
<
EXP
>&
mat
)
{
DLIB_ASSERT
(
mat
.
nr
()
>
0
&&
mat
.
nc
()
>
0
,
"
\t
const matrix_exp zeros_matrix(mat)"
<<
"
\n\t
nr and nc have to be bigger than 0"
<<
"
\n\t
mat.nr(): "
<<
mat
.
nr
()
<<
"
\n\t
mat.nc(): "
<<
mat
.
nc
()
);
typedef
typename
EXP
::
type
T
;
typedef
op_uniform_matrix_3
<
T
>
op
;
return
matrix_op
<
op
>
(
op
(
mat
.
nr
(),
mat
.
nc
(),
0
));
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
template
<
template
<
...
@@ -1902,6 +1922,26 @@ namespace dlib
...
@@ -1902,6 +1922,26 @@ namespace dlib
return
matrix_op
<
op
>
(
op
(
nr
,
nc
,
1
));
return
matrix_op
<
op
>
(
op
(
nr
,
nc
,
1
));
}
}
// ----------------------------------------------------------------------------------------
template
<
typename
EXP
>
const
matrix_op
<
op_uniform_matrix_3
<
typename
EXP
::
type
>
>
ones_matrix
(
const
matrix_exp
<
EXP
>&
mat
)
{
DLIB_ASSERT
(
mat
.
nr
()
>
0
&&
mat
.
nc
()
>
0
,
"
\t
const matrix_exp ones_matrix(mat)"
<<
"
\n\t
nr and nc have to be bigger than 0"
<<
"
\n\t
mat.nr(): "
<<
mat
.
nr
()
<<
"
\n\t
mat.nc(): "
<<
mat
.
nc
()
);
typedef
typename
EXP
::
type
T
;
typedef
op_uniform_matrix_3
<
T
>
op
;
return
matrix_op
<
op
>
(
op
(
mat
.
nr
(),
mat
.
nc
(),
1
));
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
template
<
template
<
...
@@ -2051,6 +2091,24 @@ namespace dlib
...
@@ -2051,6 +2091,24 @@ namespace dlib
return
matrix_diag_op
<
op
>
(
op
(
size
));
return
matrix_diag_op
<
op
>
(
op
(
size
));
}
}
template
<
typename
EXP
>
const
matrix_diag_op
<
op_identity_matrix_2
<
typename
EXP
::
type
>
>
identity_matrix
(
const
matrix_exp
<
EXP
>&
mat
)
{
DLIB_ASSERT
(
mat
.
nr
()
==
mat
.
nc
(),
"
\t
const matrix_exp identity_matrix(mat)"
<<
"
\n\t
mat must be a square matrix."
<<
"
\n\t
mat.nr(): "
<<
mat
.
nr
()
<<
"
\n\t
mat.nc(): "
<<
mat
.
nc
()
);
typedef
typename
EXP
::
type
T
;
typedef
op_identity_matrix_2
<
T
>
op
;
return
matrix_diag_op
<
op
>
(
op
(
mat
.
nr
()));
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
template
<
template
<
...
...
dlib/matrix/matrix_utilities_abstract.h
View file @
e3307db4
...
@@ -245,6 +245,19 @@ namespace dlib
...
@@ -245,6 +245,19 @@ namespace dlib
- returns an nr by nc matrix with elements of type T and all set to val.
- returns an nr by nc matrix with elements of type T and all set to val.
!*/
!*/
// ----------------------------------------------------------------------------------------
const
matrix_exp
ones_matrix
(
const
matrix_exp
&
mat
);
/*!
requires
- mat.nr() > 0 && mat.nc() > 0
ensures
- Let T denote the type of element in mat. Then this function
returns uniform_matrix<T>(mat.nr(), mat.nc(), 1)
!*/
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
template
<
template
<
...
@@ -261,6 +274,19 @@ namespace dlib
...
@@ -261,6 +274,19 @@ namespace dlib
- returns uniform_matrix<T>(nr, nc, 1)
- returns uniform_matrix<T>(nr, nc, 1)
!*/
!*/
// ----------------------------------------------------------------------------------------
const
matrix_exp
zeros_matrix
(
const
matrix_exp
&
mat
);
/*!
requires
- mat.nr() > 0 && mat.nc() > 0
ensures
- Let T denote the type of element in mat. Then this function
returns uniform_matrix<T>(mat.nr(), mat.nc(), 0)
!*/
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
template
<
template
<
...
@@ -277,6 +303,19 @@ namespace dlib
...
@@ -277,6 +303,19 @@ namespace dlib
- returns uniform_matrix<T>(nr, nc, 0)
- returns uniform_matrix<T>(nr, nc, 0)
!*/
!*/
// ----------------------------------------------------------------------------------------
const
matrix_exp
identity_matrix
(
const
matrix_exp
&
mat
);
/*!
requires
- mat.nr() == mat.nc()
ensures
- returns an identity matrix with the same dimensions as mat and
containing the same type of elements as mat.
!*/
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
template
<
template
<
...
...
dlib/test/matrix.cpp
View file @
e3307db4
...
@@ -65,6 +65,8 @@ namespace
...
@@ -65,6 +65,8 @@ namespace
DLIB_TEST
(
mi
.
nc
()
==
m
.
nr
());
DLIB_TEST
(
mi
.
nc
()
==
m
.
nr
());
DLIB_TEST
((
equal
(
round_zeros
(
mi
*
m
,
0.000001
)
,
identity_matrix
<
double
,
5
>
())));
DLIB_TEST
((
equal
(
round_zeros
(
mi
*
m
,
0.000001
)
,
identity_matrix
<
double
,
5
>
())));
DLIB_TEST
((
equal
(
round_zeros
(
m
*
mi
,
0.000001
)
,
identity_matrix
<
double
,
5
>
())));
DLIB_TEST
((
equal
(
round_zeros
(
m
*
mi
,
0.000001
)
,
identity_matrix
<
double
,
5
>
())));
DLIB_TEST
((
equal
(
round_zeros
(
mi
*
m
,
0.000001
)
,
identity_matrix
(
m
))));
DLIB_TEST
((
equal
(
round_zeros
(
m
*
mi
,
0.000001
)
,
identity_matrix
(
m
))));
}
}
{
{
matrix
<
double
,
5
,
0
,
MM
>
m
(
5
,
5
);
matrix
<
double
,
5
,
0
,
MM
>
m
(
5
,
5
);
...
@@ -1041,8 +1043,11 @@ namespace
...
@@ -1041,8 +1043,11 @@ namespace
}
}
{
{
matrix
<
double
>
mat
(
4
,
5
);
DLIB_TEST
((
uniform_matrix
<
double
>
(
4
,
5
,
1
)
==
ones_matrix
<
double
>
(
4
,
5
)));
DLIB_TEST
((
uniform_matrix
<
double
>
(
4
,
5
,
1
)
==
ones_matrix
<
double
>
(
4
,
5
)));
DLIB_TEST
((
uniform_matrix
<
double
>
(
4
,
5
,
1
)
==
ones_matrix
(
mat
)));
DLIB_TEST
((
uniform_matrix
<
double
>
(
4
,
5
,
0
)
==
zeros_matrix
<
double
>
(
4
,
5
)));
DLIB_TEST
((
uniform_matrix
<
double
>
(
4
,
5
,
0
)
==
zeros_matrix
<
double
>
(
4
,
5
)));
DLIB_TEST
((
uniform_matrix
<
double
>
(
4
,
5
,
0
)
==
zeros_matrix
(
mat
)));
DLIB_TEST
((
uniform_matrix
<
float
>
(
4
,
5
,
1
)
==
ones_matrix
<
float
>
(
4
,
5
)));
DLIB_TEST
((
uniform_matrix
<
float
>
(
4
,
5
,
1
)
==
ones_matrix
<
float
>
(
4
,
5
)));
DLIB_TEST
((
uniform_matrix
<
float
>
(
4
,
5
,
0
)
==
zeros_matrix
<
float
>
(
4
,
5
)));
DLIB_TEST
((
uniform_matrix
<
float
>
(
4
,
5
,
0
)
==
zeros_matrix
<
float
>
(
4
,
5
)));
DLIB_TEST
((
uniform_matrix
<
complex
<
double
>
>
(
4
,
5
,
1
)
==
ones_matrix
<
complex
<
double
>
>
(
4
,
5
)));
DLIB_TEST
((
uniform_matrix
<
complex
<
double
>
>
(
4
,
5
,
1
)
==
ones_matrix
<
complex
<
double
>
>
(
4
,
5
)));
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment