Skip to content

Commit

Permalink
Add sparse vector first implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiaisgro committed Apr 30, 2024
1 parent c0307e3 commit d09a99f
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 7 deletions.
22 changes: 15 additions & 7 deletions examples/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,24 @@
#include "theoretica.h"
using namespace th;

#include "algebra/sparse_vec.h"


int main() {

// Declare a 3D vector
vec3 v = {1, 2, 3};
sparse_vec<> v1 = sparse_vec<>({
vec2({1, PI}), vec2({9, PHI})
});

// Create a 3x3 identity matrix
mat3 A = mat3::identity();
sparse_vec<> v2 = sparse_vec<>({
vec2({1, 3.0})
});

// Transform v by A
vec3 w = A * v;

std::cout << v1 << std::endl;
std::cout << v2 << std::endl;
std::cout << v1 * v2 << std::endl;

char c;
std::cin >> c;
return 0;
}
124 changes: 124 additions & 0 deletions src/algebra/sparse_vec.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@

///
/// @file sparse_vec.h Sparse vector class implemented as a hashmap
///

#ifndef THEORETICA_SPARSE_VEC_H
#define THEORETICA_SPARSE_VEC_H

#include <map>
#include "../core/constants.h"


namespace theoretica {

/// @class sparse_vec Sparse vector class implemented as a hashmap
template<typename T = real, typename IndexType = size_t>
class sparse_vec {
public:

/// Data is stored in a hashmap as (index, value)
std::map<IndexType, T> data;


/// Initialize as the zero vector
sparse_vec() {}


/// Initialize the sparse vector from a list
/// of pairs (index, value)
template<typename Pair>
sparse_vec(const std::initializer_list<Pair>& pairs) {

for (auto& pair : pairs)
data[pair[0]] = pair[1];
}


~sparse_vec() = default;


/// Add two sparse vectors
inline sparse_vec<T> operator+(const sparse_vec<T>& other) {

sparse_vec res;
res.data = this->data;

for (auto& pair : other)
res[pair.first] += pair.second;

return res;
}


/// Dot product of two sparse vectors
inline T operator*(const sparse_vec<T>& other) {

T res = T(0.0);

for (auto& pair : data)
res += pair.second * other[pair.first];

return res;
}


/// Get the n-th element
inline T operator[](IndexType index) const {

const auto iter = data.find(index);

if(iter == data.end())
return 0;

return iter->second;
}


/// Access the n-th element
inline T& operator[](IndexType index) {
return data[index];
}


#ifndef THEORETICA_NO_PRINT

/// Convert the vector to string representation
inline std::string to_string(
const std::string& separator = ", ",
bool parenthesis = true) const {

std::stringstream res;

// if(parenthesis)
// res << "(";

for (auto& pair : this->data) {
// res << pair.second;
// res << separator;

res << "(" << pair.first << ", "
<< pair.second << ") ";

}

// if(parenthesis)
// res << ")";

return res.str();
}


/// Stream the vector in string representation to an output stream (std::ostream)
inline friend std::ostream& operator<<(
std::ostream& out, const sparse_vec<T>& obj) {
return out << obj.to_string();
}

#endif

};

}

#endif

0 comments on commit d09a99f

Please sign in to comment.