13 #if !defined(KRATOS_SVD_UTILS )
14 #define KRATOS_SVD_UTILS
53 template<
class TDataType>
77 constexpr
static TDataType
ZeroTolerance = std::numeric_limits<double>::epsilon();
113 "type_svd" : "Jacobi",
117 default_parameters["tolerance"].
SetDouble(std::numeric_limits<double>::epsilon());
120 const std::string& r_type_svd = ThisParameters[
"type_svd"].
GetString();
121 const double tolerance = ThisParameters[
"tolerance"].
GetDouble();
145 const std::string& TypeSVD =
"Jacobi",
146 const TDataType Tolerance = std::numeric_limits<double>::epsilon(),
149 if (TypeSVD ==
"Jacobi") {
152 KRATOS_ERROR <<
"SVD Type not implemented" << std::endl;
175 const TDataType Tolerance = std::numeric_limits<double>::epsilon(),
180 KRATOS_ERROR_IF(
m !=
n) <<
"Current Jacobi implementation only works with square matrices. Use \'LinearSolversApplication\' decompositions instead." << std::endl;
182 if(SMatrix.size1() !=
m || SMatrix.size2() !=
n) {
185 noalias(SMatrix) = InputMatrix;
187 if(UMatrix.size1() !=
m || UMatrix.size2() !=
m) {
192 if(VMatrix.size1() !=
n || VMatrix.size2() !=
n) {
218 noalias(UMatrix) = auxiliar_matrix_m;
220 noalias(VMatrix) = auxiliar_matrix_n;
229 noalias(SMatrix) = auxiliar_matrix_mn;
231 noalias(UMatrix) = auxiliar_matrix_m;
236 if (iter > MaxIter) {
237 KRATOS_WARNING(
"JacobiSingularValueDecomposition") <<
"Maximum number of iterations " << MaxIter <<
" reached." << std::endl;
261 const TDataType
t = (InputMatrix(0, 1) - InputMatrix(1, 0))/(InputMatrix(0, 0) + InputMatrix(1, 1));
262 const TDataType
c = 1.0/std::sqrt(1.0 +
t*
t);
263 const TDataType s =
t*
c;
274 MatrixType auxiliar_matrix_m(UMatrix.size1(), UMatrix.size2());
276 noalias(UMatrix) = auxiliar_matrix_m;
295 if(SMatrix.size1() != 2 || SMatrix.size2() != 2) {
296 SMatrix.
resize(2 ,2,
false);
298 if(UMatrix.size1() != 2 || UMatrix.size2() != 2) {
299 UMatrix.
resize(2, 2,
false);
301 if(VMatrix.size1() != 2 || VMatrix.size2() != 2) {
302 VMatrix.
resize(2, 2,
false);
306 noalias(SMatrix) = InputMatrix;
310 const TDataType
w = InputMatrix(0, 0);
311 const TDataType
y = InputMatrix(1, 0);
312 const TDataType
z = InputMatrix(1, 1);
313 const TDataType ro = (
z -
w)/(2.0 *
y);
315 const TDataType
c = 1.0/(std::sqrt(1.0 +
t*
t));
316 const TDataType s =
t*
c;
329 z_matrix(0, 1) = 0.0;
330 z_matrix(1, 0) = 0.0;
336 noalias(UMatrix) = aux_2_2_matrix;
338 noalias(SMatrix) = aux_2_2_matrix;
340 if (SMatrix(0, 0) < SMatrix(1, 1)) {
342 p_matrix(0, 0) = 0.0;
343 p_matrix(0, 1) = 1.0;
344 p_matrix(1, 0) = 1.0;
345 p_matrix(1, 1) = 0.0;
348 noalias(UMatrix) = aux_2_2_matrix;
352 noalias(VMatrix) = aux_2_2_matrix;
377 b_matrix(0, 0) = InputMatrix(Index1, Index1);
378 b_matrix(0, 1) = InputMatrix(Index1, Index2);
379 b_matrix(1, 0) = InputMatrix(Index2, Index1);
380 b_matrix(1, 1) = InputMatrix(Index2, Index2);
387 J1(Index1, Index1) = u_matrix(0, 0);
388 J1(Index1, Index2) = u_matrix(1, 0);
389 J1(Index2, Index1) = u_matrix(0, 1);
390 J1(Index2, Index2) = u_matrix(1, 1);
393 J2(Index1, Index1) = v_matrix(0, 0);
394 J2(Index1, Index2) = v_matrix(1, 0);
395 J2(Index2, Index1) = v_matrix(0, 1);
396 J2(Index2, Index2) = v_matrix(1, 1);
417 b_matrix(0, 0) = InputMatrix(Index1, Index1);
418 b_matrix(0, 1) = 0.0;
419 b_matrix(1, 0) = InputMatrix(Index2, Index1);
420 b_matrix(1, 1) = 0.0;
427 J1(Index1, Index1) = u_matrix(0, 0);
428 J1(Index1, Index2) = u_matrix(1, 0);
429 J1(Index2, Index1) = u_matrix(0, 1);
430 J1(Index2, Index2) = u_matrix(1, 1);
442 const std::string TypeSVD =
"Jacobi",
443 const TDataType Tolerance = std::numeric_limits<double>::epsilon(),
449 const SizeType size_s = s_matrix.size1();
450 const TDataType condition_number = s_matrix(0, 0)/s_matrix(size_s - 1, size_s - 1);
452 return condition_number;
void resize(std::size_t NewSize1, std::size_t NewSize2, bool preserve=0)
Definition: amatrix_interface.h:224
static int Sign(const double &ThisDataType)
Sign function.
Definition: math_utils.h:1269
This class provides to Kratos a data structure for I/O based on the standard of JSON.
Definition: kratos_parameters.h:59
double GetDouble() const
This method returns the double contained in the current Parameter.
Definition: kratos_parameters.cpp:657
void SetDouble(const double Value)
This method sets the double contained in the current Parameter.
Definition: kratos_parameters.cpp:787
int GetInt() const
This method returns the integer contained in the current Parameter.
Definition: kratos_parameters.cpp:666
void RecursivelyValidateAndAssignDefaults(const Parameters &rDefaultParameters)
This function is designed to verify that the parameters under testing match the form prescribed by th...
Definition: kratos_parameters.cpp:1389
std::string GetString() const
This method returns the string contained in the current Parameter.
Definition: kratos_parameters.cpp:684
Various mathematical utilities to compute SVD and the condition number of a matrix.
Definition: svd_utils.h:55
static void Jacobi(MatrixType &J1, const MatrixType &InputMatrix, const SizeType &Size1, const SizeType &Index1, const SizeType &Index2)
This method computes the Jacobi rotation operation.
Definition: svd_utils.h:408
static TDataType SVDConditionNumber(const MatrixType &InputMatrix, const std::string TypeSVD="Jacobi", const TDataType Tolerance=std::numeric_limits< double >::epsilon(), const IndexType MaxIter=200)
This method computes the condition number using the SVD.
Definition: svd_utils.h:440
static std::size_t JacobiSingularValueDecomposition(const MatrixType &InputMatrix, MatrixType &UMatrix, MatrixType &SMatrix, MatrixType &VMatrix, const TDataType Tolerance=std::numeric_limits< double >::epsilon(), const IndexType MaxIter=200)
This function gives the Jacobi SVD of a given mxn matrix (m>=n), returns U,S; where A=U*S*V.
Definition: svd_utils.h:170
static void SingularValueDecomposition2x2(const MatrixType &InputMatrix, MatrixType &UMatrix, MatrixType &SMatrix, MatrixType &VMatrix)
This function gives the Jacobi SVD of a given 2x2 matrix, returns U,S; where A=U*S*V.
Definition: svd_utils.h:254
static void Jacobi(MatrixType &J1, MatrixType &J2, const MatrixType &InputMatrix, const SizeType &Size1, const SizeType &Size2, const SizeType &Index1, const SizeType &Index2)
This method computes the Jacobi rotation operation.
Definition: svd_utils.h:366
std::size_t SizeType
Definition of the size type.
Definition: svd_utils.h:68
Vector VectorType
Definition of the vector type.
Definition: svd_utils.h:65
UblasSpace< TDataType, Matrix, Vector > LocalSpaceType
Definition of local space.
Definition: svd_utils.h:74
static std::size_t SingularValueDecomposition(const MatrixType &InputMatrix, MatrixType &UMatrix, MatrixType &SMatrix, MatrixType &VMatrix, Parameters ThisParameters)
This function gives the SVD of a given mxn matrix (m>=n), returns U,S; where A=U*S*V.
Definition: svd_utils.h:103
Matrix MatrixType
Definition of the matrix type.
Definition: svd_utils.h:62
static void SingularValueDecomposition2x2Symmetric(const MatrixType &InputMatrix, MatrixType &UMatrix, MatrixType &SMatrix, MatrixType &VMatrix)
Definition: svd_utils.h:288
std::size_t IndexType
Definition of index type.
Definition: svd_utils.h:71
constexpr static TDataType ZeroTolerance
Definition of epsilon zero tolerance.
Definition: svd_utils.h:77
static std::size_t SingularValueDecomposition(const MatrixType &InputMatrix, MatrixType &UMatrix, MatrixType &SMatrix, MatrixType &VMatrix, const std::string &TypeSVD="Jacobi", const TDataType Tolerance=std::numeric_limits< double >::epsilon(), const IndexType MaxIter=200)
This function gives the SVD of a given mxn matrix (m>=n), returns U,S; where A=U*S*V.
Definition: svd_utils.h:140
A class template for handling data types, matrices, and vectors in a Ublas space.
Definition: ublas_space.h:121
static TDataType TwoNorm(VectorType const &rX)
||rX||2
Definition: ublas_space.h:278
static TDataType JacobiNorm(const Matrix &rA)
Definition: ublas_space.h:313
#define KRATOS_ERROR
Definition: exception.h:161
#define KRATOS_ERROR_IF(conditional)
Definition: exception.h:162
#define KRATOS_WARNING(label)
Definition: logger.h:265
z
Definition: GenerateWind.py:163
TSpaceType::IndexType Size1(TSpaceType &dummy, typename TSpaceType::MatrixType const &rM)
Definition: add_strategies_to_python.cpp:117
TSpaceType::IndexType Size2(TSpaceType &dummy, typename TSpaceType::MatrixType const &rM)
Definition: add_strategies_to_python.cpp:123
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
AMatrix::IdentityMatrix< double > IdentityMatrix
Definition: amatrix_interface.h:564
AMatrix::MatrixProductExpression< TExpression1Type, TExpression2Type > prod(AMatrix::MatrixExpression< TExpression1Type, TCategory1 > const &First, AMatrix::MatrixExpression< TExpression2Type, TCategory2 > const &Second)
Definition: amatrix_interface.h:568
T & noalias(T &TheMatrix)
Definition: amatrix_interface.h:484
AMatrix::TransposeMatrix< const T > trans(const T &TheMatrix)
Definition: amatrix_interface.h:486
y
Other simbols definition.
Definition: generate_axisymmetric_navier_stokes_element.py:54
w
Definition: generate_convection_diffusion_explicit_element.py:108
c
Definition: generate_weakly_compressible_navier_stokes_element.py:108
int max_iter
Definition: hinsberg_optimization.py:139
float J2
Definition: isotropic_damage_automatic_differentiation.py:133
int t
Definition: ode_solve.py:392
int n
manufactured solution and derivatives (u=0 at z=0 dudz=0 at z=domain_height)
Definition: ode_solve.py:402
int j
Definition: quadrature.py:648
int m
Definition: run_marine_rain_substepping.py:8
integer i
Definition: TensorModule.f:17