]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
new function print_formatted, fill fixed
authorguido <guido@0785d39b-7218-0410-832d-ea1e28bc413d>
Mon, 18 Apr 2005 18:25:07 +0000 (18:25 +0000)
committerguido <guido@0785d39b-7218-0410-832d-ea1e28bc413d>
Mon, 18 Apr 2005 18:25:07 +0000 (18:25 +0000)
git-svn-id: https://svn.dealii.org/trunk@10511 0785d39b-7218-0410-832d-ea1e28bc413d

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

index 4074d84510dd15ceec261b431df2096cbf3764bc..538729829ca3e5501d7d5cc858396ee60ce97cd1 100644 (file)
@@ -229,6 +229,55 @@ class LAPACKFullMatrix : public TransposeTable<number>
     std::complex<number>
     eigenvalue (unsigned int i) const;
     
+                                    /**
+                                     * Print the matrix and allow
+                                     * formatting of entries.
+                                    *
+                                     * The parameters allow for a
+                                     * flexible setting of the output
+                                     * format:
+                                     *
+                                     * @arg <tt>precision</tt>
+                                     * denotes the number of trailing
+                                     * digits.
+                                     *
+                                     * @arg <tt>scientific</tt> is
+                                     * used to determine the number
+                                     * format, where
+                                     * <tt>scientific</tt> =
+                                     * <tt>false</tt> means fixed
+                                     * point notation.
+                                     *
+                                     * @arg <tt>width</tt> denotes
+                                     * the with of each column. A
+                                     * zero entry for <tt>width</tt>
+                                     * makes the function compute a
+                                     * width, but it may be changed
+                                     * to a positive value, if output
+                                     * is crude.
+                                     *
+                                     * @arg <tt>zero_string</tt>
+                                     * specifies a string printed for
+                                     * zero entries.
+                                     *
+                                     * @arg <tt>denominator</tt>
+                                     * Multiply the whole matrix by
+                                     * this common denominator to get
+                                     * nicer numbers.
+                                     *
+                                     * @arg <tt>threshold</tt>: all
+                                     * entries with absolute value
+                                     * smaller than this are
+                                     * considered zero.
+                                    */
+    void print_formatted (std::ostream       &out,
+                         const unsigned int  presicion=3,
+                         const bool          scientific  = true,
+                         const unsigned int  width       = 0,
+                         const char         *zero_string = " ",
+                         const double        denominator = 1.,
+                         const double        threshold   = 0.) const;
+    
   private:
                                     /**
                                      * Since LAPACK operations
@@ -294,18 +343,21 @@ LAPACKFullMatrix<number>::fill (
   const unsigned int src_offset_i,
   const unsigned int src_offset_j)
 {
-  const unsigned int endrow = src_offset_i + this->m();
-  const unsigned int endcol = src_offset_j + this->n();
-
-  for (unsigned int i=0;i<this->m();++i)
+  const unsigned int endrow = this->n_rows() - dst_offset_i;
+  const unsigned int endcol = src_offset_j + this->n_cols();
+  
+  const typename MATRIX::const_iterator
+    end = M.end(src_offset_i+n_rows()-dst_offset_i-1);
+  for (typename MATRIX::const_iterator entry = M.begin(src_offset_i);
+       entry != end; ++entry)
     {
-      const typename MATRIX::const_iterator end = M.end(src_offset_i+i);
-      for (typename MATRIX::const_iterator entry = M.begin(src_offset_i+i);
-          entry != end; ++entry)
-       if (entry->column() < endcol)
-         this->el(dst_offset_i+i,
-                  dst_offset_j+entry->column()-src_offset_j)
-           = entry->value();
+      const unsigned int i = entry->row();
+      const unsigned int j = entry->column();
+      
+      if (j >= src_offset_j && j < endcol)
+       this->operator()(dst_offset_i-src_offset_i+i,
+                        dst_offset_j-src_offset_j+j)
+         = entry->value();
     }
   
   state = LAPACKSupport::matrix;
index d2c4b2c6c607bcf7b13fa2b7494b79954a272eb0..8565a5072e403381949ee1a3a0bc152f3358eed1 100644 (file)
@@ -18,6 +18,7 @@
 #include <lac/vector.h>
 
 #include <iostream>
+#include <iomanip>
 
 using namespace LAPACKSupport;
 
@@ -225,6 +226,55 @@ LAPACKFullMatrix<number>::Tvmult_add (
   Tvmult(w, v, true);
 }
 
+template <typename number>
+void
+LAPACKFullMatrix<number>::print_formatted (
+  std::ostream       &out,
+  const unsigned int  precision,
+  const bool          scientific,
+  const unsigned int  width_,
+  const char         *zero_string,
+  const double        denominator,
+  const double        threshold) const
+{
+  unsigned int width = width_;
+  
+  Assert ((!this->empty()) || (this->n_cols()+this->n_rows()==0),
+         ExcInternalError());
+  
+                                  // set output format, but store old
+                                  // state
+  std::ios::fmtflags old_flags = out.flags();
+  unsigned int old_precision = out.precision (precision);
+
+  if (scientific)
+    {
+      out.setf (std::ios::scientific, std::ios::floatfield);
+      if (!width)
+       width = precision+7;
+    } else {
+      out.setf (std::ios::fixed, std::ios::floatfield);
+      if (!width)
+       width = precision+2;
+    }
+  
+  for (unsigned int i=0; i<n_rows(); ++i) 
+    {
+      for (unsigned int j=0; j<n_cols(); ++j)
+       if (std::fabs(this->el(i,j)) > threshold)
+         out << std::setw(width)
+             << this->el(i,j) * denominator << ' ';
+       else
+         out << std::setw(width) << zero_string << ' ';
+      out << std::endl;
+    };
+
+  AssertThrow (out, ExcIO());
+                                  // reset output format
+  out.flags (old_flags);
+  out.precision(old_precision);
+}
+
 
 template class LAPACKFullMatrix<double>;
 template LAPACKFullMatrix<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.