mitodl / mitx-grading-library

The MITx Grading Library, a python grading library for edX
https://edge.edx.org/courses/course-v1:MITx+grading-library+examples/
BSD 3-Clause "New" or "Revised" License
14 stars 10 forks source link

Request for variable names with superscripts #11

Closed jolyonb closed 6 years ago

jolyonb commented 6 years ago

See title! Not obvious how you'd implement this...

ChristopherChudzicki commented 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:

Here are two possible thoughts for implementation:

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.

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

jolyonb commented 6 years ago

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.

jolyonb commented 6 years ago

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?

ChristopherChudzicki commented 6 years ago

Your idea is much simpler, and I believe it displays correctly (at least in textline math). I like it!