def test(): # get the package import gsl # the terms α = 2 β = 3 # the vector x x = gsl.vector(shape=3) x[0], x[1], x[2] = 1,2,3 # the vector y y = gsl.vector(shape=3) y[0], y[1], y[2] = 2,4,6 # the matrix A A = gsl.matrix(shape=(3,3)).identity() A[0,1], A[0,2], A[1,2] = 2,3,2 # compute the form y = gsl.blas.dsymv(A.upperTriangular, α, A, x, β, y) # check # print(tuple(y)) assert tuple(y) == (34, 32, 38) # all done return
def test(): # math from math import pi, sqrt, exp # access the package import gsl # build a random number generator rng = gsl.rng() # the order of the distribution K = 10 # the weight vectors α = gsl.vector(shape=K).fill(K**(-1/2)) # build a gaussian distribution dirichlet = gsl.pdf.dirichlet(alpha=α, rng=rng) # make a vector v = gsl.vector(shape=K) # fill it with random numbers v.random(pdf=dirichlet) # make a matrix m = gsl.matrix(shape=(50, K)) # fill it with random numbers m.random(pdf=dirichlet) return dirichlet
def test(): # get the package import gsl # the terms α = 2 β = 3 # the vector x x = gsl.vector(shape=3) x[0], x[1], x[2] = 1,2,3 # the vector y y = gsl.vector(shape=3) y[0], y[1], y[2] = 2,4,6 # the matrix A A = gsl.matrix(shape=(3,3)).identity() A[0,1], A[0,2], A[1,2] = 2,3,2 A[1,0], A[2,0], A[2,1] = 2,3,2 # compute the form y = gsl.blas.dgemv(A.opNoTrans, α, A, x, β, y) # check # print(tuple(y)) assert tuple(y) == (34, 32, 38) # all done return
def test(): # package access import gsl # make a matrix m = gsl.matrix(shape=(100, 50)) # fill it with random values m.random(pdf=gsl.pdf.gaussian(mean=0.0, sigma=2, rng=gsl.rng())) # rows i = 3 # set the ith row to all {i} for j in range(m.columns): m[i, j] = i # get the row using the interface row = m.getRow(i) # check that it is a vector assert row.shape == m.columns # full of {i} assert row == gsl.vector(shape=m.columns).fill(i) # columns j = 2 # set the jth column to all {j} for i in range(m.rows): m[i, j] = j # get the column using the interface column = m.getColumn(j) # check that it is a vector assert column.shape == m.rows # full of {j} assert column == gsl.vector(shape=m.rows).fill(j) # shape rows = 100 columns = 200 # make another matrix m = gsl.matrix(shape=(rows, columns)).zero() # make a vector of ones ones = gsl.vector(shape=columns).fill(1.0) # set the middle column m.setRow(rows / 2, ones) # verify it was done properly assert m.getRow(rows / 2) == ones # make a vector of twos twos = gsl.vector(shape=rows).fill(2.0) # set the middle column m.setColumn(columns / 2, twos) # verify it was done properly assert m.getColumn(columns / 2) == twos # all done return m
def test(): # package access import gsl # make a matrix m = gsl.matrix(shape=(100,50)) # fill it with random values m.random(pdf=gsl.pdf.gaussian(mean=0.0, sigma=2, rng=gsl.rng())) # rows i = 3 # set the ith row to all {i} for j in range(m.columns) : m[i,j] = i # get the row using the interface row = m.getRow(i) # check that it is a vector assert row.shape == m.columns # full of {i} assert row == gsl.vector(shape=m.columns).fill(i) # columns j = 2 # set the jth column to all {j} for i in range(m.rows): m[i,j] = j # get the column using the interface column = m.getColumn(j) # check that it is a vector assert column.shape == m.rows # full of {j} assert column == gsl.vector(shape=m.rows).fill(j) # shape rows = 100 columns = 200 # make another matrix m = gsl.matrix(shape=(rows,columns)).zero() # make a vector of ones ones = gsl.vector(shape=columns).fill(1.0) # set the middle column m.setRow(rows/2, ones) # verify it was done properly assert m.getRow(rows/2) == ones # make a vector of twos twos = gsl.vector(shape=rows).fill(2.0) # set the middle column m.setColumn(columns/2, twos) # verify it was done properly assert m.getColumn(columns/2) == twos # all done return m
def test(): # get the package import gsl # a couple of values x = 3 y = 2 # make a couple of vectors v1 = gsl.vector(shape=10).fill(x) v2 = gsl.vector(shape=10).fill(y) # compute the dot product assert gsl.blas.ddot(v1, v2) == x * y * v1.shape # all done return
def test(): # get the package import gsl # a couple of values x = 3 y = 2 # make a couple of vectors v1 = gsl.vector(shape=10).fill(x) v2 = gsl.vector(shape=10).fill(y) # compute the dot product assert gsl.blas.ddot(v1, v2) == x*y*v1.shape # all done return
def test(): # access the package import gsl # build a random number generator rng = gsl.rng() # build a uniform distribution uniform = gsl.pdf.uniform_pos(rng=rng) # sample it sample = uniform.sample() # assert the sample is within (0, 1) assert sample > 0 and sample < 1 density = uniform.density(0) assert density == 1 # make a vector v = gsl.vector(1000) # fill it with random numbers uniform.vector(vector=v) # assert all the samples are positive for sample in v: assert sample > 0 and sample < 1 # make a matrix m = gsl.matrix(shape=(100, 100)) # fill it with random numbers uniform.matrix(matrix=m) # assert all the samples are positive for sample in m: assert sample > 0 and sample < 1 return uniform
def test(): # package access import gsl # make a couple of vectors and initialize them v1 = gsl.vector(shape=100).fill(value=1) v2 = gsl.vector(shape=100).fill(value=2) # check for e in v1: assert e == 1 for e in v2: assert e == 2 # add them and store the result in v2 v2 -= v1 # check for e in v1: assert e == 1 for e in v2: assert e == 1 # all done return v1, v2
def test(): # math from math import pi, sqrt, exp # access the package import gsl # the σ of the distribution σ = 2 # build a random number generator rng = gsl.rng() # build a gaussian distribution gaussian = gsl.pdf.gaussian(mean=0.0, sigma=σ, rng=rng) # sample it sample = gaussian.sample() # compute the density x = 0 density = gaussian.density(x) assert density == 1 / sqrt(2 * pi * σ**2) * exp(-x**2 / (2 * σ**2)) # make a vector v = gsl.vector(1000) # fill it with random numbers gaussian.vector(vector=v) # make a matrix m = gsl.matrix(shape=(50, 200)) # fill it with random numbers gaussian.matrix(matrix=m) return gaussian
def test(): # package access import gsl # make a vector v = gsl.vector(shape=100) # all done return v
def test(): # access the package import gsl # the support of the distribution support = (-1,1) # build a random number generator rng = gsl.rng() # build a uniform distribution uniform = gsl.pdf.uniform(support=support, rng=rng) # sample it sample = uniform.sample() assert sample >= support[0] and sample < support[1] density = uniform.density(0) assert density == 1/(support[1]-support[0]) # make a vector v = gsl.vector(1000) # fill it with random numbers uniform.vector(vector=v) # make a matrix m = gsl.matrix(shape=(100, 100)) # fill it with random numbers uniform.matrix(matrix=m) return uniform
def test(): # access the package import gsl # the support of the distribution support = (-1, 1) # build a random number generator rng = gsl.rng() # build a uniform distribution uniform = gsl.pdf.uniform(support=support, rng=rng) # sample it sample = uniform.sample() assert sample >= support[0] and sample < support[1] density = uniform.density(0) assert density == 1 / (support[1] - support[0]) # make a vector v = gsl.vector(1000) # fill it with random numbers uniform.vector(vector=v) # make a matrix m = gsl.matrix(shape=(100, 100)) # fill it with random numbers uniform.matrix(matrix=m) return uniform
def test(): # package access import gsl # make a couple of vectors and initialize them v1 = gsl.vector(shape=100).fill(value=2) v2 = gsl.vector(shape=100).fill(value=2) # check for e in v1: assert e == 2 for e in v2: assert e == 2 # multiply them and store the result in v1 v1 *= v2 # check for e in v1: assert e == 4 for e in v2: assert e == 2 # all done return v1, v2
def test(): # math from math import pi, sqrt, exp # access the package import gsl # the σ of the distribution σ = 2 # build a random number generator rng = gsl.rng() # build a gaussian distribution gaussian = gsl.pdf.gaussian(mean=0.0, sigma=σ, rng=rng) # sample it sample = gaussian.sample() # compute the density x = 0 density = gaussian.density(x) assert density == 1/sqrt(2*pi*σ**2) * exp(-x**2/ (2*σ**2)) # make a vector v = gsl.vector(1000) # fill it with random numbers gaussian.vector(vector=v) # make a matrix m = gsl.matrix(shape=(50, 200)) # fill it with random numbers gaussian.matrix(matrix=m) return gaussian
def test(): # setup the workload samplesPerTask = 8 workload = samplesPerTask # externals import mpi import gsl # initialize mpi.init() # get the world communicator world = mpi.world # figure out its geometry rank = world.rank tasks = world.size # decide which task is the source source = 0 # at the source task if rank == source: # allocate a vector θ = gsl.vector(shape=tasks * samplesPerTask) # initialize it for task in range(tasks): for sample in range(samplesPerTask): offset = task * samplesPerTask + sample θ[offset] = offset # print it out # θ.print(format="{}") # the other tasks else: # have a dummy source vector θ = None # make a partition part = gsl.vector(shape=workload) part.excerpt(communicator=world, source=source, vector=θ) # verify that i got the correct part for index in range(samplesPerTask): assert part[index] == rank * samplesPerTask + index # all done return
def test(): # package access import gsl # make a vector v = gsl.vector(shape=100) # set it to random values v.random(pdf=gsl.pdf.uniform(support=(-1,1), rng=gsl.rng())) # all done return v
def test(): # get the package import gsl # a couple of values a = 2 x = 3 y = 4 # make a couple of vectors v1 = gsl.vector(shape=10).fill(x) v2 = gsl.vector(shape=10).fill(y) # compute the form gsl.blas.daxpy(a, v1, v2) # verify v1 was left alone assert v1 == gsl.vector(shape=10).fill(x) # and that v2 has the right value assert v2 == gsl.vector(shape=10).fill(a*x+y) # all done return
def test(): # package access import gsl # make a vector v = gsl.vector(shape=100) # set it to random values v.random(pdf=gsl.pdf.uniform(support=(-1, 1), rng=gsl.rng())) # all done return v
def test(): # setup the workload samplesPerTask = 8 workload = samplesPerTask # externals import mpi import gsl # get the world communicator world = mpi.world # figure out its geometry rank = world.rank tasks = world.size # decide which task is the source source = 0 # at the source task if rank == source: # allocate a vector θ = gsl.vector(shape=tasks*samplesPerTask) # initialize it for task in range(tasks): for sample in range(samplesPerTask): offset = task*samplesPerTask + sample θ[offset] = offset # print it out # θ.print(format="{}") # the other tasks else: # have a dummy source vector θ = None # make a partition part = gsl.vector(shape=workload) part.excerpt(communicator=world, source=source, vector=θ) # verify that i got the correct part for index in range(samplesPerTask): assert part[index] == rank*samplesPerTask + index # all done return
def test(): # package access import gsl # make two vectors length = 100 v1 = gsl.vector(shape=length) v2 = gsl.vector(shape=length) # set them to random values rng = gsl.rng() v1.random(pdf=gsl.pdf.uniform(support=(-1,1), rng=rng)) v2.random(pdf=gsl.pdf.uniform(support=(-1,1), rng=rng)) # call correlation covariance = gsl.stats.covariance(v1, v2) # covariance between a vector and itself is its variance assert gsl.stats.covariance(v1, v1) == v1.variance(mean=v1.mean()) # all done return covariance
def test(): # package access import gsl # make a vector v1 = gsl.vector(shape=100) # set it to some value v1.fill(value=2) # verify it happened for e in v1: assert e == 2 # make and initialize another vector v2 = gsl.vector(shape=100).zero() # fill it v2.fill(range(100)) # verify assert v2.tuple() == tuple(range(100)) # all done return v1, v2
def test(): # package access import gsl # make a matrix v = gsl.vector(shape=3) # set it to some value v.fill(value=2) # print it v.print(indent=' '*4) # all done return v
def test(): # package access import gsl # make a matrix v = gsl.vector(shape=3) # set it to some value v.fill(value=2) # print it v.print(indent=' ' * 4) # all done return v
def test(): # package access import gsl # make two vectors length = 100 v1 = gsl.vector(shape=length) v2 = gsl.vector(shape=length) # set them to random values rng = gsl.rng() v1.random(pdf=gsl.pdf.uniform(support=(-1,1), rng=rng)) v2.random(pdf=gsl.pdf.uniform(support=(-1,1), rng=rng)) # call correlation correlation = gsl.stats.correlation(v1, v2) # correlation of a vector with itself should be one assert gsl.stats.correlation(v1, v1) == 1.0 # all done return correlation
def test(): # package access import gsl # make a vector v = gsl.vector(shape=100) # zero it out v.zero() # verify it happened for e in v: assert e == 0 # all done return v
def test(): # externals import gsl import math # a value x = 3 # make a couple of vectors v = gsl.vector(shape=10).fill(x) # compute the dot product assert gsl.blas.dnrm2(v) == math.sqrt(v.shape * x**2) # all done return
def test(): # externals import gsl import math # a value x = -3 # make a couple of vectors v = gsl.vector(shape=10).fill(x) # compute the dot product assert gsl.blas.dasum(v) == v.shape * abs(x) # all done return
def test(): # package access import gsl # make a vector and initialize it v = gsl.vector(shape=100) # prime for index in range(v.shape): v[index] = 2*index+1 # find the min small = v.min() # check it assert small == 1 # all done return v
def test(): # package access import gsl # make a vector v = gsl.vector(shape=100) # set an element to some value v[50] = 10 # verify it happened assert v[50] == 10 # check that it can be found assert 10 in v # all done return v
def test(): # package access import gsl # make a vector and initialize it v = gsl.vector(shape=100) # prime for index in range(v.shape): v[index] = 2*index+1 # find the max big = v.max() # check it assert big == 2*(v.shape-1)+1 # all done return v
def test(): # package access import gsl # make a vector and initialize it v = gsl.vector(shape=100) # prime for index in range(v.shape): v[index] = 2*index+1 # find both the min and the max small, big = v.minmax() # check it assert small == 1 assert big == 2*(v.shape-1)+1 # all done return v
def test(): # package access import gsl # make a vectors and initialize it v = gsl.vector(shape=100).fill(value=1) # check for e in v: assert e == 1 # scale it v *= 2 # check for e in v: assert e == 2 # all done return v
def test(): # package access import gsl # make a vector v = gsl.vector(shape=100) # set it to some value v.random(pdf=gsl.pdf.uniform(support=(-1,1), rng=gsl.rng())) # clone it u = v.clone() # and check it assert u == v # all done return v, u
def test(): # package access import gsl # make a matrix v = gsl.vector(shape=3) # set some values v[0] = 0 v[1] = 1 v[2] = 2 # verify the tuple rep assert v.tuple() == (0, 1, 2) # all done return v
def test(): # package access import gsl # make a vector v = gsl.vector(shape=100) # set it to some value v.random(pdf=gsl.pdf.uniform(support=(-1, 1), rng=gsl.rng())) # clone it u = v.clone() # and check it assert u == v # all done return v, u
def test(): # package access import gsl # make a matrix v = gsl.vector(shape=3) # set some values v[0] = 0 v[1] = 1 v[2] = 2 # verify the tuple rep assert v.tuple() == (0,1,2) # all done return v
def test(): # package access import gsl # make a vector v = gsl.vector(shape=100) # fill with a test pattern for i in range(len(v)): v[i] = i # verify it happened assert v[50] == 50 # access through reflection v[-99] == v[1] # all done return v
def test(): # package access import gsl # make a vector and initialize it v = gsl.vector(shape=100) # prime for index in range(v.shape): v[index] = 2 * index + 1 # shuffle vs = v.shuffle(rng=gsl.rng()) # check it assert vs.mean() == v.mean() assert vs.max() == v.max() assert vs.min() == v.min() # all done return v
def test(): # package access import gsl # make a vector v = gsl.vector(shape=100) # fill it with our test pattern v[:] = range(v.shape) # verify it happened for i in range(v.shape): assert v[i] == i # and again assert tuple(v[:]) == tuple(range(v.shape)) # also assert tuple(v[10:20:2]) == tuple(range(10,20,2)) # and for value, i in zip(v[10::2], range(10,2)): assert value == i # all done return v
def test(): # get the package import gsl # the vector x x = gsl.vector(shape=3) x[0], x[1], x[2] = 1,2,3 # the matrix A A = gsl.matrix(shape=(3,3)).identity() # compute the form y = gsl.blas.dtrmv(A.upperTriangular, A.opTrans, A.unitDiagonal, A, x.clone()) # check # print(tuple(y)) assert tuple(y) == tuple(x) # all done return
def test(): # setup the workload samplesPerTask = 8 workload = samplesPerTask # externals import mpi import gsl # initialize mpi.init() # get the world communicator world = mpi.world # figure out its geometry rank = world.rank tasks = world.size # build my contribution θ = gsl.vector(shape=workload) # and initialize it for index in range(samplesPerTask): θ[index] = rank * samplesPerTask + index # decide on the destination task destination = 0 # exercise it result = gsl.vector.collect(vector=θ, communicator=world, destination=destination) # at the destination task if rank == destination: # verify that i got the correct parts for task in range(tasks): for index in range(samplesPerTask): offset = task * samplesPerTask + index assert result[offset] == offset # print it out # result.print(format='{}') # all done return
def test(): # setup the workload parameters = 8 # externals import mpi import gsl # get the world communicator world = mpi.world # figure out its geometry rank = world.rank tasks = world.size # decide which task is the source source = 0 # at the source task if rank == source: # allocate a matrix θ = gsl.vector(shape=parameters) # initialize it for dof in range(parameters): θ[dof] = dof # print it out # θ.print(format="{}") # the other tasks else: # have a dummy source matrix θ = None # broadcast result = gsl.vector.bcast(source=source, vector=θ) # verify that i got the correct part for dof in range(parameters): assert result[dof] == dof # all done return
def test(): # package access import gsl # pick a size n = 20 # make one v = gsl.vector(shape=n) # fill it for i in range(n): v[i] = i # show me # print('v:') # v.print(format='{:6.2f}', indent=' '*4) # pick some parameters start = int(n/4) shape = n - start # make a subvector s = v.view(start=start, shape=shape) # show me # print('s:') # s.print(format='{:6.2f}', indent=' '*4) # check the length assert len(s) == shape # check the contents for i in range(shape): assert s[i] == start + i # now modify s.fill(0) # and check for i in range(shape): assert v[start+i] == 0 # all done return