# Algebra.js to Solve For Variable in Terms of Chosen Variables

I would like to define a system of equations in JavaScript with algebra.js (say it has variables a, b, c, and d). I would then like to solve for a chosen variable (say a) in terms of other chosen variables (say b and c).

If there is only one numerical value for a, it would just return that value. If it could be written just in terms of b, it would return a(b). It it could be written in terms of b and c, it would return a(b,c). If b, c, and d are all required (if there’s only one equation for example), it would return a(b,c,d). If there’s no symbolic solution or there’s a contradiction in the SoE, it would return an error. Basically, it would try to output an expression in terms of the fewest variables possible, preferably chosen variables.

How difficult would it be to do something like this?

This is one of those problems that sounds easy but is really hard. You need to be able to parse the syntax.

Honestly, JS wouldn’t be the language I’d use for this sort of thing.

1 Like

I looked at algebra.js a bit more and I realize it doesn’t have all the features I thought it did. To clarify, I would want the library to already be able to solve a system of nonlinear equations symbolically (assuming it’s possible of course). My challenge would be to tell it what to solve for and in terms of what.

The reason for choosing JavaScript was because I would want it to be on a website. I realize that Nerdamer might have what I need. It can solve an equation in terms of other variables (solveFor) and solve a system of equations if there are unique values (solveEquations).

Then the challenge would be to get it to solve a system of equations in terms of other variables. I think I might be able to do that by using solveFor and solveEquations in combination.

I think math.js might be able to do it? Solving a system of non-linear equations symbolically is a huge ask for a JS package.

It seems nerdamer is generally able to solve equations symbolically – there are some issues, like “tan(y)=x” gives “y=atan(x)” but “sin(y)/cos(y)=x” gives “”[ ]"" – but overall it seems ok. Maybe there are better options for incorporating in a web app?

The other challenge would be solving a system of equations in terms of specified variables (like I described in the original post). I’m not sure if there’s an easy way of doing this.

I would like to use Mathquill, Guppy, or some other natural equation editor. I want sections of the equation to be editable depending on the content, and I’m curious if there’s a way to mimic this effect, even if it’s not built-in. My idea was to access the span containing the editable section and make it highlighted white and editable as opposed to using the Mathquill or Guppy API. However, finding the correct span to modify using the DOM could be very difficult (only the innermost spans containing individual characters have inner HTML it seems). Do you think this would run into any issues, and would there be a better or easier way of doing it? Thank you!

To give a specific example, I would want the user to be able to type in solve and (similar to how typing sin changes the display), it would switch to:

solve {editable section} for {editable section}

with “solve” and “for” being non-editable.

Update: I found \MathQuillMathField in the documentation, which apparently allows for editable fields in noneditable latex.

1 Like

I’m planning on using a WYSIWYG equation editor for a site to perform symbolic math, and I want to control the display, specifically creating and removing editable fields, based on what a user types and where the cursor is located.

For example, if the user types solve into an editable field, it should convert to:
\text{solve} [Editable Region] \text{for} [Editable Region]

As another example, I want the user to be able to fill in a matrix of values, each value being an editable field. I would like the matrix to always have one more number of rows than filled rows and one more number of columns than filled columns, so the user can keep on expanding the matrix. Then when the user clicks out of the matrix, it would check if a grid of values are filled, and if they are, it would replace the editable regions with static math and remove the extra row and column.

Would this be reasonable to do, and if so what editors would you recommend (I was looking into MathQuill)?

Thanks.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.