]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
Add an add() function.
authorbangerth <bangerth@0785d39b-7218-0410-832d-ea1e28bc413d>
Wed, 27 Aug 2008 12:20:47 +0000 (12:20 +0000)
committerbangerth <bangerth@0785d39b-7218-0410-832d-ea1e28bc413d>
Wed, 27 Aug 2008 12:20:47 +0000 (12:20 +0000)
git-svn-id: https://svn.dealii.org/trunk@16673 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/lac/include/lac/trilinos_sparse_matrix.h
deal.II/lac/source/trilinos_sparse_matrix.cc

index bce08ab59cdafd01755cfe79ea3cc1121c3ef694..98b7da3c3f6f88187ef6d226079a93323de686a1 100755 (executable)
@@ -954,6 +954,15 @@ namespace TrilinosWrappers
                               const Vector &x,
                               const Vector &b) const;
 
+                                      /**
+                                       * Add <tt>matrix</tt> scaled by
+                                       * <tt>factor</tt> to this matrix,
+                                       * i.e. the matrix <tt>factor*matrix</tt>
+                                       * is added to <tt>this</tt>.
+                                       */
+      void add (const TrilinosScalar  factor,
+               const SparseMatrix   &matrix);
+
                                        /**
                                         * STL-like iterator with the
                                         * first entry.
@@ -1021,14 +1030,14 @@ namespace TrilinosWrappers
                                         */
       void write_ascii ();
       
-                                    /**
-                                     * Print the matrix to the given
-                                     * stream, using the format
-                                     * <tt>(line,col) value</tt>,
-                                     * i.e. one nonzero entry of the
-                                     * matrix per line.
-                                     */
-    void print (std::ostream &out) const;
+                                      /**
+                                       * Print the matrix to the given
+                                       * stream, using the format
+                                       * <tt>(line,col) value</tt>,
+                                       * i.e. one nonzero entry of the
+                                       * matrix per line.
+                                       */
+      void print (std::ostream &out) const;
     
                                         // TODO: Write an overloading
                                         // of the operator << for output.
index ef7c0ef74190885228703eb46c554a6c0a62b3d0..0dd2f390a80d17ac50c913ec81c17092392cf2ca 100755 (executable)
@@ -792,6 +792,54 @@ namespace TrilinosWrappers
 
 
 
+  void
+  SparseMatrix::add (const TrilinosScalar  factor,
+                    const SparseMatrix   &rhs)
+  {
+    Assert (rhs.m() == m(), ExcDimensionMismatch (rhs.m(), m()));
+    Assert (rhs.n() == n(), ExcDimensionMismatch (rhs.n(), n()));
+    
+                                    // I bet that there must be a better way
+                                    // to do this but it has not been found:
+                                    // currently, we simply go through each
+                                    // row of the argument matrix, copy it,
+                                    // scale it, and add it to the current
+                                    // matrix. that's probably not the most
+                                    // efficient way to do things.
+    const std::pair<unsigned int, unsigned int>
+      local_range = rhs.local_range();
+
+    unsigned int max_row_length = 0;
+    for (unsigned int row=local_range.first;
+        row < local_range.second; ++row)
+      max_row_length
+       = std::max (max_row_length,
+                   static_cast<unsigned int>(rhs.matrix->NumGlobalEntries(row)));
+    
+    std::vector<int>    column_indices (max_row_length);
+    std::vector<double> values (max_row_length);
+    
+    for (unsigned int row=local_range.first;
+        row < local_range.second; ++row)
+      {
+       int n_entries;
+       rhs.matrix->ExtractGlobalRowCopy (row, max_row_length,
+                                         n_entries,
+                                         &values[0],
+                                         &column_indices[0]);
+       for (int i=0; i<n_entries; ++i)
+         values[i] *= factor;
+
+       matrix->SumIntoGlobalValues (row, n_entries,
+                                    &values[0],
+                                    &column_indices[0]);
+      }
+
+    compress ();
+  }
+  
+
+
                                  // TODO: Currently this only flips
                                  // a flag that tells Trilinos that
                                  // any application should be done with

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.