Mercurial > hg > octave-nkf
changeset 14683:57569a35765c
Simplify class handling ('single' vs. 'double') for statistical distribution random functions.
* betarnd.m, cauchy_rnd.m, chi2rnd.m, exprnd.m, frnd.m, gamrnd.m, geornd.m,
lognrnd.m, nbinrnd.m, normrnd.m, poissrnd.m, trnd.m, unidrnd.m, unifrnd.m,
wblrnd.m: Use class argument to random number function rather than explicitly
or implicitly casting output to class single.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Thu, 24 May 2012 09:50:28 -0700 |
parents | a543ed02e673 |
children | 22244a235fd0 |
files | scripts/statistics/distributions/betarnd.m scripts/statistics/distributions/cauchy_rnd.m scripts/statistics/distributions/chi2rnd.m scripts/statistics/distributions/exprnd.m scripts/statistics/distributions/frnd.m scripts/statistics/distributions/gamrnd.m scripts/statistics/distributions/geornd.m scripts/statistics/distributions/lognrnd.m scripts/statistics/distributions/nbinrnd.m scripts/statistics/distributions/normrnd.m scripts/statistics/distributions/poissrnd.m scripts/statistics/distributions/trnd.m scripts/statistics/distributions/unidrnd.m scripts/statistics/distributions/unifrnd.m scripts/statistics/distributions/wblrnd.m |
diffstat | 15 files changed, 33 insertions(+), 48 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/statistics/distributions/betarnd.m +++ b/scripts/statistics/distributions/betarnd.m @@ -84,11 +84,8 @@ if (isscalar (a) && isscalar (b)) if ((a > 0) && (a < Inf) && (b > 0) && (b < Inf)) - r = randg (a, sz); - rnd = r ./ (r + randg (b, sz)); - if (strcmp (cls, "single")) - rnd = single (rnd); - endif + r = randg (a, sz, cls); + rnd = r ./ (r + randg (b, sz, cls)); else rnd = NaN (sz, cls); endif @@ -96,8 +93,8 @@ rnd = NaN (sz, cls); k = (a > 0) & (a < Inf) & (b > 0) & (b < Inf); - r = randg (a(k)); - rnd(k) = r ./ (r + randg (b(k))); + r = randg (a(k), cls); + rnd(k) = r ./ (r + randg (b(k), cls)); endif endfunction
--- a/scripts/statistics/distributions/cauchy_rnd.m +++ b/scripts/statistics/distributions/cauchy_rnd.m @@ -84,7 +84,7 @@ if (isscalar (location) && isscalar (scale)) if (!isinf (location) && (scale > 0) && (scale < Inf)) - rnd = location - cot (pi * rand (sz)) * scale; + rnd = location - cot (pi * rand (sz, cls)) * scale; else rnd = NaN (sz, cls); endif @@ -92,7 +92,7 @@ rnd = NaN (sz, cls); k = !isinf (location) & (scale > 0) & (scale < Inf); - rnd(k) = location(k)(:) - cot (pi * rand (sum (k(:)), 1)) .* scale(k)(:); + rnd(k) = location(k)(:) - cot (pi * rand (sum (k(:)), 1, cls)) .* scale(k)(:); endif endfunction
--- a/scripts/statistics/distributions/chi2rnd.m +++ b/scripts/statistics/distributions/chi2rnd.m @@ -77,10 +77,7 @@ if (isscalar (n)) if ((n > 0) && (n < Inf)) - rnd = 2 * randg (n/2, sz); - if (strcmp (cls, "single")) - rnd = single (rnd); - endif + rnd = 2 * randg (n/2, sz, cls); else rnd = NaN (sz, cls); endif @@ -88,7 +85,7 @@ rnd = NaN (sz, cls); k = (n > 0) | (n < Inf); - rnd(k) = 2 * randg (n(k)/2); + rnd(k) = 2 * randg (n(k)/2, cls); endif endfunction
--- a/scripts/statistics/distributions/exprnd.m +++ b/scripts/statistics/distributions/exprnd.m @@ -77,7 +77,7 @@ if (isscalar (lambda)) if ((lambda > 0) && (lambda < Inf)) - rnd = rande (sz) * lambda; + rnd = rande (sz, cls) * lambda; else rnd = NaN (sz, cls); endif @@ -85,7 +85,7 @@ rnd = NaN (sz, cls); k = (lambda > 0) & (lambda < Inf); - rnd(k) = rande (sum (k(:)), 1) .* lambda(k)(:); + rnd(k) = rande (sum (k(:)), 1, cls) .* lambda(k)(:); endif endfunction
--- a/scripts/statistics/distributions/frnd.m +++ b/scripts/statistics/distributions/frnd.m @@ -84,7 +84,7 @@ if (isscalar (m) && isscalar (n)) if ((m > 0) && (m < Inf) && (n > 0) && (n < Inf)) - rnd = n/m * randg (m/2, sz) ./ randg (n/2, sz); + rnd = n/m * randg (m/2, sz, cls) ./ randg (n/2, sz, cls); else rnd = NaN (sz, cls); endif @@ -92,7 +92,7 @@ rnd = NaN (sz, cls); k = (m > 0) & (m < Inf) & (n > 0) & (n < Inf); - rnd(k) = n(k) ./ m(k) .* randg (m(k)/2) ./ randg (n(k)/2); + rnd(k) = n(k) ./ m(k) .* randg (m(k)/2, cls) ./ randg (n(k)/2, cls); endif endfunction
--- a/scripts/statistics/distributions/gamrnd.m +++ b/scripts/statistics/distributions/gamrnd.m @@ -84,10 +84,7 @@ if (isscalar (a) && isscalar (b)) if ((a > 0) && (a < Inf) && (b > 0) && (b < Inf)) - rnd = b * randg (a, sz); - if (strcmp (cls, "single")) - rnd = single (rnd); - endif + rnd = b * randg (a, sz, cls); else rnd = NaN (sz, cls); endif @@ -95,7 +92,7 @@ rnd = NaN (sz, cls); k = (a > 0) & (a < Inf) & (b > 0) & (b < Inf); - rnd(k) = b(k) .* randg (a(k)); + rnd(k) = b(k) .* randg (a(k), cls); endif endfunction
--- a/scripts/statistics/distributions/geornd.m +++ b/scripts/statistics/distributions/geornd.m @@ -77,7 +77,7 @@ if (isscalar (p)) if (p > 0 && p < 1); - rnd = floor (- rande (sz) ./ log (1 - p)); + rnd = floor (- rande (sz, cls) ./ log (1 - p)); elseif (p == 0) rnd = Inf (sz, cls); elseif (p == 1) @@ -86,10 +86,10 @@ rnd = NaN (sz, cls); endif else - rnd = floor (- rande (sz) ./ log (1 - p)); + rnd = floor (- rande (sz, cls) ./ log (1 - p)); k = !(p >= 0) | !(p <= 1); - rnd(k) = NaN; + rnd(k) = NaN; k = (p == 0); rnd(k) = Inf;
--- a/scripts/statistics/distributions/lognrnd.m +++ b/scripts/statistics/distributions/lognrnd.m @@ -84,12 +84,12 @@ if (isscalar (mu) && isscalar (sigma)) if ((sigma > 0) && (sigma < Inf)) - rnd = exp (mu + sigma * randn (sz)); + rnd = exp (mu + sigma * randn (sz, cls)); else rnd = NaN (sz, cls); endif else - rnd = exp (mu + sigma .* randn (sz)); + rnd = exp (mu + sigma .* randn (sz, cls)); k = (sigma < 0) | (sigma == Inf); rnd(k) = NaN;
--- a/scripts/statistics/distributions/nbinrnd.m +++ b/scripts/statistics/distributions/nbinrnd.m @@ -84,10 +84,7 @@ if (isscalar (n) && isscalar (p)) if ((n > 0) && (n < Inf) && (p > 0) && (p <= 1)) - rnd = randp ((1 - p) ./ p .* randg (n, sz)); - if (strcmp (cls, "single")) - rnd = single (rnd); - endif + rnd = randp ((1 - p) ./ p .* randg (n, sz, cls), cls); elseif ((n > 0) && (n < Inf) && (p == 0)) rnd = zeros (sz, cls); else @@ -100,7 +97,7 @@ rnd(k) = 0; k = (n > 0) & (n < Inf) & (p > 0) & (p <= 1); - rnd(k) = randp ((1 - p(k)) ./ p(k) .* randg (n(k))); + rnd(k) = randp ((1 - p(k)) ./ p(k) .* randg (n(k), cls)); endif endfunction
--- a/scripts/statistics/distributions/normrnd.m +++ b/scripts/statistics/distributions/normrnd.m @@ -84,12 +84,12 @@ if (isscalar (mu) && isscalar (sigma)) if (!isnan (mu) && !isinf (mu) && (sigma > 0) && (sigma < Inf)) - rnd = mu + sigma * randn (sz); + rnd = mu + sigma * randn (sz, cls); else rnd = NaN (sz, cls); endif else - rnd = mu + sigma .* randn (sz); + rnd = mu + sigma .* randn (sz, cls); k = isnan (mu) | isinf (mu) | !(sigma > 0) | !(sigma < Inf); rnd(k) = NaN; endif
--- a/scripts/statistics/distributions/poissrnd.m +++ b/scripts/statistics/distributions/poissrnd.m @@ -77,10 +77,7 @@ if (isscalar (lambda)) if (lambda >= 0 && lambda < Inf) - rnd = randp (lambda, sz); - if (strcmp (cls, "single")) - rnd = single (rnd); - endif + rnd = randp (lambda, sz, cls); else rnd = NaN (sz, cls); endif @@ -88,7 +85,7 @@ rnd = NaN (sz, cls); k = (lambda >= 0) & (lambda < Inf); - rnd(k) = randp (lambda(k)); + rnd(k) = randp (lambda(k), cls); endif endfunction
--- a/scripts/statistics/distributions/trnd.m +++ b/scripts/statistics/distributions/trnd.m @@ -77,7 +77,7 @@ if (isscalar (n)) if ((n > 0) && (n < Inf)) - rnd = randn (sz) ./ sqrt (2*randg (n/2, sz) / n); + rnd = randn (sz, cls) ./ sqrt (2*randg (n/2, sz, cls) / n); else rnd = NaN (sz, cls); endif @@ -85,7 +85,7 @@ rnd = NaN (sz, cls); k = (n > 0) & (n < Inf); - rnd(k) = randn (sum (k(:)), 1) ./ sqrt (2*randg (n(k)/2) ./ n(k))(:); + rnd(k) = randn (sum (k(:)), 1, cls) ./ sqrt (2*randg (n(k)/2, cls) ./ n(k))(:); endif endfunction
--- a/scripts/statistics/distributions/unidrnd.m +++ b/scripts/statistics/distributions/unidrnd.m @@ -77,12 +77,12 @@ if (isscalar (n)) if (n > 0 && n == fix (n)) - rnd = ceil (rand (sz) * n); + rnd = ceil (rand (sz, cls) * n); else rnd = NaN (sz, cls); endif else - rnd = ceil (rand (sz) .* n); + rnd = ceil (rand (sz, cls) .* n); k = ! (n > 0 & n == fix (n)); rnd(k) = NaN;
--- a/scripts/statistics/distributions/unifrnd.m +++ b/scripts/statistics/distributions/unifrnd.m @@ -84,12 +84,12 @@ if (isscalar (a) && isscalar (b)) if ((-Inf < a) && (a < b) && (b < Inf)) - rnd = a + (b - a) * rand (sz); + rnd = a + (b - a) * rand (sz, cls); else rnd = NaN (sz, cls); endif else - rnd = a + (b - a) .* rand (sz); + rnd = a + (b - a) .* rand (sz, cls); k = !(-Inf < a) | !(a < b) | !(b < Inf); rnd(k) = NaN;
--- a/scripts/statistics/distributions/wblrnd.m +++ b/scripts/statistics/distributions/wblrnd.m @@ -84,12 +84,12 @@ if (isscalar (scale) && isscalar (shape)) if ((scale > 0) && (scale < Inf) && (shape > 0) && (shape < Inf)) - rnd = scale * rande (sz) .^ (1/shape); + rnd = scale * rande (sz, cls) .^ (1/shape); else rnd = NaN (sz, cls); endif else - rnd = scale .* rande (sz) .^ (1./shape); + rnd = scale .* rande (sz, cls) .^ (1./shape); k = (scale <= 0) | (scale == Inf) | (shape <= 0) | (shape == Inf); rnd(k) = NaN;