]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
Add functions copy_from(FullMatrix) to sparsity pattern and sparse matrix.
authorwolf <wolf@0785d39b-7218-0410-832d-ea1e28bc413d>
Wed, 6 Feb 2002 09:03:47 +0000 (09:03 +0000)
committerwolf <wolf@0785d39b-7218-0410-832d-ea1e28bc413d>
Wed, 6 Feb 2002 09:03:47 +0000 (09:03 +0000)
git-svn-id: https://svn.dealii.org/trunk@5473 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/doc/news/2002/c-3-3.html
deal.II/lac/include/lac/sparse_matrix.2.templates
deal.II/lac/include/lac/sparse_matrix.h
deal.II/lac/include/lac/sparse_matrix.templates.h
deal.II/lac/include/lac/sparsity_pattern.h
deal.II/lac/source/sparsity_pattern.cc

index 082d134a6cb40a60b8126ed2cdf16e8d33bc1478..3798092656d87a79e07de0579a9c109258333890 100644 (file)
@@ -42,6 +42,13 @@ documentation, etc</a>.
 <h3>lac</h3>
 
 <ol>
+  <li> <p> New: Functions <code
+       class="member">SparsityPattern::copy_from</code> and <code
+       class="member">SparseMatrix::copy_from</code> allow to copy a
+       full matrix into a sparse matrix.
+       <br>
+       (WB 2002/02/06)
+       </p>
 </ol>
 
 
index 4608417283b84bfff1ceaeaef57663a04a461f5a..f2f37dfb7ffbe53b20269f752fc9aa1443b27522 100644 (file)
@@ -20,6 +20,9 @@
 template SparseMatrix<TYPEMAT> &
 SparseMatrix<TYPEMAT>::copy_from (const SparseMatrix<TYPE2> &);
 
+template 
+void SparseMatrix<TYPEMAT>::copy_from (const FullMatrix<TYPE2> &);
+
 template void SparseMatrix<TYPEMAT>::add_scaled (const TYPEMAT,
                                                 const SparseMatrix<TYPE2> &);
 
index 5eeacc57d56f2e12cbb5bdd91d730ca99fd2529b..ac6980f0af5182ffdaa6e66996490f501929110d 100644 (file)
@@ -21,6 +21,7 @@
 #include <lac/sparsity_pattern.h>
 
 template<typename number> class Vector;
+template<typename number> class FullMatrix;
 
 /**
  * Sparse matrix.
@@ -344,6 +345,19 @@ class SparseMatrix : public Subscriptor
     template <typename ForwardIterator>
     void copy_from (const ForwardIterator begin,
                    const ForwardIterator end);    
+
+                                    /**
+                                     * Copy the nonzero entries of a
+                                     * full matrix into this
+                                     * object. Previous content is
+                                     * deleted. Note that the
+                                     * underlying sparsity pattern
+                                     * must be appropriate to hold
+                                     * the nonzero entries of the
+                                     * full matrix.
+                                     */
+    template <typename somenumber>
+    void copy_from (const FullMatrix<somenumber> &matrix);
     
                                     /**
                                      * Add @p{matrix} scaled by
index 7998fb8c5d62eaf948fd90cf4b0260823d8a6186..12fcc7e0ff7378d1fb90c2a38f0e723d5e4cbcb9 100644 (file)
@@ -16,6 +16,7 @@
 
 #include <lac/sparse_matrix.h>
 #include <lac/vector.h>
+#include <lac/full_matrix.h>
 
 
 // we only need output streams, but older compilers did not provide
@@ -237,6 +238,24 @@ SparseMatrix<number>::copy_from (const SparseMatrix<somenumber> &matrix)
 };
 
 
+
+template <typename number>
+template <typename somenumber>
+void
+SparseMatrix<number>::copy_from (const FullMatrix<somenumber> &matrix)
+{
+                                  // first delete previous content
+  clear ();
+
+                                  // then copy old matrix
+  for (unsigned int row=0; row<matrix.m(); ++row)
+    for (unsigned int col=0; col<matrix.n(); ++col)
+      if (matrix(row,col) != 0)
+       set (row, col, matrix(row,col));
+};
+
+
+
 template <typename number>
 template <typename somenumber>
 void
index ba81481fd810d1b42777f7fa9ce916cbf16ffc19..0155c938e52bca09341ee0b208c1c9e25a86403d 100644 (file)
@@ -21,6 +21,7 @@
 #include <vector>
 
 
+template <typename number> class FullMatrix;
 template <typename number> class SparseMatrix;
 class CompressedSparsityPattern;
 
@@ -467,11 +468,27 @@ class SparsityPattern : public Subscriptor
                                     /**
                                      * Copy data from an object of
                                      * type
-                                     * @ref{CompressedSparsityPattern}. Previous
-                                     * content of this object is
-                                     * lost.
+                                     * @ref{CompressedSparsityPattern}.
+                                     * Previous content of this
+                                     * object is lost, and the
+                                     * sparsity pattern is in
+                                     * compressed mode afterwards.
                                      */
     void copy_from (const CompressedSparsityPattern &csp);
+
+                                    /**
+                                     * Take a full matrix and use its
+                                     * nonzero entries to generate a
+                                     * sparse matrix entry pattern
+                                     * for this object.
+                                     *
+                                     * Previous content of this
+                                     * object is lost, and the
+                                     * sparsity pattern is in
+                                     * compressed mode afterwards.
+                                     */
+    template <typename number>
+    void copy_from (const FullMatrix<number> &matrix);
     
                                     /**
                                      * Return whether the object is empty. It
index 2e7e608c3c4d9cce2550120a7438ef4d14da449b..0d26610f400fb8ade89adb428530f849a6ec6fcf 100644 (file)
@@ -13,6 +13,7 @@
 
 
 #include <lac/sparsity_pattern.h>
+#include <lac/full_matrix.h>
 #include <lac/compressed_sparsity_pattern.h>
 
 #include <iostream>
@@ -495,6 +496,29 @@ SparsityPattern::copy_from (const CompressedSparsityPattern &csp)
 };
 
 
+template <typename number>
+void SparsityPattern::copy_from (const FullMatrix<number> &matrix)
+{
+                                  // first init with the number of
+                                  // entries per row
+  std::vector<unsigned int> entries_per_row (matrix.m(), 0);
+  for (unsigned int row=0; row<matrix.m(); ++row)
+    for (unsigned int col=0; col<matrix.n(); ++col)
+      if (matrix(row,col) != 0)
+       ++entries_per_row[row];
+  reinit (matrix.m(), matrix.n(), entries_per_row);
+
+                                  // now set entries
+  for (unsigned int row=0; row<matrix.m(); ++row)
+    for (unsigned int col=0; col<matrix.n(); ++col)
+      if (matrix(row,col) != 0)
+       add (row,col);
+
+                                  // finally compress
+  compress ();
+};
+
+
 
 bool
 SparsityPattern::empty () const
@@ -726,3 +750,9 @@ SparsityPattern::memory_consumption () const
          max_dim * sizeof(unsigned int) +
          max_vec_len * sizeof(unsigned int));
 };
+
+
+
+// explicit instantiations
+template void SparsityPattern::copy_from (const FullMatrix<float> &);
+template void SparsityPattern::copy_from (const FullMatrix<double> &);

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.