# Why does FunctionPeriod give two different results for alternative forms of the given function?

I have a function for the variable $$x>0$$ and constant $$a\in\mathbb{R}$$. I write it in its two alternative forms

$$f(x)=\cos a\pi-\cos x\pi =-2\sin\frac{(a-x)\pi}{2}\sin\frac{(a+x)\pi}{2}$$

Then, I use  FunctionPeriod for both functions, for the first one, it gives $$T=2$$, while for the second form, it gives $$T=4$$. The correct one is of course $$T=2$$.

Why does this problem happen? How can I trust the result if the function is more complicated?

FunctionPeriod[       Cos[a \[Pi]]-Cos[x \[Pi]]      ,   x   ]
(* 2 *)
FunctionPeriod[        -2 Sin[(a Pi)/2-(x Pi)/2]Sin[(a Pi)/2+(x Pi)/2]  ,   x   ]
(* 4 *)

• Note: Both might be considered correct (per the docs) but only one of them is the minimal period Aug 15 at 22:44
• I tried giving "a" some numerical values both results are 2. Once the value of "a" is cleared it gives me two different answers Aug 16 at 12:08
• @qahtah Yes, for a specific value of $a$ there is no problem. Aug 16 at 12:17
• FunctionPeriod[-2 Sin[(a Pi)/2 - (x Pi)/2] Sin[(a Pi)/2 + (x Pi)/2] // Simplify, x] Aug 16 at 12:28

The FunctionPeriod function is fragile. For example, testing for four variants

With[{f = Sin[a - x] Sin[x + a]}, FunctionPeriod[#, x] & /@
{f, f // Simplify, 2 f, 2 f // Simplify}]


returns {2*Pi, 2*Pi, 2*Pi, Pi}. Notice the wording in the documentation "gives a period p" but not "gives the minimal period p". Also, replacing the a with a specific nonzero value such as 1 makes no difference, but if a is zero then it gives period Pi in all four variants.

You wrote

Why does this problem happen? How can I trust the result if the function is more complicated?

The problem seems to be that the undocumented code that Mathematica uses is not capable of finding the minimal period in all cases. As far as I know, if it returns a nonzero period, then it is a genuine period, but may not be minimal.

Whether the documentation is being careful or vague, as I hinted in my comment and Somos pointed out, it states FunctionPeriod returns a number $$T$$ such that $$f(x+T) = f(x)$$. It need not be positive and it need not be minimal. We probably need a better test suite to meet the OP's demand for reliability, but I will suggest that TrigToExp greatly aids finding the minimal positive period for trigonometric expressions:

FunctionPeriod[TrigToExp@ f, x]


FunctionPeriod tends to return a negative value after TrigToExp, so we need to change its sign if a positive value is required. [Deleted: FunctionPeriod over the complexes....It caused more trouble than it was worth, no matter how cute it was.]

negateIfNeg = If[InternalSyntacticNegativeQ[#], -#, #] &;

Block[{f = {
Style["Somos", Bold, Italic],
Sin[a - x] Sin[x + a],
2 Sin[a - x] Sin[x + a] // Simplify,
Style["OP", Bold, Italic],
Cos[a Pi] - Cos[x Pi],
-2 Sin[(a Pi)/2 - (x Pi)/2] Sin[(a Pi)/2 + (x Pi)/2],
-2 Sin[(a Pi)/2 - (x Pi)/2] Sin[(a Pi)/2 + (x Pi)/2] //
TrigExpand,
Style["M-E2", Bold, Italic], Sin[4 a x] + Sin[2 a x],
Cos[a x] Cos[a b x] Cos[a b c x],
Cos[2 x] Cos[3 x] Cos[5 x]},
\$Assumptions = {b, c} \[Element] Integers},
{#,
FunctionPeriod[#, x],
negateIfNeg@FunctionPeriod[TrigToExp@#, x]
} & /@ f //
Prepend[#,
Style[#, Bold, Larger] & /@ {"Input", "FP", "T2Exp"}] & //
# /. {
{in_Style, 0, 0} :> {in},
{in_, fp : Except[_Style], t2e_} /; fp =!= t2e :> {in,
Style[fp, Red], t2e}} & //
Grid [Update: The trouble with the deleted Complexes solution suggests that a broader test suite is needed to determine whether the reliability the OP seeks is achieved. FunctionPeriod is sensitive to the form of the expression, and I have no way of proving TrigToExp works robustly. In fact, if you Expand it on the ...Cos[a b c x] example above, FunctionPeriod fails to mind the minimal period. One might need to find the FunctionPeriod on several forms and their PolynomialGCD`. ]