-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvector.py
171 lines (119 loc) · 3.44 KB
/
vector.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
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 11 11:48:29 2022
@author: hernando
"""
from collections.abc import Iterable
def _isnumber(v):
"""
Parameters
----------
v : Any
Returns
-------
bool: Return True if v is a number (int, float or complex)
"""
if (not any([type(v) == t for t in [int, float, complex]])):
raise TypeError("Must be a number (int, float, complex)")
return True
class Vector:
def __init__(self, values):
"""
Create a Vector instance
Parameters
----------
values : Iterable, list of values
Raises
------
TypeError, if values if not an iterable of numbers
Returns
-------
Create a Vector instance
"""
values = values.values if isinstance(values, Vector) else values
if (not isinstance(values, Iterable)):
raise TypeError("Vector constructor: iterable required" )
for v in values: _isnumber(v)
self.values = list(values)
self.length = len(values)
for v in values: _isnumber(v)
return
def __add__(self, vector):
"""
Add two vectors
Parameters
----------
vector : Vector or Iterable of Numbers
Raises
------
TypeError
Returns
-------
c : Vector: The sum of two vectors: c = a + b
"""
b = vector if isinstance(vector, Vector) else Vector(vector)
if (b.length != self.length):
raise TypeError("Vector add : same dimesion vector required")
c = Vector([ai + bi for ai, bi in zip(self.values, b.values)])
return c
def __mul__(self, scalar):
"""
Multiply a vector by an number
Parameters
----------
scalar : a number (int, float or complex)
DESCRIPTION.
Raises
------
TypeError
DESCRIPTION.
Returns
-------
c : TYPE
DESCRIPTION.
"""
if (not _isnumber(scalar)):
raise TypeError("Vector mul requires scalar")
c = Vector([scalar * vi for vi in self.values])
return c
def __rmul__(self, scalar):
"""
Right multiplication of a Vector by an scalar a * vector
"""
return Vector.__mul__(self, scalar)
def __getitem__(self, i):
"""
Parameters
----------
i : int, index of the coordinate
Returns
-------
Number: value of the ith coordinate of the vector
"""
return self.values[i]
def __eq__(self, vector):
"""
Parameters
----------
vector : Vector of Iterable of Numbers
Returns
-------
bool : if the two vector has identical components
"""
b = vector if isinstance(vector, Vector) else Vector(vector)
return all([xi == yi for xi, yi in zip(self.values, b.values)])
def __str__(self):
"""
Returns
-------
str, coverts vector into string
"""
return str(self.values)
def __repr__(self):
"""
Returns
-------
str, converts vector into string
"""
return str(self.values)