Message Boards Message Boards

0
|
32586 Views
|
6 Replies
|
5 Total Likes
View groups...
Share
Share this post:

|x| as the absolute value of x?

Posted 4 years ago

I'm sure I'm not the first to ask this, but I couldn't find another thread talking about this.

I was wondering why |x| isn't defined to be an abbreviation for Abs[x]?
Sure || is defined to be Or[...], but I think it's possible to distinguish between those two meanings:

  • If there is only one | you could look for the next | and put the symbols in between inside Abs[...]
  • The problem starts when there are two of them side by side. You wouldn't know it's for a nested absolute value like Abs[Abs[...]] or if it's the Or[...]
    So my proposal is:
  • || would still mean Or[...]
  • | |x|-y | would mean Abs[Abs[x]-y]
    So if there is a space between the vertical lines it means the nesting of Abs[...]

I think it wouldn't break any existing code because spaces aren't allowed between the vertical lines yet.
Really the only possible problem I see is that spaces aren't as clearly readable. And reading a line with double | could become a bit confusing, because you could only tell the meaning of it by understanding the context or trying to see if there is a space in between the lines or not.

What do you think about it? And what are the reasons against this feature?

Edit: I think even better would be to use Norm[...]. If I'm not mistaken this would be the same for single numbers, but would include dealing with lists.

POSTED BY: Nico G
6 Replies

(|) single "rawverticalbar" - this symbol is defined as Alternatives built-in symbol.

Example:

{a, b, c, d, a, b, b, b, d, d, c, b, b} /. a | b | d -> x

So, I think that´s why ..|b|.. cannot be Abs[b].

See: https://reference.wolfram.com/language/ref/Alternatives.html

POSTED BY: Claudio Chaib
Posted 4 years ago

Oh I didn't know about this. Thanks!

It still would be a cool feature, but I guess this makes it impossible to implement.

POSTED BY: Nico G
Posted 4 years ago

It might not be completely impossible to implement. Look up $PreRead in the documentation. Learning how to use that to implement your idea would take some time, study, experimenting and almost certainly some failures. Implementing your idea might break some parts of Mathematica, perhaps in surprising and possibly even impressive ways, and break some people's programs. But that doesn't mean it is impossible to do. Just be careful and don't get yourself or anyone else into trouble.

POSTED BY: Bill Nelson

How would you interpret |a|b|c|? As Abs[a*Abs[b]*c] or as Abs[a]*b*Abs[c]?

Have you considered using \[LeftBracketingBar] and \[RightBracketingBar]?

One more complication is that we have to deal with Abs[x] and RealAbs[x]. TraditionalForm does not make any visual distinction between the two, except in the Tooltip.

POSTED BY: Gianluca Gorni
Posted 4 years ago

How would you interpret |a|b|c|? As Abs[aAbs[b]c] or as Abs[a]bAbs[c]?

You're right. It's not clear what's meant here. But It wouldn't be clear to a human reader either.
If you type this exact phrase into Wolfram Alpha, it's interpreted as |a|*b*|c| and that seems reasonable to me.
What surprises me is that |(a|b|c)| isn't interpreted correctly by Wolfram Alpha.

Have you considered using [LeftBracketingBar] and [RightBracketingBar]?

Since \[VerticalBar] is used for Alternatives already this could be the way to go.
But for a human reader those Symbols look exactly the same. And it would not be clear if it's used for the mathematical purpose or the pattern purpose.

One more complication is that we have to deal with Abs[x] and RealAbs[x]. TraditionalForm does not make any visual distinction between the two, except in the Tooltip.

Maybe I'm missing something but isn't Abs[...] the general function? With a real input it would just work like RealAbs[...].

After reading the Documentation of Abs, Norm and Det I also realized that the behavior regarding Lists (Vectors and Matrices) aren't clear either.

  • Abs[x] would return the list of absolute values
  • Norm[x] would return the Norm (obviously)
  • Det[x] would return the Determinant of a matrix

All these meanings could be represented by |x|.
I'd say the intuitive meaning would be to use

  • Abs[x] if x is a single value. (Real or Complex)
  • Norm[x] if x is a one dimensional list or a N x M matrix with N=/=M
  • 'Det[x]' if x is a N x N matrix

But this would mean |x| couldn't be replaced with a definite function. But rather had to be evaluated in respect to the datatype of x.
This just wouldn't be coherent enough to be placed inside Wolfram Language.

POSTED BY: Nico G

Abs and RealAbs have different derivatives. Abs is meant to work in the complex domain, where it is not differentiable. RealAbs is differentiable in the real domain. Compare:

D[Abs[x], x]
D[RealAbs[x], x]
% // PiecewiseExpand

I wish there was a RealIntegrate that behaved this way:

RealIntegrate[1/x, x]

Log[RealAbs[x]]
POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract