Group Abstract Group Abstract

Message Boards Message Boards

0
|
90 Views
|
3 Replies
|
0 Total Likes
View groups...
Share
Share this post:
GROUPS:

Debugging a linear sum of terms

Posted 2 days ago

I added a Mathematica Notebook in order to display my problem. I haven't used Mathematica since about 1980, so I am currently very much a beginner. I don't understand whether I or others in this group can see the contents of my notebook from this website.

In the code in the notebook, I am trying to express characters of SU(2) as real linear sums of terms like Cos[n*t], where n is a positive integer (not a variable integer). I do not want to see any products of trig functions in my results. Nor do a want to see any round brackets.
Since I don't know whether a reader can see my code (or what they would have to do in order to see it) here is the code

myrules = {Cos[a_] Cos[b_] -> (Cos[a + b] + Cos[a - b])/2,
   Cos[a_]^2 -> (1 + Cos[2*a])/2, c_ -> Expand[c]};
k[0, t_] = 1;
k[n_, t_] = Cos[t]*k[n - 1, t] + Cos[n*t]  //. myrules;
Table[k[n, t], {n, 0, 2}] // TableForm

This results in

{
{1},
{Cos[t] + Cos[t] TerminatedEvaluation["RecursionLimit"]},
{Cos[2 t] + Cos[t] TerminatedEvaluation["RecursionLimit"]}
}

A previous version of the code resulted in expressions containing brackets. I want all the brackets removed. I thought Expand would do this. Perhaps Mathematica's inbuilt rules are undoing the results of my rules (named myrules).

Attachments:
POSTED BY: David Epstein
3 Replies
Posted 22 hours ago

Both answers are instructive and helpful. I will use Gianluca's code as it is attractively simple and applies to more situations. The advice from both contributors about how to use rules helped my understanding. FixedPoint seems excellent for keeping code simple.

POSTED BY: David Epstein

When you type c_ -> Expand[c], the Expand gets evaluated immediately, resulting in c_ -> c, which does nothing. Maybe TrigReduce achieves your purpose this way:

k0[0, t_] = 1;
k0[n_, t_] := k0[n, t] =
    Cos[t]*k0[n - 1, t] + Cos[n*t] //
     TrigReduce // Expand;
POSTED BY: Gianluca Gorni
Posted 2 days ago

Update: Ha! Gianluca is absolutely correct about Expand[c] being evaluated immediately since you used a plain Rule instead of RuleDelayed. I had noted that, but then went on to explain why none of your other rules would ever evaluate. In doing so, I had chosen to change your Rules to RuleDelayeds to clearly illustrate the problem. Then, obviously, I answered the question as if it had originally used RuleDelayeds. I'm only pointing this out to make it clear that the real reason your original code failed is that you weren't taking into account how rules are used in ReplaceRepeated and related functions. I.e. just changing c_ -> Expand[c] to c_ :> Expand[c] doesn't fix the problem. And of course, Gianluca's suggestion avoids that entirely by not using any sort of replacement function.

One problem is that your rules contain this:

c_ :> Expand[c]

The pattern c_ will match anything. Also, these functions stop performing replacements to a subexpression once a rule has been matched. So, even though you're using ReplaceRepeated, every single time it matches the whole expression and so the only rule you're ever applying is the Expand one.

I'm guessing that you want something like this:

myrules = {n_ Cos[a_] Cos[b_] :> n (Cos[a + b] + Cos[a - b])/2, n_ Cos[a_]^2 :> n (1 + Cos[2*a])/2};
k[0, t_] = 1;
k[n_, t_] := FixedPoint[Expand@*ReplaceRepeated[myrules], Cos[t]*k[n - 1, t] + Cos[n*t]];
POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard