blob: fe197dee40375eb9e0a9366b41433d5f8efb2789 [file] [edit]
// Copyright (c) 1997-1999 Matra Datavision
// Copyright (c) 1999-2014 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License version 2.1 as published
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
//
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.
// #ifndef OCCT_DEBUG
#define No_Standard_RangeError
#define No_Standard_OutOfRange
#define No_Standard_DimensionError
// #endif
#include <math_Matrix.hxx>
#include <math_Recipes.hxx>
#include <math_SVD.hxx>
#include <Standard_DimensionError.hxx>
#include <Standard_NotImplemented.hxx>
#include <StdFail_NotDone.hxx>
math_SVD::math_SVD(const math_Matrix& A)
: U(1, std::max(A.RowNumber(), A.ColNumber()), 1, A.ColNumber()),
V(1, A.ColNumber(), 1, A.ColNumber()),
Diag(1, A.ColNumber())
{
U.Init(0.0);
RowA = A.RowNumber();
U.Set(1, A.RowNumber(), 1, A.ColNumber(), A);
int Error = SVD_Decompose(U, Diag, V);
Done = Error == 0;
}
void math_SVD::Solve(const math_Vector& B, math_Vector& X, const double Eps)
{
StdFail_NotDone_Raise_if(!Done, " ");
Standard_DimensionError_Raise_if((RowA != B.Length()) || (X.Length() != Diag.Length()), " ");
math_Vector BB(1, U.RowNumber());
BB.Init(0.0);
BB.Set(1, B.Length(), B);
double wmin = Eps * Diag(Diag.Max());
for (int I = 1; I <= Diag.Upper(); I++)
{
if (Diag(I) < wmin)
{
Diag(I) = 0.0;
}
}
// Handle custom bounds in X vector - SVD_Solve expects 1-based indexing
if (X.Lower() != 1)
{
math_Vector anXTemp(&X.Value(X.Lower()), 1, X.Length());
SVD_Solve(U, Diag, V, BB, anXTemp);
}
else
{
SVD_Solve(U, Diag, V, BB, X);
}
}
void math_SVD::PseudoInverse(math_Matrix& Result, const double Eps)
{
int i, j;
StdFail_NotDone_Raise_if(!Done, " ");
double wmin = Eps * Diag(Diag.Max());
for (i = 1; i <= Diag.Upper(); i++)
{
if (Diag(i) < wmin)
{
Diag(i) = 0.0;
}
}
int ColA = Diag.Length();
math_Vector VNorme(1, U.RowNumber());
math_Vector Column(1, ColA);
for (j = 1; j <= RowA; j++)
{
for (i = 1; i <= VNorme.Upper(); i++)
{
VNorme(i) = 0.0;
}
VNorme(j) = 1.0;
SVD_Solve(U, Diag, V, VNorme, Column);
for (i = 1; i <= ColA; i++)
{
Result(i, j) = Column(i);
}
}
}
void math_SVD::Dump(Standard_OStream& o) const
{
o << "math_SVD";
if (Done)
{
o << " Status = Done \n";
}
else
{
o << " Status = not Done \n";
}
}