home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / sci / math / symbolic / 3337 < prev    next >
Encoding:
Text File  |  1992-12-26  |  3.4 KB  |  83 lines

  1. Newsgroups: sci.math.symbolic
  2. Path: sparky!uunet!spool.mu.edu!uwm.edu!ux1.cso.uiuc.edu!news.cso.uiuc.edu!mm-mac18.mse.uiuc.edu!gaylord
  3. From: Richard J. Gaylord <gaylord@ux1.cso.uiuc.edu>
  4. Subject: Re: "shut up and deal the cards"
  5. References: <Bzs70r.B1B@news.cso.uiuc.edu>
  6. Message-ID: <Bzv5LK.969@news.cso.uiuc.edu>
  7. X-Xxdate: Sat, 26 Dec 92 05:13:50 GMT
  8. Sender: usenet@news.cso.uiuc.edu (Net Noise owner)
  9. X-Useragent: Nuntius v1.1.1d13
  10. Organization: University of Illinois
  11. Date: Sat, 26 Dec 1992 11:08:07 GMT
  12. X-Xxmessage-Id: <A7619A8E2C019B12@mm-mac18.mse.uiuc.edu>
  13. Lines: 68
  14.  
  15. Subject: Re: "shut up and deal the cards"
  16. From: David Petry, petry@zermelo.math.washington.edu
  17. Date: 25 Dec 92 00:12:34 GMT
  18.  
  19. In article <1992Dec25.001234.21234@u.washington.edu> David Petry,
  20. petry@zermelo.math.washington.edu 
  21.  
  22. compares my
  23.  
  24. Transpose[Sort[Transpose[{Table[Random[],{Length[lis]}], lis}]]][[2]]
  25.  
  26. with his
  27.  
  28. randomize[L_] := Module[{a,b,k,p=L}, 
  29.             For[k=Length[L],k>1,k--,
  30.                 b=Random[Integer,{1,k}];
  31.                 a=p[[k]];
  32.                 p[[k]]=p[[b]];
  33.                 p[[b]]=a];
  34.             p]
  35.  
  36. and finds the first program to be five times faster than the second.  He
  37. concludes that Mathematica programmers must learn a bunch of slick tricks
  38. to write reasonably efficient programs.  
  39.  
  40. ================
  41.  
  42. A slick programming trick is simply  a technique that has not yet been
  43. mastered. Once you do, it becomes a  programming tool that can be used in
  44. many seemingly disparate situations. 
  45.  
  46. eg., about two months ago, i was working on a  computer simulation known
  47. as 'invasion percolation' (a model for flow through a porous medium and
  48. also of cancer spread) in which there is a list of lattice sites having
  49. associated random numbers (representing the resistance of a lattice site
  50. to being selected [eg., the susceptability of a cell to attack by a
  51. virus])  and one has to select the site with the lowest random number
  52. (eg., the most vulnerable cell). 
  53.  
  54. the way i finally did it was to sort the list {..{Random[], {a,b}..} and
  55. take the second part of the first element in the sorted list. 
  56.  
  57. In  other words,  i used precisely the same technique there as in the
  58. topmost program here (though i didn't recognize that my invasion
  59. percolation program could be used to randomize a list as well) . 
  60.  
  61. Of course, in the invasion percolation case, it was not a trick at all
  62. but was a very direct, straightforward implementation of the site
  63. selection process in the simulation 
  64.  
  65.  [btw - this method will appear in my next Computer Simulations column in
  66. "Mathematica in Education" as an improved version of the 'spreading
  67. phenomena' program in my column a couple of issues ago]. 
  68.  
  69. a final note: i found David's procedural program very difficult to
  70. decipher (i had to spend time figuring out all of the assignment
  71. satements) while i was able to write my functional program in a  direct
  72. way by simply implementing what i wanted to do with the entire list using
  73.  built-in functions (one key to functional programming is to deal with a
  74. list as a single data object without unnecessarily deconstructing it) [i
  75. also orignally wrote it as an anonymous function].
  76.  
  77. many people may not find 'functional' thinking to be obvious to do but
  78. that's primarily  because they haven't learned how to do it (to that
  79. extent functional programming does appear to be a trick to the person
  80. unfamiliar with it).  its really a very natural way to think. it would
  81. help if there were a book on how to learn to do functional programming
  82. [there will be by august].
  83.