compute-engine
compute-engine copied to clipboard
The ce.assign() function not working properly
Heyy @arnog , you can visit here and check https://codesandbox.io/s/infallible-worker-kdshn7
UPDATE
- [x]
ce.assign()
takes a value without brackets.. - [x] How can I wrap a variable in
\mathrm
? - [ ] The
onInlineShortcut
function does not work when attempting to type subscripts such asQ_{Ed}
.. - [ ] How can I assign a value to a variable such as
M_g,6.10a
? I've added an example in sandbox
Description
I want to evaluate formulas with variables by using the compute-engine. But I tested one case It gives me wrong answer. you can view code to above sandbox link.
Steps to Reproduce
- first I write formula and parse into compute engine like this
const ce = new ComputeEngine.ComputeEngine();
const f = ce?.parse(
x+y+xyz);
- Then i use
ce.assign()
to assign values tox
,y
andxyz
like this.
ce?.assign('xyz', 22); ce?.assign('x', 10); ce?.assign('y', 20);
- At last I use
console.log
to show diffrent results. but
Actual Behavior
it gives me results like this.
Simplyfy: xyz+x+y
evaluate: 200z+30
final: 200z+30
Expected Behavior
And I would be expecting the result 52
.
In LaTeX, xyz
is x
multiplied by y
multiplied byt z
. If you want to indicate a single, multi-letter, identifier, you need to wrap it, for example: const f = ce.parse("x+y+\\operatorname{xyz}");
. If you make that change in the codepen, you will get the expected answer.
(BTW, new ComputeEngine()
will never return null
or undefined
, so the optional chaining operator (?.
) is superfluous)
@arnog Really appreciate your really fast response Yeah that's write and working for me. if I directly use output from math-field for above mentioned simple formula.
Please look at my code-sandbox: https://codesandbox.io/s/infallible-worker-kdshn7?file=/src/App.js
But in most cases, I need to use complex formulas. like updated on my sandbox. I want to use variables with subscripts. Could you please explain to me how it will possible?
in sandbox added required equation.
Steps to Reproduce
-
ce.parse('R_{A}\\cdot X_{\\mathrm{Ed}}-\\frac12\\cdot q\\cdot X_{\\mathrm{Ed}}^2-F\\left(X_{\\mathrm{Ed}}-A\\right)')
- don't work
ce.assign()
. how would I assign value to a variable with subscript?
Actual Behavior
when i try to assign
getting this error.
Expected Behavior
assign to value all the used variables in the formula and get an answer.
If you want the subscript to also be considered a single variable, you also need to wrap it, i.e. \mathrm{R_{A}}
.
The corresponding identifier is R_A
, so use that for assign, not the LaTeX representation.
@arnog Yeah that's working, But I have another confusion.
Based on your previous response, it appears that enclosing the variable with subscripts using \\mathrm
is necessary.
Is there a method to directly obtain the appropriately wrapped output from the math-field onInput
event?
You can provide a onInlineShortcut
handler that can automatically recognize multicharacter identifiers, based on the syntax you want to support. See https://cortexjs.io/mathlive/guides/shortcuts/#multicharacter-identifiers
Hey @arnog, thank you for your quick response. However, I am facing another problem. The onInlineShortcut
function doesn't seem to recognize a subscript input when I try to use it. I have updated the example in the sandbox.
I added an input math-field where I'm attempting to input subscript variables, but onInlineShortcut
fails to detect it.
I also consulted the documentation, but I was unable to find a solution.
Do you have any other ideas on how I could wrap my variables in \mathrm{}
to assign a value and calculate?
codesandbox link - here
@arnog How can I assign a value to a variable such as M_g,6.10a
?
I have included an example in the sandbox for reference.
In my example, I have the expression \\mathrm{M_{k,6.10a}}
. When try I to ce.assign("M_g,6.10a", 10);
I get an error.
Could you please assist me with this? How can I assign a value to variables in this manner?
Unfortunately, as the error message indicate, M_g,6.10a
is not a valid identifier. The ,
(and .
) characters are not allowed in identifiers. See https://cortexjs.io/math-json/#identifiers
@arnog Thanks for your response. Is there any way to get a list of used variables from LaTeX or a math field?
For instance, I have a LaTeX response in a math-field like this.
\\mathrm{R_{A}}\\cdot \\mathrm{X_{Ed}}-\\frac12\\cdot q\\cdot \\mathrm{X_{Ed}}^2-F\\left(\\mathrm{X_{Ed}}-A\\right)
.
Is there any method or function that can provide me with an array or object of variables?
Depending on what you're looking for, either symbols
or unknowns
:
-
ce.parse(...).unknowns
-
ce.parse(...).symbols