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 #include "UblasCustomFunctions.hpp"
00030
00031 c_vector<double, 1> Create_c_vector(double x)
00032 {
00033 c_vector<double, 1> v;
00034 v[0] = x;
00035 return v;
00036 }
00037
00038 c_vector<double, 2> Create_c_vector(double x, double y)
00039 {
00040 c_vector<double, 2> v;
00041 v[0] = x;
00042 v[1] = y;
00043 return v;
00044 }
00045
00046 c_vector<double, 3> Create_c_vector(double x, double y, double z)
00047 {
00048 c_vector<double, 3> v;
00049 v[0] = x;
00050 v[1] = y;
00051 v[2] = z;
00052 return v;
00053 }
00054
00055 c_vector<double,3> CalculateEigenvectorForSmallestNonzeroEigenvalue(c_matrix<double, 3, 3>& rA)
00056 {
00057 int info;
00058 c_vector<double, 3> eigenvalues_real_part;
00059 c_vector<double, 3> eigenvalues_imaginary_part;
00060 c_vector<double, 4*3 > workspace;
00061 c_matrix<double, 3, 3> right_eigenvalues;
00062
00063 char dont_compute_left_evectors = 'N';
00064 char compute_right_evectors = 'V';
00065
00066 int matrix_size = 3;
00067 int matrix_ld = matrix_size;
00068 int workspace_size = 4*matrix_size;
00069
00070 c_matrix<double, 3, 3> a_transpose;
00071 noalias(a_transpose) = trans(rA);
00072
00073
00074 LAPACKgeev_(&dont_compute_left_evectors, &compute_right_evectors,
00075 &matrix_size, a_transpose.data(),&matrix_ld,
00076 eigenvalues_real_part.data(), eigenvalues_imaginary_part.data(),
00077 NULL, &matrix_ld,
00078 right_eigenvalues.data(),&matrix_ld,
00079 workspace.data(),&workspace_size,
00080 &info);
00081 assert(info==0);
00082
00083
00084 assert(norm_2(eigenvalues_imaginary_part) < DBL_EPSILON);
00085
00086 unsigned index_of_smallest=UINT_MAX;
00087 double min_eigenvalue = DBL_MAX;
00088
00089 for (unsigned i=0; i<3; i++)
00090 {
00091 double eigen_magnitude = fabs(eigenvalues_real_part(i));
00092 if (eigen_magnitude < min_eigenvalue && eigen_magnitude >= DBL_EPSILON)
00093 {
00094
00095 min_eigenvalue = eigen_magnitude;
00096 index_of_smallest = i;
00097 }
00098 }
00099 assert (min_eigenvalue != DBL_MAX);
00100 assert (index_of_smallest != UINT_MAX);
00101 assert (min_eigenvalue >= DBL_EPSILON);
00102
00103 c_vector<double, 3> output;
00104 output(0) = right_eigenvalues(index_of_smallest, 0);
00105 output(1) = right_eigenvalues(index_of_smallest, 1);
00106 output(2) = right_eigenvalues(index_of_smallest, 2);
00107
00108 return output;
00109 }