Re: Multinomial coefficients evaluation
- To: mathgroup at smc.vnet.net
- Subject: [mg68138] Re: Multinomial coefficients evaluation
- From: "sashap" <pavlyk at gmail.com>
- Date: Tue, 25 Jul 2006 04:01:45 -0400 (EDT)
- References: <ea1nis$pkr$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hi Michal, You would be much better off posting here input forms of the input, than the whole notebook. It has a benefit of readability without having access to Mathematica, and does not clutter the forum. Since In[3]:= Attributes[Sum] Out[3]= {HoldAll,Protected,ReadProtected} It evaluates its arguments later in the game, and for every term in the sum, resuling in multitude of subtitutions, when all you need is one substitution done before the summing commences. Thus, wrapping Evaluate around the first argument helps to speed it up. In[1]:= Timing[s1 = Sum[Evaluate[Multinomial[n1, n2, n3]*Subscript[a, 1]^n1* Subscript[a, 2]^n2*Subscript[a, 3]^n3 /. n3 -> n - n1 - n2], {n1, 0, n}, {n2, 0, n - n1}]; ] Out[1]= {0.17200000000000004*Second, Null} In[2]:= Timing[s2 = Sum[Evaluate[((n1 + n2 + n3)!/(n1!*n2!*n3!))*Subscript[a, 1]^n1* Subscript[a, 2]^n2*Subscript[a, 3]^n3 /. n3 -> n - n1 - n2], {n1, 0, n}, {n2, 0, n - n1}]; ] Out[2]= {0.07799999999999985*Second, Null} The reason for slagishness of Multinomial is in its Orderless attribute: In[21]:= Attributes[Multinomial] Out[21]= {Listable, NumericFunction, Orderless, Protected, ReadProtected} Multinomial sorts its arguments for every term evaluation. Oleksandr Pavlyk Wolfram Research Michal Kvasnicka wrote: > Why is function Multinomial so slower than its direct definition??? See > attached notebook with three equivalent ways of the simple multinomial > series expansion. > > Michal Kvasnicka > > (************** Content-type: application/mathematica ************** > CreatedBy='Mathematica 5.2' > > Mathematica-Compatible Notebook > > This notebook can be used with any Mathematica-compatible > application, such as Mathematica, MathReader or Publicon. The data > for the notebook starts with the line containing stars above. > > To get the notebook into a Mathematica-compatible application, do > one of the following: > > * Save the data starting with the line of stars above into a file > with a name ending in .nb, then open the file inside the > application; > > * Copy the data starting with the line of stars above to the > clipboard, then use the Paste menu command inside the application. > > Data for notebooks contains only printable 7-bit ASCII and can be > sent directly in email or through ftp in text mode. Newlines can be > CR, LF or CRLF (Unix, Macintosh or MS-DOS style). > > NOTE: If you modify the data for this notebook not in a Mathematica- > compatible application, you must delete the line below containing > the word CacheID, otherwise Mathematica-compatible applications may > try to use invalid cache data. > > For more information on notebooks and Mathematica-compatible > applications, contact Wolfram Research: > web: http://www.wolfram.com > email: info at wolfram.com > phone: +1-217-398-0700 (U.S.) > > Notebook reader applications are available free of charge from > Wolfram Research. > *******************************************************************) > > (*CacheID: 232*) > > > (*NotebookFileLineBreakTest > NotebookFileLineBreakTest*) > (*NotebookOptionsPosition[ 2702, 93]*) > (*NotebookOutlinePosition[ 3347, 115]*) > (* CellTagsIndexPosition[ 3303, 111]*) > (*WindowFrame->Normal*) > > > > Notebook[{ > > Cell[CellGroupData[{ > Cell[BoxData[ > \(n\ = \ 100\)], "Input"], > > Cell[BoxData[ > \(100\)], "Output"] > }, Open ]], > > Cell[CellGroupData[{ > > Cell[BoxData[ > \(Timing[\ \(Sum[ > Multinomial[n1, n2, n3]\ \(a\_1\^n1\) \(a\_2\^n2\) > a\_3\^n3 /. n3 \[Rule] n - n1 - n2, {n1, 0, n}, {n2, 0, > n - n1}];\)]\)], "Input"], > > Cell[BoxData[ > \({2.234`\ Second, Null}\)], "Output"] > }, Open ]], > > Cell[CellGroupData[{ > > Cell[BoxData[ > \(Timing[\(Sum[\(\((n1 + n2 + n3)\)!\)\/\(\(n1!\) \(n2!\) \(n3!\)\ > \)\ \(a\_1\^n1\) \(a\_2\^n2\) a\_3\^n3 /. > n3 \[Rule] n - n1 - n2, {n1, 0, n}, {n2, 0, > n - n1}];\)]\)], "Input"], > > Cell[BoxData[ > \({0.31199999999999983`\ Second, Null}\)], "Output"] > }, Open ]], > > Cell[CellGroupData[{ > > Cell[BoxData[ > \(Timing[\(Expand[\((a\_1 + a\_2 + a\_3)\)\^n];\)]\)], "Input"], > > Cell[BoxData[ > \({0.030999999999999694`\ Second, Null}\)], "Output"] > }, Open ]] > }, > FrontEndVersion->"5.2 for Microsoft Windows", > ScreenRectangle->{{0, 1280}, {0, 911}}, > WindowSize->{679, 740}, > WindowMargins->{{Automatic, 96}, {Automatic, 56}} > ] > > (******************************************************************* > Cached data follows. If you edit this Notebook file directly, not > using Mathematica, you must remove the line containing CacheID at > the top of the file. The cache data will then be recreated when > you save this file from within Mathematica. > *******************************************************************) > > (*CellTagsOutline > CellTagsIndex->{} > *) > > (*CellTagsIndex > CellTagsIndex->{} > *) > > (*NotebookFileOutline > Notebook[{ > > Cell[CellGroupData[{ > Cell[1776, 53, 44, 1, 30, "Input"], > Cell[1823, 56, 37, 1, 29, "Output"] > }, Open ]], > > Cell[CellGroupData[{ > Cell[1897, 62, 206, 4, 52, "Input"], > Cell[2106, 68, 56, 1, 29, "Output"] > }, Open ]], > > Cell[CellGroupData[{ > Cell[2199, 74, 222, 4, 43, "Input"], > Cell[2424, 80, 70, 1, 29, "Output"] > }, Open ]], > > Cell[CellGroupData[{ > Cell[2531, 86, 81, 1, 30, "Input"], > Cell[2615, 89, 71, 1, 29, "Output"] > }, Open ]] > } > ] > *) > > > > (******************************************************************* > End of Mathematica Notebook file. > *******************************************************************)