]> https://gitweb.dealii.org/ - dealii.git/commitdiff
conversion operator is written for table accessor
authorReza Rastak <rastak@stanford.edu>
Thu, 23 May 2019 07:17:15 +0000 (00:17 -0700)
committerReza Rastak <rastak@stanford.edu>
Sun, 26 May 2019 19:39:49 +0000 (12:39 -0700)
tests created for packing and unpacking a matrix

changelog added

doc/news/changes/minor/20190524RezaRastak [new file with mode: 0644]
include/deal.II/base/table.h
tests/lac/matrices_pack_unpack.cc [new file with mode: 0644]
tests/lac/matrices_pack_unpack.output [new file with mode: 0644]

diff --git a/doc/news/changes/minor/20190524RezaRastak b/doc/news/changes/minor/20190524RezaRastak
new file mode 100644 (file)
index 0000000..e3ab0d5
--- /dev/null
@@ -0,0 +1,3 @@
+Fix: FullMatrix can now be packed into std::vector and unpacked from it using std::copy and the begin() and end() iterators
+<br>
+(Reza Rastak, 2019/05/24)
index f2a0d001d6202cf9ef5013af938b7b4bd0cd1d28..6c5fcfea331ce92e0e5304b765deeb0756f32226 100644 (file)
@@ -918,6 +918,11 @@ namespace MatrixTableIterators
     const value_type &
     value() const;
 
+    /**
+     * Conversion operator that returns a constant reference to the element
+     */
+    operator const value_type &() const;
+
     /**
      * Return the row of the current entry.
      */
@@ -1042,6 +1047,11 @@ namespace MatrixTableIterators
      */
     value_type &
     value() const;
+
+    /**
+     * Conversion operator that returns a reference to the element
+     */
+    operator value_type &();
   };
 
   /**
@@ -2923,6 +2933,16 @@ namespace MatrixTableIterators
 
 
 
+  template <typename TableType, bool Constness, Storage storage_order>
+  inline AccessorBase<TableType, Constness, storage_order>::
+  operator const value_type &() const
+  {
+    assert_valid_linear_index();
+    return this->container->values[linear_index];
+  }
+
+
+
   template <typename TableType, bool Constness, Storage storage_order>
   inline typename AccessorBase<TableType, Constness, storage_order>::size_type
   AccessorBase<TableType, Constness, storage_order>::row() const
@@ -3011,6 +3031,15 @@ namespace MatrixTableIterators
 
 
 
+  template <typename TableType, Storage storage_order>
+  inline Accessor<TableType, false, storage_order>::operator value_type &()
+  {
+    this->assert_valid_linear_index();
+    return this->container->values[this->linear_index];
+  }
+
+
+
   template <typename TableType, bool Constness, Storage storage_order>
   inline Iterator<TableType, Constness, storage_order>::Iterator(
     const Accessor<TableType, Constness, storage_order> &a)
diff --git a/tests/lac/matrices_pack_unpack.cc b/tests/lac/matrices_pack_unpack.cc
new file mode 100644 (file)
index 0000000..a0a7e81
--- /dev/null
@@ -0,0 +1,44 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2005 - 2018 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.md at
+// the top level directory of deal.II.
+//
+// ---------------------------------------------------------------------
+
+// Copying from a matrix into std::vector and vice versa using iterators
+
+#include <deal.II/lac/full_matrix.h>
+
+#include "../tests.h"
+
+int
+main()
+{
+  initlog();
+
+  dealii::FullMatrix<double> A(3, 4);
+  std::iota(A.begin(), A.end(), 1.0);
+
+  deallog << "A =" << std::endl;
+  A.print(deallog);
+
+  std::vector<double> v(A.n_elements());
+  std::copy(A.begin(), A.end(), v.begin());
+  deallog << "v = " << std::endl;
+  deallog << v << std::endl;
+
+  dealii::FullMatrix<double> B(A.m(), A.n());
+  std::copy(v.cbegin(), v.cend(), B.begin());
+  deallog << "B =" << std::endl;
+  A.print(deallog);
+
+  return 0;
+}
\ No newline at end of file
diff --git a/tests/lac/matrices_pack_unpack.output b/tests/lac/matrices_pack_unpack.output
new file mode 100644 (file)
index 0000000..2c8d247
--- /dev/null
@@ -0,0 +1,11 @@
+
+DEAL::A =
+DEAL::1.0  2.0  3.0  4.0
+DEAL::5.0  6.0  7.0  8.0
+DEAL::9.0  10.  11.  12.
+DEAL::v =
+DEAL::1.00000 2.00000 3.00000 4.00000 5.00000 6.00000 7.00000 8.00000 9.00000 10.0000 11.0000 12.0000
+DEAL::B =
+DEAL::1.0  2.0  3.0  4.0
+DEAL::5.0  6.0  7.0  8.0
+DEAL::9.0  10.  11.  12.

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.