示例#1
0
def make_polynomial():
    q0 = chaospy.variable()
    poly = 1.2*q0*(q0-1.8)*(q0+1.8)
    t = numpy.linspace(-2, 2, 100)

    t0 = numpy.linspace(-2, 0, 100)
    pyplot.fill_between(t0, 0, poly(t0), alpha=0.3, color=COLOR1)
    pyplot.plot(t0, poly(t0), COLOR1, lw=4)

    t0 = numpy.linspace(0, 2, 100)
    pyplot.fill_between(t0, poly(t0), 0, alpha=0.3, color=COLOR2)
    pyplot.plot(t0, poly(t0), COLOR2, lw=4)

    save("polynomial")
示例#2
0
def make_orthogonality():

    t = numpy.linspace(-2, 2, 200)
    q0 = chaospy.variable()
    poly1 = (q0-1.2)*(q0+1.2)
    poly2 = -(q0-1.2)*(q0+1.2)

    t0 = numpy.linspace(-2, -1.2)
    pyplot.fill_between(t0, poly1(t0), poly2(t0), color=COLOR1, alpha=0.3)
    t0 = numpy.linspace(1.2, 2)
    pyplot.fill_between(t0, poly1(t0), poly2(t0), color=COLOR1, alpha=0.3)
    pyplot.plot(t, poly1(t), COLOR1, lw=4)
    t0 = numpy.linspace(-1.2, 1.2)
    pyplot.fill_between(t, poly1(t), poly2(t), color=COLOR2, alpha=0.3)
    pyplot.plot(t, poly2(t), COLOR2, lw=4)

    save("orthogonality")
def galerkin_approx(coordinates, joint, expansion_small, norms_small):
    alpha, beta = chaospy.variable(2)

    e_alpha_phi = chaospy.E(alpha*expansion_small, joint)
    initial_condition = e_alpha_phi/norms_small

    phi_phi = chaospy.outer(expansion_small, expansion_small)
    e_beta_phi_phi = chaospy.E(beta*phi_phi, joint)

    def right_hand_side(c, t):
        return -numpy.sum(c*e_beta_phi_phi, -1)/norms_small

    coefficients = odeint(
        func=right_hand_side,
        y0=initial_condition,
        t=coordinates,
    )
    return chaospy.sum(expansion_small*coefficients, -1)
示例#4
0
        def create_PCE_custom(self, uncertain_parameters=None):
            data = Data()

            q0, q1 = cp.variable(2)
            parameter_space = [cp.Uniform(), cp.Uniform()]
            distribution = cp.J(*parameter_space)

            data.uncertain_parameters = ["a", "b"]

            data.test_value = "custom PCE method"
            data.add_features(
                ["TestingModel1d", "feature0d", "feature1d", "feature2d"])

            U_hat = {}
            U_hat["TestingModel1d"] = cp.Poly([q0, q1 * q0, q1])
            U_hat["feature0d"] = cp.Poly([q0, q1 * q0, q1])
            U_hat["feature1d"] = cp.Poly([q0, q1 * q0, q1])
            U_hat["feature2d"] = cp.Poly([q0, q1 * q0, q1])

            return U_hat, distribution, data
示例#5
0
def test_poly_substitute_2():
    x, y = xy = cp.variable(2)
    assert xy(x*y+1)[0] == x*y+1
示例#6
0
def test_poly_variable():
    XYZ = cp.variable(3)
    assert len(XYZ) == 3
示例#7
0
def test_poly_indirect_evals():
    basis = cp.basis(1, 2, 2)
    x, y = cp.variable(2)
    assert basis(q1=3)[1] == 3
    assert basis(q1=3)[3] == 3*x
示例#8
0
def test_poly_power():
    X, Y = cp.variable(2)
    out = X**(1, 0, 2, 1, 0)*Y**(0, 1, 0, 1, 2)
    assert str(out) == "[q0, q1, q0^2, q0q1, q1^2]"
示例#9
0
def test_poly_matrix():
    XYZ = cp.variable(3)
    XYYZ = cp.Poly([XYZ[:2], XYZ[1:]])
    assert str(XYYZ) == "[[q0, q1], [q1, q2]]"
    assert XYYZ.shape == (2, 2)
示例#10
0
def test_poly_representation():
    XYZ = cp.variable(3)
    assert str(XYZ) == "[q0, q1, q2]"
示例#11
0
def test_poly_arithmatic():
    xyz = x, y, z = cp.variable(3)
    assert xyz[0] * .5 + y - x / 2. == y
示例#12
0
def test_poly_linearcomb():

    x, y = xy = cp.variable(2)
    mul1 = xy * np.eye(2)
    mul2 = cp.Poly([[x, 0], [0, y]])
    assert np.all(mul1 == mul2)
示例#13
0
def test_operator_E():
    dist = cp.Normal(0, 1)
    res = np.array([1, 0, 1, 0, 3])
    x = cp.variable()
    poly = x**range(5)
    assert np.allclose(cp.E(poly, dist), res)
示例#14
0
def test_operator_E():
    dist = cp.Normal(0, 1)
    res = np.array([1, 0, 1, 0, 3])
    x = cp.variable()
    poly = x**np.arange(5)
    assert np.allclose(cp.E(poly, dist), res)
示例#15
0
def test_poly_power():
    X, Y = cp.variable(2)
    out = X**(1, 0, 2, 1, 0) * Y**(0, 1, 0, 1, 2)
    assert str(out) == "[q0 q1 q0**2 q0*q1 q1**2]"
示例#16
0
def test_poly_matrix():
    XYZ = cp.variable(3)
    XYYZ = cp.polynomial([XYZ[:2], XYZ[1:]])
    assert str(XYYZ) == "[[q0 q1]\n [q1 q2]]"
    assert XYYZ.shape == (2, 2)
示例#17
0
def test_poly_composit():
    X, Y, Z = cp.variable(3)
    ZYX = cp.polynomial([Z, Y, X])
    assert str(ZYX) == "[q2 q1 q0]"
示例#18
0
import chaospy as cp
import numpy as np
import odespy

#Intrusive Galerkin method

dist_a = cp.Uniform(0, 0.1)
dist_I = cp.Uniform(8, 10)
dist = cp.J(dist_a, dist_I)  # joint multivariate dist

P, norms = cp.orth_ttr(2, dist, retall=True)
variable_a, variable_I = cp.variable(2)

PP = cp.outer(P, P)
E_aPP = cp.E(variable_a * PP, dist)
E_IP = cp.E(variable_I * P, dist)


def right_hand_side(c, x):  # c' = right_hand_side(c, x)
    return -np.dot(E_aPP, c) / norms  # -M*c


initial_condition = E_IP / norms
solver = odespy.RK4(right_hand_side)
solver.set_initial_condition(initial_condition)
x = np.linspace(0, 10, 1000)
c = solver.solve(x)[0]
u_hat = cp.dot(P, c)

#Rosenblat transformation using point collocation
示例#19
0
# :math:
# u' = -a*u
# d/dx sum(c*P) = -a*sum(c*P)
# <d/dx sum(c*P),P[k]> = <-a*sum(c*P), P[k]>
# d/dx c[k]*<P[k],P[k]> = -sum(c*<a*P,P[k]>)
# d/dx c = -E( outer(a*P,P) ) / E( P*P )
#
# u(0) = I
# <sum(c(0)*P), P[k]> = <I, P[k]>
# c[k](0) <P[k],P[k]> = <I, P[k]>
# c(0) = E( I*P ) / E( P*P )
order = 5
P, norm = cp.orth_ttr(order, dist, retall=True, normed=True)

# support structures
q0, q1 = cp.variable(2)
P_nk = cp.outer(P, P)
E_ank = cp.E(q0*P_nk, dist)
E_ik = cp.E(q1*P, dist)
sE_ank = cp.sum(E_ank, 0)

# Right hand side of the ODE
def f(c_k, x):
    return -cp.sum(c_k*E_ank, -1)/norm

solver = odespy.RK4(f)
c_0 = E_ik/norm
solver.set_initial_condition(c_0)
c_n, x = solver.solve(x)
U_hat = cp.sum(P*c_n, -1)
示例#20
0
def test_poly_substitute_2():
    x, y = xy = cp.variable(2)
    assert xy(x * y + 1)[0] == x * y + 1
示例#21
0
def test_poly_representation():
    XYZ = cp.variable(3)
    assert str(XYZ) == "[q0, q1, q2]"
示例#22
0
def test_poly_linearcomb():

    x, y = xy = cp.variable(2)
    mul1 = xy * np.eye(2)
    mul2 = cp.Poly([[x, 0], [0, y]])
    assert np.all(mul1 == mul2)
示例#23
0
def test_poly_composit():
    X, Y, Z = cp.variable(3)
    ZYX = cp.Poly([Z, Y, X])
    assert str(ZYX) == "[q2, q1, q0]"
示例#24
0
def test_poly_composit():
    X, Y, Z = cp.variable(3)
    ZYX = cp.Poly([Z, Y, X])
    assert str(ZYX) == "[q2, q1, q0]"
示例#25
0
def test_poly_matrix():
    XYZ = cp.variable(3)
    XYYZ = cp.Poly([XYZ[:2], XYZ[1:]])
    assert str(XYYZ) == "[[q0, q1], [q1, q2]]"
    assert XYYZ.shape == (2, 2)
示例#26
0
def test_poly_dimredux():
    basis = cp.basis(1, 2, 2)
    X = cp.variable()
    assert basis[0] == X
示例#27
0
def test_poly_dimredux():
    basis = cp.basis(1, 2, 2)
    X = cp.variable()
    assert basis[0] == X
示例#28
0
def test_poly_evals():
    xy = x, y = cp.variable(2)
    assert xy(0.5)[0] == 0.5
    assert xy(0.5)[1] == y
示例#29
0
def test_poly_power():
    X, Y = cp.variable(2)
    out = X**(1, 0, 2, 1, 0) * Y**(0, 1, 0, 1, 2)
    assert str(out) == "[q0, q1, q0^2, q0q1, q1^2]"
示例#30
0
def test_poly_eval_array():
    xy = cp.variable(2)
    out = xy([1, 2], 3)
    ref = np.array([[1, 2], [3, 3]])
    assert np.allclose(out, ref)
示例#31
0
def test_poly_evals():
    xy = x, y = cp.variable(2)
    assert xy(0.5)[0] == 0.5
    assert xy(0.5)[1] == y
示例#32
0
def test_poly_substitute():
    x, y = xy = cp.variable(2)
    assert xy(q1=x)[1] == x
示例#33
0
def test_poly_indirect_evals():
    basis = cp.basis(1, 2, 2)
    x, y = cp.variable(2)
    assert basis(q1=3)[1] == 3
    assert basis(q1=3)[3] == 3 * x
示例#34
0
def test_poly_arithmatic():
    xyz = x, y, z = cp.variable(3)
    assert xyz[0]*.5+y-x/2. == y
示例#35
0
def test_poly_eval_array():
    xy = cp.variable(2)
    out = xy([1, 2], 3)
    ref = np.array([[1, 2], [3, 3]])
    assert np.allclose(out, ref)
示例#36
0
def test_poly_variable():
    XYZ = cp.variable(3)
    assert len(XYZ) == 3
示例#37
0
def test_poly_substitute():
    x, y = xy = cp.variable(2)
    assert xy(q1=x)[1] == x
示例#38
0
import chaospy as cp
import numpy as np
import odespy


#Intrusive Galerkin method

dist_a = cp.Uniform(0, 0.1)
dist_I = cp.Uniform(8, 10)
dist = cp.J(dist_a, dist_I) # joint multivariate dist

P, norms = cp.orth_ttr(2, dist, retall=True)
variable_a, variable_I = cp.variable(2)

PP = cp.outer(P, P)
E_aPP = cp.E(variable_a*PP, dist)
E_IP = cp.E(variable_I*P, dist)

def right_hand_side(c, x):            # c' = right_hand_side(c, x)
    return -np.dot(E_aPP, c)/norms    # -M*c

initial_condition = E_IP/norms
solver = odespy.RK4(right_hand_side)
solver.set_initial_condition(initial_condition)
x = np.linspace(0, 10, 1000)
c = solver.solve(x)[0]
u_hat = cp.dot(P, c)



#Rosenblat transformation using point collocation
# :math:
# u' = -a*u
# d/dx sum(c*P) = -a*sum(c*P)
# <d/dx sum(c*P),P[k]> = <-a*sum(c*P), P[k]>
# d/dx c[k]*<P[k],P[k]> = -sum(c*<a*P,P[k]>)
# d/dx c = -E( outer(a*P,P) ) / E( P*P )
#
# u(0) = I
# <sum(c(0)*P), P[k]> = <I, P[k]>
# c[k](0) <P[k],P[k]> = <I, P[k]>
# c(0) = E( I*P ) / E( P*P )
order = 5
P, norm = cp.orth_ttr(order, dist, retall=True, normed=True)

# support structures
q0, q1 = cp.variable(2)
P_nk = cp.outer(P, P)
E_ank = cp.E(q0 * P_nk, dist)
E_ik = cp.E(q1 * P, dist)
sE_ank = cp.sum(E_ank, 0)


# Right hand side of the ODE
def f(c_k, x):
    return -cp.sum(c_k * E_ank, -1) / norm


solver = odespy.RK4(f)
c_0 = E_ik / norm
solver.set_initial_condition(c_0)
c_n, x = solver.solve(x)