From a6c5fa8e23111d4b26f41ca01cfc40555d91ce4d Mon Sep 17 00:00:00 2001
From: Oliver Sander <sander@igpm.rwth-aachen.de>
Date: Tue, 31 Aug 2010 12:42:24 +0000
Subject: [PATCH] compute the mixed second derivative of the squared distance
 function

[[Imported from SVN: r6293]]
---
 dune/gfe/unitvector.hh | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)

diff --git a/dune/gfe/unitvector.hh b/dune/gfe/unitvector.hh
index 66b2fad9..03e9b36a 100644
--- a/dune/gfe/unitvector.hh
+++ b/dune/gfe/unitvector.hh
@@ -149,6 +149,43 @@ public:
         return result;
     }
 
+    /** \brief Compute the mixed second derivate \partial d^2 / \partial da db
+
+    Unlike the distance itself the squared distance is differentiable at zero
+     */
+    static Dune::FieldMatrix<double,dim,dim> secondDerivativeOfDistanceSquaredWRTFirstAndSecondArgument(const UnitVector& a, const UnitVector& b) {
+
+        Dune::FieldMatrix<double,dim,dim> result;
+
+        double sp = a.data_ * b.data_;
+
+        // Compute vector A (see notes)
+        Dune::FieldMatrix<double,1,dim> row;
+        row[0] = b.globalCoordinates();
+        row *= secondDerivativeOfArcCosSquared(sp);
+
+        Dune::FieldMatrix<double,dim,1> column = b.projectOntoTangentSpace(a.globalCoordinates());
+
+        Dune::FieldMatrix<double,dim,dim> A;
+        // A = row * column
+        Dune::FMatrixHelp::multMatrix(column,row,A);
+
+        // Compute matrix B (see notes)
+        Dune::FieldMatrix<double,dim,dim> B;
+        for (int i=0; i<dim; i++)
+            for (int j=0; j<dim; j++)
+                B[i][j] = (i==j) - b.data_[i]*b.data_[j];
+
+        // Bring it all together
+        result = A;
+        result.axpy(derivativeOfArcCosSquared(sp), B);
+
+        for (int i=0; i<dim; i++)
+            result[i] = a.projectOntoTangentSpace(result[i]);
+
+        return result;
+    }
+    
     /** \brief Project tangent vector of R^n onto the tangent space */
     EmbeddedTangentVector projectOntoTangentSpace(const EmbeddedTangentVector& v) const {
         EmbeddedTangentVector result = v;
-- 
GitLab