symbolics.h 12 KB

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