-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsho3a3.py
162 lines (137 loc) · 5.48 KB
/
sho3a3.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
"""This librar is not for the direct use"""
from typing import Any
class Sho3a3:
def __init__(self, numbers: list[float]):
# هذه المكتبة ليست للإستخدام المباشر
self.sho3a3 = numbers
self.ab3ad = (1, len(numbers))
def __repr__(self) -> str:
return (f"sho3a3:{self.sho3a3}")
def __str__(self):
return (f"sho3a3:{self.sho3a3}")
def __len__(self):
return len(self.sho3a3)
def __iter__(self):
for i in (self.sho3a3):
yield i
def __getitem__(self, placement):
return self.sho3a3[placement]
def __setitem__(self, placement, value):
self.sho3a3[placement] = value
# making the add of the sho3a3
def __add__(self, other):
"""عملية طرح الشعاعين (المتجهين)"""
if isinstance(other, Sho3a3):
assert len(other) == len(
self), "sho3a3s must have the same number of elements"
jadid = []
for i in range(len(other)):
jadid.append(other.sho3a3[i] + self.sho3a3[i])
return Sho3a3(jadid)
else:
assert isinstance(other, int) or isinstance(
other, float), "Wrong argument type"
return Sho3a3([element + other for element in self.sho3a3])
def __radd__(self, other):
assert isinstance(other, int) or isinstance(
other, float), "Wrong argument type"
return Sho3a3([other + element for element in self.sho3a3])
# making the subtraction of the sho3a3
def __sub__(self, other):
"""عملية طرح الشعاعين (المتجهين)"""
if isinstance(other, Sho3a3):
assert len(other) == len(
self), "sho3a3s must have the same number of elements"
jadid = []
for i in range(len(other)):
jadid.append(other.sho3a3[i] - self.sho3a3[i])
return Sho3a3(jadid)
else:
assert isinstance(other, int) or isinstance(
other, float), "Wrong argument type"
return Sho3a3([element - other for element in self.sho3a3])
def __rsub__(self, other):
assert isinstance(other, int) or isinstance(
other, float), "Wrong argument type"
return Sho3a3([other - element for element in self.sho3a3])
# making multiplication of the sho3a3
def __mul__(self, other) -> float:
if isinstance(other, Sho3a3):
sollami = 0
for i in range(len(self.sho3a3)):
sollami += self.sho3a3[i] * other.sho3a3[i]
return sollami
else:
assert isinstance(other, int) or isinstance(
other, float), "Wrong argument type"
return Sho3a3([other * element for element in self.sho3a3])
def __rmul__(self, other):
assert isinstance(other, int) or isinstance(
other, float), "Wrong argument type"
return Sho3a3([other * element for element in self.sho3a3])
# making div of the sho3a3
def __truediv__(self, other) -> float:
if isinstance(other, Sho3a3):
sollami = 0
for i in range(len(self.sho3a3)):
sollami += self.sho3a3[i] / other.sho3a3[i]
return sollami
else:
assert isinstance(other, int) or isinstance(
other, float), "Wrong argument type"
return Sho3a3([other / element for element in self.sho3a3])
def __rtruediv__(self, other):
assert isinstance(other, int) or isinstance(
other, float), "Wrong argument type"
return Sho3a3([other / element for element in self.sho3a3])
# making the power of sho3a3
def __pow__(self, other):
if isinstance(other, Sho3a3):
sollami = 0
for i in range(len(self.sho3a3)):
sollami += self.sho3a3[i] ** other.sho3a3[i]
return sollami
else:
assert isinstance(other, int) or isinstance(
other, float), "Wrong argument type"
return Sho3a3([element ** other for element in self.sho3a3])
def __rpow__(self, other):
assert isinstance(other, int) or isinstance(
other, float), "Wrong argument type"
return Sho3a3([other ** element for element in self.sho3a3])
# printing the shape of the vector
def shape(self):
"""طباعة أبعاد المتجه"""
toul = len(self.sho3a3)
return (1, toul)
def sum(self)-> float:
jam3 = 0
for element in self.sho3a3:
jam3 += element
return jam3
def element_wise_product(self, other):
for i in range(len(self)):
self.sho3a3[i] *= other.sho3a3[i]
# sho3a3_1 = Sho3a3([1, 2, 3,30, 4,20, 5.1001])
# sho3a3_2 = Sho3a3([1, 2, 3,30, 4,20, 5.1001])
# print(sho3a3_1)
# sho3a3_1.element_wise_product(sho3a3_2)
# print(sho3a3_1)
# print(sho3a3_1.sum())
# sho3a3_2 = Sho3a3([1.1, 2, 3, 4, 5])
# for num in sho3a3_1:
# print(num)
# print(sho3a3_1)
# sho3a3_1[0] = 1000
# print(sho3a3_1 ** 2)
# print(2 ** sho3a3_1)
# print(sho3a3_1[0])
# print("Basic:",sho3a3_1)
# print("add",sho3a3_1+sho3a3_2)
# print("mul",sho3a3_1*2)
# print(2 * sho3a3_1)
# print("dot",sho3a3_1*sho3a3_2)
# print("sub",sho3a3_1-sho3a3_2)
# print(sho3a3_1-1)
# print(1-sho3a3_1)
# print("div",sho3a3_1/sho3a3_1)