math-expression-evaluator.js 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. (function webpackUniversalModuleDefinition(root, factory) {
  2. if(typeof exports === 'object' && typeof module === 'object')
  3. module.exports = factory();
  4. else if(typeof define === 'function' && define.amd)
  5. define([], factory);
  6. else if(typeof exports === 'object')
  7. exports["Mexp"] = factory();
  8. else
  9. root["Mexp"] = factory();
  10. })(self, function() {
  11. return /******/ (() => { // webpackBootstrap
  12. /******/ var __webpack_modules__ = ({
  13. /***/ 28:
  14. /***/ ((module, __unused_webpack_exports, __webpack_require__) => {
  15. var Mexp=__webpack_require__(733);
  16. Mexp.prototype.formulaEval = function () {
  17. "use strict";
  18. var stack=[],pop1,pop2,pop3;
  19. var disp=[];
  20. var temp='';
  21. var arr=this.value;
  22. for(var i=0;i<arr.length;i++){
  23. if(arr[i].type===1||arr[i].type===3){
  24. disp.push({value:arr[i].type===3?arr[i].show:arr[i].value,type:1});
  25. }
  26. else if(arr[i].type===13){
  27. disp.push({value:arr[i].show,type:1});
  28. }
  29. else if(arr[i].type===0){
  30. disp[disp.length-1]={value:arr[i].show+(arr[i].show!="-"?"(":"")+disp[disp.length-1].value+(arr[i].show!="-"?")":""),type:0};
  31. }
  32. else if(arr[i].type===7){
  33. disp[disp.length-1]={value:(disp[disp.length-1].type!=1?"(":"")+disp[disp.length-1].value+(disp[disp.length-1].type!=1?")":"")+arr[i].show,type:7};
  34. }
  35. else if(arr[i].type===10){
  36. pop1=disp.pop();
  37. pop2=disp.pop();
  38. if(arr[i].show==='P'||arr[i].show==='C')disp.push({value:"<sup>"+pop2.value+"</sup>"+arr[i].show+"<sub>"+pop1.value+"</sub>",type:10});
  39. else disp.push({value:(pop2.type!=1?"(":"")+pop2.value+(pop2.type!=1?")":"")+"<sup>"+pop1.value+"</sup>",type:1});
  40. }
  41. else if(arr[i].type===2||arr[i].type===9){
  42. pop1=disp.pop();
  43. pop2=disp.pop();
  44. disp.push({value:(pop2.type!=1?"(":"")+pop2.value+(pop2.type!=1?")":"")+arr[i].show+(pop1.type!=1?"(":"")+pop1.value+(pop1.type!=1?")":""),type:arr[i].type});
  45. }
  46. else if(arr[i].type===12){
  47. pop1=disp.pop();
  48. pop2=disp.pop();
  49. pop3=disp.pop();
  50. disp.push({value:arr[i].show+"("+pop3.value+","+pop2.value+","+pop1.value+")",type:12});
  51. }
  52. }
  53. return disp[0].value;
  54. };
  55. module.exports=Mexp;
  56. /***/ }),
  57. /***/ 618:
  58. /***/ ((module, __unused_webpack_exports, __webpack_require__) => {
  59. "use strict";
  60. var Mexp = __webpack_require__(178)
  61. function inc (arr, val) {
  62. for (var i = 0; i < arr.length; i++) {
  63. arr[i] += val
  64. }
  65. return arr
  66. }
  67. var token = [
  68. 'sin',
  69. 'cos',
  70. 'tan',
  71. 'pi',
  72. '(',
  73. ')',
  74. 'P',
  75. 'C',
  76. ' ',
  77. 'asin',
  78. 'acos',
  79. 'atan',
  80. '7',
  81. '8',
  82. '9',
  83. 'int',
  84. 'cosh',
  85. 'acosh',
  86. 'ln',
  87. '^',
  88. 'root',
  89. '4',
  90. '5',
  91. '6',
  92. '/',
  93. '!',
  94. 'tanh',
  95. 'atanh',
  96. 'Mod',
  97. '1',
  98. '2',
  99. '3',
  100. '*',
  101. 'sinh',
  102. 'asinh',
  103. 'e',
  104. 'log',
  105. '0',
  106. '.',
  107. '+',
  108. '-',
  109. ',',
  110. 'Sigma',
  111. 'n',
  112. 'Pi',
  113. 'pow',
  114. '&'
  115. ]
  116. var show = [
  117. 'sin',
  118. 'cos',
  119. 'tan',
  120. '&pi;',
  121. '(',
  122. ')',
  123. 'P',
  124. 'C',
  125. ' ',
  126. 'asin',
  127. 'acos',
  128. 'atan',
  129. '7',
  130. '8',
  131. '9',
  132. 'Int',
  133. 'cosh',
  134. 'acosh',
  135. ' ln',
  136. '^',
  137. 'root',
  138. '4',
  139. '5',
  140. '6',
  141. '&divide;',
  142. '!',
  143. 'tanh',
  144. 'atanh',
  145. ' Mod ',
  146. '1',
  147. '2',
  148. '3',
  149. '&times;',
  150. 'sinh',
  151. 'asinh',
  152. 'e',
  153. ' log',
  154. '0',
  155. '.',
  156. '+',
  157. '-',
  158. ',',
  159. '&Sigma;',
  160. 'n',
  161. '&Pi;',
  162. 'pow',
  163. '&'
  164. ]
  165. var eva = [
  166. Mexp.math.sin,
  167. Mexp.math.cos,
  168. Mexp.math.tan,
  169. 'PI',
  170. '(',
  171. ')',
  172. Mexp.math.P,
  173. Mexp.math.C,
  174. ' '.anchor,
  175. Mexp.math.asin,
  176. Mexp.math.acos,
  177. Mexp.math.atan,
  178. '7',
  179. '8',
  180. '9',
  181. Math.floor,
  182. Mexp.math.cosh,
  183. Mexp.math.acosh,
  184. Math.log,
  185. Math.pow,
  186. Math.sqrt,
  187. '4',
  188. '5',
  189. '6',
  190. Mexp.math.div,
  191. Mexp.math.fact,
  192. Mexp.math.tanh,
  193. Mexp.math.atanh,
  194. Mexp.math.mod,
  195. '1',
  196. '2',
  197. '3',
  198. Mexp.math.mul,
  199. Mexp.math.sinh,
  200. Mexp.math.asinh,
  201. 'E',
  202. Mexp.math.log,
  203. '0',
  204. '.',
  205. Mexp.math.add,
  206. Mexp.math.sub,
  207. ',',
  208. Mexp.math.sigma,
  209. 'n',
  210. Mexp.math.Pi,
  211. Math.pow,
  212. Mexp.math.and
  213. ]
  214. var preced = {
  215. 0: 11,
  216. 1: 0,
  217. 2: 3,
  218. 3: 0,
  219. 4: 0,
  220. 5: 0,
  221. 6: 0,
  222. 7: 11,
  223. 8: 11,
  224. 9: 1,
  225. 10: 10,
  226. 11: 0,
  227. 12: 11,
  228. 13: 0,
  229. 14: -1 // will be filtered after lexer
  230. } // stores precedence by types
  231. var type = [
  232. 0, 0, 0, 3, 4, 5, 10, 10, 14, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 10, 0, 1, 1, 1, 2, 7, 0, 0, 2, 1, 1,
  233. 1, 2, 0, 0, 3, 0, 1, 6, 9, 9, 11, 12, 13, 12, 8, 9
  234. ]
  235. /*
  236. 0 : function with syntax function_name(Maths_exp)
  237. 1 : numbers
  238. 2 : binary operators like * / Mod left associate and same precedence
  239. 3 : Math constant values like e,pi,Cruncher ans
  240. 4 : opening bracket
  241. 5 : closing bracket
  242. 6 : decimal
  243. 7 : function with syntax (Math_exp)function_name
  244. 8: function with syntax function_name(Math_exp1,Math_exp2)
  245. 9 : binary operator like +,-
  246. 10: binary operator like P C or ^
  247. 11: ,
  248. 12: function with , seperated three parameters and third parameter is a string that will be mexp string
  249. 13: variable of Sigma function
  250. */
  251. var type0 = {
  252. 0: true,
  253. 1: true,
  254. 3: true,
  255. 4: true,
  256. 6: true,
  257. 8: true,
  258. 9: true,
  259. 12: true,
  260. 13: true,
  261. 14: true
  262. } // type2:true,type4:true,type9:true,type11:true,type21:true,type22
  263. var type1 = {
  264. 0: true,
  265. 1: true,
  266. 2: true,
  267. 3: true,
  268. 4: true,
  269. 5: true,
  270. 6: true,
  271. 7: true,
  272. 8: true,
  273. 9: true,
  274. 10: true,
  275. 11: true,
  276. 12: true,
  277. 13: true
  278. } // type3:true,type5:true,type7:true,type23
  279. var type1Asterick = {
  280. 0: true,
  281. 3: true,
  282. 4: true,
  283. 8: true,
  284. 12: true,
  285. 13: true
  286. }
  287. var empty = {}
  288. var type3Asterick = {
  289. 0: true,
  290. 1: true,
  291. 3: true,
  292. 4: true,
  293. 6: true,
  294. 8: true,
  295. 12: true,
  296. 13: true
  297. } // type_5:true,type_7:true,type_23
  298. var type6 = {
  299. 1: true
  300. }
  301. var newAr = [
  302. [],
  303. [
  304. '1',
  305. '2',
  306. '3',
  307. '7',
  308. '8',
  309. '9',
  310. '4',
  311. '5',
  312. '6',
  313. '+',
  314. '-',
  315. '*',
  316. '/',
  317. '(',
  318. ')',
  319. '^',
  320. '!',
  321. 'P',
  322. 'C',
  323. 'e',
  324. '0',
  325. '.',
  326. ',',
  327. 'n',
  328. ' ',
  329. '&'
  330. ],
  331. ['pi', 'ln', 'Pi'],
  332. ['sin', 'cos', 'tan', 'Del', 'int', 'Mod', 'log', 'pow'],
  333. ['asin', 'acos', 'atan', 'cosh', 'root', 'tanh', 'sinh'],
  334. ['acosh', 'atanh', 'asinh', 'Sigma']
  335. ]
  336. function match (str1, str2, i, x) {
  337. for (var f = 0; f < x; f++) {
  338. if (str1[i + f] !== str2[f]) {
  339. return false
  340. }
  341. }
  342. return true
  343. }
  344. Mexp.addToken = function (tokens) {
  345. for (var i = 0; i < tokens.length; i++) {
  346. var x = tokens[i].token.length
  347. var temp = -1
  348. // newAr is a specially designed data structure index of 1d array = length of tokens
  349. newAr[x] = newAr[x] || []
  350. for (var y = 0; y < newAr[x].length; y++) {
  351. if (tokens[i].token === newAr[x][y]) {
  352. temp = token.indexOf(newAr[x][y])
  353. break
  354. }
  355. }
  356. if (temp === -1) {
  357. token.push(tokens[i].token)
  358. type.push(tokens[i].type)
  359. if (newAr.length <= tokens[i].token.length) {
  360. newAr[tokens[i].token.length] = []
  361. }
  362. newAr[tokens[i].token.length].push(tokens[i].token)
  363. eva.push(tokens[i].value)
  364. show.push(tokens[i].show)
  365. } else {
  366. // overwrite
  367. token[temp] = tokens[i].token
  368. type[temp] = tokens[i].type
  369. eva[temp] = tokens[i].value
  370. show[temp] = tokens[i].show
  371. }
  372. }
  373. }
  374. function tokenize (string) {
  375. var nodes = []
  376. var length = string.length
  377. var key, x, y
  378. for (var i = 0; i < length; i++) {
  379. if (i < length - 1 && string[i] === ' ' && string[i + 1] === ' ') {
  380. continue
  381. }
  382. key = ''
  383. for (
  384. x = string.length - i > newAr.length - 2 ? newAr.length - 1 : string.length - i;
  385. x > 0;
  386. x--
  387. ) {
  388. if (newAr[x] === undefined) continue
  389. for (y = 0; y < newAr[x].length; y++) {
  390. if (match(string, newAr[x][y], i, x)) {
  391. key = newAr[x][y]
  392. y = newAr[x].length
  393. x = 0
  394. }
  395. }
  396. }
  397. i += key.length - 1
  398. if (key === '') {
  399. throw new Mexp.Exception("Can't understand after " + string.slice(i))
  400. }
  401. var index = token.indexOf(key)
  402. nodes.push({
  403. index: index,
  404. token: key,
  405. type: type[index],
  406. eval: eva[index],
  407. precedence: preced[type[index]],
  408. show: show[index]
  409. })
  410. }
  411. return nodes
  412. }
  413. Mexp.lex = function (inp, tokens) {
  414. 'use strict'
  415. var changeSignObj = {
  416. value: Mexp.math.changeSign,
  417. type: 0,
  418. pre: 21,
  419. show: '-'
  420. }
  421. var closingParObj = {
  422. value: ')',
  423. show: ')',
  424. type: 5,
  425. pre: 0
  426. }
  427. var openingParObj = {
  428. value: '(',
  429. type: 4,
  430. pre: 0,
  431. show: '('
  432. }
  433. var str = [openingParObj]
  434. var ptc = [] // Parenthesis to close at the beginning is after one token
  435. var inpStr = inp
  436. var allowed = type0
  437. var bracToClose = 0
  438. var asterick = empty
  439. var prevKey = ''
  440. var i
  441. if (typeof tokens !== 'undefined') {
  442. Mexp.addToken(tokens)
  443. }
  444. var obj = {}
  445. var nodes = tokenize(inpStr)
  446. for (i = 0; i < nodes.length; i++) {
  447. var node = nodes[i]
  448. if (node.type === 14) {
  449. if (
  450. i > 0 &&
  451. i < nodes.length - 1 &&
  452. nodes[i + 1].type === 1 &&
  453. (nodes[i - 1].type === 1 || nodes[i - 1].type === 6)
  454. ) { throw new Mexp.Exception('Unexpected Space') }
  455. continue
  456. }
  457. var cToken = node.token
  458. var cType = node.type
  459. var cEv = node.eval
  460. var cPre = node.precedence
  461. var cShow = node.show
  462. var pre = str[str.length - 1]
  463. var j
  464. for (j = ptc.length; j--;) {
  465. // loop over ptc
  466. if (ptc[j] === 0) {
  467. if ([0, 2, 3, 4, 5, 9, 11, 12, 13].indexOf(cType) !== -1) {
  468. if (allowed[cType] !== true) {
  469. throw new Mexp.Exception(cToken + ' is not allowed after ' + prevKey)
  470. }
  471. str.push(closingParObj)
  472. allowed = type1
  473. asterick = type3Asterick
  474. ptc.pop()
  475. }
  476. } else break
  477. }
  478. if (allowed[cType] !== true) {
  479. throw new Mexp.Exception(cToken + ' is not allowed after ' + prevKey)
  480. }
  481. if (asterick[cType] === true) {
  482. cType = 2
  483. cEv = Mexp.math.mul
  484. cShow = '&times;'
  485. cPre = 3
  486. i = i - 1
  487. }
  488. obj = {
  489. value: cEv,
  490. type: cType,
  491. pre: cPre,
  492. show: cShow
  493. }
  494. if (cType === 0) {
  495. allowed = type0
  496. asterick = empty
  497. inc(ptc, 2)
  498. str.push(obj)
  499. if (nodes[i + 1].type !== 4) {
  500. str.push(openingParObj)
  501. ptc.push(2)
  502. }
  503. // bracToClose++
  504. } else if (cType === 1) {
  505. if (pre.type === 1) {
  506. pre.value += cEv
  507. inc(ptc, 1)
  508. } else {
  509. str.push(obj)
  510. }
  511. allowed = type1
  512. asterick = type1Asterick
  513. } else if (cType === 2) {
  514. allowed = type0
  515. asterick = empty
  516. inc(ptc, 2)
  517. str.push(obj)
  518. } else if (cType === 3) {
  519. // constant
  520. str.push(obj)
  521. allowed = type1
  522. asterick = type3Asterick
  523. } else if (cType === 4) {
  524. inc(ptc, 1)
  525. bracToClose++
  526. allowed = type0
  527. asterick = empty
  528. str.push(obj)
  529. } else if (cType === 5) {
  530. if (!bracToClose) {
  531. throw new Mexp.Exception('Closing parenthesis are more than opening one, wait What!!!')
  532. }
  533. bracToClose--
  534. allowed = type1
  535. asterick = type3Asterick
  536. str.push(obj)
  537. inc(ptc, 1)
  538. } else if (cType === 6) {
  539. if (pre.hasDec) {
  540. throw new Mexp.Exception('Two decimals are not allowed in one number')
  541. }
  542. if (pre.type !== 1) {
  543. pre = {
  544. value: 0,
  545. type: 1,
  546. pre: 0
  547. } // pre needs to be changed as it will the last value now to be safe in later code
  548. str.push(pre)
  549. // inc(ptc, 1)
  550. }
  551. allowed = type6
  552. inc(ptc, 1)
  553. asterick = empty
  554. pre.value += cEv
  555. pre.hasDec = true
  556. } else if (cType === 7) {
  557. allowed = type1
  558. asterick = type3Asterick
  559. inc(ptc, 1)
  560. str.push(obj)
  561. }
  562. if (cType === 8) {
  563. allowed = type0
  564. asterick = empty
  565. inc(ptc, 4)
  566. str.push(obj)
  567. // str.push(openingParObj)
  568. if (nodes[i + 1].type !== 4) {
  569. str.push(openingParObj)
  570. ptc.push(4)
  571. }
  572. } else if (cType === 9) {
  573. if (pre.type === 9) {
  574. if (pre.value === Mexp.math.add) {
  575. pre.value = cEv
  576. pre.show = cShow
  577. inc(ptc, 1)
  578. } else if (pre.value === Mexp.math.sub && cShow === '-') {
  579. pre.value = Mexp.math.add
  580. pre.show = '+'
  581. inc(ptc, 1)
  582. }
  583. } else if (
  584. pre.type !== 5 &&
  585. pre.type !== 7 &&
  586. pre.type !== 1 &&
  587. pre.type !== 3 &&
  588. pre.type !== 13
  589. ) {
  590. // changesign only when negative is found
  591. if (cToken === '-') {
  592. // do nothing for + token
  593. // don't add with the above if statement as that will run the else statement of parent if on Ctoken +
  594. allowed = type0
  595. asterick = empty
  596. inc(ptc, 2).push(2)
  597. str.push(changeSignObj)
  598. str.push(openingParObj)
  599. }
  600. } else {
  601. str.push(obj)
  602. inc(ptc, 2)
  603. }
  604. allowed = type0
  605. asterick = empty
  606. } else if (cType === 10) {
  607. allowed = type0
  608. asterick = empty
  609. inc(ptc, 2)
  610. str.push(obj)
  611. } else if (cType === 11) {
  612. allowed = type0
  613. asterick = empty
  614. str.push(obj)
  615. } else if (cType === 12) {
  616. allowed = type0
  617. asterick = empty
  618. inc(ptc, 6)
  619. str.push(obj)
  620. if (nodes[i + 1].type !== 4) {
  621. str.push(openingParObj)
  622. ptc.push(6)
  623. }
  624. } else if (cType === 13) {
  625. allowed = type1
  626. asterick = type3Asterick
  627. str.push(obj)
  628. }
  629. inc(ptc, -1)
  630. prevKey = cToken
  631. }
  632. for (j = ptc.length; j--;) {
  633. // loop over ptc
  634. str.push(closingParObj)
  635. }
  636. if (allowed[5] !== true) {
  637. throw new Mexp.Exception('complete the expression')
  638. }
  639. while (bracToClose--) {
  640. str.push(closingParObj)
  641. }
  642. str.push(closingParObj)
  643. // console.log(str);
  644. return new Mexp(str)
  645. }
  646. module.exports = Mexp
  647. /***/ }),
  648. /***/ 178:
  649. /***/ ((module) => {
  650. "use strict";
  651. var Mexp = function (parsed) {
  652. this.value = parsed
  653. }
  654. Mexp.math = {
  655. isDegree: true, // mode of calculator
  656. acos: function (x) {
  657. return (Mexp.math.isDegree ? 180 / Math.PI * Math.acos(x) : Math.acos(x))
  658. },
  659. add: function (a, b) {
  660. return a + b
  661. },
  662. asin: function (x) {
  663. return (Mexp.math.isDegree ? 180 / Math.PI * Math.asin(x) : Math.asin(x))
  664. },
  665. atan: function (x) {
  666. return (Mexp.math.isDegree ? 180 / Math.PI * Math.atan(x) : Math.atan(x))
  667. },
  668. acosh: function (x) {
  669. return Math.log(x + Math.sqrt(x * x - 1))
  670. },
  671. asinh: function (x) {
  672. return Math.log(x + Math.sqrt(x * x + 1))
  673. },
  674. atanh: function (x) {
  675. return Math.log((1 + x) / (1 - x))
  676. },
  677. C: function (n, r) {
  678. var pro = 1
  679. var other = n - r
  680. var choice = r
  681. if (choice < other) {
  682. choice = other
  683. other = r
  684. }
  685. for (var i = choice + 1; i <= n; i++) {
  686. pro *= i
  687. }
  688. return pro / Mexp.math.fact(other)
  689. },
  690. changeSign: function (x) {
  691. return -x
  692. },
  693. cos: function (x) {
  694. if (Mexp.math.isDegree) x = Mexp.math.toRadian(x)
  695. return Math.cos(x)
  696. },
  697. cosh: function (x) {
  698. return (Math.pow(Math.E, x) + Math.pow(Math.E, -1 * x)) / 2
  699. },
  700. div: function (a, b) {
  701. return a / b
  702. },
  703. fact: function (n) {
  704. if (n % 1 !== 0) return 'NaN'
  705. var pro = 1
  706. for (var i = 2; i <= n; i++) {
  707. pro *= i
  708. }
  709. return pro
  710. },
  711. inverse: function (x) {
  712. return 1 / x
  713. },
  714. log: function (i) {
  715. return Math.log(i) / Math.log(10)
  716. },
  717. mod: function (a, b) {
  718. return a % b
  719. },
  720. mul: function (a, b) {
  721. return a * b
  722. },
  723. P: function (n, r) {
  724. var pro = 1
  725. for (var i = Math.floor(n) - Math.floor(r) + 1; i <= Math.floor(n); i++) {
  726. pro *= i
  727. }
  728. return pro
  729. },
  730. Pi: function (low, high, ex) {
  731. var pro = 1
  732. for (var i = low; i <= high; i++) {
  733. pro *= Number(ex.postfixEval({
  734. n: i
  735. }))
  736. }
  737. return pro
  738. },
  739. pow10x: function (e) {
  740. var x = 1
  741. while (e--) {
  742. x *= 10
  743. }
  744. return x
  745. },
  746. sigma: function (low, high, ex) {
  747. var sum = 0
  748. for (var i = low; i <= high; i++) {
  749. sum += Number(ex.postfixEval({
  750. n: i
  751. }))
  752. }
  753. return sum
  754. },
  755. sin: function (x) {
  756. if (Mexp.math.isDegree) x = Mexp.math.toRadian(x)
  757. return Math.sin(x)
  758. },
  759. sinh: function (x) {
  760. return (Math.pow(Math.E, x) - Math.pow(Math.E, -1 * x)) / 2
  761. },
  762. sub: function (a, b) {
  763. return a - b
  764. },
  765. tan: function (x) {
  766. if (Mexp.math.isDegree) x = Mexp.math.toRadian(x)
  767. return Math.tan(x)
  768. },
  769. tanh: function (x) {
  770. return Mexp.sinha(x) / Mexp.cosha(x)
  771. },
  772. toRadian: function (x) {
  773. return x * Math.PI / 180
  774. },
  775. and: function (a, b) {
  776. return a & b
  777. }
  778. }
  779. Mexp.Exception = function (message) {
  780. this.message = message
  781. }
  782. module.exports = Mexp
  783. /***/ }),
  784. /***/ 477:
  785. /***/ ((module, __unused_webpack_exports, __webpack_require__) => {
  786. var Mexp = __webpack_require__(618);
  787. Mexp.prototype.toPostfix = function () {
  788. 'use strict';
  789. var post = [], elem, popped, prep, pre, ele;
  790. var stack = [{ value: "(", type: 4, pre: 0 }];
  791. var arr = this.value;
  792. for (var i = 1; i < arr.length; i++) {
  793. if (arr[i].type === 1 || arr[i].type === 3 || arr[i].type === 13) { //if token is number,constant,or n(which is also a special constant in our case)
  794. if (arr[i].type === 1)
  795. arr[i].value = Number(arr[i].value);
  796. post.push(arr[i]);
  797. }
  798. else if (arr[i].type === 4) {
  799. stack.push(arr[i]);
  800. }
  801. else if (arr[i].type === 5) {
  802. while ((popped = stack.pop()).type !== 4) {
  803. post.push(popped);
  804. }
  805. }
  806. else if (arr[i].type === 11) {
  807. while ((popped = stack.pop()).type !== 4) {
  808. post.push(popped);
  809. }
  810. stack.push(popped);
  811. }
  812. else {
  813. elem = arr[i];
  814. pre = elem.pre;
  815. ele = stack[stack.length - 1];
  816. prep = ele.pre;
  817. var flag = ele.value == 'Math.pow' && elem.value == 'Math.pow';
  818. if (pre > prep) stack.push(elem);
  819. else {
  820. while (prep >= pre && !flag || flag && pre < prep) {
  821. popped = stack.pop();
  822. ele = stack[stack.length - 1];
  823. post.push(popped);
  824. prep = ele.pre;
  825. flag = elem.value == 'Math.pow' && ele.value == 'Math.pow';
  826. }
  827. stack.push(elem);
  828. }
  829. }
  830. }
  831. return new Mexp(post);
  832. };
  833. module.exports = Mexp;
  834. /***/ }),
  835. /***/ 733:
  836. /***/ ((module, __unused_webpack_exports, __webpack_require__) => {
  837. var Mexp=__webpack_require__(477);
  838. Mexp.prototype.postfixEval = function (UserDefined) {
  839. 'use strict';
  840. UserDefined=UserDefined||{};
  841. UserDefined.PI=Math.PI;
  842. UserDefined.E=Math.E;
  843. var stack=[],pop1,pop2,pop3;
  844. var disp=[];
  845. var temp='';
  846. var arr=this.value;
  847. var bool=(typeof UserDefined.n!=="undefined");
  848. for(var i=0;i<arr.length;i++){
  849. if(arr[i].type===1){
  850. stack.push({value:arr[i].value,type:1});
  851. }
  852. else if(arr[i].type===3){
  853. stack.push({value:UserDefined[arr[i].value],type:1});
  854. }
  855. else if(arr[i].type===0){
  856. if(typeof stack[stack.length-1].type==="undefined"){
  857. stack[stack.length-1].value.push(arr[i]);
  858. }
  859. else stack[stack.length-1].value=arr[i].value(stack[stack.length-1].value);
  860. }
  861. else if(arr[i].type===7){
  862. if(typeof stack[stack.length-1].type==="undefined"){
  863. stack[stack.length-1].value.push(arr[i]);
  864. }
  865. else stack[stack.length-1].value=arr[i].value(stack[stack.length-1].value);
  866. }
  867. else if(arr[i].type===8){
  868. pop1=stack.pop();
  869. pop2=stack.pop();
  870. stack.push({type:1,value:arr[i].value(pop2.value,pop1.value)});
  871. }
  872. else if(arr[i].type===10){
  873. pop1=stack.pop();
  874. pop2=stack.pop();
  875. if(typeof pop2.type==="undefined"){
  876. pop2.value=pop2.concat(pop1);
  877. pop2.value.push(arr[i]);
  878. stack.push(pop2);
  879. }
  880. else if (typeof pop1.type==="undefined") {
  881. pop1.unshift(pop2);
  882. pop1.push(arr[i]);
  883. stack.push(pop1);
  884. }
  885. else{
  886. stack.push({type:1,value:arr[i].value(pop2.value,pop1.value)});
  887. }
  888. }
  889. else if(arr[i].type===2||arr[i].type===9){
  890. pop1=stack.pop();
  891. pop2=stack.pop();
  892. if(typeof pop2.type==="undefined"){
  893. pop2=pop2.concat(pop1);
  894. pop2.push(arr[i]);
  895. stack.push(pop2);
  896. }
  897. else if (typeof pop1.type==="undefined") {
  898. pop1.unshift(pop2);
  899. pop1.push(arr[i]);
  900. stack.push(pop1);
  901. }
  902. else{
  903. stack.push({type:1,value:arr[i].value(pop2.value,pop1.value)});
  904. }
  905. }
  906. else if(arr[i].type===12){
  907. pop1=stack.pop();
  908. if (typeof pop1.type!=="undefined") {
  909. pop1=[pop1];
  910. }
  911. pop2=stack.pop();
  912. pop3=stack.pop();
  913. stack.push({type:1,value:arr[i].value(pop3.value,pop2.value,new Mexp(pop1))});
  914. }
  915. else if(arr[i].type===13){
  916. if(bool){
  917. stack.push({value:UserDefined[arr[i].value],type:3});
  918. }
  919. else stack.push([arr[i]]);
  920. }
  921. }
  922. if (stack.length>1) {
  923. throw(new Mexp.Exception("Uncaught Syntax error"));
  924. }
  925. return stack[0].value>1000000000000000?"Infinity":parseFloat(stack[0].value.toFixed(15));
  926. };
  927. Mexp.eval=function(str,tokens,obj){
  928. if (typeof tokens==="undefined") {
  929. return this.lex(str).toPostfix().postfixEval();
  930. }
  931. else if (typeof obj==="undefined") {
  932. if (typeof tokens.length!=="undefined")
  933. return this.lex(str,tokens).toPostfix().postfixEval();
  934. else
  935. return this.lex(str).toPostfix().postfixEval(tokens);
  936. }
  937. else
  938. return this.lex(str,tokens).toPostfix().postfixEval(obj);
  939. };
  940. module.exports=Mexp;
  941. /***/ })
  942. /******/ });
  943. /************************************************************************/
  944. /******/ // The module cache
  945. /******/ var __webpack_module_cache__ = {};
  946. /******/
  947. /******/ // The require function
  948. /******/ function __webpack_require__(moduleId) {
  949. /******/ // Check if module is in cache
  950. /******/ var cachedModule = __webpack_module_cache__[moduleId];
  951. /******/ if (cachedModule !== undefined) {
  952. /******/ return cachedModule.exports;
  953. /******/ }
  954. /******/ // Create a new module (and put it into the cache)
  955. /******/ var module = __webpack_module_cache__[moduleId] = {
  956. /******/ // no module.id needed
  957. /******/ // no module.loaded needed
  958. /******/ exports: {}
  959. /******/ };
  960. /******/
  961. /******/ // Execute the module function
  962. /******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
  963. /******/
  964. /******/ // Return the exports of the module
  965. /******/ return module.exports;
  966. /******/ }
  967. /******/
  968. /************************************************************************/
  969. /******/
  970. /******/ // startup
  971. /******/ // Load entry module and return exports
  972. /******/ // This entry module is referenced by other modules so it can't be inlined
  973. /******/ var __webpack_exports__ = __webpack_require__(28);
  974. /******/
  975. /******/ return __webpack_exports__;
  976. /******/ })()
  977. ;
  978. });