-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfieldArray.py
More file actions
119 lines (91 loc) · 3.86 KB
/
fieldArray.py
File metadata and controls
119 lines (91 loc) · 3.86 KB
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
from field import field
import random
class fieldArray(field):
array = []
def __init__(self, arraySize, fieldSize):
self.arraySize = arraySize
self.fieldSize = fieldSize
self.array = [field(fieldSize, 0) for x in range(arraySize)]
def fillRandom(self):
for i in range(self.arraySize):
self.array[i].setValue(random.randint(0, self.fieldSize-1))
def fillWithSpecificDensity(self, sparseRate):
self.fillRandom()
assert(0 <= sparseRate < 1), "Sparse rate can be in range of [0,1)"
# specify the specific density function here
numberOfZeros = self.arraySize * sparseRate
# picks the "numberOfZeros" number in range of arraySize without any duplicate member
zeroVector = random.sample(range(self.arraySize), numberOfZeros)
for i in range(len(zeroVector)):
self.array[zeroVector[i]] = 0
def makeSparseWithSpecificRate(self, sparseRate):
assert(0 <= sparseRate < 1), "Sparse rate can be in range of [0,1)"
# specify the specific density function here
numberOfZeros = self.arraySize * sparseRate
# picks the "numberOfZeros" number in range of arraySize without any duplicate member
zeroVector = random.sample(range(self.arraySize), numberOfZeros)
for i in range(len(zeroVector)):
self.array[zeroVector[i]] = 0
def getExactDensity(self):
counter = 0
for i in range(self.arraySize):
if(self.array[i].getValue() == 0):
counter += 1
assert (self.arraySize != 0), "arraySize is zero"
return counter/self.arraySize
def setValue(self, position, other):
# control the value of te position ( out of bound problem)
assert(position < self.arraySize), "position is bigger than the array size"
assert(other.getValue() <
self.fieldSize), "value must be bounded by field size"
self.array[position] = other.getValue()
# override plus
def __add__(self, other):
assert(self.arraySize ==
other.arraySize), "array size must be eqaul which is not here!"
assert(self.fieldSize == other.getFieldSize()
), "field size must be eqaul which is not here!"
tempFieldArray = fieldArray(self.arraySize,
self.fieldSize)
for i in range(self.arraySize):
tempFieldArray.array[i] = self.array[i] + other.array[i]
return tempFieldArray
# override multiplication
def __mul__(self, other):
# check the array size, They must be the same
assert(self.arraySize ==
other.arraySize), "array size must be eqaul which is not here!"
assert(self.fieldSize == other.getFieldSize()
), "field size must be eqaul which is not here!"
tempFieldArray = fieldArray(self.arraySize, self.fieldSize)
for i in range(self.arraySize):
tempField = self.array[i] * other.array[i]
tempFieldArray.array[i] = tempField
return tempFieldArray
def show(self):
string = "["
for i in range(self.arraySize - 1):
string += str(self.array[i].getValue()) + ','
string += str(self.array[self.arraySize - 1].getValue()) + ']'
print(string)
def giveArraySimpleForm(self):
tempList = []
''' return coefficient as an simple array'''
for i in range(self.arraySize):
tempList.append(self.array[i].getValue())
return tempList
if __name__ == "__main__":
varJ = fieldArray(10, 10)
varJ.fillRandom()
varJ.show()
varK = fieldArray(10, 10)
varK.fillRandom()
varK.show()
varTest = varK * varJ
varTest.show()
varA = field(10, 7)
varB = field(10, 5)
varC = varA + varB
varD = varA * varC
print(varC.getValue())
print(varD.getValue())