sparse-base-chol.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2005-2012 David Bateman
00004 Copyright (C) 1998-2005 Andy Adler
00005 
00006 This file is part of Octave.
00007 
00008 Octave is free software; you can redistribute it and/or modify it
00009 under the terms of the GNU General Public License as published by the
00010 Free Software Foundation; either version 3 of the License, or (at your
00011 option) any later version.
00012 
00013 Octave is distributed in the hope that it will be useful, but WITHOUT
00014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00015 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00016 for more details.
00017 
00018 You should have received a copy of the GNU General Public License
00019 along with Octave; see the file COPYING.  If not, see
00020 <http://www.gnu.org/licenses/>.
00021 
00022 */
00023 
00024 #if !defined (octave_sparse_base_chol_h)
00025 #define octave_sparse_base_chol_h 1
00026 
00027 #include "oct-sparse.h"
00028 #include "dColVector.h"
00029 
00030 template <class chol_type, class chol_elt, class p_type>
00031 class
00032 sparse_base_chol
00033 {
00034 protected:
00035 #ifdef HAVE_CHOLMOD
00036   class sparse_base_chol_rep
00037   {
00038   public:
00039     sparse_base_chol_rep (void)
00040       : count (1), Lsparse (0), Common (), is_pd (false), minor_p (0),
00041         perms (), cond (0)
00042       { }
00043 
00044     sparse_base_chol_rep (const chol_type& a, const bool natural)
00045       : count (1), Lsparse (0), Common (), is_pd (false), minor_p (0),
00046         perms (), cond (0)
00047       {
00048         init (a, natural);
00049       }
00050 
00051     sparse_base_chol_rep (const chol_type& a, octave_idx_type& info,
00052                           const bool natural)
00053       : count (1), Lsparse (0), Common (), is_pd (false), minor_p (0),
00054         perms (), cond (0)
00055       {
00056         info = init (a, natural);
00057       }
00058 
00059     ~sparse_base_chol_rep (void)
00060       {
00061         if (is_pd)
00062           CHOLMOD_NAME (free_sparse) (&Lsparse, &Common);
00063       }
00064 
00065     cholmod_sparse * L (void) const { return Lsparse; }
00066 
00067     octave_idx_type P (void) const
00068       {
00069         return (minor_p == static_cast<octave_idx_type>(Lsparse->ncol) ?
00070                 0 : minor_p + 1);
00071       }
00072 
00073     ColumnVector perm (void) const { return perms + 1; }
00074 
00075     p_type Q (void) const;
00076 
00077     bool is_positive_definite (void) const { return is_pd; }
00078 
00079     double rcond (void) const { return cond; }
00080 
00081     octave_refcount<int> count;
00082 
00083   private:
00084     cholmod_sparse *Lsparse;
00085 
00086     cholmod_common Common;
00087 
00088     bool is_pd;
00089 
00090     octave_idx_type minor_p;
00091 
00092     ColumnVector perms;
00093 
00094     double cond;
00095 
00096     octave_idx_type init (const chol_type& a, bool natural = true);
00097 
00098     void drop_zeros (const cholmod_sparse* S);
00099 
00100     // No copying!
00101 
00102     sparse_base_chol_rep (const sparse_base_chol_rep&);
00103 
00104     sparse_base_chol_rep& operator = (const sparse_base_chol_rep&);
00105   };
00106 #else
00107   class sparse_base_chol_rep
00108   {
00109   public:
00110     sparse_base_chol_rep (void)
00111       : count (1), is_pd (false), minor_p (0), perms (), cond (0) { }
00112 
00113     sparse_base_chol_rep (const chol_type& a,
00114                           const bool natural)
00115       : count (1), is_pd (false), minor_p (0), perms (), cond (0)
00116       { init (a, natural); }
00117 
00118     sparse_base_chol_rep (const chol_type& a, octave_idx_type& info,
00119                           const bool natural)
00120       : count (1), is_pd (false), minor_p (0), perms (), cond (0)
00121       { info = init (a, natural); }
00122 
00123     ~sparse_base_chol_rep (void) { }
00124 
00125     octave_idx_type P (void) const { return 0; }
00126 
00127     ColumnVector perm (void) const { return perms + 1; }
00128 
00129     p_type Q (void) const;
00130 
00131     bool is_positive_definite (void) const { return is_pd; }
00132 
00133     double rcond (void) const { return cond; }
00134 
00135     octave_refcount<int> count;
00136 
00137   private:
00138     bool is_pd;
00139 
00140     octave_idx_type minor_p;
00141 
00142     ColumnVector perms;
00143 
00144     double cond;
00145 
00146     octave_idx_type init (const chol_type& a, bool natural = true);
00147 
00148     // No copying!
00149 
00150     sparse_base_chol_rep (const sparse_base_chol_rep&);
00151 
00152     sparse_base_chol_rep& operator = (const sparse_base_chol_rep&);
00153   };
00154 #endif
00155 
00156  private:
00157   sparse_base_chol_rep *rep;
00158 
00159 public:
00160 
00161   sparse_base_chol (void)
00162     : rep (new typename
00163            sparse_base_chol<chol_type, chol_elt, p_type>::sparse_base_chol_rep ())
00164     { }
00165 
00166   sparse_base_chol (const chol_type& a, const bool n)
00167     : rep (new typename
00168            sparse_base_chol<chol_type, chol_elt, p_type>::sparse_base_chol_rep (a, n))
00169     { }
00170 
00171   sparse_base_chol (const chol_type& a, octave_idx_type& info, const bool n)
00172     : rep (new typename sparse_base_chol<chol_type, chol_elt, p_type>::sparse_base_chol_rep (a, info, n))
00173     { }
00174 
00175   sparse_base_chol (const sparse_base_chol<chol_type, chol_elt, p_type>& a)
00176     : rep (a.rep)
00177     { rep->count++; }
00178 
00179   virtual ~sparse_base_chol (void)
00180     {
00181       if (--rep->count == 0)
00182         delete rep;
00183     }
00184 
00185   sparse_base_chol& operator = (const sparse_base_chol& a)
00186     {
00187       if (this != &a)
00188         {
00189           if (--rep->count == 0)
00190             delete rep;
00191 
00192           rep = a.rep;
00193           rep->count++;
00194         }
00195 
00196       return *this;
00197     }
00198 
00199   chol_type L (void) const;
00200 
00201   chol_type R (void) const { return L().hermitian (); }
00202 
00203   octave_idx_type P (void) const { return rep->P(); }
00204 
00205   ColumnVector perm (void) const { return rep->perm(); }
00206 
00207   p_type Q (void) const { return rep->Q(); }
00208 
00209   bool is_positive_definite (void) const
00210     { return rep->is_positive_definite(); }
00211 
00212   double rcond (void) const { return rep->rcond(); }
00213 
00214   chol_type inverse (void) const;
00215 };
00216 
00217 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines