ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
imatrix.h
Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2014 Regents of the University of California
00006  *
00007  * ADModelbuilder and associated libraries and documentations are
00008  * provided under the general terms of the "New BSD" license
00009  *
00010  * License:
00011  *
00012  * Redistribution and use in source and binary forms, with or without
00013  * modification, are permitted provided that the following conditions are
00014  * met:
00015  *
00016  * 1. Redistributions of source code must retain the above copyright
00017  * notice, this list of conditions and the following disclaimer.
00018  *
00019  * 2.  Redistributions in binary form must reproduce the above copyright
00020  * notice, this list of conditions and the following disclaimer in the
00021  * documentation and/or other materials provided with the distribution.
00022  *
00023  * 3.  Neither the name of the  University of California, Otter Research,
00024  * nor the ADMB Foundation nor the names of its contributors may be used
00025  * to endorse or promote products derived from this software without
00026  * specific prior written permission.
00027  *
00028  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00029  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00030  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00031  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00032  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00033  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00034  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00035  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00036  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00037  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00038  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00039  *
00040  */
00041 #ifndef __ADMB_IMATRIX_H__
00042 #define __ADMB_IMATRIX_H__
00043 
00048 class imatrix_position
00049 {
00050 public:
00051   int row_min;
00052   int row_max;
00053   ivector lb;
00054   ivector ub;
00055   ptr_vector ptr;
00056   imatrix_position(const imatrix &);
00057   imatrix_position(int min, int max);
00058   imatrix_position(const imatrix_position &);
00059   ivector_position operator()(int i);
00060 };
00061 
00062 imatrix_position restore_imatrix_position(void);
00063 imatrix restore_imatrix_value(const imatrix_position &);
00064 
00069 class imatrix
00070 {
00071 protected:
00072    int index_min;
00073    int index_max;
00074    ivector* m;
00075    mat_shapex* shape;
00076 
00077 public:
00078    int operator!(void) const
00079    {
00080       return (shape == NULL);
00081    }
00082 
00083    imatrix(int, int);
00084    // makes a matrix [0..nr][0..nc]
00085 
00086    imatrix(int nrl, int nrh, const ivector & iv);
00087    void allocate(int nrl, int nrh, const ivector & iv);
00088 
00089    imatrix(int, int, int, int);
00090    // makes a matrix [nrl..nrh][ncl..nch]
00091    imatrix(int, int, int, const ivector &);
00092    imatrix sub(int, int);
00093    imatrix(int, int, const ivector &, const ivector &);
00094    imatrix(const ad_integer & nrl, const ad_integer & nrh,
00095      const index_type & ncl, const index_type & nch);
00096 
00097    imatrix& operator=(const imatrix & t);
00098    imatrix& operator=(const int);
00099    imatrix(const imatrix &);
00100    // copy initializer
00101    imatrix(const imatrix_position &);
00102    imatrix(void);
00103 
00104    ~imatrix();
00105    void shallow_copy(const imatrix &);
00106 
00107    void save_imatrix_value(void);
00108    void save_imatrix_position(void);
00109    imatrix restore_imatrix_value(const imatrix_position & mpos);
00110    imatrix_position restore_imatrix_position(void);
00111 
00112    void allocate(void);
00113    void allocate(const imatrix & dm);
00114    void allocate(int nrl, int nrh, int ncl, int nch);
00115    void allocate(int nrl, int nrh);
00116    void allocate(int nrl, int nrh, int ncl, const ivector & nch);
00117    void allocate(int nrl, int nrh, const ivector & ncl, const ivector & nch);
00118    void allocate(const ad_integer & nrl, const ad_integer & nrh,
00119      const index_type & ncl, const index_type & nch);
00120    void deallocate();
00121 
00122    ivector& operator[](int);
00123    ivector& operator()(int);
00124    int& operator()(int, int);
00125    const ivector& operator[](int) const;
00126    const ivector& operator()(int) const;
00127    const int& operator()(int, int) const;
00128 
00129    int indexmin(void) const
00130    {
00131       return index_min;
00132    }
00133    int indexmax(void) const
00134    {
00135       return index_max;
00136    }
00137    int rowmin(void) const
00138    {
00139       return index_min;
00140    }
00141    int rowmax(void) const
00142    {
00143       return index_max;
00144    }
00145    int colmin(void) const
00146    {
00147       return ((*this) (indexmin()).indexmin());
00148    }
00149    int colmax(void) const
00150    {
00151       return ((*this) (indexmin()).indexmax());
00152    }
00153    // returns the number of rows
00154    int rowsize() const
00155    {
00156       return (rowmax() - rowmin() + 1);
00157    }
00158    // returns the number of columns
00159    int colsize() const
00160    {
00161       return (colmax() - colmin() + 1);
00162    }
00163    void rowshift(int min);
00164    inline ivector & elem(int i)
00165    {
00166       return (*(m + i));
00167    }
00168    inline int &elem(int i, int j)
00169    {
00170       return (*((*(m + i)).v + j));
00171    }
00172    inline const ivector & elem(int i) const
00173    {
00174       return (*(m + i));
00175    }
00176    inline const int &elem(int i, int j) const
00177    {
00178       return (*((*(m + i)).v + j));
00179    }
00180 
00181    void write_on(const ostream &) const;
00182    void write_on(const uostream &) const;
00183    void read_from(const istream &);
00184    void read_from(const uistream &);
00185    void initialize(void);
00186    void fill_seqadd(int, int);
00187    void colfill_seqadd(int, int, int);
00188 
00189   bool is_valid_row(const int i) const;
00190 
00191    friend char* fform(const char*, const dmatrix&);
00192    friend class i3_array;
00193 };
00194 
00195 #ifdef OPT_LIB
00196 inline ivector& imatrix::operator()(int i)
00197 {
00198   return m[i];
00199 }
00200 inline int& imatrix::operator()(int i, int j)
00201 {
00202   return (*((*(m + i)).v + j));
00203 }
00204 inline ivector& imatrix::operator[](int i)
00205 {
00206   if (!m)
00207     throw std::bad_alloc();
00208   else
00209     return m[i];
00210 }
00211 inline const ivector& imatrix::operator()(int i) const
00212 {
00213   return m[i];
00214 }
00215 inline const int& imatrix::operator()(int i, int j) const
00216 {
00217   return (*((*(m + i)).v + j));
00218 }
00219 inline const ivector& imatrix::operator[](int i) const
00220 {
00221   return m[i];
00222 }
00223 #endif
00224 #endif
00225