compute-engine icon indicating copy to clipboard operation
compute-engine copied to clipboard

The ce.assign() function not working properly

Open Nits7029 opened this issue 1 year ago • 10 comments

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 as Q_{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

  1. first I write formula and parse into compute engine like this

const ce = new ComputeEngine.ComputeEngine(); const f = ce?.parse(x+y+xyz);

  1. Then i use ce.assign()to assign values to x, y and xyz like this.

ce?.assign('xyz', 22); ce?.assign('x', 10); ce?.assign('y', 20);

  1. 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.

Nits7029 avatar Oct 02 '23 06:10 Nits7029

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 avatar Oct 02 '23 15:10 arnog

@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

  1. ce.parse('R_{A}\\cdot X_{\\mathrm{Ed}}-\\frac12\\cdot q\\cdot X_{\\mathrm{Ed}}^2-F\\left(X_{\\mathrm{Ed}}-A\\right)')
  2. don't work ce.assign(). how would I assign value to a variable with subscript?

Actual Behavior

when i try to assign Screenshot 2023-10-05 at 2 54 09 PM getting this error.

Screenshot 2023-10-05 at 2 54 34 PM

Expected Behavior

assign to value all the used variables in the formula and get an answer.

Nits7029 avatar Oct 05 '23 09:10 Nits7029

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 avatar Oct 05 '23 17:10 arnog

@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?

Nits7029 avatar Oct 09 '23 12:10 Nits7029

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

arnog avatar Oct 09 '23 15:10 arnog

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

Nits7029 avatar Oct 23 '23 12:10 Nits7029

@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. Screenshot 2023-10-30 at 4 58 07 PM

Could you please assist me with this? How can I assign a value to variables in this manner?

Nits7029 avatar Oct 30 '23 11:10 Nits7029

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 avatar Oct 30 '23 17:10 arnog

@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?

Nits7029 avatar Nov 02 '23 06:11 Nits7029

Depending on what you're looking for, either symbols or unknowns:

  • ce.parse(...).unknowns
  • ce.parse(...).symbols

arnog avatar Nov 02 '23 14:11 arnog