Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
D
dlib
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
钟尚武
dlib
Commits
4138b15a
Commit
4138b15a
authored
Nov 04, 2012
by
Davis King
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added sparse_matrix_vector_multiply()
parent
7f5a22c4
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
136 additions
and
3 deletions
+136
-3
sparse_vector.h
dlib/svm/sparse_vector.h
+70
-3
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