Bug introduced in 8.0 or earlier and persisting through 11.0.1 or later

While answering another question I discovered that TriangleWave does not automatically evaluate when given certain fractional values, specifically fractions with a denominator of 20:

TriangleWave[ Range[8] / 20 ]
{TriangleWave[1/20], 2/5, TriangleWave[3/20], 4/5, 1, 4/5, TriangleWave[7/20], 2/5}

These are reduced by Simplify:

TriangleWave[ Range[8] / 20 ] // Simplify
{1/5, 2/5, 3/5, 4/5, 1, 4/5, 3/5, 2/5}

This appears to be the only denominator under 10,000 that does not automatically evaluate:

Cases[TriangleWave[1/Range[1*^5]], _TriangleWave]

Is there a reason to believe that this behavior is anything other than a bug in TriangleWave?

  • $\begingroup$ It only seems to evaluate when (2 ArcSin[Sin[2 Pi t]])/Pi is an explicit NumberQ $\endgroup$ – Rojo Jan 9 '14 at 3:27
  • $\begingroup$ Interesting question - I've wondered sometimes how MM decides (and why) to not simplify. E.G., 4/Pi (ArcSin[Sin[2/20 Pi]]) results in a trig form, while 4/Pi (ArcSin[Sin[3/20 Pi]]) simplifies, yet both are rationals... $\endgroup$ – ciao Jan 9 '14 at 4:00
  • $\begingroup$ @rasher I have raised a question about that here. $\endgroup$ – Mr.Wizard Jan 9 '14 at 9:11
  • $\begingroup$ @Mr.Wizard I have added the bug header. Is the introduction version correct? I don't have access to earlier version than 9.0 at the moment. $\endgroup$ – user31159 Oct 8 '16 at 12:00
  • $\begingroup$ @Xavier, version 8 has the bug; since TriangleWave[] was introduced in version 7, testing that as well (by somebody other than you or me) should make for an accurate record. $\endgroup$ – J. M.'s torpor Oct 8 '16 at 12:09

Rojo's comment is spot on. If you look at the implementation of TriangleWave, you'll find something like this:

TriangleWave[t_?NumberQ] := With[{r = 2 ArcSin[Sin[2 π t]]/π}, r /; NumberQ[r]] /; Im[t] == 0

Note the use of NumberQ in the definition, which only checks if the argument is explicitly a number. For certain values of your input, ArcSin[Sin[2 π t]]/π is False for NumberQ:

{#, NumberQ@#}& /@ (ArcSin[Sin[2 π Range[8]/20]]/π) // TableForm

They should've used NumericQ instead of NumberQ.

  • $\begingroup$ How do you find the implementation? $\endgroup$ – s0rce Jan 9 '14 at 4:12
  • $\begingroup$ @s0rce You can clear the attributes and look at the definitions. $\endgroup$ – rm -rf Jan 9 '14 at 4:13
  • 1
    $\begingroup$ @s0rce Something like this peep[s_] := Module[{at}, at = Attributes[s]; ClearAttributes[s, at]; Print@Definition@s; SetAttributes[s, at]; ]; peep[TriangleWave] $\endgroup$ – Dr. belisarius Jan 9 '14 at 5:34
  • $\begingroup$ Thanks for the exploration. It sounds like you agree that this is a bug; is that correct? $\endgroup$ – Mr.Wizard Jan 9 '14 at 8:48
  • $\begingroup$ @Mr.Wizard Yes, I agree that it is a bug. $\endgroup$ – rm -rf Jan 9 '14 at 14:49

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.