Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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]
{TriangleWave[1/20]}

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 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... –  rasher Jan 9 at 4:00
    
@rasher I have raised a question about that here. –  Mr.Wizard Jan 9 at 9:11
add comment

1 Answer

up vote 5 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 at 4:12
    
@s0rce You can clear the attributes and look at the definitions. –  rm -rf Jan 9 at 4:13
1  
@s0rce Something like this peep[s_] := Module[{at}, at = Attributes[s]; ClearAttributes[s, at]; Print@Definition@s; SetAttributes[s, at]; ]; peep[TriangleWave] –  belisarius Jan 9 at 5:34
    
Thanks for the exploration. It sounds like you agree that this is a bug; is that correct? –  Mr.Wizard Jan 9 at 8:48
    
@Mr.Wizard Yes, I agree that it is a bug. –  rm -rf Jan 9 at 14:49
add comment

Your Answer

 
discard

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.