symbolic_math.sf 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. #!/usr/bin/ruby
  2. # Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Date: 25 February 2017
  5. # https://github.com/trizen
  6. # Experimental implementation of symbolic mathematical identities.
  7. class Base() {}
  8. class Power() < Base {}
  9. class Rational() < Base {}
  10. class Sum() < Base {}
  11. class Product() < Base {}
  12. class Log() < Base {}
  13. class Exp() < Base {}
  14. class Symbol() < Base {}
  15. class Base {
  16. #const I = Exp(Rational(1, 2) * Log(-1))
  17. has I = Power(-1, Rational(1, 2))
  18. method sin {
  19. Rational(Exp(I * self) - Exp(-I * self), (2*I))
  20. }
  21. method cos {
  22. Rational(Exp(-I * self) + Exp(I * self), 2)
  23. }
  24. method i {
  25. Product(I, self)
  26. }
  27. }
  28. class Symbol(name) {
  29. method +(Object o) {
  30. Sum(self, o)
  31. }
  32. method -(Object o) {
  33. Sum(self, o.neg)
  34. }
  35. method *(Object o) {
  36. Product(self, o)
  37. }
  38. method /(Object o) {
  39. Product(self, o.inv)
  40. }
  41. method **(Object o) {
  42. Power(self, o)
  43. }
  44. method neg {
  45. Product(-1, self)
  46. }
  47. method inv {
  48. Rational(1, self)
  49. }
  50. method simplify {
  51. self
  52. }
  53. method ==(Symbol o) {
  54. name == o.name
  55. }
  56. method ==(_) {
  57. false
  58. }
  59. method to_s {
  60. "Symbol('#{name}')"
  61. }
  62. }
  63. class Rational(num, den) {
  64. method +(Number o) {
  65. self + Rational(o, 1)
  66. }
  67. method +(Rational o) {
  68. Rational(
  69. num*o.den + o.num*den,
  70. den*o.den
  71. )
  72. }
  73. method +(Object o) {
  74. Sum(self, o)
  75. }
  76. method -(Number o) {
  77. self + o.neg
  78. }
  79. method -(Rational o) {
  80. self + o.neg
  81. }
  82. method -(Object o) {
  83. Sum(self, o.neg)
  84. }
  85. method *(Number o) {
  86. Rational(num*o, den)
  87. }
  88. method *(Rational o) {
  89. Rational(num*o.num, den*o.den)
  90. }
  91. method *(Object o) {
  92. Product(self, o)
  93. }
  94. method /(Number o) {
  95. Rational(
  96. num,
  97. den * o
  98. )
  99. }
  100. method /(Rational o) {
  101. Rational(
  102. num * o.den,
  103. den * o.num,
  104. )
  105. }
  106. method /(Object o) {
  107. Product(self, o.inv)
  108. }
  109. method **(Number o) {
  110. if (o < 0) {
  111. var a = o.abs
  112. Rational(den**a, num**a)
  113. }
  114. else {
  115. Rational(num**o, den**o)
  116. }
  117. }
  118. method ==(Rational o) {
  119. (o.num == num) &&
  120. (o.den == den)
  121. }
  122. method ==(_) {
  123. false
  124. }
  125. method inv {
  126. Rational(den, num)
  127. }
  128. method neg {
  129. Rational(num.neg, den)
  130. }
  131. method simplify {
  132. Rational(num.simplify, den.simplify)
  133. }
  134. method numeric {
  135. num.numeric / den.numeric
  136. }
  137. method to_s {
  138. "Rational(#{num}, #{den})"
  139. }
  140. }
  141. class Sum(*values) {
  142. method -(Sum o) {
  143. Sum(values..., o.values.map{.neg}...)
  144. }
  145. method -(Object o) {
  146. var copy = [values...]
  147. #~ if (copy.remove_first(o)) {
  148. #~ Sum(copy...)
  149. #~ }
  150. #~ else {
  151. Sum(copy..., o.neg)
  152. #~ }
  153. }
  154. method +(Sum o) {
  155. Sum(values..., o.values...)
  156. }
  157. method +(Object o) {
  158. var copy = [values...]
  159. #~ if (copy.remove_first(o.neg)) {
  160. #~ Sum(copy...)
  161. #~ }
  162. #~ else {
  163. Sum(copy..., o)
  164. #~ }
  165. }
  166. #~ method *(Sum o) {
  167. #~ var terms = []
  168. #~ for i,j in (values ~X o.values) {
  169. #~ terms << i*j
  170. #~ }
  171. #~ Sum(terms...)
  172. #~ }
  173. method *(Object o) {
  174. Sum(values.map { _ * o }...)
  175. }
  176. method /(Object o) {
  177. Sum(values.map { _ / o }...)
  178. }
  179. method simplify {
  180. var simple = values.map { .simplify }
  181. var types = simple.map{ .ref }.uniq
  182. var arr = (
  183. gather {
  184. for type in types {
  185. take(simple.grep { .kind_of(type) }.reduce('+'))
  186. }
  187. }
  188. )
  189. arr.len == 1 ? arr[0] : arr.reduce('+')
  190. }
  191. method inv {
  192. Rational(1, self)
  193. }
  194. method neg {
  195. Sum(values.map{.neg}...)
  196. }
  197. method numeric {
  198. values.map { .numeric }.sum
  199. }
  200. method to_s {
  201. "Sum(#{values.join(', ')})"
  202. }
  203. }
  204. class Product(*values) {
  205. method *(Product o) {
  206. Product(values..., o.values...)
  207. }
  208. method /(Product o) {
  209. Product(values..., o.values.map{.inv}...)
  210. }
  211. method /(Object o) {
  212. var copy = [values...]
  213. #~ if (copy.remove_first(o)) {
  214. #~ Product(copy...)
  215. #~ }
  216. #~ else {
  217. Product(copy..., o.inv)
  218. #~ }
  219. }
  220. method *(Object o) {
  221. var copy = [values...]
  222. #~ if (copy.remove_first(o.inv)) {
  223. #~ Product(copy...)
  224. #~ }
  225. #~ else {
  226. Product(copy..., o)
  227. #~ }
  228. }
  229. method **(Object o) {
  230. Product(values.map{ _ ** o }...)
  231. }
  232. method +(Object o) {
  233. Sum(self, o)
  234. }
  235. method -(Object o) {
  236. Sum(self, o.neg)
  237. }
  238. method neg {
  239. if (values) {
  240. Product(values[0].neg, values.slice(1)...)
  241. }
  242. else {
  243. Product(-1)
  244. }
  245. }
  246. method simplify {
  247. var simple = values.map { .simplify }
  248. var types = simple.map{ .ref }.uniq
  249. var arr = (
  250. gather {
  251. for type in types {
  252. take(simple.grep { .kind_of(type) }.reduce('*'))
  253. }
  254. }
  255. )
  256. arr.len == 1 ? arr[0] : arr.reduce('*')
  257. }
  258. method inv {
  259. Product(values.map { .inv }...)
  260. }
  261. method numeric {
  262. values.map { .numeric }.prod
  263. }
  264. method to_s {
  265. "Product(#{values.join(', ')})"
  266. }
  267. }
  268. class Exp(v) {
  269. method *(Exp o) {
  270. Exp(v + o.v)
  271. }
  272. method *(Object o) {
  273. Product(self, o)
  274. }
  275. method /(Exp o) {
  276. Exp(v + o.v.neg)
  277. }
  278. method /(Object o) {
  279. o.inv * self
  280. }
  281. method +(Object o) {
  282. Sum(self, o)
  283. }
  284. method -(Object o) {
  285. Sum(self, o.neg)
  286. }
  287. method **(Object o) {
  288. Exp(v * o)
  289. }
  290. method simplify {
  291. var simple = v.simplify
  292. given() {
  293. case (simple.kind_of(Log)) {
  294. simple.v
  295. }
  296. case (simple.kind_of(Product) && (simple.values.len == 2)) {
  297. var (a, b) = simple.values...
  298. if (a.class == :Log) {
  299. Power(a.v, b)
  300. }
  301. elsif (b.class == :Log) {
  302. Power(b.v, a)
  303. }
  304. else {
  305. continue
  306. }
  307. }
  308. default {
  309. Exp(simple)
  310. }
  311. }
  312. }
  313. method neg {
  314. Product(-1, self)
  315. }
  316. method numeric {
  317. v.numeric.exp
  318. }
  319. method to_s {
  320. "Exp(#{v})"
  321. }
  322. }
  323. class Log(v) {
  324. method *(Object o) {
  325. #Rational(self, o.inv)
  326. Product(self, o)
  327. #Log(Power(v, o))
  328. #o * self
  329. }
  330. method /(Object o) {
  331. #Rational(self, o)
  332. o.inv * self
  333. #Product(self, o.inv)
  334. #Log(Power(v, o.inv))
  335. }
  336. method +(Log o) {
  337. Log(v * o.v)
  338. }
  339. method +(Object o) {
  340. Sum(self, o)
  341. }
  342. method -(Log o) {
  343. Log(v / o.v)
  344. }
  345. method -(Object o) {
  346. Sum(self, o.neg)
  347. }
  348. method inv {
  349. Rational(1, self)
  350. }
  351. method neg {
  352. Product(-1, self)
  353. }
  354. method simplify {
  355. var simple = v.simplify
  356. if (simple.kind_of(Exp)) {
  357. simple.v
  358. }
  359. else {
  360. Log(simple)
  361. }
  362. }
  363. method numeric {
  364. v.numeric.log
  365. }
  366. method to_s {
  367. "Log(#{v})"
  368. }
  369. }
  370. class Power(v, n) {
  371. method +(Object o) {
  372. Sum(self, o)
  373. }
  374. method -(Object o) {
  375. Sum(self, o.neg)
  376. }
  377. method **(Number o) {
  378. Power(v, n * o)
  379. }
  380. method **(Rational o) {
  381. Power(v, n * o)
  382. }
  383. method ==(Power o) {
  384. (v == o.v) &&
  385. (n == o.n)
  386. }
  387. method /(Power o) {
  388. #~ if (o.v == v) {
  389. #~ Power(v, n - o.n)
  390. #~ }
  391. #~ else {
  392. Product(self, o.inv)
  393. #~ }
  394. }
  395. method /(Object o) {
  396. Product(self, o.inv)
  397. }
  398. method *(Power o) {
  399. #~ if (o.v == v) {
  400. #~ Power(v, n + o.n)
  401. #~ }
  402. #~ else {
  403. Product(self, o)
  404. #~ }
  405. }
  406. method *(Object o) {
  407. Product(self, o)
  408. }
  409. method ==(_) {
  410. false
  411. }
  412. method inv {
  413. Power(v, n.neg)
  414. }
  415. method neg {
  416. Product(-1, self)
  417. }
  418. method simplify {
  419. Power(v.simplify, n.simplify)
  420. }
  421. method numeric {
  422. v.numeric ** n.numeric
  423. }
  424. method to_s {
  425. "Power(#{v}, #{n})"
  426. }
  427. }
  428. class Number {
  429. # Addition
  430. method +(Product o) {
  431. o + self
  432. }
  433. method +(Sum o) {
  434. o + self
  435. }
  436. method +(Power o) {
  437. o + self
  438. }
  439. method +(Log o) {
  440. o + self
  441. }
  442. method +(Symbol o) {
  443. o + self
  444. }
  445. method +(Rational o) {
  446. o + self
  447. }
  448. method +(Exp o) {
  449. o + self
  450. }
  451. # Subtraction
  452. method -(Rational o) {
  453. o.neg + self
  454. }
  455. method -(Product o) {
  456. o.neg + self
  457. }
  458. method -(Symbol o) {
  459. o.neg + self
  460. }
  461. method -(Sum o) {
  462. o.neg + self
  463. }
  464. method -(Log o) {
  465. o.neg + self
  466. }
  467. method -(Power o) {
  468. o.neg + self
  469. }
  470. method -(Exp o) {
  471. o.neg + self
  472. }
  473. # Multiplication
  474. method *(Rational o) {
  475. o * self
  476. }
  477. method *(Log o) {
  478. o * self
  479. }
  480. method *(Product o) {
  481. o * self
  482. }
  483. method *(Power o) {
  484. o * self
  485. }
  486. method *(Symbol o) {
  487. o * self
  488. }
  489. method *(Sum o) {
  490. o * self
  491. }
  492. method *(Exp o) {
  493. o * self
  494. }
  495. # Division
  496. method /(Sum o) {
  497. o.inv * self
  498. }
  499. method /(Product o) {
  500. o.inv * self
  501. }
  502. method /(Rational o) {
  503. o.inv * self
  504. }
  505. method /(Log o) {
  506. o.inv * self
  507. }
  508. method /(Power o) {
  509. o.inv * self
  510. }
  511. method /(Symbol o) {
  512. o.inv * self
  513. }
  514. method /(Exp o) {
  515. o.inv * self
  516. }
  517. method simplify {
  518. self
  519. }
  520. method numeric {
  521. self
  522. }
  523. }
  524. if (__MAIN__ == __FILE__) {
  525. var n = Power(5, Rational(1,2))
  526. say n
  527. say n**2
  528. say n**3
  529. say n**Rational(2, 1)
  530. say Exp(Log(5) * 3).simplify
  531. say Power(3, Log(Exp(Sum(3, 4)))).simplify
  532. say Log(42)-Log(6)
  533. say Product(3, 4, 5)*Rational(1, 3)
  534. say Log(42)/Log(6)
  535. say Log(Symbol(:x))
  536. say Log(Symbol(:x))+Log(Symbol(:y))
  537. say Rational(Symbol(:a), Symbol(:b))+Rational(Symbol(:c), Symbol(:d))
  538. say "\n=> Summing..."
  539. var sum = Rational(0, 1)
  540. for n in (0..10) {
  541. sum += Rational(1, n!)
  542. #sum += Rational(Symbol(:x), n!)
  543. #sum += Power(n!, Rational(1, 2)).inv
  544. say sum
  545. }
  546. try {
  547. say sum.numeric
  548. }
  549. }