Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The ce.assign() function not working properly #120

Open
2 of 4 tasks
Nits7029 opened this issue Oct 2, 2023 · 10 comments
Open
2 of 4 tasks

The ce.assign() function not working properly #120

Nits7029 opened this issue Oct 2, 2023 · 10 comments

Comments

@Nits7029
Copy link

Nits7029 commented Oct 2, 2023

Heyy @arnog , you can visit here and check
https://codesandbox.io/s/infallible-worker-kdshn7

UPDATE

  • ce.assign() takes a value without brackets..
  • 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 Nits7029 changed the title I think this case should be work with assign function! The ce.assign() function not working properly Oct 2, 2023
@arnog
Copy link
Member

arnog commented Oct 2, 2023

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)

@Nits7029
Copy link
Author

Nits7029 commented Oct 5, 2023

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

@arnog
Copy link
Member

arnog commented Oct 5, 2023

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.

@Nits7029
Copy link
Author

Nits7029 commented Oct 9, 2023

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

@arnog
Copy link
Member

arnog commented Oct 9, 2023

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

@Nits7029
Copy link
Author

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
Copy link
Author

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

@arnog
Copy link
Member

arnog commented Oct 30, 2023

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

@Nits7029
Copy link
Author

Nits7029 commented Nov 2, 2023

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

@arnog
Copy link
Member

arnog commented Nov 2, 2023

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants