OperatorNode.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. import _defineProperty from "@babel/runtime/helpers/defineProperty";
  2. import { isNode, isConstantNode, isOperatorNode, isParenthesisNode } from '../../utils/is.js';
  3. import { map } from '../../utils/array.js';
  4. import { escape } from '../../utils/string.js';
  5. import { getSafeProperty, isSafeMethod } from '../../utils/customs.js';
  6. import { getAssociativity, getPrecedence, isAssociativeWith, properties } from '../operators.js';
  7. import { latexOperators } from '../../utils/latex.js';
  8. import { factory } from '../../utils/factory.js';
  9. var name = 'OperatorNode';
  10. var dependencies = ['Node'];
  11. export var createOperatorNode = /* #__PURE__ */factory(name, dependencies, _ref => {
  12. var {
  13. Node
  14. } = _ref;
  15. /**
  16. * Returns true if the expression starts with a constant, under
  17. * the current parenthesization:
  18. * @param {Node} expression
  19. * @param {string} parenthesis
  20. * @return {boolean}
  21. */
  22. function startsWithConstant(expr, parenthesis) {
  23. var curNode = expr;
  24. if (parenthesis === 'auto') {
  25. while (isParenthesisNode(curNode)) {
  26. curNode = curNode.content;
  27. }
  28. }
  29. if (isConstantNode(curNode)) return true;
  30. if (isOperatorNode(curNode)) {
  31. return startsWithConstant(curNode.args[0], parenthesis);
  32. }
  33. return false;
  34. }
  35. /**
  36. * Calculate which parentheses are necessary. Gets an OperatorNode
  37. * (which is the root of the tree) and an Array of Nodes
  38. * (this.args) and returns an array where 'true' means that an argument
  39. * has to be enclosed in parentheses whereas 'false' means the opposite.
  40. *
  41. * @param {OperatorNode} root
  42. * @param {string} parenthesis
  43. * @param {Node[]} args
  44. * @param {boolean} latex
  45. * @return {boolean[]}
  46. * @private
  47. */
  48. function calculateNecessaryParentheses(root, parenthesis, implicit, args, latex) {
  49. // precedence of the root OperatorNode
  50. var precedence = getPrecedence(root, parenthesis, implicit);
  51. var associativity = getAssociativity(root, parenthesis);
  52. if (parenthesis === 'all' || args.length > 2 && root.getIdentifier() !== 'OperatorNode:add' && root.getIdentifier() !== 'OperatorNode:multiply') {
  53. return args.map(function (arg) {
  54. switch (arg.getContent().type) {
  55. // Nodes that don't need extra parentheses
  56. case 'ArrayNode':
  57. case 'ConstantNode':
  58. case 'SymbolNode':
  59. case 'ParenthesisNode':
  60. return false;
  61. default:
  62. return true;
  63. }
  64. });
  65. }
  66. var result;
  67. switch (args.length) {
  68. case 0:
  69. result = [];
  70. break;
  71. case 1:
  72. // unary operators
  73. {
  74. // precedence of the operand
  75. var operandPrecedence = getPrecedence(args[0], parenthesis, implicit, root);
  76. // handle special cases for LaTeX, where some of the parentheses aren't needed
  77. if (latex && operandPrecedence !== null) {
  78. var operandIdentifier;
  79. var rootIdentifier;
  80. if (parenthesis === 'keep') {
  81. operandIdentifier = args[0].getIdentifier();
  82. rootIdentifier = root.getIdentifier();
  83. } else {
  84. // Ignore Parenthesis Nodes when not in 'keep' mode
  85. operandIdentifier = args[0].getContent().getIdentifier();
  86. rootIdentifier = root.getContent().getIdentifier();
  87. }
  88. if (properties[precedence][rootIdentifier].latexLeftParens === false) {
  89. result = [false];
  90. break;
  91. }
  92. if (properties[operandPrecedence][operandIdentifier].latexParens === false) {
  93. result = [false];
  94. break;
  95. }
  96. }
  97. if (operandPrecedence === null) {
  98. // if the operand has no defined precedence, no parens are needed
  99. result = [false];
  100. break;
  101. }
  102. if (operandPrecedence <= precedence) {
  103. // if the operands precedence is lower, parens are needed
  104. result = [true];
  105. break;
  106. }
  107. // otherwise, no parens needed
  108. result = [false];
  109. }
  110. break;
  111. case 2:
  112. // binary operators
  113. {
  114. var lhsParens; // left hand side needs parenthesis?
  115. // precedence of the left hand side
  116. var lhsPrecedence = getPrecedence(args[0], parenthesis, implicit, root);
  117. // is the root node associative with the left hand side
  118. var assocWithLhs = isAssociativeWith(root, args[0], parenthesis);
  119. if (lhsPrecedence === null) {
  120. // if the left hand side has no defined precedence, no parens are needed
  121. // FunctionNode for example
  122. lhsParens = false;
  123. } else if (lhsPrecedence === precedence && associativity === 'right' && !assocWithLhs) {
  124. // In case of equal precedence, if the root node is left associative
  125. // parens are **never** necessary for the left hand side.
  126. // If it is right associative however, parens are necessary
  127. // if the root node isn't associative with the left hand side
  128. lhsParens = true;
  129. } else if (lhsPrecedence < precedence) {
  130. lhsParens = true;
  131. } else {
  132. lhsParens = false;
  133. }
  134. var rhsParens; // right hand side needs parenthesis?
  135. // precedence of the right hand side
  136. var rhsPrecedence = getPrecedence(args[1], parenthesis, implicit, root);
  137. // is the root node associative with the right hand side?
  138. var assocWithRhs = isAssociativeWith(root, args[1], parenthesis);
  139. if (rhsPrecedence === null) {
  140. // if the right hand side has no defined precedence, no parens are needed
  141. // FunctionNode for example
  142. rhsParens = false;
  143. } else if (rhsPrecedence === precedence && associativity === 'left' && !assocWithRhs) {
  144. // In case of equal precedence, if the root node is right associative
  145. // parens are **never** necessary for the right hand side.
  146. // If it is left associative however, parens are necessary
  147. // if the root node isn't associative with the right hand side
  148. rhsParens = true;
  149. } else if (rhsPrecedence < precedence) {
  150. rhsParens = true;
  151. } else {
  152. rhsParens = false;
  153. }
  154. // handle special cases for LaTeX, where some of the parentheses aren't needed
  155. if (latex) {
  156. var _rootIdentifier;
  157. var lhsIdentifier;
  158. var rhsIdentifier;
  159. if (parenthesis === 'keep') {
  160. _rootIdentifier = root.getIdentifier();
  161. lhsIdentifier = root.args[0].getIdentifier();
  162. rhsIdentifier = root.args[1].getIdentifier();
  163. } else {
  164. // Ignore ParenthesisNodes when not in 'keep' mode
  165. _rootIdentifier = root.getContent().getIdentifier();
  166. lhsIdentifier = root.args[0].getContent().getIdentifier();
  167. rhsIdentifier = root.args[1].getContent().getIdentifier();
  168. }
  169. if (lhsPrecedence !== null) {
  170. if (properties[precedence][_rootIdentifier].latexLeftParens === false) {
  171. lhsParens = false;
  172. }
  173. if (properties[lhsPrecedence][lhsIdentifier].latexParens === false) {
  174. lhsParens = false;
  175. }
  176. }
  177. if (rhsPrecedence !== null) {
  178. if (properties[precedence][_rootIdentifier].latexRightParens === false) {
  179. rhsParens = false;
  180. }
  181. if (properties[rhsPrecedence][rhsIdentifier].latexParens === false) {
  182. rhsParens = false;
  183. }
  184. }
  185. }
  186. result = [lhsParens, rhsParens];
  187. }
  188. break;
  189. default:
  190. if (root.getIdentifier() === 'OperatorNode:add' || root.getIdentifier() === 'OperatorNode:multiply') {
  191. result = args.map(function (arg) {
  192. var argPrecedence = getPrecedence(arg, parenthesis, implicit, root);
  193. var assocWithArg = isAssociativeWith(root, arg, parenthesis);
  194. var argAssociativity = getAssociativity(arg, parenthesis);
  195. if (argPrecedence === null) {
  196. // if the argument has no defined precedence, no parens are needed
  197. return false;
  198. } else if (precedence === argPrecedence && associativity === argAssociativity && !assocWithArg) {
  199. return true;
  200. } else if (argPrecedence < precedence) {
  201. return true;
  202. }
  203. return false;
  204. });
  205. }
  206. break;
  207. }
  208. // Handles an edge case of parentheses with implicit multiplication
  209. // of ConstantNode.
  210. // In that case, parenthesize ConstantNodes that follow an unparenthesized
  211. // expression, even though they normally wouldn't be printed.
  212. if (args.length >= 2 && root.getIdentifier() === 'OperatorNode:multiply' && root.implicit && parenthesis !== 'all' && implicit === 'hide') {
  213. for (var i = 1; i < result.length; ++i) {
  214. if (startsWithConstant(args[i], parenthesis) && !result[i - 1] && (parenthesis !== 'keep' || !isParenthesisNode(args[i - 1]))) {
  215. result[i] = true;
  216. }
  217. }
  218. }
  219. return result;
  220. }
  221. class OperatorNode extends Node {
  222. /**
  223. * @constructor OperatorNode
  224. * @extends {Node}
  225. * An operator with two arguments, like 2+3
  226. *
  227. * @param {string} op Operator name, for example '+'
  228. * @param {string} fn Function name, for example 'add'
  229. * @param {Node[]} args Operator arguments
  230. * @param {boolean} [implicit] Is this an implicit multiplication?
  231. * @param {boolean} [isPercentage] Is this an percentage Operation?
  232. */
  233. constructor(op, fn, args, implicit, isPercentage) {
  234. super();
  235. // validate input
  236. if (typeof op !== 'string') {
  237. throw new TypeError('string expected for parameter "op"');
  238. }
  239. if (typeof fn !== 'string') {
  240. throw new TypeError('string expected for parameter "fn"');
  241. }
  242. if (!Array.isArray(args) || !args.every(isNode)) {
  243. throw new TypeError('Array containing Nodes expected for parameter "args"');
  244. }
  245. this.implicit = implicit === true;
  246. this.isPercentage = isPercentage === true;
  247. this.op = op;
  248. this.fn = fn;
  249. this.args = args || [];
  250. }
  251. get type() {
  252. return name;
  253. }
  254. get isOperatorNode() {
  255. return true;
  256. }
  257. /**
  258. * Compile a node into a JavaScript function.
  259. * This basically pre-calculates as much as possible and only leaves open
  260. * calculations which depend on a dynamic scope with variables.
  261. * @param {Object} math Math.js namespace with functions and constants.
  262. * @param {Object} argNames An object with argument names as key and `true`
  263. * as value. Used in the SymbolNode to optimize
  264. * for arguments from user assigned functions
  265. * (see FunctionAssignmentNode) or special symbols
  266. * like `end` (see IndexNode).
  267. * @return {function} Returns a function which can be called like:
  268. * evalNode(scope: Object, args: Object, context: *)
  269. */
  270. _compile(math, argNames) {
  271. // validate fn
  272. if (typeof this.fn !== 'string' || !isSafeMethod(math, this.fn)) {
  273. if (!math[this.fn]) {
  274. throw new Error('Function ' + this.fn + ' missing in provided namespace "math"');
  275. } else {
  276. throw new Error('No access to function "' + this.fn + '"');
  277. }
  278. }
  279. var fn = getSafeProperty(math, this.fn);
  280. var evalArgs = map(this.args, function (arg) {
  281. return arg._compile(math, argNames);
  282. });
  283. if (evalArgs.length === 1) {
  284. var evalArg0 = evalArgs[0];
  285. return function evalOperatorNode(scope, args, context) {
  286. return fn(evalArg0(scope, args, context));
  287. };
  288. } else if (evalArgs.length === 2) {
  289. var _evalArg = evalArgs[0];
  290. var evalArg1 = evalArgs[1];
  291. return function evalOperatorNode(scope, args, context) {
  292. return fn(_evalArg(scope, args, context), evalArg1(scope, args, context));
  293. };
  294. } else {
  295. return function evalOperatorNode(scope, args, context) {
  296. return fn.apply(null, map(evalArgs, function (evalArg) {
  297. return evalArg(scope, args, context);
  298. }));
  299. };
  300. }
  301. }
  302. /**
  303. * Execute a callback for each of the child nodes of this node
  304. * @param {function(child: Node, path: string, parent: Node)} callback
  305. */
  306. forEach(callback) {
  307. for (var i = 0; i < this.args.length; i++) {
  308. callback(this.args[i], 'args[' + i + ']', this);
  309. }
  310. }
  311. /**
  312. * Create a new OperatorNode whose children are the results of calling
  313. * the provided callback function for each child of the original node.
  314. * @param {function(child: Node, path: string, parent: Node): Node} callback
  315. * @returns {OperatorNode} Returns a transformed copy of the node
  316. */
  317. map(callback) {
  318. var args = [];
  319. for (var i = 0; i < this.args.length; i++) {
  320. args[i] = this._ifNode(callback(this.args[i], 'args[' + i + ']', this));
  321. }
  322. return new OperatorNode(this.op, this.fn, args, this.implicit, this.isPercentage);
  323. }
  324. /**
  325. * Create a clone of this node, a shallow copy
  326. * @return {OperatorNode}
  327. */
  328. clone() {
  329. return new OperatorNode(this.op, this.fn, this.args.slice(0), this.implicit, this.isPercentage);
  330. }
  331. /**
  332. * Check whether this is an unary OperatorNode:
  333. * has exactly one argument, like `-a`.
  334. * @return {boolean}
  335. * Returns true when an unary operator node, false otherwise.
  336. */
  337. isUnary() {
  338. return this.args.length === 1;
  339. }
  340. /**
  341. * Check whether this is a binary OperatorNode:
  342. * has exactly two arguments, like `a + b`.
  343. * @return {boolean}
  344. * Returns true when a binary operator node, false otherwise.
  345. */
  346. isBinary() {
  347. return this.args.length === 2;
  348. }
  349. /**
  350. * Get string representation.
  351. * @param {Object} options
  352. * @return {string} str
  353. */
  354. _toString(options) {
  355. var parenthesis = options && options.parenthesis ? options.parenthesis : 'keep';
  356. var implicit = options && options.implicit ? options.implicit : 'hide';
  357. var args = this.args;
  358. var parens = calculateNecessaryParentheses(this, parenthesis, implicit, args, false);
  359. if (args.length === 1) {
  360. // unary operators
  361. var assoc = getAssociativity(this, parenthesis);
  362. var operand = args[0].toString(options);
  363. if (parens[0]) {
  364. operand = '(' + operand + ')';
  365. }
  366. // for example for "not", we want a space between operand and argument
  367. var opIsNamed = /[a-zA-Z]+/.test(this.op);
  368. if (assoc === 'right') {
  369. // prefix operator
  370. return this.op + (opIsNamed ? ' ' : '') + operand;
  371. } else if (assoc === 'left') {
  372. // postfix
  373. return operand + (opIsNamed ? ' ' : '') + this.op;
  374. }
  375. // fall back to postfix
  376. return operand + this.op;
  377. } else if (args.length === 2) {
  378. var lhs = args[0].toString(options); // left hand side
  379. var rhs = args[1].toString(options); // right hand side
  380. if (parens[0]) {
  381. // left hand side in parenthesis?
  382. lhs = '(' + lhs + ')';
  383. }
  384. if (parens[1]) {
  385. // right hand side in parenthesis?
  386. rhs = '(' + rhs + ')';
  387. }
  388. if (this.implicit && this.getIdentifier() === 'OperatorNode:multiply' && implicit === 'hide') {
  389. return lhs + ' ' + rhs;
  390. }
  391. return lhs + ' ' + this.op + ' ' + rhs;
  392. } else if (args.length > 2 && (this.getIdentifier() === 'OperatorNode:add' || this.getIdentifier() === 'OperatorNode:multiply')) {
  393. var stringifiedArgs = args.map(function (arg, index) {
  394. arg = arg.toString(options);
  395. if (parens[index]) {
  396. // put in parenthesis?
  397. arg = '(' + arg + ')';
  398. }
  399. return arg;
  400. });
  401. if (this.implicit && this.getIdentifier() === 'OperatorNode:multiply' && implicit === 'hide') {
  402. return stringifiedArgs.join(' ');
  403. }
  404. return stringifiedArgs.join(' ' + this.op + ' ');
  405. } else {
  406. // fallback to formatting as a function call
  407. return this.fn + '(' + this.args.join(', ') + ')';
  408. }
  409. }
  410. /**
  411. * Get a JSON representation of the node
  412. * @returns {Object}
  413. */
  414. toJSON() {
  415. return {
  416. mathjs: name,
  417. op: this.op,
  418. fn: this.fn,
  419. args: this.args,
  420. implicit: this.implicit,
  421. isPercentage: this.isPercentage
  422. };
  423. }
  424. /**
  425. * Instantiate an OperatorNode from its JSON representation
  426. * @param {Object} json
  427. * An object structured like
  428. * ```
  429. * {"mathjs": "OperatorNode",
  430. * "op": "+", "fn": "add", "args": [...],
  431. * "implicit": false,
  432. * "isPercentage":false}
  433. * ```
  434. * where mathjs is optional
  435. * @returns {OperatorNode}
  436. */
  437. static fromJSON(json) {
  438. return new OperatorNode(json.op, json.fn, json.args, json.implicit, json.isPercentage);
  439. }
  440. /**
  441. * Get HTML representation.
  442. * @param {Object} options
  443. * @return {string} str
  444. */
  445. toHTML(options) {
  446. var parenthesis = options && options.parenthesis ? options.parenthesis : 'keep';
  447. var implicit = options && options.implicit ? options.implicit : 'hide';
  448. var args = this.args;
  449. var parens = calculateNecessaryParentheses(this, parenthesis, implicit, args, false);
  450. if (args.length === 1) {
  451. // unary operators
  452. var assoc = getAssociativity(this, parenthesis);
  453. var operand = args[0].toHTML(options);
  454. if (parens[0]) {
  455. operand = '<span class="math-parenthesis math-round-parenthesis">(</span>' + operand + '<span class="math-parenthesis math-round-parenthesis">)</span>';
  456. }
  457. if (assoc === 'right') {
  458. // prefix operator
  459. return '<span class="math-operator math-unary-operator ' + 'math-lefthand-unary-operator">' + escape(this.op) + '</span>' + operand;
  460. } else {
  461. // postfix when assoc === 'left' or undefined
  462. return operand + '<span class="math-operator math-unary-operator ' + 'math-righthand-unary-operator">' + escape(this.op) + '</span>';
  463. }
  464. } else if (args.length === 2) {
  465. // binary operatoes
  466. var lhs = args[0].toHTML(options); // left hand side
  467. var rhs = args[1].toHTML(options); // right hand side
  468. if (parens[0]) {
  469. // left hand side in parenthesis?
  470. lhs = '<span class="math-parenthesis math-round-parenthesis">(</span>' + lhs + '<span class="math-parenthesis math-round-parenthesis">)</span>';
  471. }
  472. if (parens[1]) {
  473. // right hand side in parenthesis?
  474. rhs = '<span class="math-parenthesis math-round-parenthesis">(</span>' + rhs + '<span class="math-parenthesis math-round-parenthesis">)</span>';
  475. }
  476. if (this.implicit && this.getIdentifier() === 'OperatorNode:multiply' && implicit === 'hide') {
  477. return lhs + '<span class="math-operator math-binary-operator ' + 'math-implicit-binary-operator"></span>' + rhs;
  478. }
  479. return lhs + '<span class="math-operator math-binary-operator ' + 'math-explicit-binary-operator">' + escape(this.op) + '</span>' + rhs;
  480. } else {
  481. var stringifiedArgs = args.map(function (arg, index) {
  482. arg = arg.toHTML(options);
  483. if (parens[index]) {
  484. // put in parenthesis?
  485. arg = '<span class="math-parenthesis math-round-parenthesis">(</span>' + arg + '<span class="math-parenthesis math-round-parenthesis">)</span>';
  486. }
  487. return arg;
  488. });
  489. if (args.length > 2 && (this.getIdentifier() === 'OperatorNode:add' || this.getIdentifier() === 'OperatorNode:multiply')) {
  490. if (this.implicit && this.getIdentifier() === 'OperatorNode:multiply' && implicit === 'hide') {
  491. return stringifiedArgs.join('<span class="math-operator math-binary-operator ' + 'math-implicit-binary-operator"></span>');
  492. }
  493. return stringifiedArgs.join('<span class="math-operator math-binary-operator ' + 'math-explicit-binary-operator">' + escape(this.op) + '</span>');
  494. } else {
  495. // fallback to formatting as a function call
  496. return '<span class="math-function">' + escape(this.fn) + '</span><span class="math-paranthesis math-round-parenthesis">' + '(</span>' + stringifiedArgs.join('<span class="math-separator">,</span>') + '<span class="math-paranthesis math-round-parenthesis">)</span>';
  497. }
  498. }
  499. }
  500. /**
  501. * Get LaTeX representation
  502. * @param {Object} options
  503. * @return {string} str
  504. */
  505. _toTex(options) {
  506. var parenthesis = options && options.parenthesis ? options.parenthesis : 'keep';
  507. var implicit = options && options.implicit ? options.implicit : 'hide';
  508. var args = this.args;
  509. var parens = calculateNecessaryParentheses(this, parenthesis, implicit, args, true);
  510. var op = latexOperators[this.fn];
  511. op = typeof op === 'undefined' ? this.op : op; // fall back to using this.op
  512. if (args.length === 1) {
  513. // unary operators
  514. var assoc = getAssociativity(this, parenthesis);
  515. var operand = args[0].toTex(options);
  516. if (parens[0]) {
  517. operand = "\\left(".concat(operand, "\\right)");
  518. }
  519. if (assoc === 'right') {
  520. // prefix operator
  521. return op + operand;
  522. } else if (assoc === 'left') {
  523. // postfix operator
  524. return operand + op;
  525. }
  526. // fall back to postfix
  527. return operand + op;
  528. } else if (args.length === 2) {
  529. // binary operators
  530. var lhs = args[0]; // left hand side
  531. var lhsTex = lhs.toTex(options);
  532. if (parens[0]) {
  533. lhsTex = "\\left(".concat(lhsTex, "\\right)");
  534. }
  535. var rhs = args[1]; // right hand side
  536. var rhsTex = rhs.toTex(options);
  537. if (parens[1]) {
  538. rhsTex = "\\left(".concat(rhsTex, "\\right)");
  539. }
  540. // handle some exceptions (due to the way LaTeX works)
  541. var lhsIdentifier;
  542. if (parenthesis === 'keep') {
  543. lhsIdentifier = lhs.getIdentifier();
  544. } else {
  545. // Ignore ParenthesisNodes if in 'keep' mode
  546. lhsIdentifier = lhs.getContent().getIdentifier();
  547. }
  548. switch (this.getIdentifier()) {
  549. case 'OperatorNode:divide':
  550. // op contains '\\frac' at this point
  551. return op + '{' + lhsTex + '}' + '{' + rhsTex + '}';
  552. case 'OperatorNode:pow':
  553. lhsTex = '{' + lhsTex + '}';
  554. rhsTex = '{' + rhsTex + '}';
  555. switch (lhsIdentifier) {
  556. case 'ConditionalNode': //
  557. case 'OperatorNode:divide':
  558. lhsTex = "\\left(".concat(lhsTex, "\\right)");
  559. }
  560. break;
  561. case 'OperatorNode:multiply':
  562. if (this.implicit && implicit === 'hide') {
  563. return lhsTex + '~' + rhsTex;
  564. }
  565. }
  566. return lhsTex + op + rhsTex;
  567. } else if (args.length > 2 && (this.getIdentifier() === 'OperatorNode:add' || this.getIdentifier() === 'OperatorNode:multiply')) {
  568. var texifiedArgs = args.map(function (arg, index) {
  569. arg = arg.toTex(options);
  570. if (parens[index]) {
  571. arg = "\\left(".concat(arg, "\\right)");
  572. }
  573. return arg;
  574. });
  575. if (this.getIdentifier() === 'OperatorNode:multiply' && this.implicit && implicit === 'hide') {
  576. return texifiedArgs.join('~');
  577. }
  578. return texifiedArgs.join(op);
  579. } else {
  580. // fall back to formatting as a function call
  581. // as this is a fallback, it doesn't use
  582. // fancy function names
  583. return '\\mathrm{' + this.fn + '}\\left(' + args.map(function (arg) {
  584. return arg.toTex(options);
  585. }).join(',') + '\\right)';
  586. }
  587. }
  588. /**
  589. * Get identifier.
  590. * @return {string}
  591. */
  592. getIdentifier() {
  593. return this.type + ':' + this.fn;
  594. }
  595. }
  596. _defineProperty(OperatorNode, "name", name);
  597. return OperatorNode;
  598. }, {
  599. isClass: true,
  600. isNode: true
  601. });