]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
Add FullMatrix::trace
authorwolf <wolf@0785d39b-7218-0410-832d-ea1e28bc413d>
Fri, 16 Jun 2006 14:55:43 +0000 (14:55 +0000)
committerwolf <wolf@0785d39b-7218-0410-832d-ea1e28bc413d>
Fri, 16 Jun 2006 14:55:43 +0000 (14:55 +0000)
git-svn-id: https://svn.dealii.org/trunk@13268 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/doc/news/changes.html
deal.II/lac/include/lac/full_matrix.h
deal.II/lac/include/lac/full_matrix.templates.h
tests/lac/Makefile
tests/lac/trace.cc [new file with mode: 0644]
tests/lac/trace/.cvsignore [new file with mode: 0644]
tests/lac/trace/cmp/generic [new file with mode: 0644]

index e34397eaf9a6cb0a3de10255c531cf8f9e3cf7ca..97045cf77525f9ce2100d4cf11daa202845932ac 100644 (file)
@@ -520,12 +520,19 @@ inconvenience this causes.
 
 <ol>
   
+  <li> <p>New: There is now a function <code
+       class="member">FullMatrix::trace</code> that does what its name suggests
+       it does.
+       <br>
+       (WB 2006/06/16)
+       </p>
+  
   <li> <p>Improved: <code class="class">PointerMatrixAux</code> now has a
-  default constructor and a function for setting the <code
-  class="class">VectorMemory</code> object.
-  <br>
-  (GK 2006/06/14)
-  </p>
+       default constructor and a function for setting the <code
+       class="class">VectorMemory</code> object.
+       <br>
+       (GK 2006/06/14)
+       </p>
   
   <li> <p>Fixed: <code
        class="member">FullMatrix::print</code> would yield a link error
index 9aeddd4905ec222516b781acf4f213ce964c0282..165366640d04deab3409c61b604a650af24e41a8 100644 (file)
@@ -510,6 +510,16 @@ class FullMatrix : public Table<2,number>
                                       */
     double determinant () const;
 
+                                    /**
+                                     * Return the trace of the matrix,
+                                     * i.e. the sum of the diagonal values
+                                     * (which happens to also equal the sum
+                                     * of the eigenvalues of a matrix).
+                                      * Obviously, the matrix needs to
+                                      * be quadratic for this function.
+                                      */
+    double trace () const;
+    
                                     /**
                                      * Output of the matrix in
                                      * user-defined format.
index bb21adf9f5959586b8dfd826c39d85cf5ef19db5..89401b9064034ac0fd1b6fbfb681cd5661177d8b 100644 (file)
@@ -1920,6 +1920,25 @@ FullMatrix<number>::determinant () const
 }
 
 
+
+template <typename number>
+double
+FullMatrix<number>::trace () const
+{
+  Assert (!this->empty(), ExcEmptyMatrix());
+  
+  Assert (this->n_cols() == this->n_rows(),
+         ExcDimensionMismatch(this->n_cols(), this->n_rows()));
+
+  double tr = 0;
+  for (unsigned int i=0; i<this->n_rows(); ++i)
+    tr += this->el(i,i);
+
+  return tr;
+}
+
+
+
 template <typename number>
 number
 FullMatrix<number>::frobenius_norm () const
index 6018121e6370eb7828e1ae8fb2579e622ef31e57..8365ca85b167a225d811ac5cf8929d87e3524126 100644 (file)
@@ -28,7 +28,7 @@ tests_x = vector-vector \
        matrix_lib matrix_out \
        solver eigen \
        sparse_ilu sparse_ilu_t lapack lapack_fill block_minres \
-       identity_matrix*
+       identity_matrix* trace
 
 
 # from above list of regular expressions, generate the real set of
diff --git a/tests/lac/trace.cc b/tests/lac/trace.cc
new file mode 100644 (file)
index 0000000..adf2e51
--- /dev/null
@@ -0,0 +1,49 @@
+//---------------------------------------------------------------------------
+//    $Id$
+//    Version: $Name$ 
+//
+//    Copyright (C) 2005, 2006 by the deal.II authors
+//
+//    This file is subject to QPL and may not be  distributed
+//    without copyright and license information. Please refer
+//    to the file deal.II/doc/license.html for the  text  and
+//    further information on this license.
+//
+//---------------------------------------------------------------------------
+
+// check FullMatrix::trace
+
+
+#include "../tests.h"
+#include <base/logstream.h>
+#include <lac/full_matrix.h>
+
+#include <fstream>
+#include <iostream>
+#include <cmath>
+
+
+int main()
+{
+  std::ofstream logfile("trace/output");
+  logfile.setf(std::ios::fixed);
+  logfile.precision(0);
+  deallog.attach(logfile);
+  deallog.depth_console(0);
+  deallog.threshold_double(1.e-10);
+
+  const unsigned int N=20;
+  FullMatrix<double> m (N,N);
+
+  double tr = 0;
+  for (unsigned int i=0; i<N; ++i)
+    for (unsigned int j=0; j<N; ++j)
+      {
+       m(i,j) = i+j;
+       if (i==j)
+         tr += i+j;
+      }
+
+  deallog << "Trace=" << m.trace() << std::endl;
+  Assert (m.trace() == tr, ExcInternalError());
+}
diff --git a/tests/lac/trace/.cvsignore b/tests/lac/trace/.cvsignore
new file mode 100644 (file)
index 0000000..d196dfb
--- /dev/null
@@ -0,0 +1 @@
+obj.* exe OK output
diff --git a/tests/lac/trace/cmp/generic b/tests/lac/trace/cmp/generic
new file mode 100644 (file)
index 0000000..f3099f8
--- /dev/null
@@ -0,0 +1,2 @@
+
+DEAL::Trace=380.

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.