16 #ifdef USE_CLAPACK_INTERFACE 23 int *nrhs,
double *a,
int *lda,
24 double *af,
int *ldaf,
int *ipiv,
25 char *equed,
double *r,
double *
c,
26 double *
b,
int *ldb,
double *
x,
27 int *ldx,
double *rcond,
double *ferr,
28 double *berr,
double *work,
int *iwork,
31 extern "C" void dgesv_(
const int*
n,
const int* nrhs,
32 double* a,
int* lda,
int* ipiv,
33 double*
b,
int* ldb,
int*
info);
35 extern "C" void dgetrf_(
int *m,
int *
n,
double *a,
int *lda,
int *ipiv,
int *
info);
38 extern "C" void dgesvd_(
char const* jobu,
char const* jobvt,
39 int const* m,
int const*
n,
double* a,
int const* lda,
40 double* s,
double* u,
int const* ldu,
41 double* vt,
int const* ldvt,
42 double* work,
int const* lwork,
int*
info);
44 extern "C" void dgeev_(
char const*jobvl,
char const*jobvr,
int *
n,
double *
A,
45 int *lda,
double *wr,
double *wi,
double *vl,
46 int *ldvl,
double *vr,
int *ldvr,
double *work,
int *lwork,
int *
info);
54 static inline int max(
int a,
int b) {
return (a >= b) ? a :
b; }
55 static inline int min(
int a,
int b) {
return (a <= b) ? a :
b; }
60 assert(a.rows() == a.cols() && a.cols() == b.rows() );
64 const int n = a.rows();
65 const int nrhs = b.cols();
67 std::vector<int> ipiv(n);
69 #ifndef USE_CLAPACK_INTERFACE 73 dgesv_(&n, &nrhs, &(a(0,0)), &lda, &(ipiv[0]), &(out_x(0,0)), &ldb, &info);
75 info = clapack_dgesv(CblasColMajor,
76 n, nrhs, &(a(0,0)), n,
93 assert(_a.cols() == _a.rows() && _a.cols() == _b.size() );
95 int n = (
int)_a.cols();
100 std::vector<int> ipiv(n);
107 #ifndef USE_CLAPACK_INTERFACE 109 char transpose =
'N';
111 double *af =
new double[n*
n];
117 double *r =
new double[
n];
118 double *
c =
new double[
n];
124 double *ferr =
new double[nrhs];
125 double *berr =
new double[nrhs];
126 double *work =
new double[4*
n];
128 int *iwork =
new int[
n];
131 dgesvx_(&fact, &transpose, &n, &nrhs, const_cast<double *>(&(_a(0,0))), &lda, af, &ldaf, &(ipiv[0]),
132 &equed, r, c, const_cast<double *>(&(_b(0))), &ldb, &(_x(0)), &ldx, &rcond,
133 ferr, berr, work, iwork, &info);
145 info = clapack_dgesv(CblasColMajor,
146 n, nrhs, const_cast<double *>(&(
a(0,0))), lda, &(ipiv[0]),
159 const int m = _a.rows();
160 const int n = _a.cols();
161 assert( m == static_cast<int>(_b.size()) );
168 int max_mn =
max(m,n);
169 int min_mn =
min(m,n);
175 double *s =
new double[max_mn];
177 double *u =
new double[ldu*m];
179 double *vt =
new double[ldvt*
n];
181 int lwork =
max(3*min_mn+max_mn, 5*min_mn);
182 double *work =
new double[lwork];
185 for(i = 0; i < max_mn; i++) s[i] = 0.0;
187 dgesvd_(&jobu, &jobvt, &m, &n, &(a(0,0)), &lda, s, u, &ldu, vt, &ldvt, work,
192 double smin, smax=0.0;
193 for (j = 0; j < min_mn; j++) if (s[j] > smax) smax = s[j];
194 smin = smax*_sv_ratio;
195 for (j = 0; j < min_mn; j++)
if (s[j] < smin) s[j] = 0.0;
197 double *utb =
new double[m];
199 for (j = 0; j < m; j++){
202 for (i = 0; i < m; i++) tmp += u[j*m+i] * _b(i);
209 for (j = 0; j <
n; j++){
211 for (i = 0; i <
n; i++){
212 if(s[i]) tmp += utb[
i] * vt[j*n+
i];
232 if(_a.cols() == _a.rows())
248 int m = (
int)_a.rows();
249 int n = (
int)_a.cols();
250 int max_mn =
max(m,n);
251 int min_mn =
min(m,n);
257 double *s =
new double[max_mn];
259 double *u =
new double[ldu*m];
261 double *vt =
new double[ldvt*
n];
262 int lwork =
max(3*min_mn+max_mn, 5*min_mn);
263 double *work =
new double[lwork];
266 for(i = 0; i < max_mn; i++) s[i] = 0.0;
268 dgesvd_(&jobu, &jobvt, &m, &n, &(a(0,0)), &lda, s, u, &ldu, vt, &ldvt, work,
272 double smin, smax=0.0;
273 for (j = 0; j < min_mn; j++) if (s[j] > smax) smax = s[j];
274 smin = smax*_sv_ratio;
275 for (j = 0; j < min_mn; j++)
if (s[j] < smin) s[j] = 0.0;
279 for (j = 0; j < m; j++){
281 for (i = 0; i < m; i++) u[j*m+i] /= s[j];
284 for (i = 0; i < m; i++) u[j*m+i] = 0.0;
290 for(j = 0; j <
n; j++){
291 for(i = 0; i < m; i++){
293 for(k = 0; k < min_mn; k++){
294 if(s[k]) _a_pseu(j,i) += vt[j*n+k] * u[k*m+
i];
310 assert( _a.cols() == _a.rows() );
316 mlapack evec = _evec;
317 vlapack eval = _eval;
319 int n = (
int)_a.cols();
321 double *wi =
new double[
n];
322 double *vl =
new double[n*
n];
323 double *work =
new double[4*
n];
328 dgeev_(
"N",
"V", &n, &(a(0,0)), &n, &(eval(0)), wi, vl, &n, &(evec(0,0)), &n, work, &lwork, &info);
330 _evec = evec.transpose();
349 const int c = _a.rows();
350 const int n = _a.cols();
352 if ( _w.cols() != n || _w.rows() !=
n ) {
353 _w = dmatrix::Identity(n, n);
358 a1 = (_a * _w * at + _sr_ratio * dmatrix::Identity(c,c)).
inverse();
362 _a_sr = _w * at * a1;
370 assert( _a.cols() == _a.rows() );
378 std::vector<int> ipiv(n);
380 #ifdef USE_CLAPACK_INTERFACE 381 info = clapack_dgetrf(CblasColMajor,
382 n, n, &(a(0,0)), lda, &(ipiv[0]));
384 dgetrf_(&n, &n, &a(0,0), &lda, &(ipiv[0]), &info);
389 for(
int i=0;
i < n-1;
i++)
390 if(ipiv[
i] !=
i+1) det = -
det;
392 for(
int i=0;
i <
n;
i++) det *= a(
i,
i);
int solveLinearEquationLU(const dmatrix &_a, const dvector &_b, dvector &_x)
static int min(int a, int b)
int solveLinearEquationSVD(const dmatrix &_a, const dvector &_b, dvector &_x, double _sv_ratio)
void dgetrf_(int *m, int *n, double *a, int *lda, int *ipiv, int *info)
void dgesvd_(char const *jobu, char const *jobvt, int const *m, int const *n, double *a, int const *lda, double *s, double *u, int const *ldu, double *vt, int const *ldvt, double *work, int const *lwork, int *info)
dmatrix inverse(const dmatrix &M)
png_infop png_bytep * trans
double det(const dmatrix &_a)
def j(str, encoding="cp932")
int calcSRInverse(const dmatrix &_a, dmatrix &_a_sr, double _sr_ratio, dmatrix _w)
int calcPseudoInverse(const dmatrix &_a, dmatrix &_a_pseu, double _sv_ratio)
void dgeev_(char const *jobvl, char const *jobvr, int *n, double *A, int *lda, double *wr, double *wi, double *vl, int *ldvl, double *vr, int *ldvr, double *work, int *lwork, int *info)
void dgesv_(const int *n, const int *nrhs, double *a, int *lda, int *ipiv, double *b, int *ldb, int *info)
int solveLinearEquation(const dmatrix &_a, const dvector &_b, dvector &_x, double _sv_ratio)
void dgesvx_(char *fact, char *trans, int *n, int *nrhs, double *a, int *lda, double *af, int *ldaf, int *ipiv, char *equed, double *r, double *c, double *b, int *ldb, double *x, int *ldx, double *rcond, double *ferr, double *berr, double *work, int *iwork, int *info)
int calcEigenVectors(const dmatrix &_a, dmatrix &_evec, dvector &_eval)
static int max(int a, int b)