示例#1
0
 def add_multiple_times_row_to_row(self, coefficient, row_to_add,
                                   row_to_add_to):
     c = coefficient
     rta = row_to_add
     rtat = row_to_add_to
     temp_constant = self[rta].constant_term * c
     temp_vector = Vector(self[rta].normal_vector.scalar_mult(c))
     self[rtat] = Hyperplane(Vector(temp_vector.plus(self[rtat].normal_vector)),\
                               self[rtat].constant_term + temp_constant)
     return
示例#2
0
    def __eq__(self, ell):

        if self.normal_vector.is_zero():
            if not ell.normal_vector.is_zero():
                return False
            else:
                diff = self.constant_term - ell.constant_term
                return MyDecimal(diff).is_near_zero()
        elif ell.normal_vector.is_zero():
            return False

        if not self.parallel(ell):
            return False

        x0 = self.basepoint
        y0 = ell.basepoint
        basepoint_difference = Vector(x0.minus(y0))
        n = self.normal_vector
        return basepoint_difference.orthogonal(n)
示例#3
0
    def do_gaussian_elimination_and_extract_solution(self):
        rref = self.compute_rref()

        rref.raise_exception_if_contradictory_equation()
        rref.raise_exception_too_few_pivots()

        num_variables = rref.dimension
        solution_coordinates = [
            rref[i].constant_term for i in range(num_variables)
        ]
        return Vector(solution_coordinates)
示例#4
0
    def intersection_with(self, ell):
        try:
            A, B = self.normal_vector.coordinates
            C, D = ell.normal_vector.coordinates
            k1 = self.constant_term
            k2 = ell.constant_term

            x_numerator = D * k1 - B * k2
            y_numerator = -C * k1 + A * k2
            one_over_denom = Decimal(1.0) / round((A * D - B * C), 10)
            ##            print(A*D - B*C)
            return Vector(
                Vector([x_numerator, y_numerator]).scalar_mult(one_over_denom))

        except ZeroDivisionError:
            if self == ell:
                ##                print('self:', self)
                ##                print('ell:', ell)
                ##                print(self == ell)
                return self
            else:
                return None
示例#5
0
    def __init__(self, normal_vector=None, constant_term=None):
        self.dimension = 2

        if not normal_vector:
            normal_vector = Vector((Decimal(0), ) * self.dimension)

        self.normal_vector = normal_vector

        if not constant_term:
            constant_term = 0

        self.constant_term = Decimal(constant_term)
        self.set_basepoint()
示例#6
0
    def extract_basepoint_for_parametrization(self):
        num_variables = self.dimension
        pivot_indices = self.indices_of_first_nonzero_terms_in_each_row()

        basepoint_coords = [0] * num_variables

        for i, p in enumerate(self.planes):
            pivot_var = pivot_indices[i]
            if pivot_var < 0:
                break
            basepoint_coords[pivot_var] = p.constant_term

        return Vector(basepoint_coords)
示例#7
0
    def __init__(self, normal_vector=None, constant_term=None):

        if not normal_vector:
            default_dimension = 3
            normal_vector = Vector((Decimal(0), ) * default_dimension)

        self.normal_vector = normal_vector
        self.dimension = normal_vector.dimension

        if not constant_term:
            constant_term = 0

        self.constant_term = Decimal(constant_term)
        self.set_basepoint()
示例#8
0
    def set_basepoint(self):
        try:
            n = self.normal_vector.coordinates
            c = self.constant_term
            basepoint_coords = [0]*self.dimension

            initial_index = Hyperplane.first_nonzero_index(n)
            initial_coefficient = n[initial_index]

            basepoint_coords[initial_index] = c/initial_coefficient
            self.basepoint = Vector(basepoint_coords)

        except Exception as e:
            if str(e) == Hyperplane.NO_NONZERO_ELTS_FOUND_MSG:
                return None
            else:
                raise e
示例#9
0
    def __init__(self, normal_vector=None, constant_term=None, dimension=None):
        # note: dimension parameter must be placed last since this usually a default value.
        # otherwise linsys crashes

        if not dimension and not normal_vector:
            raise Exception(self.EITHER_DIM_OR_NORMAL_VEC_MUST_BE_PROVIDED_MSG)
        elif not normal_vector:
            normal_vector = Vector((Decimal(0),) * dimension)
            self.dimension = dimension
        else:
            self.dimension = normal_vector.dimension

        self.normal_vector = normal_vector

        if not constant_term:
            constant_term = 0

        self.constant_term = Decimal(constant_term)
        self.set_basepoint()
示例#10
0
    def extract_direction_vectors_for_parametrization(self):
        num_variables = self.dimension
        pivot_indices = self.indices_of_first_nonzero_terms_in_each_row()
        free_variable_indices = set(range(num_variables)) - set(pivot_indices)

        direction_vectors = []

        for free_var in free_variable_indices:
            vector_coords = [0] * num_variables
            vector_coords[free_var] = 1
            for i, p in enumerate(self.planes):
                pivot_var = pivot_indices[i]
                if pivot_var < 0:
                    break
                vector_coords[
                    pivot_var] = -p.normal_vector.coordinates[free_var]
            direction_vectors.append(Vector(vector_coords))

        return direction_vectors
示例#11
0
from linearsys import LinearSystem
from plane import Plane
from hyperplane import Hyperplane
from vector_alt import Vector

p1 = Plane(normal_vector=Vector([5.862, 1.178, -10.366]), constant_term=-8.15)
p2 = Plane(normal_vector=Vector([-2.931, -0.589, 5.183]), constant_term=-4.075)
s = LinearSystem([p1, p2])
r = s.compute_rref()
cs = s.compute_ge_solution()
ps = s.compute_solution()
print('Gauss Elim:')
print(cs)
print('RREF:')
print(r)
print('Parametrized:')
print(ps)
print()

p1 = Plane(normal_vector=Vector([8.631, 5.112, -1.816]), constant_term=-5.113)
p2 = Plane(normal_vector=Vector([4.315, 11.132, -5.27]), constant_term=-6.775)
p3 = Plane(normal_vector=Vector([-2.158, 3.01, -1.727]), constant_term=-0.831)
s = LinearSystem([p1, p2, p3])
r = s.compute_rref()
cs = s.compute_ge_solution()
ps = s.compute_solution()
print('Gauss Elim:')
print(cs)
print('RREF:')
print(r)
print('Parametrized:')
示例#12
0
from linearsys import LinearSystem
from plane import Plane
from hyperplane import Hyperplane
from vector_alt import Vector

p1 = Plane(normal_vector=Vector([0.786, 0.786, 0.588]), constant_term=-0.714)
p2 = Plane(normal_vector=Vector([-0.131, -0.131, 0.244]), constant_term=0.319)
s = LinearSystem([p1, p2])
r = s.compute_rref()
cs = s.compute_ge_solution()
ps = s.compute_solution()
print('Gauss Elim:')
print(cs)
print('RREF:')
print(r)
print('Parametrized:')
print(ps)
print()

p1 = Plane(normal_vector=Vector([8.631, 5.112, -1.816]), constant_term=-5.113)
p2 = Plane(normal_vector=Vector([4.315, 11.132, -5.27]), constant_term=-6.775)
p3 = Plane(normal_vector=Vector([-2.158, 3.01, -1.727]), constant_term=-0.831)
s = LinearSystem([p1, p2, p3])
r = s.compute_rref()
cs = s.compute_ge_solution()
ps = s.compute_solution()
print('Gauss Elim:')
print(cs)
print('RREF:')
print(r)
print('Parametrized:')
示例#13
0
#Quiz_CodingTriangularForm.py
from linearsys import LinearSystem
from plane import Plane
from vector_alt import Vector

p1 = Plane(normal_vector=Vector([1, 1, 1]), constant_term=1)
p2 = Plane(normal_vector=Vector([0, 1, 1]), constant_term=2)
s = LinearSystem([p1, p2])
t = s.compute_triangular_form()
print(t)
if (t[0] == p1 and t[1] == p2):
    print('test case 1 passed')
else:
    print('test case 1 failed')

p1 = Plane(normal_vector=Vector([1, 1, 1]), constant_term=1)
p2 = Plane(normal_vector=Vector([1, 1, 1]), constant_term=2)
s = LinearSystem([p1, p2])
t = s.compute_triangular_form()
print(t)
if (t[0] == p1 and t[1] == Plane(constant_term=1)):
    print('test case 2 passed')
else:
    print('test case 2 failed')

p1 = Plane(normal_vector=Vector([1, 1, 1]), constant_term=1)
p2 = Plane(normal_vector=Vector([0, 1, 0]), constant_term=2)
p3 = Plane(normal_vector=Vector([1, 1, -1]), constant_term=3)
p4 = Plane(normal_vector=Vector([1, 0, -2]), constant_term=2)
s = LinearSystem([p1, p2, p3, p4])
t = s.compute_triangular_form()
示例#14
0
from vector_alt import Vector
from plane import Plane

print('1st Pair....')
n1 = Vector((-0.412, 3.806, 0.728))
k1 = -3.46
n2 = Vector((1.03, -9.515, -1.82))
k2 = 8.65
p1 = Plane(n1, k1)
p2 = Plane(n2, k2)
print('Planes Parallel?', p1.parallel(p2))
print('Planes Equal?', p1.plane_equal_with(p2))
print()
print('2nd Pair....')
n1 = Vector((2.611, 5.528, 0.283))
k1 = 4.6
n2 = Vector((7.715, 8.306, 5.342))
k2 = 3.76
p1 = Plane(n1, k1)
p2 = Plane(n2, k2)
print('Planes Parallel?', p1.parallel(p2))
print('Planes Equal?', p1.plane_equal_with(p2))
print()
print('3rd Pair....')
n1 = Vector((-7.926, 8.625, -7.212))
k1 = -7.952
n2 = Vector((-2.642, 2.875, -2.404))
k2 = -2.443
p1 = Plane(n1, k1)
p2 = Plane(n2, k2)
print('Planes Parallel?', p1.parallel(p2))
示例#15
0
 def multiply_coefficient_and_row(self, coefficient, row):
     c = coefficient
     constant_term = self[row].constant_term * c
     self[row] = Hyperplane(Vector(self[row].normal_vector.scalar_mult(c)),
                            constant_term)
     return
示例#16
0
from vector_alt import Vector
from line import Line

print('1st Pair....')
n1 = Vector((4.046, 2.836))
k1 = 1.21
n2 = Vector((10.115, 7.09))
k2 = 3.025
ell1 = Line(n1, k1)
ell2 = Line(n2, k2)
print('Calculate Intersection:', ell1.intersection_with(ell2))
print()
print('2nd Pair....')
n1 = Vector((7.204, 3.182))
k1 = 8.68
n2 = Vector((8.172, 4.114))
k2 = 9.883
ell1 = Line(n1, k1)
ell2 = Line(n2, k2)
print('Calculate Intersection:', ell1.intersection_with(ell2))
print()
print('3rd Pair....')
n1 = Vector((1.182, 5.562))
k1 = 6.744
n2 = Vector((1.773, 8.343))
k2 = 9.525
ell1 = Line(n1, k1)
ell2 = Line(n2, k2)
print('Calculate Intersection:', ell1.intersection_with(ell2))
示例#17
0
from linearsys import LinearSystem
from hyperplane import Hyperplane
from vector_alt import Vector

h1 = Hyperplane(normal_vector=Vector([0.786, 0.786]), constant_term=-0.714)
h2 = Hyperplane(normal_vector=Vector([-0.131, -0.131]), constant_term=0.319)
s = LinearSystem([h1, h2])
r = s.compute_rref()
cs = s.compute_ge_solution()
ps = s.compute_solution()
print('2D Test')
print('Gauss Elimination:')
print(cs)
print()
print('Reduced Row Eschelon Form:')
print(r)
print()
print('Parametrized:')
print(ps)
print()

h1 = Hyperplane(normal_vector=Vector([2.102, 7.489, -0.786]),
                constant_term=-0.713555)
h2 = Hyperplane(normal_vector=Vector([-1.131, -8.318, 1.209]),
                constant_term=0.118855)
h3 = Hyperplane(normal_vector=Vector([9.015, -5.873, 1.105]),
                constant_term=0.221634)
s = LinearSystem([h1, h2, h3])
r = s.compute_rref()
cs = s.compute_ge_solution()
ps = s.compute_solution()
示例#18
0
from linearsys import LinearSystem
from plane import Plane
from vector_alt import Vector

p0 = Plane(normal_vector=Vector([1, 1, 1]), constant_term=1)
p1 = Plane(normal_vector=Vector([0, 1, 0]), constant_term=2)
p2 = Plane(normal_vector=Vector([1, 1, -1]), constant_term=3)
p3 = Plane(normal_vector=Vector([1, 0, -2]), constant_term=2)

s = LinearSystem([p0, p1, p2, p3])
s.swap_rows(0, 1)
if (s[0] == p1 and s[1] == p0 and s[2] == p2 and s[3] == p3):
    print('test case 1 passed')
else:
    print('test case 1 failed')

s.swap_rows(1, 3)
if (s[0] == p1 and s[1] == p3 and s[2] == p2 and s[3] == p0):
    print('test case 2 passed')
else:
    print('test case 2 failed')

s.swap_rows(3, 1)
if (s[0] == p1 and s[1] == p0 and s[2] == p2 and s[3] == p3):
    print('test case 3 passed')
else:
    print('test case 3 failed')

s.multiply_coefficient_and_row(1, 0)
if (s[0] == p1 and s[1] == p0 and s[2] == p2 and s[3] == p3):
    print('test case 4 passed')
示例#19
0
from linearsys import LinearSystem, MyDecimal
from vector_alt import Vector
from hyperplane import Hyperplane

p0 = Hyperplane(normal_vector=Vector([1,1,1]), constant_term = 1)
p1 = Hyperplane(normal_vector=Vector([0,1,0]), constant_term = 2)
p2 = Hyperplane(normal_vector=Vector([1,1,-1]), constant_term = 3)
p3 = Hyperplane(normal_vector=Vector([1,0,-2]), constant_term = 2)

s = LinearSystem([p0,p1,p2,p3])
print(s)
print()
#print('First non-zero index of each eqn:', s.indices_of_first_nonzero_terms_in_each_row())
#print()
##print('{}\n{}\n{}\n{}'.format(s[0],s[1],s[2],s[3]))
print('# Equations:', len(s))
print()
s[0] = p1
print(s)
print()
print(MyDecimal('1e-9').is_near_zero())
print(MyDecimal('1e-11').is_near_zero())
示例#20
0
from vector_alt import Vector

print('First pair...')
# v1 = Vector([-7.579, -7.88])
# v2 = Vector([22.737, 23.64])
v1 = Vector([1, 1, 1, 1, 1, 1])
v2 = Vector([1, 1, 1, 1, 1, 1])
print('v1:', v1)
print('v2:', v2)
print('Dot Prod:', round(v1.dot_prod(v2), 3))
print('Unit Vector v1:', Vector(v1.normalize()))
print('Unit Vector v2:', Vector(v2.normalize()))
print('Dot Prod unit-v:', round(v1.dot_prod_unit(v2), 3))
print('Is Orthogonal: ', v1.orthogonal(v2))
print('Is parallel: ', v1.parallel(v2))
print('Angle:', round(v1.angle(v2), 4))
print()
print('Second pair...')
v1 = Vector([-2.029, 9.97, 4.172])
v2 = Vector([-9.231, -6.639, -7.245])
print('v1:', v1)
print('v2:', v2)
print('Dot Prod:', round(v1.dot_prod(v2), 3))
print('Unit Vector v1:', Vector(v1.normalize()))
print('Unit Vector v2:', Vector(v2.normalize()))
print('Dot Prod unit-v:', round(v1.dot_prod_unit(v2), 3))
print('Is Orthogonal: ', v1.orthogonal(v2))
print('Is parallel: ', v1.parallel(v2))
print('Angle:', round(v1.angle(v2), 4))
print()
print('Third pair...')