I am working on creating a function that takes in any physics equation as a string and converts it to an equation that I will then paste into an interactive interface as asked here: How to make an interactive calculator for any mathematical relation?. I am implementing the first answer as a function that accepts any physics equation as a string and does two things before replacing the example of a^2+b^2==c^2.
First I want to replace any instance of "=" with "==" except for an instance of "==". The goal of this is to make a user-friendly function if the user enters an equation with incorrect programming syntax. I will then convert it to an expression.
Second, after it has been converted to an expression I will extract the symbols from the equation using this code, which I got from How to find all symbols in an expression and perform an operation on them?.
Symbols[x_String] :=
DeleteDuplicates@Cases[ToExpression[x], _Symbol, Infinity]
The function for the first program is as follows:
StringReplace["\!\(\*SuperscriptBox[\(a\), \
\(2\)]\)+\!\(\*SuperscriptBox[\(b\), \
\(2\)]\)==\!\(\*SuperscriptBox[\(c\), \(2\)]\)",
Except[{"=="}, "="] -> "=="]
I am trying to base my code off this example
StringReplace["the cat in the hat",
Except[Characters["aeiou"], "c"] -> ""]
Unfortunately, this causes an error:
I am not sure what I am doing wrong here.
EDIT: I have my attempt here: https://www.wolframcloud.com/obj/burbery1/Published/Interactive%20Solver%20Function.nb In the process I came across two more unexpected hurdles: if the variable the user is solving for was already defined in a previous call of Interactive Solver or another piece of program that already executed, the symbol is regarded as a numerical value and not a variable.
The second problem is that the InteractiveSolver function returns a list of Null values when I run it for some reason.
I tried some code provided as an answer to this question and encountered the following error:
Symbols
will return symbolic numeric constants as well as variables, e.g., look at the result ofSymbols["Pi*x + E"]
. Either modify the definition toSymbols[x_String] := DeleteDuplicates@Cases[ToExpression[x], _Symbol?(!NumericQ[#]&), Infinity]
or useSymbols[x_String] := Variables[Level[x, {-1}]]
$\endgroup$Interpreter[]
might be fast enough for you:parse = Interpreter["MathFormula"]; parse["a=b^2"]
. Parsing takes a little less than a second, so it's not fast. $\endgroup$