You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Operation.h 33KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  1. /* -------------------------------------------------------------------------- *
  2. * Lepton *
  3. * -------------------------------------------------------------------------- *
  4. * This is part of the Lepton expression parser originating from *
  5. * Simbios, the NIH National Center for Physics-Based Simulation of *
  6. * Biological Structures at Stanford, funded under the NIH Roadmap for *
  7. * Medical Research, grant U54 GM072970. See https://simtk.org. *
  8. * *
  9. * Portions copyright (c) 2009-2013 Stanford University and the Authors. *
  10. * Authors: Peter Eastman *
  11. * Contributors: *
  12. * *
  13. * Permission is hereby granted, free of charge, to any person obtaining a *
  14. * copy of this software and associated documentation files (the "Software"), *
  15. * to deal in the Software without restriction, including without limitation *
  16. * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
  17. * and/or sell copies of the Software, and to permit persons to whom the *
  18. * Software is furnished to do so, subject to the following conditions: *
  19. * *
  20. * The above copyright notice and this permission notice shall be included in *
  21. * all copies or substantial portions of the Software. *
  22. * *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
  26. * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
  27. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
  28. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
  29. * USE OR OTHER DEALINGS IN THE SOFTWARE. *
  30. * -------------------------------------------------------------------------- */
  31. #pragma once
  32. #include "windowsIncludes.h"
  33. #include "CustomFunction.h"
  34. #include "Exception.h"
  35. #include <cmath>
  36. #include <map>
  37. #include <string>
  38. #include <vector>
  39. #include <sstream>
  40. #include <algorithm>
  41. namespace Lepton
  42. {
  43. class ExpressionTreeNode;
  44. /**
  45. * An Operation represents a single step in the evaluation of an expression, such as a function,
  46. * an operator, or a constant value. Each Operation takes some number of values as arguments
  47. * and produces a single value.
  48. *
  49. * This is an abstract class with subclasses for specific operations.
  50. */
  51. class LEPTON_EXPORT Operation
  52. {
  53. public:
  54. virtual ~Operation() { }
  55. /**
  56. * This enumeration lists all Operation subclasses. This is provided so that switch statements
  57. * can be used when processing or analyzing parsed expressions.
  58. */
  59. enum Id
  60. {
  61. CONSTANT, VARIABLE, CUSTOM, ADD, SUBTRACT, MULTIPLY, DIVIDE, POWER, NEGATE, SQRT, EXP, LOG,
  62. SIN, COS, SEC, CSC, TAN, COT, ASIN, ACOS, ATAN, SINH, COSH, TANH, ERF, ERFC, STEP, DELTA, SQUARE, CUBE, RECIPROCAL,
  63. ADD_CONSTANT, MULTIPLY_CONSTANT, POWER_CONSTANT, MIN, MAX, ABS
  64. };
  65. /**
  66. * Get the name of this Operation.
  67. */
  68. virtual std::string getName() const = 0;
  69. /**
  70. * Get this Operation's ID.
  71. */
  72. virtual Id getId() const = 0;
  73. /**
  74. * Get the number of arguments this operation expects.
  75. */
  76. virtual int getNumArguments() const = 0;
  77. /**
  78. * Create a clone of this Operation.
  79. */
  80. virtual Operation* clone() const = 0;
  81. /**
  82. * Perform the computation represented by this operation.
  83. *
  84. * @param args the array of arguments
  85. * @param variables a map containing the values of all variables
  86. * @return the result of performing the computation.
  87. */
  88. virtual double evaluate(double* args, const std::map<std::string, double>& variables) const = 0;
  89. /**
  90. * Return an ExpressionTreeNode which represents the analytic derivative of this Operation with respect to a variable.
  91. *
  92. * @param children the child nodes
  93. * @param childDerivs the derivatives of the child nodes with respect to the variable
  94. * @param variable the variable with respect to which the derivate should be taken
  95. */
  96. virtual ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs,
  97. const std::string& variable) const = 0;
  98. /**
  99. * Get whether this operation should be displayed with infix notation.
  100. */
  101. virtual bool isInfixOperator() const { return false; }
  102. /**
  103. * Get whether this is a symmetric binary operation, such that exchanging its arguments
  104. * does not affect the result.
  105. */
  106. virtual bool isSymmetric() const { return false; }
  107. virtual bool operator!=(const Operation& op) const { return op.getId() != getId(); }
  108. virtual bool operator==(const Operation& op) const { return !(*this != op); }
  109. class Constant;
  110. class Variable;
  111. class Custom;
  112. class Add;
  113. class Subtract;
  114. class Multiply;
  115. class Divide;
  116. class Power;
  117. class Negate;
  118. class Sqrt;
  119. class Exp;
  120. class Log;
  121. class Sin;
  122. class Cos;
  123. class Sec;
  124. class Csc;
  125. class Tan;
  126. class Cot;
  127. class Asin;
  128. class Acos;
  129. class Atan;
  130. class Sinh;
  131. class Cosh;
  132. class Tanh;
  133. class Erf;
  134. class Erfc;
  135. class Step;
  136. class Delta;
  137. class Square;
  138. class Cube;
  139. class Reciprocal;
  140. class AddConstant;
  141. class MultiplyConstant;
  142. class PowerConstant;
  143. class Min;
  144. class Max;
  145. class Abs;
  146. };
  147. class LEPTON_EXPORT Operation::Constant final : public Operation
  148. {
  149. public:
  150. Constant(double value) : value(value) { }
  151. std::string getName() const override
  152. {
  153. std::stringstream name;
  154. name << value;
  155. return name.str();
  156. }
  157. Id getId() const override { return CONSTANT; }
  158. int getNumArguments() const override { return 0; }
  159. Operation* clone() const override { return new Constant(value); }
  160. double evaluate(double* /*args*/, const std::map<std::string, double>& /*variables*/) const override { return value; }
  161. ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs,
  162. const std::string& variable) const override;
  163. double getValue() const { return value; }
  164. bool operator!=(const Operation& op) const override
  165. {
  166. const Constant* o = dynamic_cast<const Constant*>(&op);
  167. return (o == nullptr || o->value != value);
  168. }
  169. private:
  170. double value = 0;
  171. };
  172. class LEPTON_EXPORT Operation::Variable final : public Operation
  173. {
  174. public:
  175. Variable(const std::string& name) : name(name) { }
  176. std::string getName() const override { return name; }
  177. Id getId() const override { return VARIABLE; }
  178. int getNumArguments() const override { return 0; }
  179. Operation* clone() const override { return new Variable(name); }
  180. double evaluate(double* /*args*/, const std::map<std::string, double>& variables) const override
  181. {
  182. const auto iter = variables.find(name);
  183. if (iter == variables.end()) { throw Exception("No value specified for variable " + name); }
  184. return iter->second;
  185. }
  186. ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs,
  187. const std::string& variable) const override;
  188. bool operator!=(const Operation& op) const override
  189. {
  190. const Variable* o = dynamic_cast<const Variable*>(&op);
  191. return (o == nullptr || o->name != name);
  192. }
  193. private:
  194. std::string name;
  195. };
  196. class LEPTON_EXPORT Operation::Custom final : public Operation
  197. {
  198. public:
  199. Custom(const std::string& name, CustomFunction* function) : name(name), function(function), derivOrder(function->getNumArguments(), 0) { }
  200. Custom(const Custom& base, int derivIndex) : name(base.name), function(base.function->clone()), isDerivative(true), derivOrder(base.derivOrder)
  201. {
  202. derivOrder[derivIndex]++;
  203. }
  204. ~Custom() override { delete function; }
  205. std::string getName() const override { return name; }
  206. Id getId() const override { return CUSTOM; }
  207. int getNumArguments() const override { return function->getNumArguments(); }
  208. Operation* clone() const override
  209. {
  210. Custom* clone = new Custom(name, function->clone());
  211. clone->isDerivative = isDerivative;
  212. clone->derivOrder = derivOrder;
  213. return clone;
  214. }
  215. double evaluate(double* args, const std::map<std::string, double>& /*variables*/) const override
  216. {
  217. if (isDerivative) { return function->evaluateDerivative(args, &derivOrder[0]); }
  218. return function->evaluate(args);
  219. }
  220. ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs,
  221. const std::string& variable) const override;
  222. const std::vector<int>& getDerivOrder() const { return derivOrder; }
  223. bool operator!=(const Operation& op) const override
  224. {
  225. const Custom* o = dynamic_cast<const Custom*>(&op);
  226. return (o == nullptr || o->name != name || o->isDerivative != isDerivative || o->derivOrder != derivOrder);
  227. }
  228. private:
  229. std::string name;
  230. CustomFunction* function = nullptr;
  231. bool isDerivative = false;
  232. std::vector<int> derivOrder;
  233. };
  234. class LEPTON_EXPORT Operation::Add final : public Operation
  235. {
  236. public:
  237. Add() { }
  238. std::string getName() const override { return "+"; }
  239. Id getId() const override { return ADD; }
  240. int getNumArguments() const override { return 2; }
  241. Operation* clone() const override { return new Add(); }
  242. double evaluate(double* args, const std::map<std::string, double>& /*variables*/) const override { return args[0] + args[1]; }
  243. ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs,
  244. const std::string& variable) const override;
  245. bool isInfixOperator() const override { return true; }
  246. bool isSymmetric() const override { return true; }
  247. };
  248. class LEPTON_EXPORT Operation::Subtract final : public Operation
  249. {
  250. public:
  251. Subtract() { }
  252. std::string getName() const override { return "-"; }
  253. Id getId() const override { return SUBTRACT; }
  254. int getNumArguments() const override { return 2; }
  255. Operation* clone() const override { return new Subtract(); }
  256. double evaluate(double* args, const std::map<std::string, double>& /*variables*/) const override { return args[0] - args[1]; }
  257. ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs,
  258. const std::string& variable) const override;
  259. bool isInfixOperator() const override { return true; }
  260. };
  261. class LEPTON_EXPORT Operation::Multiply final : public Operation
  262. {
  263. public:
  264. Multiply() { }
  265. std::string getName() const override { return "*"; }
  266. Id getId() const override { return MULTIPLY; }
  267. int getNumArguments() const override { return 2; }
  268. Operation* clone() const override { return new Multiply(); }
  269. double evaluate(double* args, const std::map<std::string, double>& /*variables*/) const override { return args[0] * args[1]; }
  270. ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs,
  271. const std::string& variable) const override;
  272. bool isInfixOperator() const override { return true; }
  273. bool isSymmetric() const override { return true; }
  274. };
  275. class LEPTON_EXPORT Operation::Divide final : public Operation
  276. {
  277. public:
  278. Divide() { }
  279. std::string getName() const override { return "/"; }
  280. Id getId() const override { return DIVIDE; }
  281. int getNumArguments() const override { return 2; }
  282. Operation* clone() const override { return new Divide(); }
  283. double evaluate(double* args, const std::map<std::string, double>& /*variables*/) const override { return args[0] / args[1]; }
  284. ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs,
  285. const std::string& variable) const override;
  286. bool isInfixOperator() const override { return true; }
  287. };
  288. class LEPTON_EXPORT Operation::Power final : public Operation
  289. {
  290. public:
  291. Power() { }
  292. std::string getName() const override { return "^"; }
  293. Id getId() const override { return POWER; }
  294. int getNumArguments() const override { return 2; }
  295. Operation* clone() const override { return new Power(); }
  296. double evaluate(double* args, const std::map<std::string, double>& /*variables*/) const override { return std::pow(args[0], args[1]); }
  297. ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs,
  298. const std::string& variable) const override;
  299. bool isInfixOperator() const override { return true; }
  300. };
  301. class LEPTON_EXPORT Operation::Negate final : public Operation
  302. {
  303. public:
  304. Negate() { }
  305. std::string getName() const override { return "-"; }
  306. Id getId() const override { return NEGATE; }
  307. int getNumArguments() const override { return 1; }
  308. Operation* clone() const override { return new Negate(); }
  309. double evaluate(double* args, const std::map<std::string, double>& /*variables*/) const override { return -args[0]; }
  310. ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs,
  311. const std::string& variable) const override;
  312. };
  313. class LEPTON_EXPORT Operation::Sqrt final : public Operation
  314. {
  315. public:
  316. Sqrt() { }
  317. std::string getName() const override { return "sqrt"; }
  318. Id getId() const override { return SQRT; }
  319. int getNumArguments() const override { return 1; }
  320. Operation* clone() const override { return new Sqrt(); }
  321. double evaluate(double* args, const std::map<std::string, double>& /*variables*/) const override { return std::sqrt(args[0]); }
  322. ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs,
  323. const std::string& variable) const override;
  324. };
  325. class LEPTON_EXPORT Operation::Exp final : public Operation
  326. {
  327. public:
  328. Exp() { }
  329. std::string getName() const override { return "exp"; }
  330. Id getId() const override { return EXP; }
  331. int getNumArguments() const override { return 1; }
  332. Operation* clone() const override { return new Exp(); }
  333. double evaluate(double* args, const std::map<std::string, double>& /*variables*/) const override { return std::exp(args[0]); }
  334. ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs,
  335. const std::string& variable) const override;
  336. };
  337. class LEPTON_EXPORT Operation::Log final : public Operation
  338. {
  339. public:
  340. Log() { }
  341. std::string getName() const override { return "log"; }
  342. Id getId() const override { return LOG; }
  343. int getNumArguments() const override { return 1; }
  344. Operation* clone() const override { return new Log(); }
  345. double evaluate(double* args, const std::map<std::string, double>& /*variables*/) const override { return std::log(args[0]); }
  346. ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs,
  347. const std::string& variable) const override;
  348. };
  349. class LEPTON_EXPORT Operation::Sin final : public Operation
  350. {
  351. public:
  352. Sin() { }
  353. std::string getName() const override { return "sin"; }
  354. Id getId() const override { return SIN; }
  355. int getNumArguments() const override { return 1; }
  356. Operation* clone() const override { return new Sin(); }
  357. double evaluate(double* args, const std::map<std::string, double>& /*variables*/) const override { return std::sin(args[0]); }
  358. ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs,
  359. const std::string& variable) const override;
  360. };
  361. class LEPTON_EXPORT Operation::Cos final : public Operation
  362. {
  363. public:
  364. Cos() { }
  365. std::string getName() const override { return "cos"; }
  366. Id getId() const override { return COS; }
  367. int getNumArguments() const override { return 1; }
  368. Operation* clone() const override { return new Cos(); }
  369. double evaluate(double* args, const std::map<std::string, double>& /*variables*/) const override { return std::cos(args[0]); }
  370. ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs,
  371. const std::string& variable) const override;
  372. };
  373. class LEPTON_EXPORT Operation::Sec final : public Operation
  374. {
  375. public:
  376. Sec() { }
  377. std::string getName() const override { return "sec"; }
  378. Id getId() const override { return SEC; }
  379. int getNumArguments() const override { return 1; }
  380. Operation* clone() const override { return new Sec(); }
  381. double evaluate(double* args, const std::map<std::string, double>& /*variables*/) const override { return 1.0 / std::cos(args[0]); }
  382. ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs,
  383. const std::string& variable) const override;
  384. };
  385. class LEPTON_EXPORT Operation::Csc final : public Operation
  386. {
  387. public:
  388. Csc() { }
  389. std::string getName() const override { return "csc"; }
  390. Id getId() const override { return CSC; }
  391. int getNumArguments() const override { return 1; }
  392. Operation* clone() const override { return new Csc(); }
  393. double evaluate(double* args, const std::map<std::string, double>& /*variables*/) const override { return 1.0 / std::sin(args[0]); }
  394. ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs,
  395. const std::string& variable) const override;
  396. };
  397. class LEPTON_EXPORT Operation::Tan final : public Operation
  398. {
  399. public:
  400. Tan() { }
  401. std::string getName() const override { return "tan"; }
  402. Id getId() const override { return TAN; }
  403. int getNumArguments() const override { return 1; }
  404. Operation* clone() const override { return new Tan(); }
  405. double evaluate(double* args, const std::map<std::string, double>& /*variables*/) const override { return std::tan(args[0]); }
  406. ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs,
  407. const std::string& variable) const override;
  408. };
  409. class LEPTON_EXPORT Operation::Cot final : public Operation
  410. {
  411. public:
  412. Cot() { }
  413. std::string getName() const override { return "cot"; }
  414. Id getId() const override { return COT; }
  415. int getNumArguments() const override { return 1; }
  416. Operation* clone() const override { return new Cot(); }
  417. double evaluate(double* args, const std::map<std::string, double>& /*variables*/) const override { return 1.0 / std::tan(args[0]); }
  418. ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs,
  419. const std::string& variable) const override;
  420. };
  421. class LEPTON_EXPORT Operation::Asin final : public Operation
  422. {
  423. public:
  424. Asin() { }
  425. std::string getName() const override { return "asin"; }
  426. Id getId() const override { return ASIN; }
  427. int getNumArguments() const override { return 1; }
  428. Operation* clone() const override { return new Asin(); }
  429. double evaluate(double* args, const std::map<std::string, double>& /*variables*/) const override { return std::asin(args[0]); }
  430. ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs,
  431. const std::string& variable) const override;
  432. };
  433. class LEPTON_EXPORT Operation::Acos final : public Operation
  434. {
  435. public:
  436. Acos() { }
  437. std::string getName() const override { return "acos"; }
  438. Id getId() const override { return ACOS; }
  439. int getNumArguments() const override { return 1; }
  440. Operation* clone() const override { return new Acos(); }
  441. double evaluate(double* args, const std::map<std::string, double>& /*variables*/) const override { return std::acos(args[0]); }
  442. ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs,
  443. const std::string& variable) const override;
  444. };
  445. class LEPTON_EXPORT Operation::Atan final : public Operation
  446. {
  447. public:
  448. Atan() { }
  449. std::string getName() const override { return "atan"; }
  450. Id getId() const override { return ATAN; }
  451. int getNumArguments() const override { return 1; }
  452. Operation* clone() const override { return new Atan(); }
  453. double evaluate(double* args, const std::map<std::string, double>& /*variables*/) const override { return std::atan(args[0]); }
  454. ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs,
  455. const std::string& variable) const override;
  456. };
  457. class LEPTON_EXPORT Operation::Sinh final : public Operation
  458. {
  459. public:
  460. Sinh() { }
  461. std::string getName() const override { return "sinh"; }
  462. Id getId() const override { return SINH; }
  463. int getNumArguments() const override { return 1; }
  464. Operation* clone() const override { return new Sinh(); }
  465. double evaluate(double* args, const std::map<std::string, double>& /*variables*/) const override { return std::sinh(args[0]); }
  466. ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs,
  467. const std::string& variable) const override;
  468. };
  469. class LEPTON_EXPORT Operation::Cosh final : public Operation
  470. {
  471. public:
  472. Cosh() { }
  473. std::string getName() const override { return "cosh"; }
  474. Id getId() const override { return COSH; }
  475. int getNumArguments() const override { return 1; }
  476. Operation* clone() const override { return new Cosh(); }
  477. double evaluate(double* args, const std::map<std::string, double>& /*variables*/) const override { return std::cosh(args[0]); }
  478. ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs,
  479. const std::string& variable) const override;
  480. };
  481. class LEPTON_EXPORT Operation::Tanh final : public Operation
  482. {
  483. public:
  484. Tanh() { }
  485. std::string getName() const override { return "tanh"; }
  486. Id getId() const override { return TANH; }
  487. int getNumArguments() const override { return 1; }
  488. Operation* clone() const override { return new Tanh(); }
  489. double evaluate(double* args, const std::map<std::string, double>& /*variables*/) const override { return std::tanh(args[0]); }
  490. ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs,
  491. const std::string& variable) const override;
  492. };
  493. class LEPTON_EXPORT Operation::Erf final : public Operation
  494. {
  495. public:
  496. Erf() { }
  497. std::string getName() const override { return "erf"; }
  498. Id getId() const override { return ERF; }
  499. int getNumArguments() const override { return 1; }
  500. Operation* clone() const override { return new Erf(); }
  501. double evaluate(double* args, const std::map<std::string, double>& variables) const override;
  502. ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs,
  503. const std::string& variable) const override;
  504. };
  505. class LEPTON_EXPORT Operation::Erfc final : public Operation
  506. {
  507. public:
  508. Erfc() { }
  509. std::string getName() const override { return "erfc"; }
  510. Id getId() const override { return ERFC; }
  511. int getNumArguments() const override { return 1; }
  512. Operation* clone() const override { return new Erfc(); }
  513. double evaluate(double* args, const std::map<std::string, double>& variables) const override;
  514. ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs,
  515. const std::string& variable) const override;
  516. };
  517. class LEPTON_EXPORT Operation::Step final : public Operation
  518. {
  519. public:
  520. Step() { }
  521. std::string getName() const override { return "step"; }
  522. Id getId() const override { return STEP; }
  523. int getNumArguments() const override { return 1; }
  524. Operation* clone() const override { return new Step(); }
  525. double evaluate(double* args, const std::map<std::string, double>& /*variables*/) const override { return (args[0] >= 0.0 ? 1.0 : 0.0); }
  526. ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs,
  527. const std::string& variable) const override;
  528. };
  529. class LEPTON_EXPORT Operation::Delta final : public Operation
  530. {
  531. public:
  532. Delta() { }
  533. std::string getName() const override { return "delta"; }
  534. Id getId() const override { return DELTA; }
  535. int getNumArguments() const override { return 1; }
  536. Operation* clone() const override { return new Delta(); }
  537. double evaluate(double* args, const std::map<std::string, double>& /*variables*/) const override { return (args[0] == 0.0 ? 1.0 : 0.0); }
  538. ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs,
  539. const std::string& variable) const override;
  540. };
  541. class LEPTON_EXPORT Operation::Square final : public Operation
  542. {
  543. public:
  544. Square() { }
  545. std::string getName() const override { return "square"; }
  546. Id getId() const override { return SQUARE; }
  547. int getNumArguments() const override { return 1; }
  548. Operation* clone() const override { return new Square(); }
  549. double evaluate(double* args, const std::map<std::string, double>& /*variables*/) const override { return args[0] * args[0]; }
  550. ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs,
  551. const std::string& variable) const override;
  552. };
  553. class LEPTON_EXPORT Operation::Cube final : public Operation
  554. {
  555. public:
  556. Cube() { }
  557. std::string getName() const override { return "cube"; }
  558. Id getId() const override { return CUBE; }
  559. int getNumArguments() const override { return 1; }
  560. Operation* clone() const override { return new Cube(); }
  561. double evaluate(double* args, const std::map<std::string, double>& /*variables*/) const override { return args[0] * args[0] * args[0]; }
  562. ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs,
  563. const std::string& variable) const override;
  564. };
  565. class LEPTON_EXPORT Operation::Reciprocal final : public Operation
  566. {
  567. public:
  568. Reciprocal() { }
  569. std::string getName() const override { return "recip"; }
  570. Id getId() const override { return RECIPROCAL; }
  571. int getNumArguments() const override { return 1; }
  572. Operation* clone() const override { return new Reciprocal(); }
  573. double evaluate(double* args, const std::map<std::string, double>& /*variables*/) const override { return 1.0 / args[0]; }
  574. ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs,
  575. const std::string& variable) const override;
  576. };
  577. class LEPTON_EXPORT Operation::AddConstant final : public Operation
  578. {
  579. public:
  580. AddConstant(double value) : value(value) { }
  581. std::string getName() const override
  582. {
  583. std::stringstream name;
  584. name << value << "+";
  585. return name.str();
  586. }
  587. Id getId() const override { return ADD_CONSTANT; }
  588. int getNumArguments() const override { return 1; }
  589. Operation* clone() const override { return new AddConstant(value); }
  590. double evaluate(double* args, const std::map<std::string, double>& /*variables*/) const override { return args[0] + value; }
  591. ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs,
  592. const std::string& variable) const override;
  593. double getValue() const { return value; }
  594. bool operator!=(const Operation& op) const override
  595. {
  596. const AddConstant* o = dynamic_cast<const AddConstant*>(&op);
  597. return (o == nullptr || o->value != value);
  598. }
  599. private:
  600. double value = 0;
  601. };
  602. class LEPTON_EXPORT Operation::MultiplyConstant final : public Operation
  603. {
  604. public:
  605. MultiplyConstant(double value) : value(value) { }
  606. std::string getName() const override
  607. {
  608. std::stringstream name;
  609. name << value << "*";
  610. return name.str();
  611. }
  612. Id getId() const override { return MULTIPLY_CONSTANT; }
  613. int getNumArguments() const override { return 1; }
  614. Operation* clone() const override { return new MultiplyConstant(value); }
  615. double evaluate(double* args, const std::map<std::string, double>& /*variables*/) const override { return args[0] * value; }
  616. ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs,
  617. const std::string& variable) const override;
  618. double getValue() const { return value; }
  619. bool operator!=(const Operation& op) const override
  620. {
  621. const MultiplyConstant* o = dynamic_cast<const MultiplyConstant*>(&op);
  622. return (o == nullptr || o->value != value);
  623. }
  624. private:
  625. double value = 0;
  626. };
  627. class LEPTON_EXPORT Operation::PowerConstant final : public Operation
  628. {
  629. public:
  630. PowerConstant(double value) : value(value)
  631. {
  632. intValue = int(value);
  633. isIntPower = (intValue == value);
  634. }
  635. std::string getName() const override
  636. {
  637. std::stringstream name;
  638. name << "^" << value;
  639. return name.str();
  640. }
  641. Id getId() const override { return POWER_CONSTANT; }
  642. int getNumArguments() const override { return 1; }
  643. Operation* clone() const override { return new PowerConstant(value); }
  644. double evaluate(double* args, const std::map<std::string, double>& /*variables*/) const override
  645. {
  646. if (isIntPower)
  647. {
  648. // Integer powers can be computed much more quickly by repeated multiplication.
  649. int exponent = intValue;
  650. double base = args[0];
  651. if (exponent < 0)
  652. {
  653. exponent = -exponent;
  654. base = 1.0 / base;
  655. }
  656. double result = 1.0;
  657. while (exponent != 0)
  658. {
  659. if ((exponent & 1) == 1) { result *= base; }
  660. base *= base;
  661. exponent = exponent >> 1;
  662. }
  663. return result;
  664. }
  665. return std::pow(args[0], value);
  666. }
  667. ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs,
  668. const std::string& variable) const override;
  669. double getValue() const { return value; }
  670. bool operator!=(const Operation& op) const override
  671. {
  672. const PowerConstant* o = dynamic_cast<const PowerConstant*>(&op);
  673. return (o == nullptr || o->value != value);
  674. }
  675. bool isInfixOperator() const override { return true; }
  676. private:
  677. double value = 0;
  678. int intValue = 0;
  679. bool isIntPower;
  680. };
  681. class LEPTON_EXPORT Operation::Min final : public Operation
  682. {
  683. public:
  684. Min() { }
  685. std::string getName() const override { return "min"; }
  686. Id getId() const override { return MIN; }
  687. int getNumArguments() const override { return 2; }
  688. Operation* clone() const override { return new Min(); }
  689. double evaluate(double* args, const std::map<std::string, double>& /*variables*/) const override
  690. {
  691. // parens around (std::min) are workaround for horrible microsoft max/min macro trouble
  692. return (std::min)(args[0], args[1]);
  693. }
  694. ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs,
  695. const std::string& variable) const override;
  696. };
  697. class LEPTON_EXPORT Operation::Max final : public Operation
  698. {
  699. public:
  700. Max() { }
  701. std::string getName() const override { return "max"; }
  702. Id getId() const override { return MAX; }
  703. int getNumArguments() const override { return 2; }
  704. Operation* clone() const override { return new Max(); }
  705. double evaluate(double* args, const std::map<std::string, double>& /*variables*/) const override
  706. {
  707. // parens around (std::min) are workaround for horrible microsoft max/min macro trouble
  708. return (std::max)(args[0], args[1]);
  709. }
  710. ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs,
  711. const std::string& variable) const override;
  712. };
  713. class LEPTON_EXPORT Operation::Abs final : public Operation
  714. {
  715. public:
  716. Abs() { }
  717. std::string getName() const override { return "abs"; }
  718. Id getId() const override { return ABS; }
  719. int getNumArguments() const override { return 1; }
  720. Operation* clone() const override { return new Abs(); }
  721. double evaluate(double* args, const std::map<std::string, double>& /*variables*/) const override { return std::abs(args[0]); }
  722. ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs,
  723. const std::string& variable) const override;
  724. };
  725. } // namespace Lepton