]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
additional functions in SparseMatrixEZ
authorguido <guido@0785d39b-7218-0410-832d-ea1e28bc413d>
Thu, 3 Oct 2002 08:29:22 +0000 (08:29 +0000)
committerguido <guido@0785d39b-7218-0410-832d-ea1e28bc413d>
Thu, 3 Oct 2002 08:29:22 +0000 (08:29 +0000)
git-svn-id: https://svn.dealii.org/trunk@6596 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/lac/include/lac/sparse_matrix_ez.h
deal.II/lac/include/lac/sparse_matrix_ez.templates.h
tests/lac/sparse_matrices.cc
tests/lac/sparse_matrices.checked

index 637ccad61b90494d8f1ad727af5abcc13bd0aa98..c892790bd8466deae96100751c13d3dce0e1c50d 100644 (file)
@@ -463,9 +463,9 @@ class SparseMatrixEZ : public Subscriptor
                                      * The function returns a reference to
                                      * @p{this}.
                                      */
-//    template <typename somenumber>
-//    SparseMatrix<number> &
-//    copy_from (const SparseMatrix<somenumber> &source);
+    template <class MATRIX>
+    SparseMatrixEZ<number> &
+    copy_from (const MATRIX &source);
 
                                     /**
                                      * This function is complete
@@ -518,22 +518,18 @@ class SparseMatrixEZ : public Subscriptor
     
                                     /**
                                      * Add @p{matrix} scaled by
-                                     * @p{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.
+                                     * @p{factor} to this matrix.
                                      *
-                                     * The source matrix may be a matrix
-                                     * of arbitrary type, as long as its
-                                     * data type is convertible to the
-                                     * data type of this matrix.
+                                     * The source matrix may be a
+                                     * matrix of arbitrary type, as
+                                     * long as its data type is
+                                     * convertible to the data type
+                                     * of this matrix and it has the
+                                     * standard @p{const_iterator}.
                                      */
-//    template <typename somenumber>
-//    void add_scaled (const number factor,
-//                  const SparseMatrix<somenumber> &matrix);
+    template <class MATRIX>
+    void add_scaled (const number factor,
+                    const MATRIX &matrix);
     
                                     /**
                                      * Return the value of the entry
@@ -658,6 +654,11 @@ class SparseMatrixEZ : public Subscriptor
     template <typename somenumber>
     somenumber matrix_scalar_product (const Vector<somenumber> &u,
                                      const Vector<somenumber> &v) const;
+
+                                    /**
+                                     * Frobenius-norm of the matrix.
+                                     */
+    number l2_norm () const;
     
                                     /**
                                      * Return the l1-norm of the matrix, that is
@@ -670,7 +671,7 @@ class SparseMatrixEZ : public Subscriptor
                                      * $|Mv|_1\leq |M|_1 |v|_1$.
                                      * (cf. Haemmerlin-Hoffmann : Numerische Mathematik)
                                      */
-    number l1_norm () const;
+//    number l1_norm () const;
 
                                     /**
                                      * Return the linfty-norm of the
@@ -684,7 +685,7 @@ class SparseMatrixEZ : public Subscriptor
                                      * $|Mv|_infty \leq |M|_infty |v|_infty$.
                                      * (cf. Haemmerlin-Hoffmann : Numerische Mathematik)
                                      */
-    number linfty_norm () const;
+//    number linfty_norm () const;
 
                                     /**
                                      * Apply the Jacobi
@@ -1332,5 +1333,44 @@ SparseMatrixEZ<number>::end (unsigned int r) const
   return const_iterator(this, r+1, 0);
 }
 
+template<typename number>
+template <class MATRIX>
+inline
+SparseMatrixEZ<number>&
+SparseMatrixEZ<number>::copy_from (const MATRIX& M)
+{
+  reinit(M.m(), M.n());
+  
+  typename MATRIX::const_iterator start = M.begin();
+  const typename MATRIX::const_iterator final = M.end();
+
+  while (start != final)
+    {
+      set(start->row(), start->column(), start->value());
+      ++start;
+    }
+  return *this;
+}
+
+template<typename number>
+template <class MATRIX>
+inline
+void
+SparseMatrixEZ<number>::add_scaled (number factor,
+                                   const MATRIX& M)
+{
+  Assert (M.m() == m(), ExcDimensionMismatch(M.m(), m()));
+  Assert (M.n() == n(), ExcDimensionMismatch(M.n(), n()));
+  
+  typename MATRIX::const_iterator start = M.begin();
+  const typename MATRIX::const_iterator final = M.end();
+
+  while (start != final)
+    {
+      add(start->row(), start->column(), factor * start->value());
+      ++start;
+    }
+}
+
 #endif
 /*----------------------------   sparse_matrix.h     ---------------------------*/
index f54b66ec1da8cfce5fa3f71e745d00732638bd90..87ab3794b947e04957fbd6878f1858534c77c3e2 100644 (file)
@@ -113,6 +113,25 @@ SparseMatrixEZ<number>::vmult (Vector<somenumber>& dst,
 }
 
 
+template <typename number>
+number
+SparseMatrixEZ<number>::l2_norm () const
+{
+  number sum = 0.;
+  const_iterator start = begin();
+  const_iterator final = end();
+
+  while (start != final)
+    {
+      const double value = start->value();
+      sum += value*value;
+      ++start;
+    }
+  return std::sqrt(sum);
+}
+
+
+
 template <typename number>
 template <typename somenumber>
 void
index ccfab17f9397066a9de234e5b54227075b2cfff2..d67c37ae5f561fb4a7101ff05adeaa47d731de29 100644 (file)
@@ -158,6 +158,11 @@ int main()
 #ifdef DEBUG
   check_iterator(E);
 #endif
+  E.add_scaled(-1., A);
+  if (E.l2_norm() < 1.e-14)
+    deallog << "Matrices are equal" << std::endl;
+  else
+    deallog << "Matrices differ!!" << std::endl;
   
   A.clear();
   deallog << "Structure" << std::endl;
index dbc2116780f435a02aaefcb9a8216becab04aecd..60784853af8ff23fb0e906547c1eb8d86f83330f 100644 (file)
@@ -202,6 +202,7 @@ DEAL::      2       1       0       -1.00
 DEAL:: 2       2       1       5.00
 DEAL:: 2       3       2       -2.00
 DEAL:: 2       6       3       -1.00
+DEAL::Matrices are equal
 DEAL::Structure
 DEAL::Assemble
 DEAL:9-SparseMatrix<double>:Richardson::Starting 

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.