ADMB Documentation  11.5.3285
 All Classes Files Functions Variables Typedefs Friends Defines
fvar.hpp
Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 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 FVAR_HPP
00042 #define FVAR_HPP
00043 //#define __MINGW64__
00050 #if defined(__MINGW64__ )
00051   #define OFF_T off64_t
00052   #define LSEEK lseek64
00053   #if !defined(AD_LONG_INT)
00054     #define AD_LONG_INT long long int
00055   #endif
00056 #else
00057   #define OFF_T off_t
00058   #ifndef _MSC_VER
00059     #define LSEEK lseek
00060   #endif
00061   #if !defined(AD_LONG_INT)
00062     #define AD_LONG_INT long int
00063   #endif
00064 #endif
00065 
00066 #include <math.h>
00067 // Borrow definition of M_PI from GCC
00068 #ifndef M_PI
00069 #   define M_PI 3.14159265358979323846
00070 #endif
00071 #ifndef PI
00072 #   define PI 3.14159265358979323846
00073 #endif
00074 
00075 #if defined(__GNUC__) && (__GNUC__ < 3)
00076   #pragma interface
00077 #endif
00078 
00079 #if defined(THREAD_EXPERIMENT)
00080 #   define THREAD_SAFE
00081 #endif
00082 
00083 #if defined(THREAD_SAFE)
00084 #   include <pthread.h>
00085 #endif
00086 
00087 #define USE_VECTOR_SHAPE_POOL
00088 
00089 #if defined(USE_DDOUBLE)
00090 #   include <qd/qd.h>
00091 #   define  double dd_real
00092     dd_real operator ++(dd_real & d)
00093     {
00094        return d += 1.0;
00095     }
00096 
00097     dd_real pow(const dd_real & d, int i);
00098     dd_real pow(const dd_real & d, const dd_real &);
00099 #endif
00100 
00101 #if defined(__TURBOC__)
00102 #   define __STL_PTHREADS
00103 #   include <k3stl.h>
00104 #   include <pthread_alloc>
00105 #endif
00106 
00107 #define  __NUMBERVECTOR__
00108 
00112 #define ADUNCONST(type,obj) type & obj = (type&) _##obj;
00113 
00114 #define  MFCL2_CONSTRUCTORS
00115 
00116 #if defined(_ADEXEP)
00117 #   define USE_EXECPTIONS
00118 #endif
00119 
00120 #define __USE_IOSTREAM__
00121 
00122 #if defined(__BORLANDC__)
00123   #if (__BORLANDC__  >= 0x0550)
00124     #include <fcntl.h>
00125   #endif
00126 #endif
00127 
00128 #define USE_HIGHER_ARRAYS
00129 
00130 #if defined(__BORLANDC__) || defined (_MSC_VER) || defined(__WAT32__)
00131 #   include <io.h>
00132 #endif
00133 
00134 #if !defined(AD_LONG_INT)
00135   #define AD_LONG_INT long int
00136 #endif
00137 
00138 #if !defined(_MSC_VER)
00139   #include <fcntl.h> // to get fstreambase
00140   #if !defined(O_BINARY)
00141     #define O_BINARY 0
00142   #endif
00143   #define __GNU__
00144 #endif
00145 
00146 #if defined(__DJGPP__)
00147   #include <dos.h>
00148   #include <pc.h>
00149 #endif
00150 
00151 #ifndef NO_DERIVS
00152   #define  NO_DERIVS
00153 #endif
00154 
00155 // C language function prototypes
00156 extern "C"
00157 {
00158    typedef int (*fptr) (const char *format, ...);
00159    extern fptr ad_printf;
00160    typedef void (*exitptr) (int);
00161    extern exitptr ad_exit;
00162 
00163    void spdll_exit(int);
00164 }
00165 
00171 class double_and_int
00172 {
00173 public:
00175   double x;
00176 
00182   inline double &xvalue()
00183   {
00184     return x;
00185   }
00186 };
00187 
00188 
00189 // "forward" class definitions
00190 class banded_symmetric_dvar_matrix;
00191 class banded_symmetric_dmatrix;
00192 class banded_lower_triangular_dmatrix;
00193 class banded_lower_triangular_dvar_matrix;
00194 class random_number_generator;
00195 class ad_integer;
00196 class index_type;
00197 class dlink;
00198 class dvector;
00199 class dmatrix;
00200 class tdmatrix;
00201 class imatrix;
00202 class prevariable;
00203 class dvariable;
00204 class dvar_vector;
00205 class ivector;
00206 class lvector;
00207 class dvar_matrix;
00208 class uostream;
00209 class uistream;
00210 class arr_link;
00211 class arr_list;
00212 class d3_array;
00213 class d4_array;
00214 class d5_array;
00215 class d6_array;
00216 class d7_array;
00217 class dvar3_array;
00218 class dvar4_array;
00219 class dvar5_array;
00220 class dvar6_array;
00221 class dvar7_array;
00222 class dlist;
00223 class indvar_offset_list;
00224 class dvar_matrix_position;
00225 class dmatrix_position;
00226 class d3_array_position;
00227 class dvector_position;
00228 class vector_shape;
00229 class ivector_position;
00230 class kkludge_object;
00231 class dvar_vector_position;
00232 dvector restore_dvar_vector_derivatives(void);
00233 class gradient_structure;
00234 class dependent_variables_information;
00235 class vector_shapex;
00236 class predvar_vector;
00237 class independent_variables;
00238 
00239 #if defined(__GNUC__) && (__GNUC__ < 3)
00240   #include <fstream.h>
00241 #elif defined(_MSC_VER) && (_MSC_VER < 1300)
00242   #include <fstream.h>
00243 #elif defined(__SUNPRO_CC) && (__SUNPRO_CC < 0x5140)
00244   #include <fstream.h>
00245 #else
00246   #include <fstream>
00247 #endif
00248 
00249 #include <stdio.h>
00250 
00251 #if defined(__BORLANDC__)
00252 #   if (__BORLANDC__  < 0x0550)
00253 #      include <fcntl.h> // to get fstreambase
00254 #      include <strstrea.h>
00255 #   else
00256 #      include <strstream>
00257 #   endif
00258 #endif
00259 
00260 #if defined(_MSC_VER)
00261 #   if (_MSC_VER < 1300)
00262 #     include <iostream.h>
00263 #     include <strstrea.h>
00264 #   else
00265 #     include <iostream>
00266 #   endif
00267 #   include <stddef.h>
00268 #   include <fcntl.h> // to get fstreambase
00269 #   include <dos.h>
00270 #   undef __ZTC__
00271 #   undef __SUN__
00272 #endif
00273 
00274 #ifdef __ZTC__
00275 #   include <fstream.hpp> // to get fstreambase
00276 #   include <iostream.hpp>
00277 #   include <dos.h>
00278 #   undef __SUN__
00279 #endif
00280 
00281 #ifdef __SUN__
00282 #   undef __NDPX__
00283 #   include <fstream.h>
00284 #   include <iostream.h>
00285 #   ifndef _FPOS_T_DEFINED
00286 #      ifdef __GNUDOS__
00287 #         if defined(linux) || defined(__ADSGI__)|| defined(__CYGWIN32__)
00288              typedef long int fpos_t;
00289 #         else
00290              typedef unsigned long int fpos_t;
00291 #         endif
00292 #         undef __SUN__
00293           typedef int fpos_t;
00294 #      endif
00295 #      define _FPOS_T_DEFINED
00296 #   endif
00297 #endif
00298 
00299 #if defined(__BORLANDC__)
00300 #   if (__BORLANDC__  > 0x0520)
00301        using std::ofstream;
00302        using std::ifstream;
00303 #   endif
00304 #endif
00305 
00306 #if defined(__cplusplus)
00307   #include <iostream>
00308   #include <iomanip>
00309   #include <sstream>
00310   #include <istream>
00311   #include <sstream>
00312   using std::ofstream;
00313   using std::ostream;
00314   using std::ifstream;
00315   using std::istream;
00316   using std::istringstream;
00317   using std::streampos;
00318   using std::streambuf;
00319   using std::setw;
00320   using std::setprecision;
00321   using std::istringstream;
00322   using std::ios;
00323   using std::cerr;
00324   using std::cin;
00325   using std::cout;
00326   using std::endl;
00327 #else
00328   #include <iomanip.h>
00329   #include <strstream.h>
00330 #endif
00331 
00332 #define BEGIN_MINIMIZATION(nvar, objective_function, ind_vars, gradient, \
00333 cntrl) \
00334 gradient_structure gs; \
00335 while (cntrl.ireturn >= 0) \
00336 { \
00337   cntrl.fmin(objective_function,ind_vars,gradient ); \
00338   if (cntrl.ireturn > 0) \
00339   {
00340 #define END_MINIMIZATION(nvar,gradient) \
00341     gradcalc(nvar, gradient); \
00342   } \
00343 }
00344 
00345 void default_evaluation4ind(void);
00346 void default_evaluation(void);
00347 void default_evaluation0(void);
00348 void default_evaluation1(void);
00349 void default_evaluation1m(void);
00350 void default_evaluation2(void);
00351 void default_evaluation3(void);
00352 void default_evaluation4(void);
00353 void default_evaluation4m(void);
00354 
00355 void myheapcheck(char *);
00356 
00357 void RETURN_ARRAYS_INCREMENT(void);
00358 void RETURN_ARRAYS_DECREMENT(void);
00359 
00360 void *farptr_norm(void *);
00361 #ifdef DIAG
00362 long int farptr_tolong(void *);
00363 long int _farptr_tolong(void *);
00364 #endif
00365 
00366 class i3_array;
00367 
00368 ostream & operator<<(const ostream & ostr, const dmatrix & z);
00369 istream & operator>>(const istream & istr, const dmatrix & z);
00370 uostream & operator<<(const uostream & ostr, const dmatrix & z);
00371 uistream & operator>>(const uistream & istr, const dmatrix & z);
00372 
00373 ostream & operator<<(const ostream & ostr, const d3_array & z);
00374 istream & operator>>(const istream & istr, const d3_array & z);
00375 uostream & operator<<(const uostream & ostr, const d3_array & z);
00376 uistream & operator>>(const uistream & istr, const d3_array & z);
00377 
00378 ostream & operator<<(const ostream & ostr, const dvar3_array & z);
00379 istream & operator>>(const istream & istr, const dvar3_array & z);
00380 uostream & operator<<(const uostream & ostr, const dvar3_array & z);
00381 uistream & operator>>(const uistream & istr, const dvar3_array & z);
00382 
00383 ostream & operator<<(const ostream & ostr, const ivector & z);
00384 istream & operator>>(const istream & istr, const ivector & z);
00385 uostream & operator<<(const uostream & ostr, const ivector & z);
00386 uistream & operator>>(const uistream & istr, const ivector & z);
00387 
00388 ostream & operator<<(const ostream & ostr, const lvector & z);
00389 istream & operator>>(const istream & istr, const lvector & z);
00390 uostream & operator<<(const uostream & ostr, const lvector & z);
00391 uistream & operator>>(const uistream & istr, const lvector & z);
00392 
00393 ostream & operator<<(const ostream & ostr, const dvector & z);
00394 istream & operator>>(const istream & istr, const dvector & z);
00395 uostream & operator<<(const uostream & ostr, const dvector & z);
00396 uistream & operator>>(const uistream & istr, const dvector & z);
00397 
00398 ostream & operator<<(const ostream & ostr, const dvar_vector & z);
00399 istream & operator>>(const istream & istr, const dvar_vector & z);
00400 uostream & operator<<(const uostream & ostr, const dvar_vector & z);
00401 uistream & operator>>(const uistream & istr, const dvar_vector & z);
00402 
00403 ostream & operator<<(const ostream & ostr, const dvar_matrix & z);
00404 istream & operator>>(const istream & istr, const dvar_matrix & z);
00405 uostream & operator<<(const uostream & ostr, const dvar_matrix & z);
00406 uistream & operator>>(const uistream & istr, const dvar_matrix & z);
00407 
00408 
00409 ostream & operator<<(const ostream & ostr, const prevariable & z);
00410 istream & operator>>(const istream & istr, const prevariable & z);
00411 uostream & operator<<(const uostream & ostr, const prevariable & z);
00412 uistream & operator>>(const uistream & istr, const prevariable & z);
00413 
00414 ostream & setscientific(const ostream & s);
00415 
00416 //ostream& setshowpoint(const ostream& s);
00417 
00418 class preshowpoint {};
00419 preshowpoint setshowpoint(void);
00420 ostream & operator <<(const ostream &, preshowpoint);
00421 #if (__BORLANDC__  >= 0x0560)
00422   #define setfixed() std::fixed
00423 #else
00424 ostream & setfixed(const ostream & s);
00425 class prefixed {};
00426 prefixed setfixed(void);
00427 ostream & operator<<(const ostream &, prefixed);
00428 #endif
00429 
00430 #if (__BORLANDC__  >= 0x0560)
00431   #define setscientific() std::scientific
00432 #else
00433 class prescientific {};
00434 prescientific setscientific(void);
00435 ostream & operator<<(const ostream &, prescientific);
00436 #endif
00437 
00438 istream & operator>>(const istream & istr, const imatrix & z);
00439 ostream & operator<<(const ostream & istr, const imatrix & z);
00440 istream & operator>>(const istream & istr, const i3_array & z);
00441 ostream & operator<<(const ostream & istr, const i3_array & z);
00442 class grad_stack;
00443 
00449 class kkludge_object{};
00450 
00451 
00452 #ifndef _VECTOR_SHAPE
00453 #define _VECTOR_SHAPE
00454 #include <dfpool.h>
00455 
00460 class vector_shape_pool:public dfpool
00461 {
00462 public:
00463   vector_shape_pool();
00464   vector_shape_pool(const size_t);
00465 };
00466 
00471 class ts_vector_shape_pool:public tsdfpool
00472 {
00473  public:
00474    ts_vector_shape_pool();
00475    ts_vector_shape_pool(int);
00476 };
00477 #endif
00478 
00483 class vector_shape
00484 {
00485  public:
00486 #if defined(USE_VECTOR_SHAPE_POOL)
00487    static vector_shape_pool *xpool;
00488    void *operator new(size_t);
00489    void operator delete(void *ptr, size_t)
00490    {
00491       xpool->free(ptr);
00492    }
00493 #endif
00494    unsigned int ncopies;
00495    void shift(int min);
00496    int index_min;
00497    int index_max;
00498  private:
00499    friend class dvector;
00500    //friend class tdvector;
00501    friend class subdvector;
00502    friend class dvar_vector;
00503    friend class ivector;
00504    friend class lvector;
00505    friend class ptr_vector;
00506  public:
00507    unsigned int decr_ncopies(void)
00508    {
00509       return ncopies == 0 ? 0 : --ncopies;
00510    }
00511    unsigned int get_ncopies(void)
00512    {
00513       return ncopies;
00514    }
00515    unsigned int incr_ncopies(void)
00516    {
00517       return ++ncopies;
00518    }
00519    vector_shape(int lb, int lu)
00520    {
00521       index_min = lb;
00522       index_max = lu;
00523       ncopies = 0;
00524    }
00525    int indexmin() const
00526    {
00527       return index_min;
00528    }
00529    int indexmax() const
00530    {
00531       return index_max;
00532    }
00533 };
00534 
00535 
00540 class ptr_vector
00541 {
00542   void **v;
00543   vector_shape *shape;
00544 
00545 public:
00546   ptr_vector();
00547   ptr_vector(const ptr_vector & t);
00548   ptr_vector(int ncl, int ncu);
00549   ~ptr_vector();
00550 
00551   // returns the minimum allowable index
00552   int indexmin() const
00553   {
00554     return shape->index_min;
00555   }
00556   // returns the maximum allowable index
00557   int indexmax() const
00558   {
00559     return shape->index_max;
00560   }
00561   // returns the maximum allowable index
00562   int size() const
00563   {
00564     return shape->index_max - shape->index_min + 1;
00565   }
00566 
00567   void shift(int min);
00568   void allocate(int, int);
00569   //operator void ** ();
00570   void *&operator[] (int i);
00571   void *&operator() (int i);
00572   //void*& elem(int i);
00573   void *&elem(int i)
00574   {
00575     return (*(v + i));
00576   }
00577   int operator!(void) const
00578   {
00579     return (shape == NULL);
00580   }
00581   int operator() (void) const
00582   {
00583     return (shape != NULL);
00584   }
00585 
00586   ptr_vector& operator=(const ptr_vector& t);
00587   void initialize();
00588 };
00589 
00594 class preivector
00595 {
00596    ivector *p;
00597    int lb;
00598    int ub;
00599    inline preivector(ivector * _p, int _lb, int _ub)
00600    {
00601       p = _p;
00602       lb = _lb, ub = _ub;
00603    }
00604    friend class ivector;
00605 };
00606 
00607 #include <ivector.h>
00608 
00613 class lvector_position
00614 {
00615    int min;
00616    int max;
00617    int *v;
00618  public:
00619    lvector_position(void);
00620    lvector_position(const lvector & v);
00621    lvector_position(const lvector_position & dvp);
00622 };
00623 
00628 class lvector
00629 {
00630    AD_LONG_INT *v;
00631    vector_shape *shape;
00632 
00633  public:
00634    int operator!(void) const
00635    {
00636       return (shape == NULL);
00637    }
00638 
00639    inline AD_LONG_INT & elem(int i)
00640    {
00641       return (v[i]);
00642    }
00643    inline const AD_LONG_INT & elem(int i) const
00644    {
00645       return v[i];
00646    }
00647    // returns the minimum allowable index
00648    int indexmin() const
00649    {
00650       return shape->index_min;
00651    }
00652    // returns the maximum allowable index
00653    int indexmax() const
00654    {
00655       return shape->index_max;
00656    }
00657    // returns the maximum allowable index
00658    int size() const
00659    {
00660       return shape ? shape->index_max - shape->index_min + 1 : 0;
00661    }
00662    void shift(int min);
00663 
00664    void fill(const char *s);
00665    void fill_seqadd(const AD_LONG_INT &, const AD_LONG_INT &);
00666    void fill_multinomial(const int &seed, const dvector & p);
00667    void fill_multinomial(const random_number_generator & rng,
00668      const dvector & p);
00669 
00670    //lvector(unsigned int sz); //makes an array [0..sz]
00671 
00672    lvector(const lvector &);
00673 
00674    lvector(const dvector &);
00675 
00676    lvector(const ivector &);
00677 
00678    lvector(void);
00679 
00680    lvector(int ncl, int ncu);
00681    void allocate(int ncl, int ncu);
00682    void allocate(const lvector &);
00683    void allocate(void);
00684    // makes an array [ncl..ncu]
00685 
00686    lvector(unsigned int sz, AD_LONG_INT * x);
00687 
00688    operator   AD_LONG_INT *();
00689 
00690    ~lvector();
00691 
00692    void write_on(const ostream & s) const;
00693 
00694    void read_from(const istream & s);
00695 
00696    void write_on(const uostream & s) const;
00697 
00698    void read_from(const uistream & s);
00699 
00700    AD_LONG_INT& operator[](int i);
00701    AD_LONG_INT& operator()(int i);
00702    const AD_LONG_INT& operator[](int i) const;
00703    const AD_LONG_INT& operator()(int i) const;
00704 
00705    lvector operator() (const lvector & u);
00706 
00707    lvector & operator=(const lvector & t);
00708 
00709    void initialize(void);
00710    friend class lmatrix;
00711 };
00712 
00713 #ifdef OPT_LIB
00714 inline AD_LONG_INT& lvector::operator[](int i)
00715 {
00716   return v[i];
00717 }
00718 inline AD_LONG_INT& lvector::operator()(int i)
00719 {
00720   return v[i];
00721 }
00722 inline const AD_LONG_INT& lvector::operator[](int i) const
00723 {
00724   return v[i];
00725 }
00726 inline const AD_LONG_INT& lvector::operator()(int i) const
00727 {
00728   return v[i];
00729 }
00730 #endif
00731 
00732 AD_LONG_INT sum(const lvector &);
00733 
00738 class dependent_variables_information
00739 {
00740    int max_num_dependent_variables;
00741    int depvar_count;
00742    ptr_vector grad_buffer_position;
00743    lvector cmpdif_buffer_position;
00744    lvector grad_file_position;
00745    lvector cmpdif_file_position;
00746   ivector grad_file_count;
00747   ivector cmpdif_file_count;
00748    dependent_variables_information(int ndv);
00749    friend class gradient_structure;
00750 };
00751 dvar_vector_position restore_dvar_vector_position(void);
00752 dvector restore_dvar_vector_value(const dvar_vector_position & tmp);
00753 void arr_free(double_and_int *);
00754 double_and_int *arr_new(unsigned int sz);
00755 
00756 #include <gradient_structure.h>
00757 
00758 void jacobcalc(int nvar, const dmatrix & g);
00759 void jacobcalc(int nvar, const ofstream & ofs);
00760 void jacobcalc(int nvar, const uostream & ofs);
00761 
00762 #if defined(__BORLANDC__ )
00763 #if defined(__GNUC__)
00764 #if (__GNUC__ < 3)
00765 #pragma interface
00766 #endif
00767 #else
00768 #pragma interface
00769 #endif
00770 #endif
00771 
00772 //class dvect_ptr_ptr { dvector **m; };
00773 
00777 class dlink
00778 {
00779   double_and_int di;
00780   dlink* prev;
00781 
00782 public:
00783   dlink* previous() const
00784   {
00785     return prev;
00786   }
00787 
00788   //access function
00789   inline double_and_int* get_address()
00790   {
00791     return &di;
00792   }
00793 
00794   //friend tempvar();
00795   //friend class prevariable;
00796   //friend class tempvar;
00797   friend class dlist;
00798   friend void gradcalc(int nvar, const dvector & g);
00799   friend void slave_gradcalc(void);
00800   friend void gradloop();
00801   friend double_and_int *gradnew();
00802   friend void allocate_dvariable_space(void);
00803 };
00807 class dlist
00808 {
00809   dlink* last;
00810   unsigned int nlinks;
00811   dlink** dlink_addresses;
00812   char* ddlist_space;
00813   double* variables_save;
00814 
00815 public:
00816   // constructor
00817   dlist();
00818   // destructor
00819   ~dlist();
00820   // create a new link
00821   dlink* create();
00822   // append link
00823   dlink* append(dlink* link);
00824   dlink* last_remove();
00825   void initialize();
00826   void save_variables();
00827   void restore_variables();
00828 
00829   // check list integrity
00830   void check_list(void);
00831   size_t total_addresses() const;
00832 
00833   friend double_and_int *gradnew();
00834   friend void df_check_derivative_values(void);
00835   friend void df_check_derivative_values_indexed(void);
00836   friend void df_check_derivative_values_indexed_break(void);
00837   friend void funnel_gradcalc(void);
00838   friend void slave_gradcalc(void);
00839   friend void gradcalc(int nvar, const dvector& g);
00840   friend void gradloop();
00841   friend void gradient_structure::restore_variables();
00842   friend void gradient_structure::save_variables();
00843   friend void gradient_structure::jacobcalc(int nvar,
00844     const dmatrix& jac);
00845   friend void allocate_dvariable_space(void);
00846   //friend void gradient_structure::funnel_jacobcalc(void);
00847   friend void gradient_structure::jacobcalc(int nvar,
00848     const ofstream& jac);
00849   friend void gradient_structure::jacobcalc(int nvar,
00850     const uostream& jac);
00851   friend void funnel_derivatives(void);
00852 };
00853 
00854 class indvar_offset_list;
00855 
00862 class grad_stack_entry
00863 {
00864  public:
00866    void (*func) (void);
00867    double *dep_addr;    
00868    double *ind_addr1;   
00869    double mult1;        
00870    double *ind_addr2;   
00871    double mult2;        
00872  public:
00873    friend void gradcalc(int nvar, const dvector & g);
00874    friend void slave_gradcalc(void);
00875    friend void gradloop();
00876    friend void default_evaluation(void);
00877    friend class grad_stack;
00878    friend void gradient_structure::jacobcalc(int nvar,
00879      const dmatrix & jac);
00880    //friend void gradient_structure::funnel_jacobcalc(void);
00881 };
00882 void default_evaluation3ind(void);
00883 
00888 class grad_stack
00889 {
00890    grad_stack_entry *true_ptr_first;
00891    grad_stack_entry *ptr_first;
00892    grad_stack_entry *ptr_last;
00893 #ifdef __BORLANDC__
00894    long int length;
00895    long int true_length;
00896 #else
00897    size_t length;
00898    size_t true_length;
00899 #endif
00900  public:
00901    grad_stack_entry * ptr;
00902  private:
00903    //lvector * table;
00904    // js
00905    int _GRADFILE_PTR;  // should be int gradfile_handle;
00906    int _GRADFILE_PTR1;  // should be int gradfile_handle;
00907    int _GRADFILE_PTR2;  // should be int gradfile_handle;
00908    int _VARSSAV_PTR;  // should be int gradfile_handle;
00909    char gradfile_name[61];
00910    char gradfile_name1[61];
00911    char gradfile_name2[61];
00912    char var_store_file_name[61];
00913    void create_gradfile();
00914 #ifdef __BORLANDC__
00915    long end_pos;
00916    long end_pos1;
00917    long end_pos2;
00918 #else
00919    OFF_T end_pos;
00920    OFF_T end_pos1;
00921    OFF_T end_pos2;
00922 #endif
00923    //dmatrix *table;
00924  public:
00925    friend void gradcalc(int nvar, const dvector & g);
00926    friend void slave_gradcalc(void);
00927    friend void funnel_gradcalc(void);
00928    friend void default_evaluation(void);
00929    friend void default_evaluation3ind(void);
00930    friend void reset_gradient_stack(void);
00931    friend void default_evaluation4ind(void);
00932    friend void grad_chk(void);
00933    friend void gradloop();
00934    friend void cleanup_temporary_files();
00935    ostream & operator  <<(grad_stack);
00936    void print();
00937    grad_stack();
00938    ~grad_stack();
00939    void write_grad_stack_buffer(void);
00940 
00941    void set_gradient_stack(void (*func) (void),
00942      double *dep_addr, double *ind_addr1 = NULL, double mult1 = 0,
00943      double *ind_addr2 = NULL, double mult2 = 0);
00944 
00945    void set_gradient_stack(void (*func) (void),
00946      double *dep_addr, double *ind_addr1,
00947      double *ind_addr2);
00948 
00949 
00950    void set_gradient_stack0(void (*func) (void), double *dep_addr);
00951 
00952    void set_gradient_stack1(void (*func) (void),
00953      double *dep_addr, double *ind_addr1);
00954 
00955    void set_gradient_stack2(void (*func) (void),
00956      double *dep_addr, double *ind_addr1, double mult1);
00957 
00958    void set_gradient_stack4(void (*func) (void),
00959      double *dep_addr, double *ind_addr1,
00960      double *ind_addr2);
00961 
00962    void set_gradient_stack(void (*func) (void),
00963      double *dep_addr, double *ind_addr1,
00964      double mult1, double *ind_addr2, double mult2,
00965      double *ind_addr3, double mult3,
00966      double *ind_addr4, double mult4);
00967 
00968    void set_gradient_stack(void (*func) (void),
00969      double *dep_addr, double *ind_addr1,
00970      double mult1, double *ind_addr2, double mult2,
00971      double *ind_addr3, double mult3);
00972 
00973    int read_grad_stack_buffer(OFF_T & lpos);
00974    void set_gradient_stack(void (*ptr) (void));
00975    void set_gbuffer_pointers(void);
00976    //js
00977    void increment_current_gradfile_ptr(void);
00978    int decrement_current_gradfile_ptr(void);
00979    //void open_gradfile();
00980    //void close_gradfile();
00981 #ifdef _MSC_VER
00982    int gradfile_handle();
00983 #else
00984    int &gradfile_handle();
00985 #endif
00986    char *get_gradfile_name();
00987    friend class gradient_structure;
00988    //int get_ngradfiles();
00989 };
00990 
00991 
00992 #ifdef OPT_LIB
00993 
00997 inline void grad_stack::set_gradient_stack(void (*func) (void),
00998   double *dep_addr, double *ind_addr1, double mult1, double *ind_addr2,
00999   double mult2)
01000 {
01001 #ifdef NO_DERIVS
01002    if (!gradient_structure::no_derivatives)
01003    {
01004 #endif
01005 #     if defined(MYDEBUG)
01006       int wrote_buffer = 0;
01007 #     endif
01008       if (ptr > ptr_last)
01009       {
01010         // current buffer is full -- write it to disk and reset pointer
01011         // and counter
01012         this->write_grad_stack_buffer();
01013 #     if defined(MYDEBUG)
01014         wrote_buffer = 1;
01015 #     endif
01016       }
01017 #     if defined(MYDEBUG)
01018       if (wrote_buffer == 1)
01019       {
01020         cout << "WROTE BUFFER" << endl;
01021       }
01022 #     endif
01023       ptr->func = func;
01024       ptr->dep_addr = dep_addr;
01025       ptr->ind_addr1 = ind_addr1;
01026       ptr->mult1 = mult1;
01027       ptr->ind_addr2 = ind_addr2;
01028       ptr->mult2 = mult2;
01029       ptr++;
01030 #ifdef NO_DERIVS
01031    }
01032 #endif
01033 }
01034 #else
01035   //  void grad_stack::set_gradient_stack(void (* func)(void),
01036    //   double * dep_addr,double * ind_addr1, double mult1, double * ind_addr2,
01037     //  double mult2);
01038 #endif
01039 
01044 inline void grad_stack::set_gradient_stack(void (*func) (void),
01045   double *dep_addr, double *ind_addr1, double mult1, double *ind_addr2,
01046   double mult2, double *ind_addr3, double mult3)
01047 {
01048 #ifdef NO_DERIVS
01049    if (!gradient_structure::no_derivatives)
01050    {
01051 #endif
01052       if (ptr > ptr_last)
01053       {
01054          // current buffer is full -- write it to disk and reset pointer
01055          // and counter
01056          this->write_grad_stack_buffer();
01057       }
01058       ptr->func = NULL;
01059       ptr->dep_addr = dep_addr;
01060       ptr->ind_addr1 = ind_addr1;
01061       ptr->mult1 = mult1;
01062       ptr->ind_addr2 = ind_addr2;
01063       ptr->mult2 = mult2;
01064       ptr++;
01065       if (ptr > ptr_last)
01066       {
01067          // current buffer is full -- write it to disk and reset pointer
01068          // and counter
01069          this->write_grad_stack_buffer();
01070       }
01071       ptr->func = func;
01072       ptr->ind_addr1 = ind_addr3;
01073       ptr->mult1 = mult3;
01074       ptr++;
01075 #ifdef NO_DERIVS
01076    }
01077 #endif
01078 }
01079 
01084 inline void grad_stack::set_gradient_stack(void (*func) (void),
01085   double *dep_addr, double *ind_addr1, double mult1, double *ind_addr2,
01086   double mult2, double *ind_addr3, double mult3, double *ind_addr4,
01087   double mult4)
01088 {
01089 #ifdef NO_DERIVS
01090    if (!gradient_structure::no_derivatives)
01091    {
01092 #endif
01093       if (ptr > ptr_last)
01094       {
01095          // current buffer is full -- write it to disk and reset pointer
01096          // and counter
01097          this->write_grad_stack_buffer();
01098       }
01099       ptr->func = NULL;
01100       ptr->dep_addr = dep_addr;
01101       ptr->ind_addr1 = ind_addr1;
01102       ptr->mult1 = mult1;
01103       ptr->ind_addr2 = ind_addr2;
01104       ptr->mult2 = mult2;
01105       ptr++;
01106       if (ptr > ptr_last)
01107       {
01108          // current buffer is full -- write it to disk and reset pointer
01109          // and counter
01110          this->write_grad_stack_buffer();
01111       }
01112       ptr->func = func;
01113       ptr->ind_addr1 = ind_addr3;
01114       ptr->mult1 = mult3;
01115       ptr->ind_addr2 = ind_addr4;
01116       ptr->mult2 = mult4;
01117       ptr++;
01118 #ifdef NO_DERIVS
01119    }
01120 #endif
01121 }
01122 
01127 inline void grad_stack::set_gradient_stack(void (*func) (void),
01128   double *dep_addr, double *ind_addr1, double *ind_addr2)
01129 {
01130 #ifdef NO_DERIVS
01131    if (!gradient_structure::no_derivatives)
01132    {
01133 #endif
01134       if (ptr > ptr_last)
01135       {
01136          // current buffer is full -- write it to disk and reset pointer
01137          // and counter
01138          this->write_grad_stack_buffer();
01139       }
01140       ptr->func = func;
01141       ptr->dep_addr = dep_addr;
01142       ptr->ind_addr1 = ind_addr1;
01143       ptr->ind_addr2 = ind_addr2;
01144       ptr++;
01145 #ifdef NO_DERIVS
01146    }
01147 #endif
01148 }
01149 
01154 inline void grad_stack::set_gradient_stack2(void (*func) (void),
01155   double *dep_addr, double *ind_addr1, double mult1)
01156 {
01157 #ifdef NO_DERIVS
01158    if (!gradient_structure::no_derivatives)
01159    {
01160 #endif
01161       if (ptr > ptr_last)
01162       {
01163          // current buffer is full -- write it to disk and reset pointer
01164          // and counter
01165          this->write_grad_stack_buffer();
01166       }
01167       ptr->func = func;
01168       ptr->dep_addr = dep_addr;
01169       ptr->ind_addr1 = ind_addr1;
01170       ptr->mult1 = mult1;
01171       ptr++;
01172 #ifdef NO_DERIVS
01173    }
01174 #endif
01175 }
01176 
01181 inline void grad_stack::set_gradient_stack4(void (*func) (void),
01182   double *dep_addr, double *ind_addr1, double *ind_addr2)
01183 {
01184 #ifdef NO_DERIVS
01185    if (!gradient_structure::no_derivatives)
01186    {
01187 #endif
01188       if (ptr > ptr_last)
01189       {
01190          // current buffer is full -- write it to disk and reset pointer
01191          // and counter
01192          this->write_grad_stack_buffer();
01193       }
01194       ptr->func = func;
01195       ptr->dep_addr = dep_addr;
01196       ptr->ind_addr1 = ind_addr1;
01197       ptr->ind_addr2 = ind_addr2;
01198       ptr++;
01199 #ifdef NO_DERIVS
01200    }
01201 #endif
01202 }
01203 
01208 inline void grad_stack::set_gradient_stack(void (*func) (void))
01209 {
01210 #ifdef NO_DERIVS
01211    if (!gradient_structure::no_derivatives)
01212    {
01213 #endif
01214       if (ptr > ptr_last)
01215       {
01216          // current buffer is full -- write it to disk and reset pointer
01217          // and counter
01218          this->write_grad_stack_buffer();
01219       }
01220 
01221       ptr->dep_addr = NULL;
01222       ptr->func = func;
01223       // want to put a long int into the memory space of a double
01224       ptr->ind_addr2 = NULL;
01225       ptr->mult2 = 0;
01226       ptr++;
01227 #ifdef NO_DERIVS
01228    }
01229 #endif
01230 }
01231 
01236 class indvar_offset_list
01237 {
01238    // The number of independent variables
01239    int nvar;
01240    double **address;
01241 
01242  public:
01243    friend class gradient_structure;
01244    inline double *get_address(const int &i)
01245    {
01246       return address[i];
01247    }
01248    void put_address(unsigned int &i, double *iaddress)
01249    {
01250       address[i] = iaddress;
01251       //  cerr << "In put_address i = " << i << "\n";
01252    }
01253 };
01254 
01255 void gradfree(dlink *);
01256 
01257 class prevariable_position;
01258 
01266 class prevariable
01267 {
01268 protected:
01269 #ifndef __SUN__
01270 
01273   prevariable()
01274   {
01275   }
01276 #endif
01277 #ifndef __NDPX__
01278    prevariable(double_and_int * u)
01279    {
01280       v = u;
01281    }
01282 #endif
01283 
01284 public:
01285   double_and_int* v; 
01286 
01287    friend class dvar_vector_iterator;
01288    friend class dvar_vector;
01289    friend class dvar_matrix;
01290    friend class dvar3_array;
01291    //shclass sc;
01292    friend class indvar_offset_list;
01293    friend class gradient_structure;
01294    friend double_and_int *gradnew();
01295    friend void make_indvar_list(int, dvariable *);
01296 
01297    friend class banded_symmetric_dvar_matrix;
01298    friend class banded_lower_triangular_dvar_matrix;
01299    friend double &value(const prevariable & v1);
01300 
01301    friend double *address(const prevariable & v1);
01302 
01303    //void gradfree(dlink * v)
01304 
01305    friend prevariable & operator*(const prevariable& v1, const prevariable& v2);
01306 
01307    friend prevariable & operator*(double v1, const prevariable & v2);
01308 
01309    friend prevariable & operator*(const prevariable & v1, double v2);
01310 
01311    friend prevariable & operator/(const prevariable& t1, const prevariable& t2);
01312 
01313    friend prevariable & operator/(double t1, const prevariable & t2);
01314 
01315    friend prevariable & operator/(const prevariable & t1, double t2);
01316 
01317    friend prevariable & sin(const prevariable & t1);
01318 
01319    friend prevariable & fabs(const prevariable & t1);
01320    friend prevariable & sigmoid(const prevariable & t1);
01321 
01322    //"smoothed absolute value function
01323    friend prevariable & sfabs(const prevariable & t1);
01324 
01325    friend prevariable & sqrt(const prevariable & t1);
01326    friend prevariable & sqr(const prevariable & t1);
01327 
01328    friend prevariable & exp(const prevariable & t1);
01329 
01330    friend prevariable & atan(const prevariable & t1);
01331 
01332    friend prevariable & tan(const prevariable & t1);
01333    friend prevariable & tanh(const prevariable & t1);
01334 
01335    friend prevariable & atan2(const prevariable & t1,
01336      const prevariable & t2);
01337    friend prevariable & atan2(const prevariable & t1, double t2);
01338    friend prevariable & atan2(double t1, const prevariable & t2);
01339 
01340    friend prevariable & acos(const prevariable & t1);
01341 
01342    friend prevariable & asin(const prevariable & t1);
01343 
01344    friend prevariable & cos(const prevariable & t1);
01345    friend prevariable & sinh(const prevariable & t1);
01346    friend prevariable & cosh(const prevariable & t1);
01347 
01348    friend prevariable & log(const prevariable & t1);
01349    friend prevariable & log10(const prevariable & t1);
01350 
01351    friend prevariable & ldexp(const prevariable &, const int &);
01352 
01353 
01354  public:
01355    void save_prevariable_position(void) const;
01356    prevariable_position restore_prevariable_position(void);
01357    void save_prevariable_value(void) const;
01358    double restore_prevariable_value(void);
01359    double restore_prevariable_derivative(void);
01360 
01361 
01362    inline double *xadr()
01363    {
01364       return (&(v->x));
01365    }
01366    inline double &xval()
01367    {
01368       return ((v->x));
01369    }
01370 
01371    inline double_and_int *&get_v()
01372    {
01373       return v;
01374    }
01375    inline double_and_int *get_v() const
01376    {
01377       return v;
01378    }
01379 
01380    prevariable & operator=(const prevariable &);
01381    prevariable & operator=(double);
01382 #if (__BORLANDC__  >= 0x0540)
01383    prevariable & operator=(const prevariable &) const;
01384    prevariable & operator=(double) const;
01385 #endif
01386 
01387    int operator==(const prevariable & v1) const;
01388    int operator<=(const prevariable & v1) const;
01389    int operator>=(const prevariable & v1) const;
01390    int operator>(const prevariable & v1) const;
01391    int operator<(const prevariable & v1) const;
01392    int operator!=(const prevariable & v1) const;
01393 
01394    int operator==(double v1) const;
01395    int operator<=(double v1) const;
01396    int operator>=(double v1) const;
01397    int operator>(double v1) const;
01398    int operator<(double v1) const;
01399    int operator!=(double v1) const;
01400 #if defined(USE_DDOUBLE)
01401    int operator==(int v1) const;
01402    int operator<=(int v1) const;
01403    int operator>=(int v1) const;
01404    int operator>(int v1) const;
01405    int operator<(int v1) const;
01406    int operator!=(int v1) const;
01407 #endif
01408 
01409  public:
01410 #ifdef __SUN__
01411    prevariable(void)
01412    {
01413    }
01414 #endif
01415 #ifdef __NDPX__
01416    prevariable(double_and_int * u)
01417    {
01418       v = u;
01419    }
01420 #endif
01421 
01422    void initialize(void);
01423 
01424    friend char *fform(const char *, const prevariable &);
01425 
01426    void operator+=(const prevariable & t1);
01427    void operator +=(double t1);
01428 
01429    void operator-=(const prevariable & t1);
01430    void operator -=(double t1);
01431 
01432    void operator/=(const prevariable & v1);
01433    void operator /=(double v1);
01434 
01435    void operator*=(const prevariable & v1);
01436    void operator *=(double v1);
01437 
01438    friend prevariable& operator+(const prevariable& v1, const prevariable& v2);
01439 
01440    friend prevariable & operator+(double v1, const prevariable & v2);
01441 
01442    friend prevariable & operator+(const prevariable & v1, double v2);
01443 
01444    friend prevariable & operator-(const prevariable & v1);
01445 
01446    friend prevariable & operator-(const prevariable& v1, const prevariable& v2);
01447 
01448    friend prevariable & operator-(double v1, const prevariable & v2);
01449 
01450    friend prevariable & operator-(const prevariable & v1, double v2);
01451 
01452    friend prevariable & pow(const prevariable & t1, const prevariable & t2);
01453 
01454    friend prevariable & pow(const prevariable & t1, double t2);
01455 
01456    friend prevariable & pow(double t1, const prevariable & t2);
01457 };
01458 
01459 inline double &value(const prevariable & v1)
01460 {
01461    return v1.v->x;
01462 }
01463 
01464 inline double *address(const prevariable & v1)
01465 {
01466    return (&(v1.v->x));
01467 }
01468 
01469 
01470 prevariable & operator<<(const prevariable & v1, const prevariable & v2);
01471 dvar_vector & operator<<(const dvar_vector & v1, const dvar_vector & v2);
01472 dvar_matrix & operator<<(const dvar_matrix & v1, const dvar_matrix & v2);
01473 
01474 class df1_one_variable;
01475 class df1_two_variable;
01476 class df1_three_variable;
01477 
01482 class dvariable:public prevariable
01483 {
01484  public:
01485    dvariable();
01486    ~dvariable();
01487    dvariable(double t);
01488    dvariable(const int &t);
01489    dvariable(kkludge_object);
01490    dvariable(const prevariable &);
01491    dvariable & operator=(const prevariable &);
01492    dvariable & operator =(const df1_one_variable & v);
01493    dvariable & operator =(const df1_two_variable & v);
01494    dvariable & operator =(const df1_three_variable & v);
01495    dvariable & operator=(double);
01496 #if defined(USE_DDOUBLE)
01497 #  undef double
01498    dvariable & operator=(double);
01499 #  define double dd_real
01500 #endif
01501    dvariable(const dvariable &);
01502 //#  if (__BORLANDC__  > 0x0520)
01503 //     dvariable& operator+=(const prevariable&);
01504 //#  endif
01505 };
01506 
01507 #if defined(max)
01508 #undef max
01509 #endif
01510 #if defined(min)
01511 #undef min
01512 #endif
01513 
01518 class funnel_dvariable:public dvariable
01519 {
01520  public:
01521    dvariable & operator=(const prevariable &);
01522 };
01523 
01524 dvar_vector operator*(const dvar_vector & t1, double x);
01525 dvar_vector operator/(double x, const dvar_vector & t1);
01526 dvar_vector operator/(const dvar_vector & t1, double x);
01527 dvar_vector operator+(double x, const dvar_vector & t1);
01528 dvar_vector operator+(const dvar_vector & t1, double x);
01529 dvar_vector operator-(double x, const dvar_vector & t1);
01530 dvar_vector operator-(const dvar_vector & t1, double x);
01531 dvar_vector operator-(const dvar_vector & t1);
01532 dvar_vector operator*(const dvar_vector & t1, const prevariable & x);
01533 dvar_vector operator/(const prevariable & x, const dvar_vector & t1);
01534 dvar_vector operator/(const dvar_vector & t1, const prevariable & x);
01535 dvar_vector operator+(const prevariable & x, const dvar_vector & t1);
01536 dvar_vector operator+(const dvar_vector & t1, const prevariable & x);
01537 dvar_vector operator-(const prevariable & x, const dvar_vector & t1);
01538 dvar_vector operator-(const dvar_vector & t1, const prevariable & x);
01539 dvar_vector operator-(const dvector & t1, const prevariable & x);
01540 dvar_vector operator*(const dvector & t1, const prevariable & x);
01541 dvar_vector operator*(const prevariable & x, const dvector & t1);
01542 
01543 dvector operator*(const dvector & t1, double x);
01544 dvector operator/(double x, const dvector & t1);
01545 dvector operator/(const dvector & t1, double x);
01546 dvector operator+(double x, const dvector & t1);
01547 dvector operator+(const dvector & t1, double x);
01548 dvector operator-(double x, const dvector & t1);
01549 dvector operator-(const dvector & t1, double x);
01550 dvector operator-(const dvector & t1);
01551 
01552 double min(const dmatrix &);
01553 double max(const dmatrix &);
01554 int max(const imatrix &);
01555 double max(const dvector &);
01556 dvariable max(const dvar_vector &);
01557 dvariable min(const dvar_vector &);
01558 
01559 dmatrix symmetrize(const dmatrix & m1);
01560 dvector eigenvalues(const dmatrix & m1);
01561 dmatrix eigenvectors(const dmatrix & m1);
01562 dmatrix eigenvectors(const dmatrix & m1, const dvector & eigenvalues);
01563 
01564 dvar_matrix symmetrize(const dvar_matrix & m1);
01565 dvar_vector eigenvalues(const dvar_matrix & m1);
01566 dvar_matrix eigenvectors(const dvar_matrix & m1);
01567 
01568 dmatrix outer_prod(const dvector & t1, const dvector & t2);
01569 dvar_matrix outer_prod(const dvar_vector & t1, const dvar_vector & t2);
01570 dvar_matrix outer_prod(const dvector & t1, const dvar_vector & t2);
01571 dvar_matrix outer_prod(const dvar_vector & t1, const dvector & t2);
01572 dmatrix operator*(double x, const dmatrix & m);
01573 dmatrix operator*(const dmatrix & m, double d);
01574 dmatrix operator/(const dmatrix & m, double d);
01575 dmatrix operator/(double d, const dmatrix & m);
01576 dmatrix operator+(double x, const dmatrix & m);
01577 dvar_matrix operator +(const dvariable & x, const dmatrix & m);
01578 dvar_matrix operator -(const dvariable & x, const dmatrix & m);
01579 dmatrix operator+(const dmatrix & m, double d);
01580 dmatrix operator-(double x, const dmatrix & m);
01581 dmatrix operator-(const dmatrix & m, double d);
01582 dvar_matrix operator/(const dvar_matrix & m, const prevariable & x);
01583 dvar_matrix operator/(const dmatrix & m, const prevariable & x);
01584 dvar_matrix operator/(const dvar_matrix & m, double x);
01585 dvar_matrix operator/(double x, const dvar_matrix & m);
01586 dvar_matrix operator/(const prevariable & x, const dvar_matrix & m);
01587 
01588 dvar_matrix operator*(const prevariable & x, const dmatrix & m);
01589 dvar_matrix operator*(const dvar_matrix & m, const prevariable & x);
01590 dvar_matrix operator*(const prevariable & x, const dvar_matrix & m);
01591 dvar_matrix operator*(double x, const dvar_matrix & m);
01592 
01593 dvector operator&(const dvector & t1, const dvector & t2);
01594 dvar_vector operator&(const dvar_vector & t1, const dvar_vector & t2);
01595 
01596 ivector column(const imatrix& m, int i);
01597 dvector extract_column(const dmatrix & m, int i);
01598 dvector column(const dmatrix & m, int i);
01599 dvector extract_row(const dmatrix & m, int j);
01600 dvector row(const dmatrix & m, int j);
01601 dvar_vector extract_column(const dvar_matrix & m, int i);
01602 dvar_vector column(const dvar_matrix & m, int i);
01603 dvector column_value(const dvar_matrix & m, int i);
01604 dvar_vector extract_row(const dvar_matrix & m, int j);
01605 dvar_vector row(const dvar_matrix & m, int j);
01606 
01607 // dvector mathematical functions
01608 dvector sin(const dvector & t1);
01609 dvector sqrt(const dvector & t1);
01610 dvector sqr(const dvector & t1);
01611 dvector exp(const dvector & t1);
01612 dvector mfexp(const dvector & t1);
01613 dvector mfexp(const dvector & t1, double d);
01614 dvector atan(const dvector & t1);
01615 dvector tan(const dvector & t1);
01616 dvector tanh(const dvector & t1);
01617 dvector atan2(const dvector & t1, const dvector & t2);
01618 dvector atan2(const dvector & t1, double t2);
01619 dvector atan2(double t1, const dvector & t2);
01620 dvector acos(const dvector & t1);
01621 dvector asin(const dvector & t1);
01622 dvector cos(const dvector & t1);
01623 dvector sinh(const dvector & t1);
01624 dvector cosh(const dvector & t1);
01625 dvector log(const dvector & t1);
01626 dvector log10(const dvector & t1);
01627 dvector pow(const dvector & t1, double);
01628 dvector pow(const dvector & t1, int);
01629 dvector pow(double, const dvector & t1);
01630 ivector pow(const ivector & v1, int x);
01631 ivector pow(int x, const ivector & v1);
01632 // end of dvector mathematical functions
01633 
01634 // dvar_vector mathematical functions
01635 dvar_vector sin(const dvar_vector & t1);
01636 dvar_vector sqrt(const dvar_vector & t1);
01637 dvar_vector sqr(const dvar_vector & t1);
01638 dvar_vector exp(const dvar_vector & t1);
01639 dvar_vector mfexp(const dvar_vector & t1);
01640 dvar_vector mfexp(const dvar_vector & t1, double d);
01641 dvar_vector atan(const dvar_vector & t1);
01642 dvar_vector tan(const dvar_vector & t1);
01643 dvar_vector tanh(const dvar_vector & t1);
01644 dvar_vector atan2(const dvar_vector & t1, const dvar_vector & t2);
01645 dvar_vector atan2(const dvar_vector & t1, double t2);
01646 dvar_vector atan2(double t1, const dvar_vector & t2);
01647 dvar_vector acos(const dvar_vector & t1);
01648 dvar_vector asin(const dvar_vector & t1);
01649 dvar_vector cos(const dvar_vector & t1);
01650 dvar_vector sinh(const dvar_vector & t1);
01651 dvar_vector cosh(const dvar_vector & t1);
01652 dvar_vector log(const dvar_vector & t1);
01653 dvar_vector log10(const dvar_vector & t1);
01654 dvar_vector pow(const dvar_vector &, const dvar_vector & t1);
01655 dvar_vector pow(const dvar_vector &, const dvector & t1);
01656 dvar_vector pow(const dvector &, const dvar_vector & t1);
01657 dvector pow(const dvector &, const dvector & t1);
01658 dvar_vector pow(const dvar_vector & t1, double);
01659 dvar_vector pow(const dvar_vector & t1, int);
01660 dvar_vector pow(const dvar_vector & t1, const prevariable &);
01661 dvar_vector pow(const dvector & t1, const prevariable &);
01662 dvar_vector pow(const prevariable &, const dvar_vector & t1);
01663 dvar_vector pow(const dvector & x, const dvar_vector & a);
01664 // end of dvar_vector mathematical functions
01665 
01666 // dmatrix mathematical functions
01667 dmatrix exp(const dmatrix & m);
01668 dmatrix mfexp(const dmatrix & m);
01669 dmatrix mfexp(const dmatrix & m, double d);
01670 dmatrix sqrt(const dmatrix & m);
01671 dmatrix sqr(const dmatrix & m);
01672 dmatrix pow(const dmatrix & m, double e);
01673 dmatrix pow(const dmatrix & m, int e);
01674 dmatrix log(const dmatrix & m);
01675 dmatrix sin(const dmatrix & m);
01676 dmatrix cos(const dmatrix & m);
01677 dmatrix tan(const dmatrix & m);
01678 dmatrix elem_div(const dmatrix & m, const dmatrix & m2);
01679 dmatrix elem_prod(const dmatrix & m, const dmatrix & m2);
01680 // end of dmatrix mathematical functions
01681 
01682 //  dvar_matrix mathematical functions
01683 dvar_matrix exp(const dvar_matrix & m);
01684 dvar_matrix mfexp(const dvar_matrix & m);
01685 dvar_matrix mfexp(const dvar_matrix & m, double d);
01686 dvar_matrix sqrt(const dvar_matrix & m);
01687 dvar_matrix sqr(const dvar_matrix & m);
01688 dvar_matrix log(const dvar_matrix & m);
01689 dvar_matrix sin(const dvar_matrix & m);
01690 dvar_matrix cos(const dvar_matrix & m);
01691 dvar_matrix tan(const dvar_matrix & m);
01692 dvar_matrix pow(const dvar_matrix & m, double e);
01693 dvar_matrix pow(const dvar_matrix & m, const prevariable & e);
01694 dvar_matrix pow(const dmatrix & m, const prevariable & e);
01695 dvar_matrix pow(const dvar_matrix & m, int e);
01696 dvar_matrix elem_prod(const dvar_matrix & m, const dvar_matrix & m2);
01697 dvar_matrix elem_prod(const dvar_matrix & m, const dmatrix & m2);
01698 dvar_matrix elem_prod(const dmatrix & m, const dvar_matrix & m2);
01699 dvar_matrix elem_div(const dvar_matrix & m, const dvar_matrix & m2);
01700 dvar_matrix elem_div(const dvar_matrix & m, const dmatrix & m2);
01701 dvar_matrix elem_div(const dmatrix & m, const dvar_matrix & m2);
01702 //  end of dvar_matrix mathematical functions
01703 
01704 int min(const ivector & t1);
01705 int max(const ivector & t1);
01706 int Max(const ivector & t1);
01707 
01708 double mfexp(double);
01709 double mfexp(double, double bound);
01710 dvariable mfexp(const prevariable &);
01711 dvariable mfexp(const prevariable &, double bound);
01712 
01713 #if defined(THREAD_SAFE)
01714 
01717 class ts_vector_shapex
01718 {
01719  public:
01720    void *trueptr;
01721    ts_vector_shapex(int lb, int ub, void *p):index_min(lb),
01722       index_max(ub), ncopies(0), trueptr(p)
01723    {
01724    }
01725    void *get_truepointer(void)
01726    {
01727       return trueptr;
01728    }
01729    //friend class dvector;
01730    friend class ivector;
01731    //friend class tdvector;
01732    friend class dvar_vector;
01733 
01734 #  if defined(USE_VECTOR_SHAPE_POOL)
01735    static ts_vector_shape_pool **xpool;
01736    void *operator  new(size_t);
01737    void operator  delete(void *ptr, size_t n);
01738 #  endif
01739 
01740    unsigned int ncopies;
01741    void shift(int min);
01742    int index_min;
01743    int index_max;
01744  private:
01745    friend class subdvector;
01746    friend class lvector;
01747    friend class ptr_vector;
01748  public:
01749    int decr_ncopies(void)
01750    {
01751       return --ncopies;
01752    }
01753    int get_ncopies(void)
01754    {
01755       return ncopies;
01756    }
01757    int incr_ncopies(void)
01758    {
01759       return ++ncopies;
01760    }
01761    int indexmin()
01762    {
01763       return index_min;
01764    }
01765    int indexmax()
01766    {
01767       return index_max;
01768    }
01769 };
01770 #endif
01771 
01772 #include "vector_shapex.h"
01773 
01778 class predvector
01779 {
01780    dvector *p;
01781    int lb;
01782    int ub;
01783    inline predvector(dvector * _p, int _lb, int _ub)
01784    {
01785       p = _p;
01786       lb = _lb, ub = _ub;
01787    }
01788    friend class dvector;
01789 };
01790 
01795 class predvar_vector
01796 {
01797    dvar_vector *p;
01798    int lb;
01799    int ub;
01800    inline predvar_vector(dvar_vector * _p, int _lb, int _ub)
01801    {
01802       p = _p;
01803       lb = _lb, ub = _ub;
01804    }
01805    friend class dvar_vector;
01806 };
01807 
01808 #include "dvector.h"
01809 
01814 class independent_variables:public dvector
01815 {
01816  public:
01817    independent_variables(const independent_variables & v):dvector(v)
01818    {
01819    }
01820 
01821    independent_variables(int ncl, int ncu):dvector(ncl, ncu)
01822    {
01823    }
01824    // makes an array [ncl..ncu]
01825 
01826    independent_variables(unsigned int sz, double *x):dvector(sz, x)
01827    {
01828    }
01829 
01830    independent_variables & operator=(const dvector & t);
01831 };
01832 
01833 dvariable dfatan1(dvariable, double, double, const prevariable & fpen);
01834 
01835 double boundp(double x, double fmin, double fmax, const double &fpen);
01836 double boundp(double x, double fmin, double fmax);
01837 
01838 dvariable boundp(const prevariable & x, double fmin, double fmax,
01839   const prevariable & fpen);
01840 dvariable boundp(const prevariable & x, double fmin, double fmax,
01841   const prevariable & fpen, double s);
01842 
01843 double nd2fboundp(double x, double minb, double maxb, const double &pen);
01844 double boundpin(double x, double fmin, double fmax);
01845 double boundpin(const prevariable & x, double fmin, double fmax);
01846 double boundpin(const prevariable & x, double fmin, double fmax, double s);
01847 
01848 double dmin(double, double);
01849 
01850 double dmax(double i, double j);
01851 
01852 #include <stdlib.h>
01853 #ifdef __TURBOC__
01854   #include <alloc.h>
01855 #endif
01856 
01857 double sigmoid(double t1);
01858 
01863 class mat_shape
01864 {
01865    unsigned int ncopies;
01866    unsigned int nrows;
01867    unsigned int ncols;
01868    int row_min;
01869    int row_max;
01870    int col_min;
01871    int col_max;
01872    mat_shape(int rl, int ru, int cl = 0, int cu = -1);
01873    mat_shape()
01874    {
01875    };
01876    void colshift(int min);
01877    void rowshift(int min);
01878 
01879    //friend class const_dmatrix;
01880    friend class dmatrix;
01881    friend class sdmatrix;
01882    friend class dvar_matrix;
01883    friend class imatrix;
01884    friend class lmatrix;
01885    friend class i3_array;
01886 };
01887 
01892 class mat_shapex
01893 {
01894  public:
01895    void *trueptr;
01896    unsigned int ncopies;
01897    mat_shapex(const void *m)
01898    {
01899       trueptr = (void *) m;
01900       ncopies = 0;
01901    }
01902    mat_shapex()
01903    {
01904       trueptr = NULL;
01905       ncopies = 0;
01906    };
01907 
01908    void *get_pointer(void)
01909    {
01910       return trueptr;
01911    }
01912    friend class dmatrix;
01913    friend class sdmatrix;
01914    friend class dvar_matrix;
01915    friend class imatrix;
01916    friend class i3_array;
01917    friend class lmatrix;
01918 };
01919 
01920 class arr_link;
01921 
01926 class arr_list
01927 {
01928    arr_link *last;
01929    arr_link *free_last;
01930    unsigned long int last_offset;
01931    unsigned long int max_last_offset;
01932  public:
01933    unsigned long int number_arr_links;
01934    friend class arr_link;
01935 
01936  public:
01937    arr_list(void)
01938    {
01939       last = 0;
01940       free_last = 0;
01941       last_offset = 0;
01942       max_last_offset = 0;
01943       number_arr_links = 0;
01944    }
01945    unsigned long int get_last_offset()
01946    {
01947       return last_offset;
01948    }
01949    unsigned long int get_number_arr_links()
01950    {
01951       return number_arr_links;
01952    }
01953    unsigned long int get_max_last_offset()
01954    {
01955       return max_last_offset;
01956    }
01957    void reset_max_last_offset()
01958    {
01959       max_last_offset = 0;
01960    }
01961    friend double_and_int *arr_new(unsigned int);
01962    friend void arr_free(double_and_int *);
01963    friend void arr_remove(arr_link **);
01964    friend void arr_free_list_remove(arr_link **);
01965    friend void arr_free_add(arr_link *);
01966    friend void arr_free_remove(arr_link *);
01967 };
01968 
01973 class arr_link
01974 {
01975 #if defined(USE_VECTOR_SHAPE_POOL)
01976    static vector_shape_pool *xpool;
01977    void *operator new(size_t);
01978    void operator delete(void* ptr, size_t)
01979    {
01980       xpool->free(ptr);
01981    }
01982 #endif
01983    arr_link *prev;
01984    arr_link *next;
01985    arr_link *free_prev;
01986    arr_link *free_next;
01987    unsigned int status;
01988    // unsigned int     free_list_status;
01989    unsigned int size;
01990    unsigned long int offset;
01991  public:
01992    arr_link();
01993 
01994    friend double_and_int *arr_new(unsigned int);
01995    friend void arr_free(double_and_int *);
01996    friend void arr_remove(arr_link **);
01997    friend void arr_free_remove(arr_link *);
01998    friend void arr_free_add(arr_link *);
01999 };
02000 
02001 #if defined(__NUMBERVECTOR__)
02002 class param_init_number_vector;
02003 class param_init_bounded_number_vector;
02004 class param_init_bounded_number_matrix;
02005 class param_init_vector_vector;
02006 class param_init_bounded_vector_vector;
02007 #endif
02008 
02012 class dvar_vector
02013 {
02014 public:
02015   double_and_int* va;
02016   int index_min;
02017   int index_max;
02018   arr_link* link_ptr;
02019   vector_shapex* shape;
02020 
02021  public:
02022    dvar_vector operator -();
02023 
02024    dvar_vector & operator --(void)
02025    {
02026       index_min--;
02027       index_max--;
02028       va++;
02029       return *this;
02030    }
02031    dvar_vector & operator ++(void)
02032    {
02033       index_min++;
02034       index_max++;
02035       va--;
02036       return *this;
02037    }
02038    dvar_vector sub(int lb, int ub)
02039    {
02040       return predvar_vector(this, lb, ub);
02041    }
02042    dvar_vector operator () (int lb, int ub)
02043    {
02044       return predvar_vector(this, lb, ub);
02045    }
02046    void shallow_copy(const dvar_vector &);
02047    int operator!(void) const
02048    {
02049       return (shape == NULL);
02050    }
02051    friend class dvar_matrix;
02052    friend class dvar3_array;
02053    friend class banded_symmetric_dvar_matrix;
02054    friend class banded_lower_triangular_dvar_matrix;
02055    friend class banded_symmetric_dmatrix;
02056    friend class banded_lower_triangular_dmatrix;
02057 
02058    void fill_randpoisson(double lambda, const random_number_generator & rng);
02059    void fill_randnegbinomial(double lambda, double tau,
02060      const random_number_generator & rng);
02061    prevariable elem(int i)
02062    {
02063       return (va + i);
02064    }
02065 
02066    double &elem_value(int i)
02067    {
02068       return (va[i].x);
02069    }
02070 
02071    double_and_int *get_va()
02072    {
02073       return va;
02074    }
02075 
02076    prevariable elem(int i) const
02077    {
02078       return (va + i);
02079    }
02080 
02081    double &elem_value(int i) const
02082    {
02083       return (va[i].x);
02084    }
02085 
02086    double_and_int *get_va() const
02087    {
02088       return va;
02089    }
02090 
02091    friend dvar_matrix operator*(const dvar_matrix & m1, const dmatrix & m2);
02092 
02093    void deallocate();
02094    dvar_vector(const dvar_vector &);
02095    dvar_vector(const predvar_vector &);
02096    dvar_vector();
02097    dvar_vector(int ncl, int ncu); // makes an array [ncl..ncu]
02098    dvar_vector(int ncl, int ncu, kkludge_object);
02099 
02100    //dvar_vector(const ad_integer&,const ad_integer&);
02101    dvar_vector(unsigned int sz, double *x);
02102    dvar_vector(const independent_variables &);
02103    friend char *fform(const char *, const dvar_vector &);
02104 #   if defined(__NUMBERVECTOR__)
02105    dvar_vector(const param_init_number_vector &);
02106    dvar_vector(const param_init_bounded_number_vector &);
02107 #   endif
02108    dvar_vector(const dvector &);
02109    dvar_vector(const char *);
02110    ~dvar_vector();
02111    void allocate(int, int);
02112    void allocate(void);
02113    void allocate(const dvector &);
02114    void allocatec(const dvar_vector &);
02115    void allocate(const dvar_vector &);
02116 
02117    void allocate(const ad_integer &, const ad_integer &);
02118    void initialize(const dvector & ww);
02119    void initialize(void);
02120    void save_dvar_vector_position(void) const;
02121    void save_dvar_vector_value(void) const;
02122    void write_on(const ostream &) const;
02123    void write_on(const uostream &) const;
02124    void read_from(const istream &);
02125    void read_from(const uistream &);
02126    // returns the minimum allowable index
02127    int indexmin() const
02128    {
02129       return index_min;
02130    }
02131    // returns the maximum allowable index
02132    int indexmax() const
02133    {
02134       return index_max;
02135    }
02136    // returns the number of elements
02137    int size() const
02138    {
02139       return (index_max - index_min + 1);
02140    }
02141    dvar_vector & shift(int min);
02142 
02143 #ifdef OPT_LIB
02144   #if defined(__NDPX__) || defined(__SUN__)
02145    inline prevariable operator() (register int i)
02146    {
02147       return (va + i);
02148    }
02149    inline prevariable operator[] (register int i)
02150    {
02151       return (va + i);
02152    }
02153    inline const prevariable operator() (int i) const
02154    {
02155       return (va + i);
02156    }
02157    inline const prevariable operator[] (int i) const
02158    {
02159       return (va + i);
02160    }
02161   #else
02162    inline prevariable operator() (int i)
02163    {
02164       return (va + i);
02165    }
02166    inline prevariable operator[] (int i)
02167    {
02168       return (va + i);
02169    }
02170    inline const prevariable operator() (int i) const
02171    {
02172       return (va + i);
02173    }
02174    inline const prevariable operator[] (int i) const
02175    {
02176       return (va + i);
02177    }
02178   #endif
02179 #else
02180    prevariable operator[] (int i);
02181    prevariable operator() (int i);
02182    const prevariable operator[] (int i) const;
02183    const prevariable operator() (int i) const;
02184 #endif
02185 
02186    double *initpointer(void)
02187    {
02188       return ((double *) (va + indexmin()));
02189    }
02190    const double *initpointer(void) const
02191    {
02192       return ((double *) (va + indexmin()));
02193    }
02194    dvar_vector operator() (const lvector &);
02195    //dvar_vector operator()(int,int);
02196    dvar_vector operator () (const ivector & u);
02197    dvar_vector & operator+=(const prevariable & d);
02198    dvar_vector & operator+=(double d);
02199    dvar_vector & operator/=(const prevariable & d);
02200    //dvar_vector& operator*=(const dvariable& d);
02201    dvar_vector & operator*=(const prevariable & d);
02202    dvar_vector & operator*=(double d);
02203    dvar_vector & operator/=(double d);
02204    dvar_vector & operator-=(const prevariable & d);
02205    dvar_vector & operator-=(double d);
02206    dvar_vector & operator+=(const dvector & v1);
02207    dvar_vector & operator-=(const dvector & v1);
02208    dvar_vector & operator+=(const dvar_vector & v1);
02209    dvar_vector & operator-=(const dvar_vector & v1);
02210    dvar_vector & operator=(const dvar_vector & t);
02211    dvar_vector & operator=(const dvector & t);
02212    dvar_vector & operator =(double t);
02213    dvar_vector & operator=(const prevariable & t);
02214    void fill(const char *);
02215    void fill_randu(long int &n);
02216    void fill_randn(long int &n);
02217    void fill_randbi(long int &n, double);
02218 
02219    void fill_randu_ni(long int &n);
02220    void fill_randn_ni(long int &n);
02221    void fill_randbi_ni(long int &n, double);
02222 
02223    void fill_seqadd(double, double);
02224    void fill_multinomial(const int &seed, const dvector & p);
02225    void fill_multinomial(const random_number_generator& rng, const dvector& p);
02226    friend dvar_vector operator+(const dvar_vector &, const dvar_vector &);
02227    friend dvar_vector operator+(const dvar_vector &, const dvector &);
02228    friend dvar_vector operator+(const dvector &, const dvar_vector &);
02229    friend dvar_vector operator-(const dvar_vector &, const dvar_vector &);
02230 
02231    friend dvar_vector operator-(const dvector &, const dvar_vector &);
02232 
02233    friend dvar_vector operator-(const dvar_vector &, const dvector &);
02234 
02235    friend dvar_vector sigmoid(const dvar_vector & t1);
02236 
02237    friend dvariable operator*(const dvar_vector &, const dvar_vector &);
02238 
02239    friend dvar_vector elem_div(const dvar_vector &, const dvar_vector &);
02240 
02241    friend dvariable operator*(const dvector &, const dvar_vector &);
02242 
02243    friend dvariable operator*(const dvar_vector &, const dvector &);
02244 
02245    friend dvar_vector operator*(const prevariable &, const dvar_vector &);
02246 
02247    friend dvar_vector operator*(const prevariable &, const dvector &);
02248 
02249    friend dvar_vector operator*(double, const dvar_vector &);
02250 
02251    friend dvar_vector operator*(const dvar_vector &, const dmatrix &);
02252 
02253    friend dvar_vector operator*(const dmatrix &, const dvar_vector &);
02254 
02255    friend dvar_vector operator*(const dvar_vector &, const dvar_matrix &);
02256 
02257    friend dvar_vector operator*(const dvar_matrix &, const dvar_vector &);
02258 
02259    friend dvar_matrix operator*(const dvar_matrix &, const dvar_matrix &);
02260 
02261    friend dvar_matrix operator*(const dmatrix &, const dvar_matrix &);
02262 
02263    friend dvar_vector elem_prod(const dvar_vector &, const dvar_vector &);
02264 
02265    friend dvar_vector first_difference(const dvar_vector &);
02266    friend dvar_vector second_difference(const dvar_vector &);
02267 
02268    // js, see above
02269    //friend dvar_vector elem_div(const dvar_vector&, const dvar_vector&);
02270 
02271    friend dvar_vector elem_prod(const dvector &, const dvar_vector &);
02272 
02273    friend dvar_vector elem_div(const dvector &, const dvar_vector &);
02274 
02275    friend dvar_vector elem_prod(const dvar_vector &, const dvector &);
02276 
02277    friend dvar_vector elem_div(const dvar_vector &, const dvector &);
02278 
02279    friend dvariable norm(const dvar_vector &);
02280    friend dvariable norm2(const dvar_vector &);
02281    friend dvariable sumsq(const dvar_vector &);
02282 
02283    friend void copy_status(const ostream & s, const dvar_vector & v);
02284 
02285    friend dvar_vector exp(const dvar_vector &);
02286 
02287    friend dvar_vector log(const dvar_vector &);
02288 
02289    friend dvar_vector sin(const dvar_vector &);
02290 
02291    friend dvar_vector fabs(const dvar_vector &);
02292 
02293    friend dvector value(const dvar_vector & v1);
02294 
02295    friend dvar_vector sfabs(const dvar_vector &);
02296 
02297    friend void make_indvar_list(const dvar_vector &);
02298    friend class array_size;
02299 };
02300 
02301  /*
02302     class funnel_dvar_vector : public dvar_vector
02303     {
02304     public:
02305     funnel_dvar_vector(int l,int u);
02306     dvar_vector& operator=(const dvar_vector&);
02307     };
02308   */
02309 
02310 //class fvar_ptr { dvar_vector *p; };
02311 
02317 class dvar_matrix
02318 {
02319    int index_min;
02320    int index_max;
02321    dvar_vector *m;
02322    mat_shapex *shape;
02323 
02324  public:
02325    dvar_matrix & operator --(void)
02326    {
02327       index_min--;
02328       index_max--;
02329       m++;
02330       return *this;
02331    }
02332    dvar_matrix & operator ++(void)
02333    {
02334       index_min++;
02335       index_max++;
02336       m--;
02337       return *this;
02338    }
02339 
02340    int operator!(void) const
02341    {
02342       return (shape == NULL);
02343    }
02344    inline dvar_vector & elem(int i)
02345    {
02346       return (m[i]);
02347    }
02348    inline prevariable elem(int i, int j)
02349    {
02350       return (elem(i).elem(j));
02351    }
02352    inline dvar_vector & elem(int i) const
02353    {
02354       return (m[i]);
02355    }
02356    inline prevariable elem(int i, int j) const
02357    {
02358       return (elem(i).elem(j));
02359    }
02360 
02361    friend class banded_symmetric_dvar_matrix;
02362    friend class banded_lower_triangular_dvar_matrix;
02363    friend class banded_symmetric_dmatrix;
02364    friend class banded_lower_triangular_dmatrix;
02365    friend class dvar3_array;
02366    void shallow_copy(const dvar_matrix &);
02367    dvar_matrix();
02368    void allocate(int nrl, int nrh, int ncl, int nch);
02369    void allocate(int nrl, int nrh);
02370    void allocate(ad_integer nrl, ad_integer nrh);
02371    void allocate(const dmatrix & m1);
02372    void allocate(const dvar_matrix & m1);
02373    void allocate(int nrl, int nrh, const ivector & ncl, const ivector & nch);
02374    void allocate(int nrl, int nrh, int ncl, const ivector & nch);
02375    void allocate(int nrl, int nrh, const ivector & ncl, int nch);
02376    void allocate(void);
02377    void deallocate();
02378    dvar_matrix(const banded_symmetric_dvar_matrix & v);
02379    dvar_matrix(const banded_lower_triangular_dvar_matrix & v);
02380 # if defined(__NUMBERVECTOR__)
02381    dvar_matrix(const param_init_vector_vector &);
02382    dvar_matrix(const param_init_bounded_vector_vector &);
02383    dvar_matrix(const param_init_bounded_number_matrix &);
02384 # endif
02385    dvar_matrix sub(int, int);
02386 
02387    double fill_seqadd(double, double);
02388 
02389    int colmin(void) const
02390    {
02391       return ((*this) (indexmin()).indexmin());
02392    }
02393    int colmax(void) const
02394    {
02395       return ((*this) (indexmin()).indexmax());
02396    }
02397    int rowmin(void) const
02398    {
02399       return (index_min);
02400    }
02401    int rowmax(void) const
02402    {
02403       return (index_max);
02404    }
02405    int indexmin(void) const
02406    {
02407       return (index_min);
02408    }
02409    int indexmax(void) const
02410    {
02411       return (index_max);
02412    }
02413    // returns the number of rows
02414    int rowsize() const
02415    {
02416       return (rowmax() - rowmin() + 1);
02417    }
02418    // returns the number of columns
02419    int colsize() const
02420    {
02421       return (colmax() - colmin() + 1);
02422    }
02423    void colshift(int min);
02424    void rowshift(int min);
02425 
02426    friend char *fform(const char *, const dvar_matrix &);
02427 
02428    friend class dvar_vector;
02429 
02430    dvar_matrix(const ad_integer & nrl, const ad_integer & nrh,
02431      const index_type & ncl, const index_type & nch);
02432 
02433    void allocate(const ad_integer & nrl, const ad_integer & nrh,
02434      const index_type & ncl, const index_type & nch);
02435 
02436    dvar_matrix(int, int, int, int);
02437    dvar_matrix(int, int);
02438    dvar_matrix(int, int, kkludge_object kk);
02439    // makes a matrix [nrl..nrh][ncl..nch]
02440 
02441    dvar_matrix(int, int, const ivector &, const ivector &);
02442    // makes a ragged dvar_matrix [nrl..nrh][ncl..nch]
02443 
02444    dvar_matrix(int, int, int, const ivector &);
02445    // makes a ragged dvar_matrix [nrl..nrh][ncl..nch]
02446 
02447    dvar_matrix(const dvar_matrix &);
02448    // copy initializer
02449    void initialize(void);
02450 
02451    dvar_matrix(const dmatrix &);
02452 
02453    //dvar_matrix(char *);
02454 
02455    ~dvar_matrix();
02456 
02457    void save_dvar_matrix_position(void) const;
02458    void save_dvar_matrix_value(void) const;
02459 
02460    void fill(const char *);
02461    //void colfill(const int&n,...);
02462    //void rowfill(const int&n,...);
02463 
02464    void colfill_randu(const int &j, long int &n);
02465    void rowfill_randu(const int &i, long int &n);
02466    void colfill_randn(const int &j, long int &n);
02467    void rowfill_randn(const int &i, long int &n);
02468    void fill_randn(long int &n);
02469    void fill_randu(long int &n);
02470 
02471    void colfill_seqadd_ni(const int &, double, double);
02472    void colfill_randu_ni(const int &j, long int &n);
02473    void rowfill_randu_ni(const int &i, long int &n);
02474    void colfill_randn_ni(const int &j, long int &n);
02475    void rowfill_randn_ni(const int &i, long int &n);
02476    void fill_randn_ni(long int &n);
02477    void fill_randu_ni(long int &n);
02478 
02479    void colfill_seqadd(const int &, double, double);
02480    void rowfill_seqadd(const int &, double, double);
02481    void colfill(int j, const dvar_vector & v);
02482    void rowfill(int j, const dvar_vector & v);
02483 
02484    void write_on(const ostream &) const;
02485    void write_on(const uostream &) const;
02486    void read_from(const istream &);
02487    void read_from(const uistream &);
02488 
02489    dvar_vector& operator ()(int i);
02490    dvar_vector& operator[](int);
02491    const dvar_vector& operator()(int i) const;
02492    const dvar_vector& operator[](int) const;
02493 
02494 #ifdef OPT_LIB
02495 #ifdef __NDPX__
02496    prevariable operator () (register int i, register int j)
02497    {
02498       return (prevariable((m[i]).va + j));
02499    }
02500 #else
02501    inline prevariable operator () (register int i, register int j)
02502    {
02503       return ((m[i]).va + j);
02504    }
02505 #endif
02506 #else
02507    prevariable operator () (int i, int j);
02508 #endif
02509 
02510    inline double &elem_value(register int i, register int j)
02511    {
02512       return *(double *) ((m[i]).va + j);
02513    }
02514 
02515    inline const double &elem_value(register int i, register int j) const
02516    {
02517       return *(double *) ((m[i]).va + j);
02518    }
02519 #ifdef OPT_LIB
02520 #ifdef __NDPX__
02521    prevariable operator() (register int i, register int j) const
02522    {
02523       return (prevariable((m[i]).va + j));
02524    }
02525 #else
02526    inline prevariable operator() (register int i, register int j) const
02527    {
02528       return ((m[i]).va + j);
02529    }
02530 #endif
02531 #else
02532    const prevariable operator() (int i, int j) const;
02533 #endif
02534 
02535    dvar_matrix & operator+=(const dvar_matrix & x);
02536    dvar_matrix & operator-=(const dvar_matrix & x);
02537    dvar_matrix & operator+=(const dmatrix & x);
02538    dvar_matrix & operator-=(const dmatrix & x);
02539 
02540 
02541    dvar_matrix & operator=(const dvar_matrix &);
02542 
02543    dvar_matrix & operator=(const dmatrix &);
02544    dvar_matrix & operator =(double t);
02545    dvar_matrix & operator=(const prevariable & t);
02546 
02547    dvar_matrix & operator*=(const prevariable & t);
02548    dvar_matrix & operator *=(double t);
02549    dvar_matrix & operator/=(const prevariable & t);
02550    dvar_matrix & operator /=(double t);
02551 
02552    friend dvar_vector operator*(const dvar_vector &, const dvar_matrix &);
02553 
02554    friend dvar_vector operator*(const dvar_matrix &, const dvar_vector &);
02555 
02556    friend dvar_vector operator*(const dvector &, const dvar_matrix &);
02557 
02558    friend dvar_vector operator*(const dvar_matrix &, const dvector &);
02559 
02560    friend dvar_matrix operator*(const dvar_matrix &, const dvar_matrix &);
02561 
02562    friend dvar_matrix operator*(const dvar_matrix &, const dmatrix &);
02563 
02564    friend dvar_matrix operator*(const dmatrix &, const dvar_matrix &);
02565 
02566    friend dvar_matrix operator+(const dvar_matrix &, const dvar_matrix &);
02567    friend dvar_matrix operator+(const dvar_matrix &, const dmatrix &);
02568    friend dvar_matrix operator+(const dmatrix &, const dvar_matrix &);
02569 
02570    friend dvar_matrix operator+(double, const dvar_matrix &);
02571    friend dvar_matrix operator+(const dvar_matrix &, double);
02572    friend dvar_matrix operator-(double, const dvar_matrix &);
02573    friend dvar_matrix operator-(const dvar_matrix &, double);
02574 
02575    friend dvar_matrix operator+(const dvariable &, const dvar_matrix &);
02576    friend dvar_matrix operator+(const dvar_matrix &, const dvariable &);
02577    friend dvar_matrix operator-(const dvariable &, const dvar_matrix &);
02578    friend dvar_matrix operator-(const dvar_matrix &, const dvariable &);
02579 
02580    friend dvar_matrix operator-(const dvar_matrix &, const dvar_matrix &);
02581    friend dvar_matrix operator-(const dvar_matrix &, const dmatrix &);
02582    friend dvar_matrix operator-(const dmatrix &, const dvar_matrix &);
02583 
02584    friend dvar_matrix inv(const dvar_matrix &);
02585 
02586    friend dvariable det(const dvar_matrix &);
02587    friend dvariable ln_det(const dvar_matrix &, int& sgn);
02588 
02589    //friend dvar_matrix testsub(dvar_matrix);
02590 
02591    friend dvar_matrix trans(const dvar_matrix &);
02592 
02593    friend dvariable norm(const dvar_matrix &);
02594    friend dvariable norm2(const dvar_matrix &);
02595    friend dvariable sumsq(const dvar_matrix &);
02596 
02597    friend void copy_status(const ostream & s, const dvar_matrix & m1);
02598 };
02599 
02600 #ifdef OPT_LIB
02601 inline dvar_vector& dvar_matrix::operator[](int i)
02602 {
02603   if (!m)
02604     throw std::bad_alloc();
02605   else
02606     return m[i];
02607 }
02608 inline dvar_vector& dvar_matrix::operator()(int i)
02609 {
02610   if (!m)
02611     throw std::bad_alloc();
02612   else
02613     return m[i];
02614 }
02615 inline const dvar_vector& dvar_matrix::operator[](int i) const
02616 {
02617   return m[i];
02618 }
02619 inline const dvar_vector& dvar_matrix::operator()(int i) const
02620 {
02621   return m[i];
02622 }
02623 #endif
02624 
02625 dvariable ln_det(const dvar_matrix &);
02626 dvar_matrix operator *(const dvar_matrix & t1, double x);
02627 dmatrix value(const dvar_matrix & m);
02628 d3_array value(const dvar3_array & a);
02629 dvar_vector sort(const dvar_vector &, int NSTACK = 60);
02630 dvector sort(const dvector &, int NSTACK = 60);
02631 ivector sort(const ivector &, int NSTACK = 60);
02632 dvector sort(const dvector &, const ivector & index, int NSTACK = 60);
02633 ivector sort(const ivector &, const ivector & index, int NSTACK = 60);
02634 dmatrix sort(const dmatrix &, int column, int NSTACK = 60);
02635 imatrix sort(const imatrix &, int column, int NSTACK = 60);
02636 
02637 
02638 #include "factors.h"
02639 int count_factor(const dvector & v, const double &eps);
02640 ivector as_factor(const dvector & v, const double eps = 1.0e-6);
02641 int count_factor(const ivector & v);
02642 
02643  //void gradcalc( int , double *);
02644 void gradcalc(int nvar, const dvector & g);
02645 double gradcalc(int nvar, const dvector& g, dvariable& f);
02646 void slave_gradcalc(void);
02647 
02652 class dmatrix
02653 {
02654  protected:
02655    int index_min;
02656    int index_max;
02657    dvector *m;
02658    mat_shapex *shape;
02659    friend char *fform(const char *, const dmatrix &);
02660    friend class dvar_matrix;
02661  public:
02662    dmatrix & operator --(void)
02663    {
02664       index_min--;
02665       index_max--;
02666       m++;
02667       return *this;
02668    }
02669    dmatrix & operator ++(void)
02670    {
02671       index_min++;
02672       index_max++;
02673       m--;
02674       return *this;
02675    }
02676    void shallow_copy(const dmatrix &);
02677    int operator!(void) const
02678    {
02679       return (shape == NULL);
02680    }
02681 
02682    dmatrix sub(int, int);
02683    dmatrix(void);
02684    dmatrix(int, int, kkludge_object);
02685    dmatrix(int, int);
02686    void allocate(void);
02687    void allocate(const dmatrix & dm);
02688    void allocate(const dvar_matrix &);
02689    void allocate(int nrl, int nrh, int ncl, int nch);
02690    void allocate(int nrl, int nrh);
02691    void allocate(ad_integer nrl, ad_integer nrh);
02692    void allocate(int nrl, int nrh, int ncl, const ivector & nch);
02693    //void allocate(int nrl,int nrh,
02694    // const index_type& ncl,const index_type& nch);
02695    void allocate(int nrl, int nrh, const ivector & ncl, int nch);
02696    void deallocate();
02697    void allocate(const ad_integer & nrl, const ad_integer & nrh,
02698      const index_type & ncl, const index_type & nch);
02699    void allocate(int nrl, int nrh, const ivector & ncl,
02700      const ivector & nch);
02701    friend class banded_symmetric_dmatrix;
02702    friend class banded_lower_triangular_dmatrix;
02703 
02704    dmatrix(int, int, int, int);
02705    // makes a matrix [nrl..nrh][ncl..nch]
02706 
02707    dmatrix(const ad_integer & nrl, const ad_integer & nrh,
02708      const index_type & ncl, const index_type & nch);
02709 
02710    dmatrix(int, int, const ivector & coll, const ivector & colh);
02711    // makes a ragged dmatrix[nrl..nrh][ncl..nch]
02712 
02713    dmatrix(int, int, int coll, const ivector & colh);
02714    // makes a ragged dmatrix[nrl..nrh][ncl..nch]
02715 
02716    dmatrix(const dvar_matrix_position &);
02717 
02718    dmatrix(const dmatrix_position &);
02719 
02720    dmatrix(const dmatrix &);
02721    dmatrix(const banded_symmetric_dmatrix &);
02722    dmatrix(const banded_lower_triangular_dmatrix &);
02723    dmatrix(char *);
02724    void fill(const char *);
02725    double fill_seqadd(double, double);
02726    void initialize(void);
02727    // copy initializer
02728 
02729    ~dmatrix();
02730    void save_dmatrix_derivatives(const dvar_matrix_position & pos) const;
02731    void save_dmatrix_derivatives_na(const dvar_matrix_position & pos)
02732       const;
02733    void save_dmatrix_value(void) const;
02734    void save_dmatrix_position(void) const;
02735    //void save_dmatrix_derivatives(void);
02736 
02737    int indexmin(void) const
02738    {
02739       return index_min;
02740    }
02741    int indexmax(void) const
02742    {
02743       return index_max;
02744    }
02745    int rowmin(void) const
02746    {
02747       return index_min;
02748    }
02749    int rowmax(void) const
02750    {
02751       return index_max;
02752    }
02753    int colmin(void) const
02754    {
02755       return ((*this) (indexmin()).indexmin());
02756    }
02757    int colmax(void) const
02758    {
02759       return ((*this) (indexmin()).indexmax());
02760    }
02761    // returns the number of rows
02762    int rowsize() const
02763    {
02764       return (rowmax() - rowmin() + 1);
02765    }
02766    // returns the number of columns
02767    int colsize() const
02768    {
02769       return (colmax() - colmin() + 1);
02770    }
02771    void rowshift(int min);
02772    void colshift(int min);
02773 
02774    void write_on(const ostream &) const;
02775    void write_on(const uostream &) const;
02776    void read_from(const istream &);
02777    void read_from(const uistream &);
02778 
02779    //void colfill(const int&n,...);
02780    //void rowfill(const int&n,...);
02781 
02782    void colfill_randu(const int &j, long int &n);
02783    void rowfill_randu(const int &i, long int &n);
02784    void colfill_randn(const int &j, long int &n);
02785    void fill_randn(long int &n);
02786    void fill_randu(long int &n);
02787    void rowfill_randn(const int &i, long int &n);
02788 
02789    void colfill_randu(const int &j, const random_number_generator & rng);
02790    void rowfill_randu(const int &i, const random_number_generator & rng);
02791    void fill_randn(const random_number_generator & rng);
02792    void fill_randcau(const random_number_generator & rng);
02793    void fill_randu(const random_number_generator & rng);
02794    void colfill_randn(const int &j, const random_number_generator & rng);
02795    void rowfill_randn(const int &i, const random_number_generator & rng);
02796 
02797    void colfill_randu_ni(const int &j, long int &n);
02798    void rowfill_randu_ni(const int &i, long int &n);
02799    void colfill_randn_ni(const int &j, long int &n);
02800    void fill_randn_ni(long int &n);
02801    void fill_randu_ni(long int &n);
02802    void rowfill_randn_ni(const int &i, long int &n);
02803 
02804    void colfill_seqadd(const int &, const int &, const int &);
02805    void colfill_seqadd(const int &, double, double);
02806    void rowfill_seqadd(const int &, double, double);
02807    void colfill(int j, const dvector & v);
02808    void rowfill(int j, const dvector & v);
02809 
02810    dvector& operator()(int i);
02811    dvector& operator[](int);
02812    const dvector& operator()(int i) const;
02813    const dvector& operator[](int) const;
02814 
02815 #if defined(OPT_LIB) && !defined(__INTEL_COMPILER)
02816    inline double &operator() (register int i, register int j)
02817    {
02818       return (*(m[i].v + j));
02819    }
02820    inline const double &operator() (register int i, register int j) const
02821    {
02822       return (*(m[i].v + j));
02823    }
02824 #else
02825    double &operator() (int i, int j);
02826    const double &operator() (int i, int j) const;
02827 #endif
02828 
02829    inline dvector & elem(int i)
02830    {
02831       return (*(m + i));
02832    }
02833    inline double &elem(int i, int j)
02834    {
02835       return (*((*(m + i)).v + j));
02836    }
02837    inline const dvector & elem(int i) const
02838    {
02839       return (*(m + i));
02840    }
02841    inline const double &elem(int i, int j) const
02842    {
02843       return (*((*(m + i)).v + j));
02844    }
02845    friend class d3_array;
02846    friend dvector operator*(const dvector &, const dmatrix &);
02847 
02848    friend dvector operator*(const dmatrix &, const dvector &);
02849 
02850    friend dvar_vector operator*(const dvar_vector &, const dmatrix &);
02851 
02852    friend dvar_vector operator*(const dmatrix &, const dvar_vector &);
02853 
02854    friend dmatrix operator*(const dmatrix &, const dmatrix &);
02855 
02856    friend dvar_matrix operator*(const dvar_matrix &, const dmatrix &);
02857 
02858    friend dvar_matrix operator*(const dmatrix &, const dvar_matrix &);
02859 
02860    friend dvar_matrix::dvar_matrix(const dmatrix &);
02861 
02862    friend dmatrix operator-(const dmatrix &, const dmatrix &);
02863    friend dmatrix operator+(const dmatrix &, const dmatrix &);
02864 
02865    friend dvar_matrix operator+(const dvar_matrix &, const dmatrix &);
02866 
02867    friend dvar_matrix operator+(const dmatrix &, const dvar_matrix &);
02868 
02869    friend dmatrix trans(const dmatrix & m1);
02870 
02871    friend dmatrix inv(const dmatrix &);
02872    friend dmatrix inv(const dmatrix & m1, const double &_ln_det,
02873      const int &_sgn);
02874 
02875    friend double det(const dmatrix &);
02876    friend double ln_det(const dmatrix & m1, int& sgn);
02877 
02878    friend double norm(const dmatrix &);
02879    friend double norm2(const dmatrix &);
02880    friend double sumsq(const dmatrix &);
02881 
02882    dmatrix & operator+=(const dmatrix & t);
02883    dmatrix & operator-=(const dmatrix & t);
02884 
02885    dmatrix & operator =(const dmatrix & t);
02886    dmatrix & operator =(double t);
02887 
02888    dmatrix operator() (const ivector & t);
02889 
02890    friend dvar_matrix & dvar_matrix::operator=(const dmatrix &);
02891 
02892    dmatrix(const tdmatrix & t);
02893 
02894    dmatrix & operator /=(double d);
02895    dmatrix & operator *=(double d);
02896 
02897   bool is_valid_row(const int i) const;
02898 };
02899 
02900 #if defined(OPT_LIB)
02901 inline dvector& dmatrix::operator()(int i)
02902 {
02903   return m[i];
02904 }
02905 inline dvector& dmatrix::operator[](int i)
02906 {
02907   return m[i];
02908 }
02909 inline const dvector& dmatrix::operator()(int i) const
02910 {
02911   return m[i];
02912 }
02913 inline const dvector& dmatrix::operator[](int i) const
02914 {
02915   return m[i];
02916 }
02917 #endif
02918 
02919 imatrix operator*(const imatrix &, const imatrix &);
02920 
02921 dmatrix trans(const dmatrix & m1);
02922 
02923 imatrix trans(const imatrix & m1);
02924 
02925 dvariable dfatan1(dvariable, double, double, double *);
02926 
02927 double dftinv(double, double, double);
02928 
02929 dvariable boundp(double, double, double, double *);
02930 
02931 dvariable dfboundp(double, double, double, double *);
02932 dvariable dfboundp(const prevariable &, double, double);
02933 
02934 double mean(const dvector &);
02935 double mean(const dmatrix &);
02936 double mean(const d3_array &);
02937 
02938 double std_dev(const dvector &);
02939 double var(const dvector &);
02940 
02941 dvariable mean(const dvar_vector &);
02942 dvariable mean(const dvar_matrix &);
02943 dvariable mean(const dvar3_array &);
02944 
02945 dvariable std_dev(const dvar_vector &);
02946 dvariable var(const dvar_vector &);
02947 
02948 dvariable sum(const dvar_vector &);
02949 double sum(const dvector &);
02950 int sum(const ivector &);
02951 
02952 dvar_vector rowsum(const dvar_matrix &);
02953 dvar_vector colsum(const dvar_matrix &);
02954 
02955 dvector colsum(const dmatrix &);
02956 dvector rowsum(const dmatrix &);
02957 
02958 ivector colsum(const imatrix &);
02959 ivector rowsum(const imatrix &);
02960 
02961 int colsum(const imatrix &, int column);
02962 double colsum(const dmatrix &, int column);
02963 dvariable colsum(const dvar_matrix &, int column);
02964 
02965 double sfabs(double t1); //"smoothed absolute value function
02966 
02967 dvector sfabs(const dvector & t1); //"smoothed absolute value function
02968 
02969 #include <imatrix.h>
02970 
02971 dvariable regression(const dvector & obs, const dvar_vector & pred);
02972 double regression(const dvector & obs, const dvector & pred);
02973 
02974 dvariable robust_regression_fixed(const dvector& obs, const dvar_vector& pred,
02975   double a = 0.7);
02976 dvariable robust_regression(const dvector & obs, const dvar_vector & pred,
02977   double a = 0.7);
02978 
02979 dvariable robust_regression(const dvector & obs, const dvar_vector & pred,
02980   const dvariable & cutoff);
02981 
02982 dmatrix column_vector(const dvector &);
02983 dmatrix row_vector(const dvector &);
02984 
02985 dvar_matrix column_vector(const dvar_vector &);
02986 dvar_matrix row_vector(const dvar_vector &);
02987 
02988 dmatrix identity_matrix(int min, int max);
02989 
02990 istream & operator>>(const istream & s, const ptr_vector & v);
02991 ostream & operator<<(const ostream & s, const ptr_vector & v);
02992 
02996 class fmm_control
02997 {
02998  public:
02999    int noprintx;
03000    long maxfn;
03001    long iprint;
03002    double crit;
03003    double fringe;
03004    long imax;
03005    double dfn;
03006    long ifn;
03007    long iexit;
03008    long ialph;
03009    long ihflag;
03010    long ihang;
03011    int scroll_flag;
03012    int maxfn_flag;
03013    int quit_flag;
03014    double min_improve;
03015    int ireturn;
03016    int dcheck_flag;
03017    int use_control_c;
03018 
03019    void set_defaults();
03020    fmm_control();
03021    fmm_control(const fmm_control &);
03022    fmm_control(const lvector & ipar);
03023    void writeon(const ostream & s) const;
03024 };
03025 
03030 class sdmatrix:public dmatrix
03031 {
03032  public:
03033    void allocate(int);
03034    void allocate();
03035    sdmatrix(int);
03036    sdmatrix();
03037    ~sdmatrix();
03038    void deallocate();
03039 };
03040 
03041 class dfsdmat;
03042 
03043 uistream & operator>>(const uistream &, const dfsdmat &);
03044 uostream & operator<<(const uostream &, const dfsdmat &);
03045 
03050 class dfsdmat
03051 {
03052    int tmp_file;
03053    int disk_save_flag;
03054    double *ptr;
03055    double **m;
03056    double *minp;
03057    double *maxp;
03058    int shared_memory;
03059    int n;
03060  public:
03061    int disk_save(void)
03062    {
03063       return disk_save_flag;
03064    }
03065    void save(void);
03066    void restore(void);
03067    double *getminp(void)
03068    {
03069       return minp;
03070    }
03071    int operator!(void) const
03072    {
03073       return (ptr == NULL);
03074    }
03075    int size(void)
03076    {
03077       return n;
03078    }
03079    dfsdmat(int n);
03080    dfsdmat();
03081    dfsdmat(int n, const gradient_structure & gs);
03082    void allocate(int n);
03083    void allocate(int n, const gradient_structure & gs);
03084    void allocate(void);
03085    ~dfsdmat();
03086    void deallocate(void);
03087    friend uistream & operator>>(const uistream &, const dfsdmat &);
03088    friend uostream & operator<<(const uostream &, const dfsdmat &);
03089 
03090   double &elem(int i, int j);
03091   double &operator () (int i, int j);
03092 };
03093 
03094 #if defined(OPT_LIB) && !defined(__INTEL_COMPILER)
03095 inline double& dfsdmat::elem(int i, int j)
03096 {
03097   return *(m[i] + j);
03098 }
03099 inline double& dfsdmat::operator()(int i, int j)
03100 {
03101   return *(m[i] + j);
03102 }
03103 #endif
03104 
03108 class fmm:public fmm_control
03109 {
03110  private:
03111    dfsdmat h;
03112    dvector w;
03113    dvector funval;
03114  public:
03115    double dmin, fbest, df;
03116 
03117    long int llog, n1, ic, iconv, i1, xxlink;
03118    double z, zz, gys, gs, sig, gso, alpha, tot, fy, dgs;
03119    long int itn, icc, nn;
03120    int i, j, is, iu, iv, np, ib;
03121    double gmax; 
03122    double fsave;
03123    dvector xx;
03124    dvector gbest;
03125    dvector xsave;
03126    dvector gsave;
03127 
03128    int n;
03129    int disk_save;
03130 
03131  public:
03132    fmm(int nvar, int disk_save = 0);
03133    fmm(int nvar, const lvector & ipar, int disk_save = 0);
03134    double minimize(const independent_variables & x,
03135      double (*pf) (const dvar_vector &));
03136 
03138    double minimize(const independent_variables & x, const dvector & c,
03139      double (*pf) (const dvar_vector &, const dvector &));
03140 
03141  //void fmin(const double& f, const independent_variables &x, const dvector& g);
03142    void fmin(const double &f, const dvector & x, const dvector & g);
03143 
03144    dmatrix & hessian(); 
03145 };
03146 
03147 class function_minimizer;
03148 
03153 class fmmt1:public fmm_control
03154 {
03155  private:
03156    dvector w;
03157    dvector funval;
03158    int xm;
03159    dmatrix xstep;
03160    dvector xrho;
03161    dvector rrr;
03162    dmatrix xy;
03163    dvector xold;
03164    dvector gold;
03165  public:
03166    double dmin, fbest, df;
03167 
03168    long int llog, n1, ic, iconv, i1, link;
03169    double z, zz, gys, gs, sig, gso, alpha, tot, fy, dgs;
03170    long int icc, np, nn, iv, ib;
03171    int i, j, iu, is, itn;
03172    double gmax;
03173    double fsave;
03174    dvector xx;
03175    dvector gbest;
03176    dvector xsave;
03177    dvector gsave;
03178 
03179    int n;
03180 
03181  public:
03182    fmmt1(int nvar, int _xm = 7);
03183    fmmt1(int nvar, const lvector & ipar);
03184    double minimize(const independent_variables & x,
03185      double (*pf) (const dvar_vector &));
03186 
03187    double minimize(const independent_variables & x, const dvector & c,
03188      double (*pf) (const dvar_vector &, const dvector &));
03189 
03190    void fmin2(const double &f, const independent_variables & x,
03191      const dvector & g, function_minimizer *);
03192 
03193    void fmin(const double &f, const dvector & x, const dvector & g);
03194 
03195 //  dmatrix& hessian();
03196 };
03197 
03198 void derch(const double &f, const independent_variables & x,
03199   const dvector & g, int n, const int &ireturn);
03200 
03201 void fmin(double f, const independent_variables & x, const dvector & g,
03202   const int &n, const dvector & w, const dvector & h, const fmm_control & fmc);
03203 
03204 void fmmdisp(const dvector & x, const dvector & g, const int &nvar,
03205   int scroll_flag, int noprintx = 0);
03206 
03207 void fmmdisp(const double *x, const double *g, const int &nvar,
03208   int scroll_flag, int noprintx = 0);
03209 
03210 ostream & operator<<(const ostream & s, const fmm_control & fmc);
03211 
03216 class uostream:public ofstream
03217 {
03218  public:
03219 #if defined(__TURBOC__) && (__BORLANDC__  <= 0x0520)
03220   uostream(const char*, int = ios::out | ios::binary, int = filebuf::openprot);
03221   void open(const char*, int = ios::out | ios::binary, int = filebuf::openprot);
03222 #endif
03223 #if (__BORLANDC__  >= 0x0540)
03224    uostream(const char *, int = ios::out | ios::binary, int protection = 666);
03225    void open(const char *, int = ios::out | ios::binary, int protection = 666);
03226 #endif
03227 #if defined (_MSC_VER) || defined (__WAT32__)
03228   #if (_MSC_VER < 1300)
03229   uostream(const char*, int = ios::out | ios::binary, int = filebuf::openprot);
03230   void open(const char*, int = ios::out | ios::binary, int = filebuf::openprot);
03231   #else
03232   uostream(const char *, int = ios::out | ios::binary, int prot = 0664);
03233   void open(const char *, int = ios::out | ios::binary, int prot = 0664);
03234   #endif
03235 #endif
03236 
03237 #ifdef __ZTC__
03238    uostream(const char *, int = ios::out, int = filebuf::openprot);
03239    void open(const char *, int = ios::out, int = filebuf::openprot);
03240 #endif
03241 
03242 #ifdef __NDPX__
03243    uostream(const char *, int = ios::out, int = filebuf::openprot);
03244    void open(const char *, int = ios::out, int = filebuf::openprot);
03245 #endif
03246 
03247 #ifdef __SUN__
03248    //uostream(const char*, int = ios::out, int = openprot);
03249    //void open(const char*, int = ios::out, int = openprot);
03250 #endif
03251 
03252 #if !defined(_MSC_VER)
03253   #if defined(__ADSGI__)
03254   uostream(const char *name, int mode = ios::out, int prot = 0664);
03255   void open(const char *name, int mode = ios::out, int prot = 0664);
03256   #else
03257   uostream(const char *name, int mode = ios::out | ios::binary,
03258     int prot = 0664);
03259   void open(const char *name, int mode = ios::out | ios::binary,
03260     int prot = 0664);
03261   #endif
03262 #endif
03263 
03264    // insert character
03265 #ifndef __SUN__
03266    uostream & operator<<(signed char);
03267 #endif
03268    uostream & operator<<(unsigned char);
03269 
03270    // insert numeric value
03271    uostream & operator<<(short);
03272    uostream & operator<<(unsigned short);
03273    uostream & operator<<(int);
03274    uostream & operator<<(unsigned int);
03275    uostream & operator<<(long);
03276    uostream & operator<<(unsigned long);
03277    uostream & operator<<(float);
03278    uostream & operator<<(double);
03279    uostream & operator<<(const char *)
03280    {
03281       return *this;
03282    };
03283 #ifdef __TURBOC__
03284    uostream & operator<<(long double);
03285 #endif
03286 
03287    // insert pointer
03288    uostream & operator<<(void *);
03289 };
03290 
03291 
03292  // inline void uostream::open(const char* name, int m, int prot)
03293  // {
03294  // #if defined (__TURBOC__) &&   (__BORLANDC__  <= 0x0520)
03295  //   fstreambase::open(name, m, prot);
03296  // #endif
03297  // #if (__BORLANDC__  >= 0x0540 && __BORLANDC__  <= 0x0550)
03298  //   ofstream::open(name, m, prot);
03299  // #else
03300  // #  if defined(linux)
03301  // #    if (__GNUC__  >= 3)
03302  //        ofstream::open(name, std::_Ios_Openmode(m));
03303  // #    else
03304  //        ofstream::open(name, m);
03305  // #    endif
03306  // #  else
03307  //      ofstream::open(name, m);
03308  // #  endif
03309  // #endif
03310  //
03311  // #ifdef _MSC_VER
03312  // #  if (_MSC_VER >= 1400)
03313  //   ofstream::open(name, m);
03314  // #  else
03315  //   //fstreambase::open(name, m, prot);
03316  //   ofstream::open(name, m, prot);
03317  // #  endif
03318  // #endif
03319  // #ifdef __ZTC__
03320  //   fstream_common::open(name, m, prot);
03321  // #endif
03322  // #ifdef __NDPX__
03323  //   ofstream::open(name, m, prot);
03324  // #endif
03325  // #ifdef __SUN__
03326  //   ofstream::open(name, m, prot);
03327  // #endif
03328  // }
03329 
03334 class uistream:public ifstream
03335 {
03336  public:
03337 #if defined (__TURBOC__) &&   (__BORLANDC__  <= 0x0520)
03338    uistream(const char *, int = ios::in | ios::binary, int = filebuf::openprot);
03339   void open(const char *, int = ios::in | ios::binary, int = filebuf::openprot);
03340 #endif
03341 #if (__BORLANDC__  >= 0x0540)
03342    uistream(const char *, int = ios::in | ios::binary, int protection = 666);
03343    void open(const char *, int = ios::in | ios::binary, int protection = 666);
03344 #endif
03345 #if defined (_MSC_VER) || defined (__WAT32__)
03346   #if (_MSC_VER < 1300)
03347   uistream(const char *, int = ios::in | ios::binary, int = filebuf::openprot);
03348   void open(const char *, int = ios::in | ios::binary, int = filebuf::openprot);
03349   #else
03350   uistream(const char *, int = ios::in | ios::binary, int prot = 0664);
03351   void open(const char *, int = ios::in | ios::binary, int prot = 0664);
03352   #endif
03353 #endif
03354 #ifdef __ZTC__
03355    uistream(const char *, int = ios::in, int = filebuf::openprot);
03356    void open(const char *, int = ios::in, int = filebuf::openprot);
03357 #endif
03358 
03359 #ifdef __NDPX__
03360    uistream(const char *, int = ios::in, int = filebuf::openprot);
03361    void open(const char *, int = ios::in, int = filebuf::openprot);
03362 #endif
03363 
03364 #ifdef __SUN__
03365    // uistream(const char* name, int mode = ios::in, int prot=0664);
03366    // void open(const char* name, int mode = ios::in, int prot=0664);
03367 #endif
03368 
03369 #if !defined(_MSC_VER)
03370   #if defined(__ADSGI__)
03371   uistream(const char *name, int mode = ios::in, int prot = 0664);
03372   void open(const char *name, int mode = ios::in, int prot = 0664);
03373   #else
03374   uistream(const char *name, int mode = ios::in | ios::binary,
03375     int prot = 0664);
03376   void open(const char *name, int mode = ios::in | ios::binary,
03377     int prot = 0664);
03378   #endif
03379 #endif
03380 
03381    // extract characters into an array
03382 #ifndef __SUN__
03383    uistream & get(signed char *, int, char = '\n');
03384 #endif
03385    uistream & get(unsigned char *, int, char = '\n');
03386 
03387    // extract a single character
03388    uistream & get(unsigned char &);
03389 #ifndef __SUN__
03390    uistream & get(signed char &);
03391 #endif
03392    int get();
03393 
03394    // extract and discard chars but stop at delim
03395    uistream & ignore(int = 1, int = EOF);
03396 
03397    uistream& operator>>(int&);
03398 
03399 #ifndef __SUN__
03400    uistream & operator>>(const signed char *);
03401 #endif
03402    uistream & operator>>(const unsigned char *);
03403    uistream & operator>>(const unsigned char &);
03404 #ifndef __SUN__
03405    uistream & operator>>(const signed char &);
03406 #endif
03407    uistream & operator>>(const short &);
03408    uistream & operator>>(const int &);
03409    uistream & operator>>(const long &);
03410    uistream & operator>>(const unsigned short &);
03411    uistream & operator>>(const unsigned int &);
03412    uistream & operator>>(const unsigned long &);
03413    uistream & operator>>(const float &);
03414    uistream & operator>>(const double &);
03415    uistream & operator>>(const char &);
03416 #if defined(__TURBOC__) || defined (_MSC_VER)
03417    uistream & operator>>(const long double &);
03418 #endif
03419 };
03420 
03421   // inline void   uistream::open(const char* name, int m, int prot)
03422   // {
03423   // #if defined(__TURBOC__) && (__BORLANDC__  <= 0x0520)
03424   //   fstreambase::open(name, m, prot);
03425   // #endif
03426   // #ifdef __ZTC__
03427   //   fstream_common::open(name, m, prot);
03428   // #endif
03429   // #ifdef __NDPX__
03430   //   ifstream::open(name, m, prot);
03431   // #endif
03432   // #ifdef __SUN__
03433   //   ifstream::open(name, m, prot);
03434   // #endif
03435   // }
03436 
03437 class fmmc;
03438 
03439 void derch(const double &f, const dvector & x, const dvector & gg, int n,
03440   const int &ireturn);
03441 
03446 class fmmc
03447 {
03448  public:
03449    int maxfn;
03450    double crit;
03451    double min_improve;
03452    int iprint;
03453    int scroll_flag;
03454    int j;
03455    int J;
03456    long int ifn;
03457    long int iter;
03458    int imax;
03459    long ihang;
03460    int quit_flag;
03461    dvector *funval;
03462    dvector *left_bracket_gradient;
03463    dvector *right_bracket_gradient;
03464    dvector *g;
03465    dvector *h;
03466    dvector *xi;
03467    dvector *d;
03468    dvector *extx;
03469    dvector *g2;
03470    dvector *grad;
03471    dvector *extg;
03472    dvector *theta;
03473    dvector *xbest;
03474    dvector *gbest;
03475    int lin_flag;
03476    int ext_flag;
03477    int int_flag;
03478    int ifnex;
03479    int ireturn;
03480    int frp_flag;
03481    double gg;
03482    double gam;
03483    double fp;
03484    double dgg;
03485    double rho_min;
03486    double converge_flag;
03487    double gamma;
03488    double Psi_0;
03489    double extf;
03490    double crit1;
03491    double rho_1;
03492    double Psi_1;
03493    double dir_deriv;
03494    double rho_i;
03495    double left_bracket;
03496    double left_bracket_value;
03497    double right_bracket;
03498    double right_bracket_value;
03499    double rho_0;
03500    double fbest;
03501    fmmc(const int &n);
03502    ~fmmc();
03503    void fmin(const double &f, const dvector & p, const dvector & gg);
03504    double dfn;
03505    int maxfn_flag;
03506    long iexit;
03507    long ihflag;
03508 };
03509 
03510 class dd3_array;
03511 
03516 class three_array_shape
03517 {
03518    //unsigned int nslices;
03519    unsigned int ncopies;
03520    //unsigned int nrows;
03521    //unsigned int ncols;
03522    int slice_min;
03523    int slice_max;
03524    // int row_min;
03525    // int row_max;
03526    //int col_min;
03527    //int col_max;
03528    three_array_shape(int sl, int sh);
03529    //mat_shape(){};
03530 
03531    friend class i3_array;
03532    friend class d3_array;
03533    friend class dd3_array;
03534    friend class qd3_array;
03535    friend class dvar3_array;
03536 };
03537 
03538 //class dmatrix_ptr { dmatrix *p; };
03539 //class dvar_matrix_ptr { dvar_matrix *p; };
03540 
03545 class d3_array
03546 {
03547    dmatrix *t;
03548    three_array_shape *shape;
03549    friend class d4_array;
03550  public:
03551    int operator!(void) const
03552    {
03553       return (shape == NULL);
03554    }
03555    // conclass cgors
03556    d3_array(void);
03557    void save_d3_array_value(void) const;
03558    void shallow_copy(const d3_array &);
03559    d3_array sub(int, int);
03560    d3_array(int sl, int sh, int nrl, int nrh, int ncl, int nch);
03561    d3_array(int sl, int sh, int nrl, int nrh);
03562    d3_array(int sl, int sh, const index_type & nrl, const index_type & nrh);
03563    d3_array(int sl, int sh);
03564    d3_array(const d3_array_position &);
03565 
03566    void save_d3_array_position(void) const;
03567 
03568    d3_array(int sl, int sh, int nrl, int nrh, const ivector & ncl, int nch);
03569 
03570    d3_array(const ad_integer & sl, const ad_integer & sh,
03571      const index_type & nrl, const index_type & nrh,
03572      const index_type & ncl, const index_type & nch);
03573 
03574    void allocate(const ad_integer & sl, const ad_integer & sh,
03575      const index_type & nrl, const index_type & nrh,
03576      const index_type & ncl, const index_type & nch);
03577 
03578    d3_array(int sl, int sh, const ivector & nrl, const ivector & nrh,
03579      const imatrix & ncl, const imatrix & nch);
03580    d3_array(int sl, int sh, const ivector & nrl, const ivector & nrh,
03581      int ncl, const imatrix & nch);
03582    d3_array(int sl, int sh, int nrl, const ivector & nrh,
03583      int ncl, const imatrix & nch);
03584    d3_array(int sl, int sh, const ivector & nrl, const ivector & nrh,
03585      const ivector & ncl, const ivector & nch);
03586    d3_array(int sl, int sh, int nrl, int nrh, const ivector & ncl,
03587      const ivector & nch);
03588    d3_array(int sl, int sh, int nrl, const ivector & nrh,
03589      int ncl, const ivector & nch);
03590    d3_array(int sl, int sh, int nrl, const ivector & nrh,
03591      int ncl, int nch);
03592    d3_array(const d3_array & m2);
03593    ~d3_array();
03594 
03595    void allocate(const dvar3_array &);
03596    void allocate(const d3_array & d3v);
03597    void allocate(int sl, int sh, int nrl, int nrh, int ncl, int nch);
03598    void allocate(int sl, int sh, int nrl, int nrh);
03599    void allocate(int sl, int sh, const index_type & nrl,
03600      const index_type & nrh);
03601    void allocate(int sl, int sh);
03602 
03603    void allocate(int sl, int sh, int nrl, int nrh, const ivector& ncl, int nch);
03604    void allocate(int sl, int sh, const ivector & nrl, const ivector & nrh,
03605      const imatrix & ncl, const imatrix & nch);
03606    void allocate(int sl, int sh, int nrl, const ivector & nrh, int ncl,
03607      const imatrix & nch);
03608    void allocate(int sl, int sh, const ivector & nrl, const ivector & nrh,
03609      int ncl, const imatrix & nch);
03610    void allocate(int sl, int sh, const ivector & nrl, const ivector & nrh,
03611      int ncl, int nch);
03612    void allocate(int sl, int sh, const ivector& nrl, int nrh, int ncl, int nch);
03613    void allocate(int sl, int sh, int nrl, const ivector& nrh, int ncl, int nch);
03614    void allocate(int sl, int sh, const ivector & nrl, const ivector & nrh,
03615      const ivector & ncl, const ivector & nch);
03616    void allocate(int sl, int sh, int nrl, const ivector & nrh, int ncl,
03617      const ivector & nch);
03618    void allocate(int sl, int sh, int nrl, int nrh, const ivector & ncl,
03619      const ivector & nch);
03620    void allocate(int sl, int sh, int nrl, int nrh, int ncl,
03621      const ivector & nch);
03622    void allocate(void);
03623    void deallocate(void);
03624    void initialize(int sl, int sh, int nrl, const ivector & nrh,
03625      int ncl, const ivector & nch);
03626 
03627    //access functions
03628    int indexmin(void) const
03629    {
03630       return shape->slice_min;
03631    }
03632    int indexmax(void) const
03633    {
03634       return shape->slice_max;
03635    }
03636    int slicemin(void) const
03637    {
03638       return shape->slice_min;
03639    }
03640    int slicemax(void) const
03641    {
03642       return shape->slice_max;
03643    }
03644    int colmin(void) const
03645    {
03646       return ((*this) (slicemin()).colmin());
03647    }
03648    int colmax(void) const
03649    {
03650       return ((*this) (slicemin()).colmax());
03651    }
03652    int rowmin(void) const
03653    {
03654       return ((*this) (slicemin()).rowmin());
03655    }
03656    int rowmax(void) const
03657    {
03658       return ((*this) (slicemin()).rowmax());
03659    }
03660    // returns the number of rows
03661    int slicesize(void) const
03662    {
03663       return (slicemax() - slicemin() + 1);
03664    }
03665    // returns the number of rows
03666    int rowsize(void) const
03667    {
03668       return (rowmax() - rowmin() + 1);
03669    }
03670    // returns the number of columns
03671    int colsize(void) const
03672    {
03673       return (colmax() - colmin() + 1);
03674    }
03675    void initialize(void);
03676 
03677    dmatrix & elem(int k)
03678    {
03679       return (t[k]);
03680    }
03681    const dmatrix & elem(int k) const
03682    {
03683       return t[k];
03684    }
03685    double &operator () (int k, int i, int j);
03686    dvector & operator ()(int k, int i);
03687    dmatrix & operator[](int i);
03688    dmatrix & operator()(int i);
03689    const double &operator() (int k, int i, int j) const;
03690    const dvector & operator() (int k, int i) const;
03691    const dmatrix & operator[] (int i) const;
03692    const dmatrix & operator() (int i) const;
03693 
03694    d3_array & operator=(const d3_array & m1);
03695    d3_array & operator=(double x);
03696    friend d3_array value(const dvar3_array & ar);
03697 
03698    void fill_randn(const random_number_generator & rng);
03699    void fill_randcau(const random_number_generator & rng);
03700    void fill_randu(const random_number_generator & rng);
03701 
03702    void fill_randu(long int &n);
03703    void fill_randn(long int &n);
03704 
03705    void fill_randu_ni(long int &n);
03706    void fill_randn_ni(long int &n);
03707    double fill_seqadd(double, double);
03708    void operator /=(double d);
03709 };
03710 #ifdef OPT_LIB
03711 inline const double& d3_array::operator()(int k, int i, int j) const
03712 {
03713   return ((t[k].m[i]).v)[j];
03714 }
03715 inline const dvector& d3_array::operator()(int k, int i) const
03716 {
03717   return t[k].m[i];
03718 }
03719 inline const dmatrix& d3_array::operator()(int i) const
03720 {
03721   return t[i];
03722 }
03723 inline const dmatrix& d3_array::operator[](int i) const
03724 {
03725   return t[i];
03726 }
03727 inline double& d3_array::operator()(int k, int i, int j)
03728 {
03729   return ((t[k].m[i]).v)[j];
03730 }
03731 inline dvector& d3_array::operator()(int k, int i)
03732 {
03733   return t[k].m[i];
03734 }
03735 inline dmatrix& d3_array::operator()(int i)
03736 {
03737   return t[i];
03738 }
03739 inline dmatrix& d3_array::operator[](int i)
03740 {
03741   return t[i];
03742 }
03743 #endif
03744 
03749 class i3_array
03750 {
03751    imatrix *t;
03752    three_array_shape *shape;
03753  public:
03754 #  if defined(MFCL2_CONSTRUCTORS)
03755    i3_array(int sl, int sh, int nrl, int nrh, const ivector & nc);
03756    void allocate(int sl, int sh, int nrl, int nrh, const ivector & nc);
03757 #  endif
03758    int operator!(void) const
03759    {
03760       return (shape == NULL);
03761    }
03762    // conclass cgors
03763    void shallow_copy(const i3_array &);
03764    i3_array(void);
03765    i3_array(int sl, int sh, const index_type & nrl, const index_type & nrh,
03766      const index_type & ncl, const index_type & nch);
03767 
03768    i3_array(int _sl, int _sh, const imatrix & m1);
03769 
03770    i3_array(int sl, int sh);
03771    i3_array(int sl, int sh, int nrl, int nrh, int ncl, int nch);
03772    i3_array(int sl, int sh, int nrl, int nrh, const ivector & ncl, int nch);
03773 
03774    i3_array(int sl, int sh, const ivector & nrl, const ivector & nrh,
03775      const imatrix & ncl, const imatrix & nch);
03776    i3_array(int sl, int sh, const ivector & nrl, const ivector & nrh,
03777      int ncl, const imatrix & nch);
03778    i3_array(int sl, int sh, const ivector & nrl, const ivector & nrh,
03779      const ivector & ncl, const ivector & nch);
03780    i3_array(int sl, int sh, int nrl, int nrh, const ivector & ncl,
03781      const ivector & nch);
03782    i3_array(int sl, int sh, int nrl, const ivector & nrh, int ncl,
03783      const ivector & nch);
03784    i3_array(int sl, int sh, int nrl, const ivector & nrh, int ncl, int nch);
03785    i3_array(int sl, int sh, int nrl, const ivector & nrh, int ncl,
03786      const imatrix & nch);
03787    i3_array(const i3_array & m2);
03788    ~i3_array();
03789 
03790    void allocate(int sl, int sh, int nrl, const ivector& nrh, int ncl, int nch);
03791    void allocate(const dvar3_array &);
03792    void allocate(const i3_array & i3v);
03793    void allocate(int sl, int sh, int nrl, int nrh, int ncl, int nch);
03794    void allocate(int sl, int sh);
03795 
03796    void allocate(int sl, int sh, int nrl, int nrh, const ivector& ncl, int nch);
03797 
03798    void allocate(int sl, int sh, const index_type& nrl, const index_type& nrh,
03799      const index_type & ncl, const index_type & nch);
03800 
03801    void allocate(int sl, int sh, const ivector & nrl, const ivector & nrh,
03802      const imatrix & ncl, const imatrix & nch);
03803    void allocate(int sl, int sh, int nrl, const ivector & nrh, int ncl,
03804      const imatrix & nch);
03805    void allocate(int sl, int sh, const ivector & nrl, const ivector & nrh,
03806      int ncl, const imatrix & nch);
03807    void allocate(int sl, int sh, const ivector & nrl, const ivector & nrh,
03808      int ncl, int nch);
03809    void allocate(int sl, int sh, const ivector & nrl, int nrh,
03810      int ncl, int nch);
03811    //void allocate(int sl, int sh, int nrl, const ivector& nrh,
03812    //  int ncl,int nch);
03813    void allocate(int sl, int sh, const ivector & nrl, const ivector & nrh,
03814      const ivector & ncl, const ivector & nch);
03815    void allocate(int sl, int sh, int nrl, const ivector & nrh,
03816      int ncl, const ivector & nch);
03817    void allocate(int sl, int sh, int nrl, int nrh, const ivector & ncl,
03818      const ivector & nch);
03819    void allocate(int sl, int sh, int nrl, int nrh, int ncl, const ivector& nch);
03820    void allocate(void);
03821    void deallocate(void);
03822    void initialize(int sl, int sh, int nrl, const ivector & nrh, int ncl,
03823      const ivector & nch);
03824 
03825    //access functions
03826    int indexmin(void) const
03827    {
03828       return shape->slice_min;
03829    }
03830    int indexmax(void) const
03831    {
03832       return shape->slice_max;
03833    }
03834    int slicemin(void) const
03835    {
03836       return shape->slice_min;
03837    }
03838    int slicemax(void) const
03839    {
03840       return shape->slice_max;
03841    }
03842    int colmin(void) const
03843    {
03844       return ((*this) (slicemin()).colmin());
03845    }
03846    int colmax(void) const
03847    {
03848       return ((*this) (slicemin()).colmax());
03849    }
03850    int rowmin(void) const
03851    {
03852       return ((*this) (slicemin()).rowmin());
03853    }
03854    int rowmax(void) const
03855    {
03856       return ((*this) (slicemin()).rowmax());
03857    }
03858    // returns the number of rows
03859    int slicesize() const
03860    {
03861       return (slicemax() - slicemin() + 1);
03862    }
03863    // returns the number of rows
03864    int rowsize() const
03865    {
03866       return (rowmax() - rowmin() + 1);
03867    }
03868    // returns the number of columns
03869    int colsize() const
03870    {
03871       return (colmax() - colmin() + 1);
03872    }
03873    void initialize(void);
03874 
03875    imatrix & elem(int k)
03876    {
03877       return (t[k]);
03878    }
03879    const imatrix & elem(int k) const
03880    {
03881       return t[k];
03882    }
03883    int &operator()(int k, int i, int j);
03884    ivector & operator()(int k, int i);
03885    imatrix & operator[](int i);
03886    imatrix & operator()(int i);
03887    const int &operator()(int k, int i, int j) const;
03888    const ivector & operator()(int k, int i) const;
03889    const imatrix & operator[](int i) const;
03890    const imatrix & operator()(int i) const;
03891 
03892    i3_array & operator=(const i3_array & m1);
03893    i3_array & operator=(int x);
03894 
03895    void fill_randu(long int &n);
03896    void fill_randn(long int &n);
03897    void fill_randu_ni(long int &n);
03898    void fill_randn_ni(long int &n);
03899 };
03900 
03901 #ifdef OPT_LIB
03902 inline const int& i3_array::operator()(int k, int i, int j) const
03903 {
03904   return ((t[k].m[i]).v)[j];
03905 }
03906 inline const ivector& i3_array::operator()(int k, int i) const
03907 {
03908   return t[k].m[i];
03909 }
03910 inline const imatrix& i3_array::operator()(int i) const
03911 {
03912   return t[i];
03913 }
03914 inline const imatrix& i3_array::operator[](int i) const
03915 {
03916   return t[i];
03917 }
03918 inline int& i3_array::operator()(int k, int i, int j)
03919 {
03920   return ((t[k].m[i]).v)[j];
03921 }
03922 inline ivector& i3_array::operator()(int k, int i)
03923 {
03924   return t[k].m[i];
03925 }
03926 inline imatrix& i3_array::operator()(int i)
03927 {
03928   return t[i];
03929 }
03930 inline imatrix& i3_array::operator[](int i)
03931 {
03932   return t[i];
03933 }
03934 #endif
03935 
03936 #   if defined(__NUMBERVECTOR__)
03937 class param_init_matrix_vector;
03938 class param_init_bounded_matrix_vector;
03939 #   endif
03940 
03945 class dvar3_array
03946 {
03947   dvar_matrix* t;
03948   three_array_shape* shape;
03949 
03950 public:
03951    void shallow_copy(const dvar3_array &);
03952    dvar3_array sub(int, int);
03953    dvar3_array(int, int);
03954    int operator!(void) const
03955    {
03956       return (shape == NULL);
03957    }
03958    // conclass cgors
03959 
03960    void initialize(void);
03961    void allocate(int sl, int sh, int nrl, int nrh, int ncl, int nch);
03962    void allocate(int sl, int sh, int nrl, int nrh);
03963    void allocate(int sl, int sh, const index_type& nrl, const index_type& nrh);
03964    void allocate(int sl, int sh);
03965 
03966    void allocate(int sl, int sh, int nrl, int nrh, const ivector& ncl, int nch);
03967    void allocate(const d3_array & m1);
03968    void allocate(void);
03969    void allocate(const dvar3_array & m1);
03970    void allocate(int sl, int sh, int nrl, int nrh,
03971      const ivector & ncl, const ivector & nch);
03972    void allocate(int sl, int sh, const ivector & nrl, const ivector & nrh,
03973      const ivector & ncl, const ivector & nch);
03974    void allocate(int sl, int sh, const ivector & nrl, const ivector & nrh,
03975      int ncl, int nch);
03976    void allocate(int sl, int sh, const ivector & nrl, int nrh,
03977      int ncl, int nch);
03978    void allocate(int sl, int sh, int nrl, const ivector & nrh,
03979      int ncl, int nch);
03980    void allocate(int sl, int sh, int nrl, const ivector & nrh,
03981      int ncl, const ivector & nch);
03982    void allocate(int sl, int sh, int nrl, int nrh,
03983      int ncl, const ivector & nch);
03984    void allocate(ad_integer sl, ad_integer sh, const index_type & nrl,
03985      const index_type & nrh, const index_type & ncl, const index_type & nch);
03986    void allocate(ad_integer sl, ad_integer sh, const index_type & nrl,
03987      const index_type & nrh);
03988    void allocate(ad_integer sl, ad_integer sh);
03989 
03990    void deallocate();
03991    dvar3_array(int sl, int sh, int nrl, int nrh, int ncl, int nch);
03992    dvar3_array(int sl, int sh, int nrl, int nrh, const ivector & ncl, int nch);
03993 
03994    dvar3_array(int sl, int sh, const ivector & nrl, const ivector & nrh,
03995      ivector & ncl, const ivector & nch);
03996 
03997    dvar3_array(int sl, int sh, int nrl, const ivector & nrh, int ncl,
03998      const ivector & nch);
03999 
04000    dvar3_array(int sl, int sh, int nrl, const ivector & nrh, int ncl, int nch);
04001 
04002    dvar3_array(ad_integer sl, ad_integer sh, const index_type & nrl,
04003      const index_type & nrh, const index_type & ncl, const index_type & nch);
04004 
04005    dvar3_array(const d3_array & m2);
04006 #   if defined(__NUMBERVECTOR__)
04007    dvar3_array(const param_init_matrix_vector & m2);
04008    dvar3_array(const param_init_bounded_matrix_vector & m2);
04009 #   endif
04010 
04011    dvar3_array(const dvar3_array & m2);
04012 
04013    dvar3_array(void);
04014 
04015    ~dvar3_array();
04016 
04017    d3_array value(const dvar3_array &);
04018 
04019    //access functions
04020    int indexmin(void) const
04021    {
04022       return shape->slice_min;
04023    }
04024    int indexmax(void) const
04025    {
04026       return shape->slice_max;
04027    }
04028    int slicemin(void) const
04029    {
04030       return shape->slice_min;
04031    }
04032    int slicemax(void) const
04033    {
04034       return (shape->slice_max);
04035    }
04036    int colmin(void) const
04037    {
04038       return ((*this) (slicemin()).colmin());
04039    }
04040    int colmax(void) const
04041    {
04042       return ((*this) (slicemin()).colmax());
04043    }
04044    int rowmin(void) const
04045    {
04046       return ((*this) (slicemin()).rowmin());
04047    }
04048    int rowmax(void) const
04049    {
04050       return ((*this) (slicemin()).rowmax());
04051    }
04052    // returns the number of rows
04053    int slicesize() const
04054    {
04055       return (slicemax() - slicemin() + 1);
04056    }
04057    // returns the number of rows
04058    int rowsize() const
04059    {
04060       return (rowmax() - rowmin() + 1);
04061    }
04062    // returns the number of columns
04063    int colsize() const
04064    {
04065       return (colmax() - colmin() + 1);
04066    }
04067    dvar_matrix & elem(int k)
04068    {
04069       return (t[k]);
04070    }
04071    prevariable elem(int i, int j, int k)
04072    {
04073       return (t[k].elem(i, j));
04074    }
04075    const dvar_matrix & elem(int k) const
04076    {
04077       return t[k];
04078    }
04079    const prevariable elem(int i, int j, int k) const
04080    {
04081       return t[k].elem(i, j);
04082    }
04083 
04084 #ifdef OPT_LIB
04085    inline const prevariable operator() (int k, int i, int j) const
04086    {
04087       return (((t[k].m[i]).va) + j);
04088    }
04089 
04090    inline const dvar_vector & operator() (int k, int i) const
04091    {
04092       return (t[k].m[i]);
04093    }
04094 
04095    inline const dvar_matrix & operator() (int i) const
04096    {
04097       return (t[i]);
04098    }
04099 
04100    inline const dvar_matrix & operator[] (int i) const
04101    {
04102       return (t[i]);
04103    }
04104 
04105    inline prevariable operator () (int k, int i, int j)
04106    {
04107       return (((t[k].m[i]).va) + j);
04108    }
04109 
04110    inline dvar_vector & operator () (int k, int i)
04111    {
04112       return (t[k].m[i]);
04113    }
04114 
04115    inline dvar_matrix & operator() (int i)
04116    {
04117       return (t[i]);
04118    }
04119 
04120    inline dvar_matrix & operator[] (int i)
04121    {
04122       return (t[i]);
04123    }
04124 #else
04125    prevariable operator () (int k, int i, int j);
04126    dvar_vector & operator ()(int k, int i);
04127    dvar_matrix & operator[](int i);
04128    dvar_matrix & operator()(int i);
04129    const prevariable operator() (int k, int i, int j) const;
04130    const dvar_vector & operator() (int k, int i) const;
04131    const dvar_matrix & operator[] (int i) const;
04132    const dvar_matrix & operator() (int i) const;
04133 #endif
04134 
04135    dvar3_array & operator=(const d3_array & m1);
04136    dvar3_array & operator=(double x);
04137    dvar3_array & operator=(const dvar3_array & m1);
04138 
04139    void fill_randu(long int &n);
04140    void fill_randn(long int &n);
04141 
04142    void fill_randu_ni(long int &n);
04143    void fill_randn_ni(long int &n);
04144    double fill_seqadd(double, double);
04145    void operator/=(const prevariable &);
04146    void operator /=(double);
04147 };
04148 
04149 dvariable inv_cumd_exponential(const prevariable & y);
04150 dvariable cumd_exponential(const prevariable & x);
04151 
04152 double inv_cumd_exponential(double y);
04153 double cumd_exponential(double x);
04154 
04155 double cumd_logistic(const double &x);
04156 double inv_cumd_logistic(const double &x);
04157 
04158 dvariable cumd_logistic(const prevariable & x);
04159 dvariable inv_cumd_logistic(const prevariable & x);
04160 double inv_cumd_norm(const double &x);
04161 double cumd_norm(const double &x);
04162 double cumd_norm(const double &x, double);
04163 dvariable inv_cumd_norm(const prevariable & x);
04164 dvar_vector inv_cumd_norm(const dvar_vector & x);
04165 prevariable & cumd_norm(const prevariable & x);
04166 prevariable & bounded_cumd_norm(const prevariable & x, double);
04167 double bounded_cumd_norm(double x, double);
04168 //dvariable& old_cumd_norm(const prevariable& x);
04169 double normal_tail_right(const double &x);
04170 
04171 dvariable inv_cumd_norm_logistic(const prevariable & x, double);
04172 prevariable & cumd_norm_logistic(const prevariable & x, double);
04173 double inv_cumd_norm_logistic(double x, double);
04174 double cumd_norm_logistic(double x, double);
04175 
04180 class prevariable_position
04181 {
04182    double_and_int *v;
04183  public:
04184    prevariable_position(const prevariable & x)
04185    {
04186       v = x.get_v();
04187    }
04188    prevariable_position(double_and_int * p)
04189    {
04190       v = p;
04191    }
04192    double &xval()
04193    {
04194       return ((v->x));
04195    }
04196 };
04197 
04198 double restore_prevariable_derivative(const prevariable_position & pre);
04199 double restore_prevariable_derivative(void);
04200 prevariable_position restore_prevariable_position(void);
04201 void save_double_derivative(double x, const prevariable_position & pos);
04202 double restore_prevariable_value(void);
04203 void save_double_value(double x);
04204 int sum(const imatrix &);
04205 double sum(const dmatrix &);
04206 double sum(const d3_array &);
04207 double sum(const d4_array &);
04208 double sum(const d5_array &);
04209 double sum(const d6_array &);
04210 double sum(const d7_array &);
04211 dvariable sum(const dvar_matrix &);
04212 dvariable sum(const dvar3_array &);
04213 dvariable sum(const dvar4_array &);
04214 dvariable sum(const dvar5_array &);
04215 dvariable sum(const dvar6_array &);
04216 dvariable sum(const dvar7_array &);
04217 
04218 dmatrix fabs(const dmatrix & m);
04219 //double& value(const  double& u);
04220    //const double& value(const double& u);
04221 double norm(const d3_array &);
04222 double norm2(const d3_array &);
04223 double sumsq(const d3_array &);
04224 d3_array exp(const d3_array & m);
04225 d3_array mfexp(const d3_array & m);
04226 d3_array mfexp(const d3_array & m, double d);
04227 d3_array log(const d3_array & m);
04228 d3_array fabs(const d3_array & m);
04229 d3_array sin(const d3_array & m);
04230 d3_array cos(const d3_array & m);
04231 d3_array tan(const d3_array & m);
04232 d3_array sqrt(const d3_array & m);
04233 d3_array sqr(const d3_array & m);
04234 d3_array elem_prod(const d3_array & m1, const d3_array & m2);
04235 d3_array elem_div(const d3_array & m1, const d3_array & m2);
04236 d3_array operator+(const d3_array & m1, const d3_array & m2);
04237 d3_array operator+(const d3_array & m1, double m2);
04238 d3_array operator/(const d3_array & m1, double m2);
04239 d3_array operator/(double m2, const d3_array & m1);
04240 d3_array operator+(double m1, const d3_array & m2);
04241 d3_array operator-(const d3_array & m1, const d3_array & m2);
04242 d3_array operator-(const d3_array & m1, double m2);
04243 d3_array operator-(double m1, const d3_array & m2);
04244 d3_array operator*(const d3_array & m1, const d3_array & m2);
04245 dmatrix operator *(const d3_array & m1, const dvector & m2);
04246 d3_array operator*(const d3_array & m1, double m2);
04247 d3_array operator*(double m1, const d3_array & m2);
04248 
04249 dvariable norm(const dvar3_array & m);
04250 dvariable norm2(const dvar3_array & m);
04251 dvariable sumsq(const dvar3_array & m);
04252 dvar3_array exp(const dvar3_array & m);
04253 dvar3_array mfexp(const dvar3_array & m);
04254 dvar3_array mfexp(const dvar3_array & m, double d);
04255 dvar3_array log(const dvar3_array & m);
04256 dvar3_array fabs(const dvar3_array & m);
04257 dvar3_array sin(const dvar3_array & m);
04258 dvar3_array cos(const dvar3_array & m);
04259 dvar3_array tan(const dvar3_array & m);
04260 dvar3_array sqrt(const dvar3_array & m);
04261 dvar3_array sqr(const dvar3_array & m);
04262 dvar3_array elem_prod(const dvar3_array & m1, const dvar3_array & m2);
04263 dvar3_array elem_div(const dvar3_array & m1, const dvar3_array & m2);
04264 dvar3_array operator+(const dvar3_array & m1, const dvar3_array & m2);
04265 dvar3_array operator-(const dvar3_array & m1, const dvar3_array & m2);
04266 dvar3_array elem_prod(const d3_array & m1, const dvar3_array & m2);
04267 dvar3_array elem_div(const d3_array & m1, const dvar3_array & m2);
04268 dvar3_array operator+(const d3_array & m1, const dvar3_array & m2);
04269 dvar3_array operator-(const d3_array & m1, const dvar3_array & m2);
04270 dvar3_array elem_prod(const dvar3_array & m1, const d3_array & m2);
04271 dvar3_array elem_div(const dvar3_array & m1, const d3_array & m2);
04272 dvar3_array operator+(const dvar3_array & m1, const d3_array & m2);
04273 dvar3_array operator+(const dvar3_array & m1, const dvariable & m2);
04274 dvar3_array operator+(const dvariable & d1, const dvar3_array & m1);
04275 
04276 dvar3_array operator/(const prevariable & m2, const dvar3_array & m1);
04277 dvar3_array operator/(const prevariable & m2, const d3_array & m1);
04278 dvar3_array operator/(double m2, const dvar3_array & m1);
04279 
04280 dvar3_array operator/(const dvar3_array & m1, const prevariable & m2);
04281 dvar3_array operator/(const d3_array & m1, const prevariable & m2);
04282 dvar3_array operator/(const dvar3_array & m1, double m2);
04283 
04284 dvar3_array operator+(const dvariable & m1, const d3_array & m2);
04285 dvar3_array operator+(double m1, const dvar3_array & m2);
04286 dvar3_array operator-(const dvar3_array & m1, const d3_array & m2);
04287 dvar3_array operator-(const dvar3_array & m1, const dvariable & m2);
04288 dvar3_array operator-(const dvariable & m1, const d3_array & m2);
04289 dvar3_array operator-(const dvariable & m1, const dvar3_array & m2);
04290 dvar3_array operator-(double m1, const dvar3_array & m2);
04291 dvar3_array operator*(const dvar3_array & m1, const d3_array & m2);
04292 dvar3_array operator*(const dvar3_array & m1, const dvariable & m2);
04293 dvar3_array operator*(const dvariable & m1, const d3_array & m2);
04294 dvar3_array operator*(const dvariable & m1, const dvar3_array & m2);
04295 dvar3_array operator*(double m1, const dvar3_array & m2);
04296 
04297 ivector square(const ivector& x);
04298 
04299 double square(double x);
04300 dvector square(const dvector & x);
04301 dmatrix square(const dmatrix & x);
04302 d3_array square(const d3_array & x);
04303 
04304 dvariable & square(const prevariable & x);
04305 dvar_vector square(const dvar_vector & x);
04306 dvar_matrix square(const dvar_matrix & x);
04307 dvar3_array square(const dvar3_array & x);
04308 
04309 double cube(double x);
04310 double fourth(double x);
04311 dvector cube(const dvector & x);
04312 dmatrix cube(const dmatrix & x);
04313 d3_array cube(const d3_array & x);
04314 
04315 d3_array pow(const d3_array & x, int e);
04316 dvar3_array pow(const dvar3_array & x, int e);
04317 
04318 prevariable & cube(const prevariable & x);
04319 dvar_vector cube(const dvar_vector & x);
04320 dvar_matrix cube(const dvar_matrix & x);
04321 dvar3_array cube(const dvar3_array & x);
04322 
04323 void set_value(const dvar_matrix & x, const dvar_vector & v, const int &_ii,
04324   double s);
04325 void set_value(const dvar_matrix & x, const dvar_vector & v, const int &ii,
04326   double fmin, double fmax, const dvariable & fpen, double s);
04327 void set_value_inv(const dvar_matrix & x, const dvector & v, const int &ii,
04328   double s);
04329 void set_value_inv(const dvar_matrix & x, const dvector & v, const int &ii,
04330   double fmin, double fmax, double s);
04331 void set_value(const dvar_vector & x, const dvar_vector & v, const int &_ii,
04332    double s);
04333 void set_value(const dvar_vector & _x, const dvar_vector & v, const int &_ii,
04334   double fmin, double fmax, const dvariable & fpen, double s);
04335 void set_value_inv(const dvar_vector & x, const dvector & _v, const int &_ii,
04336   double s);
04337 void set_value_inv(const dvar_vector & x, const dvector & _v, const int &_ii,
04338   double fmin, double fmax, double s);
04339 void set_value_inv(const dvar_matrix & x, const dvector & v, const int &ii);
04340 void set_value_inv(const prevariable & x, const dvector & v, const int &ii,
04341   double s);
04342 void set_value_inv(const prevariable & x, const dvector & v, const int &ii);
04343 void set_value_inv(const dvar_matrix & x, const dvector & v, const int &ii);
04344 void set_value_inv(const dvar_matrix & u, const dvector & x, const int &ii,
04345   double fmin, double fmax);
04346 void set_value_inv(const dvar3_array & u, const dvector & x, const int &ii,
04347   double fmin, double fmax);
04348 void set_value_inv(const dvar3_array & u, const dvector & x, const int &ii);
04349 
04350 void set_value_inv(double x, const dvector & v, const int &ii);
04351 
04352 void set_value_inv_exp(const prevariable & x, const dvector & _v,
04353   const int &_ii, double fmin, double fmax, double s);
04354 
04355 void set_value_inv(const prevariable & x, const dvector & _v, const int &_ii,
04356   double fmin, double fmax, double s);
04357 
04358 void set_value_inv(const prevariable & u, const dvector & x, const int &ii,
04359   double fmin, double fmax);
04360 void set_value_inv(double u, const dvector & x, const int &ii, double fmin,
04361   double fmax);
04362 void set_value_inv(const dvector & x, const dvector & v, const int &ii);
04363 void set_value_inv(const dvar_vector & x, const dvector & v, const int &ii);
04364 void set_value_inv(const dvar_vector & x, const dvector & v, const int &ii,
04365   double fmin, double fmax);
04366 void set_value_inv(const dvector & x, const dvector & v, const int &ii,
04367   double fmin, double fmax);
04368 void set_value_inv(const dmatrix & x, const dvector & v, const int &ii);
04369 void set_value_inv(const dmatrix & x, const dvector & v, const int &ii,
04370   double fmin, double fmax);
04371 void set_value_inv(const d3_array & x, const dvector & v, const int &ii);
04372 void set_value_inv(const d3_array & x, const dvector & v, const int &ii,
04373   double fmin, double fmax);
04374 void set_value(const prevariable & x, const dvar_vector & v, const int &ii);
04375 void set_value(const prevariable & x, const dvar_vector & v, const int &ii,
04376   double s);
04377 void set_value(const dvar_vector & x, const dvar_vector & v, const int &ii);
04378 
04379 void set_value_exp(const prevariable & _x, const dvar_vector & v,
04380   const int &_ii, double fmin, double fmax, const dvariable & fpen, double s);
04381 void set_value(const prevariable & _x, const dvar_vector & v, const int &_ii,
04382   double fmin, double fmax, const dvariable & fpen, double s);
04383 
04384 void set_value(const prevariable & x, const dvar_vector & v, const int &ii,
04385   double fmin, double fmax, const dvariable & fpen);
04386 void set_value(const dvar_vector & x, const dvar_vector & v, const int &ii,
04387   double fmin, double fmax, const dvariable & fpen);
04388 void set_value(const dvar_matrix & x, const dvar_vector & v,
04389   const int &ii);
04390 void set_value(const dvar_matrix & x, const dvar_vector & v, const int &ii,
04391   double fmin, double fmax, const dvariable & fpen);
04392 void set_value(dvar3_array & x, const dvar_vector & v, const int &ii);
04393 void set_value(dvar3_array & x, const dvar_vector & v, const int &ii,
04394   double fmin, double fmax, const dvariable & fpen);
04395 
04396 void set_value_inv_partial(const dvector & x, const dvector & v, const int &ii,
04397   int n);
04398 void set_value_inv_partial(const dvector & x, const dvector & v, const int &ii,
04399   int n, double fmin, double fmax);
04400 void set_value_inv_partial(const dmatrix & x, const dvector & v, const int &ii,
04401   int n);
04402 void set_value_inv_partial(const dvar_matrix & x, const dvector & v,
04403   const int &ii, int n);
04404 void set_value_inv_partial(const d3_array & x, const dvector& v, const int &ii,
04405   int n);
04406 
04407 void set_value_inv_partial(const dvar_vector & x, const dvector & v,
04408   const int &ii, int n);
04409 void set_value_inv_partial(const dvar_vector & x, const dvector & v,
04410   const int &ii, int n, double fmin, double fmax);
04411 
04412 void set_value_partial(const dvar_vector & x, const dvar_vector & v,
04413   const int &ii, int n);
04414 void set_value_partial(const dvar_vector & x, const dvar_vector & v,
04415   const int &ii, int n, double fmin, double fmax, const dvariable & fpen);
04416 void set_value_partial(const dvar_matrix & x, const dvar_vector & v,
04417   const int &ii, int n);
04418 void set_value_partial(dvar3_array & x, const dvar_vector & v, const int &ii,
04419   int n);
04420 
04421 int size_count(const dvar_vector & x);
04422 int size_count(const dvar_matrix & x);
04423 int size_count(const dvar3_array & x);
04424 int size_count(const dvar4_array & x);
04425 int size_count(const dvector & x);
04426 int size_count(const dmatrix & x);
04427 int size_count(const d3_array & x);
04428 int size_count(const d4_array & x);
04429 
04430 int size_count_partial(const dvar_vector & x, int);
04431 int size_count_partial(const dvar_matrix & x, int);
04432 int size_count_partial(const dvar3_array & x, int);
04433 int size_count_partial(const dvector & x, int);
04434 int size_count_partial(const dmatrix & x, int);
04435 int size_count_partial(const d3_array & x, int);
04436 
04437 int min(int, int);
04438 // ********************************************************
04439 // Prototypes for compiled derivative calculations
04440 void dfinvpret(void);
04441 void dvdv_dot(void);
04442 void dmdm_prod(void);
04443 void dv_init(void);
04444 
04445 
04446 // ********************************************************
04447 int save_identifier_string(const char *);
04448 void insert_identifier_string(const char *s);
04449 void verify_identifier_string(const char *);
04450 
04451 
04452 ivector restore_ivector_value(const ivector_position &);
04453 ivector_position restore_ivector_position(void);
04454 dvar_matrix_position restore_dvar_matrix_position(void);
04455 dvector restore_dvar_matrix_derivative_row(const dvar_matrix_position& pos,
04456   const int &ii);
04457 dvector restore_dvar_matrix_derivative_column(const dvar_matrix_position& pos,
04458   const int &ii);
04459 dmatrix restore_dvar_matrix_derivatives(const dvar_matrix_position & pos);
04460 dmatrix restore_dvar_matrix_derivatives(void);
04461 double restore_prevariable_derivative(void);
04462 double restore_double_value(void);
04463 int restore_int_value(void);
04464 void save_double_value(double x);
04465 void save_int_value(int x);
04466 void save_pointer_value(void *ptr);
04467 void *restore_pointer_value(void);
04468 dvar_matrix nograd_assign_trans(const dmatrix & m);
04469 dvar_matrix nograd_assign(const dmatrix &);
04470 dvariable nograd_assign(double tmp);
04471 dvar_vector nograd_assign(dvector tmp);
04472 dmatrix restore_dvar_matrix_value(const dvar_matrix_position & mpos);
04473 dmatrix_position restore_dmatrix_position(void);
04474 dvector_position restore_dvector_position(void);
04475 dvector restore_dvector_value(const dvector_position &);
04476 dmatrix restore_dmatrix_value(const dmatrix_position &);
04477 dvector restore_dvar_matrix_derivatives(const dvar_matrix_position & pos,
04478   const int &ii);
04479 dvector restore_dvar_vector_derivatives(const dvar_vector_position & tmp);
04480 dmatrix restore_dvar_matrix_derivatives(const dvar_matrix_position & pos);
04481 void save_dmatrix_derivatives(const dvar_matrix_position & pos, double x,
04482   const int &i, int &j);
04483 dmatrix restore_dvar_matrix_der_nozero(const dvar_matrix_position & pos);
04484 dvector restore_dvar_vector_der_nozero(const dvar_vector_position & tmp);
04485 d3_array_position restore_d3_array_position(void);
04486 d3_array restore_d3_array_value(const d3_array_position &);
04487 void nograd_assign_row(const dvar_matrix & m, const dvector & v,
04488   const int &ii);
04489 void nograd_assign_column(const dvar_matrix & m, const dvector & v,
04490   const int &ii);
04491 
04492 long int reset_gs_stack(void);
04493 void reset_gs_stack(long int);
04494 
04495 dvar_vector solve(const dvar_matrix & aa, const dvar_vector & z);
04496 dvar_vector solve(const dvar_matrix & aa, const dvar_vector & z,
04497   prevariable & ln_unsigned_det, const prevariable & sign);
04498 
04499 //dvar_vector solve(const dvar_matrix& aa, const dvar_vector& z,
04500  // prevariable& ln_unsigned_det, const prevariable& sign);
04501 
04502 dvector csolve(const dmatrix & aa, const dvector & z);
04503 dvector solve(const dmatrix & aa, const dvector & z);
04504 dvector solve(const dmatrix & aa, const dvector & z,
04505   const double &ln_unsigned_det, double &sign);
04506 
04507 dmatrix choleski_decomp(const dmatrix & M);
04508 dmatrix choleski_decomp_error(const dmatrix & M, int &ierror);
04509 dmatrix choleski_decomp_neghess_error(const dmatrix & M, int &ierror);
04510 dmatrix choleski_decomp_positive(const dmatrix & MM, const int &ierr);
04511 dmatrix choleski_decomp_positive(const dmatrix & MM, double bound);
04512 dvar_matrix choleski_decomp(const dvar_matrix & M);
04513 
04514 dvar_matrix solve(const dvar_matrix & aa, const dvar_matrix & zz);
04515 dmatrix expm(const dmatrix & A);
04516 dvar_matrix expm(const dvar_matrix & A);
04517 
04518 dvariable factln(const dvariable & n);
04519 double factln(double n);
04520 dvar_vector factln(const dvar_vector & n);
04521 dvector factln(const dvector & n);
04522 
04523 dvar_vector posfun(const dvar_vector & x, double eps, const prevariable & pen);
04524 dvariable posfun(const dvariable& x, const double eps, const prevariable & pen);
04525 dvariable posfun2(const dvariable& x, const double eps, const prevariable& pen);
04526 double posfun(const double &x, const double eps, const double &_pen);
04527 double posfun2(const double &x, const double eps, const double &_pen);
04528 double dfposfun(const double &x, const double eps);
04529 dvariable dfposfun(const prevariable & x, const double eps);
04530 double dfposfun1(const double &x, const double eps);
04531 dvar_vector log_comb(const dvar_vector & n, const dvector & k);
04532 dvariable log_comb(double n, const dvariable & k);
04533 dvar_vector log_comb(const dvar_vector & n, const dvar_vector & k);
04534 dvar_vector log_comb(const dvector & n, const dvar_vector & k);
04535 dvar_vector log_comb(double n, const dvar_vector & k);
04536 dvar_vector log_comb(const dvariable & n, const dvector & k);
04537 dvar_vector log_comb(const dvariable & n, const dvar_vector & k);
04538 dvariable log_comb(const dvariable & n, double k);
04539 dvariable log_comb(const dvariable & n, const dvariable & k);
04540 dvector log_comb(const dvector & n, const dvector & k);
04541 dvector log_comb(double n, const dvector & k);
04542 double log_comb(double n, double k);
04543 dmatrix orthpoly(int n, int deg);
04544 dmatrix orthpoly(int n, int deg, int skip);
04545 dvar_vector gammln(const dvar_vector & n);
04546 dvector gammln(const dvector & n);
04547 
04552 class dvar_vector_position
04553 {
04554 public:
04555   dvar_vector_position();
04556   dvar_vector_position(const dvar_vector& v);
04557   dvar_vector_position(const dvar_vector_position& dvp);
04558 
04559   double &operator() (const int &i);
04560 
04561   int min;
04562   int max;
04563   double_and_int* va;
04564   int indexmin() const
04565   {
04566     return min;
04567   }
04568   int indexmax() const
04569   {
04570     return max;
04571   }
04572 
04573   friend class dvar_matrix_position;
04574 };
04575 
04580 class dvar_matrix_position
04581 {
04582  public:
04583    int row_min;
04584    int row_max;
04585    ivector lb;
04586    ivector ub;
04587    ptr_vector ptr;
04588    dvar_matrix_position(const dvar_matrix &, int);
04589    dvar_matrix_position(int min, int max);
04590    dvar_matrix_position(const dvar_matrix_position &);
04591    dvar_vector_position operator () (int i);
04592    int &rowmin(void)
04593    {
04594       return row_min;
04595    }
04596    int &rowmax(void)
04597    {
04598       return row_max;
04599    }
04600    ivector & colmin(void)
04601    {
04602       return lb;
04603    }
04604    ivector & colmax(void)
04605    {
04606       return ub;
04607    }
04608    friend ostream & operator<<(const ostream &, const dvar_matrix_position &);
04609    friend class dmatrix_position;
04610    friend class dmatrix;
04611 };
04612 
04613 dvar_matrix use_shape(const dvar_matrix & m);
04614 dmatrix use_shape(const dmatrix & m);
04615 
04620 class dmatrix_position
04621 {
04622  public:
04623    int row_min;
04624    int row_max;
04625    ivector lb;
04626    ivector ub;
04627    ptr_vector ptr;
04628    dmatrix_position(const dmatrix &);
04629    dmatrix_position(int min, int max);
04630    dmatrix_position(const dmatrix_position &);
04631    dvector_position operator () (int i);
04632    friend class dmatrix;
04633 };
04634 
04639 class d3_array_position
04640 {
04641    int min;
04642    int max;
04643  public:
04644    d3_array_position(int mmin, int mmax);
04645 
04646    int indexmin() const
04647    {
04648       return min;
04649    }
04650    int indexmax() const
04651    {
04652       return max;
04653    }
04654 };
04655 
04660 class dvector_position
04661 {
04662    int min;
04663    int max;
04664    double *v;
04665  public:
04666    dvector_position(const dvector & v);
04667    dvector_position(const dvector_position & dvp);
04668    dvector_position(void);
04669    int indexmin() const
04670    {
04671       return min;
04672    }
04673    int indexmax() const
04674    {
04675       return max;
04676    }
04677    friend class dmatrix_position;
04678 };
04679 
04684 class ivector_position
04685 {
04686    int min;
04687    int max;
04688    int *v;
04689  public:
04690    ivector_position(void);
04691    ivector_position(const ivector & v);
04692    ivector_position(const ivector_position & dvp);
04693    int indexmin() const
04694    {
04695       return min;
04696    }
04697    int indexmax() const
04698    {
04699       return max;
04700    }
04701 };
04702 
04703 ostream & operator<<(const ostream & s, const ptr_vector & ptr);
04704 ostream & operator<<(const ostream &, const dvar_matrix_position &);
04705 
04706 char which_library();
04707 
04712 class fmmq:public fmm_control
04713 {
04714  private:
04715    dvector h;
04716    dvector w;
04717    dvector funval;
04718 
04719    double dmin, fbest, df;
04720    long int llog, n1, ic, iconv, i1, link;
04721    double z, zz, gys, gs, sig, gso, alpha, tot, fy, dgs;
04722    long int itn, icc, np, nn, is, iu, iv, ib, ifn;
04723    int i, j;
04724    double gmax;
04725    double fsave;
04726    dvector gbest;
04727    dvector xbest;
04728    dvector xsave;
04729    dvector gsave;
04730    dvector scale;
04731    dvector xa;
04732    dvector xb;
04733    dvector d;
04734    dvector ga;
04735    dvector gb;
04736    int mode;
04737    int igwindow;
04738    int ir;
04739    int isfv;
04740    int istart;
04741    int istop;
04742    double c;
04743    double cc;
04744    double dff;
04745    double fa;
04746    double fb;
04747    double dga;
04748    double dgb;
04749    double stmin;
04750    double stepbd;
04751    double tfmin;
04752    double gmin;
04753    double step;
04754    double gl1;
04755    double gl2;
04756    unsigned int k;
04757    int ititle;
04758    int print;
04759    int ipra;
04760    int ip;
04761    int n;
04762  public:
04763    fmmq(int nvar);
04764    fmmq(int nvar, const lvector & ipar);
04765    double minimize(const dvector & x, double (*pf) (const dvar_vector &));
04766    double minimize(const independent_variables & x, const dvector & c,
04767      double (*pf) (const dvar_vector &, const dvector &));
04768    void fmin(const double &f, const dvector & x, const dvector & g);
04769    void va13c(const dvector & x, double f, const dvector & g);
04770 };
04771 
04776 class vcubic_spline_function
04777 {
04778    dvector x; // indep variables values
04779    dvar_vector y; // dep variable values
04780    dvar_vector y2; // second derivatives
04781  public:
04782    vcubic_spline_function(const dvector & _x, const dvar_vector & _y,
04783      double yp1 = 0.0, double ypn = 0.0);
04784    vcubic_spline_function(const dvector & _x, const dvar_vector & _y,
04785      dvariable yp1, dvariable ypn);
04786    vcubic_spline_function(const dvector & _x, const dvar_vector & _y,
04787      dvariable yp1);
04788    dvariable operator   () (double u);
04789    dvar_vector operator () (const dvector & u);
04790    dvar_vector operator () (const dvar_vector & u);
04791 };
04792 
04797 class cubic_spline_function
04798 {
04799    dvector x; // indep variables values
04800    dvector y; // dep variable values
04801    dvector y2; // second derivatives
04802  public:
04803    cubic_spline_function(const dvector & _x, const dvector & _y,
04804      double yp1 = 0.0, double ypn = 0.0);
04805    double operator () (double u);
04806    dvector operator() (const dvector & u);
04807 };
04808 
04809 #ifdef __ZTC__
04810 void *cdecl _farptr_norm(void *);
04811 void *cdecl _farptr_fromlong(unsigned long int);
04812 #endif
04813 /*
04814 #if DOS386==1
04815   #ifdef __NDPX__
04816     void * farptr_fromlong(long int);
04817     void * _farptr_fromlong(long int i);
04818   #elif __SUN__
04819     void * farptr_fromlong(long int);
04820   #elif __GNU__
04821     void * farptr_fromlong(long int);
04822   #elif __ZTC__
04823     void * _farptr_fromlong(unsigned long int i);
04824     void * _farptr_norm(void *);
04825   #else
04826     void * farptr_fromlong(long int);
04827   #endif
04828 #else
04829   #ifdef __ZTC__
04830     void * _farptr_norm(void *);
04831     void * _farptr_fromlong(unsigned long int);
04832   #else
04833     void * farptr_fromlong(long int);
04834   #endif
04835 #endif
04836 */
04837 
04838 // this is the speical version with an index for reordering the matrix
04839 void ludcmp_index(const dmatrix & a, const ivector & indx, const double &d);
04840 
04841 void ludcmp(const dmatrix & a, const ivector & indx, const double &d);
04842 
04847 class function_tweaker
04848 {
04849    double mult;
04850    double eps;
04851    dvector coffs;
04852  public:
04853    function_tweaker(double eps, double mult);
04854    double operator () (double);
04855 };
04856 
04861 class dfunction_tweaker
04862 {
04863    double mult;
04864    double eps;
04865    dvector coffs;
04866  public:
04867    dfunction_tweaker(double eps, double mult);
04868    dvariable operator () (const prevariable &);
04869 };
04870 
04875 class four_array_shape
04876 {
04877    unsigned int ncopies;
04878    int hslice_min;
04879    int hslice_max;
04880    //int slice_min;
04881    //int slice_max;
04882    //int row_min;
04883    //int row_max;
04884    //int col_min;
04885    //int col_max;
04886    four_array_shape(int hsl, int hsu);//, int sl,int sh,int rl,
04887    // int ru,int cl,int cu);
04888    //mat_shape(){};
04889 
04890    friend class d4_array;
04891    friend class dvar4_array;
04892 };
04893 
04898 class d4_array
04899 {
04900    four_array_shape *shape;
04901    d3_array *t;
04902  public:
04903    void shallow_copy(const d4_array &);
04904    d4_array(int, int);
04905    d4_array sub(int, int);
04906    void allocate(int hsl, int hsu, int sl, int sh, int nrl, int nrh, int ncl,
04907      int nch);
04908    void allocate(int hsl, int hsu, int sl, const ivector & sh, int nrl,
04909      const imatrix & nrh, int ncl, const imatrix & nch);
04910    void allocate(int hsl, int hsu, int sl, const ivector & sh, int nrl,
04911      const imatrix & nrh, int ncl, const i3_array & nch);
04912    void allocate(int hsl, int hsu, int sl, int sh, int nrl,
04913      int nrh, const ivector & ncl, const ivector & nch);
04914    void allocate(int hsl, int hsu, int sl, int sh, const ivector & nrl,
04915      const ivector & nrh, const ivector & ncl, const ivector & nch);
04916    void allocate(int hsl, int hsu, int sl, const ivector & sh, int nrl,
04917      const imatrix & nrh, int ncl, int nch);
04918    void deallocate(void);
04919    void allocate(void);
04920    void allocate(const d4_array &);
04921    void allocate(const dvar4_array &);
04922    int operator!(void) const
04923    {
04924       return (shape == NULL);
04925    }
04926    d4_array(int hsl, int hsu, int sl, int sh, ivector nrl, ivector nrh,
04927      ivector ncl, ivector nch);
04928 
04929    d4_array(int hsl, int hsu, int sl, const ivector & sh, int nrl,
04930      const imatrix & nrh, int ncl, const i3_array & nch);
04931 
04932    d4_array(int hsl, int hsu, const index_type & sl, const index_type & sh,
04933      const index_type & nrl, const index_type & nrh, const index_type & ncl,
04934      const index_type & nch);
04935 
04936    void allocate(int hsl, int hsu, const index_type & sl, const index_type& sh,
04937      const index_type & nrl, const index_type & nrh, const index_type & ncl,
04938      const index_type & nch);
04939    void allocate(ad_integer hsl, ad_integer hsu, const index_type & sl,
04940      const index_type & sh, const index_type & nrl, const index_type & nrh,
04941      const index_type & ncl, const index_type & nch);
04942 
04943    void allocate(ad_integer hsl, ad_integer hsu, const index_type & sl,
04944      const index_type & sh, const index_type & nrl, const index_type & nrh);
04945    void allocate(ad_integer hsl, ad_integer hsu, const index_type & sl,
04946      const index_type & sh);
04947    void allocate(ad_integer hsl, ad_integer hsu);
04948 
04949    d4_array & operator=(const d4_array &);
04950    d4_array(const d4_array & m2);
04951    d4_array(int, int, int, int, int, int, int, int);
04952    //d4_array(int,int,int,ivector,int,imatrix,int,int);
04953    d4_array(int hsl, int hsu, int sl, const ivector & sh, int nrl,
04954      const imatrix & nrh, int ncl, int nch);
04955    d4_array();
04956    ~d4_array();
04957    d3_array & elem(int i)
04958    {
04959       return t[i];
04960    }
04961    dmatrix & elem(int i, int j)
04962    {
04963       return ((*this) (i)) (j);
04964    }
04965    dvector & elem(int i, int j, int k)
04966    {
04967       return (((*this) (i, j)) (k));
04968    }
04969    double &elem(int i, int j, int k, int l)
04970    {
04971       return (((*this) (i, j, k)) (l));
04972    }
04973    const d3_array & elem(int i) const
04974    {
04975       return t[i];
04976    }
04977    const dmatrix & elem(int i, int j) const
04978    {
04979       return ((*this) (i)) (j);
04980    }
04981    const dvector & elem(int i, int j, int k) const
04982    {
04983       return (((*this) (i, j)) (k));
04984    }
04985    const double &elem(int i, int j, int k, int l) const
04986    {
04987       return (((*this) (i, j, k)) (l));
04988    }
04989    const d3_array& operator()(int i) const;
04990    const d3_array& operator[](int i) const;
04991    const dmatrix& operator()(int i, int j) const;
04992    const dvector& operator()(int i, int j, int k) const;
04993    const double& operator()(int i, int j, int k, int l) const;
04994    d3_array& operator()(int);
04995    d3_array& operator[](int);
04996    dmatrix& operator()(int, int);
04997    dvector&