Subversion Repositories gelsvn

Rev

Rev 464 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 464 Rev 630
1
/*
1
/*
2
 *  CSCMatrixBuilder.h
2
 *  CSCMatrixBuilder.h
3
 *  GEL
3
 *  GEL
4
 *	Added to simplify sequetnial building of sparse matrices in CSC format
4
 *	Added to simplify sequetnial building of sparse matrices in CSC format
5
 *  At this version this doesn't check if we are outside matrix (no matrix size)
5
 *  At this version this doesn't check if we are outside matrix (no matrix size)
6
 *  1. Insert the entries column by column (for symmetric matrices only up triangle) by using:
6
 *  1. Insert the entries column by column (for symmetric matrices only up triangle) by using:
7
 *				insert_entry(row_number,entry);
7
 *				insert_entry(row_number,entry);
8
 *				next_column()
8
 *				next_column()
9
 *				next_column_nonsort() - can be used instead of next_column where the columns were also filled
9
 *				next_column_nonsort() - can be used instead of next_column where the columns were also filled
10
 *											sequentially (in their order)
10
 *											sequentially (in their order)
11
 *		You can also use LoadDense or LoadSymmetricDense if you have dense representation of the matrix in
11
 *		You can also use LoadDense or LoadSymmetricDense if you have dense representation of the matrix in
12
 *      LinAlg::CMatrix type.
12
 *      LinAlg::CMatrix type.
13
 *  2. Call get_Matrix() to get the representation of the matrix as a ARluSymMatrix<Type> 
13
 *  2. Call get_Matrix() to get the representation of the matrix as a ARluSymMatrix<Type> 
14
 *		(you can pass this type directly to the symmetric problem constructor).
14
 *		(you can pass this type directly to the symmetric problem constructor).
15
 *  Created by Katarzyna Gebal on 18/11/08
15
 *  Created by Katarzyna Gebal on 18/11/08
16
 *	
16
 *	
17
 */
17
 */
18
#ifndef __MESHEDIT_CSCMATRIXBUILDER_H__
18
#ifndef __MESHEDIT_CSCMATRIXBUILDER_H__
19
#define __MESHEDIT_CSCMATRIXBUILDER_H__
19
#define __MESHEDIT_CSCMATRIXBUILDER_H__
20
 
20
 
21
#include <vector>
21
#include <vector>
22
#include <LinAlg/Matrix.h>
22
#include <LinAlg/Matrix.h>
23
#include "arlsmat.h"
23
#include "arlsmat.h"
24
 
24
 
25
template<class T>
25
template<class T>
26
class CSCMatrixBuilder
26
class CSCMatrixBuilder
27
{
27
{
28
	typedef T Type;
28
	typedef T Type;
29
	std::vector<Type> vA;
29
	std::vector<Type> vA;
30
	std::vector<int> vpcol;
30
	std::vector<int> vpcol;
31
	std::vector<int> virow;
31
	std::vector<int> virow;
32
 
32
 
33
	ARluSymMatrix<Type> mat;
33
	ARluSymMatrix<Type> mat;
34
	int *irow;
34
	int *irow;
35
	int *pcol;
35
	int *pcol;
36
	Type *A;
36
	Type *A;
37
	bool mat_created;
37
	bool mat_created;
38
 
38
 
39
	int get_nnz()
39
	int get_nnz()
40
	{
40
	{
41
		return vA.size();
41
		return vA.size();
42
	}
42
	}
43
 
43
 
44
	int get_ncol()
44
	int get_ncol()
45
	{
45
	{
46
		return vpcol.size()-1;
46
		return vpcol.size()-1;
47
	}
47
	}
48
 
48
 
49
	int* get_pcol()
49
	int* get_pcol()
50
	{
50
	{
51
		int *pcol = new int[vpcol.size()];
51
		int *pcol = new int[vpcol.size()];
52
		std::vector<int>::iterator cit = vpcol.begin();
52
		std::vector<int>::iterator cit = vpcol.begin();
53
		for(int i = 0; i < vpcol.size(); i++)
53
		for(int i = 0; i < vpcol.size(); i++)
54
		{
54
		{
55
			pcol[i] = *cit;
55
			pcol[i] = *cit;
56
			++cit;
56
			++cit;
57
		}
57
		}
58
		return pcol;
58
		return pcol;
59
	}
59
	}
60
 
60
 
61
	int* get_irow()
61
	int* get_irow()
62
	{
62
	{
63
		int nnz = vA.size();
63
		int nnz = vA.size();
64
		std::vector<int>::iterator rit = virow.begin();
64
		std::vector<int>::iterator rit = virow.begin();
65
		int *irow = new int[nnz];
65
		int *irow = new int[nnz];
66
		for(int i = 0; i < nnz; i++)
66
		for(int i = 0; i < nnz; i++)
67
		{
67
		{
68
			irow[i] = *rit;
68
			irow[i] = *rit;
69
			++rit;
69
			++rit;
70
		}
70
		}
71
		return irow;
71
		return irow;
72
	}
72
	}
73
 
73
 
74
	
74
	
75
	Type* get_A()
75
	Type* get_A()
76
	{
76
	{
77
		int nnz = vA.size();
77
		int nnz = vA.size();
78
		Type *A = new Type[nnz];
78
		Type *A = new Type[nnz];
79
		for(int i = 0; i < nnz; i++)
79
		for(int i = 0; i < nnz; i++)
80
			A[i] = vA[i];
80
			A[i] = vA[i];
81
		return A;
81
		return A;
82
	}
82
	}
83
 
83
 
84
	void create_mat()
84
	void create_mat()
85
	{
85
	{
86
		if(mat_created)
86
		if(mat_created)
87
			return;
87
			return;
88
		mat_created = true;
88
		mat_created = true;
89
		irow = get_irow();               
89
		irow = get_irow();               
90
		pcol = get_pcol();     
90
		pcol = get_pcol();     
91
		A = get_A();
91
		A = get_A();
92
		mat = ARluSymMatrix<Type>(get_ncol(), get_nnz(), A, irow, pcol);	
92
		mat = ARluSymMatrix<Type>(get_ncol(), get_nnz(), A, irow, pcol);	
93
 
93
 
94
	}
94
	}
95
public:
95
public:
96
	ARluSymMatrix<Type>& get_Matrix()
96
	ARluSymMatrix<Type>& get_Matrix()
97
	{
97
	{
98
		create_mat();
98
		create_mat();
99
		return mat;
99
		return mat;
100
	}
100
	}
101
	CSCMatrixBuilder()
101
	CSCMatrixBuilder()
102
	{
102
	{
103
		mat_created = false;
103
		mat_created = false;
104
		vpcol.push_back(0);
104
		vpcol.push_back(0);
105
	}
105
	}
106
	~CSCMatrixBuilder()
106
	~CSCMatrixBuilder()
107
	{
107
	{
108
		if(mat_created)
108
		if(mat_created)
109
		{
109
		{
110
			delete [] irow;
110
			delete [] irow;
111
			delete [] pcol;
111
			delete [] pcol;
112
			delete [] A;
112
			delete [] A;
113
		}
113
		}
114
	}
114
	}
115
 
115
 
116
	void sort_entries(int id)
116
	void sort_entries(int id)
117
	{
117
	{
118
		int start = vpcol[id];
118
		int start = vpcol[id];
119
		int stop = vpcol[id+1];
119
		int stop = vpcol[id+1];
120
		Type dpom;
120
		Type dpom;
121
		int ipom;
121
		int ipom;
122
		for(int i = start; i < stop; i++)
122
		for(int i = start; i < stop; i++)
123
			for(int j = i+1; j < stop; j++)
123
			for(int j = i+1; j < stop; j++)
124
			{
124
			{
125
				if(virow[i] > virow[j])
125
				if(virow[i] > virow[j])
126
				{
126
				{
127
					ipom = virow[i]; virow[i] = virow[j]; virow[j] = ipom;
127
					ipom = virow[i]; virow[i] = virow[j]; virow[j] = ipom;
128
					dpom = vA[i]; vA[i] = vA[j]; vA[j] = dpom;
128
					dpom = vA[i]; vA[i] = vA[j]; vA[j] = dpom;
129
				}
129
				}
130
			}
130
			}
131
	}
131
	}
132
 
132
 
133
	void insert_entry(int row, Type entry)
133
	void insert_entry(int row, Type entry)
134
	{
134
	{
135
		vA.push_back(entry);
135
		vA.push_back(entry);
136
		virow.push_back(row);
136
		virow.push_back(row);
137
	}
137
	}
138
 
138
 
139
	void next_column_nonsort()
139
	void next_column_nonsort()
140
	{
140
	{
141
		vpcol.push_back(vA.size());
141
		vpcol.push_back(vA.size());
142
	}
142
	}
143
 
143
 
144
	void next_column()
144
	void next_column()
145
	{
145
	{
146
		vpcol.push_back(vA.size());
146
		vpcol.push_back(vA.size());
147
		sort_entries(vpcol.size()-2);
147
		sort_entries(vpcol.size()-2);
148
	}
148
	}
149
 
149
 
150
	void LoadSymmetricDense(LinAlg::CMatrix Q, Type eps)
150
	void LoadSymmetricDense(LinAlg::CMatrix Q, Type eps)
151
	{
151
	{
152
		 for(int i = 0; i < Q.Cols(); i++)
152
		 for(int i = 0; i < Q.Cols(); i++)
153
		 {
153
		 {
154
			 for(int j = i; j < Q.Rows(); j++)
154
			 for(int j = i; j < Q.Rows(); j++)
155
			 {
155
			 {
156
				 if(Q[i][j] > eps || Q[i][j] < -eps)
156
				 if(Q[i][j] > eps || Q[i][j] < -eps)
157
					 insert_entry(j,Q[i][j]);
157
					 insert_entry(j,Q[i][j]);
158
			 }
158
			 }
159
			 next_column();
159
			 next_column();
160
		 }
160
		 }
161
	}
161
	}
162
 
162
 
163
	void LoadDense(LinAlg::CMatrix Q, Type eps)
163
	void LoadDense(LinAlg::CMatrix Q, Type eps)
164
	{
164
	{
165
		 for(int i = 0; i < Q.Cols(); i++)
165
		 for(int i = 0; i < Q.Cols(); i++)
166
		 {
166
		 {
167
			 for(int j = 0; j < Q.Rows(); j++)
167
			 for(int j = 0; j < Q.Rows(); j++)
168
			 {
168
			 {
169
				 if(Q[i][j] > eps || Q[i][j] < -eps)
169
				 if(Q[i][j] > eps || Q[i][j] < -eps)
170
					 insert_entry(j,Q[i][j]);
170
					 insert_entry(j,Q[i][j]);
171
			 }
171
			 }
172
			 next_column();
172
			 next_column();
173
		 }
173
		 }
174
	}
174
	}
175
 
175
 
176
};
176
};
177
 
177
 
178
#endif
178
#endif
179
 
179
 
180

Generated by GNU Enscript 1.6.6.
180

Generated by GNU Enscript 1.6.6.
181
 
181
 
182
 
182
 
183
 
183