inmost_variable.h 39 KB
Newer Older
Kirill Terekhov's avatar
Kirill Terekhov committed
1 2 3 4 5 6 7 8 9 10 11

#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>

Kirill Terekhov's avatar
Kirill Terekhov committed
12
#if defined(USE_AUTODIFF) && defined(USE_MESH)
Kirill Terekhov's avatar
Kirill Terekhov committed
13 14 15 16 17 18 19 20

//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
21
// 7. change stencil_variable with foreach_variable and introduce function foreach(iterator beg, iterator end, arg)
Kirill Terekhov's avatar
Kirill Terekhov committed
22
// 8. enclose in namespace
Kirill Terekhov's avatar
Kirill Terekhov committed
23
// 9. maybe should not use const A & in classes, since some class may be destroyed prior use - investigate
Kirill Terekhov's avatar
Kirill Terekhov committed
24

Kirill Terekhov's avatar
Kirill Terekhov committed
25 26 27 28 29
//This should stop Visual Studio from complaining of very long auto-generated class types
#ifdef _MSC_VER
#pragma warning(disable : 4503)
#endif

Kirill Terekhov's avatar
Kirill Terekhov committed
30 31 32 33


namespace INMOST
{
34 35 36
	
	template<class Op, class A>
	class unary_pool
37
	{
38 39 40 41
		A arg;
		Op operand;
	public:
		unary_pool(const A & parg) : arg(parg), operand(arg) {}
42 43
		unary_pool(const unary_pool & other) : arg(other.arg), operand(other.operand,arg) {}
		unary_pool & operator = (unary_pool const & other) {arg = other.arg; operand.assign(other.operand,arg); return * this;}
44 45 46 47 48
		const shell_expression<A> & get_arg() {return arg;}
		Op & get_op() {return operand;}
		const Op & get_op() const {return operand;}
	};
	
49

50 51 52
	
	template<class Op, class A, class B>
	class binary_pool
53
	{
54 55 56 57 58 59
		
		A left;
		B right;
		Op operand;
	public:
		binary_pool(const A & pleft, const B & pright) : left(pleft), right(pright), operand(left,right) {}
60 61
		binary_pool(const binary_pool & other) : left(other.left), right(other.right), operand(other.operand,left,right) {}
		binary_pool & operator = (binary_pool const & other) {left = other.left; right = other.right; operand.assign(other.operand,left,right); return * this;}
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
		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) {}
78 79
		ternary_pool(const ternary_pool & other) : cond(other.cond), left(other.left), right(other.right), operand(other.operand,cond,left,right) {}
		ternary_pool & operator =(ternary_pool const & other) {cond = other.cond; left = other.left; right = other.right; operand.assign(other.operand,cond,left,right); return * this;}
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
		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) {}
		unary_pool_expression & operator = (unary_pool_expression const & other) {pool = other.pool; return * this;}
		__INLINE INMOST_DATA_REAL_TYPE GetValue() const { return pool.get_op().GetValue(); }
		__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);}
		__INLINE void GetHessian(INMOST_DATA_REAL_TYPE multJ, Sparse::Row & J, INMOST_DATA_REAL_TYPE multH, Sparse::HessianRow & H) const {pool.get_op().GetHessian(multJ,J,multH,H);}
	};
101

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 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 175 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 337 338 339 340 341 342 343 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
	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) {}
		binary_pool_expression & operator = (binary_pool_expression const & other) {pool = other.pool; return * this;}
		__INLINE INMOST_DATA_REAL_TYPE GetValue() const { return pool.get_op().GetValue(); }
		__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);}
		__INLINE void GetHessian(INMOST_DATA_REAL_TYPE multJ, Sparse::Row & J, INMOST_DATA_REAL_TYPE multH, Sparse::HessianRow & H) const {pool.get_op().GetHessian(multJ,J,multH,H);}
	};
	
	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) {}
		ternary_pool_expression & operator = (ternary_pool_expression const & other) {pool = other.pool; return * this;}
		__INLINE INMOST_DATA_REAL_TYPE GetValue() const { return pool.get_op().GetValue(); }
		__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);}
		__INLINE void GetHessian(INMOST_DATA_REAL_TYPE multJ, Sparse::Row & J, INMOST_DATA_REAL_TYPE multH, Sparse::HessianRow & H) const {pool.get_op().GetHessian(multJ,J,multH,H);}
	};
	
	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;
		virtual abstract_dynamic_variable * Copy() const = 0;
		virtual ~abstract_dynamic_variable() {}
	};
	
	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);}
	};
	
	
	
	
	
	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;
		virtual abstract_dynamic_variable * Copy() const = 0;
		virtual ~basic_dynamic_variable() {}
	};
	
	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);}
		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(); }
	};
	
	class stored_variable_expression : public shell_dynamic_variable<multivar_expression,stored_variable_expression>
	{
		abstract_dynamic_variable * var;
	public:
		stored_variable_expression() : var(NULL) {}
		stored_variable_expression(const abstract_dynamic_variable & pvar) : var(pvar.Copy()) {}
		stored_variable_expression(const stored_variable_expression & other) : var(other.var->Copy()) {}
		~stored_variable_expression() {delete var; var = NULL;}
		stored_variable_expression operator =(stored_variable_expression const & other) {var = other.var->Copy(); return *this;}
		stored_variable_expression operator =(const abstract_dynamic_variable & pvar) {var = pvar.Copy(); return *this;}
		INMOST_DATA_REAL_TYPE Value(const Storage & e) const {return var->Value(e);}
		multivar_expression Variable(const Storage & e) const {return var->Variable(e);}
		multivar_expression operator [](const Storage & e) const {return var->Variable(e);}
		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); }
		
		template<typename T>
		get_variable<T> get_variable() {return get_variable<T>(*var);}
		abstract_dynamic_variable & retrive_expression() {return *var;}
		const abstract_dynamic_variable & retrive_expression() const {return *var;}
		abstract_dynamic_variable * Copy() const {return static_cast<abstract_dynamic_variable *>(new stored_variable_expression(*this));}
	};
	
	
	class dynamic_variable : public shell_dynamic_variable<var_expression,dynamic_variable >
	{
	private:
		const AbstractEntry * entry;
		INMOST_DATA_ENUM_TYPE comp;
	public:
		dynamic_variable() :entry(NULL), comp(ENUMUNDEF) {}
		dynamic_variable(Automatizator & aut, INMOST_DATA_ENUM_TYPE reg_index, INMOST_DATA_ENUM_TYPE comp = 0) : entry(reg_index==ENUMUNDEF?NULL:&aut.GetEntry(reg_index)), comp(comp) {}
		dynamic_variable(const AbstractEntry * re, INMOST_DATA_ENUM_TYPE comp = 0) : entry(re), comp(comp) {}
		dynamic_variable(const dynamic_variable & other) : entry(other.entry), comp(other.comp) {}
		dynamic_variable & operator =(const dynamic_variable & other)
		{
			entry = other.entry;
			comp = other.comp;
			return * this;
		}
		INMOST_DATA_REAL_TYPE Value(const Storage & e) const {return entry->Value(e,comp);}
		INMOST_DATA_ENUM_TYPE Index(const Storage & e) const {return entry->isValid(e) ? entry->Index(e,comp):ENUMUNDEF;}
		multivar_expression Variable(const Storage & e) const
		{
			if( entry->isValid(e) )
				return entry->Unknown(e,comp);
			else
				return entry->Value(e,comp);
		}
		var_expression operator [](const Storage & e) const {return var_expression(entry->Value(e,comp),entry->isValid(e)?entry->Index(e,comp):ENUMUNDEF);}
		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); }
		bool isUnknown(const Storage & e) const {return entry->isValid(e)?true:false;}
		abstract_dynamic_variable * Copy() const {return static_cast<abstract_dynamic_variable *>(new dynamic_variable(*this));}
	};
	
	class const_variable : public shell_dynamic_variable<const_expression,const_variable>
	{
	private:
		INMOST_DATA_REAL_TYPE value;
	public:
		const_variable(INMOST_DATA_REAL_TYPE _value) : value(_value)  {}
		const_variable(const const_variable & other) : value(other.value) {}
		const_variable & operator =(const const_variable & other)
		{
			value = other.value;
			return * this;
		}
		INMOST_DATA_REAL_TYPE Value(const Storage & e) const {return value;}
		multivar_expression Variable(const Storage & e) const
		{
			return multivar_expression(value);
		}
		const_expression operator [](const Storage & e) const {return const_expression(value);}
		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); }
		abstract_dynamic_variable * Copy() const {return static_cast<abstract_dynamic_variable *>(new const_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;}
		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); }
		bool isUnknown(const Storage & e) const {(void)e; 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_reference,stored_variable>
	{
	private:
		Tag variable_tag;
		INMOST_DATA_ENUM_TYPE comp;
	public:
		stored_variable() : variable_tag(), comp(ENUMUNDEF) {}
		stored_variable(Tag t, INMOST_DATA_ENUM_TYPE pcomp = 0) : variable_tag(t), comp(pcomp)
		{
			assert(t.GetDataType() == DATA_REAL || t.GetDataType() == DATA_VARIABLE);
		}
		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
		{
			if( variable_tag.GetDataType() == DATA_VARIABLE )
				return e->VariableArray(variable_tag)[comp].GetValue();
			else if( variable_tag.GetDataType() == DATA_REAL )
				return e->RealArray(variable_tag)[comp];
			else throw NotImplemented;
		}
		multivar_expression Variable(const Storage & e) const
		{
			if( variable_tag.GetDataType() == DATA_VARIABLE )
				return e->VariableArray(variable_tag)[comp];
			else if( variable_tag.GetDataType() == DATA_REAL )
				return variable(e->RealArray(variable_tag)[comp]);
			else throw NotImplemented;
		}
		multivar_expression_reference operator [](const Storage & e) const
		{
			if( variable_tag.GetDataType() == DATA_VARIABLE )
				return e->VariableArray(variable_tag)[comp];
			else if( variable_tag.GetDataType() == DATA_REAL )
				return multivar_expression_reference(e->RealArray(variable_tag)[comp],NULL);
			else throw NotImplemented;
		}
		Tag VariableTag() {return variable_tag;}
		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); }
		bool isUnknown(const Storage & e) const {(void)e; return false;}
		abstract_dynamic_variable * Copy() const {return static_cast<abstract_dynamic_variable *>(new stored_variable(*this));}
	};
	
	template<class A>
	class stencil_variable : public shell_dynamic_variable< stencil_expression<typename A::Var>, stencil_variable<A> >
	{
	private:
		Tag tag_elems;
		Tag tag_coefs;
		A Arg;
	public:
		stencil_variable(Tag tag_elems, Tag tag_coefs, const shell_dynamic_variable<typename A::Var,A> & parg) : tag_elems(tag_elems), tag_coefs(tag_coefs), Arg(parg) {}
		stencil_variable(const stencil_variable & other) : tag_elems(other.tag_elems), tag_coefs(other.tag_coefs), Arg(other.Arg) {}
		stencil_variable & operator =(const stencil_variable & other) {tag_elems = other.tag_elems; tag_coefs = other.tag_coefs; Arg = other.Arg; return * this;}
		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< const_multiplication_expression<typename A::Var>, 64> tmp;
			Storage::real_array      coefs = e.RealArray(tag_coefs);
			Storage::reference_array elems = e.RealArray(tag_elems);
			assert(coefs.size() == elems.size());
			tmp.resize(elems.size());
			for(INMOST_DATA_ENUM_TYPE k = 0; k < elems.size(); ++k)
				tmp[k] = const_multiplication_expression<A>(Arg[elems[k]],coefs[k]);
			return stencil_expression<typename A::Var>(tmp);
		}
		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); }
		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> >
	{
		A Arg;
		const keyval_table & Table;
	public:
novikov's avatar
novikov committed
395
		table_variable(const shell_dynamic_variable<typename A::Var,A> & parg,  const keyval_table  & ptable) : Arg(parg), Table(ptable) {}
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
		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);
		}
		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); }
		abstract_dynamic_variable * Copy() const {return static_cast<abstract_dynamic_variable *>(new table_variable(*this));}
	};
	
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 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
	/// This class makes possible to evaluate different expressions on different element types.
	/// See etype_branch function.
	template<class A, class B>
	class etype_branch_variable : public shell_dynamic_variable< multivar_expression, etype_branch_variable<A,B> >
	{
	private:
		A ArgA; //< Variable expression to be evaluated when type of provided element matches selected types.
		B ArgB; //< Variable expression to be evaluated when type of provided element does not match selected types.
		ElementType types_true; //< Selected types of elements.
	public:
		/// Constructor. Used by etype_branch function.
		etype_branch_variable(ElementType _types_true, const A & _ArgA, const B & _ArgB) : types_true(_types_true), ArgA(_ArgA), ArgB(_ArgB) {}
		/// Copy constructor.
		etype_branch_variable(const etype_branch_variable & other) : types_true(other.types_true), ArgA(other.ArgA), ArgB(other.ArgB) {}
		/// Assignment operator.
		etype_branch_variable & operator =(etype_branch_variable const & other)
		{
			types_true = other.types_true;
			ArgA = other.ArgA;
			ArgB = other.ArgB;
			return *this;
		}
		/// Get value of variable expression on provided element e.
		INMOST_DATA_REAL_TYPE Value(const Storage & e) const {return (*this)[e].GetValue();}
		/// Get value with derivatives of variable expression on provided element e.
		/// This function collapses associated expression tree into multivar_expression.
		multivar_expression Variable(const Storage & e) const { return (*this)[e]; }
		/// Build an expression associated with variable expression on provided element e.
		multivar_expression operator [](const Storage & e) const
		{
			if( e->GetElementType() & types_true )
				return ArgA[e];
			else return ArgB[e];
		}
		/// Retrive first derivatives of variable expression on provided element e, default approach.
		void GetVariation(const Storage & e, Sparse::Row & r) const { (*this)[e].GetJacobian(1.0,r); }
		/// Retrive first derivatives of variable expression on provided element e, with supplimentary structure Sparse::RowMerger.
		void GetVariation(const Storage & e, Sparse::RowMerger & r) const { (*this)[e].GetJacobian(1.0,r); }
		/// Make a copy of this class, used to reproduce and store a tree of variable expressions.
		abstract_dynamic_variable * Copy() const {return static_cast<abstract_dynamic_variable *>(new etype_branch_variable(*this));}
	};
	
	/// This class makes possible to evaluate different expressions depending on the markers.
	/// Works similarly for shared and private markers.
	/// See marker_branch function.
	template<class A, class B>
	class marker_branch_variable : public shell_dynamic_variable< multivar_expression, marker_branch_variable<A,B> >
	{
	private:
		A ArgA; //< Variable expression to be evaluated when marker is set on the element.
		B ArgB; //< Variable expression to be evaluated when marker is not set on the element.
		MarkerType marker; //< Marker.
	public:
		/// Constructor. Used by marker_branch function.
		marker_branch_variable(MarkerType _marker, const A & _ArgA, const B & _ArgB) : marker(_marker), ArgA(_ArgA), ArgB(_ArgB) {}
		/// Copy constructor.
		marker_branch_variable(const marker_branch_variable & other) : marker(other.marker), ArgA(other.ArgA), ArgB(other.ArgB) {}
		/// Assignment operator.
		marker_branch_variable & operator =(marker_branch_variable const & other)
		{
			marker = other.marker;
			ArgA = other.ArgA;
			ArgB = other.ArgB;
			return *this;
		}
		/// Get value of variable expression on provided element e.
		INMOST_DATA_REAL_TYPE Value(const Storage & e) const {return (*this)[e].GetValue();}
		/// Get value with derivatives of variable expression on provided element e.
		/// This function collapses associated expression tree into multivar_expression.
		multivar_expression Variable(const Storage & e) const { return (*this)[e]; }
		/// Build an expression associated with variable expression on provided element e.
		multivar_expression operator [](const Storage & e) const
		{
tgaripov's avatar
tgaripov committed
490
			if( isPrivate(marker) ? e->GetPrivateMarker(marker) : e->GetMarker(marker) )
491 492 493 494 495 496 497 498 499 500 501 502
				return ArgA[e];
			else return ArgB[e];
		}
		/// Retrive first derivatives of variable expression on provided element e, default approach.
		void GetVariation(const Storage & e, Sparse::Row & r) const { (*this)[e].GetJacobian(1.0,r); }
		/// Retrive first derivatives of variable expression on provided element e, with supplimentary structure Sparse::RowMerger.
		void GetVariation(const Storage & e, Sparse::RowMerger & r) const { (*this)[e].GetJacobian(1.0,r); }
		/// Make a copy of this class, used to reproduce and store a tree of variable expressions.
		abstract_dynamic_variable * Copy() const {return static_cast<abstract_dynamic_variable *>(new marker_branch_variable(*this));}
	};
	
	
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 548 549 550 551 552 553 554 555
	
	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:
		A Arg;
	public:
		unary_custom_variable(const shell_dynamic_variable<typename A::Var,A> & parg) : Arg(parg) {}
		unary_custom_variable(const unary_custom_variable & other) : Arg(other.Arg) {}
		unary_custom_variable & operator =(unary_custom_variable const & other) {Arg = other.Arg; return * this;}
		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
		{
			unary_pool<Expr,typename A::Var> pool(Arg[e]);
			return unary_pool_expression<Expr, typename A::Var >(pool);
		}
		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); }
		abstract_dynamic_variable * Copy() const {return static_cast<abstract_dynamic_variable *>(new unary_custom_variable(*this));}
	};
	
	
	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:
		A Left;
		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) {}
		binary_custom_variable & operator =(binary_custom_variable const & other) {Left = other.Left; Right = other.Right; return * this;}
		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
		{
			binary_pool<Expr,typename A::Var,typename B::Var> pool(Left[e],Right[e]);
			return binary_pool_expression<Expr, typename A::Var, typename B::Var >(pool);
		}
		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); }
		abstract_dynamic_variable * Copy() const {return static_cast<abstract_dynamic_variable *>(new binary_custom_variable(*this));}
	};
556

557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
	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:
		A Cond;
		B Left;
		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) {}
		ternary_custom_variable & operator =(ternary_custom_variable const & other) {Left = other.Left; Right = other.Right; return * this;}
		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
		{
			ternary_pool<Expr,typename A::Var,typename B::Var, typename C::Var> pool(Cond[e],Left[e],Right[e]);
			return ternary_pool_expression<Expr, typename A::Var, typename B::Var, typename C::Var>(pool);
		}
		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); }
		abstract_dynamic_variable * Copy() const {return static_cast<abstract_dynamic_variable *>(new ternary_custom_variable(*this));}
	};
	typedef abstract_dynamic_variable abstract_variable;
585 586
}

Kirill Terekhov's avatar
Kirill Terekhov committed
587
template<class A, class B, class C> 
588 589
__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); }
590
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); }
Kirill Terekhov's avatar
Kirill Terekhov committed
591
template<class A>          __INLINE                                  INMOST::unary_custom_variable<INMOST::unary_plus_expression<typename A::Var>,A> operator+(INMOST::shell_dynamic_variable<typename A::Var, A> const & Arg) { return INMOST::unary_custom_variable<INMOST::unary_plus_expression<typename A::Var>,A>(Arg); }
592 593 594 595 596 597
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); }
598
//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);}
599 600 601 602 603
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); }
604 605 606 607 608 609 610 611 612 613
//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
614
template<class A>          __INLINE                                                                                      INMOST::stencil_variable<A>   stencil(INMOST::Tag tag_elems, INMOST::Tag tag_coefs, INMOST::shell_dynamic_variable<typename A::Var,A> const & Arg) { return INMOST::stencil_variable<A>(tag_elems,tag_coefs,Arg); }
Kirill Terekhov's avatar
Kirill Terekhov committed
615
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>(Arg,Table);}
Kirill Terekhov's avatar
Kirill Terekhov committed
616 617 618 619 620 621 622
template<class A>          __INLINE                                                                                    INMOST::stencil_expression<A>   stencil(INMOST::HandleType * elems, INMOST_DATA_REAL_TYPE * coefs, INMOST_DATA_ENUM_TYPE num, INMOST::shell_dynamic_variable<typename A::Var,A> const & Arg)
{
	INMOST::dynarray< INMOST::const_multiplication_expression<typename A::Var>, 64> tmp;
	for( INMOST_DATA_ENUM_TYPE k = 0; k < num; ++k) if( elems[k] != 0 )
		tmp.push_back(INMOST::const_multiplication_expression<typename A::Var>(Arg[elems[k]],coefs[k]));
	return INMOST::stencil_expression<typename A::Var>(tmp);
}
623 624
template<class A, class B> __INLINE INMOST::etype_branch_variable<A,B> etype_branch(INMOST::ElementType true_type, INMOST::shell_dynamic_variable<typename A::Var,A> const & iftrue, INMOST::shell_dynamic_variable<typename B::Var,B> const & iffalse) {return INMOST::etype_branch_variable<A,B>(true_type,iftrue,iffalse);}
template<class A, class B> __INLINE INMOST::marker_branch_variable<A,B> marker_branch(INMOST::MarkerType marker, INMOST::shell_dynamic_variable<typename A::Var,A> const & iftrue, INMOST::shell_dynamic_variable<typename B::Var,B> const & iffalse) {return INMOST::marker_branch_variable<A,B>(marker,iftrue,iffalse);}
Kirill Terekhov's avatar
Kirill Terekhov committed
625

Kirill Terekhov's avatar
Kirill Terekhov committed
626
#endif //defined(USE_AUTODIFF) && defined(USE_MESH)
627

628

629

Kirill Terekhov's avatar
Kirill Terekhov committed
630

Kirill Terekhov's avatar
Kirill Terekhov committed
631 632
#endif //INMOST_AUTODIFF_ETVAR_H_INCLUDED