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
2f2aecc9
Commit
2f2aecc9
authored
May 01, 2012
by
Davis King
Browse files
Added overloads for dot() so you can dot() a std::vector with a std::map.
parent
440c6571
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
82 additions
and
24 deletions
+82
-24
dlib/svm/sparse_vector.h
dlib/svm/sparse_vector.h
+56
-24
dlib/svm/sparse_vector_abstract.h
dlib/svm/sparse_vector_abstract.h
+26
-0
No files found.
dlib/svm/sparse_vector.h
View file @
2f2aecc9
...
...
@@ -7,6 +7,8 @@
#include <cmath>
#include <limits>
#include "../algs.h"
#include <vector>
#include <map>
namespace
dlib
...
...
@@ -216,37 +218,67 @@ namespace dlib
// ------------------------------------------------------------------------------------
template
<
typename
T
>
typename
T
::
value_type
::
second_type
dot
(
const
T
&
a
,
const
T
&
b
)
namespace
impl
{
typedef
typename
T
::
value_type
::
second_type
scalar_type
;
template
<
typename
T
,
typename
U
>
typename
T
::
value_type
::
second_type
dot
(
const
T
&
a
,
const
U
&
b
)
{
typedef
typename
T
::
value_type
::
second_type
scalar_type
;
typename
T
::
const_iterator
ai
=
a
.
begin
();
typename
T
::
const_iterator
bi
=
b
.
begin
();
typename
T
::
const_iterator
ai
=
a
.
begin
();
typename
U
::
const_iterator
bi
=
b
.
begin
();
scalar_type
sum
=
0
;
while
(
ai
!=
a
.
end
()
&&
bi
!=
b
.
end
())
{
if
(
ai
->
first
==
bi
->
first
)
{
sum
+=
ai
->
second
*
bi
->
second
;
++
ai
;
++
bi
;
}
else
if
(
ai
->
first
<
bi
->
first
)
scalar_type
sum
=
0
;
while
(
ai
!=
a
.
end
()
&&
bi
!=
b
.
end
())
{
++
ai
;
}
else
{
++
bi
;
if
(
ai
->
first
==
bi
->
first
)
{
sum
+=
ai
->
second
*
bi
->
second
;
++
ai
;
++
bi
;
}
else
if
(
ai
->
first
<
bi
->
first
)
{
++
ai
;
}
else
{
++
bi
;
}
}
return
sum
;
}
}
return
sum
;
template
<
typename
T
>
inline
typename
T
::
value_type
::
second_type
dot
(
const
T
&
a
,
const
T
&
b
)
{
return
dlib
::
sparse_vector
::
impl
::
dot
(
a
,
b
);
}
template
<
typename
T1
,
typename
T2
,
typename
T3
,
typename
T4
,
typename
T5
,
typename
T6
>
inline
T4
dot
(
const
std
::
vector
<
T1
,
T2
>&
a
,
const
std
::
map
<
T3
,
T4
,
T5
,
T6
>&
b
)
{
return
dlib
::
sparse_vector
::
impl
::
dot
(
a
,
b
);
}
template
<
typename
T1
,
typename
T2
,
typename
T3
,
typename
T4
,
typename
T5
,
typename
T6
>
inline
T4
dot
(
const
std
::
map
<
T3
,
T4
,
T5
,
T6
>&
a
,
const
std
::
vector
<
T1
,
T2
>&
b
)
{
return
dlib
::
sparse_vector
::
impl
::
dot
(
a
,
b
);
}
// ------------------------------------------------------------------------------------
...
...
dlib/svm/sparse_vector_abstract.h
View file @
2f2aecc9
...
...
@@ -6,6 +6,8 @@
#include <cmath>
#include "../algs.h"
#include "../serialize.h"
#include <map>
#include <vector>
namespace
dlib
{
...
...
@@ -164,6 +166,30 @@ namespace dlib
- returns the dot product between the vectors a and b
!*/
template
<
typename
T1
,
typename
T2
,
typename
T3
,
typename
T4
,
typename
T5
,
typename
T6
>
T4
dot
(
const
std
::
vector
<
T1
,
T2
>&
a
,
const
std
::
map
<
T3
,
T4
,
T5
,
T6
>&
b
);
/*!
requires
- a and b are sparse vectors
ensures
- returns the dot product between the vectors a and b
!*/
template
<
typename
T1
,
typename
T2
,
typename
T3
,
typename
T4
,
typename
T5
,
typename
T6
>
T4
dot
(
const
std
::
map
<
T3
,
T4
,
T5
,
T6
>&
a
,
const
std
::
vector
<
T1
,
T2
>&
b
);
/*!
requires
- a and b are sparse vectors
ensures
- returns the dot product between the vectors a and b
!*/
// ----------------------------------------------------------------------------------------
template
<
typename
T
,
typename
EXP
>
...
...
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