4
$\begingroup$

Bug introduced in 8.0 or earlier and fixed in 11.0.0


Why does the code

Function[t, SeriesData[t, 0, {0}, 0, 1, 1]]

give me the following error?

First::nofirst: {} has zero length and no first element.

First, why in the world is SeriesData even evaluated inside the Function?

Second, what exactly is the error? Can't a series be zero?

$\endgroup$
  • $\begingroup$ Have you tried to click on the >> sign? $\endgroup$ – user9660 Mar 11 '16 at 8:16
  • $\begingroup$ @Louis: Yes I have, and it was completely irrelevant. Have you found it relevant? $\endgroup$ – Mehrdad Mar 11 '16 at 8:19
  • 1
    $\begingroup$ (continued), but both of those SeriesData expressions have the same InputForm, that is InputForm[SeriesData[t, 0, {}, 1, 2, 1]] === InputForm[SeriesData[t, 0, {0}, 1, 2, 1]] evaluates to True $\endgroup$ – Jason B. Mar 11 '16 at 8:51
  • 1
    $\begingroup$ @Dr.belisarius - but Hold@SeriesData[t, 0, {}, 0, 1, 1] does not give the error. Something about the internal representation of SeriesData when the coefficients are zero $\endgroup$ – Jason B. Mar 11 '16 at 8:52
  • 3
    $\begingroup$ I believe the more general question is why Hold@SeriesData[t, 0, {0}, 0, 1, 1] reports an error. The Function[ ] thing is holding it, hence the error $\endgroup$ – Dr. belisarius Mar 11 '16 at 9:02
7
$\begingroup$

This is a formatting issue, that I think should be reported to WRI.

Quick and dirty workaround could be something like this:

Unprotect[SeriesData];
SeriesData /: MakeBoxes[
    ser : SeriesData[
        x_, x0_,
        {__?(Function[Null, MiscDump`effectiveSign@HoldComplete[#] === "0", HoldAllComplete])},
        nmin_Integer, nmax_Integer, den_Integer
    ],
    fmt_
] :=
    MakeBoxes[SeriesData[x, x0, {}, nmin, nmax, den], fmt]
Protect[SeriesData];

Now outputting held series data doesn't result in error messages.

SeriesData[t, 0, {0}, 0, 1, 1] // Hold
SeriesData[t, 0, {0.}, 0, 1, 1] // Hold
SeriesData[t, 0, {0 a}, 0, 1, 1] // Hold
SeriesData[t, 0, {Print["leak"] 0}, 0, 1, 1] // Hold
SeriesData[t, 0, {0, 0., 0 a, Print["leak"] 0}, 0, 1, 1] // Hold
(* Hold[O[t]^1]  (in all cases) *)

In Function[t, SeriesData[t, 0, {0}, 0, 1, 1]] expression SeriesData[t, 0, {0}, 0, 1, 1] is not evaluated, but when this expression is outputted in the FrontEnd it is formatted.

Rules used for formatting of SeriesData can be found in FormatValues[SeriesData]. We can see there two rules: first for empty list of coefficients, second for non-empty. In second rule we can find DeleteCases[MiscDump`signsAndTerms, {"0", _}] code followed by First[MiscDump`signsAndTerms], where MiscDump`signsAndTerms is list containing certain special representation of series terms. Above DeleteCases removes terms corresponding to 0 coefficients.

It seems that possibility that there will be only 0 coefficients was overlooked and in such case First[MiscDump`signsAndTerms] receives empty list which results in First::nofirst error.

Formatting of non-held SeriesData expression with zero-only coefficients does not result in error, because such expression evaluates to SeriesData with empty list of coefficients which is covered by first formatting rule.

SeriesData[t, 0, {0}, 0, 1, 1] // InputForm
(* SeriesData[t, 0, {}, 1, 1, 1] *)
$\endgroup$
  • $\begingroup$ Interesting, thanks! It's funny that I've only been seriously coding in Mathematica for a week or so and I've run into bugs already. $\endgroup$ – Mehrdad Mar 11 '16 at 18:08
  • $\begingroup$ Perhaps this question should have the bugs tag; what does everyone think? $\endgroup$ – Mr.Wizard Mar 11 '16 at 20:45
  • 1
    $\begingroup$ An easy bug to fix. One comment: it would be better to write {0 ..} instead of {(zero_ /; zero == 0) ..}. The latter allows the arguments to evaluate (which may have bad consequences--onsider {Print[foo}), and its much slower. $\endgroup$ – Itai Seggev Mar 15 '16 at 19:27
  • 2
    $\begingroup$ One other minor point: Series would never produce a list only containing zeroes--the canonical form is an empty list--which is probably how this bug snuck through. $\endgroup$ – Itai Seggev Mar 15 '16 at 19:35
  • 1
    $\begingroup$ Another refinement: you don't need Repeated, i.e. _?test .., as __?test will do the same thing. $\endgroup$ – Mr.Wizard Mar 16 '16 at 10:44

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.