MathGroup Archive 2007

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

Search the Archive

Re: Rule and Module not working together

  • To: mathgroup at smc.vnet.net
  • Subject: [mg77915] Re: [mg77864] Rule and Module not working together
  • From: DrMajorBob <drmajorbob at bigfoot.com>
  • Date: Tue, 19 Jun 2007 06:58:40 -0400 (EDT)
  • References: <6059828.1182167540984.JavaMail.root@m35>
  • Reply-to: drmajorbob at bigfoot.com

A and B in the Module have nothing to do with A and B on the pattern side 
of the rule (the scope is different), and you'll often need RuleDelayed 

(":>") rather than Rule ("->"):

Clear[myfunc]
myfunc[x_] := x /. Log[a_] + Log[b_] :> Log[a*b]

b = 666;
myfunc[Log[f] + Log[g]]

Log[f g]

myfunc[Log[4] + Log[5]]

Log[20]

Bobby

On Mon, 18 Jun 2007 06:07:06 -0500, Steven Siew <stevensiew2 at gmail.com> 
wrote:

> On Mathematica 5.2, I tried to write a function which applies the rule
> Log[A_] + Log[B_] -> Log[A * B] but somehow it does not work as I
> expected.
>
> Instead of Log[f] + Log[g] becoming Log[f * g], I get the result of
> Log[666 * f] even though I have marked both A and B as private
> variables.
>
> Can anyone help? I'm sure I made a big mistake somewhere in my
> understanding of Mathematica.
>
>
>
> In[1]:= AppendTo[$Echo,"stdout"]
> Out[1]= {stdout}
>
> In[2]:= (* Write your mathematica code below *)
>
> In[3]:= If[Length[Names["Global`*"]] > 0, Remove["Global`*"]]
>
> In[4]:= Off[General::spell, General::spell1]
>
> In[5]:= myfunc[x_] := Module[{s, A, B},
>    s = {Log[A_] + Log[B_] -> Log[A*B]}; Return[x /. s]]
>
> In[6]:= ? myfunc
> Global`myfunc
>
> myfunc[x_] := Module[{s, A, B}, s = {Log[A_] + Log[B_] -> Log[A*B]};
>      Return[x /. s]]
>
> In[7]:= B = 666;
>
> In[8]:= myfunc[Log[f] + Log[g]]
>
> Out[8]= Log[666 f]
>
> In[9]:= myfunc[Log[4] + Log[5]]
>
> Out[9]= Log[2664]
>
> In[10]:= (* End of mathematica code *)
>
> In[11]:= Quit[];
>
>
>



-- 
DrMajorBob at bigfoot.com


  • Prev by Date: Re: Indefinate integrals, erroneus Natural log?
  • Next by Date: Re: Manipulate a complex expression
  • Previous by thread: Re: Rule and Module not working together
  • Next by thread: Re: Rule and Module not working together