]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Add operators *= and /= to LAPACKFullMatrix 4046/head
authorNiklas Fehn <fehn@lnm.mw.tum.de>
Tue, 7 Mar 2017 10:41:42 +0000 (11:41 +0100)
committerNiklas Fehn <fehn@lnm.mw.tum.de>
Tue, 7 Mar 2017 10:42:40 +0000 (11:42 +0100)
include/deal.II/lac/lapack_full_matrix.h
source/lac/lapack_full_matrix.cc
tests/lapack/full_matrix_13.cc [new file with mode: 0644]
tests/lapack/full_matrix_13.output [new file with mode: 0644]

index ecf25ee85f6a525bd61edba0b2b90108bfd01e70..e31861687e80e28066aebb3cc1e2260e3112d385 100644 (file)
@@ -121,6 +121,18 @@ public:
   LAPACKFullMatrix<number> &
   operator = (const double d);
 
+  /**
+   * This operator multiplies all entries by a fixed factor.
+   */
+  LAPACKFullMatrix<number> &
+  operator*= (const number factor);
+
+  /**
+   * This operator divides all entries by a fixed factor.
+   */
+  LAPACKFullMatrix<number> &
+  operator/= (const number factor);
+
   /**
    * Assignment from different matrix classes, performing the usual conversion
    * to the transposed format expected by LAPACK. This assignment operator
index 29029c4ddc9930a1aca43b560bb94d16acfbf88d..7b46ef45a8273fdcb1185c7a7233bdb7c97d6ed2 100644 (file)
@@ -130,6 +130,41 @@ LAPACKFullMatrix<number>::operator = (const double d)
 }
 
 
+template <typename number>
+LAPACKFullMatrix<number> &
+LAPACKFullMatrix<number>::operator*= (const number factor)
+{
+  Assert(state == LAPACKSupport::matrix ||
+         state == LAPACKSupport::inverse_matrix,
+         ExcState(state));
+
+  for (unsigned int column = 0; column<this->n(); ++column)
+    for (unsigned int row = 0; row<this->m(); ++row)
+      (*this)(row,column) *= factor;
+
+  return *this;
+}
+
+
+template <typename number>
+LAPACKFullMatrix<number> &
+LAPACKFullMatrix<number>::operator/= (const number factor)
+{
+  Assert(state == LAPACKSupport::matrix ||
+         state == LAPACKSupport::inverse_matrix,
+         ExcState(state));
+
+  AssertIsFinite(factor);
+  Assert (factor != number(0.), ExcZero() );
+
+  for (unsigned int column = 0; column<this->n(); ++column)
+    for (unsigned int row = 0; row<this->m(); ++row)
+      (*this)(row,column) /= factor;
+
+  return *this;
+}
+
+
 template <typename number>
 void
 LAPACKFullMatrix<number>::vmult (
diff --git a/tests/lapack/full_matrix_13.cc b/tests/lapack/full_matrix_13.cc
new file mode 100644 (file)
index 0000000..cd649d1
--- /dev/null
@@ -0,0 +1,69 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2014 - 2015 by the deal.II authors
+//
+// This file is part of the deal.II library.
+//
+// The deal.II library is free software; you can use it, redistribute
+// it, and/or modify it under the terms of the GNU Lesser General
+// Public License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+// The full text of the license can be found in the file LICENSE at
+// the top level of the deal.II distribution.
+//
+// ---------------------------------------------------------------------
+
+
+// Tests LAPACKFullMatrix::operator*= and operator/=
+
+#include "../tests.h"
+#include <deal.II/base/logstream.h>
+#include <deal.II/lac/lapack_full_matrix.h>
+
+#include <fstream>
+#include <iostream>
+
+
+
+void test()
+{
+  const unsigned int m=7;
+  const unsigned int n=11;
+  LAPACKFullMatrix<double> A(m,n);
+  for (unsigned int i=0; i<m; ++i)
+    for (unsigned int j=0; j<n; ++j)
+      A(i,j) = (double)rand()/RAND_MAX;
+
+  LAPACKFullMatrix<double> A_check(A);
+
+  const double factor = 2.345;
+
+  // multiply by factor
+  A *= factor;
+
+  // test multiplication
+  for (unsigned int i=0; i<m; ++i)
+    for (unsigned int j=0; j<n; ++j)
+      AssertThrow(std::abs(A(i,j)-factor*A_check(i,j))<1.e-12,ExcInternalError());
+
+  // divide by factor
+  A /= factor;
+
+  // test division
+  for (unsigned int i=0; i<m; ++i)
+    for (unsigned int j=0; j<n; ++j)
+      AssertThrow(std::abs(A(i,j)-A_check(i,j))<1.e-12,ExcInternalError());
+
+  deallog << "OK" << std::endl;
+}
+
+int main()
+{
+  const std::string logname = "output";
+  std::ofstream logfile(logname.c_str());
+  logfile.precision(3);
+  deallog.attach(logfile);
+  deallog.threshold_double(1.e-10);
+
+  test();
+}
diff --git a/tests/lapack/full_matrix_13.output b/tests/lapack/full_matrix_13.output
new file mode 100644 (file)
index 0000000..0fd8fc1
--- /dev/null
@@ -0,0 +1,2 @@
+
+DEAL::OK

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.