def randn(*args): """u = randn(d0,d1,...,dn) returns zero-mean, unit-variance Gaussian random numbers in an array of size (d0,d1,...,dn).""" import numarray.random_array as ra x1 = ra.random(args) x2 = ra.random(args) return sqrt(-2 * log(x1)) * cos(2 * pi * x2)
def randn(*args): """u = randn(d0,d1,...,dn) returns zero-mean, unit-variance Gaussian random numbers in an array of size (d0,d1,...,dn).""" import numarray.random_array as ra x1 = ra.random(args) x2 = ra.random(args) return sqrt(-2*log(x1))*cos(2*pi*x2)
def setdata(self, X, V): A = self.bialtprodeye(2*self.F.J_coords) """Note: p, q <= min(n,m)""" self.data.Brand = 2*(random((A.shape[0],self.data.p))-0.5) self.data.Crand = 2*(random((A.shape[1],self.data.q))-0.5) self.data.B = zeros((A.shape[0],self.data.p), Float) self.data.C = zeros((A.shape[1],self.data.q), Float) self.data.D = zeros((self.data.q,self.data.p), Float) U, S, Vh = linalg.svd(A) self.data.b = U[:,-1:] self.data.c = num_transpose(Vh)[:,-1:] if self.update: self.data.B[:,1] = self.data.b self.data.C[:,1] = self.data.c U2, S2, Vh2 = linalg.svd(c_[r_[A, transpose(self.data.C[:,1])], r_[self.data.B[:,1], [[0]]]]) self.data.B[:,2] = U2[0:A.shape[0],-1:] self.data.C[:,2] = num_transpose(Vh2)[0:A.shape[1],-1:] self.data.D[0,1] = U2[A.shape[0],-1] self.data.D[1,0] = num_transpose(Vh2)[A.shape[1],-1] else: self.data.B = self.data.Brand self.data.C = self.data.Crand
def timeRandomCorrect(n, size): s = size*size a = rand.randint(0,2**16, s) a = num.array(a, uint16) d = rand.random(s) d = num.array(d, float64) b = rand.random(s) b = num.array(b, float64) timefunc(n, correct, (a,d,b))
def timeRandomCorrect(n, size): s = size * size a = rand.randint(0, 2**16, s) a = num.array(a, uint16) d = rand.random(s) d = num.array(d, float64) b = rand.random(s) b = num.array(b, float64) timefunc(n, correct, (a, d, b))
def setdata(self, A): """Note: p, q <= min(n,m)""" self.data.Brand = 2*(random((A.shape[0],self.data.p))-0.5) self.data.Crand = 2*(random((A.shape[1],self.data.q))-0.5) self.data.D = zeros((self.data.q,self.data.p), Float) if self.update: U, S, Vh = linalg.svd(A) self.data.B = U[:,-1*self.data.p:] self.data.C = num_transpose(Vh)[:,-1*self.data.q:] else: self.data.B = self.data.Brand self.data.C = self.data.Crand
def rand(*args): """rand(d1,...,dn) returns a matrix of the given dimensions which is initialized to random numbers from a uniform distribution in the range [0,1). """ import numarray.random_array as ra return ra.random(args)
def sample(self): #Sample a value given the distribution specified in self.table rnum = ra.random() probRange = 0 i = -1 for prob in self.table: probRange += prob i += 1 if rnum <= probRange: break return i
def star(n,noise=0.,prop=0): """Create a regular n-pointed star, possibly with noise and properties. n should be odd and > 3. A noise parameter can be given to vary the regular shape. A prop can be set too. """ m = n/2 f = Formex([[[0,1]]]).rosette(n,m*360./n).data() if noise != 0.: f = f + noise * random_array.random(f.shape) P = Formex(concatenate([f,f[:1]])) return Formex.connect([P,P],bias=[0,1]).setProp(prop)
def sample(arr): #given an array of probabilities return a randomly generated int with #probability equal to the values of array nPossibleValues = len(arr) rnum = ra.random() probRange = arr[0] i = 0 for prob in arr[1:]: if rnum < probRange: break else: probRange += prob i += 1 return i
## This file is part of pyFormex 0.2 Release Mon Jan 3 14:54:38 2005 ## pyFormex is a python implementation of Formex algebra ## Homepage: http://pyformex.berlios.de/ ## Copyright (C) 2004 Benedict Verhegghe ([email protected]) ## Copyright (C) 2004 Bart Desloovere ([email protected]) ## Distributed under the General Public License, see file COPYING for details ## # """Stars""" from numarray import random_array nstars = 100 # number of stars npoints = 7 # number of points in the star noise = 0.3 # relative amplitude of noise displ = nstars*0.6 # relative displacement def star(n,noise=0.,prop=0): """Create a regular n-pointed star, possibly with noise and properties. n should be odd and > 3. A noise parameter can be given to vary the regular shape. A prop can be set too. """ m = n/2 f = Formex([[[0,1]]]).rosette(n,m*360./n).data() if noise != 0.: f = f + noise * random_array.random(f.shape) P = Formex(concatenate([f,f[:1]])) return Formex.connect([P,P],bias=[0,1]).setProp(prop) Stars = Formex.concatenate([ star(npoints,noise,i).translate(displ*random_array.random((3,))) for i in range(nstars) ]) clear() drawProp(Stars)
value goodCases= N.logical_and((Pz < pd), (Pz > -pd))# use the enclosing square goodCases= N.logical_and.reduce(goodCases, 1) Pz= N.compress(goodCases, Pz) # discard points outside the square if len(Pz) == 1: Pt= Pz[0] # We have found the closest Pz= [] lengthRemaining[trial]+= [len(Pz)] z= 100 trial+= 1 return Pt + p if __name__ == '__main__': for sampleSize in range(100, 5000, 100): P= R.random(shape= (sampleSize, 2)) for i in range(20): p= R.random((1, 2)) # point a= find(P, p) ## print 'Closest neighbour:', a[0] ## print 'Check - Point(p):', p[0] ## check= [] ## for i in range(len(P)): ## check+= [(math.sqrt((P[i, 0]-p[0, 0])**2 + (P[i, 1]-p[0,1])**2), P[i, 0], P[i, 1])] ## print P[i], math.sqrt((P[i, 0]-p[0, 0])**2 + (P[i, 1]-p[0, 1])**2) ## check.sort() ## print check[0] ## print check print 'Number of scans:', sum([len(lst) for lst in lengthRemaining]) print 'Sample size:', P.shape[0], ' Average numner of scans:', sum([len(lst) for lst in lengthRemaining])/float(len(lengthRemaining))
def timeRandomStats(n, shape): a = rand.random(shape) a = num.array(a, float32) timefunc(n, stats, (a,))
def timeRandomStats(n, shape): a = rand.random(shape) a = num.array(a, float32) timefunc(n, stats, (a, ))
def run(d): print "----" print "dataset =", d print "mean =", mean(d) print "median =", median(d) print "mode(s) =", mode(d) print "range =", rang(d) print "midrange =", midrange(d) print "mean deviation =", meandev(d) print "standard deviation =", stddev(d) print "variance =", variance(d) print "bias-corrected variance =", bvariance(d) print "----\n" o = random_array.random(3) * 10 e = random_array.random(4) * 10 ao = random_array.random([3,3]) * 10 ae = random_array.random([3,4]) * 10 h = array([[1, 2, 3], [1, 4, 5], [3, 2, 8], [1, 8, 5]]) run(o) run(e) run(ao) run(ae) run(h) print "----\nNormal Distribution" r = normpdf(0, 0, 1) print "normpdf(0, 0, 1) =", r
#!/usr/bin/env pyformex # $Id$ # Creates random points, bars, triangles, quads, ... """Random""" from numarray import random_array npoints = 100 P = Formex(random_array.random((npoints,1,3))) clear();drawProp(P);sleep() for n in range(2,4): F = Formex.connect([P for i in range(n)],bias=[i*(n-1) for i in range(n)],loop=True) F.setProp(arange(npoints)) clear();drawProp(F);sleep()
def randomStats(shape): a = rand.random(shape) stats(a)