# How to Force the PolyGamma[0, x] function or the HarmonicNumber[ x ] function

I'm crunching some infinite summations, and sometimes Mathematica generates results that have the PolyGamma[0, x] function (which is the Digamma) and sometimes the HarmonicNumber[ ]. I'd like to choose one or the other as to what's generated. My fallback is to use a rule for replacement, but curious as to how Mathematica chooses what to generate.

Here's an interesting example:

The first one has PolyGamma in it, both before and after FullSimplify

res = m + Sum[2 (m - k + 1) 1/(1 - 4 (1 - k)^2), {k, 2, m}] // FullSimplify
(* 1/2 (EulerGamma + 2/(-1 + 2 m) + Log[4] + PolyGamma[0, -(1/2) + m]) *)

This one, same as the one above but without the presum of m, resolves with PolyGamma

res = Sum[2 (m - k + 1) 1/(1 - 4 (1 - k)^2), {k, 2, m}]
(* (2 + 2 m - 4 m^2 + PolyGamma[0, 1/2] - 2 m PolyGamma[0, 1/2] - PolyGamma[0, -(1/2) + m] + 2 m PolyGamma[0, -(1/2) + m])/(2 (-1 + 2 m))

But if simplified, goes to the Harmonic Number

res // FullSimplify
(* (2 + (-1 + 2 m) HarmonicNumber[-(3/2) + m] - Log[4] + 2 m (1 - 2 m + Log[4]))/(-2 + 4 m) *)

What is "simpler" or more complex is a very subjective question. The topic has come up a few times before (see e.g. my previous answer to Advice for Mathematica as Mathematician's Aid and links therein).

The Simplify functions fiddle with an expression systematically in an attempt to minimize a pre-defined "complexity function", which is loosely based on the LeafCount of the expression (the exact function is somewhere in the documentation of Simplify, if I recall correctly). Even small changes (e.g. PolyGamma vs. HarmonicNumber) can significantly affect this complexity measure.

Fortunately, however, you can supply your own complexity function using the ComplexityFunctionoption, and bias it in the direction you prefer. For instance:

res1 = m + Sum[2 (m - k + 1) 1/(1 - 4 (1 - k)^2), {k, 2, m}]
res2 = Sum[2 (m - k + 1) 1/(1 - 4 (1 - k)^2), {k, 2, m}]

simpPoly = FullSimplify[#, ComplexityFunction -> (LeafCount[#] + 10 Count[#, _HarmonicNumber, All] &)] &
simpHarmonic = FullSimplify[#, ComplexityFunction -> (LeafCount[#] + 10 Count[#, _PolyGamma, All] &)] &

The first one severely penalizes expressions that contain HarmonicNumber, thus preferring those that contain PolyGamma; the second does the opposite. Here are the results:

simpHarmonic /@ {res1, res2}

simpPoly /@ {res1, res2}

• Ah, I don't have to write an answer anymore... :) May 13, 2020 at 15:38
• @J.M Thank you! :-) May 13, 2020 at 16:17
• Very cool, muchos gracias. May 13, 2020 at 16:46
• @MikeY You are very welcome! May 13, 2020 at 19:15