]> https://gitweb.dealii.org/ - dealii.git/commitdiff
new TransposeTable class
authorGuido Kanschat <dr.guido.kanschat@gmail.com>
Sun, 6 Mar 2005 01:04:49 +0000 (01:04 +0000)
committerGuido Kanschat <dr.guido.kanschat@gmail.com>
Sun, 6 Mar 2005 01:04:49 +0000 (01:04 +0000)
git-svn-id: https://svn.dealii.org/trunk@10005 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/base/include/base/table.h
tests/base/table.cc

index 28075d7f52d3b2309a6a590138aa3c82aeae4171..d80c14f591b1e386be362c1ed7aaac769fe6a1a9 100644 (file)
@@ -2,7 +2,7 @@
 //    $Id$
 //    Version: $Name$
 //
-//    Copyright (C) 2000, 2001, 2002, 2003, 2004 by the deal.II authors
+//    Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 by the deal.II authors
 //
 //    This file is subject to QPL and may not be  distributed
 //    without copyright and license information. Please refer
@@ -707,11 +707,12 @@ namespace internal
  * dimensions. Of course, this is not possible.
  *
  * The way out of the first problem (and partly the second one as
- * well) is to have derived class for each value of <tt>N</tt> that have a
- * constructor with the right number of arguments, one for each
- * dimension. These then transform their arguments into the data type
- * this class wants to see, both for construction as well as access
- * through the <tt>operator()</tt> function.
+ * well) is to have a common base class TableBase and a derived class
+ * for each value of <tt>N</tt>.  This derived class has a constructor
+ * with the correct number of arguments, namely <tt>N</tt>. These then
+ * transform their arguments into the data type the base class (this
+ * class in fact) uses in the constructor as well as in
+ * element access through operator() functions.
  *
  * The second problem is that we would like to allow access through a
  * sequence of <tt>operator[]</tt> calls. This mostly because, as said,
@@ -724,7 +725,7 @@ namespace internal
  * namespace.
  *
  *
- * @section TableComp Comparison with the Tensor class
+ * <h3>Comparison with the Tensor class</h3>
  *
  * In some way, this class is similar to the Tensor class, in
  * that it templatizes on the number of dimensions. However, there are
@@ -1255,6 +1256,139 @@ class Table<2,T> : public TableBase<2,T>
 
 
 
+/**
+ * A class representing a transpose two-dimensional table, i.e. a
+ * matrix of objects (not necessarily only numbers) in column first
+ * numbering (FORTRAN convention).
+ *
+ * This class copies the functions of Table<2,T>, but the element
+ * access and the dimensions will be for the transpose ordering of the
+ * data field in TableBase.
+ *
+ * @author Guido Kanschat, 2005
+ */
+template <typename T>
+class TransposeTable : public TableBase<2,T>
+{
+  public:
+                                     /**
+                                      * Default constructor. Set all
+                                      * dimensions to zero.
+                                      */
+    TransposeTable ();
+
+                                     /**
+                                      * Constructor. Pass down the
+                                      * given dimensions to the base
+                                      * class.
+                                      */
+    TransposeTable (const unsigned int size1,
+                   const unsigned int size2);
+
+                                     /**
+                                      * Reinitialize the object. This
+                                      * function is mostly here for
+                                      * compatibility with the earlier
+                                      * <tt>vector2d</tt> class. Passes
+                                      * down to the base class by
+                                      * converting the arguments to
+                                      * the data type requested by the
+                                      * base class.
+                                      */
+    void reinit (const unsigned int size1,
+                 const unsigned int size2);
+
+                                     /**
+                                      * Direct access to one element
+                                      * of the table by specifying all
+                                      * indices at the same time. Range
+                                      * checks are performed.
+                                      *
+                                      * This version of the function
+                                      * only allows read access.
+                                      */
+    const T & operator () (const unsigned int i,
+                           const unsigned int j) const;
+    
+
+                                     /**
+                                      * Direct access to one element
+                                      * of the table by specifying all
+                                      * indices at the same time. Range
+                                      * checks are performed.
+                                      *
+                                      * This version of the function
+                                      * allows read-write access.
+                                      */
+    T & operator () (const unsigned int i,
+                     const unsigned int j);
+
+    
+                                     /**
+                                      * Number of rows. This function
+                                      * really makes only sense since
+                                      * we have a two-dimensional
+                                      * object here.
+                                      */
+    unsigned int n_rows () const;
+    
+                                     /**
+                                      * Number of columns. This function
+                                      * really makes only sense since
+                                      * we have a two-dimensional
+                                      * object here.
+                                      */
+    unsigned int n_cols () const;
+
+  protected:
+                                     /**
+                                      * Return a read-write reference
+                                      * to the element <tt>(i,j)</tt>.
+                                      *
+                                      * This function does no bounds
+                                      * checking and is only to be
+                                      * used internally and in
+                                      * functions already checked.
+                                      *
+                                      * These functions are mainly
+                                      * here for compatibility with a
+                                      * former implementation of these
+                                      * table classes for 2d arrays,
+                                      * then called <tt>vector2d</tt>.
+                                      */
+    T & el (const unsigned int i,
+            const unsigned int j);
+  
+                                     /**
+                                      * Return the value of the
+                                      * element <tt>(i,j)</tt> as a
+                                      * read-only reference.
+                                      *
+                                      * This function does no bounds
+                                      * checking and is only to be
+                                      * used internally and in
+                                      * functions already checked.
+                                      *
+                                      * We return the requested value
+                                      * as a constant reference rather
+                                      * than by value since this
+                                      * object may hold data types
+                                      * that may be large, and we
+                                      * don't know here whether
+                                      * copying is expensive or not.
+                                      *
+                                      * These functions are mainly
+                                      * here for compatibility with a
+                                      * former implementation of these
+                                      * table classes for 2d arrays,
+                                      * then called <tt>vector2d</tt>.
+                                      */
+    const T & el (const unsigned int i,
+                  const unsigned int j) const;
+};
+
+
+
 /**
  * A class representing a three-dimensional table of objects (not
  * necessarily only numbers).
@@ -2429,7 +2563,7 @@ Table<1,T>::operator () (const unsigned int i)
 }
 
 
-
+//----------------------------------------------------------------------
 
 template <typename T>
 Table<2,T>::Table ()
@@ -2554,6 +2688,106 @@ Table<2,T>::n_cols () const
 
 
 
+//----------------------------------------------------------------------
+
+template <typename T>
+TransposeTable<T>::TransposeTable ()
+{}
+
+
+
+template <typename T>
+TransposeTable<T>::TransposeTable (const unsigned int size1,
+                                  const unsigned int size2)
+                :
+                TableBase<2,T> (TableIndices<2> (size2, size1))
+{}
+
+
+
+template <typename T>
+void
+TransposeTable<T>::reinit (const unsigned int size1,
+                          const unsigned int size2)
+{
+  this->TableBase<2,T>::reinit (TableIndices<2> (size2, size1));
+}
+
+
+
+template <typename T>
+inline
+const T &
+TransposeTable<T>::operator () (const unsigned int i,
+                               const unsigned int j) const
+{
+  Assert (i < this->table_size[1],
+          ExcIndexRange (i, 0, this->table_size[1]));
+  Assert (j < this->table_size[0],
+          ExcIndexRange (j, 0, this->table_size[0]));
+  return this->val[j*this->table_size[1]+i];
+}
+
+
+
+template <typename T>
+inline
+T &
+TransposeTable<T>::operator () (const unsigned int i,
+                               const unsigned int j)
+{
+  Assert (i < this->table_size[1],
+          ExcIndexRange (i, 0, this->table_size[1]));
+  Assert (j < this->table_size[0],
+          ExcIndexRange (j, 0, this->table_size[0]));
+  return this->val[j*this->table_size[1]+i];
+}
+
+
+
+template <typename T>
+inline
+const T &
+TransposeTable<T>::el (const unsigned int i,
+                      const unsigned int j) const
+{
+  return this->val[j*this->table_size[1]+i];
+}
+
+
+
+template <typename T>
+inline
+T &
+TransposeTable<T>::el (const unsigned int i,
+                      const unsigned int j)
+{
+  return this->val[j*this->table_size[1]+i];
+}
+
+
+
+template <typename T>
+inline
+unsigned int
+TransposeTable<T>::n_rows () const
+{
+  return this->table_size[1];
+}
+
+
+
+template <typename T>
+inline
+unsigned int
+TransposeTable<T>::n_cols () const
+{
+  return this->table_size[0];
+}
+
+
+
+//----------------------------------------------------------------------
 
 
 template <typename T>
index 8edab04d8ea812feb3b5531019a72248d1d18143..5f662ed77744b40126cd7d5126094f5a99f36336 100644 (file)
@@ -2,7 +2,7 @@
 //    $Id$
 //    Version: $Name$ 
 //
-//    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004 by the deal.II authors
+//    Copyright (C) 1998 - 2005 by the deal.II authors
 //
 //    This file is subject to QPL and may not be  distributed
 //    without copyright and license information. Please refer
@@ -40,7 +40,7 @@ main ()
   deallog.attach(logfile);
   deallog.depth_console(0);
 
-                                   // first check: a square table
+                                   // a square table
   if (true)
     {
       Table<2,double> Td(3,3);
@@ -70,7 +70,7 @@ main ()
           };
     };
   
-                                   // second check: a rectangular table
+                                   // a rectangular table
   if (true)
     {
       Table<2,double> Td(4,3);
@@ -105,7 +105,37 @@ main ()
     };
 
 
-                                   // third check: a 1d-table
+                                   // a transposed rectangular table
+  if (true)
+    {
+      TransposeTable<float> Td(4,3);
+      TransposeTable<int> Ti(4,3);
+
+                                      // Fill the float matrix in row
+                                      // first ordering
+      for (unsigned int i=0; i<12; ++i)
+        {
+          Td(i/3,i%3) = entries[i];
+        };
+
+                                      // This fills the integer
+                                      // matrix in column first
+                                      // ordering
+      Ti.fill(entries);
+                                      // Output both matrices...
+                                      // They should be different
+      for (unsigned int i=0; i<4; ++i)
+       {
+         for (unsigned int j=0; j<3; ++j)
+           {
+             logfile << '\t' << Ti(i,j) << '/' << Td(i,j);
+           }
+         logfile << std::endl;
+       }
+    }
+  
+
+                                   // a 1d-table
   if (true)
     {
       const unsigned int N=10;
@@ -128,7 +158,7 @@ main ()
         };
     };
   
-                                   // fourth check: a 3d-table
+                                   // a 3d-table
   if (true)
     {
       const unsigned int I=4, J=3, K=2;
@@ -170,7 +200,7 @@ main ()
                   ExcInternalError());
     };
 
-                                   // fifth check: a 4d-table
+                                   // a 4d-table
   if (true)
     {
       const unsigned int I=5, J=4, K=3, L=2;
@@ -223,7 +253,7 @@ main ()
                                         static_cast<signed int>(L)));
     };
 
-                                   // sixth check: a 5d-table
+                                   // 5d-table
   if (true)
     {
       const unsigned int I=6, J=2, K=3, L=4, M=5;
@@ -272,7 +302,7 @@ main ()
                                           static_cast<signed int>(M)));
     };
   
-                                   // seventh check: a 6d-table
+                                   // a 6d-table
   if (true)
     {
       const unsigned int I=6, J=2, K=4, L=3, M=5, N=7;

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.