MathGroup Archive 2011

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

Search the Archive

Re: Just another Mathematica "Gotcha"

  • To: mathgroup at smc.vnet.net
  • Subject: [mg120863] Re: Just another Mathematica "Gotcha"
  • From: Daniel Lichtblau <danl at wolfram.com>
  • Date: Sat, 13 Aug 2011 06:49:26 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com

> In primary schools one does not learn about // or /. thus the need for
> parentheses is subtle. Using FullForm (or writing in Lisp) makes such
> issues disappear.

Of course one of those was covered in elementary school.

4.5/.3 = ?

> I think that making /. and // same precedence would not be a bad idea.
> 
> The double (or triple) clicking on the operator is nice to know about,
> but doesn't help for people who compose programs off-line.
> 
> RJF

I'm not convinced making them the same precedence would be without consequence. (Since we do not do such things, it would however be without precedent...) Here is an example that would stop working.

In[95]:= j = 5;
e1 = RandomInteger[{0, 100}, 10].x^Range[0, 9];
num = Expand[e1*RandomInteger[{0, 100}, 10].x^Range[0, 9]];
den = Expand[e1*RandomInteger[{0, 100}, 10].x^Range[0, 9]];
expr = num/den

Out[99]= (3180 + 5910*x + 11630*x^2 + 15675*x^3 + 20818*x^4 + 
   21396*x^5 + 
      23735*x^6 + 18518*x^7 + 24206*x^8 + 24548*x^9 + 20829*x^10 + 
      18664*x^11 + 16985*x^12 + 10126*x^13 + 8096*x^14 + 3556*x^15 + 
      4398*x^16 + 5278*x^17 + 372*x^18)/(1020 + 4710*x + 6590*x^2 + 
      9805*x^3 + 14702*x^4 + 13991*x^5 + 16237*x^6 + 13253*x^7 + 
      18173*x^8 + 19324*x^9 + 16882*x^10 + 15162*x^11 + 14934*x^12 + 
      7594*x^13 + 6920*x^14 + 2952*x^15 + 4812*x^16 + 4084*x^17 + 
      280*x^18)

expr // Together[#, Modulus -> p] & /. p -> Prime[j]

Out[100]= (3 + 2*x + 10*x^2 + 10*x^3 + 3*x^4 + 5*x^5 + 6*x^6 + x^7 + 
   3*x^8 + 9*x^9)/(5*(7 + x + 7*x^4 + 9*x^5 + 6*x^7 + 8*x^8 + x^9))

This requires that infix PostFix bind more loosely than postfix Function, which in turn must be looser than infix ReplaceAll. That is to say, the operations group as below.

In[103]:= expr // ((Together[#, Modulus -> p] &) /. p -> Prime[j])
Out[103]= (3 + 2*x + 10*x^2 + 10*x^3 + 3*x^4 + 5*x^5 + 6*x^6 + x^7 + 
   3*x^8 + 9*x^9)/(5*(7 + x + 7*x^4 + 9*x^5 + 6*x^7 + 8*x^8 + x^9))

If // and /. have equal precedence that is looser than postfix Function then we get the different outcome below.

In[104]:= (expr // (Together[#, Modulus -> p] &)) /. p -> Prime[j]
Out[104]= (53 + 72*x + 96*x^2 + 63*x^3 + 64*x^4 + 48*x^5 + 84*x^6 + 
   3*x^7 + 64*x^8 + 93*x^9)/(17 + 70*x + 55*x^2 + 33*x^3 + 61*x^4 + 
   25*x^5 + 66*x^6 + 2*x^7 + 76*x^8 + 70*x^9)

If infix // binds tighter than postfix &, we get

In[109]:= (expr // Together[#, Modulus -> p]) & /. p -> Prime[j]
Out[109]= Together[#1, Modulus -> 11][expr] &

These next two might also give pause when considering a change to precedence of // and/or /. .

In[123]:= f[expr] /. f[ee_] -> ee // Together[#, Modulus -> Prime[j]] &
Out[123]= (3 + 2*x + 10*x^2 + 10*x^3 + 3*x^4 + 5*x^5 + 6*x^6 + x^7 + 
   3*x^8 + 9*x^9)/(5*(7 + x + 7*x^4 + 9*x^5 + 6*x^7 + 8*x^8 + x^9))

In[124]:= 
f[expr] /. (f[ee_] -> ee // Together[#, Modulus -> Prime[j]] &)
Out[124]= (3180 + 5910*x + 11630*x^2 + 15675*x^3 + 20818*x^4 + 
   21396*x^5 + 
      23735*x^6 + 18518*x^7 + 24206*x^8 + 24548*x^9 + 20829*x^10 + 
      18664*x^11 + 16985*x^12 + 10126*x^13 + 8096*x^14 + 3556*x^15 + 
      4398*x^16 + 5278*x^17 + 372*x^18)/(1020 + 4710*x + 6590*x^2 + 
      9805*x^3 + 14702*x^4 + 13991*x^5 + 16237*x^6 + 13253*x^7 + 
      18173*x^8 + 19324*x^9 + 16882*x^10 + 15162*x^11 + 14934*x^12 + 
      7594*x^13 + 6920*x^14 + 2952*x^15 + 4812*x^16 + 4084*x^17 + 
      280*x^18)

Daniel Lichtblau
Wolfram Research





  • Prev by Date: Re: NMinimize fails to find a minimum value
  • Next by Date: Re: Getting the Date and Time From a Time Server
  • Previous by thread: Re: Just another Mathematica "Gotcha"
  • Next by thread: How to plot weighted edges on this graph?