MathGroup Archive 1993

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: MapAt Complaint

  • To: mathgroup at
  • Subject: Re: MapAt Complaint
  • From: bert at (Roberto Sierra)
  • Date: Wed, 10 Nov 1993 15:35:04 -0800

Martin McClain (wmm at wrote:
>        I am a fairly experienced Mma user, but quite frequently I cannot
>get MapAt to do what I want.  My question:  Am I missing something obvious,
>or does MapAt really need to be extended?  Here is the latest example:

>I want to go from   expr1 = 1+Sin[a+b]+Sin[a-b]
>to                  expr2 = 1 + 2 Cos[a] Sin[b].  


>If you try
>                        MapAt[Simplify,expr1,{{2},{3}}]
>it seems to try the two parts separately, producing no change.  If you try
>                        MapAt[Simplify,expr1,{{2,3}}]
>it thinks you mean MapAt[Simplify,expr1,{2,3}] and complains that there is
>no part {2,3}.  Of course, it can always be done by extracting,
>Simplifying, and replacing, but I am looking for an easy one liner.  Any

Try using a replacement rule, as in:

	expr1 /. Sin[x_ - y_] + Sin[x_ + y_] -> 2 Cos[x] Sin[y]

which results in

	1 + 2 Cos[a] Sin[b]

If you want to avoid typing the rule over and over you can build a
'MyTrigSimplify' function to do the work, or you can have MMA always
apply the rule in all expressions my modifying the default behavior
of the Plus function:

	Sin[x_ - y_] + Sin[x_ + y_] := 2 Cos[x] Sin[y];

MapAt appears to be capable of operating on *single* parts of an expression,
not on ranges of parts.  Part specificiations work differently in Part and
MapAt, Map, etc.  For example, you can extract the second and third parts
of an expression using Part[expr,{2,3}].  However, you can't map a function
onto the second and third parts *together* using MapAt[func,expr,{2,3}] --
this returns an error.  You *can* map a function onto the second and third
parts *separately* using MapAt[func,expr,{{2},{3}}], as Martin found out.

This is why a replacement rule would work better.  It will also be much
smarter and recognize simple associative and commutative variations.  For

	expr3 = 1 + Sin[a+b+c] + Sin[a-b+c];
	expr3 /. Sin[x_ - y_] + Sin[x_ + y_] -> 2 Cos[x] Sin[y]

results in

	1 + 2 Cos[a + c] Sin[b]

However, not all variations can be recognized by this single, simplistic
replacement rule.  For example, in

	expr4 = 1 + Sin[a-b-c] + Sin[a+b+c];
	expr4 /. Sin[x_ - y_] + Sin[x_ + y_] -> 2 Cos[x] Sin[y]

expr4 will not be reduced to 1 + 2 Cos[a] Sin[b+c].  You would need
more sophisticated rules to catch this type of reduction.

Hope this info helps!!

 \\|//                         "Television is a medium -- it is
  - -                           neither rare nor well done."
  o o                                            -- Ernie Kovacs
   J   roberto sierra
   O   tempered microdesigns    NOTICE:
  \_/  san francisco, ca        The ideas and opinions expressed
       bert at          herein are not those of the author.

  • Prev by Date: ODE problem
  • Next by Date: re: MapAt complaint
  • Previous by thread: MapAt complaint
  • Next by thread: re: MapAt complaint