|
1 | 1 | class Vector: |
2 | | - """ |
3 | | - Constructor |
4 | | - |
5 | | - self: a reference to the object we are creating |
6 | | - vals: a list of integers which are the contents of our vector |
7 | | - """ |
8 | | - def __init__(self, vals): |
9 | | - self.vals = vals |
10 | | - # print("Assigned values ", vals, " to vector.") |
11 | | - |
12 | | - """ |
| 2 | + """ |
| 3 | + Constructor |
| 4 | +
|
| 5 | + self: a reference to the object we are creating |
| 6 | + vals: a list of integers which are the contents of our vector |
| 7 | + """ |
| 8 | + |
| 9 | + def __init__(self, vals): |
| 10 | + self.vals = vals |
| 11 | + # print("Assigned values ", vals, " to vector.") |
| 12 | + |
| 13 | + """ |
13 | 14 | String Function |
14 | 15 | |
15 | 16 | Converts the object to a string in readable format for programmers |
16 | 17 | """ |
17 | | - def __str__(self): |
18 | | - return str(self.vals) |
19 | | - |
20 | | - """ |
| 18 | + |
| 19 | + def __str__(self): |
| 20 | + return str(self.vals) |
| 21 | + |
| 22 | + """ |
21 | 23 | Elementwise power: raises each element in our vector to the given power |
22 | 24 | """ |
23 | | - def __pow__(self, power): |
24 | | - return Vector([i**power for i in self.vals]) |
25 | | - |
26 | | - """ |
| 25 | + |
| 26 | + def __pow__(self, power): |
| 27 | + return Vector([i ** power for i in self.vals]) |
| 28 | + |
| 29 | + """ |
27 | 30 | Addition: adds each element to corresponding element in other vector |
28 | 31 | """ |
29 | | - def __add__(self, vec): |
30 | | - return Vector([self.vals[i] + vec.vals[i] for i in range(len(self.vals))]) |
31 | | - |
32 | | - """ |
| 32 | + |
| 33 | + def __add__(self, vec): |
| 34 | + return Vector( |
| 35 | + [self.vals[i] + vec.vals[i] for i in range(len(self.vals))] |
| 36 | + ) |
| 37 | + |
| 38 | + """ |
33 | 39 | Multiplies each element in the vector by a specified constant |
34 | 40 | """ |
35 | | - def __mul__(self, constant): |
36 | | - return Vector([self.vals[i] * constant for i in range(len(self.vals))]) |
37 | | - |
38 | | - """ |
| 41 | + |
| 42 | + def __mul__(self, constant): |
| 43 | + return Vector([self.vals[i] * constant for i in range(len(self.vals))]) |
| 44 | + |
| 45 | + """ |
39 | 46 | Elementwise subtraction: does the same as addition, just subtraction instead |
40 | 47 | """ |
41 | | - def __sub__(self, vec): |
42 | | - return self + (vec * (-1)) |
43 | | - |
| 48 | + |
| 49 | + def __sub__(self, vec): |
| 50 | + return self + (vec * (-1)) |
| 51 | + |
| 52 | + |
44 | 53 | vec = Vector([2, 3, 2]) |
45 | 54 | otherVec = Vector([3, 4, 5]) |
46 | | -print(str(vec)) # [2, 3, 2] |
47 | | -print(vec ** 2) # [4, 9, 4] |
48 | | -print(vec - otherVec) # [-1, -1, -3] |
49 | | -print(vec + otherVec) # [5, 7, 7] |
50 | | -print(vec * 5) # [10, 15, 10] |
| 55 | +print(str(vec)) # [2, 3, 2] |
| 56 | +print(vec ** 2) # [4, 9, 4] |
| 57 | +print(vec - otherVec) # [-1, -1, -3] |
| 58 | +print(vec + otherVec) # [5, 7, 7] |
| 59 | +print(vec * 5) # [10, 15, 10] |
0 commit comments