17
$\begingroup$

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

$\endgroup$
  • 2
    $\begingroup$ Related: (31797) $\endgroup$ – Mr.Wizard Jan 8 '15 at 19:07
  • 1
    $\begingroup$ Users interested in such question should consider Trace[x/## &[a, b, c]]. $\endgroup$ – Michael E2 Aug 4 '15 at 11:19
18
$\begingroup$

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.

$\endgroup$
  • 1
    $\begingroup$ Accepting this, because it covers both Minus and Divide. $\endgroup$ – Martin Ender Jan 11 '15 at 15:33
17
$\begingroup$
x/## & // FullForm
Function[Times[x,Power[SlotSequence[1],-1]]]

and Power[a,b,c...] == Power[a, Power[b, c...]] so now it should be clear.

This syntax is mentioned in the last bullet point in details of Power documentation.

$\endgroup$
  • 4
    $\begingroup$ beat me by a femtosecond $\endgroup$ – Dr. belisarius Jan 8 '15 at 18:08
  • $\begingroup$ Ah, thanks a lot. I was thinking it would probably be analogous to the minus case, but couldn't work it out. I think linking to the documentation for Divide would be more useful than linking to the documentation for Power though. $\endgroup$ – Martin Ender Jan 8 '15 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.