summaryrefslogtreecommitdiff
blob: a95bc5bd9d497565eef55cd1734f8dc40c739929 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
69
70
71
72
73
74
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
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
//=====================================================
// Copyright (C) 2011 Andrea Arteaga <andyspiros@gmail.com>
//=====================================================
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program 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 General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
#define LAPACKFUNC(NAME) CAT(CAT(SCALAR_PREFIX,NAME),_)

template<> class lapack_interface<SCALAR> : public c_interface_base<SCALAR>
{
public:

	static inline std::string name()
	{
		return MAKE_STRING(LAPACKNAME);
	}

	static inline void general_solve(gene_matrix& A, gene_vector& b, gene_vector& x, int N)
	{
		int *ipiv = new int[N];
		int info;
		LAPACKFUNC(copy)(&N, b, &intone, x, &intone);
		LAPACKFUNC(gesv)(&N, &intone, A, &N, ipiv, x, &N, &info);
		delete[] ipiv;
	}

	static inline void least_squares(gene_matrix& A, gene_vector& b, gene_vector& x, int N)
	{
		int *ipiv = new int[N];
		int info;
		LAPACKFUNC(copy)(&N, b, &intone, x, &intone);
		SCALAR work1;
		int MONE = -1;
		LAPACKFUNC(gels)(&notrans, &N, &N, &intone, A, &N, x, &N, &work1, &MONE, &info);
		int lwork = (int)work1;
		SCALAR *work2 = new SCALAR[lwork];
		LAPACKFUNC(gels)(&notrans, &N, &N, &intone, A, &N, x, &N, work2, &lwork, &info);
		delete[] work2;
		delete[] ipiv;
	}

	static inline void lu_decomp(const gene_matrix& X, gene_matrix& C, int N)
	{
		int N2 = N*N;
		int *ipiv = new int[N];
		int info;
		LAPACKFUNC(copy)(&N2, X, &intone, C, &intone);
		LAPACKFUNC(getrf)(&N, &N, C, &N, ipiv, &info);
		delete[] ipiv;
	}

	static inline void cholesky(const gene_matrix& X, gene_matrix& C, int N)
	{
		int N2 = N*N;
		int info;
		LAPACKFUNC(copy)(&N2, X, &intone, C, &intone);
		LAPACKFUNC(potrf)(&lower, &N, C, &N, &info);
	}
	
	static inline void qr_decomp(const gene_matrix& X, gene_matrix& QR, gene_vector& tau, const int& N)
	{
		int N2 = N*N;
		LAPACKFUNC(copy)(&N2, X, &intone, QR, &intone);
		
		SCALAR *work = new SCALAR;
		int lwork = -1, info;
		LAPACKFUNC(geqrf)(&N, &N, QR, &N, tau, work, &lwork, &info);
		lwork = *work;
		delete work;
		work = new SCALAR[lwork];
		LAPACKFUNC(geqrf)(&N, &N, QR, &N, tau, work, &lwork, &info);
		delete[] work;
	}

	static inline void svd_decomp(const gene_matrix& X, gene_matrix& U, gene_vector& S, gene_matrix& VT, const int& N)
	{
		int N2 = N*N;
		stl_vector Xcopy(N2);
		LAPACKFUNC(copy)(&N2, X, &intone, &Xcopy[0], &intone);

		stl_vector work(1);
		int lwork = -1, info;
		LAPACKFUNC(gesvd)("A", "A", &N, &N, &Xcopy[0], &N, S, U, &N, VT, &N, &work[0], &lwork, &info);
		lwork = work[0];
		work.resize(lwork);
		LAPACKFUNC(gesvd)("A", "A", &N, &N, &Xcopy[0], &N, S, U, &N, VT, &N, &work[0], &lwork, &info);
	}

	static inline void syev(const gene_matrix& X, gene_matrix& V, gene_vector& W, const int& N)
	{
		int N2 = N*N;
		LAPACKFUNC(copy)(&N2, X, &intone, V, &intone);

		stl_vector work(1);
		int lwork = -1, info;
		LAPACKFUNC(syev)("V", "U", &N, V, &N, W, &work[0], &lwork, &info);
		lwork = work[0];
		work.resize(lwork);
		LAPACKFUNC(syev)("V", "U", &N, V, &N, W, &work[0], &lwork, &info);
	}

	/* Size of D, W: N; size of E: N-1, size of V: NxN */
	static inline void stev(const gene_vector& D, const gene_vector& E, gene_vector& W, gene_matrix& V, const int& N)
	{
		int N0 = N;
		int N1 = N-1;
		LAPACKFUNC(copy)(&N0, D, &intone, W, &intone);
		stl_vector E_(E, E+N-1), work(max(1, 2*N-2));

		int info;
		LAPACKFUNC(stev)("V", &N, W, &E_[0], V, &N, &work[0], &info);
	}

	static inline void symm_ev(const gene_matrix& X, gene_vector& W, int N)
	{
		char jobz = 'N';
		SCALAR *work = new SCALAR;
		int lwork = -1, info;
		LAPACKFUNC(syev)(&jobz, &lower, &N, X, &N, W, work, &lwork, &info);
		lwork = *work;
		delete work;
		work = new SCALAR[lwork];
		LAPACKFUNC(syev)(&jobz, &lower, &N, X, &N, W, work, &lwork, &info);
		delete[] work;
	}


};