static void applyGaussian(matrix_t& m) { matrix_t::array_type& arr = m.data(); double sig = *std::max_element(arr.begin(), arr.end()); BOOST_FOREACH(double& v, arr) { v = std::exp(-v * v / (2 * sig * sig)); }
// LU factorization of a general matrix A. // Computes an LU factorization of a general M-by-N matrix A using // partial pivoting with row interchanges. Factorization has the form // A = P*L*U. // a (IN/OUT - matrix(M,N)) On entry, the coefficient matrix A to be factored. On exit, the factors L and U from the factorization A = P*L*U. // ipivot (OUT - vector(min(M,N))) Integer vector. The row i of A was interchanged with row IPIV(i). // info (OUT - int) // 0 : successful exit // < 0 : If INFO = -i, then the i-th argument had an illegal value. // > 0 : If INFO = i, then U(i,i) is exactly zero. The factorization has been completed, but the factor U is exactly singular, and division by zero will occur if it is used to solve a system of equations. int getrf (matrix_t& a, pivot_t& ipivot) { matrix_t::value_type* _a = a.data().begin(); int _m = int(a.size1()); int _n = int(a.size2()); int _lda = _m; // minor size int _info; rawLAPACK::getrf (_m, _n, _a, _lda, ipivot.data().begin(), _info); return _info; }
// Solution to a system using LU factorization // Solves a system of linear equations A*X = B with a general NxN // matrix A using the LU factorization computed by GETRF. // transa (IN - char) 'T' for the transpose of A, 'N' otherwise. // a (IN - matrix(M,N)) The factors L and U from the factorization A = P*L*U as computed by GETRF. // ipivot (IN - vector(min(M,N))) Integer vector. The pivot indices from GETRF; row i of A was interchanged with row IPIV(i). // b (IN/OUT - matrix(ldb,NRHS)) Matrix of same numerical type as A. On entry, the right hand side matrix B. On exit, the solution matrix X. // // info (OUT - int) // 0 : function completed normally // < 0 : The ith argument, where i = abs(return value) had an illegal value. // > 0 : if INFO = i, U(i,i) is exactly zero; the matrix is singular and its inverse could not be computed. int getrs (char transa, matrix_t& a, pivot_t& ipivot, matrix_t& b) { matrix_t::value_type* _a = a.data().begin(); int a_n = int(a.size1()); int _lda = a_n; int p_n = int(ipivot.size()); matrix_t::value_type* _b = b.data().begin(); int b_n = int(b.size1()); int _ldb = b_n; int _nrhs = int(b.size2()); /* B's size2 is the # of vectors on rhs */ if (a_n != b_n) /*Test to see if AX=B has correct dimensions */ return -101; if (p_n < a_n) /*Check to see if ipivot is big enough */ return -102; int _info; rawLAPACK::getrs (transa, a_n, _nrhs, _a, _lda, ipivot.data().begin(), _b, _ldb, _info); return _info; }
// QR Factorization of a MxN General Matrix A. // a (IN/OUT - matrix(M,N)) On entry, the coefficient matrix A. On exit , the upper triangle and diagonal is the min(M,N) by N upper triangular matrix R. The lower triangle, together with the tau vector, is the orthogonal matrix Q as a product of min(M,N) elementary reflectors. // tau (OUT - vector (min(M,N))) Vector of the same numerical type as A. The scalar factors of the elementary reflectors. // info (OUT - int) // 0 : function completed normally // < 0 : The ith argument, where i = abs(return value) had an illegal value. int geqrf (matrix_t& a, vector_t& tau) { int _m = int(a.size1()); int _n = int(a.size2()); int _lda = int(a.size1()); int _info; // make_sure tau's size is greater than or equal to min(m,n) if (int(tau.size()) < (_n<_m ? _n : _m) ) return -104; int ldwork = _n*_n; vector_t dwork(ldwork); rawLAPACK::geqrf (_m, _n, a.data().begin(), _lda, tau.data().begin(), dwork.data().begin(), ldwork, _info); return _info; }