Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
It only seems to evaluate when (2 ArcSin[Sin[2 Pi t]])/Pi is an explicit NumberQ – Rojo Jan 9 '14 at 3:27
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... – ciao Jan 9 '14 at 4:00
@rasher I have raised a question about that here. – Mr.Wizard Jan 9 '14 at 9:11
up vote 7 down vote accepted

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.

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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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