not compiling yet
This commit is contained in:
@@ -26,27 +26,31 @@
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include "fastlib/fastlib.h"
|
||||
#include "fastlib/la/vector.h"
|
||||
#include "la/matrix.h"
|
||||
#include "Epetra_CrsMatrix.h"
|
||||
#include "Epetra_SerialComm.h"
|
||||
#include "Epetra_Map.h"
|
||||
|
||||
class SparseVectorTest;
|
||||
class SparseVectorTest;
|
||||
class sparse;
|
||||
|
||||
class SparseVector {
|
||||
public:
|
||||
friend class SparseVectorTest;
|
||||
friend class sparse;
|
||||
SparseVector() {
|
||||
}
|
||||
SparseVector(std::vector<index_t> &indices,
|
||||
Vector &values,
|
||||
index_t dimension);
|
||||
SparseVector(std::map<index_t, double> data,
|
||||
SparseVector(std::map<index_t, double> &data,
|
||||
index_t dimension);
|
||||
SparseVector(index_t estimated_non_zero_elements,
|
||||
index_t dimension);
|
||||
SparseVector(Epetra_CrsMatrix *one_dim_matrix,
|
||||
index_t dimension);
|
||||
SparceVector(const SparseVector &other);
|
||||
void Destruct();
|
||||
SparseVector(const SparseVector &other);
|
||||
~SparseVector() {
|
||||
Destruct();
|
||||
}
|
||||
@@ -59,11 +63,10 @@ class SparseVector {
|
||||
void Init(std::map<index_t, double> data, index_t dimension);
|
||||
void Init(index_t estimated_non_zero_elements, index_t dimension);
|
||||
void Init(Epetra_CrsMatrix *one_dim_matrix, index_t dimension);
|
||||
void Copy(const SparseVector &other)
|
||||
void Destruct();
|
||||
void Copy(const SparseVector &other);
|
||||
void MakeSubvector(index_t start_index,
|
||||
index_t len,
|
||||
SparseVector* dest)
|
||||
SparseVector* dest);
|
||||
double get(index_t i);
|
||||
void set(index_t i, double value);
|
||||
void set_start(index_t i);
|
||||
@@ -74,7 +77,7 @@ class SparseVector {
|
||||
private:
|
||||
Epetra_CrsMatrix *vector_;
|
||||
Epetra_SerialComm comm_;
|
||||
Epetra_Map map_;
|
||||
Epetra_Map *map_;
|
||||
index_t *myglobal_elements_;
|
||||
index_t dimension_;
|
||||
index_t start_;
|
||||
@@ -83,8 +86,9 @@ class SparseVector {
|
||||
|
||||
};
|
||||
|
||||
namespace sparse {
|
||||
inline void AddVectors(SparseVector &v1, SparseVector &v2, SparseVector* sum) {
|
||||
class sparse {
|
||||
public:
|
||||
static inline void AddVectors(SparseVector &v1, SparseVector &v2, SparseVector* sum) {
|
||||
if (unlikely(v1.dimension_ != v2.dimension_)) {
|
||||
FATAL("Sparse Vectors have different dimensions %i != %i", v1.dimension_, v2.dimension_);
|
||||
}
|
||||
@@ -116,17 +120,17 @@ namespace sparse {
|
||||
j++;
|
||||
}
|
||||
if (i<num1) {
|
||||
values3.insert(values3.end(), &values1+i, values1.end());
|
||||
indices3.insert(indices3.end(), &indices1+i, indices1.end());
|
||||
values3.insert(values3.end(), values1+i, values1+num1);
|
||||
indices3.insert(indices3.end(), indices1+i, indices1+num1);
|
||||
}
|
||||
if (j<num2) {
|
||||
values3.insert(values3.end(), &values2+j, values2.end());
|
||||
indices3.insert(indices3.end(), &indices2+i, indices2.end());
|
||||
values3.insert(values3.end(), values2+j, values2+num2);
|
||||
indices3.insert(indices3.end(), indices2+i, indices2+num2);
|
||||
}
|
||||
sum->Init(indices3, values3, v1.dimension_);
|
||||
}
|
||||
|
||||
inline void Subtract(SparseVector &v1, SparseVector &v2, SparseVector *diff) {
|
||||
static inline void Subtract(SparseVector &v1, SparseVector &v2, SparseVector *diff) {
|
||||
if (unlikely(v1.dimension_ != v2.dimension_)) {
|
||||
FATAL("Sparse Vectors have different dimensions %i != %i", v1.dimension_, v2.dimension_);
|
||||
}
|
||||
@@ -158,8 +162,8 @@ namespace sparse {
|
||||
j++;
|
||||
}
|
||||
if (i<num1) {
|
||||
values3.insert(values3.end(), &values1+i, values1.end());
|
||||
indices3.insert(indices3.end(), &indices1+i, indices1.end());
|
||||
values3.insert(values3.end(), values1+i, values1+num1);
|
||||
indices3.insert(indices3.end(), indices1+i, indices1+num1);
|
||||
}
|
||||
if (j<num2) {
|
||||
for(index_t jj=i; jj<num2; jj++) {
|
||||
@@ -170,7 +174,7 @@ namespace sparse {
|
||||
diff->Init(indices3, values3, v1.dimension_);
|
||||
}
|
||||
|
||||
inline void PointProduct(SparseVector &v1, SparseVector &v2, SparseVector *point_prod) {
|
||||
static inline void PointProduct(SparseVector &v1, SparseVector &v2, SparseVector *point_prod) {
|
||||
if (unlikely(v1.dimension_ != v2.dimension_)) {
|
||||
FATAL("Sparse Vectors have different dimensions %i != %i", v1.dimension_, v2.dimension_);
|
||||
}
|
||||
@@ -199,7 +203,7 @@ namespace sparse {
|
||||
point_prod->Init(indices3, values3, v1.dimension_);
|
||||
}
|
||||
|
||||
inline void DotProduct(SparseVector &v1, SparseVector &v2, double *dot_product) {
|
||||
static inline void DotProduct(SparseVector &v1, SparseVector &v2, double *dot_product) {
|
||||
if (unlikely(v1.dimension_ != v2.dimension_)) {
|
||||
FATAL("Sparse Vectors have different dimensions %i != %i", v1.dimension_, v2.dimension_);
|
||||
}
|
||||
@@ -219,13 +223,13 @@ namespace sparse {
|
||||
}
|
||||
}
|
||||
if ( likely(i<num1) && indices1[i] == indices2[j]) {
|
||||
dot_product+=values1[i] * values2[j];
|
||||
*dot_product += values1[i] * values2[j];
|
||||
}
|
||||
j++;
|
||||
}
|
||||
}
|
||||
|
||||
inline void DistanceSqEuclidean(SparseVector &v1, SparseVector &v2, double *dist) {
|
||||
static inline void DistanceSqEuclidean(SparseVector &v1, SparseVector &v2, double *dist) {
|
||||
if (unlikely(v1.dimension_ != v2.dimension_)) {
|
||||
FATAL("Sparse Vectors have different dimensions %i != %i", v1.dimension_, v2.dimension_);
|
||||
}
|
||||
@@ -234,9 +238,11 @@ namespace sparse {
|
||||
index_t *indices1, *indices2;
|
||||
v1.vector_->ExtractGlobalRowView(0, num1, values1, indices1);
|
||||
v2.vector_->ExtractGlobalRowView(0, num2, values2, indices2);
|
||||
std::vector<double> values3;
|
||||
std::vector<index_t> indices3;
|
||||
index_t i=0;
|
||||
index_t j=0;
|
||||
double dist=0;
|
||||
*dist=0;
|
||||
while (likely(i<num1 && j<num2)) {
|
||||
while (indices1[i] < indices2[j]) {
|
||||
values3.push_back(values1[i]);
|
||||
@@ -247,30 +253,27 @@ namespace sparse {
|
||||
}
|
||||
}
|
||||
if ( likely(i<num1) && indices1[i] == indices2[j]) {
|
||||
dist += (values1[i] - values2[j]) * (values1[i] - values2[j]);
|
||||
*dist += (values1[i] - values2[j]) * (values1[i] - values2[j]);
|
||||
} else {
|
||||
dist += values2[j] * values2[j];
|
||||
*dist += values2[j] * values2[j];
|
||||
}
|
||||
j++;
|
||||
}
|
||||
if (i<num1) {
|
||||
for(index_t ii=i; i<num1; i++) {
|
||||
dist += values1[ii] * values1[ii];
|
||||
*dist += values1[ii] * values1[ii];
|
||||
}
|
||||
}
|
||||
if (j<num2) {
|
||||
for(index_t jj=j; jj<num2; jj++) {
|
||||
dist += values2[jj] * values2[jj];
|
||||
*dist += values2[jj] * values2[jj];
|
||||
|
||||
}
|
||||
}
|
||||
diff->Init(indices3, values3, v1.dimension_);
|
||||
}
|
||||
|
||||
template<int t_pow>
|
||||
inline void RawLMetric(SparseVector &v1, SparseVector &v2, double *dist);
|
||||
template<int t_pow>
|
||||
inline void RawLMetric(SparseVector &v1, SparseVector &v2, double *dist);
|
||||
};
|
||||
|
||||
#include "sparse_vector_impl.h"
|
||||
|
||||
@@ -17,18 +17,19 @@
|
||||
*/
|
||||
|
||||
inline SparseVector::SparseVector(std::vector<index_t> &indices,
|
||||
Vector &values
|
||||
Vector &values,
|
||||
index_t dimension) {
|
||||
Init(indices, values, dimension);
|
||||
}
|
||||
|
||||
inline SparseVector::SparseVector(std::map<index_t, double> data) {
|
||||
Init(data);
|
||||
inline SparseVector::SparseVector(std::map<index_t, double> &data,
|
||||
index_t dimension) {
|
||||
Init(data, dimension);
|
||||
}
|
||||
|
||||
inline SparseVector::SparseVector(index_t estimated_non_zero_elements,
|
||||
index_t dimension) {
|
||||
Init(estimated_non_zero_elements);
|
||||
Init(estimated_non_zero_elements, dimension);
|
||||
}
|
||||
|
||||
inline SparseVector::SparseVector(Epetra_CrsMatrix *one_dim_matrix,
|
||||
@@ -48,9 +49,9 @@ inline void SparseVector::Init(std::vector<index_t> &indices,
|
||||
Vector &values,
|
||||
index_t dimension) {
|
||||
Init();
|
||||
if (unlikely(indices.size()!=values.size())) {
|
||||
if (unlikely(indices.size()!=values.length())) {
|
||||
FATAL("Indices vector has %i elements while Values vectors has %i\n",
|
||||
indices.size(), values.size());
|
||||
indices.size(), values.length());
|
||||
}
|
||||
std::vector<index_t>::iterator it = std::max_element(indices.begin(), indices.end());
|
||||
dimension_ = *it+1;
|
||||
@@ -63,16 +64,16 @@ inline void SparseVector::Init(std::vector<index_t> &indices,
|
||||
}
|
||||
}
|
||||
vector_ = new Epetra_CrsMatrix(Copy , *map_, indices.size());
|
||||
my_global_elements_ = map_.MyGlobalElements();
|
||||
myglobal_elements_ = map_.MyGlobalElements();
|
||||
vector_->InsertGlobalValues(*myglobal_elements_,
|
||||
values.size(),
|
||||
&values.ptr(),
|
||||
values.length(),
|
||||
values.ptr(),
|
||||
&indices);
|
||||
start_ = 0;
|
||||
end_ = dimension_-1;
|
||||
}
|
||||
|
||||
void Sparse::Init(std::vector<index_t> &indices, std::vector<double> &values,
|
||||
void SparseVector::Init(std::vector<index_t> &indices, std::vector<double> &values,
|
||||
index_t dimension) {
|
||||
Init();
|
||||
if (unlikely(indices.size()!=values.size())) {
|
||||
@@ -102,7 +103,7 @@ void Sparse::Init(std::vector<index_t> &indices, std::vector<double> &values,
|
||||
void SparseVector::Init(index_t *indices, double *values, index_t len, index_t dimension) {
|
||||
Init();
|
||||
vector_ = new Epetra_CrsMatrix(Copy , *map_, len);
|
||||
my_global_elements_ = map_.MyGlobalElements();
|
||||
myglobal_elements_ = map_->MyGlobalElements();
|
||||
vector_->InsertGlobalValues(*myglobal_elements_,
|
||||
len,
|
||||
values,
|
||||
@@ -113,7 +114,7 @@ void SparseVector::Init(index_t *indices, double *values, index_t len, index_t d
|
||||
}
|
||||
|
||||
|
||||
inline void SparseMatrix::Init(std::map<index_t, double> &data, index_t dimension) {
|
||||
inline void SparseVector::Init(std::map<index_t, double> &data, index_t dimension) {
|
||||
Init();
|
||||
std::map<index_t, double>::iterator it=max_element(data.begin(), data.end());
|
||||
dimension_ = it->first+1;
|
||||
@@ -146,7 +147,7 @@ inline void SparseMatrix::Init(std::map<index_t, double> &data, index_t dimensio
|
||||
|
||||
}
|
||||
|
||||
inline void SparseMatrix::Init(index_t estimated_non_zero_elements, index_t dimension) {
|
||||
inline void SparseVector::Init(index_t estimated_non_zero_elements, index_t dimension) {
|
||||
Init();
|
||||
vector_ = new Epetra_CrsMatrix(Copy, map_, estimated_non_zero_elements);
|
||||
dimension_ = dimension;
|
||||
@@ -154,7 +155,7 @@ inline void SparseMatrix::Init(index_t estimated_non_zero_elements, index_t dime
|
||||
end_ = dimension_-1;
|
||||
}
|
||||
|
||||
inline void SparseMatrix::Init(Epetra_CrsMatrix *one_dim_matrix, index_t dimension) {
|
||||
inline void SparseVector::Init(Epetra_CrsMatrix *one_dim_matrix, index_t dimension) {
|
||||
Init();
|
||||
vector_ = one_dim_matrix;
|
||||
*map_ = one_dim_matrix->RowMap();
|
||||
@@ -164,7 +165,7 @@ inline void SparseMatrix::Init(Epetra_CrsMatrix *one_dim_matrix, index_t dimensi
|
||||
end_ = dimension_-1;
|
||||
}
|
||||
|
||||
inline void Copy(const SparseVector &other) {
|
||||
inline void SparseVector::Copy(const SparseVector &other) {
|
||||
Init();
|
||||
vector_ = new Epetra_CrsMatrix(other);
|
||||
dimension_ = other.dimension;
|
||||
@@ -178,7 +179,7 @@ inline void SparseVector::Destruct() {
|
||||
delete map_;
|
||||
}
|
||||
|
||||
inline void SparseMatrix::MakeSubvector(index_t start_index, index_t len, SparseVector* dest) {
|
||||
inline void SparseVector::MakeSubvector(index_t start_index, index_t len, SparseVector* dest) {
|
||||
DEBUG_BOUNDS(start_ + start_index, end_+1);
|
||||
DEBUG_BOUNDS(start_ + start_index+len-1, end_+1);
|
||||
dest->Init(this->vector_, dimension_);
|
||||
@@ -186,7 +187,7 @@ inline void SparseMatrix::MakeSubvector(index_t start_index, index_t len, Sparse
|
||||
dest->set_end(start_ + start_index_t + len-1);
|
||||
}
|
||||
|
||||
inline double SparseMatrix::get(index_t i) {
|
||||
inline double SparseVector::get(index_t i) {
|
||||
DEBUG_BOUNDS(start_+i, end_+1);
|
||||
pos = start_+i;
|
||||
double *values;
|
||||
|
||||
@@ -15,12 +15,12 @@
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
#include <limit>
|
||||
#include <limits>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include "fastlib/fastlib.h"
|
||||
#include "u/nvasil/sparse_matrix/sparse_vector.h"
|
||||
|
||||
/*
|
||||
class SparseVectorTest {
|
||||
public:
|
||||
void Init() {
|
||||
@@ -93,26 +93,93 @@ class SparseVectorTest {
|
||||
void TestAdd() {
|
||||
SparseVector v;
|
||||
sparse::Add(v1, v2, &v);
|
||||
double epected_result[dim_];
|
||||
memset(expected_result, 0, dim_*sizeof(double));
|
||||
for(index_t i=0; i<10; i++) {
|
||||
expected_result[2*i+1]+= 3*i+1;
|
||||
}
|
||||
for(index_t i=0; i<5; i++) {
|
||||
expected_result[2*i+1]+= 4*i+1;
|
||||
}
|
||||
for(index_t i=0; i<dim_; i++) {
|
||||
TEST_DOUBLE_ASSERT(v.get(i), expected_result[i],
|
||||
numeric_limits<Precision_t>::epsilon());
|
||||
}
|
||||
}
|
||||
|
||||
void TestSubtract() {
|
||||
SparseVector v;
|
||||
sparse::Subtract(v1, v2, &v);
|
||||
double epected_result[dim_];
|
||||
memset(expected_result, 0, dim_*sizeof(double));
|
||||
for(index_t i=0; i<10; i++) {
|
||||
expected_result[2*i+1]+= 3*i+1;
|
||||
}
|
||||
for(index_t i=0; i<5; i++) {
|
||||
expected_result[2*i+1]-= 4*i+1;
|
||||
}
|
||||
for(index_t i=0; i<dim_; i++) {
|
||||
TEST_DOUBLE_ASSERT(v.get(i), expected_result[i],
|
||||
numeric_limits<Precision_t>::epsilon());
|
||||
}
|
||||
}
|
||||
|
||||
void TestPointProduct() {
|
||||
SparseVector v;
|
||||
sparse::PointProduct(v1, v2, &v);
|
||||
double epected_result[dim_];
|
||||
memset(expected_result, 0, dim_*sizeof(double));
|
||||
for(index_t i=0; i<10; i++) {
|
||||
expected_result[2*i+1]+= 3*i+1;
|
||||
}
|
||||
for(index_t i=0; i<5; i++) {
|
||||
expected_result[2*i+1]*= 4*i+1;
|
||||
}
|
||||
for(index_t i=0; i<dim_; i++) {
|
||||
TEST_DOUBLE_ASSERT(v.get(i), expected_result[i],
|
||||
numeric_limits<Precision_t>::epsilon());
|
||||
}
|
||||
}
|
||||
|
||||
void TestDotProduct() {
|
||||
double dot_prod;
|
||||
sparse::DotProduct(v1, v2, &dot_prod);
|
||||
double epected_result[dim_];
|
||||
memset(expected_result, 0, dim_*sizeof(double));
|
||||
for(index_t i=0; i<10; i++) {
|
||||
expected_result[2*i+1]+= 3*i+1;
|
||||
}
|
||||
for(index_t i=0; i<5; i++) {
|
||||
expected_result[2*i+1]*= 4*i+1;
|
||||
}
|
||||
double expected_dot_prod=0;
|
||||
for(index_t i=0; i<dim_; i++) {
|
||||
expected_dot_prod+=expected_result[i];
|
||||
}
|
||||
TEST_DOUBLE_ASSERT(dot_prod,
|
||||
expected_dot_prod,
|
||||
numeric_limits<Precision_t>::epsilon());
|
||||
|
||||
}
|
||||
|
||||
void TestDistance() {
|
||||
double dist;
|
||||
sparse::Distance(v1, v2, &dist);
|
||||
double epected_result[dim_];
|
||||
memset(expected_result, 0, dim_*sizeof(double));
|
||||
for(index_t i=0; i<10; i++) {
|
||||
expected_result[2*i+1]+= 3*i+1;
|
||||
}
|
||||
for(index_t i=0; i<5; i++) {
|
||||
expected_result[2*i+1]-= 4*i+1;
|
||||
}
|
||||
double distance=0;
|
||||
for(index_t i=0; i<dim_; i++) {
|
||||
distance = expected_result[i] * expected_result[i];
|
||||
}
|
||||
TEST_DOUBLE_ASSERT(distance,
|
||||
dist,
|
||||
numeric_limits<Precision_t>::epsilon());
|
||||
}
|
||||
|
||||
void TestAll() {
|
||||
@@ -143,3 +210,4 @@ int main() {
|
||||
SparseVectorTest test;
|
||||
test.TestAll();
|
||||
}
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user