[prev in list] [next in list] [prev in thread] [next in thread] 

List:       koffice-devel
Subject:    Re: [patch] KSpread: RANDNORM function
From:       Tomas Mecir <mecirt () gmail ! com>
Date:       2004-07-19 19:26:15
Message-ID: 492258b10407191226539cc796 () mail ! gmail ! com
[Download RAW message or body]

On Sun, 18 Jul 2004 14:35:49 +0200, Ariya Hidayat <ariya@kde.org> wrote:
> No need to peek on Python, this is the normal Box-M=FCller transformation
> (see http://ariya.pandu.org/blog/2004-01.htm#30). However, you could try
> the Polar form as it's faster and more stable. Check
> http://www.taygeta.com/random/gaussian.html.

Ahh I see, thanks :)  Improved version attached.

/ Tomas

["kspread_randnorm-v2.patch" (application/octet-stream)]

Index: kspread_functions_math.cc
===================================================================
RCS file: /home/kde/koffice/kspread/kspread_functions_math.cc,v
retrieving revision 1.37
diff -u -3 -p -r1.37 kspread_functions_math.cc
--- kspread_functions_math.cc	13 May 2004 16:32:13 -0000	1.37
+++ kspread_functions_math.cc	19 Jul 2004 19:21:05 -0000
@@ -92,6 +92,7 @@ bool kspreadfunc_randbernoulli( KSContex
 bool kspreadfunc_randbinom( KSContext & context );
 bool kspreadfunc_randexp( KSContext & context );
 bool kspreadfunc_randnegbinom( KSContext & context );
+bool kspreadfunc_randnorm( KSContext & context );
 bool kspreadfunc_randpoisson( KSContext & context );
 bool kspreadfunc_rootn( KSContext& context );
 bool kspreadfunc_round( KSContext& context );
@@ -164,6 +165,7 @@ void KSpreadRegisterMathFunctions()
   repo->registerFunction( "RANDBINOM",     kspreadfunc_randbinom );
   repo->registerFunction( "RANDEXP",       kspreadfunc_randexp );
   repo->registerFunction( "RANDNEGBINOM",  kspreadfunc_randnegbinom );
+  repo->registerFunction( "RANDNORM",      kspreadfunc_randnorm );
   repo->registerFunction( "RANDPOISSON",   kspreadfunc_randpoisson );
   repo->registerFunction( "ROOTN",         kspreadfunc_rootn );
   repo->registerFunction( "ROUND",         kspreadfunc_round );
@@ -1244,6 +1246,41 @@ bool kspreadfunc_randbernoulli( KSContex
   return true;
 }
 
+bool kspreadfunc_randnorm( KSContext & context )
+{
+  QValueList<KSValue::Ptr> & args = context.value()->listValue();
+
+  if ( !KSUtil::checkArgumentsCount( context, 2, "RANDNORM", true ) )
+    return false;
+
+  if( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
+    return false;
+  if( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
+    return false;
+
+  double mu = args[0]->doubleValue();
+  double sigma = args[1]->doubleValue();
+
+  //using polar form of the Box-Muller transformation
+  //refer to http://www.taygeta.com/random/gaussian.html for more info
+
+  double x1, x2, w;
+  do {
+    x1 = (double) rand() / (RAND_MAX + 1.0);
+    x2 = (double) rand() / (RAND_MAX + 1.0);
+    x1 = 2.0 * x1 - 1.0;
+    x2 = 2.0 * x2 - 1.0;
+    w = x1 * x1 + x2 * x2;
+  } while (w >= 1.0);
+
+  w = sqrt ((-2.0 * log (w)) / w);
+  double res = x1 * w;
+
+  res = res * sigma + mu;
+  context.setValue( new KSValue( res ) );
+  return true;
+}
+
 bool kspreadfunc_randpoisson( KSContext & context )
 {
   QValueList<KSValue::Ptr> & args = context.value()->listValue();
Index: extensions/math.xml
===================================================================
RCS file: /home/kde/koffice/kspread/extensions/math.xml,v
retrieving revision 1.30
diff -u -3 -p -r1.30 math.xml
--- extensions/math.xml	4 Aug 2003 08:52:19 -0000	1.30
+++ extensions/math.xml	19 Jul 2004 19:21:06 -0000
@@ -876,6 +876,25 @@
     </Function>
 
     <Function>
+      <Name>RANDNORM</Name>
+      <Type>Float</Type>
+      <Parameter>
+	<Comment>Mean value of the normal distribution</Comment>
+	<Type>Float</Type>
+      </Parameter>
+      <Parameter>
+	<Comment>Dispersion of the normal distribution</Comment>
+	<Type>Float</Type>
+      </Parameter>
+      <Help>
+	<Text>The RANDNORM() function returns a Normal(Gaussian)-distributed pseudo-random number.</Text>
+	<Syntax>RANDNORM(mu,sigma)</Syntax>
+	<Example>RANDNORM(0,1)</Example>
+        <Related>RAND</Related>
+      </Help>
+    </Function>
+
+    <Function>
       <Name>RANDBETWEEN</Name>
       <Type>Float</Type>
       <Parameter>


_______________________________________________
koffice-devel mailing list
koffice-devel@mail.kde.org
https://mail.kde.org/mailman/listinfo/koffice-devel


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic