inmost_expression.h 88.9 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
#ifndef INMOST_AUTODIFF_ETEXPR_H_INCLUDED
#define INMOST_AUTODIFF_ETEXPR_H_INCLUDED
#include "inmost_common.h"
#include "inmost_sparse.h"
#include <sstream> //for debug
#include <new>


//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. floor, ceil, atan, acos, asin, max, min functions
// 7. choice of directional derivatives at discontinuities for abs, pow, max, min (see ADOL-C)
Kirill Terekhov's avatar
Kirill Terekhov committed
17
18
// 8. replace stencil with foreach for provided iterators
// 9. enclose in namespace
Kirill Terekhov's avatar
Kirill Terekhov committed
19
20
21
22
//10. CheckCurrentAutomatizator -> CheckCurrentRowMerger
//10.0 structure/service to handle multiple RowMerger objects in openmp environment
//10.1 user should be able to provide RowMerger when Automatizator is not compiled
//10.2 Automatizator may provide internal structure for RowMerger
Kirill Terekhov's avatar
Kirill Terekhov committed
23

Kirill Terekhov's avatar
Kirill Terekhov committed
24
25
26
#ifdef _MSC_VER
#pragma warning(disable : 4503)
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
27
28
29
30

#if defined(USE_AUTODIFF)
namespace INMOST
{
Kirill Terekhov's avatar
Kirill Terekhov committed
31
	
Kirill Terekhov's avatar
Kirill Terekhov committed
32
33
34
35

	class basic_expression
	{
	public:
Kirill Terekhov's avatar
Kirill Terekhov committed
36
		basic_expression() {}//if( GetAutodiffPrint() ) std::cout << this << " Created" << std::endl;}
37
		basic_expression(const basic_expression & other) {(void)other;};//std::cout << this << " Created from " << &other << std::endl;}
38
		virtual INMOST_DATA_REAL_TYPE GetValue() const = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
39
		virtual void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::RowMerger & r) const = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
40
41
42
		virtual void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::Row & r) const = 0;
		virtual void GetHessian(INMOST_DATA_REAL_TYPE multJ, Sparse::Row & J, INMOST_DATA_REAL_TYPE multH, Sparse::HessianRow & H) const = 0;
		virtual ~basic_expression() {}//if( GetAutodiffPrint() ) std::cout << this << " Destroied" << std::endl;}
Kirill Terekhov's avatar
Kirill Terekhov committed
43
44
	};

Kirill Terekhov's avatar
Kirill Terekhov committed
45
46
47
48
49
50
	bool CheckCurrentAutomatizator();
	void FromBasicExpression(Sparse::Row & entries, const basic_expression & expr);
	void AddBasicExpression(Sparse::Row & entries, INMOST_DATA_REAL_TYPE multme, INMOST_DATA_REAL_TYPE multit, const basic_expression & expr);
	void FromGetJacobian(const basic_expression & expr, INMOST_DATA_REAL_TYPE mult, Sparse::Row & r);
	//bool GetAutodiffPrint();
	//void SetAutodiffPrint(bool set);
Kirill Terekhov's avatar
Kirill Terekhov committed
51

Kirill Terekhov's avatar
Kirill Terekhov committed
52
53
54
55
	template<class Derived>
	class shell_expression : virtual public basic_expression
	{
	public:
Kirill Terekhov's avatar
Kirill Terekhov committed
56
		shell_expression() {}// if( GetAutodiffPrint() ) std::cout << this << " Shell Created for " << dynamic_cast<basic_expression *>(this) << std::endl;}
Kirill Terekhov's avatar
Kirill Terekhov committed
57
58
		shell_expression(const shell_expression & other) {}//std::cout << this << " Shell Created from " << &other << std::endl;}
		__INLINE virtual INMOST_DATA_REAL_TYPE GetValue() const {return static_cast<const Derived *>(this)->GetValue(); }
Kirill Terekhov's avatar
Kirill Terekhov committed
59
60
61
62
63
64
65
		__INLINE virtual void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::RowMerger & r) const { return static_cast<const Derived *>(this)->GetJacobian(mult,r); }
		__INLINE virtual void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::Row & r) const { return static_cast<const Derived *>(this)->GetJacobian(mult,r); }
		__INLINE virtual void GetHessian(INMOST_DATA_REAL_TYPE multJ, Sparse::Row & J, INMOST_DATA_REAL_TYPE multH, Sparse::HessianRow & H) const {return static_cast<const Derived *>(this)->GetHessian(multJ,J,multH,H); }
		operator Derived & () {return *static_cast<Derived *>(this);}
		operator const Derived & () const {return *static_cast<const Derived *>(this);}
		~shell_expression() {}// if( GetAutodiffPrint() ) std::cout << this << " Shell Destroied for " << dynamic_cast<basic_expression *>(this) << std::endl;}
		//Derived * GetDerived() { return dynamic_cast<Derived *>(this); }
Kirill Terekhov's avatar
Kirill Terekhov committed
66
67
68
69
	};

  
 
Kirill Terekhov's avatar
Kirill Terekhov committed
70
71
72
73
74
75
76
	class const_expression : public shell_expression<const_expression>
	{
		INMOST_DATA_REAL_TYPE value;
	public:
		const_expression(const const_expression & other) :value(other.value) {}
		const_expression(INMOST_DATA_REAL_TYPE pvalue) : value(pvalue) {}
		__INLINE INMOST_DATA_REAL_TYPE GetValue() const { return value; }
77
78
79
		__INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::RowMerger & r) const {(void)mult; (void)r;}
		__INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::Row & r) const {(void)mult; (void)r;}
		__INLINE void GetHessian(INMOST_DATA_REAL_TYPE multJ, Sparse::Row & J, INMOST_DATA_REAL_TYPE multH, Sparse::HessianRow & H) const {(void)multJ; (void)J; (void)multH; (void)H;}
Kirill Terekhov's avatar
Kirill Terekhov committed
80
81
82
83
84
85
86
		__INLINE const_expression & operator =(const_expression const & other)
		{
			value = other.value;
			return *this;
		}
		operator INMOST_DATA_REAL_TYPE () {return value;}
	};
Kirill Terekhov's avatar
Kirill Terekhov committed
87
88
  

Kirill Terekhov's avatar
Kirill Terekhov committed
89
90
91
92
93
94
95
96
97
98
99
	class var_expression : public shell_expression<var_expression>
	{
		INMOST_DATA_REAL_TYPE value;
		INMOST_DATA_ENUM_TYPE index;
	public:
		var_expression(const var_expression & other) :value(other.value), index(other.index) {}
		var_expression(INMOST_DATA_REAL_TYPE pvalue, INMOST_DATA_ENUM_TYPE pindex) : value(pvalue), index(pindex) {}
		__INLINE void SetValue(INMOST_DATA_REAL_TYPE val) { value = val; }
		__INLINE INMOST_DATA_REAL_TYPE GetValue() const { return value; }
		__INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::RowMerger & r) const {if( index != ENUMUNDEF ) r[index] += mult;}
		__INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::Row & r) const {if( index != ENUMUNDEF ) r[index] += mult;}
100
		__INLINE void GetHessian(INMOST_DATA_REAL_TYPE multJ, Sparse::Row & J, INMOST_DATA_REAL_TYPE multH, Sparse::HessianRow & H) const {if( index != ENUMUNDEF ) J.Push(index,multJ);  (void)multH; (void)H;}
Kirill Terekhov's avatar
Kirill Terekhov committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
		__INLINE var_expression & operator =(var_expression const & other)
		{
			value = other.value;
			index = other.index;
			return *this;
		}
		__INLINE var_expression & operator =(INMOST_DATA_REAL_TYPE other)
		{
			value = other;
			index = ENUMUNDEF;
			return *this;
		}
		bool check_nans() const
		{
			return value != value;
		}
	};
Kirill Terekhov's avatar
Kirill Terekhov committed
118

Kirill Terekhov's avatar
Kirill Terekhov committed
119
	
Kirill Terekhov's avatar
Kirill Terekhov committed
120
121
	//forward declaration that helps define copy constructor and assignment operator.
	class hessian_multivar_expression;
Kirill Terekhov's avatar
Kirill Terekhov committed
122

Kirill Terekhov's avatar
Kirill Terekhov committed
123
124
125
126
#if defined(PACK_ARRAY)
#pragma pack(push,r1,4)
#endif

Kirill Terekhov's avatar
Kirill Terekhov committed
127

Kirill Terekhov's avatar
Kirill Terekhov committed
128
129
130
	/// A class that represents a variable with multiple
	/// first order variations.
	/// Short type name is variable.
Kirill Terekhov's avatar
Kirill Terekhov committed
131
132
	class multivar_expression : public shell_expression<multivar_expression>
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
133
134
		INMOST_DATA_REAL_TYPE value; //< Value of the variable.
		Sparse::Row entries; //< Sparse vector of variations.
Kirill Terekhov's avatar
Kirill Terekhov committed
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
	public:
		multivar_expression() :value(0) {}
		multivar_expression(INMOST_DATA_REAL_TYPE pvalue) : value(pvalue) {}
		multivar_expression(const multivar_expression & other) : value(other.value), entries(other.entries) {}
		multivar_expression(INMOST_DATA_REAL_TYPE pvalue, Sparse::Row & pentries)
			: value(pvalue), entries(pentries) {}
		multivar_expression(INMOST_DATA_REAL_TYPE pvalue, INMOST_DATA_ENUM_TYPE pindex, INMOST_DATA_REAL_TYPE pdmult = 1.0)
			: value(pvalue)
		{
			entries.Push(pindex,pdmult);
		}
		multivar_expression(const basic_expression & expr)
		{
			value = expr.GetValue();
			if( CheckCurrentAutomatizator() )
				FromBasicExpression(entries,expr); //Optimized version
			else expr.GetJacobian(1.0,entries);
		}
		__INLINE INMOST_DATA_REAL_TYPE GetValue() const { return value; }
		__INLINE void SetValue(INMOST_DATA_REAL_TYPE val) { value = val; }
		__INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::RowMerger & r) const 
		{
			for(Sparse::Row::const_iterator it = entries.Begin(); it != entries.End(); ++it)
				r[it->first] += it->second*mult;
		}
		__INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::Row & r) const 
		{
			if( CheckCurrentAutomatizator() )
				FromGetJacobian(*this,mult,r);
			else
			{
				for(Sparse::Row::const_iterator it = entries.Begin(); it != entries.End(); ++it)
					r[it->first] += it->second*mult;
			}
		}
		__INLINE void GetHessian(INMOST_DATA_REAL_TYPE multJ, Sparse::Row & J, INMOST_DATA_REAL_TYPE multH, Sparse::HessianRow & H) const
		{
			J = entries;
			if( !J.isSorted() ) std::sort(J.Begin(),J.End());
			for(Sparse::Row::iterator it = J.Begin(); it != J.End(); ++it) it->second *= multJ;
Kirill Terekhov's avatar
Kirill Terekhov committed
175
			H.Clear();
Kirill Terekhov's avatar
Kirill Terekhov committed
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
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
308
309
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
335
336
		}
		__INLINE multivar_expression & operator = (INMOST_DATA_REAL_TYPE pvalue)
		{
			value = pvalue;
			entries.Clear();
			return *this;
		}
		__INLINE multivar_expression & operator = (basic_expression const & expr)
		{
			value = expr.GetValue();
			if( CheckCurrentAutomatizator() )
				FromBasicExpression(entries,expr);
			else
			{
				Sparse::Row tmp;
				expr.GetJacobian(1.0,tmp);
				entries.Swap(tmp);
			}
			return *this;
		}
		__INLINE multivar_expression & operator = (multivar_expression const & other)
		{
			value = other.value;
			entries = other.entries;
			return *this;
		}
		__INLINE Sparse::Row & GetRow() {return entries;}
		__INLINE const Sparse::Row & GetRow() const {return entries;}
		__INLINE multivar_expression & operator +=(basic_expression const & expr)
		{
			value += expr.GetValue();
			if( CheckCurrentAutomatizator() )
				AddBasicExpression(entries,1.0,1.0,expr);
			else
			{ 
				Sparse::Row tmp(entries);
				expr.GetJacobian(1.0,tmp);
				entries.Swap(tmp);
			}
			return *this;
		}
		__INLINE multivar_expression & operator -=(basic_expression const & expr)
		{
			value -= expr.GetValue();
			if( CheckCurrentAutomatizator() )
				AddBasicExpression(entries,1.0,-1.0,expr);
			else
			{
				Sparse::Row tmp(entries);
				expr.GetJacobian(-1.0,tmp);
				entries.Swap(tmp);
			}
			return *this;
		}
		__INLINE multivar_expression & operator *=(basic_expression const & expr)
		{
			INMOST_DATA_REAL_TYPE lval = value, rval = expr.GetValue();
			if( CheckCurrentAutomatizator() )
				AddBasicExpression(entries,rval,lval,expr);
			else
			{
				Sparse::Row tmp(entries);
				for(Sparse::Row::iterator it = tmp.Begin(); it != tmp.End(); ++it) it->second *= rval;
				expr.GetJacobian(lval,tmp);
				entries.Swap(tmp);
			}
			value *= rval;
			return *this;
		}
		__INLINE multivar_expression & operator /=(basic_expression const & expr)
		{
			INMOST_DATA_REAL_TYPE rval = expr.GetValue();
			INMOST_DATA_REAL_TYPE reciprocial_rval = 1.0/rval;
			value *= reciprocial_rval;
			if( CheckCurrentAutomatizator() )
				AddBasicExpression(entries,reciprocial_rval,-value*reciprocial_rval,expr);
			else
			{
				Sparse::Row tmp(entries);
				for(Sparse::Row::iterator it = tmp.Begin(); it != tmp.End(); ++it) it->second *= reciprocial_rval;
				expr.GetJacobian(-value*reciprocial_rval,tmp); 
				entries.Swap(tmp);
			}
			return *this;
		}
		__INLINE multivar_expression & operator +=(INMOST_DATA_REAL_TYPE right)
		{
			value += right;
			return *this;
		}
		__INLINE multivar_expression & operator -=(INMOST_DATA_REAL_TYPE right)
		{
			value -= right;
			return *this;
		}
		__INLINE multivar_expression & operator *=(INMOST_DATA_REAL_TYPE right)
		{
			value *= right;
			for(Sparse::Row::iterator it = entries.Begin(); it != entries.End(); ++it) it->second *= right;
			return *this;
		}
		__INLINE multivar_expression & operator /=(INMOST_DATA_REAL_TYPE right)
		{
			value /= right;
			for(Sparse::Row::iterator it = entries.Begin(); it != entries.End(); ++it) it->second /= right;
			return *this;
		}
		bool check_nans() const
		{
			if( value != value ) return true;
			for(Sparse::Row::const_iterator it = entries.Begin(); it != entries.End(); ++it)
				if( it->second != it->second ) return true;
			return false;
		}
		/// Write variable into array of entries.
		/// Size of array can be determined via RecordSize.
		/// Used internally by Mesh::GetData.
		/// @param v Array of entries that will store data of the variable.
		/// @return Number of entries used.
		INMOST_DATA_ENUM_TYPE Record(Sparse::Row::entry * v) const
		{
			INMOST_DATA_ENUM_TYPE k = 0;
			v[k].first = entries.Size();
			v[k].second = value;
			k++;
			for(INMOST_DATA_ENUM_TYPE r = 0; r < entries.Size(); ++r)
			{
				v[k].first = entries.GetIndex(r);
				v[k].second = entries.GetValue(r);
				k++;
			}
			return k;
		}
		/// Number of entries required to record the variable.
		INMOST_DATA_ENUM_TYPE RecordSize() const
		{
			return 1 + entries.Size();
		}
		/// Retrive variable from array of entries.
		/// Size of array without retrival can be determined via RetriveSize.
		/// @param v Array of entries that will store data of the variable.
		/// @return Number of entries red.
		INMOST_DATA_ENUM_TYPE Retrive(const Sparse::Row::entry * v)
		{
			int k = 0;
			value = v[k].second;
			entries.Resize(v[k].first);
			k++;
			for(int r = 0; r < (int)entries.Size(); ++r)
			{
				entries.GetIndex(r) = v[k].first;
				entries.GetValue(r) = v[k].second;
				k++;
			}
			return k;
		}
		/// Number of entries used.
		static INMOST_DATA_ENUM_TYPE RetriveSize(const Sparse::Row::entry * v)
		{
			return 1 + v[0].first;
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
337
338
339
340
341
		void Print() const
		{
			std::cout << value << std::endl;
			entries.Print();
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
342
		friend class multivar_expression_reference;
Kirill Terekhov's avatar
Kirill Terekhov committed
343
	};
Kirill Terekhov's avatar
Kirill Terekhov committed
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
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
397
398
399
400
401
402
403
404
405
406
407
	
	/// A class that represents a variable with multiple
	/// first order and second order variations.
	/// Short type name is hessian_variable.
	class hessian_multivar_expression : public shell_expression<hessian_multivar_expression>
	{
		INMOST_DATA_REAL_TYPE value;
		Sparse::Row entries;
		Sparse::HessianRow hessian_entries;
	public:
		/// Sets zero value and no first or second order variations.
		hessian_multivar_expression() :value(0) {}
		/// Sets value and no first or second order variations.
		hessian_multivar_expression(INMOST_DATA_REAL_TYPE pvalue) : value(pvalue) {}
		/// Copy value and all first and second order variations from hessian_variable.
		hessian_multivar_expression(const hessian_multivar_expression & other) : value(other.value), entries(other.entries), hessian_entries(other.hessian_entries) {}
		/// Copy value and all first variations from variable, no second order variations.
		hessian_multivar_expression(const multivar_expression & other) : value(other.GetValue()), entries(other.GetRow()) {}
		/// Sets value and all first variations, no second order variations.
		hessian_multivar_expression(INMOST_DATA_REAL_TYPE pvalue, Sparse::Row & pentries)
		: value(pvalue), entries(pentries) {}
		/// Sets value and all first and second order variations.
		hessian_multivar_expression(INMOST_DATA_REAL_TYPE pvalue, Sparse::Row & pentries, Sparse::HessianRow & phentries)
		: value(pvalue), entries(pentries), hessian_entries(phentries) {}
		/// Sets value and it's variation index.
		hessian_multivar_expression(INMOST_DATA_REAL_TYPE pvalue, INMOST_DATA_ENUM_TYPE pindex, INMOST_DATA_REAL_TYPE pdmult = 1.0)
		: value(pvalue)
		{
			entries.Push(pindex,pdmult);
		}
		/// Evaluates argument expression to get the value and all variations of variable.
		hessian_multivar_expression(const basic_expression & expr)
		{
			value = expr.GetValue();
			expr.GetHessian(1.0,entries,1.0,hessian_entries);
		}
		__INLINE INMOST_DATA_REAL_TYPE GetValue() const { return value; }
		__INLINE void SetValue(INMOST_DATA_REAL_TYPE val) { value = val; }
		__INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::RowMerger & r) const
		{
			for(Sparse::Row::const_iterator it = entries.Begin(); it != entries.End(); ++it)
				r[it->first] += it->second*mult;
		}
		__INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::Row & r) const
		{
			if( CheckCurrentAutomatizator() )
				FromGetJacobian(*this,mult,r);
			else
			{
				for(Sparse::Row::const_iterator it = entries.Begin(); it != entries.End(); ++it)
					r[it->first] += it->second*mult;
			}
		}
		__INLINE void GetHessian(INMOST_DATA_REAL_TYPE multJ, Sparse::Row & J, INMOST_DATA_REAL_TYPE multH, Sparse::HessianRow & H) const
		{
			J = entries;
			if( !J.isSorted() ) std::sort(J.Begin(),J.End());
			for(Sparse::Row::iterator it = J.Begin(); it != J.End(); ++it) it->second *= multJ;
			H = hessian_entries;
			for(Sparse::HessianRow::iterator it = H.Begin(); it != H.End(); ++it) it->second *= multH;
		}
		__INLINE multivar_expression GetVariable(INMOST_DATA_ENUM_TYPE index)
		{
			multivar_expression ret(0);
Kirill Terekhov's avatar
Kirill Terekhov committed
408
			for(int k = 0; k < (int)entries.Size(); ++k)
Kirill Terekhov's avatar
Kirill Terekhov committed
409
410
411
412
				if( entries.GetIndex(k) == index )
				{
					ret.SetValue(entries.GetValue(k));
					Sparse::Row & r = ret.GetRow();
Kirill Terekhov's avatar
Kirill Terekhov committed
413
					for(int q = 0; q < (int)hessian_entries.Size(); ++q)
Kirill Terekhov's avatar
Kirill Terekhov committed
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
					{
						Sparse::HessianRow::index & i = hessian_entries.GetIndex(q);
						if( i.first == index )
							r.Push(i.second,hessian_entries.GetValue(q)*(i.first == i.second ? 1.0 : 0.5));
						else if( i.second == index )
							r.Push(i.first,hessian_entries.GetValue(q)*(i.first == i.second ? 1.0 : 0.5));
					}
					break;
				}
			return ret;
		}
		__INLINE hessian_multivar_expression & operator = (INMOST_DATA_REAL_TYPE pvalue)
		{
			value = pvalue;
			entries.Clear();
			hessian_entries.Clear();
			return *this;
		}
		__INLINE hessian_multivar_expression & operator = (basic_expression const & expr)
		{
			value = expr.GetValue();
			Sparse::Row tmp;
			Sparse::HessianRow htmp;
			expr.GetHessian(1.0,tmp,1.0,htmp);
			entries.Swap(tmp);
			hessian_entries.Swap(htmp);
			return *this;
		}
		__INLINE hessian_multivar_expression & operator = (multivar_expression const & other)
		{
			value = other.GetValue();
			entries = other.GetRow();
			hessian_entries.Clear();
			return *this;
		}
		__INLINE hessian_multivar_expression & operator = (hessian_multivar_expression const & other)
		{
			value = other.value;
			entries = other.entries;
			hessian_entries = other.hessian_entries;
			return *this;
		}
		__INLINE Sparse::Row & GetRow() {return entries;}
		__INLINE Sparse::HessianRow & GetHessianRow() {return hessian_entries;}
		__INLINE const Sparse::Row & GetRow() const {return entries;}
		__INLINE const Sparse::HessianRow & GetHessianRow() const {return hessian_entries;}
		__INLINE hessian_multivar_expression & operator +=(basic_expression const & expr)
		{
			value += expr.GetValue();
463
464
465
466
			Sparse::Row tmpr, tmp;
			Sparse::HessianRow htmpr, htmp;
			expr.GetHessian(1.0,tmpr,1.0,htmpr);
			Sparse::Row::MergeSortedRows(1.0,entries,1.0,tmpr,tmp);
Kirill Terekhov's avatar
Kirill Terekhov committed
467
			entries.Swap(tmp);
468
			Sparse::HessianRow::MergeSortedRows(1.0,hessian_entries,1.0,htmpr,htmp);
Kirill Terekhov's avatar
Kirill Terekhov committed
469
470
471
472
473
474
			hessian_entries.Swap(htmp);
			return *this;
		}
		__INLINE hessian_multivar_expression & operator -=(basic_expression const & expr)
		{
			value -= expr.GetValue();
475
476
477
478
			Sparse::Row tmpr, tmp;
			Sparse::HessianRow htmpr, htmp;
			expr.GetHessian(1.0,tmpr,1.0,htmpr);
			Sparse::Row::MergeSortedRows(1.0,entries,-1.0,tmpr,tmp);
Kirill Terekhov's avatar
Kirill Terekhov committed
479
			entries.Swap(tmp);
480
			Sparse::HessianRow::MergeSortedRows(1.0,hessian_entries,-1.0,htmpr,htmp);
Kirill Terekhov's avatar
Kirill Terekhov committed
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
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
536
537
538
539
540
541
542
543
544
545
546
547
			hessian_entries.Swap(htmp);
			return *this;
		}
		__INLINE hessian_multivar_expression & operator *=(basic_expression const & expr)
		{
			throw NotImplemented; //check code below is correct
			INMOST_DATA_REAL_TYPE lval = value, rval = expr.GetValue();
			Sparse::Row tmp(entries);
			Sparse::HessianRow htmp(hessian_entries);
			for(Sparse::Row::iterator it = tmp.Begin(); it != tmp.End(); ++it) it->second *= rval;
			for(Sparse::HessianRow::iterator it = htmp.Begin(); it != htmp.End(); ++it) it->second *= rval;
			expr.GetHessian(lval,tmp,lval,htmp);
			entries.Swap(tmp);
			hessian_entries.Swap(htmp);
			value *= rval;
			return *this;
		}
		__INLINE hessian_multivar_expression & operator /=(basic_expression const & expr)
		{
			throw NotImplemented; //check code below is correct
			INMOST_DATA_REAL_TYPE rval = expr.GetValue();
			INMOST_DATA_REAL_TYPE reciprocial_rval = 1.0/rval;
			value *= reciprocial_rval;
			Sparse::Row tmp(entries);
			Sparse::HessianRow htmp(hessian_entries);
			for(Sparse::Row::iterator it = tmp.Begin(); it != tmp.End(); ++it) it->second *= reciprocial_rval;
			for(Sparse::HessianRow::iterator it = htmp.Begin(); it != htmp.End(); ++it) it->second *= reciprocial_rval;
			expr.GetHessian(-value*reciprocial_rval,tmp,1.0,htmp);
			entries.Swap(tmp);
			hessian_entries.Swap(htmp);
			return *this;
		}
		__INLINE hessian_multivar_expression & operator +=(INMOST_DATA_REAL_TYPE right)
		{
			value += right;
			return *this;
		}
		__INLINE hessian_multivar_expression & operator -=(INMOST_DATA_REAL_TYPE right)
		{
			value -= right;
			return *this;
		}
		__INLINE hessian_multivar_expression & operator *=(INMOST_DATA_REAL_TYPE right)
		{
			value *= right;
			for(Sparse::Row::iterator it = entries.Begin(); it != entries.End(); ++it) it->second *= right;
			for(Sparse::HessianRow::iterator it = hessian_entries.Begin(); it != hessian_entries.End(); ++it) it->second *= right;
			return *this;
		}
		__INLINE hessian_multivar_expression & operator /=(INMOST_DATA_REAL_TYPE right)
		{
			value /= right;
			for(Sparse::Row::iterator it = entries.Begin(); it != entries.End(); ++it) it->second /= right;
			for(Sparse::HessianRow::iterator it = hessian_entries.Begin(); it != hessian_entries.End(); ++it) it->second /= right;
			return *this;
		}
		bool check_nans() const
		{
			if( value != value ) return true;
			for(Sparse::Row::const_iterator it = entries.Begin(); it != entries.End(); ++it)
				if( it->second != it->second ) return true;
			for(Sparse::HessianRow::const_iterator it = hessian_entries.Begin(); it != hessian_entries.End(); ++it)
				if( it->second != it->second ) return true;
			return false;
		}
	};
	
Kirill Terekhov's avatar
Kirill Terekhov committed
548
549
550
#if defined(PACK_ARRAY)
#pragma pack(pop,r1)
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
551
552
553
554
555
556
557
	
	
	
	class multivar_expression_reference : public shell_expression<multivar_expression_reference>
	{
		INMOST_DATA_REAL_TYPE & value;
		Sparse::Row * entries;
Kirill Terekhov's avatar
Kirill Terekhov committed
558
559
  public:
    /// Constructor, set links to the provided value and entries
Kirill Terekhov's avatar
Kirill Terekhov committed
560
561
    multivar_expression_reference(INMOST_DATA_REAL_TYPE & _value, Sparse::Row * _entries)
      : value(_value), entries(_entries) {}
Kirill Terekhov's avatar
Kirill Terekhov committed
562
563
564
    /// Copy constructor, sets links to the same reference of value and entries
    multivar_expression_reference(const multivar_expression_reference & other) 
      : value(other.value), entries(other.entries) {}
Kirill Terekhov's avatar
Kirill Terekhov committed
565
566
567
	  /// Copy constructor from multivar_expression, sets links to the same reference of value and entries
	  multivar_expression_reference(multivar_expression & other)
	  : value(other.value), entries(&other.entries) {}
Kirill Terekhov's avatar
Kirill Terekhov committed
568
569
570
571
572
    /// Retrive value
    __INLINE INMOST_DATA_REAL_TYPE GetValue() const { return value; }
    /// Set value without changing derivatives
    __INLINE void SetValue(INMOST_DATA_REAL_TYPE val) { value = val; }
    /// Retrive derivatives with multiplier into Sparse::RowMerger structure.
Kirill Terekhov's avatar
Kirill Terekhov committed
573
    __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::RowMerger & r) const 
Kirill Terekhov's avatar
Kirill Terekhov committed
574
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
575
      for(Sparse::Row::iterator it = entries->Begin(); it != entries->End(); ++it)
Kirill Terekhov's avatar
Kirill Terekhov committed
576
577
578
        r[it->first] += it->second*mult;
    }
    /// Retrive derivatives with multiplier into Sparse::Row structure.
Kirill Terekhov's avatar
Kirill Terekhov committed
579
    __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::Row & r) const 
Kirill Terekhov's avatar
Kirill Terekhov committed
580
581
    {
      if( CheckCurrentAutomatizator() )
Kirill Terekhov's avatar
Kirill Terekhov committed
582
        FromGetJacobian(*this,mult,r);
Kirill Terekhov's avatar
Kirill Terekhov committed
583
584
      else
      {
Kirill Terekhov's avatar
Kirill Terekhov committed
585
        for(Sparse::Row::iterator it = entries->Begin(); it != entries->End(); ++it)
Kirill Terekhov's avatar
Kirill Terekhov committed
586
587
588
          r[it->first] += it->second*mult;
      }
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
589
    __INLINE void GetHessian(INMOST_DATA_REAL_TYPE multJ, Sparse::Row & J,INMOST_DATA_REAL_TYPE multH, Sparse::HessianRow & H) const
Kirill Terekhov's avatar
Kirill Terekhov committed
590
591
592
    {
        J = *entries;
        if( !J.isSorted() ) std::sort(J.Begin(),J.End());
Kirill Terekhov's avatar
Kirill Terekhov committed
593
        for(Sparse::Row::iterator it = J.Begin(); it != J.End(); ++it) it->second *= multJ;
Kirill Terekhov's avatar
Kirill Terekhov committed
594
		H.Clear();
Kirill Terekhov's avatar
Kirill Terekhov committed
595
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
596
597
598
    __INLINE multivar_expression_reference & operator = (INMOST_DATA_REAL_TYPE pvalue)
    {
      value = pvalue;
Kirill Terekhov's avatar
Kirill Terekhov committed
599
      entries->Clear();
Kirill Terekhov's avatar
Kirill Terekhov committed
600
601
602
603
604
605
      return *this;
    }
    __INLINE multivar_expression_reference & operator = (basic_expression const & expr)
    {
      value = expr.GetValue();
      if( CheckCurrentAutomatizator() )
Kirill Terekhov's avatar
Kirill Terekhov committed
606
        FromBasicExpression(*entries,expr);
Kirill Terekhov's avatar
Kirill Terekhov committed
607
608
609
      else
      {
        Sparse::Row tmp;
Kirill Terekhov's avatar
Kirill Terekhov committed
610
        expr.GetJacobian(1.0,tmp);
Kirill Terekhov's avatar
Kirill Terekhov committed
611
        entries->Swap(tmp);
Kirill Terekhov's avatar
Kirill Terekhov committed
612
613
614
615
616
617
      }
      return *this;
    }
    __INLINE multivar_expression_reference & operator = (multivar_expression_reference const & other)
    {
      value = other.GetValue();
Kirill Terekhov's avatar
Kirill Terekhov committed
618
      *entries = other.GetRow();
Kirill Terekhov's avatar
Kirill Terekhov committed
619
620
621
622
623
      return *this;
    }
    __INLINE multivar_expression_reference & operator = (multivar_expression const & other)
    {
      value = other.GetValue();
Kirill Terekhov's avatar
Kirill Terekhov committed
624
      *entries = other.GetRow();
Kirill Terekhov's avatar
Kirill Terekhov committed
625
626
      return *this;
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
627
628
    __INLINE Sparse::Row & GetRow() {return *entries;}
    __INLINE const Sparse::Row & GetRow() const {return *entries;}
Kirill Terekhov's avatar
Kirill Terekhov committed
629
630
631
632
    __INLINE multivar_expression_reference & operator +=(basic_expression const & expr)
    {
      value += expr.GetValue();
      if( CheckCurrentAutomatizator() )
Kirill Terekhov's avatar
Kirill Terekhov committed
633
        AddBasicExpression(*entries,1.0,1.0,expr);
Kirill Terekhov's avatar
Kirill Terekhov committed
634
635
      else
      { 
Kirill Terekhov's avatar
Kirill Terekhov committed
636
        Sparse::Row tmp(*entries);
Kirill Terekhov's avatar
Kirill Terekhov committed
637
        expr.GetJacobian(1.0,tmp);
Kirill Terekhov's avatar
Kirill Terekhov committed
638
        entries->Swap(tmp);
Kirill Terekhov's avatar
Kirill Terekhov committed
639
640
641
642
643
644
645
      }
      return *this;
    }
    __INLINE multivar_expression_reference & operator -=(basic_expression const & expr)
    {
      value -= expr.GetValue();
      if( CheckCurrentAutomatizator() )
Kirill Terekhov's avatar
Kirill Terekhov committed
646
        AddBasicExpression(*entries,1.0,-1.0,expr);
Kirill Terekhov's avatar
Kirill Terekhov committed
647
648
      else
      {
Kirill Terekhov's avatar
Kirill Terekhov committed
649
        Sparse::Row tmp(*entries);
Kirill Terekhov's avatar
Kirill Terekhov committed
650
        expr.GetJacobian(-1.0,tmp);
Kirill Terekhov's avatar
Kirill Terekhov committed
651
        entries->Swap(tmp);
Kirill Terekhov's avatar
Kirill Terekhov committed
652
653
654
655
656
657
658
      }
      return *this;
    }
    __INLINE multivar_expression_reference & operator *=(basic_expression const & expr)
    {
      INMOST_DATA_REAL_TYPE lval = value, rval = expr.GetValue();
      if( CheckCurrentAutomatizator() )
Kirill Terekhov's avatar
Kirill Terekhov committed
659
        AddBasicExpression(*entries,rval,lval,expr);
Kirill Terekhov's avatar
Kirill Terekhov committed
660
661
      else
      {
Kirill Terekhov's avatar
Kirill Terekhov committed
662
        Sparse::Row tmp(*entries);
Kirill Terekhov's avatar
Kirill Terekhov committed
663
        for(Sparse::Row::iterator it = tmp.Begin(); it != tmp.End(); ++it) it->second *= rval;
Kirill Terekhov's avatar
Kirill Terekhov committed
664
        expr.GetJacobian(lval,tmp);
Kirill Terekhov's avatar
Kirill Terekhov committed
665
        entries->Swap(tmp);
Kirill Terekhov's avatar
Kirill Terekhov committed
666
667
668
669
670
671
672
673
674
675
      }
      value *= rval;
      return *this;
    }
    __INLINE multivar_expression_reference & operator /=(basic_expression const & expr)
    {
      INMOST_DATA_REAL_TYPE rval = expr.GetValue();
      INMOST_DATA_REAL_TYPE reciprocial_rval = 1.0/rval;
      value *= reciprocial_rval;
      if( CheckCurrentAutomatizator() )
Kirill Terekhov's avatar
Kirill Terekhov committed
676
        AddBasicExpression(*entries,reciprocial_rval,-value*reciprocial_rval,expr);
Kirill Terekhov's avatar
Kirill Terekhov committed
677
678
      else
      {
Kirill Terekhov's avatar
Kirill Terekhov committed
679
        Sparse::Row tmp(*entries);
Kirill Terekhov's avatar
Kirill Terekhov committed
680
        for(Sparse::Row::iterator it = tmp.Begin(); it != tmp.End(); ++it) it->second *= reciprocial_rval;
Kirill Terekhov's avatar
Kirill Terekhov committed
681
        expr.GetJacobian(-value*reciprocial_rval,tmp); 
Kirill Terekhov's avatar
Kirill Terekhov committed
682
        entries->Swap(tmp);
Kirill Terekhov's avatar
Kirill Terekhov committed
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
      }
      return *this;
    }
    __INLINE multivar_expression_reference & operator +=(INMOST_DATA_REAL_TYPE right)
    {
      value += right;
      return *this;
    }
    __INLINE multivar_expression_reference & operator -=(INMOST_DATA_REAL_TYPE right)
    {
      value -= right;
      return *this;
    }
    __INLINE multivar_expression_reference & operator *=(INMOST_DATA_REAL_TYPE right)
    {
      value *= right;
Kirill Terekhov's avatar
Kirill Terekhov committed
699
      for(Sparse::Row::iterator it = entries->Begin(); it != entries->End(); ++it) it->second *= right;
Kirill Terekhov's avatar
Kirill Terekhov committed
700
701
702
703
704
      return *this;
    }
    __INLINE multivar_expression_reference & operator /=(INMOST_DATA_REAL_TYPE right)
    {
      value /= right;
Kirill Terekhov's avatar
Kirill Terekhov committed
705
      for(Sparse::Row::iterator it = entries->Begin(); it != entries->End(); ++it) it->second /= right;
Kirill Terekhov's avatar
Kirill Terekhov committed
706
707
708
709
710
      return *this;
    }
    bool check_nans() const
    {
      if( value != value ) return true;
Kirill Terekhov's avatar
Kirill Terekhov committed
711
      for(Sparse::Row::iterator it = entries->Begin(); it != entries->End(); ++it)
Kirill Terekhov's avatar
Kirill Terekhov committed
712
713
714
715
        if( it->second != it->second ) return true;
      return false;
    }
  };
Kirill Terekhov's avatar
Kirill Terekhov committed
716
	
Kirill Terekhov's avatar
Kirill Terekhov committed
717

Kirill Terekhov's avatar
Kirill Terekhov committed
718
719
720
721
722
723
724
725
726
727
728
729
  template<class A>
  class const_multiplication_expression : public shell_expression<const_multiplication_expression<A> >
  {
    const A & arg;
    INMOST_DATA_REAL_TYPE value, dmult;
  public:
    const_multiplication_expression(const shell_expression<A> & parg,INMOST_DATA_REAL_TYPE pdmult) : arg(parg), dmult(pdmult)
    {
      value = arg.GetValue()*dmult;
    }
    const_multiplication_expression(const const_multiplication_expression & other) : arg(other.arg), value(other.value), dmult(other.dmult) {}
    __INLINE INMOST_DATA_REAL_TYPE GetValue() const { return value; }
Kirill Terekhov's avatar
Kirill Terekhov committed
730
    __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::RowMerger & r) const
Kirill Terekhov's avatar
Kirill Terekhov committed
731
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
732
      arg.GetJacobian(mult*dmult,r);
Kirill Terekhov's avatar
Kirill Terekhov committed
733
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
734
    __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::Row & r) const
Kirill Terekhov's avatar
Kirill Terekhov committed
735
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
736
      arg.GetJacobian(mult*dmult,r);
Kirill Terekhov's avatar
Kirill Terekhov committed
737
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
738
    __INLINE void GetHessian(INMOST_DATA_REAL_TYPE multJ, Sparse::Row & J, INMOST_DATA_REAL_TYPE multH, Sparse::HessianRow & H) const
Kirill Terekhov's avatar
Kirill Terekhov committed
739
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
740
        arg.GetHessian(multJ*dmult,J,multH*dmult,H);
Kirill Terekhov's avatar
Kirill Terekhov committed
741
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
742
743
744
745
746
747
748
749
750
751
752
753
754
755
  };

  template<class A>
  class variation_multiplication_expression : public shell_expression<variation_multiplication_expression<A> >
  {
    const A & arg;
    INMOST_DATA_REAL_TYPE value, dmult;
  public:
    variation_multiplication_expression(const shell_expression<A> & parg,INMOST_DATA_REAL_TYPE pdmult) : arg(parg), dmult(pdmult)
    {
      value = arg.GetValue();
    }
    variation_multiplication_expression(const variation_multiplication_expression & other) : arg(other.arg), value(other.value), dmult(other.dmult) {}
    __INLINE INMOST_DATA_REAL_TYPE GetValue() const { return value; }
Kirill Terekhov's avatar
Kirill Terekhov committed
756
    __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::RowMerger & r) const
Kirill Terekhov's avatar
Kirill Terekhov committed
757
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
758
      arg.GetJacobian(mult*dmult,r);
Kirill Terekhov's avatar
Kirill Terekhov committed
759
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
760
    __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::Row & r) const
Kirill Terekhov's avatar
Kirill Terekhov committed
761
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
762
      arg.GetJacobian(mult*dmult,r);
Kirill Terekhov's avatar
Kirill Terekhov committed
763
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
764
    __INLINE void GetHessian(INMOST_DATA_REAL_TYPE multJ, Sparse::Row & J, INMOST_DATA_REAL_TYPE multH, Sparse::HessianRow & H) const
Kirill Terekhov's avatar
Kirill Terekhov committed
765
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
766
      arg.GetHessian(multJ*dmult,J,multH*dmult,H);
Kirill Terekhov's avatar
Kirill Terekhov committed
767
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
  };


  template<class A>
  class const_division_expression : public shell_expression<const_division_expression<A> >
  {
    const A & arg;
    INMOST_DATA_REAL_TYPE value, dmult;
  public:
    const_division_expression(const shell_expression<A> & parg,INMOST_DATA_REAL_TYPE pdmult) : arg(parg), dmult(pdmult)
    {
      dmult = 1.0/dmult;
      value = arg.GetValue()*dmult;
    }
    const_division_expression(const const_division_expression & other) : arg(other.arg), value(other.value), dmult(other.dmult) {}
    __INLINE INMOST_DATA_REAL_TYPE GetValue() const { return value; }
Kirill Terekhov's avatar
Kirill Terekhov committed
784
    __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::RowMerger & r) const
Kirill Terekhov's avatar
Kirill Terekhov committed
785
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
786
      arg.GetJacobian(mult*dmult,r);
Kirill Terekhov's avatar
Kirill Terekhov committed
787
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
788
    __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::Row & r) const
Kirill Terekhov's avatar
Kirill Terekhov committed
789
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
790
      arg.GetJacobian(mult*dmult,r);
Kirill Terekhov's avatar
Kirill Terekhov committed
791
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
792
    __INLINE void GetHessian(INMOST_DATA_REAL_TYPE multJ, Sparse::Row & J, INMOST_DATA_REAL_TYPE multH, Sparse::HessianRow & H) const
Kirill Terekhov's avatar
Kirill Terekhov committed
793
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
794
        arg.GetHessian(multJ*dmult,J,multH*dmult,H);
Kirill Terekhov's avatar
Kirill Terekhov committed
795
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
796
797
798
799
800
801
802
803
804
805
806
807
808
809
  };
  
  template<class A>
  class const_addition_expression : public shell_expression<const_addition_expression<A> >
  {
    const A & arg;
    INMOST_DATA_REAL_TYPE value;
  public:
    const_addition_expression(const shell_expression<A> & parg,INMOST_DATA_REAL_TYPE padd) : arg(parg)
    {
      value = arg.GetValue()+padd;
    }
    const_addition_expression(const const_addition_expression & other) : arg(other.arg), value(other.value) {}
    __INLINE INMOST_DATA_REAL_TYPE GetValue() const { return value; }
Kirill Terekhov's avatar
Kirill Terekhov committed
810
    __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::RowMerger & r) const
Kirill Terekhov's avatar
Kirill Terekhov committed
811
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
812
      arg.GetJacobian(mult,r);
Kirill Terekhov's avatar
Kirill Terekhov committed
813
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
814
    __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::Row & r) const
Kirill Terekhov's avatar
Kirill Terekhov committed
815
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
816
      arg.GetJacobian(mult,r);
Kirill Terekhov's avatar
Kirill Terekhov committed
817
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
818
    __INLINE void GetHessian(INMOST_DATA_REAL_TYPE multJ, Sparse::Row & J, INMOST_DATA_REAL_TYPE multH, Sparse::HessianRow & H) const
Kirill Terekhov's avatar
Kirill Terekhov committed
819
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
820
        arg.GetHessian(multJ,J,multH,H);
Kirill Terekhov's avatar
Kirill Terekhov committed
821
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
822
823
824
825
826
827
828
829
830
831
832
833
834
835
  };

  template<class A>
  class const_subtraction_expression : public shell_expression<const_subtraction_expression<A> >
  {
    const A & arg;
    INMOST_DATA_REAL_TYPE value;
  public:
    const_subtraction_expression(const shell_expression<A> & parg,INMOST_DATA_REAL_TYPE pleft) : arg(parg)
    {
      value = pleft-arg.GetValue();
    }
    const_subtraction_expression(const const_subtraction_expression & other) : arg(other.arg), value(other.value) {}
    __INLINE INMOST_DATA_REAL_TYPE GetValue() const { return value; }
Kirill Terekhov's avatar
Kirill Terekhov committed
836
    __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::RowMerger & r) const
Kirill Terekhov's avatar
Kirill Terekhov committed
837
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
838
      arg.GetJacobian(-mult,r);
Kirill Terekhov's avatar
Kirill Terekhov committed
839
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
840
    __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::Row & r) const
Kirill Terekhov's avatar
Kirill Terekhov committed
841
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
842
      arg.GetJacobian(-mult,r);
Kirill Terekhov's avatar
Kirill Terekhov committed
843
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
844
    __INLINE void GetHessian(INMOST_DATA_REAL_TYPE multJ, Sparse::Row & J, INMOST_DATA_REAL_TYPE multH, Sparse::HessianRow & H) const
Kirill Terekhov's avatar
Kirill Terekhov committed
845
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
846
        arg.GetHessian(-multJ,J,-multH,H);
Kirill Terekhov's avatar
Kirill Terekhov committed
847
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
848
849
  };

Kirill Terekhov's avatar
Kirill Terekhov committed
850
851
  /// (c/x)' = -c dx / (x*x)
  /// (c/x)'' = 2 c dx dx / (x*x*x)
Kirill Terekhov's avatar
Kirill Terekhov committed
852
853
854
855
  template<class A>
  class reciprocal_expression : public shell_expression<reciprocal_expression<A> >
  {
    const A & arg;
Kirill Terekhov's avatar
Kirill Terekhov committed
856
    INMOST_DATA_REAL_TYPE value, reciprocial_val;
Kirill Terekhov's avatar
Kirill Terekhov committed
857
  public:
Kirill Terekhov's avatar
Kirill Terekhov committed
858
    reciprocal_expression(const shell_expression<A> & parg,INMOST_DATA_REAL_TYPE coef) : arg(parg)
Kirill Terekhov's avatar
Kirill Terekhov committed
859
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
860
861
      reciprocial_val = 1.0/arg.GetValue();
      value = coef*reciprocial_val;
Kirill Terekhov's avatar
Kirill Terekhov committed
862
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
863
864
    reciprocal_expression(const reciprocal_expression & other) 
      : arg(other.arg), value(other.value), reciprocial_val(other.reciprocial_val) {}
Kirill Terekhov's avatar
Kirill Terekhov committed
865
    __INLINE INMOST_DATA_REAL_TYPE GetValue() const { return value; }
Kirill Terekhov's avatar
Kirill Terekhov committed
866
    __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::RowMerger & r) const
Kirill Terekhov's avatar
Kirill Terekhov committed
867
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
868
      arg.GetJacobian(-mult*value*reciprocial_val,r);
Kirill Terekhov's avatar
Kirill Terekhov committed
869
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
870
    __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::Row & r) const
Kirill Terekhov's avatar
Kirill Terekhov committed
871
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
872
      arg.GetJacobian(-mult*value*reciprocial_val,r);
Kirill Terekhov's avatar
Kirill Terekhov committed
873
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
874
    __INLINE void GetHessian(INMOST_DATA_REAL_TYPE multJ, Sparse::Row & J, INMOST_DATA_REAL_TYPE multH, Sparse::HessianRow & H) const
Kirill Terekhov's avatar
Kirill Terekhov committed
875
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
876
877
878
879
880
881
        Sparse::HessianRow ArgH;
        double coefJ = -multJ*value*reciprocial_val;
        double signJ = coefJ < 0 ? -1 : 1;
        arg.GetHessian(signJ,J,-2*multH*value*reciprocial_val,ArgH);
        Sparse::HessianRow::MergeJacobianHessian(2*value*reciprocial_val*reciprocial_val*signJ,J,J,1.0,ArgH,H);
        for(INMOST_DATA_ENUM_TYPE k = 0; k < J.Size(); ++k) J.GetValue(k) *= coefJ*signJ;
Kirill Terekhov's avatar
Kirill Terekhov committed
882
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
883
884
885
  };

  template<class A>
Kirill Terekhov's avatar
Kirill Terekhov committed
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
  class unary_minus_expression : public shell_expression<unary_minus_expression<A> >
  {
      const A & arg;
      INMOST_DATA_REAL_TYPE value;
  public:
      unary_minus_expression(const shell_expression<A> & parg) : arg(parg) {value = -arg.GetValue();}
      unary_minus_expression(const unary_minus_expression & b) : arg(b.arg) {}
      __INLINE INMOST_DATA_REAL_TYPE GetValue() const {return value;}
      __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::RowMerger & r) const 
      {
          arg.GetJacobian(-mult,r);
      }
      __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::Row & r) const 
      {
          arg.GetJacobian(-mult,r);
      }
Kirill Terekhov's avatar
Kirill Terekhov committed
902
      __INLINE void GetHessian(INMOST_DATA_REAL_TYPE multJ, Sparse::Row & J, INMOST_DATA_REAL_TYPE multH, Sparse::HessianRow & H) const
Kirill Terekhov's avatar
Kirill Terekhov committed
903
      {
Kirill Terekhov's avatar
Kirill Terekhov committed
904
          arg.GetHessian(-multJ,J,-multH,H);
Kirill Terekhov's avatar
Kirill Terekhov committed
905
906
      }
  };
Kirill Terekhov's avatar
Kirill Terekhov committed
907

Kirill Terekhov's avatar
Kirill Terekhov committed
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
  template<class A>
  class unary_plus_expression : public shell_expression<unary_plus_expression<A> >
  {
      const A & arg;
      INMOST_DATA_REAL_TYPE value;
  public:
      unary_plus_expression(const shell_expression<A> & parg) : arg(parg) {value = arg.GetValue();}
      unary_plus_expression(const unary_plus_expression & b) : arg(b.arg) {}
      __INLINE INMOST_DATA_REAL_TYPE GetValue() const {return value;}
      __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::RowMerger & r) const 
      {
          arg.GetJacobian(mult,r);
      }
      __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::Row & r) const 
      {
          arg.GetJacobian(mult,r);
      }
      __INLINE void GetHessian(INMOST_DATA_REAL_TYPE multJ, Sparse::Row & J, INMOST_DATA_REAL_TYPE multH, Sparse::HessianRow & H) const
      {
          arg.GetHessian(multJ,J,multH,H);
      }
  };

Kirill Terekhov's avatar
Kirill Terekhov committed
931
932
  template<class A>
  class abs_expression : public shell_expression<abs_expression<A> >
Kirill Terekhov's avatar
Kirill Terekhov committed
933
934
935
936
937
938
939
940
941
942
943
944
945
946
  {
      const A & arg;
      INMOST_DATA_REAL_TYPE value, dmult;
  public:
      abs_expression(const shell_expression<A> & parg) : arg(parg) 
      {
          value = arg.GetValue();
          dmult = value < 0.0 ? -1.0 : 1.0;
          value = ::fabs(value);
      }
      abs_expression(const abs_expression & b) : arg(b.arg), value(b.value), dmult(b.dmult) {}
      __INLINE INMOST_DATA_REAL_TYPE GetValue() const {return value;}
      __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::RowMerger & r) const 
      {
Kirill Terekhov's avatar
Kirill Terekhov committed
947
          arg.GetJacobian( (value == 0 ? (mult < 0.0 ? -1 : 1) : 1) * mult * dmult, r);
Kirill Terekhov's avatar
Kirill Terekhov committed
948
949
950
      }
      __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::Row & r) const 
      {
Kirill Terekhov's avatar
Kirill Terekhov committed
951
          arg.GetJacobian( (value == 0 ? (mult < 0.0 ? -1 : 1) : 1) * mult * dmult, r);
Kirill Terekhov's avatar
Kirill Terekhov committed
952
      }
Kirill Terekhov's avatar
Kirill Terekhov committed
953
      __INLINE void GetHessian(INMOST_DATA_REAL_TYPE multJ,Sparse::Row & J, INMOST_DATA_REAL_TYPE multH, Sparse::HessianRow & H) const
Kirill Terekhov's avatar
Kirill Terekhov committed
954
      {
Kirill Terekhov's avatar
Kirill Terekhov committed
955
956
957
          double a = (value == 0 ? (multJ < 0.0 ? -1 : 1) : 1);
          double b = (value == 0 ? (multH < 0.0 ? -1 : 1) : 1);
          arg.GetHessian( a * multJ * dmult,J, b*multH * dmult,H);
Kirill Terekhov's avatar
Kirill Terekhov committed
958
959
      }
  };
Kirill Terekhov's avatar
Kirill Terekhov committed
960

Kirill Terekhov's avatar
Kirill Terekhov committed
961
  // ex
Kirill Terekhov's avatar
Kirill Terekhov committed
962
  template<class A>
Kirill Terekhov's avatar
Kirill Terekhov committed
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
  class exp_expression : public shell_expression<exp_expression<A> >
  {
      const A & arg;
      INMOST_DATA_REAL_TYPE value;
  public:
      exp_expression(const shell_expression<A> & parg) : arg(parg) 
      {
          value = arg.GetValue();
          value = ::exp(value);
      }
      exp_expression(const exp_expression & b) : arg(b.arg), value(b.value) {}
      __INLINE INMOST_DATA_REAL_TYPE GetValue() const { return value; };
      __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::RowMerger & r) const 
      {
          arg.GetJacobian( mult * value, r);
      }
      __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::Row & r) const 
      {
          arg.GetJacobian( mult * value, r);
      }
Kirill Terekhov's avatar
Kirill Terekhov committed
983
      __INLINE void GetHessian(INMOST_DATA_REAL_TYPE multJ, Sparse::Row & J, INMOST_DATA_REAL_TYPE multH, Sparse::HessianRow & H) const
Kirill Terekhov's avatar
Kirill Terekhov committed
984
      {
Kirill Terekhov's avatar
Kirill Terekhov committed
985
986
987
988
989
990
          Sparse::HessianRow ArgH;
          double coefJ = multJ*value;
          double signJ = coefJ < 0.0 ? -1 : 1;
          arg.GetHessian(signJ, J, multH*value, ArgH); //check
          Sparse::HessianRow::MergeJacobianHessian(coefJ*signJ,J,J,1.0,ArgH,H);
          for(INMOST_DATA_ENUM_TYPE k = 0; k < J.Size(); ++k) J.GetValue(k) *= coefJ*signJ;
Kirill Terekhov's avatar
Kirill Terekhov committed
991
      }
Kirill Terekhov's avatar
Kirill Terekhov committed
992
993
994
  };

  template<class A>
Kirill Terekhov's avatar
Kirill Terekhov committed
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
  class log_expression : public shell_expression<log_expression<A> >
  {
      const A & arg;
      INMOST_DATA_REAL_TYPE value, dmult;
  public:
      log_expression(const shell_expression<A> & parg) : arg(parg) 
      {
          value = arg.GetValue();
          dmult = 1.0/value;
          value = ::log(value);
      }
      log_expression(const log_expression & b) : arg(b.arg), value(b.value), dmult(b.dmult) {}
      __INLINE INMOST_DATA_REAL_TYPE GetValue() const { return value; };
      __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::RowMerger & r) const 
      {
          arg.GetJacobian(mult*dmult,r);
      }
      __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::Row & r) const 
      {
          arg.GetJacobian(mult*dmult,r);
      }
Kirill Terekhov's avatar
Kirill Terekhov committed
1016
      __INLINE void GetHessian(INMOST_DATA_REAL_TYPE multJ, Sparse::Row & J, INMOST_DATA_REAL_TYPE multH, Sparse::HessianRow & H) const
Kirill Terekhov's avatar
Kirill Terekhov committed
1017
      {
Kirill Terekhov's avatar
Kirill Terekhov committed
1018
1019
1020
1021
1022
1023
          Sparse::HessianRow ArgH;
          double coefJ = multJ*dmult;
          double signJ = coefJ < 0.0 ? -1 : 1;
          arg.GetHessian(signJ, J, 2*multH*dmult, ArgH); //check
          Sparse::HessianRow::MergeJacobianHessian(-coefJ*signJ*dmult,J,J,1.0,ArgH,H);
          for(INMOST_DATA_ENUM_TYPE k = 0; k < J.Size(); ++k) J.GetValue(k) *= coefJ*signJ;
Kirill Terekhov's avatar
Kirill Terekhov committed
1024
1025
      }
  };
Kirill Terekhov's avatar
Kirill Terekhov committed
1026
1027


Kirill Terekhov's avatar
Kirill Terekhov committed
1028
	template<class A>
Kirill Terekhov's avatar
Kirill Terekhov committed
1029
1030
1031
1032
1033
1034
1035
	class sin_expression : public shell_expression<sin_expression<A> >
	{
    const A & arg;
    INMOST_DATA_REAL_TYPE value, dmult;
	public:
    sin_expression(const shell_expression<A> & parg) : arg(parg) 
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
1036
1037
1038
		value = arg.GetValue();
		dmult = ::cos(value);
		value = ::sin(value);
Kirill Terekhov's avatar
Kirill Terekhov committed
1039
1040
1041
    }
    sin_expression(const sin_expression & b) : arg(b.arg), value(b.value), dmult(b.dmult) {}
		__INLINE INMOST_DATA_REAL_TYPE GetValue() const { return value; };
Kirill Terekhov's avatar
Kirill Terekhov committed
1042
		__INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::RowMerger & r) const 
Kirill Terekhov's avatar
Kirill Terekhov committed
1043
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
1044
      arg.GetJacobian(mult*dmult,r);
Kirill Terekhov's avatar
Kirill Terekhov committed
1045
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
1046
    __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::Row & r) const 
Kirill Terekhov's avatar
Kirill Terekhov committed
1047
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
1048
      arg.GetJacobian(mult*dmult,r);
Kirill Terekhov's avatar
Kirill Terekhov committed
1049
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
1050
    __INLINE void GetHessian(INMOST_DATA_REAL_TYPE multJ, Sparse::Row & J, INMOST_DATA_REAL_TYPE multH, Sparse::HessianRow & H) const
Kirill Terekhov's avatar
Kirill Terekhov committed
1051
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
1052
		Sparse::HessianRow htmp;
1053
        arg.GetHessian(1,J,1,htmp);
1054
1055
1056
1057
1058
		assert(J.isSorted());
		assert(htmp.isSorted());
		Sparse::HessianRow::MergeJacobianHessian(-value*multH,J,J,dmult*multH,htmp,H);
		for(Sparse::Row::iterator it = J.Begin(); it != J.End(); ++it) it->second*=dmult*multJ;
		assert(H.isSorted());
Kirill Terekhov's avatar
Kirill Terekhov committed
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
    }
	};

  template<class A>
	class cos_expression : public shell_expression<cos_expression<A> >
	{
    const A & arg;
    INMOST_DATA_REAL_TYPE value, dmult;
	public:
    cos_expression(const shell_expression<A> & parg) : arg(parg) 
    {
      value = arg.GetValue();
      dmult = -(::sin(value));
      value = ::cos(value);
    }
    cos_expression(const cos_expression & b) : arg(b.arg), value(b.value), dmult(b.dmult) {}
		__INLINE INMOST_DATA_REAL_TYPE GetValue() const { return value; };
Kirill Terekhov's avatar
Kirill Terekhov committed
1076
		__INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::RowMerger & r) const 
Kirill Terekhov's avatar
Kirill Terekhov committed
1077
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
1078
      arg.GetJacobian(mult*dmult,r);
Kirill Terekhov's avatar
Kirill Terekhov committed
1079
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
1080
    __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::Row & r) const 
Kirill Terekhov's avatar
Kirill Terekhov committed
1081
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
1082
      arg.GetJacobian(mult*dmult,r);
Kirill Terekhov's avatar
Kirill Terekhov committed
1083
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
1084
    __INLINE void GetHessian(INMOST_DATA_REAL_TYPE multJ, Sparse::Row & J, INMOST_DATA_REAL_TYPE multH, Sparse::HessianRow & H) const
Kirill Terekhov's avatar
Kirill Terekhov committed
1085
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
1086
1087
        //arg.GetHessian(multJ*dmult,J,-multH*value,H);
		Sparse::HessianRow htmp;
1088
1089
1090
        arg.GetHessian(1,J,1,htmp);
		Sparse::HessianRow::MergeJacobianHessian(-value*multH,J,J,dmult*multH,htmp,H);
		for(Sparse::Row::iterator it = J.Begin(); it != J.End(); ++it) it->second*=dmult*multJ;
Kirill Terekhov's avatar
Kirill Terekhov committed
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
    }
	};

  template<class A>
	class sqrt_expression : public shell_expression<sqrt_expression<A> >
	{
    const A & arg;
    INMOST_DATA_REAL_TYPE value;
	public:
    sqrt_expression(const shell_expression<A> & parg) : arg(parg) 
    {
      value = ::sqrt(arg.GetValue());
    }
    sqrt_expression(const sqrt_expression & b) : arg(b.arg), value(b.value) {}
		__INLINE INMOST_DATA_REAL_TYPE GetValue() const { return value; };
Kirill Terekhov's avatar
Kirill Terekhov committed
1106
		__INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::RowMerger & r) const 
Kirill Terekhov's avatar
Kirill Terekhov committed
1107
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
1108
      arg.GetJacobian(0.5*mult/value,r);
Kirill Terekhov's avatar
Kirill Terekhov committed
1109
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
1110
    __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::Row & r) const 
Kirill Terekhov's avatar
Kirill Terekhov committed
1111
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
1112
      arg.GetJacobian(0.5*mult/value,r);
Kirill Terekhov's avatar
Kirill Terekhov committed
1113
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
1114
    __INLINE void GetHessian(INMOST_DATA_REAL_TYPE multJ, Sparse::Row & J, INMOST_DATA_REAL_TYPE multH, Sparse::HessianRow & H) const
Kirill Terekhov's avatar
Kirill Terekhov committed
1115
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
1116
1117
1118
		//general formula:
		// (F(G))'' = F'(G) G'' + F''(G) G'.G'
		Sparse::HessianRow htmp;
1119
1120
1121
		arg.GetHessian(1,J,1,htmp);
		Sparse::HessianRow::MergeJacobianHessian(-0.25/::pow(value,3.0)*multH,J,J,0.5/value*multH,htmp,H);
		for(Sparse::Row::iterator it = J.Begin(); it != J.End(); ++it) it->second *= 0.5/value*multJ;
Kirill Terekhov's avatar
Kirill Terekhov committed
1122
        //arg.GetHessian(0.5*multJ/value,J,-0.25*multH/::pow(value,3),H);
Kirill Terekhov's avatar
Kirill Terekhov committed
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
    }
	};


  template<class A>
	class soft_abs_expression : public shell_expression<soft_abs_expression<A> >
	{
    const A & arg;
    INMOST_DATA_REAL_TYPE value, dmult;
	public:
    soft_abs_expression(const shell_expression<A> & parg, INMOST_DATA_REAL_TYPE tol) : arg(parg) 
    {
      INMOST_DATA_REAL_TYPE lval = arg.GetValue();
      value = ::sqrt(lval*lval+tol*tol);
      dmult = lval/value;
    }
    soft_abs_expression(const soft_abs_expression & b) : arg(b.arg), value(b.value), dmult(b.dmult) {}
		__INLINE INMOST_DATA_REAL_TYPE GetValue() const { return value; };
Kirill Terekhov's avatar
Kirill Terekhov committed
1141
		__INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::RowMerger & r) const 
Kirill Terekhov's avatar
Kirill Terekhov committed
1142
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
1143
      arg.GetJacobian(mult*dmult,r);
Kirill Terekhov's avatar
Kirill Terekhov committed
1144
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
1145
    __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::Row & r) const 
Kirill Terekhov's avatar
Kirill Terekhov committed
1146
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
1147
      arg.GetJacobian(mult*dmult,r);
Kirill Terekhov's avatar
Kirill Terekhov committed
1148
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
1149
    __INLINE void GetHessian(INMOST_DATA_REAL_TYPE multJ, Sparse::Row & J, INMOST_DATA_REAL_TYPE multH, Sparse::HessianRow & H) const
Kirill Terekhov's avatar
Kirill Terekhov committed
1150
1151
    {
        throw NotImplemented;
Kirill Terekhov's avatar
Kirill Terekhov committed
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
    }
	};

  template<class A>
	class soft_sign_expression : public shell_expression<soft_sign_expression<A> >
	{
    const A & arg;
    INMOST_DATA_REAL_TYPE value, dmult;
	public:
    soft_sign_expression(const shell_expression<A> & parg, INMOST_DATA_REAL_TYPE tol) : arg(parg) 
    {
      INMOST_DATA_REAL_TYPE lval = arg.GetValue(), lval2 = lval*lval;
      INMOST_DATA_REAL_TYPE div = lval2+tol*tol;
      INMOST_DATA_REAL_TYPE sdiv = ::sqrt(div);
      value = lval/sdiv;
      dmult = (1.0 - lval2/div)/sdiv;
    }
    soft_sign_expression(const soft_sign_expression & b) : arg(b.arg), value(b.value), dmult(b.dmult) {}
		__INLINE INMOST_DATA_REAL_TYPE GetValue() const { return value; };
Kirill Terekhov's avatar
Kirill Terekhov committed
1171
		__INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::RowMerger & r) const 
Kirill Terekhov's avatar
Kirill Terekhov committed
1172
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
1173
      arg.GetJacobian(mult*dmult,r);
Kirill Terekhov's avatar
Kirill Terekhov committed
1174
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
1175
    __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::Row & r) const 
Kirill Terekhov's avatar
Kirill Terekhov committed
1176
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
1177
      arg.GetJacobian(mult*dmult,r);
Kirill Terekhov's avatar
Kirill Terekhov committed
1178
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
1179
    __INLINE void GetHessian(INMOST_DATA_REAL_TYPE multJ, Sparse::Row & J, INMOST_DATA_REAL_TYPE multH, Sparse::HessianRow & H) const
Kirill Terekhov's avatar
Kirill Terekhov committed
1180
1181
    {
        throw NotImplemented;
Kirill Terekhov's avatar
Kirill Terekhov committed
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
    }
	};

  template<class A, class B>
  class soft_max_expression : public shell_expression<soft_max_expression<A,B> >
  {
    const A & left;
    const B & right;
    INMOST_DATA_REAL_TYPE value, ldmult, rdmult;
  public:
    soft_max_expression(const shell_expression<A> & pleft, const shell_expression<B> & pright, INMOST_DATA_REAL_TYPE tol) : left(pleft), right(pright)
    {
      INMOST_DATA_REAL_TYPE lval = left.GetValue(), rval = right.GetValue();
      INMOST_DATA_REAL_TYPE diff = lval-rval, root = ::sqrt(diff*diff+tol*tol);
      value = 0.5*(lval+rval + root);
      ldmult = 0.5*(1 + diff/root);
      rdmult = 0.5*(1 - diff/root);
    }
    soft_max_expression(const soft_max_expression & other) 
      : left(other.left), right(other.right), value(other.value), ldmult(other.ldmult), rdmult(other.rdmult) {}
    __INLINE INMOST_DATA_REAL_TYPE GetValue() const { return value; }
Kirill Terekhov's avatar
Kirill Terekhov committed
1203
    __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::RowMerger & r) const
Kirill Terekhov's avatar
Kirill Terekhov committed
1204
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
1205
1206
      left.GetJacobian(mult*ldmult,r);
      right.GetJacobian(mult*rdmult,r);
Kirill Terekhov's avatar
Kirill Terekhov committed
1207
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
1208
    __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::Row & r) const
Kirill Terekhov's avatar
Kirill Terekhov committed
1209
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
1210
1211
      left.GetJacobian(mult*ldmult,r);
      right.GetJacobian(mult*rdmult,r);
Kirill Terekhov's avatar
Kirill Terekhov committed
1212
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
1213
    __INLINE void GetHessian(INMOST_DATA_REAL_TYPE multJ, Sparse::Row & J, INMOST_DATA_REAL_TYPE multH, Sparse::HessianRow & H) const
Kirill Terekhov's avatar
Kirill Terekhov committed
1214
1215
1216
    {
        throw NotImplemented;
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
  };

  template<class A, class B>
  class soft_min_expression : public shell_expression<soft_min_expression<A,B> >
  {
    const A & left;
    const B & right;
    INMOST_DATA_REAL_TYPE value, ldmult, rdmult;
  public:
    soft_min_expression(const shell_expression<A> & pleft, const shell_expression<B> & pright, INMOST_DATA_REAL_TYPE tol) : left(pleft), right(pright)
    {
      INMOST_DATA_REAL_TYPE lval = left.GetValue(), rval = right.GetValue();
      INMOST_DATA_REAL_TYPE diff = lval-rval, root = ::sqrt(diff*diff+tol*tol);
      value = 0.5*(lval+rval - root);
      ldmult = 0.5*(1 - diff/root);
      rdmult = 0.5*(1 + diff/root);
    }
    soft_min_expression(const soft_min_expression & other) 
      : left(other.left), right(other.right), value(other.value), ldmult(other.ldmult), rdmult(other.rdmult) {}
    __INLINE INMOST_DATA_REAL_TYPE GetValue() const { return value; }
Kirill Terekhov's avatar
Kirill Terekhov committed
1237
    __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::RowMerger & r) const
Kirill Terekhov's avatar
Kirill Terekhov committed
1238
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
1239
1240
      left.GetJacobian(mult*ldmult,r);
      right.GetJacobian(mult*rdmult,r);
Kirill Terekhov's avatar
Kirill Terekhov committed
1241
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
1242
    __INLINE void GetJacobian(INMOST_DATA_REAL_TYPE mult, Sparse::Row & r) const
Kirill Terekhov's avatar
Kirill Terekhov committed
1243
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
1244
1245
      left.GetJacobian(mult*ldmult,r);
      right.GetJacobian(mult*rdmult,r);
Kirill Terekhov's avatar
Kirill Terekhov committed
1246
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
1247
    __INLINE void GetHessian(INMOST_DATA_REAL_TYPE multJ, Sparse::Row & J, INMOST_DATA_REAL_TYPE multH, Sparse::HessianRow & H) const
Kirill Terekhov's avatar
Kirill Terekhov committed
1248
1249
1250
    {
        throw NotImplemented;
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
1251
1252
1253
1254
1255
1256
1257
1258
  };


  template<class A, class B>
  class multiplication_expression : pub