I am working on a project that requires me to do very large factorial evaluations. On R the built in factorial function and the one I created both are not able to do factorials over 170. The first gives an error and mine return Inf. Is there a way to have R do these larger calculations (the calculator in accessories can do 10000 factorial and Maple can do even larger) -- View this message in context: http://www.nabble.com/large-factorials-tp23175816p23175816.html Sent from the R help mailing list archive at Nabble.com.
You don't say what the error was, for the R factorial function, but it is probably irrelevant for your question. Factorials get to be big numbers rather quickly and unless you are using a program that does arbitrary precission arithmetic you will quickly exceed the precission limits, for storing a number. If you have Maple, do 170! and count the number of digits in the result. You will see what I mean. There are some tricks when working with large factorials, depending on what you are doing with them. I'd first try the log factorial function in R I think its called lfactorial. Just do a ?factorial and you'll find documentation. If this doesn't work, for you, repost with a clear description of what you're trying to do and someone may be able to help. Murray M Cooper, Ph.D. Richland Statistics 9800 N 24th St Richland, MI, USA 49083 Mail: richstat at earthlink.net ----- Original Message ----- From: "molinar" <sky2k2 at hotmail.com> To: <r-help at r-project.org> Sent: Wednesday, April 22, 2009 3:21 PM Subject: [R] large factorials> > I am working on a project that requires me to do very large factorial > evaluations. On R the built in factorial function and the one I created > both are not able to do factorials over 170. The first gives an error and > mine return Inf. > > Is there a way to have R do these larger calculations (the calculator in > accessories can do 10000 factorial and Maple can do even larger) > -- > View this message in context: > http://www.nabble.com/large-factorials-tp23175816p23175816.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
On Wednesday 22 April 2009 12:21:41 pm molinar wrote:> I am working on a project that requires me to do very large factorial > evaluations. On R the built in factorial function and the one I created > both are not able to do factorials over 170. The first gives an error and > mine return Inf. > > Is there a way to have R do these larger calculations (the calculator in > accessories can do 10000 factorial and Maple can do even larger)If you need an open source arbitrary precision calculator, you might want to look at Octave which is OS and works similarly to Mathematica - up to a point books for Mathematica will be a significant help with Octave. JDougherty
Also the R sympy package can handle this:> library(rSymPy)Loading required package: rJava> factorial.sympy <- function(n) sympy(paste("factorial(", n, ")"))> # note that first time sympy is called it loads java, jython and sympy > # but on subsequent calls its faster. So make a dummy call first. > factorial.sympy(10)[1] "3628800"> # code from earlier post defining factorial.bc to be inserted here> benchmark(replications=10, columns=c('test', 'elapsed'),+ bc=factorial.bc(500), + sympy = factorial.sympy(500)) test elapsed 1 bc 2.17 2 sympy 0.09 See the rSymPy, r-bc and rbenchmark home pages: http://rsympy.googlecode.com http://r-bc.googlecode.com http://rbenchmark.googlecode.com On Wed, Apr 22, 2009 at 3:21 PM, molinar <sky2k2 at hotmail.com> wrote:> > I am working on a project that requires me to do very large factorial > evaluations. ?On R the built in factorial function and the one I created > both are not able to do factorials over 170. ?The first gives an error and > mine return Inf. > > Is there a way to have R do these larger calculations (the calculator in > accessories can do 10000 factorial and Maple can do even larger) > -- > View this message in context: http://www.nabble.com/large-factorials-tp23175816p23175816.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Albyn Jones wrote:> On Wed, Apr 22, 2009 at 08:26:51PM -0700, Ben Bolker wrote: > > >> ??? octave is a Matlab clone, not a Mathematica clone (I would be >> interested in an open source Mathematica clone ...) ??? >> >> > > You might take a look at Sage. It is not a mathematica clone, but > open source mathematical software which has a development community > similar to that of R. > >... except for that ws has quite a different attitude than bdr. vQ
The modified function I presented contains a stupid error, which is corrected below. sum1 <- function(l,u,t,i,n,w,tol=.Machine$double.xmin) { v <- 0 v2 <- 1 for (m in 0 :w & v2 > tol) { v1 <- ((u^(1/2))*(l^(1/2))*t)^(i-n+2*m) v2 <- exp(-lgamma(i-n+m+1)-lgamma(m+1)) v3 <- v1*v2 v <- v+v3 } return(v) } sum1(1,2,10,80,3,80) [1] 5.519201e-23> sum1(1,2,10,80,3,100)[1] 6.892307e-23> sum1(1,2,10,80,3,200)[1] 1.375784e-22> sum1(1,2,10,80,3,1000)[1] 6.86821e-22 Need more coffee... Joseph F. Lucke Senior Statistician Research Institute on Addictions University at Buffalo State University of New York 1021 Main Street Buffalo, NY 14203-1016 Office: 716-887-6807 Fax: 716-887-2510 http://www.ria.buffalo.edu/profiles/lucke.html [[alternative HTML version deleted]]