I've been playing around with sequences a bit. In particular with using ## with unary and binary operators.
Let's start simple, the following all make some kind of sense:
+ ## & [a,b] (* a + b *)
x + ## & [a,b] (* x + a + b *)
x * ## & [a,b] (* x * a * b *)
x ^ ## & [a,b] (* x ^ a ^ b *)
Now here is a slightly weird case:
- ## & [a,b] (* -a*b *)
x - ## & [a,b] (* x - a*b *)
I guess, this sort of makes sense if - is actually interpreted as something like +(-1)*. But it also means that +##-## is generally non-zero.
But now here's the real puzzle:
x / ## & [a,b] (* x a^(1/b) *)
x / ## & [a,b,c] (* x a^b^(1/c) *)
Wh... what? Can anyone explain what's happening here or at least give some justification like the one for subtraction? Answers which correct my explanation for subtraction are also welcome!
(No, I would never use this stuff in production code. But knowing what exactly is going on under the hood could come in handy some time.)
Bonus Question: Are there any other operators that yield unexpected and potentially "useful" results? (I mean, !## will yield Not[a,b] but that's neither very unexpected nor useful.)
Answer
The documentation for Minus states that
-x is converted to
Times[-1,x]on input.
So -Sequence[a,b] == Times[-1,Sequence[a,b]] == Times[-1,a,b] by this definition. Similarly the documentation for Divide states that
x/y is converted to x y^-1 on input.
and therefore x / Sequence[a,b] == x Sequence[a,b]^-1. Sequence[a,b]^c == Power[a, Power[b,c]]. When c == -1 you get Power[b, -1] == 1/b.
Comments
Post a Comment