linear_solver.h 5.76 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1
2
3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
Pierre Kraemer's avatar
Pierre Kraemer committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
*                                                                              *
* This library is free software; you can redistribute it and/or modify it      *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your     *
* option) any later version.                                                   *
*                                                                              *
* This library is distributed in the hope that it will be useful, but WITHOUT  *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License  *
* for more details.                                                            *
*                                                                              *
* You should have received a copy of the GNU Lesser General Public License     *
* along with this library; if not, write to the Free Software Foundation,      *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
*                                                                              *
20
* Web site: http://cgogn.unistra.fr/                                           *
Pierre Kraemer's avatar
Pierre Kraemer committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

/*
 * This Linear Solver class extends the one found in Graphite,
 * developped by Bruno Levy, ALICE Project, LORIA, INRIA Lorraine
 */


#ifndef __LINEAR_SOLVER__
#define __LINEAR_SOLVER__

#include <vector>
#include <iostream>
#include <cassert>
#include <cstdlib>

39
#include <Eigen/Sparse>
40
#include "cholesky_solver.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68


template <class CoeffType>
class Variable {
public:
    Variable() : x_(0), index_(-1), locked_(false) { }
    double value() const { return x_; }
    void set_value(double x_in) { x_ = x_in ; }
    void lock()   { locked_ = true ; }
    void unlock() { locked_ = false ; }
    bool is_locked() const { return locked_ ; }
    unsigned int index() const {
        assert(index_ != -1) ;
        return (unsigned int)(index_) ;
    }
    void set_index(unsigned int index_in) {
        index_ = index_in ;
    }
private:
    CoeffType x_ ;
    int index_ ;
    bool locked_ ;
}  ;


/**
 * Solves a linear system or minimizes a quadratic form.
 */
69
template <typename CoeffType>
Pierre Kraemer's avatar
Pierre Kraemer committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
class LinearSolver {
protected:
    enum State {
        INITIAL, IN_SYSTEM, IN_ROW, CONSTRUCTED, SOLVED
    } ;

public:
	LinearSolver(unsigned int nb_variables) ;
	LinearSolver() ;
	~LinearSolver() {
		delete[] variable_ ;
		delete A_ ;
		delete x_ ;
		delete b_ ;
84
		delete direct_solver_ ;
Pierre Kraemer's avatar
Pierre Kraemer committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
	}

	// __________________ Parameters ________________________

	void set_direct(bool x) { direct_ = x ; }
	void set_least_squares(bool x) { least_squares_ = x ; }

	// __________________ Access ____________________________

	unsigned int nb_variables() const { return nb_variables_ ; }

	Variable<CoeffType>& variable(unsigned int idx) {
		assert(idx < nb_variables_) ;
		return variable_[idx] ;
	}

	const Variable<CoeffType>& variable(unsigned int idx) const {
		assert(idx < nb_variables_) ;
		return variable_[idx] ;
	}

	// _________________ Construction _______________________

	void begin_system() ;
	inline void begin_row() ;
	inline void set_right_hand_side(CoeffType b) ;
	inline void add_coefficient(unsigned int iv, CoeffType a) ;
	inline void normalize_row(CoeffType weight = 1) ;
	inline void scale_row(CoeffType s) ;
	inline void end_row() ;
	void end_system() ;

	// ----------------------------- Solver -------------------------------

	void solve() ;

	void reset(bool keep_matrix) ;

protected:

	// ----------- Converting between user representation and the internal representation -----

	void vector_to_variables() ;
	void variables_to_vector() ;

	// ----------- Finite state automaton (checks that calling sequence is respected) ---------

	std::string state_to_string(State s) {
		switch(s) {
			case INITIAL:
				return "initial" ;
			case IN_SYSTEM:
				return "in system" ;
			case IN_ROW:
				return "in row" ;
			case CONSTRUCTED:
				return "constructed" ;
			case SOLVED:
				return "solved" ;
		}
		// Should not go there.
		assert(false) ;
		return "undefined" ;
	}

	void check_state(State s) {
		if(state_ != s) {
			std::cerr << "Wrong state, expected: "
					<< state_to_string(s)
					<< " got:"
					<< state_to_string(state_)
					<< std::endl ;
			std::cerr << "exiting ..." << std::endl ;
		}
		assert(state_ == s) ;
	}

	void transition(State from, State to) {
		check_state(from) ;
		state_ = to ;
	}

private:

	// --------------- parameters --------------------------
	bool least_squares_ ;
	bool direct_ ;
	bool matrix_already_set_ ;

	// --------------- user representation --------------
	unsigned int nb_variables_ ;
	Variable<CoeffType>* variable_ ;

	// --------------- construction -----------------------
	State state_ ;
	unsigned int current_row_ ;
	std::vector<CoeffType> af_ ;
	std::vector<unsigned int> if_ ;
	std::vector<CoeffType> al_ ;
	std::vector<CoeffType> xl_ ;
	CoeffType bk_ ;

	// --------------- internal representation ---------
188
189
190
	Eigen::SparseMatrix<CoeffType>* A_ ;
	Eigen::Matrix<CoeffType, Eigen::Dynamic, 1>* x_ ;
	Eigen::Matrix<CoeffType, Eigen::Dynamic, 1>* b_ ;
Pierre Kraemer's avatar
Pierre Kraemer committed
191

192
	Solver_CHOLESKY<CoeffType>* direct_solver_ ;
Pierre Kraemer's avatar
Pierre Kraemer committed
193
194
195
196
197
} ;

#include "OpenNL/linear_solver.hpp"

#endif