3
$\begingroup$

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.

$\endgroup$
4
  • 1
    $\begingroup$ Perhaps you should replace minimum character count by minimum computing effort? $\endgroup$
    – Yves Klett
    Commented Aug 23, 2012 at 6:48
  • 1
    $\begingroup$ What exactly is this "plotter" you speak of that gives you this peculiar requirement? $\endgroup$ Commented Aug 23, 2012 at 7:48
  • $\begingroup$ @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. $\endgroup$ Commented Aug 23, 2012 at 7:55
  • $\begingroup$ 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? $\endgroup$ Commented Aug 23, 2012 at 7:57

3 Answers 3

10
$\begingroup$

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]
$\endgroup$
5
  • $\begingroup$ Good one, many-gravatared one! For speed, one might perhaps further save on cycles with a With[{cos=Cos[t]},...] or similar. $\endgroup$
    – Yves Klett
    Commented Aug 23, 2012 at 6:47
  • $\begingroup$ 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. $\endgroup$ Commented Aug 23, 2012 at 6:57
  • $\begingroup$ Just wanted to suggest you include that verbatim in the answer for educational purposes. $\endgroup$
    – Yves Klett
    Commented Aug 23, 2012 at 7:00
  • $\begingroup$ Ah, I'm slow today. Of course... $\endgroup$ Commented Aug 23, 2012 at 7:02
  • $\begingroup$ Same thing here: "es ist zu heiß..." (it is too hot). $\endgroup$
    – Yves Klett
    Commented Aug 23, 2012 at 8:59
7
$\begingroup$

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
$\endgroup$
2
  • $\begingroup$ Why not -{-13, 5, 2, 1}.Cos[Range[4] t]/4? :) $\endgroup$ Commented Aug 23, 2012 at 8:02
  • $\begingroup$ @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 :-) $\endgroup$ Commented Aug 23, 2012 at 12:27
3
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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