-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvectors_projections.py
162 lines (141 loc) · 5.51 KB
/
vectors_projections.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
print(
'-----------------------------------------\n'\
'Python Library for Linear Algebra Operations | Calculating projections of the entered vectors by user\n'\
'-----------------------------------------\n'
)
print(
'Task:\n'\
'-----------------------------------------\n'\
'Write a simple Python module for calculating all necessary parameters projections of the entered vectors by user. You should calculate: \n- proj b (V) ;\n- orthogonal vector (V) ;\n- length of vector (V) ;\n'
)
print(
'Solution:\n'\
'-----------------------------------------'\
)
import os, sys, math
# Module for handling wrong data entered by user, along entering appropriate command:
class UnAcceptedValueError(Exception):
def __init__(self, data):
self.data = data
def __str__(self):
return repr(self.data)
#Module for creating a simple mathematical vectors on the dimension:
class Vector(object):
def __init__(self, coordinates):
try:
if not coordinates:
raise ValueError
self.coordinates = tuple(coordinates)
self.dimension = len(coordinates)
except ValueError:
raise ValueError('The coordinates must be nonempty')
except TypeError:
raise ValueError('The coordinate must be an iterable')
def magnitude(self):
coordinates_squared = [x**2 for x in self.coordinates]
return math.sqrt(sum(coordinates_squared))
def normalized(self):
try:
magnitude = self.magnitude()
return [x * (1 / magnitude) for x in self.coordinates]
except ZeroDivisionError:
raise Exception('Cannot normalize the zero vector.')
def __str__(self):
return 'Vector {}'.format(self.coordinates)
def __eq__(self, v):
return self.coordinates == v.coordinates
# Default function for handling execution loop:\
def execution_loop():
data = int(input("Do you want to try again ? Enter [1] - for continue / [0] - for quit : \n>>>"))
if data == 1:
return True
elif data == 0:
return False
else:
print('Error, you entered incorrect command. Please, try again...')
execution_loop()
# Function for creating a two vectors for verifying:
def creating_vectors():
vectors = []
coordinate_counter = 1
coordinate_input_control = True
print('Please, enter all coordinates of the vector A:')
vectors.append(Vector(coordinate_create('A')))
print('Please, enter all coordinates of the vector B:')
vectors.append(Vector(coordinate_create('B', size=vectors[0].dimension)))
return vectors
# Function for interable creating a next coordinate of the vector:
def vector_loop(index):
data = int(input("Do you want to add a new {0}-th coordinate ? Enter :\n[2] - for adding a new coordinate;\n[1] - for adding a new vector;\n>>>".format(index)))
if data == 2:
return 2
elif data == 1:
return 1
else:
print('Error: you entered incorrect command. Please, try again...')
vector_loop()
# Function for loop-creating all coordinates of the selected vector:
def coordinate_create(name, **kwargs):
vector = []
coordinate_counter = 1
coordinate_input_control = True
size = kwargs.get('size', None)
while coordinate_input_control:
# print('Please, enter {0}-st coordinate:'.format(coordinate_counter))
vector.append(vector_coordinate_input(coordinate_counter))
if len(vector) == 3:
print('You almost entered the maximum of coordinates of the {0} vector. The maximum number of coordinates should be less of equal to 3'.format(name))
break
elif len(vector) > 1:
if name == 'A':
data = vector_loop(2)
if data == 2:
coordinate_input_control = True
else:
coordinate_input_control = False
break
else:
if size == 2:
coordinate_input_control = False
break
else:
coordinate_input_control = True
coordinate_counter = coordinate_counter + 1
print('You entered vector {0} with coordinates: {1}'.format(name, vector))
return vector
# Function for creating every new coordinate of the vector:
def vector_coordinate_input(coordinate_counter):
c_counter = coordinate_counter
print('Please, enter {0}-st coordinate:'.format(c_counter))
try:
user_coordinate = float(input(">>>"))
print('user_coordinate:')
print(user_coordinate)
return user_coordinate
except SyntaxError:
print("You cannot leave coordinate-field empty. Please, try enter any number...")
vector_coordinate_input(c_counter)
except KeyboardInterrupt:
print("You cannot quit from the program here. Please, try enter any number...")
vector_coordinate_input(c_counter)
except:
print("Coordinate of the vector should be only number. Please, try enter any number...")
vector_coordinate_input(c_counter)
# Default loop for handling execution:
again_exec = True
counter_exec = 0
# Default loop for handling execution loop:
while again_exec:
vectors = []
vectors = creating_vectors()
again_exec = execution_loop()
counter_exec = counter_exec + 1
# The end of execution:
if again_exec == False:
print('Program was executed: {0} times.'.format(counter_exec))
break
print(
'\n-----------------------------------------\n'\
'Copyright 2019 Vladimir Pavlov. All Rights Reserved.\n'\
'-----------------------------------------'
)