Пример #1
0
def run_task(first_vector, second_vector, sphere_props, iterations):
    # Defining variables and functions.
    variables = x, y, z = symbols('x y z', real=True)
    functions = f1, f2, f3 = \
        (x - sphere_props[0][0]) ** 2 + (y - sphere_props[0][1]) ** 2 + (z - sphere_props[0][2]) ** 2 - sphere_props[0][3] ** 2, \
        (x - sphere_props[1][0]) ** 2 + (y - sphere_props[1][1]) ** 2 + (z - sphere_props[1][2]) ** 2 - sphere_props[1][3] ** 2, \
        (x - sphere_props[2][0]) ** 2 + (y - sphere_props[2][1]) ** 2 + (z - sphere_props[2][2]) ** 2 - sphere_props[2][3] ** 2

    # Defining matrices to use.
    DF = Matrix(3, 3, lambda i, j: diff(functions[i], variables[j]))
    F = Matrix([f1, f2, f3])
    X = Matrix(first_vector)

    # Finding first point
    # Running Multivariate Newton’s Method
    for _ in range(iterations):
        X += DF.LUsolve(-F).subs([(x, X[0, 0]), (y, X[1, 0]), (z, X[2, 0])])

    point1 = X, F.subs([(x, X[0, 0]), (y, X[1, 0]), (z, X[2, 0])])

    # Finding second point
    # Running Multivariate Newton’s Method
    X = Matrix(second_vector)
    for _ in range(iterations):
        X += DF.LUsolve(-F).subs([(x, X[0, 0]), (y, X[1, 0]), (z, X[2, 0])])

    point2 = X, F.subs([(x, X[0, 0]), (y, X[1, 0]), (z, X[2, 0])])

    return point1[0], point1[1], point2[0], point2[1]
Пример #2
0
def test_LUsolve():
    A = Matrix([[2, 3, 5], [3, 6, 2], [8, 3, 6]])
    x = Matrix(3, 1, [3, 7, 5])
    b = A * x
    soln = A.LUsolve(b)
    assert soln == x
    A = Matrix([[0, -1, 2], [5, 10, 7], [8, 3, 4]])
    x = Matrix(3, 1, [-1, 2, 5])
    b = A * x
    soln = A.LUsolve(b)
    assert soln == x
Пример #3
0
    def _mat_inv_mul(self, A, B):
        """Internal Function

        Computes A^-1 * B symbolically w/ substitution, where B is not
        necessarily a vector, but can be a matrix.

        """

        # Note: investigate difficulty in only creating symbols for non-zero
        # entries; this could speed things up, perhaps?

        r1, c1 = A.shape
        r2, c2 = B.shape
        temp1 = Matrix(r1, c1, lambda i, j: Symbol('x' + str(j + r1 * i)))
        temp2 = Matrix(r2, c2, lambda i, j: Symbol('y' + str(j + r2 * i)))
        for i in range(len(temp1)):
            if A[i] == 0:
                temp1[i] = 0
        for i in range(len(temp2)):
            if B[i] == 0:
                temp2[i] = 0
        temp3 = []
        for i in range(c2):
            temp3.append(temp1.LUsolve(temp2.extract(range(r2), [i])))
        temp3 = Matrix([i.T for i in temp3]).T
        if Kane.simp == True:
            temp3.simplify()
        return temp3.subs(dict(zip(temp1, A))).subs(dict(zip(temp2, B)))
Пример #4
0
                def km(f):
                    a = [0, 1]
                    for ai in a:
                        if f(ai) == 0:
                            return Polynomial([-ai, 1], f.var)
                    ms = IZ.divisors(f(a[0]), positive=True)
                    for i in range(1, f.degree // 2 + 1):
                        ms_all = IZ.divisors(f(a[i]))
                        if i > 1:
                            ms = [(*mi[0], mi[1])
                                  for mi in it.product(ms, ms_all)]
                        else:
                            ms = list(it.product(ms, ms_all))
                        matrix = [
                            list(
                                it.accumulate([1] + [ai] * (len(a) - 1),
                                              lambda x, y: x * y)) for ai in a
                        ]
                        matrix = Matrix(matrix)
                        for m in ms:
                            b = matrix.LUsolve(Matrix(m))
                            g = Polynomial(b, f.var)
                            q, r = self._divmod_rational(f, g)
                            if b[i] != 0 and r == self.zero:
                                return g, q
                        a.append(-a[-1] if a[-1] > 0 else -a[-1] + 1)

                    return f, None
Пример #5
0
def parabola(population_data):
    a = Matrix(4, 3, lambda i, j: fill(population_data, i, j, 0))
    b = Matrix(4, 1, population_data[:, 1])

    b = a.transpose() * b
    a = a.transpose() * a

    return a.LUsolve(b)
Пример #6
0
def line(population_data, t):
    a = Matrix(4, 2, lambda i, j: fill(population_data, i, j, 0))
    b = Matrix(4, 1, [ln(pop[1]) for pop in population_data])

    b = a.transpose() * b
    a = a.transpose() * a

    exponential = a.LUsolve(b)

    return E**exponential[0, 0] * E**(exponential[1, 0] * t)
Пример #7
0
    def _solve_matrix(self, solve_nodes):
        all_eq_list = []
        remainder_list = []
        for cur_node_name in solve_nodes:
            cur_eq_list = []
            cur_expr = self.nodes[cur_node_name].expand() if not isinstance(self.nodes[cur_node_name], numbers.Number) else self.nodes[cur_node_name]
            cur_sum = 0
            for cur_coeff_name in solve_nodes:
                cur_coeff = cur_expr.coeff(self.var_list[cur_coeff_name])
                cur_eq_list += [cur_coeff]
                cur_sum += cur_coeff * self.var_list[cur_coeff_name]
            remainder = simplify(cur_sum - cur_expr)
            remainder_list += [remainder]
            all_eq_list += [cur_eq_list]

        a_matrix = Matrix(all_eq_list)
        b_matrix = Matrix(remainder_list)
        soln = a_matrix.LUsolve(b_matrix)
        return soln
Пример #8
0
from sympy import symbols, diff, Matrix

# DF(x)s=-F(x)

sym = x, y, z = symbols('x y z', real=True)

fun = f1, f2, f3 = (x - 1) ** 2 + y ** 2 + (z - 1) ** 2 - 8, \
                   x ** 2 + (y - 2) ** 2 + (z - 2) ** 2 - 2, \
                   x ** 2 + (y - 3) ** 2 + (z - 3) ** 2 - 2

DF = Matrix(3, 3, lambda i, j: diff(fun[i], sym[j]))
F = Matrix([[f1], [f2], [f3]])
xn = Matrix([[10.0], [10.0], [10.0]])

for _ in range(0, 20):
    xn += DF.LUsolve(-F).subs([(x, xn[0, 0]), (y, xn[1, 0]), (z, xn[2, 0])])

print("x:", xn)

print("Error:", F.subs([(x, xn[0, 0]), (y, xn[1, 0]), (z, xn[2, 0])]))
Пример #9
0

def fill(i, j, t):
    if j == 0:
        return 1.0
    if j == 1:
        return pop[i][t]
    if j == 2:
        return pop[i][t]**2
    return pop[i][t]**3


# Line
A = Matrix(4, 2, lambda i, j: fill(i, j, 0))
b = Matrix(4, 1, [ln(p[1]) for p in pop])

b = A.transpose() * b
A = A.transpose() * A

exp = A.LUsolve(b)

t = symbols('t', real=True)

f = E**exp[0, 0] * E**(exp[1, 0] * t)

est = f.subs(t, 1980)
actual = 4452584592

print("Funksjon:", f)
print("1980: Estimert:", est, "Feil:", abs(est - actual))
Пример #10
0
from sympy import symbols, diff, Matrix

# DF(x)s=-F(x)

sym = u, v = symbols('u v', real=True)

fun = f1, f2 = u ** 3 - v ** 3 + u, \
               u ** 2 + v ** 2 - 1

DF = Matrix(2, 2, lambda i, j: diff(fun[i], sym[j]))
F = Matrix([[f1], [f2]])
x = Matrix([[1.0], [1.0]])

for _ in range(0, 20):
    x += DF.LUsolve(-F).subs([(u, x[0, 0]), (v, x[1, 0])])

print("x:", x)

print("Error:", F.subs([(u, x[0, 0]), (v, x[1, 0])]))
Пример #11
0
#       [3] Implicit form where the kinematics and dynamics are separate
#           M(q, p) u' = F(q, u, t, r, p)
#           q' = G(q, u, t, r, p)
dyn_implicit_mat = Matrix([[1, 0, -x / m], [0, 1, -y / m], [0, 0, l**2 / m]])

dyn_implicit_rhs = Matrix([0, 0, u**2 + v**2 - g * y])

comb_implicit_mat = Matrix([[1, 0, 0, 0, 0], [0, 1, 0, 0, 0],
                            [0, 0, 1, 0, -x / m], [0, 0, 0, 1, -y / m],
                            [0, 0, 0, 0, l**2 / m]])

comb_implicit_rhs = Matrix([u, v, 0, 0, u**2 + v**2 - g * y])

kin_explicit_rhs = Matrix([u, v])

comb_explicit_rhs = comb_implicit_mat.LUsolve(comb_implicit_rhs)

# Set up a body and load to pass into the system
theta = atan(x / y)
N = ReferenceFrame('N')
A = N.orientnew('A', 'Axis', [theta, N.z])
O = Point('O')
P = O.locatenew('P', l * A.x)

Pa = Particle('Pa', P, m)

bodies = [Pa]
loads = [(P, g * m * N.x)]

# Set up some output equations to be given to SymbolicSystem
# Change to make these fit the pendulum
Пример #12
0
        return 1.0
    if j == 1:
        return pop[i][t]
    if j == 2:
        return pop[i][t]**2
    return pop[i][t]**3


# Line
A = Matrix(4, 2, lambda i, j: fill(i, j, 0))
b = Matrix(4, 1, pop[:, 1])

b = A.transpose() * b
A = A.transpose() * A

line = A.LUsolve(b)

# Parabola
A = Matrix(4, 3, lambda i, j: fill(i, j, 0))
b = Matrix(4, 1, pop[:, 1])

b = A.transpose() * b
A = A.transpose() * A

par = A.LUsolve(b)

t = symbols('t', real=True)

line = line[0, 0] + line[1, 0] * t
par = par[0, 0] + par[1, 0] * t + par[2, 0] * t**2