UblasCustomFunctions.cpp

00001 /*
00002 
00003 Copyright (c) 2005-2015, University of Oxford.
00004 All rights reserved.
00005 
00006 University of Oxford means the Chancellor, Masters and Scholars of the
00007 University of Oxford, having an administrative office at Wellington
00008 Square, Oxford OX1 2JD, UK.
00009 
00010 This file is part of Chaste.
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014  * Redistributions of source code must retain the above copyright notice,
00015    this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright notice,
00017    this list of conditions and the following disclaimer in the documentation
00018    and/or other materials provided with the distribution.
00019  * Neither the name of the University of Oxford nor the names of its
00020    contributors may be used to endorse or promote products derived from this
00021    software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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     //Check for symmetry
00065     if (norm_inf( rA - trans(rA)) > 10*DBL_EPSILON)
00066     {
00067         EXCEPTION("Matrix should be symmetric");
00068     }
00069 
00070     // Find the eigenvector by brute-force using the power method.
00071     // We can't use the inverse method, because the matrix might be singular
00072 
00073     c_matrix<double,3,3> copy_A(rA);
00074     //Eigenvalue 1
00075     c_vector<double, 3> eigenvec1 = scalar_vector<double>(3, 1.0);
00076 
00077     double eigen1 = CalculateMaxEigenpair(copy_A, eigenvec1);
00078 
00079     // Take out maximum eigenpair
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     // Take out maximum (second) eigenpair
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     //Look backwards through the eigenvalues, checking that they are non-zero
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) //Machine precision
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             //We don't care about a zero eigenvector, so don't polish it
00122             break;
00123         }
00124         step = norm_inf(rEigenvector-old_value);
00125     }
00126     return norm;
00127 }
00128 

Generated by  doxygen 1.6.2