Commit 4138b15a authored by Davis King's avatar Davis King

Added sparse_matrix_vector_multiply()

parent 7f5a22c4
......@@ -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;
}
}
// ----------------------------------------------------------------------------------------
}
......
......@@ -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)
!*/
// ----------------------------------------------------------------------------------------
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment