// matrix/jama-svd.h
// Copyright 2009-2011 Microsoft Corporation
// See ../../COPYING for clarification regarding multiple authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABLITY OR NON-INFRINGEMENT.
// See the Apache 2 License for the specific language governing permissions and
// limitations under the License.
// This file consists of a port and modification of materials from
// JAMA: A Java Matrix Package
// under the following notice: This software is a cooperative product of
// The MathWorks and the National Institute of Standards and Technology (NIST)
// which has been released to the public. This notice and the original code are
// available at http://math.nist.gov/javanumerics/jama/domain.notice
#ifndef KALDI_MATRIX_JAMA_SVD_H_
#define KALDI_MATRIX_JAMA_SVD_H_ 1
#include "matrix/kaldi-matrix.h"
#include "matrix/sp-matrix.h"
#include "matrix/cblas-wrappers.h"
namespace kaldi {
#if defined(HAVE_ATLAS) || defined(USE_KALDI_SVD)
// using ATLAS as our math library, which doesn't have SVD -> need
// to implement it.
// This routine is a modified form of jama_svd.h which is part of the TNT distribution.
// (originally comes from JAMA).
/** Singular Value Decomposition.
* <P>
* For an m-by-n matrix A with m >= n, the singular value decomposition is
* an m-by-n orthogonal matrix U, an n-by-n diagonal matrix S, and
* an n-by-n orthogonal matrix V so that A = U*S*V'.
* <P>
* The singular values, sigma[k] = S(k, k), are ordered so that
* sigma[0] >= sigma[1] >= ... >= sigma[n-1].
* <P>
* The singular value decompostion always exists, so the constructor will
* never fail. The matrix condition number and the effective numerical
* rank can be computed from this decomposition.
* <p>
* (Adapted from JAMA, a Java Matrix Library, developed by jointly
* by the Mathworks and NIST; see http://math.nist.gov/javanumerics/jama).
*/
template<typename Real>
bool MatrixBase<Real>::JamaSvd(VectorBase<Real> *s_in,
MatrixBase<Real> *U_in,
MatrixBase<Real> *V_in) { // Destructive!
KALDI_ASSERT(s_in != NULL && U_in != this && V_in != this);
int wantu = (U_in != NULL), wantv = (V_in != NULL);
Matrix<Real> Utmp, Vtmp;
MatrixBase<Real> &U = (U_in ? *U_in : Utmp), &V = (V_in ? *V_in : Vtmp);
VectorBase<Real> &s = *s_in;
int m = num_rows_, n = num_cols_;
KALDI_ASSERT(m>=n && m != 0 && n != 0);
if (wantu) KALDI_ASSERT((int)U.num_rows_ == m && (int)U.num_cols_ == n);
if (wantv) KALDI_ASSERT((int)V.num_rows_ == n && (int)V.num_cols_ == n);
KALDI_ASSERT((int)s.Dim() == n); // n<=m so n is min.
int nu = n;
U.SetZero(); // make sure all zero.
Vector<Real> e(n);
Vector<Real> work(m);
MatrixBase<Real> &A(*this);
Real *adata = A.Data(), *workdata = work.Data(), *edata = e.Data(),
*udata = U.Data(), *vdata = V.Data();
int astride = static_cast<int>(A.Stride()),
ustride = static_cast<int>(U.Stride()),
vstride = static_cast<int>(V.Stride());
int i = 0, j = 0, k = 0;
// Reduce A to bidiagonal form, storing the diagonal elements
// in s and the super-diagonal elements in e.
int nct = std::min(m-1, n);
int nrt = std::max(0, std::min(n-2, m));
for (k = 0; k < std::max(nct, nrt); k++) {
if (k < nct) {
// Compute the transformation for the k-th column and
// place the k-th diagonal in s(k).
// Compute 2-norm of k-th column without under/overflow.
s(k) = 0;
for (i = k; i < m; i++) {
s(k) = hypot(s(k), A(i, k));
}
if (s(k) != 0.0) {
if (A(k, k) < 0.0) {
s(k) = -s(k);
}
for (i = k; i < m; i++) {
A(i, k) /= s(k);
}
A(k, k) += 1.0;
}
s(k) = -s(k);
}
for (j = k+1; j < n; j++) {
if ((k < nct) && (s(k) != 0.0)) {
// Apply the transformation.
Real t = cblas_Xdot(m - k, adata + astride*k + k, astride,
adata + astride*k + j, astride);
/*for (i = k; i < m; i++) {
t += adata[i*astride + k]*adata[i*astride + j]; // A(i, k)*A(i, j); // 3
}*/
t = -t/A(k, k);
cblas_Xaxpy(m - k, t, adata + k*astride + k, astride,
adata + k*astride + j, astride);
/*for (i = k; i < m; i++) {
adata[i*astride + j] += t*adata[i*astride + k]; // A(i, j) += t*A(i, k); // 5
}*/
}
// Place the k-th row of A into e for the
// subsequent calculation of the row transformation.
e(j) = A(k, j);
}
if (wantu & (k < nct)) {
// Place the transformation in U for subsequent back
// multiplication.
for (i = k; i < m; i++) {
U(i, k) = A(i, k);
}
}
if (k < nrt) {
// Compute the k-th row transformation and place the
// k-th super-diagonal in e(k).
// Compute 2-norm without under/overflow.
e(k) = 0;
for (i = k+1; i < n; i++) {
e(k) = hypot(e(k), e(i));
}
if (e(k) != 0.0) {
if (e(k+1) < 0.0) {
e(k) = -e(k);
}
for (i = k+1; i < n; i++) {
e(i) /= e(k);
}
e(k+1) += 1.0;
}
e(k) = -e(k);
if ((k+1 < m) & (e(k) != 0.0)) {
// Apply the transformation.
for (i = k+1; i < m; i++) {
work(i) = 0.0;
}
for (j = k+1; j < n; j++) {
for (i = k+1; i < m; i++) {
workdata[i] += edata[j] * adata[i*astride + j]; // work(i) += e(j)*A(i, j); // 5
}
}
for (j = k+1; j < n; j++) {
Real t(-e(j)/e(k+1));
cblas_Xaxpy(m - (k+1), t, workdata + (k+1), 1,
adata + (k+1)<