Ejemplo n.º 1
0
def test_nsolve():
    # onedimensional
    x = Symbol('x')
    assert nsolve(sin(x), 2) - pi.evalf() < 1e-15
    assert nsolve(Eq(2 * x, 2), x, -10) == nsolve(2 * x - 2, -10)
    # Testing checks on number of inputs
    raises(TypeError, lambda: nsolve(Eq(2 * x, 2)))
    raises(TypeError, lambda: nsolve(Eq(2 * x, 2), x, 1, 2))
    # issue 4829
    assert nsolve(x**2 / (1 - x) / (1 - 2 * x)**2 - 100, x, 0)  # doesn't fail
    # multidimensional
    x1 = Symbol('x1')
    x2 = Symbol('x2')
    f1 = 3 * x1**2 - 2 * x2**2 - 1
    f2 = x1**2 - 2 * x1 + x2**2 + 2 * x2 - 8
    f = Matrix((f1, f2)).T
    F = lambdify((x1, x2), f.T, modules='mpmath')
    for x0 in [(-1, 1), (1, -2), (4, 4), (-4, -4)]:
        x = nsolve(f, (x1, x2), x0, tol=1.e-8)
        assert mnorm(F(*x), 1) <= 1.e-10
    # The Chinese mathematician Zhu Shijie was the very first to solve this
    # nonlinear system 700 years ago (z was added to make it 3-dimensional)
    x = Symbol('x')
    y = Symbol('y')
    z = Symbol('z')
    f1 = -x + 2 * y
    f2 = (x**2 + x * (y**2 - 2) - 4 * y) / (x + 4)
    f3 = sqrt(x**2 + y**2) * z
    f = Matrix((f1, f2, f3)).T
    F = lambdify((x, y, z), f.T, modules='mpmath')

    def getroot(x0):
        root = nsolve(f, (x, y, z), x0)
        assert mnorm(F(*root), 1) <= 1.e-8
        return root

    assert list(map(round, getroot((1, 1, 1)))) == [2.0, 1.0, 0.0]
    assert nsolve([Eq(f1), Eq(f2), Eq(f3)], [x, y, z],
                  (1, 1, 1))  # just see that it works
    a = Symbol('a')
    assert abs(
        nsolve(1 / (0.001 + a)**3 - 6 /
               (0.9 - a)**3, a, 0.3) - mpf('0.31883011387318591')) < 1e-15
Ejemplo n.º 2
0
def test_nsolve():
    # onedimensional
    x = Symbol('x')
    assert nsolve(sin(x), 2) - pi.evalf() < 1e-15
    assert nsolve(Eq(2*x, 2), x, -10) == nsolve(2*x - 2, -10)
    # Testing checks on number of inputs
    raises(TypeError, lambda: nsolve(Eq(2*x, 2)))
    raises(TypeError, lambda: nsolve(Eq(2*x, 2), x, 1, 2))
    # issue 4829
    assert nsolve(x**2/(1 - x)/(1 - 2*x)**2 - 100, x, 0)  # doesn't fail
    # multidimensional
    x1 = Symbol('x1')
    x2 = Symbol('x2')
    f1 = 3 * x1**2 - 2 * x2**2 - 1
    f2 = x1**2 - 2 * x1 + x2**2 + 2 * x2 - 8
    f = Matrix((f1, f2)).T
    F = lambdify((x1, x2), f.T, modules='mpmath')
    for x0 in [(-1, 1), (1, -2), (4, 4), (-4, -4)]:
        x = nsolve(f, (x1, x2), x0, tol=1.e-8)
        assert mnorm(F(*x), 1) <= 1.e-10
    # The Chinese mathematician Zhu Shijie was the very first to solve this
    # nonlinear system 700 years ago (z was added to make it 3-dimensional)
    x = Symbol('x')
    y = Symbol('y')
    z = Symbol('z')
    f1 = -x + 2*y
    f2 = (x**2 + x*(y**2 - 2) - 4*y) / (x + 4)
    f3 = sqrt(x**2 + y**2)*z
    f = Matrix((f1, f2, f3)).T
    F = lambdify((x, y, z), f.T, modules='mpmath')

    def getroot(x0):
        root = nsolve(f, (x, y, z), x0)
        assert mnorm(F(*root), 1) <= 1.e-8
        return root
    assert list(map(round, getroot((1, 1, 1)))) == [2.0, 1.0, 0.0]
    assert nsolve([Eq(
        f1), Eq(f2), Eq(f3)], [x, y, z], (1, 1, 1))  # just see that it works
    a = Symbol('a')
    assert nsolve(1/(0.001 + a)**3 - 6/(0.9 - a)**3, a, 0.3).ae(
        mpf('0.31883011387318591'))
def cond(_matrix):
    return mnorm(_matrix, 1)
Ejemplo n.º 4
0
 def getroot(x0):
     root = nsolve(f, (x, y, z), x0)
     assert mnorm(F(*root), 1) <= 1.e-8
     return root
Ejemplo n.º 5
0
 def getroot(x0):
     root = nsolve(f, (x, y, z), x0)
     assert mnorm(F(*root), 1) <= 1.e-8
     return root
Ejemplo n.º 6
0
    ],
    'agm': [
        'primitive',
        [lambda x, y: mp.agm(x) if y is None else mp.agm(x, y[0]), None]
    ],
    #
    'matrix': [
        'primitive',
        [
            lambda x, y: mp.matrix(x) if isa(x, Vector) else mp.matrix(x)
            if y is None else mp.matrix(x, y[0]), None
        ]
    ],
    'matrix-ref': ['primitive', [lambda x, y: x[y[0], y[1], [0]], None]],
    'matrix-set': [
        'primitive',
        [lambda x, y: matrix_set(x, y[0], y[1][0], y[1][1][0]), None]
    ],
    'zeros': ['primitive', [lambda x, y: mp.zeros(x), None]],
    'ones': ['primitive', [lambda x, y: mp.ones(x), None]],
    'eye': ['primitive', [lambda x, y: mp.eye(x), None]],
    'diag': ['primitive', [lambda x, y: mp.diag(x), None]],
    'randmatrix': ['primitive', [lambda x, y: mp.randmatrix(x), None]],
    'matrix-inv': ['primitive', [lambda x, y: x**(-1), None]],
    'norm': [
        'primitive',
        [lambda x, y: mp.norm(x) if y is None else mp.norm(x, y[0]), None]
    ],
    'mnorm': ['primitive', [lambda x, y: mp.mnorm(x, y[0]), None]],
}
Ejemplo n.º 7
0
                if i > j:
                    d_mat_b[ij_index] = float(mpfr_sqrt3 * (i + 1))
                    mp_mat_b[ij_index] = mp_sqrt3 * mpmath.mp.mpf(i + 1)
                    mpfr_mat_b[ij_index] = mpfr_sqrt3 * gmpy2.mpfr(i + 1)
                else:
                    d_mat_b[ij_index] = float(mpfr_sqrt3 * (j + 1))
                    mp_mat_b[ij_index] = mp_sqrt3 * mpmath.mp.mpf(j + 1)
                    mpfr_mat_b[ij_index] = mpfr_sqrt3 * gmpy2.mpfr(j + 1)

                mpmat_b[i, j] = mp_mat_b[ij_index]

        print('double= ', xd_norm_fro(d_mat_a, row_dim, mid_dim, d_zero),
              xd_norm_fro(d_mat_b, mid_dim, col_dim, d_zero))
        print('mp    = ', xd_norm_fro(mp_mat_a, row_dim, mid_dim, mp_zero),
              xd_norm_fro(mp_mat_b, mid_dim, col_dim, mp_zero))
        print('mpmat = ', mpmath.mnorm(mpmat_a, 'f'),
              mpmath.mnorm(mpmat_b, 'f'))
        print('mpfr  = ', xd_norm_fro(mpfr_mat_a, row_dim, mid_dim, mpfr_zero),
              xd_norm_fro(mpfr_mat_b, mid_dim, col_dim, mpfr_zero))

        # double
        start_time = time.time()
        d_mat_c = xd_mymatmul(d_mat_a, row_dim, mid_dim, d_mat_b, mid_dim,
                              col_dim, d_zero)
        end_time = time.time()
        d_matmul_time = end_time - start_time
        print('d(', 53, ')')
        # cProfile.run('xd_mymatmul(d_mat_a, row_dim, mid_dim, d_mat_b, mid_dim, col_dim, d_zero)')

        # mpmath
        start_time = time.time()