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
4138b15a
"torchvision/git@developer.sourcefind.cn:OpenDAS/vision.git" did not exist on "4b2ad55f1b11d70cf2b31a903fbb685fc9f79e6a"
Commit
4138b15a
authored
Nov 04, 2012
by
Davis King
Browse files
Added sparse_matrix_vector_multiply()
parent
7f5a22c4
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
136 additions
and
3 deletions
+136
-3
dlib/svm/sparse_vector.h
dlib/svm/sparse_vector.h
+70
-3
dlib/svm/sparse_vector_abstract.h
dlib/svm/sparse_vector_abstract.h
+66
-0
No files found.
dlib/svm/sparse_vector.h
View file @
4138b15a
...
...
@@ -9,8 +9,7 @@
#include "../algs.h"
#include <vector>
#include <map>
// This is included just so we can do some disable_if stuff on it in the max_index_plus_one routine().
#include "../manifold_regularization/sample_pair.h"
#include "../manifold_regularization/graph_creation.h"
#include "../matrix.h"
...
...
@@ -545,7 +544,8 @@ namespace dlib
template
<
typename
T
>
typename
disable_if_c
<
is_pair
<
typename
T
::
value_type
>::
value
||
is_same_type
<
typename
T
::
value_type
,
sample_pair
>::
value
,
unsigned
long
>::
type
is_same_type
<
typename
T
::
value_type
,
sample_pair
>::
value
||
is_same_type
<
typename
T
::
value_type
,
ordered_sample_pair
>::
value
,
unsigned
long
>::
type
max_index_plus_one
(
const
T
&
samples
)
...
...
@@ -908,6 +908,73 @@ namespace dlib
return
T
(
temp
.
begin
(),
temp
.
end
());
}
// ----------------------------------------------------------------------------------------
template
<
typename
EXP
,
typename
T
,
long
NR
,
long
NC
,
typename
MM
,
typename
L
>
void
sparse_matrix_vector_multiply
(
const
std
::
vector
<
sample_pair
>&
edges
,
const
matrix_exp
<
EXP
>&
v
,
matrix
<
T
,
NR
,
NC
,
MM
,
L
>&
result
)
{
// make sure requires clause is not broken
DLIB_ASSERT
(
max_index_plus_one
(
edges
)
<=
(
unsigned
long
)
v
.
size
()
&&
is_col_vector
(
v
),
"
\t
void sparse_matrix_vector_multiply()"
<<
"
\n\t
Invalid inputs were given to this function"
<<
"
\n\t
max_index_plus_one(edges): "
<<
max_index_plus_one
(
edges
)
<<
"
\n\t
v.size(): "
<<
v
.
size
()
<<
"
\n\t
is_col_vector(v): "
<<
is_col_vector
(
v
)
);
result
.
set_size
(
v
.
nr
(),
v
.
nc
());
result
=
0
;
for
(
unsigned
long
k
=
0
;
k
<
edges
.
size
();
++
k
)
{
const
long
i
=
edges
[
k
].
index1
();
const
long
j
=
edges
[
k
].
index2
();
const
double
d
=
edges
[
k
].
distance
();
result
(
i
)
+=
v
(
j
)
*
d
;
if
(
i
!=
j
)
result
(
j
)
+=
v
(
i
)
*
d
;
}
}
// ----------------------------------------------------------------------------------------
template
<
typename
EXP
,
typename
T
,
long
NR
,
long
NC
,
typename
MM
,
typename
L
>
void
sparse_matrix_vector_multiply
(
const
std
::
vector
<
ordered_sample_pair
>&
edges
,
const
matrix_exp
<
EXP
>&
v
,
matrix
<
T
,
NR
,
NC
,
MM
,
L
>&
result
)
{
// make sure requires clause is not broken
DLIB_ASSERT
(
max_index_plus_one
(
edges
)
<=
(
unsigned
long
)
v
.
size
()
&&
is_col_vector
(
v
),
"
\t
void sparse_matrix_vector_multiply()"
<<
"
\n\t
Invalid inputs were given to this function"
<<
"
\n\t
max_index_plus_one(edges): "
<<
max_index_plus_one
(
edges
)
<<
"
\n\t
v.size(): "
<<
v
.
size
()
<<
"
\n\t
is_col_vector(v): "
<<
is_col_vector
(
v
)
);
result
.
set_size
(
v
.
nr
(),
v
.
nc
());
result
=
0
;
for
(
unsigned
long
k
=
0
;
k
<
edges
.
size
();
++
k
)
{
const
long
i
=
edges
[
k
].
index1
();
const
long
j
=
edges
[
k
].
index2
();
const
double
d
=
edges
[
k
].
distance
();
result
(
i
)
+=
v
(
j
)
*
d
;
}
}
// ----------------------------------------------------------------------------------------
}
...
...
dlib/svm/sparse_vector_abstract.h
View file @
4138b15a
...
...
@@ -9,6 +9,8 @@
#include "../matrix.h"
#include <map>
#include <vector>
#include "../manifold_regularization/sample_pair_abstract.h"
#include "../manifold_regularization/ordered_sample_pair_abstract.h"
namespace
dlib
{
...
...
@@ -500,6 +502,70 @@ namespace dlib
key values).
!*/
// ----------------------------------------------------------------------------------------
template
<
typename
EXP
,
typename
T
,
long
NR
,
long
NC
,
typename
MM
,
typename
L
>
void
sparse_matrix_vector_multiply
(
const
std
::
vector
<
sample_pair
>&
edges
,
const
matrix_exp
<
EXP
>&
v
,
matrix
<
T
,
NR
,
NC
,
MM
,
L
>&
result
);
/*!
requires
- is_col_vector(v) == true
- max_index_plus_one(edges) <= v.size()
ensures
- Interprets edges as representing a symmetric sparse matrix M. The elements
of M are defined such that, for all valid i,j:
- M(i,j) == sum of edges[k].distance() for all k where edges[k]==sample_pair(i,j)
- This means that any element of M that doesn't have any edges associated
with it will have a value of 0.
- #result == M*v
(i.e. this function multiplies the vector v with the sparse matrix
represented by edges and stores the output into result)
- get_rect(#result) == get_rect(v)
(i.e. result will have the same dimensions as v)
!*/
// ----------------------------------------------------------------------------------------
template
<
typename
EXP
,
typename
T
,
long
NR
,
long
NC
,
typename
MM
,
typename
L
>
void
sparse_matrix_vector_multiply
(
const
std
::
vector
<
ordered_sample_pair
>&
edges
,
const
matrix_exp
<
EXP
>&
v
,
matrix
<
T
,
NR
,
NC
,
MM
,
L
>&
result
);
/*!
requires
- is_col_vector(v) == true
- max_index_plus_one(edges) <= v.size()
ensures
- Interprets edges as representing a square sparse matrix M. The elements of M
are defined such that, for all valid i,j:
- M(i,j) == sum of edges[k].distance() for all k where edges[k]==ordered_sample_pair(i,j)
- This means that any element of M that doesn't have any edges associated
with it will have a value of 0.
- #result == M*v
(i.e. this function multiplies the vector v with the sparse matrix
represented by edges and stores the output into result)
- get_rect(#result) == get_rect(v)
(i.e. result will have the same dimensions as v)
!*/
// ----------------------------------------------------------------------------------------
}
...
...
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