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
6b2b213d
Commit
6b2b213d
authored
Nov 24, 2012
by
Davis King
Browse files
Added an overload of sparse_matrix_vector_multiply() that multiplies a dense
matrix with a sparse vector.
parent
aee36c3d
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
95 additions
and
2 deletions
+95
-2
dlib/svm/sparse_vector.h
dlib/svm/sparse_vector.h
+52
-2
dlib/svm/sparse_vector_abstract.h
dlib/svm/sparse_vector_abstract.h
+43
-0
No files found.
dlib/svm/sparse_vector.h
View file @
6b2b213d
...
@@ -1003,10 +1003,60 @@ namespace dlib
...
@@ -1003,10 +1003,60 @@ namespace dlib
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
}
template
<
typename
EXP
,
typename
sparse_vector_type
,
typename
T
,
long
NR
,
long
NC
,
typename
MM
,
typename
L
>
void
sparse_matrix_vector_multiply
(
const
matrix_exp
<
EXP
>&
m
,
const
sparse_vector_type
&
v
,
matrix
<
T
,
NR
,
NC
,
MM
,
L
>&
result
)
{
// make sure requires clause is not broken
DLIB_ASSERT
(
max_index_plus_one
(
v
)
<=
(
unsigned
long
)
m
.
nc
(),
"
\t
void sparse_matrix_vector_multiply()"
<<
"
\n\t
Invalid inputs were given to this function"
<<
"
\n\t
max_index_plus_one(v): "
<<
max_index_plus_one
(
v
)
<<
"
\n\t
m.size(): "
<<
m
.
size
()
);
#endif // DLIB_SVm_SPARSE_VECTOR
result
.
set_size
(
m
.
nr
(),
1
);
result
=
0
;
for
(
typename
sparse_vector_type
::
const_iterator
i
=
v
.
begin
();
i
!=
v
.
end
();
++
i
)
{
for
(
long
r
=
0
;
r
<
result
.
nr
();
++
r
)
{
result
(
r
)
+=
m
(
r
,
i
->
first
)
*
i
->
second
;
}
}
}
// ----------------------------------------------------------------------------------------
template
<
typename
EXP
,
typename
sparse_vector_type
>
matrix
<
typename
EXP
::
type
,
0
,
1
>
sparse_matrix_vector_multiply
(
const
matrix_exp
<
EXP
>&
m
,
const
sparse_vector_type
&
v
)
{
matrix
<
typename
EXP
::
type
,
0
,
1
>
result
;
sparse_matrix_vector_multiply
(
m
,
v
,
result
);
return
result
;
}
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_SVm_SPARSE_VECTOR
dlib/svm/sparse_vector_abstract.h
View file @
6b2b213d
...
@@ -618,6 +618,49 @@ namespace dlib
...
@@ -618,6 +618,49 @@ namespace dlib
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
template
<
typename
EXP
,
typename
sparse_vector_type
,
typename
T
,
long
NR
,
long
NC
,
typename
MM
,
typename
L
>
void
sparse_matrix_vector_multiply
(
const
matrix_exp
<
EXP
>&
m
,
const
sparse_vector_type
&
v
,
matrix
<
T
,
NR
,
NC
,
MM
,
L
>&
result
);
/*!
requires
- max_index_plus_one(v) < m.nc()
- v == an unsorted sparse vector
ensures
- #result == m*v
(i.e. multiply m by the vector v and store the output in result)
!*/
// ----------------------------------------------------------------------------------------
template
<
typename
EXP
,
typename
sparse_vector_type
>
matrix
<
typename
EXP
::
type
,
0
,
1
>
sparse_matrix_vector_multiply
(
const
matrix_exp
<
EXP
>&
m
,
const
sparse_vector_type
&
v
);
/*!
requires
- max_index_plus_one(v) < m.nc()
- v == an unsorted sparse vector
ensures
- returns m*v
(i.e. multiply m by the vector v and return the resulting vector)
!*/
// ----------------------------------------------------------------------------------------
}
}
#endif // DLIB_SVm_SPARSE_VECTOR_ABSTRACT_
#endif // DLIB_SVm_SPARSE_VECTOR_ABSTRACT_
...
...
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