Пример #1
0
def test():
    # get the package
    import gsl

    # the terms
    α = 2
    β = 3
    # the matrix A
    A = gsl.matrix(shape=(2, 2))
    A[0, 0], A[0, 1] = 2, 3
    A[1, 0], A[1, 1] = 3, 2
    # the matrix B
    B = gsl.matrix(shape=(2, 3))
    B[0, 0], B[0, 1], B[0, 2] = 2, 3, 2
    B[1, 0], B[1, 1], B[1, 2] = 1, 2, 1
    # the matrix C
    C = gsl.matrix(shape=(2, 3))
    C[0, 0], C[0, 1], C[0, 2] = 0, 0, 0
    C[1, 0], C[1, 1], C[1, 2] = 0, 0, 1

    # compute the form
    gsl.blas.dsymm(A.sideLeft, A.upperTriangular, α, A, B, β, C)

    # check
    assert tuple(C) == (14, 24, 14, 16, 26, 19)

    # all done
    return
Пример #2
0
def test():
    # get the package
    import gsl

    # the terms
    α = 2
    β = 3
    # the matrix A
    A = gsl.matrix(shape=(2,2))
    A[0,0], A[0,1] = 2,3
    A[1,0], A[1,1] = 1,2
    # the matrix B
    B = gsl.matrix(shape=(2,3))
    B[0,0], B[0,1], B[0,2] = 2,3,2
    B[1,0], B[1,1], B[1,2] = 1,2,1
    # the matrix C
    C = gsl.matrix(shape=(2,3))
    C[0,0], C[0,1], C[0,2] = 0,0,0
    C[1,0], C[1,1], C[1,2] = 0,0,0

    # compute the form
    gsl.blas.dgemm(A.opNoTrans, B.opNoTrans, α, A, B, β, C)

    # check
    # print(tuple(y))
    assert tuple(C) == (14, 24, 14, 8, 14, 8)

    # all done
    return
Пример #3
0
def test():
    # get the package
    import gsl

    # the terms
    α = 2
    β = 3
    # the matrix A
    A = gsl.matrix(shape=(2,2))
    A[0,0], A[0,1] = 2,3
    A[1,0], A[1,1] = 1,2
    # the matrix B
    B = gsl.matrix(shape=(2,3))
    B[0,0], B[0,1], B[0,2] = 2,3,2
    B[1,0], B[1,1], B[1,2] = 1,2,1
    # the matrix C
    C = gsl.matrix(shape=(2,3))
    C[0,0], C[0,1], C[0,2] = 0,0,0
    C[1,0], C[1,1], C[1,2] = 0,0,0

    # compute the form
    gsl.blas.dgemm(A.opNoTrans, B.opNoTrans, α, A, B, β, C)

    # check
    # print(tuple(y))
    assert tuple(C) == (14, 24, 14, 8, 14, 8)

    # all done
    return
Пример #4
0
def test():
    # get the package
    import gsl

    # the terms
    α = 2
    β = 3
    # the matrix A
    A = gsl.matrix(shape=(2,2))
    A[0,0], A[0,1] = 2,3
    A[1,0], A[1,1] = 3,2
    # the matrix B
    B = gsl.matrix(shape=(2,3))
    B[0,0], B[0,1], B[0,2] = 2,3,2
    B[1,0], B[1,1], B[1,2] = 1,2,1
    # the matrix C
    C = gsl.matrix(shape=(2,3))
    C[0,0], C[0,1], C[0,2] = 0,0,0
    C[1,0], C[1,1], C[1,2] = 0,0,1

    # compute the form
    gsl.blas.dsymm(A.sideLeft, A.upperTriangular, α, A, B, β, C)

    # check
    assert tuple(C) == (14, 24, 14, 16, 26, 19)

    # all done
    return
Пример #5
0
def test():
    # package access
    import gsl

    # pick the matrix dimensions
    s1, s2 = 3, 4

    # make a matrix
    m1 = gsl.matrix(shape=(s1, s2))
    # set it to some value
    m1.fill(value=2)
    # verify it happened
    for e in m1:
        assert e == 2

    # make and initialize another matrix
    m2 = gsl.matrix(shape=(s1, s2)).zero()
    # fill it
    m2.fill(range(s1 * s2))
    # verify
    assert m2.tuple() == tuple(
        tuple(range(n * s2, (n + 1) * s2)) for n in range(s1))

    # all done
    return m1, m2
Пример #6
0
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
Пример #7
0
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
Пример #8
0
def test():
    # setup the workload
    sampleSize = 4
    samplesPerTask = 4
    workload = (samplesPerTask, sampleSize)

    # 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 matrix
        θ = gsl.matrix(shape=(tasks * samplesPerTask, sampleSize))
        # initialize it
        for task in range(tasks):
            for sample in range(samplesPerTask):
                for dof in range(sampleSize):
                    offset = task * samplesPerTask + sample
                    θ[offset, dof] = offset * sampleSize + dof
        # print it out
        # θ.print(format="{}")
    # the other tasks
    else:
        # have a dummy source matrix
        θ = None

    # build the destination matrix
    part = gsl.matrix(shape=workload)
    # make a partition
    part.excerpt(communicator=world, source=source, matrix=θ)

    # verify that i got the correct part
    for row in range(samplesPerTask):
        for column in range(sampleSize):
            assert part[
                row,
                column] == (rank * samplesPerTask + row) * sampleSize + column

    # all done
    return
Пример #9
0
def test():
    # get the package
    import gsl

    # create a trivial matrix
    one = gsl.matrix(shape=(100,100)).identity()

    # clone it
    one_c = one.clone()
    # decompose it
    decomp = gsl.linalg.LU_decomposition(one_c)
    # check
    assert type(decomp[0]) == gsl.matrix
    assert type(decomp[1]) == gsl.permutation
    assert type(decomp[2]) == int

    # invert it
    inv = gsl.linalg.LU_invert(*decomp)
    # check it
    for i in range(inv.rows):
        for j in range(inv.columns):
            if i == j:
                assert one[i,j] == 1
            else:
                assert one[i,j] == 0

    # compute the determinant
    det = gsl.linalg.LU_det(*decomp)
    # check it
    assert det == 1

    # return the decomposition
    return decomp
Пример #10
0
def test():
    """
    Test Cholesky/trmm/inverse/gemm
    """
    samples = 10
    parameters = 20
    precision = 'float64'

    m = gsl.matrix(shape=(samples, parameters))
    for sample in range(samples):
        for parameter in range(parameters):
            m[sample, parameter] = sample + parameter


    subset = numpy.asarray(list (range(6,8))+ [12, 15] +list(range(18,20)),  dtype='int64')
    gsubset = cuda.vector(source=subset)

    gm = cuda.matrix(source=m, dtype=precision)
    gm_sub = cuda.matrix(shape=(samples, gsubset.shape))
    gm.copycols(dst=gm_sub, indices=gsubset, batch=samples)

    print("A submatrix for indices", subset)
    gm_sub.print()
    
    
    gm.fill(1)
    gm.insert(src=gm_sub, start=(0,1))
    print("insert submatrix back")
    gm.print()

    return
Пример #11
0
def test():
    # package access
    import gsl
    # make a matrix
    m = gsl.matrix(shape=(100, 100))
    # all done
    return m
Пример #12
0
def test():
    # package access
    import gsl
    # make a matrix
    m = gsl.matrix(shape=(100, 100))
    # fill it with zeroes
    m.fill(0)
    # set an element to some value
    m[50,50] = 10
    # verify it happened
    assert m[50,50] == 10

    # set another
    m[99,99] = 5
    # access using reflected indices
    assert m[-1, -1] == 5

    # out of bounds get
    try:
        m[500,500]
        assert False
    except IndexError:
        pass

    # out of bounds set
    try:
        m[500,500] = 1
        assert False
    except IndexError:
        pass

    # reflected out of bounds get
    try:
        m[-500,-500]
        assert False
    except IndexError:
        pass

    # reflected out of bounds set
    try:
        m[-500,-500] = 1
        assert False
    except IndexError:
        pass

    # bad index tuples
    try:
        m[1,2,3]
        assert False
    except TypeError:
        pass
    # and
    try:
        m[1,2,3] = 0
        assert False
    except TypeError:
        pass

    # all done
    return m
Пример #13
0
def test():
    # package access
    import gsl
    # make a couple of matrices and initialize them
    m1 = gsl.matrix(shape=(100,100)).fill(value=1)
    m2 = gsl.matrix(shape=(100,100)).fill(value=2)
    # check
    for e in m1: assert e == 1
    for e in m2: assert e == 2
    # divide them and store the result in m1
    m1 /= m2
    # check
    for e in m1: assert e == .5
    for e in m2: assert e == 2
    # all done
    return m1, m2
Пример #14
0
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
Пример #15
0
def test():
    # package access
    import gsl
    # make a matrix
    m = gsl.matrix(shape=(100, 100))
    # fill it with zeroes
    m.fill(0)
    # set an element to some value
    m[50,50] = 10
    # verify it happened
    assert m[50,50] == 10

    # set another
    m[99,99] = 5
    # access using reflected indices
    assert m[-1, -1] == 5

    # out of bounds get
    try:
        m[500,500]
        assert False
    except IndexError:
        pass

    # out of bounds set
    try:
        m[500,500] = 1
        assert False
    except IndexError:
        pass

    # reflected out of bounds get
    try:
        m[-500,-500]
        assert False
    except IndexError:
        pass

    # reflected out of bounds set
    try:
        m[-500,-500] = 1
        assert False
    except IndexError:
        pass

    # bad index tuples
    try:
        m[1,2,3]
        assert False
    except TypeError:
        pass
    # and
    try:
        m[1,2,3] = 0
        assert False
    except TypeError:
        pass

    # all done
    return m
Пример #16
0
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
Пример #17
0
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
Пример #18
0
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
Пример #19
0
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
Пример #20
0
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
Пример #21
0
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
Пример #22
0
def test():
    # get the package
    import gsl

    # create a trivial matrix
    one = gsl.matrix(shape=(100, 100)).identity()

    # clone it
    one_c = one.clone()
    # decompose it
    decomp = gsl.linalg.LU_decomposition(one_c)
    # check
    assert type(decomp[0]) == gsl.matrix
    assert type(decomp[1]) == gsl.permutation
    assert type(decomp[2]) == int

    # invert it
    inv = gsl.linalg.LU_invert(*decomp)
    # check it
    for i in range(inv.rows):
        for j in range(inv.columns):
            if i == j:
                assert one[i, j] == 1
            else:
                assert one[i, j] == 0

    # compute the determinant
    det = gsl.linalg.LU_det(*decomp)
    # check it
    assert det == 1

    # return the decomposition
    return decomp
Пример #23
0
def test():
    # package access
    import gsl
    # make a couple of matrices and initialize them
    m1 = gsl.matrix(shape=(100,100)).fill(value=1)
    m2 = gsl.matrix(shape=(100,100)).fill(value=2)
    # check
    for e in m1: assert e == 1
    for e in m2: assert e == 2
    # divide them and store the result in m1
    m1 /= m2
    # check
    for e in m1: assert e == .5
    for e in m2: assert e == 2
    # all done
    return m1, m2
Пример #24
0
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
Пример #25
0
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
Пример #26
0
def test():
    # setup the workload
    sampleSize = 4
    samplesPerTask = 4
    workload = (samplesPerTask, sampleSize)

    # 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.matrix(shape=(tasks*samplesPerTask, sampleSize))
        # initialize it
        for task in range(tasks):
            for sample in range(samplesPerTask):
                for dof in range(sampleSize):
                    offset = task*samplesPerTask + sample
                    θ[offset, dof] = offset*sampleSize + dof
        # print it out
        # θ.print(format="{}")
    # the other tasks
    else:
        # have a dummy source matrix
        θ = None

    # build the destination matrix
    part = gsl.matrix(shape=workload)
    # make a partition
    part.excerpt(communicator=world, source=source, matrix=θ)

    # verify that i got the correct part
    for row in range(samplesPerTask):
        for column in range(sampleSize):
            assert part[row, column] == (rank*samplesPerTask+row)*sampleSize + column

    # all done
    return
Пример #27
0
def lower():
    # make a triangular matrix
    m = gsl.matrix(shape=(3,3)).identity()
    # that's non-trivial
    m[1,0] = 2
    m[2,0] = 3
    m[2,1] = 4
    # all done
    return m
Пример #28
0
def test():
    # package access
    import gsl
    # make a matrix
    m = gsl.matrix(shape=(100, 50))
    # set it to random values
    m.random(pdf=gsl.pdf.gaussian(mean=0.0, sigma=2, rng=gsl.rng()))
    # all done
    return m
Пример #29
0
def test():
    # package access
    import gsl
    # make a matrix
    m = gsl.matrix(shape=(100,100)).fill(1)
    # set an element to some value
    m[50,50] = 10
    # verify it happened
    assert 10 in m
    # all done
    return m
Пример #30
0
def test():
    # package access
    import gsl
    # make a matrix
    m = gsl.matrix(shape=(3, 3))
    # set it to some value
    m.fill(value=2)
    # print it
    m.print(indent=' ' * 4)
    # all done
    return m
Пример #31
0
def test():
    # package access
    import gsl
    # make a matrix
    m = gsl.matrix(shape=(100, 100)).fill(1)
    # set an element to some value
    m[50, 50] = 10
    # verify it happened
    assert 10 in m
    # all done
    return m
Пример #32
0
def test():
    # package access
    import gsl
    # make a matrix
    m = gsl.matrix(shape=(100,100))
    # zero it out
    m.zero()
    # verify it happened
    for e in m: assert e == 0
    # all done
    return m
Пример #33
0
def test():
    # package access
    import gsl
    # make a matrix
    m = gsl.matrix(shape=(100, 100))
    # zero it out
    m.zero()
    # verify it happened
    for e in m:
        assert e == 0
    # all done
    return m
Пример #34
0
def full():
    """
    Build a sample matrix
    """
    # make one
    m = gsl.matrix(shape=(3,3))
    # fill it
    for i in range(3):
        for j in range(3):
            m[i,j] = 3*i + j
    # all done
    return m
Пример #35
0
def test():
    # package access
    import gsl
    # make a matrix
    m = gsl.matrix(shape=(100, 100))
    # set it to some value
    m.fill(value=2)
    # verify it happened
    for e in m:
        assert e == 2
    # all done
    return m
Пример #36
0
def test():
    # package access
    import gsl
    # make a couple of matrices and initialize them
    m = gsl.matrix(shape=(100,100)).fill(value=1)
    # check
    for e in m: assert e == 1
    # add them and store the result in m1
    m += 1
    # check
    for e in m: assert e == 2
    # all done
    return m
Пример #37
0
def test():
    # package access
    import gsl
    # make a couple of matrices and initialize them
    m = gsl.matrix(shape=(100,100)).fill(value=1)
    # check
    for e in m: assert e == 1
    # add them and store the result in m1
    m *= 2
    # check
    for e in m: assert e == 2
    # all done
    return m
Пример #38
0
def test():
    # package access
    import gsl

    # pick the matrix dimensions
    s1, s2 = 3, 4

    # make a matrix
    m1 = gsl.matrix(shape=(s1,s2))
    # set it to some value
    m1.fill(value=2)
    # verify it happened
    for e in m1: assert e == 2

    # make and initialize another matrix
    m2 = gsl.matrix(shape=(s1,s2)).zero()
    # fill it
    m2.fill(range(s1*s2))
    # verify
    assert m2.tuple() == tuple(tuple(range(n*s2, (n+1)*s2)) for n in range(s1))

    # all done
    return m1, m2
Пример #39
0
def test():
    # package access
    import gsl
    # make a matrix
    m = gsl.matrix(shape=(100,50))
    # set it to random values
    m.random(pdf=gsl.pdf.gaussian(mean=0.0, sigma=2, rng=gsl.rng()))
    # clone it
    n = m.clone()
    # and check it
    assert m == n

    # all done
    return m, n
Пример #40
0
def test():
    # package access
    import gsl
    # make a matrix and initialize it
    m = gsl.matrix(shape=(100, 100)).fill(value=1)
    # unpack the shape
    s0, s1 = m.shape
    # prime
    for i0 in range(s0):
        for i1 in range(s1):
            m[i0, i1] = 2 * (i0 + i1) + 1
    # find the min
    small = m.min()
    # check it
    assert small == 1
    # all done
    return m
Пример #41
0
def test():
    # package access
    import gsl
    # make a matrix and initialize it
    m = gsl.matrix(shape=(100,100)).fill(value=1)
    # unpack the shape
    s0, s1 = m.shape
    # prime
    for i0 in range(s0):
        for i1 in range(s1):
            m[i0, i1] = 2*(i0+i1)+1
    # find the min
    small = m.min()
    # check it
    assert small == 1
    # all done
    return m
Пример #42
0
def test():
    # setup the workload
    sampleSize = 4
    samplesPerTask = 1
    workload = (samplesPerTask, sampleSize)

    # 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.matrix(shape=workload)
    # and initialize it
    for row in range(samplesPerTask):
        for column in range(sampleSize):
            θ[row,
              column] = (rank * samplesPerTask + row) * sampleSize + column

    # decide on the destination task
    destination = 0
    # exercise it
    result = gsl.matrix.collect(matrix=θ,
                                communicator=world,
                                destination=destination)

    # at the destination task
    if rank == destination:
        # verify that i got the correct parts
        for task in range(tasks):
            for sample in range(samplesPerTask):
                for dof in range(sampleSize):
                    offset = task * samplesPerTask + sample
                    assert result[offset, dof] == offset * sampleSize + dof
        # print it out
        # result.print(format='{}')

    # all done
    return
Пример #43
0
def test():
    # setup the workload
    samples = 4
    parameters = 8
    workload = (samples, parameters)

    # 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 matrix
        θ = gsl.matrix(shape=workload)
        # initialize it
        for sample in range(samples):
            for dof in range(parameters):
                θ[sample, dof] = sample * parameters + dof
        # print it out
        # θ.print(format="{}")
    # the other tasks
    else:
        # have a dummy source matrix
        θ = None

    # broadcast
    result = gsl.matrix.bcast(source=source, matrix=θ)

    # verify that i got the correct part
    for sample in range(samples):
        for dof in range(parameters):
            assert result[sample, dof] == sample * parameters + dof

    # all done
    return
Пример #44
0
def test():
    # package access
    import gsl
    # make a matrix
    m = gsl.matrix(shape=(2, 3))
    # set some values
    m[0, 0] = 0
    m[0, 1] = 1
    m[0, 2] = 2
    m[1, 0] = 10
    m[1, 1] = 11
    m[1, 2] = 12

    # verify the tuple rep
    assert m.tuple() == ((0, 1, 2), (10, 11, 12))

    # all done
    return m
Пример #45
0
def test():
    # package access
    import gsl
    # make a matrix
    m = gsl.matrix(shape=(2,3))
    # set some values
    m[0,0] = 0
    m[0,1] = 1
    m[0,2] = 2
    m[1,0] = 10
    m[1,1] = 11
    m[1,2] = 12

    # verify the tuple rep
    assert m.tuple() == ( (0,1,2), (10,11,12) )

    # all done
    return m
Пример #46
0
def test():
    # package access
    import gsl
    # pick a size
    n = 4
    # make one
    m = gsl.matrix(shape=(n,n))
    # fill it
    for i in range(n):
        for j in range(n):
            m[i,j] = i*n + j
    # show me
    # print('m:')
    # m.print(format='{:6.2f}', indent=' '*4)

    # pick some parameters
    start = (1,1)
    shape = (2,2)
    # make a submatrix
    v = m.view(start=start, shape=shape)
    # show me
    # print('v:')
    # v.print(format='{:6.2f}', indent=' '*4)

    # verify the shape
    assert v.shape == shape
    # verify the contents
    for i in range(shape[0]):
        for j in range(shape[1]):
            assert v[i,j] == m[i+start[0], j+start[1]]

    # now modify
    v.fill(0)
    # show me
    # print('m:')
    # m.print(format='{:6.2f}', indent=' '*4)
    # and check
    for i in range(shape[0]):
        for j in range(shape[1]):
            assert m[i+start[0],j+start[1]] == 0

    # all done
    return
Пример #47
0
def test():
    # setup the workload
    samples = 4
    parameters = 8
    workload = (samples, parameters)

    # 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.matrix(shape=workload)
        # initialize it
        for sample in range(samples):
            for dof in range(parameters):
                θ[sample, dof] = sample*parameters + dof
        # print it out
        # θ.print(format="{}")
    # the other tasks
    else:
        # have a dummy source matrix
        θ = None

    # broadcast
    result = gsl.matrix.bcast(source=source, matrix=θ)

    # verify that i got the correct part
    for sample in range(samples):
        for dof in range(parameters):
            assert result[sample, dof] == sample*parameters + dof

    # all done
    return
Пример #48
0
def test():
    # package access
    import gsl
    # pick a size
    n = 4
    # make one
    m = gsl.matrix(shape=(n,n))
    # fill it
    for i in range(n):
        for j in range(n):
            m[i,j] = i*n + j
    # show me
    # print('m:')
    # m.print(format='{:6.2f}', indent=' '*4)

    # pick some parameters
    start = (1,1)
    shape = (2,2)
    # make a submatrix
    v = m.view(start=start, shape=shape)
    # show me
    # print('v:')
    # v.print(format='{:6.2f}', indent=' '*4)

    # verify the shape
    assert v.shape == shape
    # verify the contents
    for i in range(shape[0]):
        for j in range(shape[1]):
            assert v[i,j] == m[i+start[0], j+start[1]]

    # now modify
    v.fill(0)
    # show me
    # print('m:')
    # m.print(format='{:6.2f}', indent=' '*4)
    # and check
    for i in range(shape[0]):
        for j in range(shape[1]):
            assert m[i+start[0],j+start[1]] == 0

    # all done
    return
Пример #49
0
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
Пример #50
0
def test():
    # setup the workload
    sampleSize = 4
    samplesPerTask = 1
    workload = (samplesPerTask, sampleSize)

    # externals
    import mpi
    import gsl

    # get the world communicator
    world = mpi.world
    # figure out its geometry
    rank = world.rank
    tasks = world.size

    # build my contribution
    θ = gsl.matrix(shape=workload)
    # and initialize it
    for row in range(samplesPerTask):
        for column in range(sampleSize):
            θ[row, column] = (rank*samplesPerTask+row)*sampleSize + column

    # decide on the destination task
    destination = 0
    # exercise it
    result = gsl.matrix.collect(matrix=θ, communicator=world, destination=destination)

    # at the destination task
    if rank == destination:
        # verify that i got the correct parts
        for task in range(tasks):
            for sample in range(samplesPerTask):
                for dof in range(sampleSize):
                    offset = task*samplesPerTask+sample
                    assert result[offset, dof] == offset*sampleSize + dof
        # print it out
        # result.print(format='{}')

    # all done
    return
Пример #51
0
    def copy_to_host(self, target=None, type='gsl'):
        """
        copy cuda matrix to host (gsl or numpy)
        gsl.matrix is double precison only
        numpy.ndarray can be any type
        """
        # if target is not given
        if target is None:
            if type == 'gsl':
                target = gsl.matrix(shape=self.shape)
                libcuda.matrix_togsl(target.data, self.data)
            else: #numpy
                target = numpy.ndarray(shape=self.shape, dtype=self.dtype)
                libcuda.matrix_tonumpy(target, self.data)
        # if target is a pre-allocated gsl.matrix
        elif isinstance(target, gsl.matrix):
            libcuda.matrix_togsl(target.data, self.data)
        # assume numpy ndarray for rest cases
        else:
            libcuda.matrix_tonumpy(target, self.data)

        # return
        return target
Пример #52
0
def test():
    # make a lower triangular matrix
    A = lower()
    # show me
    # A.print()

    # make a sample matrix
    B = full()
    # show me
    # B.print()

    # try one of the supported operations
    B = gsl.blas.dtrmm(
        A.sideLeft, A.lowerTriangular, A.opNoTrans, A.unitDiagonal,
        2, A, B
        )
    # show me
    # B.print()
    # the expected result
    result = gsl.matrix(shape=(3,3))
    result[0,0] = 0
    result[0,1] = 2
    result[0,2] = 4
    result[1,0] = 6
    result[1,1] = 12
    result[1,2] = 18
    result[2,0] = 36
    result[2,1] = 52
    result[2,2] = 68

    # check
    assert B == result

    # make an upper triangular matrix
    A = upper()
    # show me
    # A.print()

    # make a sample matrix
    B = full()
    # show me
    # B.print()

    # try one of the supported operations
    B = gsl.blas.dtrmm(
        A.sideLeft, A.upperTriangular, A.opNoTrans, A.unitDiagonal,
        2, A, B
        )
    # show me
    # B.print()
    # the expected result
    result = gsl.matrix(shape=(3,3))
    result[0,0] = 48
    result[0,1] = 60
    result[0,2] = 72
    result[1,0] = 54
    result[1,1] = 64
    result[1,2] = 74
    result[2,0] = 12
    result[2,1] = 14
    result[2,2] = 16

    # check
    assert B == result
    # all done
    return