b2api
B2000++ API Reference Manual, VERSION 4.6
 
Loading...
Searching...
No Matches
b2singular_value_decomposition.H
Go to the documentation of this file.
1//---------------------------------------------------------------------------
2// b2singular_value_decomposition.H --
3//
4// Singular value decomposition.
5//
6// written by Thomas Ludwig.
7//
8// Copyright (c) 2011-2012 SMR Engineering & Development SA
9// 2502 Bienne, Switzerland
10//
11// All Rights Reserved. Proprietary source code. The contents of
12// this file may not be disclosed to third parties, copied or
13// duplicated in any form, in whole or in part, without the prior
14// written permission of SMR.
15//---------------------------------------------------------------------------
16
17#ifndef __B2_SINGULAR_VALUE_DECOMPOSITION_H__
18#define __B2_SINGULAR_VALUE_DECOMPOSITION_H__
19
20#include <algorithm>
21#include <cmath>
22
23#include "b2eigen_decomposition.H"
24#include "b2tensor_calculus.H"
25
32namespace b2000 {
33
47inline void svd_3_3(const double A[3][3], double U[3][3], double S[3], double V[3][3]) {
48 // fprintf(stderr, "A=numpy.array([\n");
49 // fprintf(stderr, " [%.15g, %.15g, %.15g],\n",
50 // A[0][0], A[1][0], A[2][0]);
51 // fprintf(stderr, " [%.15g, %.15g, %.15g],\n",
52 // A[0][1], A[1][1], A[2][1]);
53 // fprintf(stderr, " [%.15g, %.15g, %.15g],\n",
54 // A[0][2], A[1][2], A[2][2]);
55 // fprintf(stderr, "])\n");
56
57 // Compute A^T * A.
58 double AA[3][3];
59 inner_product_3_3_TN(A, A, AA);
60 // fprintf(stderr, "AA=numpy.array([\n");
61 // fprintf(stderr, " [%.15g, %.15g, %.15g],\n",
62 // AA[0][0], AA[1][0], AA[2][0]);
63 // fprintf(stderr, " [%.15g, %.15g, %.15g],\n",
64 // AA[0][1], AA[1][1], AA[2][1]);
65 // fprintf(stderr, " [%.15g, %.15g, %.15g],\n",
66 // AA[0][2], AA[1][2], AA[2][2]);
67 // fprintf(stderr, "])\n");
68
69 // Compute sorted eigenvalues and eigenvectors of AA.
70 eigen_decomposition(AA, S);
71
72 // Make sure that eigenvalues are non-negative.
73 double s1 = std::max(0.0, S[0]);
74 double s2 = std::max(0.0, S[1]);
75 double s3 = std::max(0.0, S[2]);
76
77 // Store the square root of the sorted eigenvalues in S.
78 S[0] = std::sqrt(s1);
79 S[1] = std::sqrt(s2);
80 S[2] = std::sqrt(s3);
81
82 // Store the sorted eigenvectors in V^T.
83 std::copy(AA[0], AA[0] + 3 * 3, V[0]);
84 // fprintf(stderr, "V_svd=numpy.array([\n");
85 // fprintf(stderr, " [%.15g, %.15g, %.15g],\n",
86 // V[0][0], V[1][0], V[2][0]);
87 // fprintf(stderr, " [%.15g, %.15g, %.15g],\n",
88 // V[0][1], V[1][1], V[2][1]);
89 // fprintf(stderr, " [%.15g, %.15g, %.15g],\n",
90 // V[0][2], V[1][2], V[2][2]);
91 // fprintf(stderr, "])\n");
92
93 // Compute U * S = A * V^T.
94 inner_product_3_3_NT(A, V, U);
95 // fprintf(stderr, "AV=numpy.array([\n");
96 // fprintf(stderr, " [%.15g, %.15g, %.15g],\n",
97 // U[0][0], U[1][0], U[2][0]);
98 // fprintf(stderr, " [%.15g, %.15g, %.15g],\n",
99 // U[0][1], U[1][1], U[2][1]);
100 // fprintf(stderr, " [%.15g, %.15g, %.15g],\n",
101 // U[0][2], U[1][2], U[2][2]);
102 // fprintf(stderr, "])\n");
103
104 // Normalize the columns of U.
105 for (int i = 0; i != 3; ++i) {
106 const double s = 1.0 / std::sqrt(U[i][0] * U[i][0] + U[i][1] * U[i][1] + U[i][2] * U[i][2]);
107 // {
108 // const double ss =
109 // U[i][0] * U[i][0] + U[i][1] * U[i][1] + U[i][2] * U[i][2];
110 // fprintf(stderr, "i=%d ss=%.15g s=%.15g\n", i, ss, s);
111 // fprintf(stderr, "U[%d]=[%.15g,%.15g,%.15g]\n", i,
112 // U[i][0], U[i][1], U[i][2]);
113 // }
114 U[i][0] *= s;
115 U[i][1] *= s;
116 U[i][2] *= s;
117 }
118 // fprintf(stderr, "U_svd=numpy.array([\n");
119 // fprintf(stderr, " [%.15g, %.15g, %.15g],\n",
120 // U[0][0], U[1][0], U[2][0]);
121 // fprintf(stderr, " [%.15g, %.15g, %.15g],\n",
122 // U[0][1], U[1][1], U[2][1]);
123 // fprintf(stderr, " [%.15g, %.15g, %.15g],\n",
124 // U[0][2], U[1][2], U[2][2]);
125 // fprintf(stderr, "])\n");
126}
127
128}; // namespace b2000
129
130#endif /* __B2_SINGULAR_VALUE_DECOMPOSITION_H__ */
Contains the base classes for implementing Finite Elements.
Definition b2boundary_condition.H:32
void inner_product_3_3_NT(const T a[3][3], const T b[3][3], T c[3][3])
Definition b2tensor_calculus.H:848
void inner_product_3_3_TN(const T a[3][3], const T b[3][3], T c[3][3])
Definition b2tensor_calculus.H:823
void svd_3_3(const double A[3][3], double U[3][3], double S[3], double V[3][3])
Definition b2singular_value_decomposition.H:47