8

I am writing an equation parser for fun and I kinda thought parsing absolute value signs would be like parsing ordinary bracktets - but this is not so as there is no 'open' or 'close' version of $|$. In particular there are two potential interpretations of the expression $$ |x|x|x|,$$ namely $$ |x|x|x| = |x|\cdot x \cdot |x| = x^3,$$ or $$|x|x|x| = {\large|} x\cdot |x| \cdot x{ \large |} = |x^3|. $$

I think there is no agreed upon convention that determines which one of these should be correct. But I was wondering if there are any best practices that we can give some kind of arguments for.

I realize that the expression $|x|x|x|$ looks ridiculous, but we can also have for example $$ |x|f(x) + g(y)|y| . $$ I would certainly parse this as $(|x|f(x)) + (|y| g(y))$ but who am I to say it does not equal $|x \cdot |f(x)+g(y)| \cdot y|$.

Edit: The parser I wrote already accepts $\operatorname{abs}(x)$ and the like. But I want to allow the user to write the very natural $|x|$ as well. But then I can't prevent them for also writing $|x|x|x|$, even though this is silly and borderline criminal. So I do have to make some choice on how to handle this and am just wondering if anyone has any preference for one of the interpretations and maybe some reasons :)

Slugger
  • 5,736
  • 1
  • 29
  • 58
  • 14
    That's awful notation. Anyone who writes nested absolute values without adjusting their sizes should be banned from writing mathematics. – Trebor Aug 20 '21 at 12:22
  • 1
    @Trebor Harsh but fair :) Anyway, when typing into a simple graphical calculator you cannot adjust sizes, and I will need to choose some interpretation as I don't want to simply crash the program when a user enters a clumsily written expression. For what it's worth, Wolfram says $|x|x|x| = x^3$. Moreover, things like $||x-5|-3|$ are common place, but this can of course be interpreted correctly in only one way. – Slugger Aug 20 '21 at 12:26
  • I think any graphical calculater excepts the abs() notation. So you can write simply abs(xabs(x)x) or something like that. – Cornman Aug 20 '21 at 12:35
  • 7
    I understand that the OP wants to choose an interpretation/to parse this notation, which is okay: just choose one or the other and be consistent with it. However, I believe the actual question in the OP is about best practices; in that vein, I would definitely say that best practice is to never use notation like this. – Greg Martin Aug 20 '21 at 12:40
  • @GregMartin Fair enough. I think I didn't make it clear that I am will be looking at user input, which is not required to be sensible, but I want to parse it if at all possible :) I thought it might be interesting to ponder the issue. – Slugger Aug 20 '21 at 12:43
  • 1
    I think that $|x|x|x| = |x|\cdot x \cdot |x|$ is the most natural. It also gives less backtracking. – md2perpe Aug 20 '21 at 12:50
  • @md2perpe Yeah that's why I'm leaning that way at the moment. Thanks for the response – Slugger Aug 20 '21 at 12:51
  • Part of the problem is the implicit multiplication. Expanding on your example, you can have $|x|x\cdot|x|$ or $|x|x|\cdot x|$. This means that it is impossible to parse without looking ahead or backtracking - the meaning of the first | signs depends on the context of the later | signs. – Jaap Scherphuis Aug 20 '21 at 12:52
  • Wolfram alpha has preference for $|x|x|x|=|x|\cdot x \cdot |x|$. The ApproachZero forces you to specify opening and closing lines when entering a math equation. But in the end, they both use latex to display the result and use longer lines to distinguish outer and inner lines. – Vepir Aug 20 '21 at 12:57
  • @JaapScherphuis But how would $|x|x|\cdot x|$ make sense in any context? There is no way to match the $|$'s here that gives anything meaningful I think. I think there might a single pass way to parse these expressions, always trying to greedily interpret a $|$ as a closing one whenever possible – Slugger Aug 20 '21 at 12:59
  • $|x|x|\cdot x|$ is just your second interpretation $|x\cdot |x|\cdot x|$ with only the first multiplication made implicit instead of both. – Jaap Scherphuis Aug 20 '21 at 13:02
  • @JaapScherphuis Ah right :) I guess that underlines the confusion that can arise – Slugger Aug 20 '21 at 13:04
  • Perhaps more challenging to parse: $|a|b|c+d|e|f+g|h|i|$. – Barry Cipra Aug 20 '21 at 13:10
  • The problem with the Wolfram Alpha parsing is that the handling of absolute values can change. For example |x|x|x| is parsed to $|x| \cdot x \cdot |x|$ so "closing first" is prefered by WA here. But if you just omit the last x and write |x|x|| then it is parsed to $|x \cdot |x||$ so in this example "opening first" is prefered by WA, presumably because prefering the closing interpretation would give an invalid expression namely $|x| \cdot x \cdot ||$ which makes no sense (the last absolute value group would have an empty argument). I would probably prefer issuing a syntax error instead. – SampleTime Aug 20 '21 at 14:56
  • @SampleTime I will likely take exactly the approach that WolframAlpha takes: Trying to close first if possible, else parse 'outside to inside'. But in any case issue just a warning when there is ambiguity so the user is aware and can clearly write what they intend – Slugger Aug 20 '21 at 14:59

3 Answers3

2

I think, when you really need to write $|x.|x|.x|$, it's better to prefer $\bigg |x|x|x\bigg |$ or even better $\bigg|x^2|x|\bigg|$. So, in case of $|f(x).|g(x)|.h(x)||$, better use $\bigg|f(x)h(x)|g(x)|\bigg|$.

And, I think, we can keep $|x|x|x|$ as $|x|.x.|x|$ as that's how you read it from left to right. I guess, that is the reason Wolfram prefers this version.

And, if the input is to be typed in by a user, I think, it's better to use $*$ or $\times$ or $\cdot$ (according to where it is being typed) to denote multiplication and maintain clarity.

Sayan Dutta
  • 10,345
  • Thanks for the response. The input given will simply be typed by a user, as in for example the wolframalpha site. So the user has no opportunity to format their input for readability. – Slugger Aug 20 '21 at 14:10
  • @Slugger Please check the edit. Thanks. – Sayan Dutta Aug 20 '21 at 14:38
  • Yeah that would certainly work. However, I do want to allow people to write things like $2|x|$ etc. – Slugger Aug 20 '21 at 14:55
1

I would suggest that you make up some rules in which situations multiplication must be written using a dot or asterisk ($3 \cdot 4$ or $3 * 4$) and when they don't. Hopefully these rules make |x|x|x| illegal and require $|x| \cdot x \cdot |x|$.

These rules may be complicated, for example I think 2x is quite acceptable, but x2 isn't (for two times x).

gnasher729
  • 10,611
  • 20
  • 38
  • Thanks for the response! Currently I am allowing writing $x\ 2$ and interpreting as $x\cdot 2$. However, I do give a warning that this is suspicious. I will probably do the same for multi-interpretable absolute value signs, give a warning but still parse in whatever way is easiest. The reason is that I would prefer there be no complex rules for the user to follow, so that there is a low threshold for entry, and simply give a heads up when things are suspicious – Slugger Aug 20 '21 at 14:03
1

As you say there are no open and close versions of $\mid$. That being the case, my first thought would be that the second $\mid$ encountered by the parser should match with the first (unless prioritised with brackets), so $\mid x\mid x\mid x \mid = x^3$.
In my (limited) experience of writing parsers, tokens are best dealt with at the first opportunity.

Peter Phipps
  • 3,100
  • Thanks for the response! Indeed I will probably try to match the absolute value signs at the earliest possible opportunity. This might still be a nuisance as $|1 |x|x|$ will match the first two and last two $|$ signs while $|-|x|x|$ only makes sense when matching the outer and inner pairs. – Slugger Aug 20 '21 at 14:12
  • 1
    I would say that $\mid -\mid x\mid x\mid$ has no meaning at all. – Peter Phipps Aug 20 '21 at 14:23
  • I would read it as $| (-|x|) \cdot x|$, maybe a slightly more realistic example is $|1-|x|x| = |(1 - x|x|)|$. – Slugger Aug 20 '21 at 14:24