Results 1 to 5 of 5

Math Help - Mathematica - ReplaceAll

  1. #1
    Newbie
    Joined
    Oct 2008
    Posts
    5

    Mathematica - ReplaceAll

    Can anyone help me out with this command that I am trying to get to work:

    Sum[((Binomial[12, i]*Binomial[40, n - i])/Binomial[52, n]), {i, 1, n}] /. n -> {1, 2, 3, 4}

    I would like it to carry out the sum four times and return the results for each one. If I go in and manually change the n on the top of the sum for each of the desired values, it works fine... however that defeats the purpose of using the /.

    Thanks in advance.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Super Member
    Joined
    Aug 2008
    Posts
    903
    Use the Map operator. The shorthand symbol is /@. Note the use of the # symbols. These are placeholders for the Map function which maps the values from the set {1,2,3,4} into these places. The & symbol designates the Sum expression as a pure function with the list {1,2,3,4} mapped sequentially into the placeholder expressions.

    Code:
    In[15]:=
    (Sum[(Binomial[12, i]*Binomial[40, #1 - i])/
         Binomial[52, #1], {i, 1, #1}] & ) /@ 
      {1, 2, 3, 4}
    
    Out[15]=
    {3/13, 7/17, 47/85, 2759/4165}
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    Oct 2008
    Posts
    12

    Table vs. Map

    I would suggest using table for that instance:
    Table[Sum[((Binomial[12, i]*Binomial[40, n - i])/Binomial[52, n]), {i, 1, n}] ,{n,1,4}]
    So n becomes an iterator which varies from 1-4.
    Actually, map and table are completely exchangable in most cases, however I prefer to use table where the iterator is a simple number. Problems that can arise with annonymous functions if you nest them, as you may often be tempted to do when shortening long pieces of code. It can become incredibly confusing to figure out which # means which thing coming from where, so it is best to use table whenever it makes sense. You can use table exactly like map if you set it up like
    Table[f[n],{n,{List of values}}]
    which is equivalent to
    f/@list of values.

    The problem with anonymous functions can, of course, easily be avoided by writing out the anonymous function:
    Function[n,Sum[((Binomial[12, i]*Binomial[40, n - i])/Binomial[52, n]), {i, 1, n}]]/@{1,2,3,4}
    which is the long hand of what shadowsend wrote, replacing # with n (the first argument of function)
    when in doubt, never be reluctant to write out your anonymous functions.
    A third option, which is my favorite, is to write
    Array[Sum[((Binomial[12, i]*Binomial[40, # - i])/Binomial[52, #]), {i, 1,#}]&,4]

    ReplaceAll is for changing the form of the answer, often to prepare it to be the argument of another function. You can make some really powerful algorithims out of it, but it can be hard to use. In this case, it does not work.
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Newbie
    Joined
    Oct 2008
    Posts
    5
    This /@ seems pretty powerful. I'll need to devote some time to learning how to use it. Thanks to both of you.
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Newbie
    Joined
    Oct 2008
    Posts
    12
    Map is quite powerful, as are all of the functional programming functions.
    I suggest that you become familliar with Map, Apply, Fold, Select, and Pick. I have found these to be the staples of most programs. Fold is the hardest to grasp, and solves many problems almost instantly. Map and Apply I use in just about every program I make.

    The way I learned to use mathematica was through lots of experimentation, and several ambitious programming assignments. Its fun, and totally worth it. Good luck =)
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Help about Mathematica
    Posted in the Math Software Forum
    Replies: 2
    Last Post: June 1st 2010, 12:46 PM
  2. help me in Mathematica
    Posted in the Math Software Forum
    Replies: 2
    Last Post: May 18th 2010, 02:26 AM
  3. Mathematica Help
    Posted in the Math Software Forum
    Replies: 1
    Last Post: March 5th 2010, 08:12 AM
  4. mathematica
    Posted in the Math Software Forum
    Replies: 0
    Last Post: October 27th 2009, 02:03 PM
  5. MLE and Mathematica
    Posted in the Math Software Forum
    Replies: 0
    Last Post: June 21st 2008, 02:27 AM

Search Tags


/mathhelpforum @mathhelpforum