]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Implement copying and adding of sparse matrices.
authorWolfgang Bangerth <bangerth@math.tamu.edu>
Tue, 14 Apr 1998 16:16:26 +0000 (16:16 +0000)
committerWolfgang Bangerth <bangerth@math.tamu.edu>
Tue, 14 Apr 1998 16:16:26 +0000 (16:16 +0000)
git-svn-id: https://svn.dealii.org/trunk@172 0785d39b-7218-0410-832d-ea1e28bc413d

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

index 4162200eed509c1d9fec29ba777f954e2955c989..8dbfba49ffeaf98409522c01b3dd79524d280773 100644 (file)
@@ -40,32 +40,32 @@ class dSMatrixStruct
   bool compressed;
 
 public:
-  //////////
-  void reinit (const unsigned int m, const unsigned int n,
-              const unsigned int max_per_row);
                                     //////////
-  dSMatrixStruct (const unsigned int m, const unsigned int n,
-                 const unsigned int max_per_row);
+    void reinit (const unsigned int m, const unsigned int n,
+                const unsigned int max_per_row);
+                                    //////////
+    dSMatrixStruct (const unsigned int m, const unsigned int n,
+                   const unsigned int max_per_row);
     
                                     //////////
-  dSMatrixStruct (const unsigned int n, const unsigned int max_per_row);
+    dSMatrixStruct (const unsigned int n, const unsigned int max_per_row);
+                                    //////////
+    ~dSMatrixStruct ();
+                                    //////////
+    void compress ();
+                                    //////////
+    int operator() (const unsigned int i, const unsigned int j);
+                                    //////////
+    void add (const unsigned int i, const unsigned int j);
+                                    //////////
+    void add_matrix (const unsigned int n, const int* rowcols);
+                                    //////////
+    void add_matrix (const unsigned int m, const unsigned int n,
+                    const int* rows, const int* cols);
+                                    //////////
+    void add_matrix (const iVector& rowcols);
                                     //////////
-  ~dSMatrixStruct ();
-  //////////
-  void compress ();
-  //////////
-  int operator() (const unsigned int i, const unsigned int j);
-  //////////
-  void add (const unsigned int i, const unsigned int j);
-  //////////
-  void add_matrix (const unsigned int n, const int* rowcols);
-  //////////
-  void add_matrix (const unsigned int m, const unsigned int n,
-                  const int* rows, const int* cols);
-  //////////
-  void add_matrix (const iVector& rowcols);
-  //////////
-  void add_matrix (const iVector& rows, const iVector& cols);
+    void add_matrix (const iVector& rows, const iVector& cols);
 
     void print_gnuplot (ostream &) const;
     unsigned int n_rows () const;
@@ -186,13 +186,51 @@ class dSMatrix
                                     //
     unsigned int n () const;
 
-                                    //
+                                    /**
+                                     * Set the element #(i,j)# to #value#.
+                                     * Throws an error if the entry does
+                                     * not exist.
+                                     */
     void set (const unsigned int i, const unsigned int j,
              const double value);
-                                    //
+    
+                                    /**
+                                     * Add #value# to the element #(i,j)#.
+                                     * Throws an error if the entry does
+                                     * not exist.
+                                     */
     void add (const unsigned int i, const unsigned int j,
              const double value);
 
+                                    /**
+                                     * Copy the given matrix to this one.
+                                     * The operation throws an error if the
+                                     * sparsity patterns of the two involved
+                                     * matrices do not point to the same
+                                     * object, since in this case the copy
+                                     * operation is cheaper. Since this
+                                     * operation is notheless not for free,
+                                     * we do not make it available through
+                                     * #operator =#, since this may lead
+                                     * to unwanted usage, e.g. in copy
+                                     * arguments to functions, which should
+                                     * really be arguments by reference.
+                                     *
+                                     * The function returns a reference to
+                                     * #this#.
+                                     */
+    dSMatrix & copy_from (const dSMatrix &);
+
+                                    /**
+                                     * Add #matrix# scaled by #factor# to this
+                                     * matrix. The function throws an error
+                                     * if the sparsity patterns of the two
+                                     * involved matrices do not point to the
+                                     * same object, since in this case the
+                                     * operation is cheaper.
+                                     */
+    void add_scaled (const double factor, const dSMatrix &matrix);
+    
                                     /**
                                      * Return the value of the entry (i,j).
                                      * This may be an expensive operation
@@ -267,9 +305,20 @@ class dSMatrix
                                     //
     void precondition (dVector& dst, const dVector& src) { dst=src; }
 
+                                    /**
+                                     * Return a (constant) reference to the
+                                     * underlying sparsity pattern of this
+                                     * matrix.
+                                     */
     const dSMatrixStruct & get_sparsity_pattern () const;
-    
-    void print (ostream &) const;
+
+                                    /**
+                                     * Print the matrix to the given stream,
+                                     * using the format
+                                     * #(line,col) value#, i.e. one
+                                     * nonzero entry of the matrix per line.
+                                     */
+    void print (ostream &out) const;
 
                                     /**
                                      * Exception
@@ -303,6 +352,10 @@ class dSMatrix
                                      * Exception
                                      */
     DeclException0 (ExcMatrixNotSquare);
+                                    /**
+                                     * Exception
+                                     */
+    DeclException0 (ExcDifferentSparsityPatterns);
 };
 
 
index 32c29d75bbf9d00a63f4dc7c7407cb58dc994f84..7c73da5cfe4a7f7cb02c6e61fb65af1f2ee4ea1b 100644 (file)
@@ -406,6 +406,30 @@ dSMatrix::clear () {
 
 
 
+dSMatrix &
+dSMatrix::copy_from (const dSMatrix &matrix) {
+  Assert (cols != 0, ExcMatrixNotInitialized());
+  Assert (cols == matrix.cols, ExcDifferentSparsityPatterns());
+
+  for (unsigned int i = 0 ; i<cols->vec_len; i++)
+    val[i] = matrix.val[i];
+
+  return *this;
+};
+
+
+
+void
+dSMatrix::add_scaled (const double factor, const dSMatrix &matrix) {
+  Assert (cols != 0, ExcMatrixNotInitialized());
+  Assert (cols == matrix.cols, ExcDifferentSparsityPatterns());
+
+  for (unsigned int i = 0 ; i<cols->vec_len; i++)
+    val[i] += factor*matrix.val[i];
+};
+
+
+
 void
 dSMatrix::vmult (dVector& dst, const dVector& src) const
 {

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.