有没有办法重新格式化方程式以从公式中获取未知变量?
Is there a way to reformat equation to get unknown variable from formula?
我正在制作一个应用程序,我想在其中使用 formulas/equations 数学和物理学。我在每个公式中都包含了一个计算器部分,以便用户能够插入他的值并获得方程式的结果。问题是,我需要为每个未知变量重新格式化方程式,是否有脚本或至少某种算法能够做到这一点?
我正在使用 MathJax react-native 模块来显示表达式,并使用 javascript eval
函数来计算它们。
我找到了 algebra.js 插件,但它似乎不包含任何我需要的东西,与 github.
上的 "nerdamer" js 插件相同
我用这个函数来计算方程,variable_finding
是方程 "left side" 上的变量,其余代码负责处理 "right side"
calculateSolution = (variable_finding) => {
equation = this.state.input_equation
vars = []
for (let i = 0; i < this.state.variables.length; i++) {
if (this.state.variables[i].variable !=
variable_finding.variable) {
vars.push([
this.state.variables[i].variable,
'£' + this.state.variables[i].variable + '£',
this.state.variables[i].value
])
}
}
for (let i = 0; i < vars.length; i++) {
equation = equation.replace(vars[i][1], vars[i][2]);
}
this.setState({
solution: equation,
})
}
我想找一些 solution/function,这样我就可以输入一个带变量的方程式,然后选择一个变量,函数会创建一个新方程式,这样所选变量将在等式的左侧,其余的将在右侧。
求解给定变量的方程是一个非常复杂的问题。
您正在寻找的本质上是 CAS
for javascript.
如果您不介意在服务器上求解方程式,您可以使用免费提供的 CAS 之一(单击 here 获取列表)
例如,在 MATLAB 中,您可以使用 solve(eq, var)
求解特定变量。
就纯 javascript 实现而言,您可以使用 coffeequate
:
let equation = CQ("E = m*(c**2)");
let solutions = equation.solve("c");
for(let solution of solutions)
console.log("c =", solution.toString());
<script src="https://unpkg.com/coffeequate@1.3.0/coffeequate.min.js"></script>
请注意,coffeequate
无法求解所有类型的方程。
您也可以通过使用 toFunction
:
摆脱您的 eval
let expression = CQ("a + b**c");
let fn = expression.toFunction("a", "b", "c");
console.log("1 + 2**3 =", fn(1, 2, 3).toString()); // 9
console.log("3 + 2**1 =", fn(3, 2, 1).toString()); // 5
<script src="https://unpkg.com/coffeequate@1.3.0/coffeequate.min.js"></script>
此外,如果您需要该功能,您还可以将其用于 simplify / expand 方程式:
let eqStr = "(x + y)*(x - y)";
let equation = CQ(eqStr);
console.log(eqStr, "===", equation.simplify().toString());
<script src="https://unpkg.com/coffeequate@1.3.0/coffeequate.min.js"></script>
如果我们只是处理像
这样的二次方程
y = a x^2 + b x + c
那么二次方程就有显式解
x = ( -b +/- sqrt( b^2 - 4 * a * (c-y)) / 2 a.
要实现这个,你需要一个方程式解析器,使用类似
https://en.wikipedia.org/wiki/Shunting-yard_algorithm.
解析后,您可以操作解析树,提取常量 a、b、c,并对公式求值。
立方体要难得多。有一个公式,但它很讨厌。
我正在制作一个应用程序,我想在其中使用 formulas/equations 数学和物理学。我在每个公式中都包含了一个计算器部分,以便用户能够插入他的值并获得方程式的结果。问题是,我需要为每个未知变量重新格式化方程式,是否有脚本或至少某种算法能够做到这一点?
我正在使用 MathJax react-native 模块来显示表达式,并使用 javascript eval
函数来计算它们。
我找到了 algebra.js 插件,但它似乎不包含任何我需要的东西,与 github.
我用这个函数来计算方程,variable_finding
是方程 "left side" 上的变量,其余代码负责处理 "right side"
calculateSolution = (variable_finding) => {
equation = this.state.input_equation
vars = []
for (let i = 0; i < this.state.variables.length; i++) {
if (this.state.variables[i].variable !=
variable_finding.variable) {
vars.push([
this.state.variables[i].variable,
'£' + this.state.variables[i].variable + '£',
this.state.variables[i].value
])
}
}
for (let i = 0; i < vars.length; i++) {
equation = equation.replace(vars[i][1], vars[i][2]);
}
this.setState({
solution: equation,
})
}
我想找一些 solution/function,这样我就可以输入一个带变量的方程式,然后选择一个变量,函数会创建一个新方程式,这样所选变量将在等式的左侧,其余的将在右侧。
求解给定变量的方程是一个非常复杂的问题。
您正在寻找的本质上是 CAS
for javascript.
如果您不介意在服务器上求解方程式,您可以使用免费提供的 CAS 之一(单击 here 获取列表)
例如,在 MATLAB 中,您可以使用 solve(eq, var)
求解特定变量。
就纯 javascript 实现而言,您可以使用 coffeequate
:
let equation = CQ("E = m*(c**2)");
let solutions = equation.solve("c");
for(let solution of solutions)
console.log("c =", solution.toString());
<script src="https://unpkg.com/coffeequate@1.3.0/coffeequate.min.js"></script>
请注意,coffeequate
无法求解所有类型的方程。
您也可以通过使用 toFunction
:
eval
let expression = CQ("a + b**c");
let fn = expression.toFunction("a", "b", "c");
console.log("1 + 2**3 =", fn(1, 2, 3).toString()); // 9
console.log("3 + 2**1 =", fn(3, 2, 1).toString()); // 5
<script src="https://unpkg.com/coffeequate@1.3.0/coffeequate.min.js"></script>
此外,如果您需要该功能,您还可以将其用于 simplify / expand 方程式:
let eqStr = "(x + y)*(x - y)";
let equation = CQ(eqStr);
console.log(eqStr, "===", equation.simplify().toString());
<script src="https://unpkg.com/coffeequate@1.3.0/coffeequate.min.js"></script>
如果我们只是处理像
这样的二次方程y = a x^2 + b x + c
那么二次方程就有显式解
x = ( -b +/- sqrt( b^2 - 4 * a * (c-y)) / 2 a.
要实现这个,你需要一个方程式解析器,使用类似 https://en.wikipedia.org/wiki/Shunting-yard_algorithm.
解析后,您可以操作解析树,提取常量 a、b、c,并对公式求值。
立方体要难得多。有一个公式,但它很讨厌。