UblasCustomFunctions.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "UblasCustomFunctions.hpp"
00037
00038 c_vector<double, 1> Create_c_vector(double x)
00039 {
00040 c_vector<double, 1> v;
00041 v[0] = x;
00042 return v;
00043 }
00044
00045 c_vector<double, 2> Create_c_vector(double x, double y)
00046 {
00047 c_vector<double, 2> v;
00048 v[0] = x;
00049 v[1] = y;
00050 return v;
00051 }
00052
00053 c_vector<double, 3> Create_c_vector(double x, double y, double z)
00054 {
00055 c_vector<double, 3> v;
00056 v[0] = x;
00057 v[1] = y;
00058 v[2] = z;
00059 return v;
00060 }
00061
00062 c_vector<double,3> CalculateEigenvectorForSmallestNonzeroEigenvalue(c_matrix<double, 3, 3>& rA)
00063 {
00064
00065 if (norm_inf( rA - trans(rA)) > 10*DBL_EPSILON)
00066 {
00067 EXCEPTION("Matrix should be symmetric");
00068 }
00069
00070
00071
00072
00073 c_matrix<double,3,3> copy_A(rA);
00074
00075 c_vector<double, 3> eigenvec1 = scalar_vector<double>(3, 1.0);
00076
00077 double eigen1 = CalculateMaxEigenpair(copy_A, eigenvec1);
00078
00079
00080 c_matrix<double, 3, 3> wielandt_reduce_first_vector = identity_matrix<double>(3,3);
00081 wielandt_reduce_first_vector -= outer_prod(eigenvec1, eigenvec1);
00082 copy_A = prod(wielandt_reduce_first_vector, copy_A);
00083
00084 c_vector<double, 3> eigenvec2 = scalar_vector<double>(3, 1.0);
00085 double eigen2 = CalculateMaxEigenpair(copy_A, eigenvec2);
00086
00087
00088 c_matrix<double, 3, 3> wielandt_reduce_second_vector = identity_matrix<double>(3,3);
00089 wielandt_reduce_second_vector -= outer_prod(eigenvec2, eigenvec2);
00090 copy_A = prod(wielandt_reduce_second_vector, copy_A);
00091
00092 c_vector<double, 3> eigenvec3 = scalar_vector<double>(3, 1.0);
00093 double eigen3 = CalculateMaxEigenpair(copy_A, eigenvec3);
00094
00095
00096 if (eigen3 >= DBL_EPSILON)
00097 {
00098 return eigenvec3;
00099 }
00100 if (eigen2 >= DBL_EPSILON)
00101 {
00102 return eigenvec2;
00103 }
00104 UNUSED_OPT(eigen1);
00105 assert( eigen1 > DBL_EPSILON);
00106 return eigenvec1;
00107 }
00108
00109 double CalculateMaxEigenpair(c_matrix<double, 3, 3>& rA, c_vector<double, 3>& rEigenvector)
00110 {
00111 double norm = 0.0;
00112 double step = DBL_MAX;
00113 while (step > DBL_EPSILON)
00114 {
00115 c_vector<double, 3> old_value(rEigenvector);
00116 rEigenvector = prod(rA, rEigenvector);
00117 norm = norm_2(rEigenvector);
00118 rEigenvector /= norm;
00119 if (norm < DBL_EPSILON)
00120 {
00121
00122 break;
00123 }
00124 step = norm_inf(rEigenvector-old_value);
00125 }
00126 return norm;
00127 }
00128