Chaste  Release::2018.1
UblasCustomFunctions.cpp
1 /*
2 
3 Copyright (c) 2005-2018, University of Oxford.
4 All rights reserved.
5 
6 University of Oxford means the Chancellor, Masters and Scholars of the
7 University of Oxford, having an administrative office at Wellington
8 Square, Oxford OX1 2JD, UK.
9 
10 This file is part of Chaste.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright notice,
17  this list of conditions and the following disclaimer in the documentation
18  and/or other materials provided with the distribution.
19  * Neither the name of the University of Oxford nor the names of its
20  contributors may be used to endorse or promote products derived from this
21  software without specific prior written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 
36 #include "UblasCustomFunctions.hpp"
37 
38 c_vector<double, 1> Create_c_vector(double x)
39 {
40  c_vector<double, 1> v;
41  v[0] = x;
42  return v;
43 }
44 
45 c_vector<double, 2> Create_c_vector(double x, double y)
46 {
47  c_vector<double, 2> v;
48  v[0] = x;
49  v[1] = y;
50  return v;
51 }
52 
53 c_vector<double, 3> Create_c_vector(double x, double y, double z)
54 {
55  c_vector<double, 3> v;
56  v[0] = x;
57  v[1] = y;
58  v[2] = z;
59  return v;
60 }
61 
62 c_vector<double, 3> CalculateEigenvectorForSmallestNonzeroEigenvalue(c_matrix<double, 3, 3>& rA)
63 {
64  //Check for symmetry
65  if (norm_inf(rA - trans(rA)) > 10 * DBL_EPSILON)
66  {
67  EXCEPTION("Matrix should be symmetric");
68  }
69 
70  // Find the eigenvector by brute-force using the power method.
71  // We can't use the inverse method, because the matrix might be singular
72 
73  c_matrix<double, 3, 3> copy_A(rA);
74  //Eigenvalue 1
75  c_vector<double, 3> eigenvec1 = scalar_vector<double>(3, 1.0);
76 
77  double eigen1 = CalculateMaxEigenpair(copy_A, eigenvec1);
78 
79  // Take out maximum eigenpair
80  c_matrix<double, 3, 3> wielandt_reduce_first_vector = identity_matrix<double>(3, 3);
81  wielandt_reduce_first_vector -= outer_prod(eigenvec1, eigenvec1);
82  copy_A = prod(wielandt_reduce_first_vector, copy_A);
83 
84  c_vector<double, 3> eigenvec2 = scalar_vector<double>(3, 1.0);
85  double eigen2 = CalculateMaxEigenpair(copy_A, eigenvec2);
86 
87  // Take out maximum (second) eigenpair
88  c_matrix<double, 3, 3> wielandt_reduce_second_vector = identity_matrix<double>(3, 3);
89  wielandt_reduce_second_vector -= outer_prod(eigenvec2, eigenvec2);
90  copy_A = prod(wielandt_reduce_second_vector, copy_A);
91 
92  c_vector<double, 3> eigenvec3 = scalar_vector<double>(3, 1.0);
93  double eigen3 = CalculateMaxEigenpair(copy_A, eigenvec3);
94 
95  //Look backwards through the eigenvalues, checking that they are non-zero
96  if (eigen3 >= DBL_EPSILON)
97  {
98  return eigenvec3;
99  }
100  if (eigen2 >= DBL_EPSILON)
101  {
102  return eigenvec2;
103  }
104  UNUSED_OPT(eigen1);
105  assert(eigen1 > DBL_EPSILON);
106  return eigenvec1;
107 }
108 
109 double CalculateMaxEigenpair(c_matrix<double, 3, 3>& rA, c_vector<double, 3>& rEigenvector)
110 {
111  double norm = 0.0;
112  double step = DBL_MAX;
113  while (step > DBL_EPSILON) //Machine precision
114  {
115  c_vector<double, 3> old_value(rEigenvector);
116  rEigenvector = prod(rA, rEigenvector);
117  norm = norm_2(rEigenvector);
118  rEigenvector /= norm;
119  if (norm < DBL_EPSILON)
120  {
121  //We don't care about a zero eigenvector, so don't polish it
122  break;
123  }
124  step = norm_inf(rEigenvector - old_value);
125  }
126  return norm;
127 }
c_vector< double, 3 > CalculateEigenvectorForSmallestNonzeroEigenvalue(c_matrix< double, 3, 3 > &rA)
#define EXCEPTION(message)
Definition: Exception.hpp:143
c_vector< double, 1 > Create_c_vector(double x)
#define UNUSED_OPT(var)
Definition: Exception.hpp:216
double CalculateMaxEigenpair(c_matrix< double, 3, 3 > &rA, c_vector< double, 3 > &rEigenvector)