RE: For loop problem in mathematica
- To: mathgroup at smc.vnet.net
- Subject: [mg90234] RE: [mg90163] For loop problem in mathematica
- From: "Jose Luis Gomez" <jose.luis.gomez at itesm.mx>
- Date: Thu, 3 Jul 2008 06:09:49 -0400 (EDT)
- References: <200807011059.GAA14895@smc.vnet.net>
PhysNova, I hope this is useful for you: FIRST: It is Not a good idea to use a For loop in Mathematica, Mathematica is functional and list-based, therefore a For loop will be very inefficient in Mathematica. Instead of For loops, you should use functional programming constructs, like Nest, NestWhile, Map, Apply, etc. This document might be interesting for you: http://homepage.cem.itesm.mx/lgomez/matecmatica/fp/fp.html http://homepage.cem.itesm.mx/lgomez/matecmatica/funcionalprog.nb SECOND: It is Not the same to write 1/(i^2) and to write 1.0/(i^2.0) In the first case Mathematica will use slow exact arithmetic, while in the second case Mathematica will use fast approximate floating point numbers (which is what most other programs do) Bye! Jose Mexico -----Mensaje original----- De: PhysNova [mailto:skhoshbinfar at gmail.com] Enviado el: Martes, 01 de Julio de 2008 05:59 Para: mathgroup at smc.vnet.net Asunto: [mg90163] For loop problem in mathematica Hi, i wrote a simple program of evaluating Pi number in M6 to test cpu computation timing, to do this a simple for loop was used: x=0;For[i = 1, i < 10,000,000, i++, x = x + 1/(i^2)];N[Sqrt(6*x),25]// Timing the result was catastrophe! it take few minuates. but i first expect to do this very simple job in few seconds.computation time is just satisfactory up to 100000 cycle. could anyone interperet this falut? our get an idea to improve the result? Tanks
- References:
- For loop problem in mathematica
- From: PhysNova <skhoshbinfar@gmail.com>
- For loop problem in mathematica