Closed jolyonb closed 6 years ago
Curious: What's the use-case? Tensor notation? Superscript notation for derivatives? Do people just want f^2
, or will they way f^(2,1)
as a variable also?
To elaborate on your comment, the issue is ambiguity: we want users to enter both "square this number" and "superscript variable" using the ^
character, but how does the parser know if x^2
is "x superscript 2" or "x squared". First, we'd want some validation:
x^2
is a variable, then x
should not be a variable, or theres some truly unresolvable ambiguity. f^2
is a function, it seems like f
could perhaps also be a function without causing problems.Here are two possible thoughts for implementation:
x^2
with a valid (unused) variable like (x1
). This might complicate sampling and error messages. This seems a bit messy.Edit: Actually, sincere variables names appear in error messages, it might be easier to replace all of the squaring-carets with some other character (e.g., **
). So if we have variables ['a', 'x^2', 'y^2']
, we'd pre-process a user input like 'a^2+x^2+y^2
to a**2 + x^2 +y^2
.
x^2
and y^2
are declared as variables, alter the parser to usesuperscript_vars = ['x^2', 'y^2'] # would be passed as an argument
inner_varname = Combine(
Or(superscript_vars) |
(
Word(alphas + "_", alphanums + "_") +
ZeroOrMore("'")
)
)
This correctly recognizes a^2
as "a squared", but treats x^2
and y^2
as single variables:
<sum>
<product>
<parallel>
<power>
<atom>
<variable>
<ITEM>a</ITEM>
</variable>
</atom>
<ITEM>^</ITEM>
<atom>
<number>
<ITEM>2</ITEM>
</number>
</atom>
</power>
</parallel>
</product>
<ITEM>+</ITEM>
<product>
<parallel>
<power>
<atom>
<variable>
<ITEM>y^2</ITEM>
</variable>
</atom>
</power>
</parallel>
</product>
<ITEM>+</ITEM>
<product>
<parallel>
<power>
<atom>
<variable>
<ITEM>x^2</ITEM>
</variable>
</atom>
</power>
</parallel>
</product>
</sum>
Caveat about the second approach: The order of parsing terms in inner_varname seems to be important—superscript_vars needs to come before the normal variables—and I do not completely understand why that is yet.
Simona wanted to have tensor notation. I'm not sure if she wanted abstract index notation or just specific component notation. I was thinking something like a^{ijk} might be possible, using the curly braces to specify that indices were coming.
I haven't checked how asciimath parses a^{ijk} though. On a related note, did you know that "oo" in asciimath renders infinity? Do we want to add that to the integral grader?
Your idea is much simpler, and I believe it displays correctly (at least in textline math). I like it!
See title! Not obvious how you'd implement this...