def add_multiple_times_row_to_row(self, coefficient, row_to_add, row_to_be_added_to):
     row1 = self[row_to_add].normal_vector.times_scalar(coefficient)
     k1 = self[row_to_add].constant_term * coefficient
     row2 = self[row_to_be_added_to].normal_vector.plus(row1)
     k2 = self[row_to_be_added_to].constant_term
     k = k1 + k2
     self[row_to_be_added_to] = Hyperplane(normal_vector=row2,constant_term=k)        
Ejemplo n.º 2
0
 def multiply_coefficient_and_row(self, coefficient, row):
     '''mulitply normal vector and constant term with coefficient'''
     n = self[row].normal_vector
     k = self[row].constant_term
     new_normal_vector = n.times_scalar(coefficient)
     new_constant_term = k * coefficient
     self[row] = Hyperplane(normal_vector=new_normal_vector,
                            constant_term=new_constant_term)
Ejemplo n.º 3
0
 def add_multiple_times_row_to_row(self, coefficient, row_to_add,
                                   row_to_be_added_to):
     recipient_plane = self[row_to_be_added_to]
     new_plane = _get_new_plane(coefficient, self[row_to_add])
     new_normal_vector = \
         recipient_plane.normal_vector.plus(new_plane.normal_vector)
     constant_term = new_plane.constant_term + recipient_plane.constant_term
     self[row_to_be_added_to] = Hyperplane(normal_vector=new_normal_vector,
                                           constant_term=constant_term)
Ejemplo n.º 4
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
Ejemplo n.º 5
0
    def add_multiple_times_row_to_row(self, coefficient, row_to_add, row_to_be_added_to):
        n1 = self[row_to_add].normal_vector
        k1 = self[row_to_add].constant_term
        n2 = self[row_to_be_added_to].normal_vector
        k2 = self[row_to_be_added_to].constant_term

        new_normal_vector = n1.times_scalar(coefficient).plus(n2)
        new_constant_term = k1 * coefficient + k2

        self[row_to_be_added_to] = Hyperplane(normal_vector=new_normal_vector,
                                         constant_term=new_constant_term)
Ejemplo n.º 6
0
    def add_multiple_times_row_to_row(self, coefficient, from_here, to_here):
        '''add multiple times from_here row to_here row'''
        n1 = self[from_here].normal_vector
        n2 = self[to_here].normal_vector
        k1 = self[from_here].constant_term
        k2 = self[to_here].constant_term

        new_normal_vector = n1.times_scalar(coefficient).plus(n2)
        new_constant_term = (k1 * coefficient) + k2
        self[to_here] = Hyperplane(normal_vector=new_normal_vector,
                                   constant_term=new_constant_term)
Ejemplo n.º 7
0
    def multiply_coefficient_and_row(self, coefficient, row):
        # Notice that I make a new plane object here instead of just altering the normal_vector and constant_term of the current plane.
        # If I had altered the existing plane, I need to reset the plane's
        # base_point or else I would introduce some subtle bugs.
        n = self[row].normal_vector
        k = self[row].constant_term

        new_normal_vector = n.times_scalar(coefficient)
        new_constant_term = k * coefficient

        self[row] = Hyperplane(normal_vector=new_normal_vector,
                          constant_term=new_constant_term)
Ejemplo n.º 8
0
 def add_multiple_times_row_to_row(self, coofficient, row_to_add,
                                   row_to_be_add_to):
     try:
         receipt_row = self[row_to_be_add_to]
         new_plane = self.multiply_coefficient_and_row(
             coofficient, row_to_add)
         new_normal_vector = receipt_row.normal_vector.plus(
             new_plane.normal_vector)
         new_contant_term = receipt_row.contant_term + new_plane.contant_term
         self[row_to_be_add_to] = Hyperplane(
             normal_vector=new_normal_vector, contant_term=new_contant_term)
     except Exception as e:
         return str(e)
        self.direction_vectors = direction_vectors
        self.dimension = self.basepoint.dimension

        try:
            for v in direction_vectors:
                assert v.dimension == self.dimension

        except AssertionError:
            raise Exception(self.BASEPT_AND_DIR_VECTORS_MUST_BE_IN_SAME_DIM)

    def __str__(self):

        output = ''
        for coord in range(self.dimension):
            output += 'x_{} = {} '.format(coord + 1,
                                          round(self.basepoint[coord], 3))
            for free_var, vector in enumerate(self.direction_vectors):
                output += '+ {} t_{}'.format(round(vector[coord], 3),
                                             free_var + 1)
            output += '\n'
        return output

p1 = Hyperplane(normal_vector=Vector(['0.786', '0.786', '8.123', '1.111', '-8.363']),
                constant_term='-9.955')
p2 = Hyperplane(normal_vector=Vector(['0.131', '-0.131', '7.05', '-2.813', '1.19']),
                constant_term='-1.991')
p3 = Hyperplane(normal_vector=Vector(['9.015', '-5.873', '-1.105', '2.013', '-2.802']),
                constant_term='-3.982')

system = LinearSystem([p1, p2, p3])
print (system.compute_solution())
Ejemplo n.º 10
0
def _get_new_plane(coofficient, plane):
    new_normal_vector = plane.normal_vector.times_scalar(coofficient)
    return Hyperplane(dimension=new_normal_vector.dimension,
                      normal_vector=new_normal_vector,
                      constant_term=Decimal(plane.constant_term) *
                      Decimal(coofficient))
Ejemplo n.º 11
0
def _get_new_plane(coefficient, plane):
    new_normal_vector = plane.normal_vector.times_scalar(coefficient)
    return Hyperplane(normal_vector=new_normal_vector,
                      constant_term=coefficient * plane.constant_term)
Ejemplo n.º 12
0
    # p2 = Hyperplane(normal_vector=Vector(['-2.158','3.01','-1.727']), constant_term='-0.831')
    #
    # s2 = LinearSystem([p0,p1,p2])
    #
    # print('System 2 Solution:\n{}'.format(s2.compute_solution()))
    #
    # p0 = Hyperplane(normal_vector=Vector(['0.935','1.76','-9.365']), constant_term='-9.955')
    # p1 = Hyperplane(normal_vector=Vector(['0.187','0.352','-1.873']), constant_term='-1.991')
    # p2 = Hyperplane(normal_vector=Vector(['0.374','0.704','-3.746']), constant_term='-3.982')
    # p3 = Hyperplane(normal_vector=Vector(['-0.561','-1.056','5.619']), constant_term='5.973')
    #
    # s3 = LinearSystem([p0,p1,p2,p3])
    #
    # print('System 3 Solution:\n{}'.format(s3.compute_solution()))
    '''Extendint to Higher Dimensions'''
    p0 = Hyperplane(normal_vector=Vector([0.786, 0.786]), constant_term=-0.714)
    p1 = Hyperplane(normal_vector=Vector([-0.131, -0.131]),
                    constant_term=0.319)

    s1 = LinearSystem([p0, p1])

    print('System 1 Solution:\n{}'.format(s1.compute_solution()))

    p0 = Hyperplane(normal_vector=Vector(['2.102', '7.489', '-0.786']),
                    constant_term='-5.113')
    p1 = Hyperplane(normal_vector=Vector(['-1.131', '-8.318', '1.209']),
                    constant_term='-6.775')
    p2 = Hyperplane(normal_vector=Vector(['9.015', '-5.873', '1.105']),
                    constant_term='-0.831')

    s2 = LinearSystem([p0, p1, p2])
Ejemplo n.º 13
0
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:')
print(ps)
print()

p1 = Hyperplane(normal_vector=Vector([5.262, 2.739, -9.878]),
                constant_term=-3.441)
p2 = Hyperplane(normal_vector=Vector([5.111, 6.358, 7.638]),
                constant_term=-2.152)
p3 = Hyperplane(normal_vector=Vector([2.016, -9.924, -1.367]),
                constant_term=-9.278)
p4 = Hyperplane(normal_vector=Vector([2.167, -13.543, -18.883]),
                constant_term=-10.567)
s = LinearSystem([p1, p2, p3, p4])
r = s.compute_rref()
cs = s.compute_ge_solution()
ps = s.compute_solution()
print('Gauss Elim:')
print(cs)
print('RREF:')
print(r)
print('Parametrized:')
Ejemplo n.º 14
0
    def solve(self):
        """
        Solve the linear quation system.
        :return: None if there is no solution, a Vector object if there is exactly one solution,
                 a Parametrization if there are indefinitely many solutions.
        """
        # Compute the solution in form of rref:
        rref = self.compute_rref()
        print(rref)

        # 1. System is inconsistent if one row is 0 = k for k <> 0
        for plane in rref.planes:
            try:
                Plane.first_nonzero_index(plane.normal_vector)
            except Exception as ex:
                if str(ex) == Plane.NO_NONZERO_ELTS_FOUND_MSG:
                    k = MyDecimal(plane.constant_term)
                    if not k.is_near_zero():
                        # row is 0 = k for k <> 0, so no solution
                        return None
                else:
                    raise ex

        # 2. Indefinite number of solutions, if number of pivot variables < dimension of solution set
        pivot_indices = rref.indices_of_first_nonzero_terms_in_each_row()
        num_pivots = sum([1 if index >= 0 else 0 for index in pivot_indices])
        if num_pivots < rref.dimension:
            # Yes, indefinite number of solutions.
            #
            # First: Calculate the direction vectors of parametrized solution:
            #
            direction_vectors = []
            # Calculate indices of free variables (those who are not pivot variables):
            free_var_indices = set(range(rref.dimension)) - set(pivot_indices)
            # for every free variable calulate a direction vector:
            for free_var_index in free_var_indices:
                # build an empty direction vector
                dir_vector_coords = [0] * rref.dimension
                # set coordinate of free variable to 1
                dir_vector_coords[free_var_index] = 1
                # set coordinates of pivot variables:
                for i, plane in enumerate(rref.planes):
                    if pivot_indices[i] < 0:
                        # this one is not a pivot variable -> goto next
                        break
                    # calculate direction vector coordinate for this variable:
                    dir_vector_coords[pivot_indices[
                        i]] = -plane.normal_vector[free_var_index]
                # Add direction vector to :
                direction_vectors.append(Vector(dir_vector_coords))

            #
            # Second: Calculate the base point of parametrized solution
            #
            base_point_coords = [0] * rref.dimension
            for i, plane in enumerate(rref.planes):
                if pivot_indices[i] < 0:
                    break
                base_point_coords[pivot_indices[i]] = plane.constant_term
            base_point = Vector(base_point_coords)

            return Parametrization(base_point, direction_vectors)

        # 3. Consistent set has unique solution <=> each variable is a pivot variable
        solution_coords = [
            rref.planes[row].constant_term for row in range(rref.dimension)
        ]
        return Vector(solution_coords)
Ejemplo n.º 15
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())
Ejemplo n.º 16
0
    # print(s.indices_of_first_nonzero_terms_in_each_row())
    # print('{},{},{},{}'.format(s[0], s[1], s[2], s[3]))
    # print(len(s))
    # print(s)

    # s[0] = p1
    # print(s)

    # print(MyDecimal('1e-9').is_near_zero())
    # print(MyDecimal('1e-11').is_near_zero())

    print('###########################')
    print('Quiz: Coding Row Operations')

    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])

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

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

    s.swap_rows(3, 1)
Ejemplo n.º 17
0
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:')
print(ps)
print()

p1 = Hyperplane(normal_vector=Vector([0.935, 1.76, -9.365]),
                constant_term=-9.955)
p2 = Hyperplane(normal_vector=Vector([0.187, 0.352, -1.873]),
                constant_term=-1.991)
p3 = Hyperplane(normal_vector=Vector([0.374, 0.704, -3.746]),
                constant_term=-3.982)
p4 = Hyperplane(normal_vector=Vector([-0.561, -1.056, 5.619]),
                constant_term=5.973)
s = LinearSystem([p1, p2, p3, p4])
r = s.compute_rref()
cs = s.compute_ge_solution()
ps = s.compute_solution()
print('Gauss Elim:')
print(cs)
print('RREF:')
print(r)
print('Parametrized:')
Ejemplo n.º 18
0
        t[3] == Hyperplane()):
    print 'test case 3 failed'

p1 = Hyperplane(normal_vector=Vector(['0', '1', '1']), constant_term='1')
p2 = Hyperplane(normal_vector=Vector(['1', '-1', '1']), constant_term='2')
p3 = Hyperplane(normal_vector=Vector(['1', '2', '-5']), constant_term='3')
s = LinearSystem([p1, p2, p3])
t = s.compute_triangular_form()
if not (t[0] == Hyperplane(normal_vector=Vector(['1', '-1', '1']), constant_term='2') and
        t[1] == Hyperplane(normal_vector=Vector(['0', '1', '1']), constant_term='1') and
        t[2] == Hyperplane(normal_vector=Vector(['0', '0', '-9']), constant_term='-2')):
    print 'test case 4 failed'
'''

# Test Cases - Gaussian Elimination Solution
p1 = Hyperplane(normal_vector=Vector(['5.862', '1.178', '-10.366']), constant_term='-8.15')
p2 = Hyperplane(normal_vector=Vector(['-2.931', '-0.589', '5.183']), constant_term='-4.075')
s = LinearSystem([p1, p2])
solution = s.compute_solution()
print "{}\nSolution: {} \n".format(s,solution)

p1 = Hyperplane(normal_vector=Vector(['8.631', '5.112', '-1.816']), constant_term='-5.113')
p2 = Hyperplane(normal_vector=Vector(['4.315', '11.132', '-5.27']), constant_term='-6.775')
p3 = Hyperplane(normal_vector=Vector(['-2.158', '3.01', '-1.727']), constant_term='-0.831')
s1 = LinearSystem([p1, p2, p3])
solution1 = s1.compute_solution()
print "{}\n{} \n".format(s1,solution1)

p1 = Hyperplane(normal_vector=Vector(['5.262', '2.739', '-9.878']), constant_term='-3.441')
p2 = Hyperplane(normal_vector=Vector(['5.111', '6.358', '7.638']), constant_term='-2.152')
p3 = Hyperplane(normal_vector=Vector(['2.016', '-9.924', '-1.367']), constant_term='-9.278')
Ejemplo n.º 19
0
# print (system.compute_solution())


# The systems bellow are just to test hyperplanes

# p1 = Hyperplane(normal_vector=Vector([0.786, 0.786]), constant_term=0.786)
# p2 = Hyperplane(normal_vector=Vector([-0.131, -0.131]), constant_term=-0.131)

# system = LinearSystem([p1, p2])
# print (system.compute_solution())


# p1 = Hyperplane(normal_vector=Vector([2.102, 7.489, -0.786]),
#                 constant_term=-5.113)
# p2 = Hyperplane(normal_vector=Vector([-1.131, 8.318, -1.209]),
#                 constant_term=-6.775)
# p3 = Hyperplane(normal_vector=Vector([9.015, 5.873, -1.105]),
#                 constant_term=-0.831)

# system = LinearSystem([p1, p2, p3])
# print (system.compute_solution())

p1 = Hyperplane(normal_vector=Vector([0.786, 0.786, 8.123, 1.111, -8.363]),
                constant_term=-9.955)
p2 = Hyperplane(normal_vector=Vector([0.131, -0.131, 7.05, -2.813, 1.19]),
                constant_term=-1.991)
p3 = Hyperplane(normal_vector=Vector([9.015, -5.873, -1.105, 2.013, -2.802]),
                constant_term=-3.982)

system = LinearSystem([p1, p2, p3])
print (system.compute_solution())
Ejemplo n.º 20
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
 def multiply_coefficient_and_row(self, coefficient, row):
     aux = self[row].normal_vector.times_scalar(coefficient)
     aux2 = self[row].constant_term * coefficient
     self[row] = Hyperplane(normal_vector=aux, constant_term=aux2)
Ejemplo n.º 22
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()
Ejemplo n.º 23
0
p2 = Plane(Vector([4.315, 11.132, -5.27]), -6.775)
p3 = Plane(Vector([-2.158, 3.01, -1.727]), -0.831)

system = LinearSystem([p1, p2, p3])
print system.compute_solution()

p1 = Plane(Vector([0.935, 1.76, -9.365]), -9.955)
p2 = Plane(Vector([0.187, 0.352, -1.873]), -1.991)
p3 = Plane(Vector([0.374, 0.704, -3.746]), -3.982)
p4 = Plane(Vector([-0.561, -1.056, 5.619]), 5.973)

print system.compute_solution()

# The systems bellow are just to test hyperplanes

p1 = Hyperplane(normal_vector=Vector([0.786, 0.786]), constant_term=0.786)
p2 = Hyperplane(normal_vector=Vector([-0.131, -0.131]), constant_term=-0.131)

system = LinearSystem([p1, p2])
print system.compute_solution()

p1 = Hyperplane(normal_vector=Vector([2.102, 7.489, -0.786]),
                constant_term=-5.113)
p2 = Hyperplane(normal_vector=Vector([-1.131, 8.318, -1.209]),
                constant_term=-6.775)
p3 = Hyperplane(normal_vector=Vector([9.015, 5.873, -1.105]),
                constant_term=-0.831)

system = LinearSystem([p1, p2, p3])
print system.compute_solution()