Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将LaTeX转换为动态Javascript函数_Javascript_Latex - Fatal编程技术网

将LaTeX转换为动态Javascript函数

将LaTeX转换为动态Javascript函数,javascript,latex,Javascript,Latex,我有一个等式的用户输入——这个输入使用一个单独的API生成LaTeX代码,我没有编写这个API(即Mathquill,这并不重要) 我的问题最好用一个例子来说明:假设从用户输入生成的LaTeX代码如下: x^2+3x-10sin\left(2x\right) 我如何将它(当然是动态地)转换成一个JavaScript函数,硬编码的JavaScript函数如下所示: function(x) { return Math.pow(x, 2) + 3 * x - 10 * Math.sin(2 *

我有一个等式的用户输入——这个输入使用一个单独的API生成LaTeX代码,我没有编写这个API(即Mathquill,这并不重要)

我的问题最好用一个例子来说明:假设从用户输入生成的LaTeX代码如下:

x^2+3x-10sin\left(2x\right) 
我如何将它(当然是动态地)转换成一个JavaScript函数,硬编码的JavaScript函数如下所示:

function(x) {
  return Math.pow(x, 2) + 3 * x - 10 * Math.sin(2 * x);
}

有没有API,或者我正在考虑写一些东西来解释LaTeX符号并以某种方式生成一个函数?或者什么?

好吧,你必须在某个时候决定你到底支持哪些操作。在这之后,使用类似的解析器来实现计算器应该不难,以生成更易于计算的表达式表示(即抽象语法树)

我有一个用JavaScript编写的这种计算器的简单示例:它采用逻辑表达式,如
!(p^^q)&~(p| | q)
而不是LaTeX,但它可能仍然是一个有用的示例

JavaScript():

我已经写了一个(绝不是通用的)解决方案,主要基于George的代码

这是:

var CALC_CONST = {
  // define your constants
  e: Math.E,
  pi: Math.PI
};

var CALC_NUMARGS = [
  [/^(\^|\*|\/|\+|\-)$/, 2],
  [/^(floor|ceil|(sin|cos|tan|sec|csc|cot)h?)$/, 1]
];

var Calc = function(expr, infix) {
  this.valid = true;
  this.expr = expr;

  if (!infix) {
    // by default treat expr as raw latex
    this.expr = this.latexToInfix(expr);
  }

  var OpPrecedence = function(op) {
    if (typeof op == "undefined") return 0;

    return op.match(/^(floor|ceil|(sin|cos|tan|sec|csc|cot)h?)$/) ? 10

         : (op === "^") ? 9
         : (op === "*" || op === "/") ? 8
         : (op === "+" || op === "-") ? 7

         : 0;
  }

  var OpAssociativity = function(op) {
    return op.match(/^(floor|ceil|(sin|cos|tan|sec|csc|cot)h?)$/) ? "R" : "L";
  }

  var numArgs = function(op) {
    for (var i = 0; i < CALC_NUMARGS.length; i++) {
      if (CALC_NUMARGS[i][0].test(op)) return CALC_NUMARGS[i][1];
    }
    return false;
  }

  this.rpn_expr = [];
  var rpn_expr = this.rpn_expr;

  this.expr = this.expr.replace(/\s+/g, "");

  // This nice long regex matches any valid token in a user
  // supplied expression (e.g. an operator, a constant or
  // a variable)
  var in_tokens = this.expr.match(/(\^|\*|\/|\+|\-|\(|\)|[a-zA-Z0-9\.]+)/gi);
  var op_stack = [];

  in_tokens.forEach(function(token) {
    if (/^[a-zA-Z]$/.test(token)) {
      if (CALC_CONST.hasOwnProperty(token)) {
        // Constant. Pushes a value onto the stack.
        rpn_expr.push(["num", CALC_CONST[token]]);
      }
      else {
        // Variables (i.e. x as in f(x))
        rpn_expr.push(["var", token]);
      }
    }
    else {
      var numVal = parseFloat(token);
      if (!isNaN(numVal)) {
        // Number - push onto the stack
        rpn_expr.push(["num", numVal]);
      }
      else if (token === ")") {
        // Pop tokens off the op_stack onto the rpn_expr until we reach the matching (
        while (op_stack[op_stack.length - 1] !== "(") {
          rpn_expr.push([numArgs(op_stack[op_stack.length - 1]), op_stack.pop()]);
          if (op_stack.length === 0) {
            this.valid = false;
            return;
          }
        }

        // remove the (
        op_stack.pop();
      }
      else if (token === "(") {
        op_stack.push(token);
      }
      else {
        // Operator
        var tokPrec = OpPrecedence(token),
           headPrec = OpPrecedence(op_stack[op_stack.length - 1]);

        while ((OpAssociativity(token) === "L" && tokPrec <= headPrec) ||
          (OpAssociativity(token) === "R" && tokPrec < headPrec)) {

          rpn_expr.push([numArgs(op_stack[op_stack.length - 1]), op_stack.pop()]);
          if (op_stack.length === 0) break;

          headPrec = OpPrecedence(op_stack[op_stack.length - 1]);
        }

        op_stack.push(token);
      }
    }
  });

  // Push all remaining operators onto the final expression
  while (op_stack.length > 0) {
    var popped = op_stack.pop();
    if (popped === ")") {
      this.valid = false;
      break;
    }
    rpn_expr.push([numArgs(popped), popped]);
  }
}

/**
 * returns the result of evaluating the current expression
 */
Calc.prototype.eval = function(x) {
  var stack = [], rpn_expr = this.rpn_expr;

  rpn_expr.forEach(function(token) {
    if (typeof token[0] == "string") {
      switch (token[0]) {
        case "var":
          // Variable, i.e. x as in f(x); push value onto stack
          //if (token[1] != "x") return false;
          stack.push(x);
          break;

        case "num":
          // Number; push value onto stack
          stack.push(token[1]);
          break;
      }
    }
    else {
      // Operator
      var numArgs = token[0];
      var args = [];
      do {
        args.unshift(stack.pop());
      } while (args.length < numArgs);

      switch (token[1]) {
        /* BASIC ARITHMETIC OPERATORS */
        case "*":
          stack.push(args[0] * args[1]);
          break;
        case "/":
          stack.push(args[0] / args[1]);
          break;
        case "+":
          stack.push(args[0] + args[1]);
          break;
        case "-":
          stack.push(args[0] - args[1]);
          break;

        // exponents
        case "^":
          stack.push(Math.pow(args[0], args[1]));
          break;

        /* TRIG FUNCTIONS */
        case "sin":
          stack.push(Math.sin(args[0]));
          break;
        case "cos":
          stack.push(Math.cos(args[0]));
          break;
        case "tan":
          stack.push(Math.tan(args[0]));
          break;
        case "sec":
          stack.push(1 / Math.cos(args[0]));
          break;
        case "csc":
          stack.push(1 / Math.sin(args[0]));
          break;
        case "cot":
          stack.push(1 / Math.tan(args[0]));
          break;
        case "sinh":
          stack.push(.5 * (Math.pow(Math.E, args[0]) - Math.pow(Math.E, -args[0])));
          break;
        case "cosh":
          stack.push(.5 * (Math.pow(Math.E, args[0]) + Math.pow(Math.E, -args[0])));
          break;
        case "tanh":
          stack.push((Math.pow(Math.E, 2*args[0]) - 1) / (Math.pow(Math.E, 2*args[0]) + 1));
          break;
        case "sech":
          stack.push(2 / (Math.pow(Math.E, args[0]) + Math.pow(Math.E, -args[0])));
          break;
        case "csch":
          stack.push(2 / (Math.pow(Math.E, args[0]) - Math.pow(Math.E, -args[0])));
          break;
        case "coth":
          stack.push((Math.pow(Math.E, 2*args[0]) + 1) / (Math.pow(Math.E, 2*args[0]) - 1));
          break;


        case "floor":
          stack.push(Math.floor(args[0]));
          break;
        case "ceil":
          stack.push(Math.ceil(args[0]));
          break;

        default:
          // unknown operator; error out
          return false;
      }
    }
  });

  return stack.pop();
};

Calc.prototype.latexToInfix = function(latex) {
  /**
    * function: converts latex notation to infix notation (human-readable, to be converted
    * again to prefix in order to be processed
    *
    * Supported functions / operators / notation:
    * parentheses, exponents, adding, subtracting, multipling, dividing, fractions
    * trigonometric (including hyperbolic) functions, floor, ceil
    */

  var infix = latex;

  infix = infix
    .replace(/\\frac{([^}]+)}{([^}]+)}/g, "($1)/($2)") // fractions
    .replace(/\\left\(/g, "(") // open parenthesis
    .replace(/\\right\)/g, ")") // close parenthesis
    .replace(/[^\(](floor|ceil|(sin|cos|tan|sec|csc|cot)h?)\(([^\(\)]+)\)[^\)]/g, "($&)") // functions
    .replace(/([^(floor|ceil|(sin|cos|tan|sec|csc|cot)h?|\+|\-|\*|\/)])\(/g, "$1*(")
    .replace(/\)([\w])/g, ")*$1")
    .replace(/([0-9])([A-Za-z])/g, "$1*$2")
  ;

  return infix;
};
也许你可以试试。LatexJS是一个API服务,我将其组合在一起,以便将latex数学符号转换为Javascript函数。因此,您可以输入latex表达式并动态地返回Javascript函数。例如:

输入

x^2+3x-10sin\left(2x\right)
{
    "func": "(x)=>{return Math.pow(x,2)+3*x-10*Math.sin(2*x)};",
    "params": ["x"]
}
输出

x^2+3x-10sin\left(2x\right)
{
    "func": "(x)=>{return Math.pow(x,2)+3*x-10*Math.sin(2*x)};",
    "params": ["x"]
}
评估

> func = (x)=>{return Math.pow(x,2)+3*x-10*Math.sin(2*x)};
> func(2)
< 17.56802495307928
>func=(x)=>{return Math.pow(x,2)+3*x-10*Math.sin(2*x)};
>func(2)
< 17.56802495307928

您是否计划支持定义良好的数学表达式子集?指数和三角函数可以很好地转换,但积分和导数等东西在JavaScript中没有简单的对应项。如果您真的需要对任意数学表达式进行求值,您可能会对使用Wolfram | Alpha的TeX支持感兴趣:我对微积分不感兴趣,尽管这会很好,但它绝对不是必需的。我正在进行的项目是一个快速区间平分线,用于近似函数的(无理)根。谢谢,我将研究调车场算法。多亏了你,我成功地完成了手头的任务,大量修改了你的代码并添加了一些我自己的添加项(不同的运算符等等)。干杯:我意识到函数列表非常缺乏,例如没有反向触发器,但添加更多的函数应该相当简单。这很好!正是我想要的。尽管我将修改它,以便它可以接受命名变量。现在是用一个参数替换latex表达式中的任何变量。我认为如果你有一个负数,这会中断,因为它认为这是一个减法运算,比如说它的\frac{2}{3}。我通过添加一个0来修复它,所以它的\frac{0-2}{3}这真的很好,但希望它是自由名称。真是太棒了。不过,如果它是免费的,我会非常喜欢:(我正在致力于开源。我仍然需要在npm上发表。