OperatorNode.js 24 KB

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