Example #1
0
def test_multi_dot():
    for dim in [4, 6]:
        my_list = []
        length = 1000 + np.random.randint(200)
        for i in range(dim**2):
            my_list.append(
                pe.Obs([np.random.rand(length),
                        np.random.rand(length + 1)], ['t1', 't2']))
        my_array = pe.cov_Obs(1.0, 0.002, 'cov') * np.array(my_list).reshape(
            (dim, dim))
        tt = pe.linalg.matmul(
            my_array, my_array, my_array,
            my_array) - my_array @ my_array @ my_array @ my_array
        for t, e in np.ndenumerate(tt):
            assert e.is_zero(), t

        my_list = []
        length = 1000 + np.random.randint(200)
        for i in range(dim**2):
            my_list.append(
                pe.CObs(
                    pe.Obs(
                        [np.random.rand(length),
                         np.random.rand(length + 1)], ['t1', 't2']),
                    pe.Obs(
                        [np.random.rand(length),
                         np.random.rand(length + 1)], ['t1', 't2'])))
        my_array = np.array(my_list).reshape(
            (dim, dim)) * pe.cov_Obs(1.0, 0.002, 'cov')
        tt = pe.linalg.matmul(
            my_array, my_array, my_array,
            my_array) - my_array @ my_array @ my_array @ my_array
        for t, e in np.ndenumerate(tt):
            assert e.is_zero(), t
Example #2
0
def test_complex_matrix_operations():
    dimension = 4
    base_matrix = np.empty((dimension, dimension), dtype=object)
    for (n, m), entry in np.ndenumerate(base_matrix):
        exponent_real = np.random.normal(3, 5)
        exponent_imag = np.random.normal(3, 5)
        base_matrix[n, m] = pe.CObs(
            pe.pseudo_Obs(2 + 10**exponent_real, 10**(exponent_real - 1), 't'),
            pe.pseudo_Obs(2 + 10**exponent_imag, 10**(exponent_imag - 1), 't'))

    for other in [2, 2.3, (1 - 0.1j), (0 + 2.1j)]:
        ta = base_matrix * other
        tb = other * base_matrix
        diff = ta - tb
        for (i, j), entry in np.ndenumerate(diff):
            assert entry.is_zero()
        ta = base_matrix + other
        tb = other + base_matrix
        diff = ta - tb
        for (i, j), entry in np.ndenumerate(diff):
            assert entry.is_zero()
        ta = base_matrix - other
        tb = other - base_matrix
        diff = ta + tb
        for (i, j), entry in np.ndenumerate(diff):
            assert entry.is_zero()
        ta = base_matrix / other
        tb = other / base_matrix
        diff = ta * tb - 1
        for (i, j), entry in np.ndenumerate(diff):
            assert entry.is_zero()
Example #3
0
def get_complex_matrix(dimension):
    base_matrix = np.empty((dimension, dimension), dtype=object)
    for (n, m), entry in np.ndenumerate(base_matrix):
        exponent_real = np.random.normal(0, 1)
        exponent_imag = np.random.normal(0, 1)
        base_matrix[n, m] = pe.CObs(
            pe.Obs([np.random.normal(1.0, 0.1, 100)], ['t']),
            pe.Obs([np.random.normal(1.0, 0.1, 100)], ['t']))

    return base_matrix
Example #4
0
def test_b_cmatmul(benchmark):
    dim = 4
    my_list = []
    for i in range(dim**2):
        my_list.append(
            pe.CObs(pe.Obs([np.random.rand(length)], ['t1']),
                    pe.Obs([np.random.rand(length)], ['t1'])))
    my_array = np.array(my_list).reshape((dim, dim))

    benchmark(pe.linalg.matmul, my_array, my_array)
Example #5
0
def test_cobs():
    obs1 = pe.pseudo_Obs(1.0, 0.1, 't')
    obs2 = pe.pseudo_Obs(-0.2, 0.03, 't')

    my_cobs = pe.CObs(obs1, obs2)
    my_cobs == my_cobs
    str(my_cobs)
    repr(my_cobs)
    assert not (my_cobs + my_cobs.conjugate()).real.is_zero()
    assert (my_cobs + my_cobs.conjugate()).imag.is_zero()
    assert (my_cobs - my_cobs.conjugate()).real.is_zero()
    assert not (my_cobs - my_cobs.conjugate()).imag.is_zero()
    np.abs(my_cobs)

    assert (my_cobs * my_cobs / my_cobs - my_cobs).is_zero()
    assert (my_cobs + my_cobs - 2 * my_cobs).is_zero()

    fs = [[lambda x: x[0] + x[1], lambda x: x[1] + x[0]],
          [lambda x: x[0] * x[1], lambda x: x[1] * x[0]]]
    for other in [
            3, 1.1, (1.1 - 0.2j), (2.3 + 0j), (0.0 + 7.7j),
            pe.CObs(obs1),
            pe.CObs(obs1, obs2)
    ]:
        for funcs in fs:
            ta = funcs[0]([my_cobs, other])
            tb = funcs[1]([my_cobs, other])
            diff = ta - tb
            assert diff.is_zero()

        ta = my_cobs - other
        tb = other - my_cobs
        diff = ta + tb
        assert diff.is_zero()

        ta = my_cobs / other
        tb = other / my_cobs
        diff = ta * tb - 1
        assert diff.is_zero()

        assert (my_cobs / other * other - my_cobs).is_zero()
        assert (other / my_cobs * my_cobs - other).is_zero()
Example #6
0
def test_cobs_overloading():
    obs = pe.pseudo_Obs(1.1, 0.1, 't')
    cobs = pe.CObs(obs, obs)

    cobs + obs
    obs + cobs

    cobs - obs
    obs - cobs

    cobs * obs
    obs * cobs

    cobs / obs
    obs / cobs
Example #7
0
def test_matmul():
    for dim in [4, 6]:
        for const in [
                1,
                pe.cov_Obs([1.0, 1.0], [[0.001, 0.0001], [0.0001, 0.002]],
                           'norm')[1]
        ]:
            my_list = []
            length = 100 + np.random.randint(200)
            for i in range(dim**2):
                my_list.append(
                    pe.Obs(
                        [np.random.rand(length),
                         np.random.rand(length + 1)], ['t1', 't2']))
            my_array = const * np.array(my_list).reshape((dim, dim))
            tt = pe.linalg.matmul(my_array, my_array) - my_array @ my_array
            for t, e in np.ndenumerate(tt):
                assert e.is_zero(), t

            my_list = []
            length = 100 + np.random.randint(200)
            for i in range(dim**2):
                my_list.append(
                    pe.CObs(
                        pe.Obs([
                            np.random.rand(length),
                            np.random.rand(length + 1)
                        ], ['t1', 't2']),
                        pe.Obs([
                            np.random.rand(length),
                            np.random.rand(length + 1)
                        ], ['t1', 't2'])))
            my_array = np.array(my_list).reshape((dim, dim)) * const
            tt = pe.linalg.matmul(my_array, my_array) - my_array @ my_array
            for t, e in np.ndenumerate(tt):
                assert e.is_zero(), t
Example #8
0
def test_complex_matrix_inverse():
    dimension = 4
    base_matrix = np.empty((dimension, dimension), dtype=object)
    matrix = np.empty((dimension, dimension), dtype=complex)
    for (n, m), entry in np.ndenumerate(base_matrix):
        exponent_real = np.random.normal(2, 3)
        exponent_imag = np.random.normal(2, 3)
        base_matrix[n, m] = pe.CObs(
            pe.pseudo_Obs(2 + 10**exponent_real, 10**(exponent_real - 1), 't'),
            pe.pseudo_Obs(2 + 10**exponent_imag, 10**(exponent_imag - 1), 't'))

    # Construct invertible matrix
    obs_matrix = np.identity(dimension) + base_matrix @ base_matrix.T

    for (n, m), entry in np.ndenumerate(obs_matrix):
        matrix[n, m] = entry.real.value + 1j * entry.imag.value

    inverse_matrix = np.linalg.inv(matrix)
    inverse_obs_matrix = pe.linalg.inv(obs_matrix)
    for (n, m), entry in np.ndenumerate(inverse_matrix):
        assert np.isclose(inverse_matrix[n, m].real,
                          inverse_obs_matrix[n, m].real.value)
        assert np.isclose(inverse_matrix[n, m].imag,
                          inverse_obs_matrix[n, m].imag.value)
Example #9
0
def test_b_cmul(benchmark):
    my_obs = pe.CObs(pe.Obs([np.random.rand(length)], ['t1']),
                     pe.Obs([np.random.rand(length)], ['t1']))

    benchmark(mul, my_obs, my_obs)