SparseMatrix.hpp 3.26 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
/*
 * Matrix.hpp
 *
 *  Created on: Oct 5, 2015
 *      Author: i-bird
 */

#ifndef OPENFPM_NUMERICS_SRC_MATRIX_SPARSEMATRIX_HPP_
#define OPENFPM_NUMERICS_SRC_MATRIX_SPARSEMATRIX_HPP_

11
#ifdef HAVE_EIGEN
12
#include <Eigen/Sparse>
13
14
15
16
#define DEFAULT_MATRIX  = Eigen::SparseMatrix<T,0,id_t>
#else
#define DEFAULT_MATRIX = void
#endif
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

/*! \brief It store the non zero elements of the matrix
 *
 *
 */
template<typename T> struct cval
{
	size_t j;
	T value;
};

/*! \brief It store the non zero elements of the matrix
 *
 *
 */
Pietro Incardona's avatar
Pietro Incardona committed
32
template<typename T, int impl> struct triplet
33
34
35
36
37
{
	long int i;
	long int j;
	T val;

38
39
40
41
42
43
44
45
46
47
48
	triplet(long int i, long int j, T val)
	{
		row() = i;
		col() = j;
		value() = val;
	}

	triplet()
	{
	}

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
	long int & row()
	{
		return i;
	}

	long int & col()
	{
		return j;
	}

	T & value()
	{
		return val;
	}
};

Pietro Incardona's avatar
Pietro Incardona committed
65
/*! \brief Sparse Matrix implementation
66
 *
Pietro Incardona's avatar
Pietro Incardona committed
67
 * \tparam T Type of the sparse Matrix store on each row,colums
68
 * \tparam id_t type of id
Pietro Incardona's avatar
Pietro Incardona committed
69
 * \tparam Mi implementation
70
71
 *
 */
72
template<typename T,typename id_t ,typename Mi DEFAULT_MATRIX>
73
74
class SparseMatrix
{
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
public:

	//! Triplet implementation id
	typedef boost::mpl::int_<-1> triplet_impl;

	//! Triplet type
	typedef triplet<T,-1> triplet_type;

	openfpm::vector<triplet_type> stub_vt;
	int stub_i;
	T stub_t;

public:


	SparseMatrix(size_t N1, size_t N2) {std::cerr << __FILE__ << ":" << __LINE__ << " Error in order to use this class you must compile OpenFPM with linear algebra support" << std::endl;}
	SparseMatrix()	{std::cerr << __FILE__ << ":" << __LINE__ << " Error in order to use this class you must compile OpenFPM with linear algebra support" << std::endl;}
	openfpm::vector<triplet_type> & getMatrixTriplets() {std::cerr << __FILE__ << ":" << __LINE__ << " Error in order to use this class you must compile OpenFPM with linear algebra support" << std::endl; return stub_vt;}
	const int & getMat() const {std::cerr << __FILE__ << ":" << __LINE__ << " Error in order to use this class you must compile OpenFPM with linear algebra support" << std::endl; return stub_i;}
	int & getMat() {std::cerr << __FILE__ << ":" << __LINE__ << " Error in order to use this class you must compile OpenFPM with linear algebra support" << std::endl; return stub_i;}
	void resize(size_t row, size_t col) {std::cerr << __FILE__ << ":" << __LINE__ << " Error in order to use this class you must compile OpenFPM with linear algebra support" << std::endl;}
	T operator()(id_t i, id_t j) {std::cerr << __FILE__ << ":" << __LINE__ << " Error in order to use this class you must compile OpenFPM with linear algebra support" << std::endl; return stub_t;}
	bool save(const std::string & file) const {std::cerr << __FILE__ << ":" << __LINE__ << " Error in order to use this class you must compile OpenFPM with linear algebra support" << std::endl;return true;}
	bool load(const std::string & file) {std::cerr << __FILE__ << ":" << __LINE__ << " Error in order to use this class you must compile OpenFPM with linear algebra support" << std::endl; return false;}
	T getValue(size_t r, size_t c) {std::cerr << __FILE__ << ":" << __LINE__ << " Error in order to use this class you must compile OpenFPM with linear algebra support" << std::endl; return stub_i;}
100
101
};

102
#ifdef HAVE_EIGEN
103
#include "SparseMatrix_Eigen.hpp"
104
#endif
105
106

#endif /* OPENFPM_NUMERICS_SRC_MATRIX_SPARSEMATRIX_HPP_ */