# How to simplify the following trigonometry expression such that the number of used characters is minimal?

How to simplify the following trigonometry expression such that the number of used characters is minimal?

(13*Cos[t] - 5*Cos[2*t] - 2*Cos[3*t] - Cos[4*t])/4


The question sounds weird because I ask for the minimal number of used characters, but it is not a typo. The number of used characters must be minimal because the plotter needs short expression. Please ignore the computation performance for this case.

• Perhaps you should replace minimum character count by minimum computing effort? Aug 23, 2012 at 6:48
• What exactly is this "plotter" you speak of that gives you this peculiar requirement? Aug 23, 2012 at 7:48
• @J.M.: In addition to the plotter constraint, the fewer the number of used characters is, the easier it is for us to remember the expression. Please see the sixth curve. That is why I am interested to simplify the expression as minimal as possible. Aug 23, 2012 at 7:55
• Oh, it's for the heart curve? You might be interested in this, then... also, "the easier it is for us to remember the expression." - you can't look up stuff where you are? Aug 23, 2012 at 7:57

I don't know about the expression with "minimal chracters", but the following is the version I like, since you only have to evaluate the cosine once as a common subexpression:

HornerForm[(13 Cos[t] - 5 Cos[2 t] - 2 Cos[3 t] - Cos[4 t])/4 /.
Cos[n_Integer  t] :> ChebyshevT[n, Cos[t]], Cos[t]]

1 + Cos[t] (19/4 + Cos[t] (-(1/2) + (-2 - 2 Cos[t]) Cos[t]))


As Yves notes, you can now use a scoping construct (i.e. any of Block[]/Module[]/With[]) to isolate the common subexpression. For instance:

With[{ct = Cos[t]}, 1 + ct (19/2 - ct (1 + 4 ct (1 + ct)))/2]

• Good one, many-gravatared one! For speed, one might perhaps further save on cycles with a With[{cos=Cos[t]},...] or similar. Aug 23, 2012 at 6:47
• Yes, I had scoping with something like With[] in mind when I said "evaluate the cosine once as a common subexpression". Putting the expression in Horner form additionally cuts down on the multiplications needed. Aug 23, 2012 at 6:57
• Just wanted to suggest you include that verbatim in the answer for educational purposes. Aug 23, 2012 at 7:00
• Ah, I'm slow today. Of course... Aug 23, 2012 at 7:02
• Same thing here: "es ist zu heiß..." (it is too hot). Aug 23, 2012 at 8:59

I'm not sure I understand the question - surely there must be some syntax rules for the expression sent to the plotter. Anyway, the shortest Mathematica expression I can think of is:

{-13, 5, 2, 1}.Cos[{1, 2, 3, 4} t]/-4

• Why not -{-13, 5, 2, 1}.Cos[Range[4] t]/4? :) Aug 23, 2012 at 8:02
• @J.M. good point! I considered Range but decided it was longer than writing the list out in full. This is what happens when you try to count to 9 before the first coffee of the day :-) Aug 23, 2012 at 12:27

One idea is to use something like

FullSimplify[
(13*Cos[t] - 5*Cos[2*t] - 2*Cos[3*t] - Cos[4*t])/4
, ComplexityFunction -> Composition[StringLength, ToString, InputForm]
]


which instructs FullSimplify to optimize the number of input characters for the expression. In this case it doesn't get any shorter though.