4
$\begingroup$

I've been using the combination

ConstantArray[0, {}]

(* 0 *)

in my code for some time, as a way for returning zero (i.e. a zero-dimensional array of zeroes) inside scalable code that can be made to produce empty arrays of arbitrary dimensions but also zeros. On my system (currently v11 over linux) it's always worked well since I introduced it.

However, when I tried to run the same code on other systems, you sometimes get the following:

ConstantArray[0, {}]
ConstantArray::ilsmn : Single or list of non-negative machine-sized integers 
   expected at position 2 of ConstantArray[0, {}]

(*Out[]:= ConstantArray[0, {}]*)

The above screenshot was taken over version 10 on OSX; a separate user also observed this in v8 over Windows 7.

I find this confusing, and I'm pretty sure it's a bug. In particular, the documentation for ConstantArray claims that its history is

Introduced in 2007 (6.0) | Updated in 2008 (7.0)

which means that there should be no change between v8 and v11. I will file a bug report shortly, but mostly I would like to build a workaround (since I want my code to be usable on systems like e.g. v10/OSX). At present, thus, my main question is: on which systems does this behaviour show up, and on which ones does it return a plain 0 without complaints? I suspect this depends both on the specific version and the operating system, but it'd be nice to know for sure.

$\endgroup$
  • 1
    $\begingroup$ Seems to be OK in 11.01, Mac os $\endgroup$ – bill s Dec 8 '16 at 15:33
  • $\begingroup$ Works for me on 11.0.1, Windows 10 $\endgroup$ – lowriniak Dec 8 '16 at 15:37
  • $\begingroup$ I tried it on 10.4.1 running on OS X. No problem. $\endgroup$ – m_goldberg Dec 8 '16 at 15:43
  • $\begingroup$ This fails in version 7.0.1 but works in 10.1.0 under Windows 7 x64. $\endgroup$ – Mr.Wizard Dec 8 '16 at 15:47
  • 1
    $\begingroup$ I don't think you can call it a bug since there is nothing in the docs that indicates the zero should be expected behavior. The docs do say ConstantArray[c,dims] has Dimensions[dims]` which fails here as Dimensions[{}] != Dimensions[0] $\endgroup$ – george2079 Dec 8 '16 at 15:57
4
$\begingroup$

This was a change introduced in version 10.1.0 (on all platforms).

The new behavior is more consistent with Array, e.g.

Array[f, {}]                                                            

(* f[] *)

Array[Function[0], {}]

(* 0 *)
$\endgroup$
  • $\begingroup$ Thanks for this. So do all <10.1.0 versions produce the same error and the unevaluated return? $\endgroup$ – Emilio Pisanty Dec 8 '16 at 16:52
  • $\begingroup$ (Either way, it still strikes me as a potential documentation bug - if this behaviour is settled, shouldn't it be reported in the documentation, and marked as the last change in the function?) $\endgroup$ – Emilio Pisanty Dec 8 '16 at 16:53
  • $\begingroup$ Yes (starting with 6.0 of course). $\endgroup$ – ilian Dec 8 '16 at 16:53
  • 3
    $\begingroup$ In some cases it would be safer to interpret the 'last updated in' markup as referring to the documentation page content, not necessarily the function implementation. $\endgroup$ – ilian Dec 8 '16 at 16:56

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.