summaryrefslogtreecommitdiff
path: root/Build/source/texk/web2c/mplibdir/mpmath.w
diff options
context:
space:
mode:
authorAkira Kakuto <kakuto@fuk.kindai.ac.jp>2015-10-07 01:43:37 +0000
committerAkira Kakuto <kakuto@fuk.kindai.ac.jp>2015-10-07 01:43:37 +0000
commit4ca2692153042a0c2f7db8d681e190b867ae2faf (patch)
tree5ce3f1b1d2b21911766b40bb856a8279cec96fed /Build/source/texk/web2c/mplibdir/mpmath.w
parent357d78ed0c02ea3e4c41bac6c13f2cdb7c50784c (diff)
web2c/mplibdir: Sync with the upstream.
git-svn-id: svn://tug.org/texlive/trunk@38574 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Build/source/texk/web2c/mplibdir/mpmath.w')
-rw-r--r--Build/source/texk/web2c/mplibdir/mpmath.w43
1 files changed, 42 insertions, 1 deletions
diff --git a/Build/source/texk/web2c/mplibdir/mpmath.w b/Build/source/texk/web2c/mplibdir/mpmath.w
index b7d1948ec3e..b713dc14151 100644
--- a/Build/source/texk/web2c/mplibdir/mpmath.w
+++ b/Build/source/texk/web2c/mplibdir/mpmath.w
@@ -1,4 +1,4 @@
-% $Id: mpmath.w 2057 2015-03-20 01:33:59Z luigi $
+% $Id: mpmath.w 2070 2015-10-06 10:35:23Z luigi $
%
% This file is part of MetaPost;
% the MetaPost program is in the public domain.
@@ -56,6 +56,7 @@ static void mp_number_angle_to_scaled (mp_number *A);
static void mp_number_fraction_to_scaled (mp_number *A);
static void mp_number_scaled_to_fraction (mp_number *A);
static void mp_number_scaled_to_angle (mp_number *A);
+static void mp_m_unif_rand (MP mp, mp_number *ret, mp_number x_orig);
static void mp_m_norm_rand (MP mp, mp_number *ret);
static void mp_m_exp (MP mp, mp_number *ret, mp_number x_orig);
static void mp_m_log (MP mp, mp_number *ret, mp_number x_orig);
@@ -254,6 +255,7 @@ void * mp_initialize_scaled_math (MP mp) {
math->n_arg = mp_n_arg;
math->m_log = mp_m_log;
math->m_exp = mp_m_exp;
+ math->m_unif_rand = mp_m_unif_rand;
math->m_norm_rand = mp_m_norm_rand;
math->pyth_add = mp_pyth_add;
math->pyth_sub = mp_pyth_sub;
@@ -1836,6 +1838,45 @@ static void mp_next_random (MP mp, mp_number *ret) {
}
+@ To produce a uniform random number in the range |0<=u<x| or |0>=u>x|
+or |0=u=x|, given a |scaled| value~|x|, we proceed as shown here.
+
+Note that the call of |take_fraction| will produce the values 0 and~|x|
+with about half the probability that it will produce any other particular
+values between 0 and~|x|, because it rounds its answers.
+
+@c
+static void mp_m_unif_rand (MP mp, mp_number *ret, mp_number x_orig) {
+ mp_number y; /* trial value */
+ mp_number x, abs_x;
+ mp_number u;
+ new_fraction (y);
+ new_number (x);
+ new_number (abs_x);
+ new_number (u);
+ mp_number_clone (&x, x_orig);
+ mp_number_clone (&abs_x, x);
+ mp_number_abs (&abs_x);
+ mp_next_random(mp, &u);
+ /*take_fraction (y, abs_x, u);*/
+ mp_number_take_fraction (mp,&y, abs_x,u);
+ free_number (u);
+ if (mp_number_equal(y, abs_x)) {
+ /*set_number_to_zero(*ret);*/
+ mp_number_clone (ret, ((math_data *)mp->math)->zero_t);
+ } else if (mp_number_greater(x, ((math_data *)mp->math)->zero_t)) {
+ mp_number_clone (ret, y);
+ } else {
+ mp_number_clone (ret, y);
+ mp_number_negate (ret);
+ }
+ free_number (abs_x);
+ free_number (x);
+ free_number (y);
+}
+
+
+
@ Finally, a normal deviate with mean zero and unit standard deviation
can readily be obtained with the ratio method (Algorithm 3.4.1R in