symbolics.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. /*-----------------------------------------------------------------------------
  2. | Copyright (c) 2013-2017, Nucleic Development Team.
  3. |
  4. | Distributed under the terms of the Modified BSD License.
  5. |
  6. | The full license is in the file COPYING.txt, distributed with this software.
  7. |----------------------------------------------------------------------------*/
  8. #pragma once
  9. #include <vector>
  10. #include "constraint.h"
  11. #include "expression.h"
  12. #include "term.h"
  13. #include "variable.h"
  14. namespace kiwi
  15. {
  16. // Variable multiply, divide, and unary invert
  17. inline
  18. Term operator*( const Variable& variable, double coefficient )
  19. {
  20. return Term( variable, coefficient );
  21. }
  22. inline
  23. Term operator/( const Variable& variable, double denominator )
  24. {
  25. return variable * ( 1.0 / denominator );
  26. }
  27. inline
  28. Term operator-( const Variable& variable )
  29. {
  30. return variable * -1.0;
  31. }
  32. // Term multiply, divide, and unary invert
  33. inline
  34. Term operator*( const Term& term, double coefficient )
  35. {
  36. return Term( term.variable(), term.coefficient() * coefficient );
  37. }
  38. inline
  39. Term operator/( const Term& term, double denominator )
  40. {
  41. return term * ( 1.0 / denominator );
  42. }
  43. inline
  44. Term operator-( const Term& term )
  45. {
  46. return term * -1.0;
  47. }
  48. // Expression multiply, divide, and unary invert
  49. inline
  50. Expression operator*( const Expression& expression, double coefficient )
  51. {
  52. std::vector<Term> terms;
  53. terms.reserve( expression.terms().size() );
  54. typedef std::vector<Term>::const_iterator iter_t;
  55. iter_t begin = expression.terms().begin();
  56. iter_t end = expression.terms().end();
  57. for( iter_t it = begin; it != end; ++it )
  58. terms.push_back( ( *it ) * coefficient );
  59. return Expression( terms, expression.constant() * coefficient );
  60. }
  61. inline
  62. Expression operator/( const Expression& expression, double denominator )
  63. {
  64. return expression * ( 1.0 / denominator );
  65. }
  66. inline
  67. Expression operator-( const Expression& expression )
  68. {
  69. return expression * -1.0;
  70. }
  71. // Double multiply
  72. inline
  73. Expression operator*( double coefficient, const Expression& expression )
  74. {
  75. return expression * coefficient;
  76. }
  77. inline
  78. Term operator*( double coefficient, const Term& term )
  79. {
  80. return term * coefficient;
  81. }
  82. inline
  83. Term operator*( double coefficient, const Variable& variable )
  84. {
  85. return variable * coefficient;
  86. }
  87. // Expression add and subtract
  88. inline
  89. Expression operator+( const Expression& first, const Expression& second )
  90. {
  91. std::vector<Term> terms;
  92. terms.reserve( first.terms().size() + second.terms().size() );
  93. terms.insert( terms.begin(), first.terms().begin(), first.terms().end() );
  94. terms.insert( terms.end(), second.terms().begin(), second.terms().end() );
  95. return Expression( terms, first.constant() + second.constant() );
  96. }
  97. inline
  98. Expression operator+( const Expression& first, const Term& second )
  99. {
  100. std::vector<Term> terms;
  101. terms.reserve( first.terms().size() + 1 );
  102. terms.insert( terms.begin(), first.terms().begin(), first.terms().end() );
  103. terms.push_back( second );
  104. return Expression( terms, first.constant() );
  105. }
  106. inline
  107. Expression operator+( const Expression& expression, const Variable& variable )
  108. {
  109. return expression + Term( variable );
  110. }
  111. inline
  112. Expression operator+( const Expression& expression, double constant )
  113. {
  114. return Expression( expression.terms(), expression.constant() + constant );
  115. }
  116. inline
  117. Expression operator-( const Expression& first, const Expression& second )
  118. {
  119. return first + -second;
  120. }
  121. inline
  122. Expression operator-( const Expression& expression, const Term& term )
  123. {
  124. return expression + -term;
  125. }
  126. inline
  127. Expression operator-( const Expression& expression, const Variable& variable )
  128. {
  129. return expression + -variable;
  130. }
  131. inline
  132. Expression operator-( const Expression& expression, double constant )
  133. {
  134. return expression + -constant;
  135. }
  136. // Term add and subtract
  137. inline
  138. Expression operator+( const Term& term, const Expression& expression )
  139. {
  140. return expression + term;
  141. }
  142. inline
  143. Expression operator+( const Term& first, const Term& second )
  144. {
  145. std::vector<Term> terms;
  146. terms.reserve( 2 );
  147. terms.push_back( first );
  148. terms.push_back( second );
  149. return Expression( terms );
  150. }
  151. inline
  152. Expression operator+( const Term& term, const Variable& variable )
  153. {
  154. return term + Term( variable );
  155. }
  156. inline
  157. Expression operator+( const Term& term, double constant )
  158. {
  159. return Expression( term, constant );
  160. }
  161. inline
  162. Expression operator-( const Term& term, const Expression& expression )
  163. {
  164. return -expression + term;
  165. }
  166. inline
  167. Expression operator-( const Term& first, const Term& second )
  168. {
  169. return first + -second;
  170. }
  171. inline
  172. Expression operator-( const Term& term, const Variable& variable )
  173. {
  174. return term + -variable;
  175. }
  176. inline
  177. Expression operator-( const Term& term, double constant )
  178. {
  179. return term + -constant;
  180. }
  181. // Variable add and subtract
  182. inline
  183. Expression operator+( const Variable& variable, const Expression& expression )
  184. {
  185. return expression + variable;
  186. }
  187. inline
  188. Expression operator+( const Variable& variable, const Term& term )
  189. {
  190. return term + variable;
  191. }
  192. inline
  193. Expression operator+( const Variable& first, const Variable& second )
  194. {
  195. return Term( first ) + second;
  196. }
  197. inline
  198. Expression operator+( const Variable& variable, double constant )
  199. {
  200. return Term( variable ) + constant;
  201. }
  202. inline
  203. Expression operator-( const Variable& variable, const Expression& expression )
  204. {
  205. return variable + -expression;
  206. }
  207. inline
  208. Expression operator-( const Variable& variable, const Term& term )
  209. {
  210. return variable + -term;
  211. }
  212. inline
  213. Expression operator-( const Variable& first, const Variable& second )
  214. {
  215. return first + -second;
  216. }
  217. inline
  218. Expression operator-( const Variable& variable, double constant )
  219. {
  220. return variable + -constant;
  221. }
  222. // Double add and subtract
  223. inline
  224. Expression operator+( double constant, const Expression& expression )
  225. {
  226. return expression + constant;
  227. }
  228. inline
  229. Expression operator+( double constant, const Term& term )
  230. {
  231. return term + constant;
  232. }
  233. inline
  234. Expression operator+( double constant, const Variable& variable )
  235. {
  236. return variable + constant;
  237. }
  238. inline
  239. Expression operator-( double constant, const Expression& expression )
  240. {
  241. return -expression + constant;
  242. }
  243. inline
  244. Expression operator-( double constant, const Term& term )
  245. {
  246. return -term + constant;
  247. }
  248. inline
  249. Expression operator-( double constant, const Variable& variable )
  250. {
  251. return -variable + constant;
  252. }
  253. // Expression relations
  254. inline
  255. Constraint operator==( const Expression& first, const Expression& second )
  256. {
  257. return Constraint( first - second, OP_EQ );
  258. }
  259. inline
  260. Constraint operator==( const Expression& expression, const Term& term )
  261. {
  262. return expression == Expression( term );
  263. }
  264. inline
  265. Constraint operator==( const Expression& expression, const Variable& variable )
  266. {
  267. return expression == Term( variable );
  268. }
  269. inline
  270. Constraint operator==( const Expression& expression, double constant )
  271. {
  272. return expression == Expression( constant );
  273. }
  274. inline
  275. Constraint operator<=( const Expression& first, const Expression& second )
  276. {
  277. return Constraint( first - second, OP_LE );
  278. }
  279. inline
  280. Constraint operator<=( const Expression& expression, const Term& term )
  281. {
  282. return expression <= Expression( term );
  283. }
  284. inline
  285. Constraint operator<=( const Expression& expression, const Variable& variable )
  286. {
  287. return expression <= Term( variable );
  288. }
  289. inline
  290. Constraint operator<=( const Expression& expression, double constant )
  291. {
  292. return expression <= Expression( constant );
  293. }
  294. inline
  295. Constraint operator>=( const Expression& first, const Expression& second )
  296. {
  297. return Constraint( first - second, OP_GE );
  298. }
  299. inline
  300. Constraint operator>=( const Expression& expression, const Term& term )
  301. {
  302. return expression >= Expression( term );
  303. }
  304. inline
  305. Constraint operator>=( const Expression& expression, const Variable& variable )
  306. {
  307. return expression >= Term( variable );
  308. }
  309. inline
  310. Constraint operator>=( const Expression& expression, double constant )
  311. {
  312. return expression >= Expression( constant );
  313. }
  314. // Term relations
  315. inline
  316. Constraint operator==( const Term& term, const Expression& expression )
  317. {
  318. return expression == term;
  319. }
  320. inline
  321. Constraint operator==( const Term& first, const Term& second )
  322. {
  323. return Expression( first ) == second;
  324. }
  325. inline
  326. Constraint operator==( const Term& term, const Variable& variable )
  327. {
  328. return Expression( term ) == variable;
  329. }
  330. inline
  331. Constraint operator==( const Term& term, double constant )
  332. {
  333. return Expression( term ) == constant;
  334. }
  335. inline
  336. Constraint operator<=( const Term& term, const Expression& expression )
  337. {
  338. return expression >= term;
  339. }
  340. inline
  341. Constraint operator<=( const Term& first, const Term& second )
  342. {
  343. return Expression( first ) <= second;
  344. }
  345. inline
  346. Constraint operator<=( const Term& term, const Variable& variable )
  347. {
  348. return Expression( term ) <= variable;
  349. }
  350. inline
  351. Constraint operator<=( const Term& term, double constant )
  352. {
  353. return Expression( term ) <= constant;
  354. }
  355. inline
  356. Constraint operator>=( const Term& term, const Expression& expression )
  357. {
  358. return expression <= term;
  359. }
  360. inline
  361. Constraint operator>=( const Term& first, const Term& second )
  362. {
  363. return Expression( first ) >= second;
  364. }
  365. inline
  366. Constraint operator>=( const Term& term, const Variable& variable )
  367. {
  368. return Expression( term ) >= variable;
  369. }
  370. inline
  371. Constraint operator>=( const Term& term, double constant )
  372. {
  373. return Expression( term ) >= constant;
  374. }
  375. // Variable relations
  376. inline
  377. Constraint operator==( const Variable& variable, const Expression& expression )
  378. {
  379. return expression == variable;
  380. }
  381. inline
  382. Constraint operator==( const Variable& variable, const Term& term )
  383. {
  384. return term == variable;
  385. }
  386. inline
  387. Constraint operator==( const Variable& first, const Variable& second )
  388. {
  389. return Term( first ) == second;
  390. }
  391. inline
  392. Constraint operator==( const Variable& variable, double constant )
  393. {
  394. return Term( variable ) == constant;
  395. }
  396. inline
  397. Constraint operator<=( const Variable& variable, const Expression& expression )
  398. {
  399. return expression >= variable;
  400. }
  401. inline
  402. Constraint operator<=( const Variable& variable, const Term& term )
  403. {
  404. return term >= variable;
  405. }
  406. inline
  407. Constraint operator<=( const Variable& first, const Variable& second )
  408. {
  409. return Term( first ) <= second;
  410. }
  411. inline
  412. Constraint operator<=( const Variable& variable, double constant )
  413. {
  414. return Term( variable ) <= constant;
  415. }
  416. inline
  417. Constraint operator>=( const Variable& variable, const Expression& expression )
  418. {
  419. return expression <= variable;
  420. }
  421. inline
  422. Constraint operator>=( const Variable& variable, const Term& term )
  423. {
  424. return term <= variable;
  425. }
  426. inline
  427. Constraint operator>=( const Variable& first, const Variable& second )
  428. {
  429. return Term( first ) >= second;
  430. }
  431. inline
  432. Constraint operator>=( const Variable& variable, double constant )
  433. {
  434. return Term( variable ) >= constant;
  435. }
  436. // Double relations
  437. inline
  438. Constraint operator==( double constant, const Expression& expression )
  439. {
  440. return expression == constant;
  441. }
  442. inline
  443. Constraint operator==( double constant, const Term& term )
  444. {
  445. return term == constant;
  446. }
  447. inline
  448. Constraint operator==( double constant, const Variable& variable )
  449. {
  450. return variable == constant;
  451. }
  452. inline
  453. Constraint operator<=( double constant, const Expression& expression )
  454. {
  455. return expression >= constant;
  456. }
  457. inline
  458. Constraint operator<=( double constant, const Term& term )
  459. {
  460. return term >= constant;
  461. }
  462. inline
  463. Constraint operator<=( double constant, const Variable& variable )
  464. {
  465. return variable >= constant;
  466. }
  467. inline
  468. Constraint operator>=( double constant, const Expression& expression )
  469. {
  470. return expression <= constant;
  471. }
  472. inline
  473. Constraint operator>=( double constant, const Term& term )
  474. {
  475. return term <= constant;
  476. }
  477. inline
  478. Constraint operator>=( double constant, const Variable& variable )
  479. {
  480. return variable <= constant;
  481. }
  482. // Constraint strength modifier
  483. inline
  484. Constraint operator|( const Constraint& constraint, double strength )
  485. {
  486. return Constraint( constraint, strength );
  487. }
  488. inline
  489. Constraint operator|( double strength, const Constraint& constraint )
  490. {
  491. return constraint | strength;
  492. }
  493. } // namespace kiwi