Closed stefnotch closed 2 years ago
Great question!
The name of a symbol can include a _
underbar. So {sym: 'A_tri'}
represent the variable A_tri
.
If no matching symbol is found, the _
is interpreted as the Subscript
operator, so a_2
would become ["Subscript", "a", 2]
.
In addition, it is possible to "construct" symbols, using the Symbol
operator: ["Symbol", "'a'", 2]
is a synonym for the symbol a_2
. The Symbol
operates cast all its arguments to strings (by applying the String
operator on them), then concatenate them to form a symbol.
Oh, lovely! I guess using strings to represent variables works quite nicely in this case.
Which brings me to a more difficult one: I've seen variables with a tilde above them. And they're distinct from variables without one. Like this:
How would one represent that? Would it possible require the inclusion of a new MathJson function?
You have two options:
The parser can parse these LaTeX expressions (presumably, the first one is something like \overset{\triangle}f
and the other one \tilde{f}
and map them to whatever symbol name you'd like, perhaps f_with_triangle_above
and f_with_tilde_above
, or something more semantic if appropriate (which would be preferred)
Represent the "decorations" around the symbol as function applications, so \tilde{f}
would become ["OverTilde", "f"]
. This is how the Compute Engine currently handles it, although of course you can override it to implement the first solution if you prefer (with a custom parsing dictionary). The default Compute Engine dictionary does not have a definition for the delta-above, though, so one would need to be added.
Sounds good, I'll check out both options then.
Regarding the ["OverTilde", "f"]
option, would using the Hold
function be reasonable there? As in ["Hold", ["OverTilde", "f"]]
By default (i.e. the version that's in the Compute Engine default dictionary), the OverTilde
is an "inert" function, that is it is used to "decorate" another expression, and provide some information about it. However, evaluating it does not change it, that is ce.box(["OverTilde", "f"]).evaluate().json
is ["OverTilde", "f"]
.
The Hold
function is useful to prevent an evaluation, but in this case, since the evaluation doesn't do anything, the Hold
function is not necessary.
Sometimes, there are variables with a subscript. For example, when one has a triangle and a square and wants to calculate their areas:
A_tri
andA_squ
. In this case, they're clearly two different variables.However, in a different context, one has matrices. To index the first row/column (depending on convention), one would write
a_1
. To index the second one, it would bea_2
. In that case, they're referring to the same variable.The same thing applies to
f(x) = 3x^2 + 2x
, thenf'
means "the first derivative of f"a'
Is there a general way of distinguishing between those two options? As in, something along the lines of
["Symbol/Variable/Function/..", ["Subscript", "a", "t"]]
vs["Subscript", "a", "1"]
. The former would mean "this entire thing is the variable name" while the latter would mean "the subscript probably has a special meaning"