inmost_variable.h 33.3 KB
Newer Older
Kirill Terekhov's avatar
Kirill Terekhov committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

#ifndef INMOST_AUTODIFF_ETVAR_H_INCLUDED
#define INMOST_AUTODIFF_ETVAR_H_INCLUDED
#include "inmost_common.h"
#include "inmost_expression.h"
#include "inmost_mesh.h"
#include "inmost_autodiff.h"
#include "inmost_solver.h"
#include <sstream> //for debug
#include <new>

#if defined(USE_AUTODIFF) && (!defined(USE_MESH) || !defined(USE_SOLVER))
#warning "USE_AUTODIFF require USE_MESH"
#undef USE_AUTODIFF
#endif


//TODO:
// 1. Incorporate tables
// 2. (ok, test) implement condition
// 3. (ok, test) implement stencil
// 4. (???) copying of basic_dynamic_variable
// 5. Consider optimization by checking zero variation multipliers, check that assembly do not degrade.
// 6. Document everything



#if defined(USE_AUTODIFF)
namespace INMOST
{
  
  template<class Op, class A>
  class unary_pool
  {
    A arg;
    Op operand;
  public:
    unary_pool(const A & parg) : arg(parg), operand(arg) {}
    unary_pool(const unary_pool & other) : arg(other.arg), operand(arg) {}
Kirill Terekhov's avatar
Kirill Terekhov committed
40
    unary_pool & operator = (unary_pool const & other) {arg = other.arg; operand = Op(arg); return * this;}
Kirill Terekhov's avatar
Kirill Terekhov committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
    const shell_expression<A> & get_arg() {return arg;}
    Op & get_op() {return operand;}
    const Op & get_op() const {return operand;}
  };


  template<class Op, class A>
  class unary_const_pool
  {
    A left;
    INMOST_DATA_REAL_TYPE right;
    Op operand;
  public:
    unary_const_pool(const A & pleft, INMOST_DATA_REAL_TYPE pright) : left(pleft), right(pright), operand(left,right) {}
    unary_const_pool(const unary_const_pool & other) : left(other.left), right(other.right), operand(left,right) {}
Kirill Terekhov's avatar
Kirill Terekhov committed
56
    unary_const_pool & operator = (unary_const_pool const & other) {left = other.left; right = other.right; operand = Op(left,right); return * this;}
Kirill Terekhov's avatar
Kirill Terekhov committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
    const shell_expression<A> & get_arg() {return left;}
    Op & get_op() {return operand;}
    const Op & get_op() const {return operand;}
  };

  template<class Op, class A, class B>
  class binary_pool
  {
    
    A left;
    B right;
    Op operand;
  public:
    binary_pool(const A & pleft, const B & pright) : left(pleft), right(pright), operand(left,right) {}
    binary_pool(const binary_pool & other) : left(other.left), right(other.right), operand(left,right) {}
Kirill Terekhov's avatar
Kirill Terekhov committed
72
    binary_pool & operator =(binary_pool const & other) {left = other.left; right = other.right; operand = Op(left,right); return * this;}
Kirill Terekhov's avatar
Kirill Terekhov committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
    const shell_expression<A> & get_left() {return left;}
    const shell_expression<B> & get_right() {return right;}
    Op & get_op() {return operand;}
    const Op & get_op() const {return operand;}
    ~binary_pool() {}
  };

  template<class Op, class A, class B, class C>
  class ternary_pool
  {
    A cond;
    B left;
    C right;
    Op operand;
  public:
    ternary_pool(const A & pcond, const B & pleft, const C & pright) : cond(pcond), left(pleft), right(pright), operand(cond,left,right) {}
    ternary_pool(const ternary_pool & other) : cond(other.cond), left(other.left), right(other.right), operand(cond,left,right) {}
Kirill Terekhov's avatar
Kirill Terekhov committed
90
    ternary_pool & operator =(ternary_pool const & other) {cond = other.cond; left = other.left; right = other.right; operand = Op(cond,left,right); return * this;}
Kirill Terekhov's avatar
Kirill Terekhov committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
    const shell_expression<A> & get_cond() {return cond;}
    const shell_expression<B> & get_left() {return left;}
    const shell_expression<C> & get_right() {return right;}
    Op & get_op() {return operand;}
    const Op & get_op() const {return operand;}
    ~ternary_pool() {}
  };

  template<class A, class ArgA>
  class unary_pool_expression : public shell_expression<unary_pool_expression<A,ArgA> >
  {
    unary_pool<A,ArgA> pool;
   public:
    unary_pool_expression(const unary_pool<A,ArgA> & ppool) : pool(ppool) {}
    unary_pool_expression(const unary_pool_expression & other) : pool(other.pool) {}
Kirill Terekhov's avatar
Kirill Terekhov committed
106
    unary_pool_expression & operator = (unary_pool_expression const & other) {pool = other.pool; return * this;}
Kirill Terekhov's avatar
Kirill Terekhov committed
107
    __INLINE INMOST_DATA_REAL_TYPE GetValue() const { return pool.get_op().GetValue(); }
Kirill Terekhov's avatar
Kirill Terekhov committed
108
109
    __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::RowMerger & r) const {pool.get_op().GetJacobian(mult,r);}
    __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::Row & r) const {pool.get_op().GetJacobian(mult,r);}
Kirill Terekhov's avatar
Kirill Terekhov committed
110
111
112
113
114
115
116
117
118
  };

  template<class A, class ArgA>
  class unary_const_pool_expression : public shell_expression<unary_const_pool_expression<A,ArgA> >
  {
    unary_const_pool<A,ArgA> pool;
   public:
    unary_const_pool_expression(const unary_const_pool<A,ArgA> & ppool) : pool(ppool) {}
    unary_const_pool_expression(const unary_const_pool_expression & other) : pool(other.pool) {}
Kirill Terekhov's avatar
Kirill Terekhov committed
119
    unary_const_pool_expression & operator = (unary_const_pool_expression const & other) {pool = other.pool; return * this;}
Kirill Terekhov's avatar
Kirill Terekhov committed
120
    __INLINE INMOST_DATA_REAL_TYPE GetValue() const { return pool.get_op().GetValue(); }
Kirill Terekhov's avatar
Kirill Terekhov committed
121
122
    __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::RowMerger & r) const {pool.get_op().GetJacobian(mult,r);}
    __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::Row & r) const {pool.get_op().GetJacobian(mult,r);}
Kirill Terekhov's avatar
Kirill Terekhov committed
123
124
125
126
127
128
129
130
131
  };

  template<class A, class ArgA, class ArgB>
  class binary_pool_expression : public shell_expression<binary_pool_expression<A,ArgA,ArgB> >
  {
    binary_pool<A,ArgA,ArgB> pool;
   public:
    binary_pool_expression(const binary_pool<A,ArgA,ArgB> & ppool) : pool(ppool) {}
    binary_pool_expression(const binary_pool_expression & other) : pool(other.pool) {}
Kirill Terekhov's avatar
Kirill Terekhov committed
132
    binary_pool_expression & operator = (binary_pool_expression const & other) {pool = other.pool; return * this;}
Kirill Terekhov's avatar
Kirill Terekhov committed
133
    __INLINE INMOST_DATA_REAL_TYPE GetValue() const { return pool.get_op().GetValue(); }
Kirill Terekhov's avatar
Kirill Terekhov committed
134
135
    __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::RowMerger & r) const {pool.get_op().GetJacobian(mult,r);}
    __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::Row & r) const {pool.get_op().GetJacobian(mult,r);}
Kirill Terekhov's avatar
Kirill Terekhov committed
136
137
138
139
140
141
142
143
144
  };

  template<class A, class ArgA, class ArgB, class ArgC>
  class ternary_pool_expression : public shell_expression<ternary_pool_expression<A,ArgA,ArgB,ArgC> >
  {
    ternary_pool<A,ArgA,ArgB,ArgC> pool;
   public:
    ternary_pool_expression(const ternary_pool<A,ArgA,ArgB,ArgC> & ppool) : pool(ppool) {}
    ternary_pool_expression(const ternary_pool_expression & other) : pool(other.pool) {}
Kirill Terekhov's avatar
Kirill Terekhov committed
145
    ternary_pool_expression & operator = (ternary_pool_expression const & other) {pool = other.pool; return * this;}
Kirill Terekhov's avatar
Kirill Terekhov committed
146
    __INLINE INMOST_DATA_REAL_TYPE GetValue() const { return pool.get_op().GetValue(); }
Kirill Terekhov's avatar
Kirill Terekhov committed
147
148
    __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::RowMerger & r) const {pool.get_op().GetJacobian(mult,r);}
    __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::Row & r) const {pool.get_op().GetJacobian(mult,r);}
Kirill Terekhov's avatar
Kirill Terekhov committed
149
150
151
152
153
154
155
156
157
  };

  class abstract_dynamic_variable
  {
  public:
    virtual INMOST_DATA_REAL_TYPE Value (const Storage & e) const = 0;
    virtual multivar_expression Variable(const Storage & e) const = 0;
    virtual void GetVariation(const Storage & e, Sparse::Row & r) const = 0;
    virtual void GetVariation(const Storage & e, Sparse::RowMerger & r) const = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
158
    virtual abstract_dynamic_variable * Copy() const = 0;
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
159
    virtual ~abstract_dynamic_variable() {}
Kirill Terekhov's avatar
Kirill Terekhov committed
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
  };

  template<typename RetType>
  class get_variable
  {
  public:
    virtual RetType operator()(const Storage & e) const = 0;
  };

  template<>
  class get_variable<multivar_expression>
  {
    const abstract_dynamic_variable & var;
  public:
    typedef multivar_expression type;
    get_variable(const abstract_dynamic_variable & var) : var(var) {}
    multivar_expression operator()(const Storage & e) const {return var.Variable(e);}
  };

  template<>
  class get_variable<INMOST_DATA_REAL_TYPE>
  {
    const abstract_dynamic_variable & var;
  public:
    typedef INMOST_DATA_REAL_TYPE type;
    get_variable(const abstract_dynamic_variable & var) : var(var) {}
    INMOST_DATA_REAL_TYPE operator()(const Storage & e) const {return var.Value(e);}
  };

Kirill Terekhov's avatar
Kirill Terekhov committed
189
  class store_variable
Kirill Terekhov's avatar
Kirill Terekhov committed
190
  {
Kirill Terekhov's avatar
Kirill Terekhov committed
191
    abstract_dynamic_variable * var;
Kirill Terekhov's avatar
Kirill Terekhov committed
192
  public:
Kirill Terekhov's avatar
Kirill Terekhov committed
193
194
195
196
197
198
    store_variable(const abstract_dynamic_variable & var) : var(var.Copy()) {}
    ~store_variable() {delete var;}
    template<typename T>
    get_variable<T> get_variable() {return get_variable<T>(*var);}
    abstract_dynamic_variable & retrive() {return *var;}
    const abstract_dynamic_variable & retrive() const {return *var;}
Kirill Terekhov's avatar
Kirill Terekhov committed
199
200
201
202
203
204
205
206
207
208
209
210
211
212
  };



  template<class VariableType>
  class basic_dynamic_variable : public abstract_dynamic_variable
  {
  public:
    typedef VariableType Var;
    virtual INMOST_DATA_REAL_TYPE Value(const Storage & e) const = 0;
    virtual multivar_expression Variable(const Storage & e) const = 0;
    virtual VariableType operator[](const Storage & e) const = 0;
    virtual void GetVariation(const Storage & e, Sparse::Row & r) const = 0;
    virtual void GetVariation(const Storage & e, Sparse::RowMerger & r) const = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
213
    virtual abstract_dynamic_variable * Copy() const = 0;
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
214
    virtual ~basic_dynamic_variable() {}
Kirill Terekhov's avatar
Kirill Terekhov committed
215
216
217
218
219
220
221
222
223
224
225
226
  };

  template<class VariableType, class Derived>
  class shell_dynamic_variable : virtual public basic_dynamic_variable<VariableType>
  {
  public:
    typedef VariableType Var;
    virtual INMOST_DATA_REAL_TYPE Value(const Storage & e) const {return static_cast<const Derived *>(this)->Value(e);}
    virtual multivar_expression operator ()(const Storage & e) const {return static_cast<const Derived *>(this)->Variable(e);}
    virtual VariableType operator[](const Storage & e) const {return (*static_cast<const Derived *>(this))[e];}
    virtual void GetVariation(const Storage & e, Sparse::Row & r) const {static_cast<const Derived *>(this)->GetVariation(e,r);}
    virtual void GetVariation(const Storage & e, Sparse::RowMerger & r) const {static_cast<const Derived *>(this)->GetVariation(e,r);}
Kirill Terekhov's avatar
Kirill Terekhov committed
227
228
229
    operator Derived & () {return *static_cast<Derived *>(this);}
    operator const Derived & () const {return *static_cast<const Derived *>(this);}
    virtual abstract_dynamic_variable * Copy() const { return static_cast<const Derived *>(this)->Copy(); }
Kirill Terekhov's avatar
Kirill Terekhov committed
230
231
  };
	
Kirill Terekhov's avatar
Kirill Terekhov committed
232
233
  
  class dynamic_variable : public shell_dynamic_variable<var_expression,dynamic_variable >
Kirill Terekhov's avatar
Kirill Terekhov committed
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
  {
  private:
    Automatizator & aut;
    Tag index_tag, value_tag;
    MarkerType mask;
    INMOST_DATA_ENUM_TYPE comp;
  public:
    dynamic_variable(Automatizator & paut, INMOST_DATA_ENUM_TYPE pregval, INMOST_DATA_ENUM_TYPE pcomp = 0) : aut(paut), comp(pcomp) 
    {
      if( pregval != ENUMUNDEF )
      {
        mask = aut.GetDynamicMask(pregval);
        if( pregval < AD_CTAG )
        {
          value_tag = aut.GetDynamicValueTag(pregval);
          index_tag = aut.GetDynamicIndexTag(pregval);
        }
        else
        {
          value_tag = aut.GetStaticValueTag(pregval);
          index_tag = Tag();
        }
      }
    }
    dynamic_variable(const dynamic_variable & other) : aut(other.aut), index_tag(other.index_tag), value_tag(other.value_tag), mask(other.mask), comp(other.comp) {}
    dynamic_variable & operator =(const dynamic_variable & other) 
    {
      aut = other.aut; 
      index_tag = other.index_tag; 
      value_tag = other.value_tag;
      mask = other.mask;
      comp = other.comp;
Kirill Terekhov's avatar
Kirill Terekhov committed
266
      return * this;
Kirill Terekhov's avatar
Kirill Terekhov committed
267
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
268
269
    INMOST_DATA_REAL_TYPE Value(const Storage & e) const {return e->RealArray(value_tag)[comp];}
    INMOST_DATA_ENUM_TYPE Index(const Storage & e) const {return (!mask || e->GetMarker(mask))?e->IntegerArray(index_tag)[comp]:ENUMUNDEF;}
Kirill Terekhov's avatar
Kirill Terekhov committed
270
271
272
    multivar_expression Variable(const Storage & e) const 
    {
      if( !mask || e->GetMarker(mask) )
Kirill Terekhov's avatar
Kirill Terekhov committed
273
        return multivar_expression(e->RealArray(value_tag)[comp],e->IntegerArray(index_tag)[comp]);
Kirill Terekhov's avatar
Kirill Terekhov committed
274
275
276
277
278
279
      else
        return multivar_expression(e->Real(value_tag));
    }
    var_expression operator [](const Storage & e) const {return var_expression(e->Real(value_tag),(!mask || e->GetMarker(mask))?e->Integer(index_tag):ENUMUNDEF);}
    Tag IndexTag() {return index_tag;}
    Tag ValueTag() {return value_tag;}
Kirill Terekhov's avatar
Kirill Terekhov committed
280
281
    void GetVariation(const Storage & e, Sparse::Row & r) const { (*this)[e].GetJacobian(1.0,r); }
    void GetVariation(const Storage & e, Sparse::RowMerger & r) const { (*this)[e].GetJacobian(1.0,r); }
Kirill Terekhov's avatar
Kirill Terekhov committed
282
    bool isUnknown(const Storage & e) const {return (!mask || e->GetMarker(mask))?true:false;}
Kirill Terekhov's avatar
Kirill Terekhov committed
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
    abstract_dynamic_variable * Copy() const {return static_cast<abstract_dynamic_variable *>(new dynamic_variable(*this));}
  };

  
  class static_variable : public shell_dynamic_variable<const_expression,static_variable>
  {
  private:
    Tag value_tag;
    INMOST_DATA_ENUM_TYPE comp;
  public:
    static_variable(Tag t, INMOST_DATA_ENUM_TYPE pcomp = 0) : value_tag(t), comp(pcomp)  {}
    static_variable(const static_variable & other) : value_tag(other.value_tag), comp(other.comp) {}
    static_variable & operator =(const static_variable & other) 
    {
      value_tag = other.value_tag;
      comp = other.comp;
      return * this;
    }
    INMOST_DATA_REAL_TYPE Value(const Storage & e) const {return e->RealArray(value_tag)[comp];}
    multivar_expression Variable(const Storage & e) const 
    {
      return multivar_expression(e->RealArray(value_tag)[comp]);
    }
    const_expression operator [](const Storage & e) const {return const_expression(e->RealArray(value_tag)[comp]);}
    Tag ValueTag() {return value_tag;}
Kirill Terekhov's avatar
Kirill Terekhov committed
308
309
    void GetVariation(const Storage & e, Sparse::Row & r) const { (*this)[e].GetJacobian(1.0,r); }
    void GetVariation(const Storage & e, Sparse::RowMerger & r) const { (*this)[e].GetJacobian(1.0,r); }
Kirill Terekhov's avatar
Kirill Terekhov committed
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
    bool isUnknown(const Storage & e) const {return false;}
    abstract_dynamic_variable * Copy() const {return static_cast<abstract_dynamic_variable *>(new static_variable(*this));}
  };

  class stored_variable : public shell_dynamic_variable<multivar_expression,stored_variable>
  {
  private:
    Tag variable_tag;
    INMOST_DATA_ENUM_TYPE comp;
  public:
    stored_variable(Tag t, INMOST_DATA_ENUM_TYPE pcomp = 0) : variable_tag(t), comp(pcomp)  {}
    stored_variable(const stored_variable & other) : variable_tag(other.variable_tag), comp(other.comp) {}
    stored_variable & operator =(const stored_variable & other) 
    {
      variable_tag = other.variable_tag;
      comp = other.comp;
      return * this;
    }
    INMOST_DATA_REAL_TYPE Value(const Storage & e) const {return e->VariableArray(variable_tag)[comp].GetValue();}
    multivar_expression Variable(const Storage & e) const 
    {
      return e->VariableArray(variable_tag)[comp];
    }
    multivar_expression operator [](const Storage & e) const {return e->VariableArray(variable_tag)[comp];}
    Tag VariableTag() {return variable_tag;}
Kirill Terekhov's avatar
Kirill Terekhov committed
335
336
    void GetVariation(const Storage & e, Sparse::Row & r) const { (*this)[e].GetJacobian(1.0,r); }
    void GetVariation(const Storage & e, Sparse::RowMerger & r) const { (*this)[e].GetJacobian(1.0,r); }
Kirill Terekhov's avatar
Kirill Terekhov committed
337
338
    bool isUnknown(const Storage & e) const {return false;}
    abstract_dynamic_variable * Copy() const {return static_cast<abstract_dynamic_variable *>(new stored_variable(*this));}
Kirill Terekhov's avatar
Kirill Terekhov committed
339
340
341
342
343
344
345
346
347
348
349
350
351
  };

  template<class A>
  class stencil_variable : public shell_dynamic_variable< stencil_expression<typename A::Var>, stencil_variable<A> >
  {
  private:
    Automatizator & aut;
    INMOST_DATA_ENUM_TYPE stnclind;
    const A & Arg;
    void * user_data;
  public:
    stencil_variable(Automatizator & paut, INMOST_DATA_ENUM_TYPE pstnclind, const shell_dynamic_variable<typename A::Var,A> & parg, void * puser_data = NULL) : aut(paut), stnclind(pstnclind), Arg(parg), user_data(puser_data) {}
    stencil_variable(const stencil_variable & other) : aut(other.aut), stnclind(other.stnclind), Arg(other.Arg), user_data(other.user_data) {}
Kirill Terekhov's avatar
Kirill Terekhov committed
352
    stencil_variable & operator =(const stencil_variable & other) {aut = other.aut; stnclind = other.stnclind; Arg = other.Arg; user_data = other.user_data; return * this;}
Kirill Terekhov's avatar
Kirill Terekhov committed
353
354
355
356
357
358
359
360
361
362
    INMOST_DATA_REAL_TYPE Value(const Storage & e) const {return (*this)[e].GetValue();}
    multivar_expression Variable(const Storage & e) const
    {
      multivar_expression ret = (*this)[e];
      return ret;
    }
    stencil_expression<typename A::Var> operator [](const Storage & e) const
    {
      dynarray<std::pair<INMOST_DATA_REAL_TYPE, typename A::Var>, 64> tmp;
      Automatizator::stencil_pairs stncl;
363
			aut.GetStencil(stnclind, e, user_data, stncl);
Kirill Terekhov's avatar
Kirill Terekhov committed
364
365
      tmp.resize(stncl.size());
      for(INMOST_DATA_ENUM_TYPE k = 0; k < stncl.size(); ++k)
Kirill Terekhov's avatar
Kirill Terekhov committed
366
        tmp[k] = std::make_pair(stncl[k].first, Arg[stncl[k].second]);
Kirill Terekhov's avatar
Kirill Terekhov committed
367
368
      return stencil_expression<typename A::Var>(tmp);
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
369
370
    void GetVariation(const Storage & e, Sparse::Row & r) const { (*this)[e].GetJacobian(1.0,r); }
    void GetVariation(const Storage & e, Sparse::RowMerger & r) const { (*this)[e].GetJacobian(1.0,r); }
Kirill Terekhov's avatar
Kirill Terekhov committed
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
    abstract_dynamic_variable * Copy() const {return static_cast<abstract_dynamic_variable *>(new stencil_variable(*this));}
  };

  template<class A>
  class table_variable : public shell_dynamic_variable< unary_pool_expression< function_expression<typename A::Var>,typename A::Var > , table_variable<A> >
  {
    const A & Arg;
    const keyval_table & Table;
  public:
    table_variable(const shell_dynamic_variable<typename A::Var,A> & parg, const keyval_table & ptable) : Arg(parg), Table(ptable) {}
    table_variable(const table_variable & other) : Arg(other.Arg), Table(other.table) {}
    table_variable & operator = (table_variable const & other) {Arg = other.Arg; Table = other.Table; return * this;}
    multivar_expression Variable(const Storage & e) const
    {
      multivar_expression ret = (*this)[e];
      return ret;
    }
    unary_pool_expression< function_expression<typename A::Var> ,typename A::Var > operator [](const Storage & e) const
    {
      typename A::Var arg = Arg[e];
      unary_pool< function_expression<typename A::Var>, typename A::Var> pool(arg);
      std::pair<INMOST_DATA_REAL_TYPE, INMOST_DATA_REAL_TYPE> both = Table.GetBoth(arg.GetValue());
      pool.get_op().SetFunctionValue(both.first);
      pool.get_op().SetFunctionDerivative(both.second);
      return unary_pool_expression< function_expression<typename A::Var>, typename A::Var >(pool);
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
397
398
    void GetVariation(const Storage & e, Sparse::Row & r) const { (*this)[e].GetJacobian(1.0,r); }
    void GetVariation(const Storage & e, Sparse::RowMerger & r) const { (*this)[e].GetJacobian(1.0,r); }
Kirill Terekhov's avatar
Kirill Terekhov committed
399
    abstract_dynamic_variable * Copy() const {return static_cast<abstract_dynamic_variable *>(new table_variable(*this));}
Kirill Terekhov's avatar
Kirill Terekhov committed
400
401
402
403
404
405
406
407
  };

  template<class Expr, class A>
  class unary_custom_variable : public shell_dynamic_variable< unary_pool_expression<Expr, typename A::Var >,unary_custom_variable<Expr,A> >
  {
  private:
    const A & Arg;
  public:
Kirill Terekhov's avatar
Kirill Terekhov committed
408
    unary_custom_variable(const shell_dynamic_variable<typename A::Var,A> & parg) : Arg(parg) {}
Kirill Terekhov's avatar
Kirill Terekhov committed
409
    unary_custom_variable(const unary_custom_variable & other) : Arg(other.Arg) {}
Kirill Terekhov's avatar
Kirill Terekhov committed
410
    unary_custom_variable & operator =(unary_custom_variable const & other) {Arg = other.Arg; return * this;}
Kirill Terekhov's avatar
Kirill Terekhov committed
411
412
413
414
415
416
417
418
    INMOST_DATA_REAL_TYPE Value(const Storage & e) const {return (*this)[e].GetValue();}
    multivar_expression Variable(const Storage & e) const
    {
      multivar_expression ret = (*this)[e];
      return ret;
    }
    unary_pool_expression<Expr, typename A::Var > operator [](const Storage & e) const 
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
419
      unary_pool<Expr,typename A::Var> pool(Arg[e]);
Kirill Terekhov's avatar
Kirill Terekhov committed
420
421
      return unary_pool_expression<Expr, typename A::Var >(pool);
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
422
423
    void GetVariation(const Storage & e, Sparse::Row & r) const { (*this)[e].GetJacobian(1.0,r); }
    void GetVariation(const Storage & e, Sparse::RowMerger & r) const { (*this)[e].GetJacobian(1.0,r); }
Kirill Terekhov's avatar
Kirill Terekhov committed
424
    abstract_dynamic_variable * Copy() const {return static_cast<abstract_dynamic_variable *>(new unary_custom_variable(*this));}
Kirill Terekhov's avatar
Kirill Terekhov committed
425
426
427
428
429
430
431
432
433
434
435
436
437
  };


  template<class Expr, class A, class B>
  class binary_custom_variable : public shell_dynamic_variable< binary_pool_expression<Expr, typename A::Var, typename B::Var >,binary_custom_variable<Expr,A,B> >
  {
  private:
    const A & Left;
    const B & Right;
  public:
    binary_custom_variable(const shell_dynamic_variable<typename A::Var,A> & pleft, const shell_dynamic_variable<typename B::Var,B> & pright) 
    : Left(pleft), Right(pright) {}
    binary_custom_variable(const binary_custom_variable & other) : Left(other.Left), Right(other.Right) {}
Kirill Terekhov's avatar
Kirill Terekhov committed
438
    binary_custom_variable & operator =(binary_custom_variable const & other) {Left = other.Left; Right = other.Right; return * this;}
Kirill Terekhov's avatar
Kirill Terekhov committed
439
440
441
442
443
444
445
446
    INMOST_DATA_REAL_TYPE Value(const Storage & e) const {return (*this)[e].GetValue();}
    multivar_expression Variable(const Storage & e) const
    {
      multivar_expression ret = (*this)[e];
      return ret;
    }
    binary_pool_expression<Expr, typename A::Var, typename B::Var > operator [](const Storage & e) const 
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
447
      binary_pool<Expr,typename A::Var,typename B::Var> pool(Left[e],Right[e]);
Kirill Terekhov's avatar
Kirill Terekhov committed
448
449
      return binary_pool_expression<Expr, typename A::Var, typename B::Var >(pool);
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
450
451
    void GetVariation(const Storage & e, Sparse::Row & r) const { (*this)[e].GetJacobian(1.0,r); }
    void GetVariation(const Storage & e, Sparse::RowMerger & r) const { (*this)[e].GetJacobian(1.0,r); }
Kirill Terekhov's avatar
Kirill Terekhov committed
452
    abstract_dynamic_variable * Copy() const {return static_cast<abstract_dynamic_variable *>(new binary_custom_variable(*this));}
Kirill Terekhov's avatar
Kirill Terekhov committed
453
454
455
456
457
458
459
460
461
462
463
464
  };

  template<class Expr, class A>
  class unary_const_custom_variable : public shell_dynamic_variable< unary_const_pool_expression<Expr, typename A::Var >,unary_const_custom_variable<Expr,A> >
  {
  private:
    const A & Left;
    INMOST_DATA_REAL_TYPE Right;
  public:
    unary_const_custom_variable(const shell_dynamic_variable<typename A::Var,A> & pleft, INMOST_DATA_REAL_TYPE pright) 
    : Left(pleft), Right(pright) {}
    unary_const_custom_variable(const unary_const_custom_variable & other) : Left(other.Left), Right(other.Right) {}
Kirill Terekhov's avatar
Kirill Terekhov committed
465
    unary_const_custom_variable & operator =(unary_const_custom_variable const & other) {Left = other.Left; Right = other.Right; return * this;}
Kirill Terekhov's avatar
Kirill Terekhov committed
466
467
468
469
470
471
472
473
    INMOST_DATA_REAL_TYPE Value(const Storage & e) const {return (*this)[e].GetValue();}
    multivar_expression Variable(const Storage & e) const
    {
      multivar_expression ret = (*this)[e];
      return ret;
    }
    unary_const_pool_expression<Expr, typename A::Var > operator [](const Storage & e) const 
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
474
      unary_const_pool<Expr,typename A::Var> pool(Left[e],Right);
Kirill Terekhov's avatar
Kirill Terekhov committed
475
476
      return unary_const_pool_expression<Expr, typename A::Var >(pool);
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
477
478
    void GetVariation(const Storage & e, Sparse::Row & r) const { (*this)[e].GetJacobian(1.0,r); }
    void GetVariation(const Storage & e, Sparse::RowMerger & r) const { (*this)[e].GetJacobian(1.0,r); }
Kirill Terekhov's avatar
Kirill Terekhov committed
479
    abstract_dynamic_variable * Copy() const {return static_cast<abstract_dynamic_variable *>(new unary_const_custom_variable(*this));}
Kirill Terekhov's avatar
Kirill Terekhov committed
480
481
482
483
484
485
486
487
488
489
490
491
492
  };

  template<class Expr, class A, class B, class C>
  class ternary_custom_variable : public shell_dynamic_variable< ternary_pool_expression<Expr, typename A::Var, typename B::Var, typename C::Var >,ternary_custom_variable<Expr,A,B,C> >
  {
  private:
    const A & Cond;
    const B & Left;
    const C & Right;
  public:
    ternary_custom_variable(const shell_dynamic_variable<typename A::Var,A> & pcond, const shell_dynamic_variable<typename B::Var,B> & pleft, const shell_dynamic_variable<typename C::Var,C> & pright) 
    : Cond(pcond), Left(pleft), Right(pright) {}
    ternary_custom_variable(const ternary_custom_variable & other) : Left(other.Left), Right(other.Right) {}
Kirill Terekhov's avatar
Kirill Terekhov committed
493
    ternary_custom_variable & operator =(ternary_custom_variable const & other) {Left = other.Left; Right = other.Right; return * this;}
Kirill Terekhov's avatar
Kirill Terekhov committed
494
495
496
497
498
499
500
501
    INMOST_DATA_REAL_TYPE Value(const Storage & e) const {return (*this)[e].GetValue();}
    multivar_expression Variable(const Storage & e) const
    {
      multivar_expression ret = (*this)[e];
      return ret;
    }
    ternary_pool_expression<Expr, typename A::Var, typename B::Var, typename C::Var > operator [](const Storage & e) const 
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
502
      ternary_pool<Expr,typename A::Var,typename B::Var, typename C::Var> pool(Cond[e],Left[e],Right[e]);
Kirill Terekhov's avatar
Kirill Terekhov committed
503
504
      return ternary_pool_expression<Expr, typename A::Var, typename B::Var, typename C::Var>(pool);
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
505
506
    void GetVariation(const Storage & e, Sparse::Row & r) const { (*this)[e].GetJacobian(1.0,r); }
    void GetVariation(const Storage & e, Sparse::RowMerger & r) const { (*this)[e].GetJacobian(1.0,r); }
Kirill Terekhov's avatar
Kirill Terekhov committed
507
    abstract_dynamic_variable * Copy() const {return static_cast<abstract_dynamic_variable *>(new ternary_custom_variable(*this));}
Kirill Terekhov's avatar
Kirill Terekhov committed
508
509
  };
  typedef abstract_dynamic_variable abstract_variable;
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
}

template<class A, class B, class C> __INLINE INMOST::ternary_custom_variable<INMOST::condition_expression<typename A::Var, typename B::Var, typename C::Var>,A,B,C> condition(INMOST::shell_dynamic_variable<typename A::Var, A> const & control, INMOST::shell_dynamic_variable<typename B::Var, B> const & if_ge_zero, INMOST::shell_dynamic_variable<typename C::Var, C> const & if_lt_zero) { return INMOST::ternary_custom_variable<INMOST::condition_expression<typename A::Var, typename B::Var, typename C::Var>,A,B,C>(control,if_ge_zero,if_lt_zero); }
template<class A>          __INLINE                                 INMOST::unary_custom_variable<INMOST::unary_minus_expression<typename A::Var>,A> operator-(INMOST::shell_dynamic_variable<typename A::Var, A> const & Arg) { return INMOST::unary_custom_variable<INMOST::unary_minus_expression<typename A::Var>,A>(Arg); }
template<class A>          __INLINE                                         INMOST::unary_custom_variable<INMOST::abs_expression<typename A::Var>,A>      fabs(INMOST::shell_dynamic_variable<typename A::Var, A> const & Arg) { return INMOST::unary_custom_variable<INMOST::abs_expression<typename A::Var>,A>(Arg); }
template<class A>          __INLINE                                         INMOST::unary_custom_variable<INMOST::exp_expression<typename A::Var>,A>       exp(INMOST::shell_dynamic_variable<typename A::Var, A> const & Arg) { return INMOST::unary_custom_variable<INMOST::exp_expression<typename A::Var>,A>(Arg); }
template<class A>          __INLINE                                         INMOST::unary_custom_variable<INMOST::log_expression<typename A::Var>,A>       log(INMOST::shell_dynamic_variable<typename A::Var, A> const & Arg) { return INMOST::unary_custom_variable<INMOST::log_expression<typename A::Var>,A>(Arg); }
template<class A>          __INLINE                                         INMOST::unary_custom_variable<INMOST::sin_expression<typename A::Var>,A>       sin(INMOST::shell_dynamic_variable<typename A::Var, A> const & Arg) { return INMOST::unary_custom_variable<INMOST::sin_expression<typename A::Var>,A>(Arg ); }
template<class A>          __INLINE                                         INMOST::unary_custom_variable<INMOST::cos_expression<typename A::Var>,A>       cos(INMOST::shell_dynamic_variable<typename A::Var, A> const & Arg) { return INMOST::unary_custom_variable<INMOST::cos_expression<typename A::Var>,A>(Arg); }
template<class A>          __INLINE                                        INMOST::unary_custom_variable<INMOST::sqrt_expression<typename A::Var>,A>      sqrt(INMOST::shell_dynamic_variable<typename A::Var, A> const & Arg) { return INMOST::unary_custom_variable<INMOST::sqrt_expression<typename A::Var>,A>(Arg); }
template<class A>          __INLINE              INMOST::unary_const_custom_variable<INMOST::variation_multiplication_expression<typename A::Var>,A> variation(INMOST::shell_dynamic_variable<typename A::Var, A> const & Arg, INMOST_DATA_REAL_TYPE Mult) {return INMOST::unary_const_custom_variable<INMOST::variation_multiplication_expression<typename A::Var>,A>(Arg,Mult);}
template<class A, class B> __INLINE                INMOST::binary_custom_variable<INMOST::addition_expression<typename A::Var,typename B::Var>,A, B> operator+(INMOST::shell_dynamic_variable<typename A::Var,A> const & Left, INMOST::shell_dynamic_variable<typename B::Var,B> const & Right) { return INMOST::binary_custom_variable<INMOST::addition_expression<typename A::Var,typename B::Var>,A, B> (Left, Right); }
template<class A, class B> __INLINE             INMOST::binary_custom_variable<INMOST::subtraction_expression<typename A::Var,typename B::Var>,A, B> operator-(INMOST::shell_dynamic_variable<typename A::Var,A> const & Left, INMOST::shell_dynamic_variable<typename B::Var,B> const & Right) { return INMOST::binary_custom_variable<INMOST::subtraction_expression<typename A::Var,typename B::Var>, A, B> (Left, Right); }
template<class A, class B> __INLINE          INMOST::binary_custom_variable<INMOST::multiplication_expression<typename A::Var,typename B::Var>,A, B> operator*(INMOST::shell_dynamic_variable<typename A::Var,A> const & Left, INMOST::shell_dynamic_variable<typename B::Var,B> const & Right) { return INMOST::binary_custom_variable<INMOST::multiplication_expression<typename A::Var,typename B::Var>, A, B> (Left, Right); }
template<class A, class B> __INLINE                INMOST::binary_custom_variable<INMOST::division_expression<typename A::Var,typename B::Var>,A, B> operator/(INMOST::shell_dynamic_variable<typename A::Var,A> const & Left, INMOST::shell_dynamic_variable<typename B::Var,B> const & Right) { return INMOST::binary_custom_variable<INMOST::division_expression<typename A::Var,typename B::Var>, A, B> (Left, Right); }
template<class A, class B> __INLINE                     INMOST::binary_custom_variable<INMOST::pow_expression<typename A::Var,typename B::Var>,A, B>       pow(INMOST::shell_dynamic_variable<typename A::Var,A> const & Left, INMOST::shell_dynamic_variable<typename B::Var,B> const & Right) { return INMOST::binary_custom_variable<INMOST::pow_expression<typename A::Var,typename B::Var>,A, B>(Left, Right); }
template<class B>          __INLINE                             INMOST::unary_const_custom_variable<INMOST::const_pow_expression<typename B::Var>,B>       pow(INMOST_DATA_REAL_TYPE Left, INMOST::shell_dynamic_variable<typename B::Var,B> const & Right) { return INMOST::unary_const_custom_variable<INMOST::const_pow_expression<typename B::Var>,B>(Left, Right); }
template<class A>          __INLINE                             INMOST::unary_const_custom_variable<INMOST::pow_const_expression<typename A::Var>,A>       pow(INMOST::shell_dynamic_variable<typename A::Var,A> const & Left, INMOST_DATA_REAL_TYPE Right) { return INMOST::unary_const_custom_variable<INMOST::pow_const_expression<typename A::Var>,A>(Left, Right); }
template<class B>          __INLINE                  INMOST::unary_const_custom_variable<INMOST::const_multiplication_expression<typename B::Var>,B> operator*(INMOST_DATA_REAL_TYPE Left, INMOST::shell_dynamic_variable<typename B::Var,B> const & Right) { return INMOST::unary_const_custom_variable<INMOST::const_multiplication_expression<typename B::Var>,B>(Right,Left); }
template<class A>          __INLINE                  INMOST::unary_const_custom_variable<INMOST::const_multiplication_expression<typename A::Var>,A> operator*(INMOST::shell_dynamic_variable<typename A::Var,A> const & Left, INMOST_DATA_REAL_TYPE Right) { return INMOST::unary_const_custom_variable<INMOST::const_multiplication_expression<typename A::Var>,A>(Left,Right); }
template<class B>          __INLINE                            INMOST::unary_const_custom_variable<INMOST::reciprocal_expression<typename B::Var>,B> operator/(INMOST_DATA_REAL_TYPE Left, INMOST::shell_dynamic_variable<typename B::Var,B> const & Right) { return INMOST::unary_const_custom_variable<INMOST::reciprocal_expression<typename B::Var>,B>(Right,Left); }
template<class A>          __INLINE                        INMOST::unary_const_custom_variable<INMOST::const_division_expression<typename A::Var>,A> operator/(INMOST::shell_dynamic_variable<typename A::Var,A> const & Left, INMOST_DATA_REAL_TYPE Right) { return INMOST::unary_const_custom_variable<INMOST::const_division_expression<typename A::Var>,A>(Left, Right); }
template<class B>          __INLINE                        INMOST::unary_const_custom_variable<INMOST::const_addition_expression<typename B::Var>,B> operator+(INMOST_DATA_REAL_TYPE Left, INMOST::shell_dynamic_variable<typename B::Var,B> const & Right) { return INMOST::unary_const_custom_variable<INMOST::const_addition_expression<typename B::Var>,B>(Right,Left); }
template<class A>          __INLINE                        INMOST::unary_const_custom_variable<INMOST::const_addition_expression<typename A::Var>,A> operator+(INMOST::shell_dynamic_variable<typename A::Var,A> const & Left, INMOST_DATA_REAL_TYPE Right) { return INMOST::unary_const_custom_variable<INMOST::const_addition_expression<typename A::Var>,A>(Left,Right); }
template<class B>          __INLINE                     INMOST::unary_const_custom_variable<INMOST::const_subtraction_expression<typename B::Var>,B> operator-(INMOST_DATA_REAL_TYPE Left, INMOST::shell_dynamic_variable<typename B::Var,B> const & Right) { return INMOST::unary_const_custom_variable<INMOST::const_subtraction_expression<typename B::Var>,B>(Right, Left); }
template<class A>          __INLINE                        INMOST::unary_const_custom_variable<INMOST::const_addition_expression<typename A::Var>,A> operator-(INMOST::shell_dynamic_variable<typename A::Var,A> const & Left, INMOST_DATA_REAL_TYPE Right) { return INMOST::unary_const_custom_variable<INMOST::const_addition_expression<typename A::Var>,A>(Left, -Right); }
Kirill Terekhov's avatar
Kirill Terekhov committed
536
537
template<class A>          __INLINE                                                                                      INMOST::stencil_variable<A>   stencil(INMOST::Automatizator & aut, INMOST_DATA_ENUM_TYPE stncl, INMOST::shell_dynamic_variable<typename A::Var,A> const & Arg, void * user_data = NULL) { return INMOST::stencil_variable<A>(aut,stncl,Arg,user_data); }
template<class A>          __INLINE                                                                                        INMOST::table_variable<A> get_table(INMOST::shell_dynamic_variable<typename A::Var,A> const & Arg, const INMOST::keyval_table & Table) {return INMOST::table_variable<A>(Table,Arg);}
538

Kirill Terekhov's avatar
Kirill Terekhov committed
539
  
540
541
  

Kirill Terekhov's avatar
Kirill Terekhov committed
542
543
544

#endif
#endif