예제 #1
0
파일: base.py 프로젝트: bsilverthorn/cargo
def semicast_arguments(out_argument, *in_arguments):
    """
    Appropriately broadcast arguments and an output array.
    """

    # XXX semicasting isn't quite right---needs to be slightly rethought
    # XXX to handle the "extra" parameters correctly; perhaps what we're
    # XXX truly doing is semicasting individual regions across arrays?
    # XXX see notes, eg, 11/5/2010:2

    # semicast arrays
    from cargo.numpy import semicast

    prefix_i = lambda d, e: -(e + len(d.shape)) or None
    out_pair = (out_argument.array, prefix_i(*out_argument[1:]))
    in_pairs = [(numpy.asarray(a, d.base), prefix_i(d, e)) for (a, d, e) in in_arguments]

    if out_argument.array is None:
        (shape, cast_arrays) = semicast(*in_pairs)
        out_array = numpy.empty(shape, out_argument.dtype)

        return (shape, [out_array] + cast_arrays)
    else:
        (shape, cast_arrays) = semicast(*([out_pair] + in_pairs))

        assert out.shape == shape + out_argument.dtype.shape

        return (shape, cast_arrays)
예제 #2
0
def semicast_arguments(out_argument, *in_arguments):
    """
    Appropriately broadcast arguments and an output array.
    """

    # XXX semicasting isn't quite right---needs to be slightly rethought
    # XXX to handle the "extra" parameters correctly; perhaps what we're
    # XXX truly doing is semicasting individual regions across arrays?
    # XXX see notes, eg, 11/5/2010:2

    # semicast arrays
    from cargo.numpy import semicast

    prefix_i = lambda d, e: -(e + len(d.shape)) or None
    out_pair = (out_argument.array, prefix_i(*out_argument[1:]))
    in_pairs = [(numpy.asarray(a, d.base), prefix_i(d, e))
                for (a, d, e) in in_arguments]

    if out_argument.array is None:
        (shape, cast_arrays) = semicast(*in_pairs)
        out_array = numpy.empty(shape, out_argument.dtype)

        return (shape, [out_array] + cast_arrays)
    else:
        (shape, cast_arrays) = semicast(*([out_pair] + in_pairs))

        assert out.shape == shape + out_argument.dtype.shape

        return (shape, cast_arrays)
예제 #3
0
def test_semicast_partial():
    """
    Test partial ndarray broadcasting via semicast.
    """

    from cargo.numpy import semicast

    a = numpy.ones((2, 3, 4))
    b = numpy.zeros((3, 5))

    (shape, (aa, bb)) = semicast((a, -1), (b, -1))

    assert_equal(shape, (2, 3))
    assert_equal(aa.shape, (2, 3, 4))
    assert_equal(bb.shape, (2, 3, 5))
    assert_equal(aa.tolist(), numpy.ones((2, 3, 4)).tolist())
    assert_equal(bb.tolist(), numpy.zeros((2, 3, 5)).tolist())
예제 #4
0
def test_semicast_full():
    """
    Test full ndarray broadcasting via semicast.
    """

    from cargo.numpy import semicast

    a = numpy.arange(2)
    b = numpy.arange(4)

    (shape, (aa, bb)) = semicast((a, None), (b[:, None], None))

    assert_equal(shape, (4, 2))
    assert_equal(aa.shape, (4, 2))
    assert_equal(bb.shape, (4, 2))
    assert_equal(aa.tolist(), [[0, 1]] * 4)
    assert_equal(bb.tolist(), [[0, 0], [1, 1], [2, 2], [3, 3]])
예제 #5
0
def test_semicast_partial():
    """
    Test partial ndarray broadcasting via semicast.
    """

    from cargo.numpy import semicast

    a = numpy.ones((2, 3, 4))
    b = numpy.zeros((3, 5))

    (shape, (aa, bb)) = semicast((a, -1), (b, -1))

    assert_equal(shape, (2, 3))
    assert_equal(aa.shape, (2, 3, 4))
    assert_equal(bb.shape, (2, 3, 5))
    assert_equal(aa.tolist(), numpy.ones((2, 3, 4)).tolist())
    assert_equal(bb.tolist(), numpy.zeros((2, 3, 5)).tolist())
예제 #6
0
def test_semicast_full():
    """
    Test full ndarray broadcasting via semicast.
    """

    from cargo.numpy import semicast

    a = numpy.arange(2)
    b = numpy.arange(4)

    (shape, (aa, bb)) = semicast((a, None), (b[:, None], None))

    assert_equal(shape, (4, 2))
    assert_equal(aa.shape, (4, 2))
    assert_equal(bb.shape, (4, 2))
    assert_equal(aa.tolist(), [[0, 1]] * 4)
    assert_equal(bb.tolist(), [[0, 0], [1, 1], [2, 2], [3, 3]])
예제 #7
0
def test_semicast_full_fields():
    """
    Test full ndarray broadcasting via semicast, with struct dtype.
    """

    from cargo.numpy import semicast

    d = numpy.dtype([("v", numpy.uint)])
    a = numpy.array([(i,) for i in xrange(2)], d)
    b = numpy.array([(i,) for i in xrange(4)], d)

    (shape, (aa, bb)) = semicast((a, None), (b[:, None], None))

    assert_equal(shape, (4, 2))
    assert_equal(aa.shape, (4, 2))
    assert_equal(bb.shape, (4, 2))
    assert_equal(aa.dtype, d)
    assert_equal(bb.dtype, d)
    assert_equal(aa.tolist(), [[(0,), (1,)]] * 4)
    assert_equal(bb.tolist(), [[(i,), (i,)] for i in xrange(4)])
예제 #8
0
def test_semicast_full_fields():
    """
    Test full ndarray broadcasting via semicast, with struct dtype.
    """

    from cargo.numpy import semicast

    d = numpy.dtype([("v", numpy.uint)])
    a = numpy.array([(i, ) for i in xrange(2)], d)
    b = numpy.array([(i, ) for i in xrange(4)], d)

    (shape, (aa, bb)) = semicast((a, None), (b[:, None], None))

    assert_equal(shape, (4, 2))
    assert_equal(aa.shape, (4, 2))
    assert_equal(bb.shape, (4, 2))
    assert_equal(aa.dtype, d)
    assert_equal(bb.dtype, d)
    assert_equal(aa.tolist(), [[(0, ), (1, )]] * 4)
    assert_equal(bb.tolist(), [[(i, ), (i, )] for i in xrange(4)])