Esempio n. 1
0
    def test_intermediate_ureal(self):
        """
        When uarray has more than one element of different types
        
        """
        x1 = uarray([ureal(2, 1), ureal(1.5, 1)])
        x2 = uarray([ureal(5, 1), ureal(.2, 1)])
        x3 = uarray([ureal(7, 1), ureal(3.2, 1)])

        x4 = result(x3**x1 * x2)
        y = log10(x4)

        dy_dx = rp.sensitivity(y, x4)
        uy_x = rp.u_component(y, x4)
        self.assertTrue(equivalent_sequence(dy_dx, uy_x / x4.u))

        x4 = result(x3**x1 * x2, label='x4')
        y = log10(x4)

        for i, x4_i in enumerate(x4):
            self.assertEqual('x4[{0}]'.format(i), x4_i.label)

        dy_dx = rp.sensitivity(y, x4)
        uy_x = rp.u_component(y, x4)
        self.assertTrue(equivalent_sequence(dy_dx, uy_x / x4.u))

        label = ["x4[{}]".format(i) for i in range(x4.size)]
        x4 = result(x3**x1 * x2, label=label)
        y = log10(x4)

        for i, x4_i in enumerate(x4):
            self.assertEqual('x4[{0}]'.format(i), x4_i.label)
Esempio n. 2
0
    def test_intermediate_ucomplex(self):
        """
        When uarray has more than one element of different types
        
        """
        z1 = uarray([ucomplex(1 + 2j, 1), ucomplex(1.5 + 2.1j, 1)])
        z2 = uarray([ucomplex(2 - 1.5j, 1), ucomplex(.5 - 0.5j, 1)])
        z3 = uarray([ucomplex(.1 + 1.8j, 1), ucomplex(.4 + 1.1j, 1)])

        z4 = result(z1**z2 * z3)
        z = log(z4)

        for z_i, z4_i in zip(z, z4):

            uz_i_z4_i = rp.u_component(z_i, z4_i)
            dz_i_dz4_i = rp.sensitivity(z_i, z4_i)

            self.assertTrue(
                equivalent(dz_i_dz4_i.rr, uz_i_z4_i.rr / z4_i.real.u))
            self.assertTrue(
                equivalent(dz_i_dz4_i.ir, uz_i_z4_i.ir / z4_i.real.u))
            self.assertTrue(
                equivalent(dz_i_dz4_i.ri, uz_i_z4_i.ri / z4_i.imag.u))
            self.assertTrue(
                equivalent(dz_i_dz4_i.ii, uz_i_z4_i.ii / z4_i.imag.u))

        z4 = result(z1**z2 * z3, label='z4')
        z = log(z4)

        for i, z4_i in enumerate(z4):
            self.assertEqual('z4[{0}]'.format(i), z4_i.label)
            self.assertEqual('z4[{0}]_re'.format(i), z4_i.real.label)
            self.assertEqual('z4[{0}]_im'.format(i), z4_i.imag.label)

        for z_i, z4_i in zip(z, z4):

            uz_i_z4_i = rp.u_component(z_i, z4_i)
            dz_i_dz4_i = rp.sensitivity(z_i, z4_i)

            self.assertTrue(
                equivalent(dz_i_dz4_i.rr, uz_i_z4_i.rr / z4_i.real.u))
            self.assertTrue(
                equivalent(dz_i_dz4_i.ir, uz_i_z4_i.ir / z4_i.real.u))
            self.assertTrue(
                equivalent(dz_i_dz4_i.ri, uz_i_z4_i.ri / z4_i.imag.u))
            self.assertTrue(
                equivalent(dz_i_dz4_i.ii, uz_i_z4_i.ii / z4_i.imag.u))

        label = ["z4[{}]".format(i) for i in range(z4.size)]
        z4 = result(z1**z2 * z3, label=label)
        z = log(z4)

        for i, z4_i in enumerate(z4):
            self.assertEqual('z4[{0}]'.format(i), z4_i.label)
            self.assertEqual('z4[{0}]_re'.format(i), z4_i.real.label)
            self.assertEqual('z4[{0}]_im'.format(i), z4_i.imag.label)
Esempio n. 3
0
    def setUp(self):
        self.x = [ureal(23, 2), ureal(26, 1), ureal(20, 3), ureal(25, 1), ureal(28, 2), ureal(24, 2)]
        self.y = [ureal(25, 2), ureal(24, 4), ureal(23, 1), ureal(25, 2), ureal(19, 6), ureal(27, 1)]
        self.xa = uarray(self.x)
        self.ya = uarray(self.y)

        self.xc = [ucomplex(23+2j, 2), ucomplex(26+1j, 1), ucomplex(20+0j, 3),
                   ucomplex(25+3j, 1), ucomplex(22+7j, 2), ucomplex(23+3j, 2)]
        self.yc = [ucomplex(25+1j, 2), ucomplex(24+4j, 4), ucomplex(23+2j, 1),
                   ucomplex(25+1j, 2), ucomplex(26+2j, 1), ucomplex(18+8j, 5)]
        self.xca = uarray(self.xc)
        self.yca = uarray(self.yc)
Esempio n. 4
0
    def test_uarray_ucomplex(self):
        """
        When uarray has more than one element 
        
        """
        x = uarray([
            ucomplex(1 + 2j, 1),
            ucomplex(2 - 1.5j, 1),
            ucomplex(.1 + 1.8j, 1)
        ])

        z = sin(x)

        dz_dx = rp.sensitivity(z, x)
        uz_x = rp.u_component(z, x)
        for dz_dx_i, uz_x_i in zip(dz_dx, uz_x):
            self.assertTrue(equivalent_sequence(dz_dx_i, uz_x_i))

        # Do the differentiation by hand
        dx_dx_calc = cos(value(x))
        for dz_dx_i, uz_x_i in zip(dz_dx, uz_x):
            self.assertTrue(equivalent_sequence(dz_dx_i, uz_x_i))
Esempio n. 5
0
    def test_uarray_ureal(self):
        """
        When uarray has more than one element 
        
        """
        x = uarray([ureal(2, 1), ureal(5, 1), ureal(7, 1)])

        y = sin(x)

        dy_dx = rp.sensitivity(y, x)
        uy_x = rp.u_component(y, x)

        self.assertTrue(isinstance(dy_dx, UncertainArray))
        self.assertTrue(isinstance(uy_x, UncertainArray))

        for dy_dx_i, uy_x_i in zip(dy_dx, uy_x):
            self.assertTrue(equivalent(dy_dx_i, uy_x_i))

        # Do the differentiation by hand
        dy_dx_calc = cos(value(x))
        for dy_dx_i, dy_dx_calc_i in zip(dy_dx, dy_dx_calc):
            self.assertTrue(equivalent(dy_dx_i, dy_dx_calc_i))
Esempio n. 6
0
    def test_uarray_mixed(self):
        """
        When uarray has more than one element of different types
        
        """
        x = uarray([ureal(2, 1), ucomplex(2 - 1.5j, 1)])
        y = sin(x)

        dy_dx = rp.sensitivity(y, x)
        uy_x = rp.u_component(y, x)

        self.assertTrue(isinstance(dy_dx, UncertainArray))
        self.assertTrue(isinstance(uy_x, UncertainArray))

        self.assertTrue(equivalent(dy_dx[0], uy_x[0]))
        self.assertTrue(equivalent_sequence(dy_dx[1], uy_x[1]))

        # Do the differentiation by hand
        dy_dx_calc = cos(value(x))
        self.assertTrue(equivalent(dy_dx[0], dy_dx_calc[0]))
        self.assertTrue(equivalent(dy_dx[0], dy_dx_calc[0]))
        self.assertTrue(
            equivalent_sequence(dy_dx[1], fn.complex_to_seq(dy_dx_calc[1])))
Esempio n. 7
0
def run():
    m = [[ureal(5, 1), ureal(-1, 0.3),
          ureal(3, 1.3)], [ureal(1, 0.1),
                           ureal(2, 0.8),
                           ureal(-3, 1)],
         [ureal(-1, 0.5), ureal(2, 1.1),
          ureal(4, 0.3)]]
    b = [ureal(1, 0.2), ureal(2, 1.1), ureal(3, 0.4)]

    ma = uarray(m)
    ba = uarray(b)

    # vector * vector

    z = b[0] * 1 + b[1] * 2 + b[2] * 3
    za = ba @ [1, 2, 3]
    assert equivalent(z.x, za.value())
    assert equivalent(z.u, za.uncertainty())

    # switch lhs and rhs
    z = 1 * b[0] + 2 * b[1] + 3 * b[2]
    za = [1, 2, 3] @ ba
    assert equivalent(z.x, za.value())
    assert equivalent(z.u, za.uncertainty())

    try:
        ba @ [1, 2]
    except ValueError:  # Expect this error -> shapes (3,) and (2,) not aligned: 3 (dim 0) != 2 (dim 0)
        pass
    else:
        raise ValueError('this should not work -> ba @ [1, 2]')

    # vector * matrix

    z = [
        1 * m[0][0] + 2 * m[1][0] + 3 * m[2][0],
        1 * m[0][1] + 2 * m[1][1] + 3 * m[2][1],
        1 * m[0][2] + 2 * m[1][2] + 3 * m[2][2]
    ]
    za = [1, 2, 3] @ ma
    for i in range(3):
        assert equivalent(z[i].x, za[i].x)
        assert equivalent(z[i].u, za[i].u)

    try:
        [1, 2] @ ma
    except ValueError:  # Expect this error -> shapes (2,) and (3,3) not aligned: 2 (dim 0) != 3 (dim 0)
        pass
    else:
        raise ValueError('this should not work -> [1, 2] @ ma')

    # matrix * vector

    z = [
        m[0][0] * b[0] + m[0][1] * b[1] + m[0][2] * b[2],
        m[1][0] * b[0] + m[1][1] * b[1] + m[1][2] * b[2],
        m[2][0] * b[0] + m[2][1] * b[1] + m[2][2] * b[2]
    ]

    za = ma @ ba
    for i in range(3):
        assert equivalent(z[i].x, za[i].x)
        assert equivalent(z[i].u, za[i].u)

    try:
        ma @ np.arange(4)
    except ValueError:  # Expect this error -> shapes (3,3) and (4,) not aligned: 3 (dim 1) != 4 (dim 0)
        pass
    else:
        raise ValueError('this should not work -> ma @ np.arange(4)')

    # matrix * matrix

    na = np.arange(10 * 10).reshape(10, 10) * -3.1
    nb = np.arange(10 * 10).reshape(10, 10) * 2.3
    nc = na @ nb

    ua = uarray(na.copy() * ureal(1, 0))
    ub = uarray(nb.copy() * ureal(1, 0))
    uc = ua @ ub
    assert nc.shape == uc.shape

    i, j = nc.shape
    for ii in range(i):
        for jj in range(j):
            assert equivalent(na[ii, jj], ua[ii, jj].x)
            assert equivalent(nb[ii, jj], ub[ii, jj].x)
            assert equivalent(nc[ii, jj], uc[ii, jj].x, tol=1e-10)

    # switch the ndarray and uarray order and also use a regular Python list
    for mix in [na @ ub, ua @ nb, na.tolist() @ ub, ua @ nb.tolist()]:
        assert mix.shape == nc.shape
        i, j = mix.shape
        for ii in range(i):
            for jj in range(j):
                assert equivalent(mix[ii, jj].x, nc[ii, jj], tol=1e-10)

    try:
        ma @ np.arange(4 * 4).reshape(4, 4)
    except ValueError:  # Expect this error -> shapes (3,3) and (4,4) not aligned: 3 (dim 1) != 4 (dim 0)
        pass
    else:
        raise ValueError(
            'this should not work -> ma @ np.arange(4*4).reshape(4,4)')

    # test a bunch of different dimensions
    test_dims = [
        #[(), ()],
        [(0, ), (1, 3)],
        [(1, ), (1, 3)],
        [(4, ), (4, 3)],
        [(2, 4), (4, )],
        [(2, 4), (3, )],
        [(2, 4), (3, 2)],
        [(2, 4), (4, 2)],
        [(1, 2, 4), (1, 4, 2)],
        [(2, 2, 4), (1, 4, 2)],
        [(1, 2, 4), (2, 4, 2)],
        [(2, 2, 4), (2, 4, 2)],
        [(3, 2, 4), (3, 4, 2)],
        [(6, 2, 4), (3, 2, 2)],
        [(6, 2, 4), (3, 4, 8)],
        [(6, 2, 4), (6, 4, 8)],
        [(5, 3, 2, 4), (5, 3, 4, 2)],
        [(3, 2, 2, 4), (3, 9, 4, 2)],
        [(8, 3, 1, 2, 4), (8, 3, 9, 4, 2)],
    ]

    for s1, s2 in test_dims:
        na = np.arange(int(np.prod(np.array(s1)))).reshape(s1)
        nb = np.arange(int(np.prod(np.array(s2)))).reshape(s2)
        try:
            nc = na @ nb
        except:
            nc = None

        ua = uarray(na.copy() * ureal(1, 0))
        ub = uarray(nb.copy() * ureal(1, 0))
        try:
            uc = ua @ ub
        except:
            if nc is not None:
                raise AssertionError(
                    'The regular @ PASSED, the custom-written @ FAILED')
        else:
            if nc is None:
                raise AssertionError(
                    'The regular @ FAILED, the custom-written @ PASSED')
            assert np.array_equal(
                nc, uc), 'The arrays are not equal\n{}\n{}'.format(nc, uc)
Esempio n. 8
0
    def test_sum_ndarray(self):
        # 1D array
        xlist = [ureal(i, i*0.1) for i in range(100)]
        xarray = uarray(xlist)
        self.assertTrue(xarray.shape == (100,))

        b = 0
        for x in xlist:
            b += x
        for a in [function.sum(xarray)]:
            self.assertTrue(equivalent(value(a), b.x))
            self.assertTrue(equivalent(uncertainty(a), b.u))

        # 3D array
        xlist = [[[ureal(i*j*k, i*j*k*0.1) for k in range(1, 5)] for j in range(7, 10)] for i in range(3, 9)]
        xarray = uarray(xlist)
        self.assertTrue(xarray.shape == (6, 3, 4))

        axis_none = 0.0
        axis_0 = [[0.0 for i in range(4)] for j in range(3)]
        axis_1 = [[0.0 for i in range(4)] for j in range(6)]
        axis_2 = [[0.0 for i in range(3)] for j in range(6)]
        for i in range(6):
            for j in range(3):
                for k in range(4):
                    _value = xlist[i][j][k]
                    axis_none += _value
                    axis_0[j][k] += _value
                    axis_1[i][k] += _value
                    axis_2[i][j] += _value

        # axis=None
        for a in [function.sum(xarray)]:
            self.assertTrue(equivalent(value(a), axis_none.x))
            self.assertTrue(equivalent(uncertainty(a), axis_none.u))

        # axis=0
        m, n = len(axis_0), len(axis_0[0])
        for a in [function.sum(xarray, axis=0)]:
            self.assertTrue(a.shape == (m, n))
            for j in range(m):
                for k in range(n):
                    self.assertTrue(equivalent(a[j, k].x, axis_0[j][k].x))
                    self.assertTrue(equivalent(a[j, k].u, axis_0[j][k].u))

        # axis=1
        m, n = len(axis_1), len(axis_1[0])
        for a in [function.sum(xarray, axis=1)]:
            self.assertTrue(a.shape == (m, n))
            for i in range(m):
                for k in range(n):
                    self.assertTrue(equivalent(a[i, k].x, axis_1[i][k].x))
                    self.assertTrue(equivalent(a[i, k].u, axis_1[i][k].u))

        # axis=2
        m, n = len(axis_2), len(axis_2[0])
        for a in [function.sum(xarray, axis=2)]:
            self.assertTrue(a.shape == (m, n))
            for i in range(m):
                for j in range(n):
                    self.assertTrue(equivalent(a[i, j].x, axis_2[i][j].x))
                    self.assertTrue(equivalent(a[i, j].u, axis_2[i][j].u))