1
NestList[RotateLeft, IntegerDigits[19], 1]
FromDigits[%]

(* WRONG *)

NestList[RotateLeft, IntegerDigits[197], 2]
FromDigits[%]

OK

Looks like a bug in 10.2 under windows? enter image description here

Or is it I misunderstood something when there are only two digits?? enter image description here

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
Chen Stats Yu
  • 4,986
  • 2
  • 24
  • 50
  • 5
    It's threading over the second sublist, e.g. FromDigits[{{1, 9}, {1, 2, 3, 4}}]. I'd use FromDigits /@ {{1, 9}, {9, 1}}. – ilian Jul 21 '15 at 16:44
  • FromDigits doesn't have the listable attribute so it doesn't automatically map over lists like some other functions do, which might be why it's confusing. – N.J.Evans Jul 21 '15 at 16:49
  • 4
    To expand on Ilian's comment, the form FromDigits[{list, n}] raises the number created from list to n, and in this case, when n is a list itself, e.g. FromDigits[{{1, 9}, {1, 2, 3, 4}}], you get {19/10, 19, 19^2, 19^3}. – rcollyer Jul 21 '15 at 16:50
  • 2
    By the way, this is another example of the same issue. – ilian Jul 21 '15 at 17:26
  • @rcollyer I dont get where the 0 comes from n 19/10? – Chen Stats Yu Jul 21 '15 at 19:35
  • 1
    My apologies, that wasn't as clear as it could be. FromDigits[{{a,b}, n}] returns 10^(-2 + n) (10 a + b) where 2 comes from the number of digits present in {a,b}. Does that help? – rcollyer Jul 21 '15 at 19:49

1 Answers1

8

As noted in the comments this behavior follows from the definition of FromDigits, though I only understood this myself within the last year or two when someone* used it to boost performance.

Consider a symbolic example:

sym = FromDigits[{a, b, c}]

sym /. {
     a -> {a1, a2, a3, a4, a5},
     b -> {b1, b2, b2, b4, b5},
     c -> {c1, c2, c3, c4, c5}
   }
10 (10 a + b) + c

{10 (10 a1 + b1) + c1, 10 (10 a2 + b2) + c2, 10 (10 a3 + b2) + c3, 10 (10 a4 + b4) + c4, 10 (10 a5 + b5) + c5}

Note that the evaluation of the expression sym threads over the substituted lists due to the properties of Times. Therefore these operations are also equivalent in output:

FromDigits /@ {{a1, b1, c1}, {a2, b2, c2}, {a3, b3, c3}, {a4, b4, c4}, {a5, b5, c5}}

FromDigits[{{a1, a2, a3, a4, a5}, {b1, b2, b3, b4, b5}, {c1, c2, c3, c4, c5}}]
{10 (10 a1 + b1) + c1,
 10 (10 a2 + b2) + c2,
 10 (10 a3 + b3) + c3,
 10 (10 a4 + b4) + c4, 
 10 (10 a5 + b5) + c5}

The performance of the two methods on long lists of short integers can be orders of magnitude apart however:

big = NestList[RotateLeft, IntegerDigits[12345], 5000];

(r1 = FromDigits /@ big)           // Length // RepeatedTiming
(r2 = FromDigits[big\[Transpose]]) // Length // RepeatedTiming

r1 === r2
{0.0025, 5001}

{0.0000953, 5001}

True

* I thought it felt longer but I believe I am recalling this answer by rasher/ciao:

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • 1
    I don't see how you get from the intro to FromDigits[big\[Transpose]], no matter how impressive that is. Would you elaborate? – rcollyer Jul 21 '15 at 17:15
  • @rcollyer I might be spewing nonsense; I apologize if that's the case. I'll have to try to think this through again but I am becoming increasingly tired. – Mr.Wizard Jul 21 '15 at 17:20
  • I blame the same thing on my lack of understanding. If you get to it, great. If not, oh well. – rcollyer Jul 21 '15 at 17:21
  • 1
    this makes perfect sense, take 10 (10 (10 a + b) + c) + d , with each of the a,b,c,d being lists you get from Transpose. – george2079 Jul 21 '15 at 17:44
  • 1
    Ah, I see it now. It must mean, I need more coffee. – rcollyer Jul 21 '15 at 17:56
  • and, as I look at it more, it is truly impressive. I can see where you'd get the speed boost. +1 (not that you need it) – rcollyer Jul 21 '15 at 17:59
  • 2
  • Why does this (Thread[f[big]] /. f -> FromDigits) work, but not (Thread[FromDigits[big]] ). I keep reading the docs for Thread for the part where it says it behaves differently depending on the function. – george2079 Jul 21 '15 at 18:02
  • @george2079 Thread doesn't hold its argument therefore you would need Thread[Unevaluated @ FromDigits @ big] I believe, or FromDigits will act first. – Mr.Wizard Jul 21 '15 at 18:04
  • 1
    I figured that trick would go into your toolbox ;-) – ciao Jul 21 '15 at 21:03