Skip to content

Commit 0e87742

Browse files
Update radix2_fft printing
Improved the printing method with f.prefix and String.join()
1 parent 9737b43 commit 0e87742

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

maths/radix2_fft.py

+22-17
Original file line numberDiff line numberDiff line change
@@ -174,20 +174,25 @@ def __multiply(self):
174174

175175
# Overwrite __str__ for print(); Shows A, B and A*B
176176
def __str__(self):
177-
A = "A = "
178-
B = "B = "
179-
C = "A*B = "
180-
for i in range(self.len_A):
181-
A += str(self.polyA[i]) + "*x^" + str(i) + " + "
182-
for i in range(self.len_B):
183-
B += str(self.polyB[i]) + "*x^" + str(i) + " + "
184-
for i in range(self.len_B + self.len_A - 1):
185-
C += (
186-
str(self.product[i])
187-
+ "*x^"
188-
+ str(i)
189-
+ " + "
190-
)
177+
A = "A = " + " + ".join(
178+
[
179+
f"{self.polyA[i]}*x^{i}"
180+
for i in range(self.len_A)
181+
]
182+
)
183+
B = "B = " + " + ".join(
184+
[
185+
f"{self.polyB[i]}*x^{i}"
186+
for i in range(self.len_B)
187+
]
188+
)
189+
C = "A*B = " + " + ".join(
190+
[
191+
f"{self.product[i]}*x^{i}"
192+
for i in range(len(self.product))
193+
]
194+
)
195+
191196
return A + "\n \n" + B + "\n \n" + C
192197

193198

@@ -197,6 +202,6 @@ def __str__(self):
197202
A = [0, 1, 0, 2] # x+2x^3
198203
B = (2, 3, 4, 0) # 2+3x+4x^2
199204
x = FFT(A, B)
200-
print(x.product) # 2x + 3x^2 + 8x^3 + 4x^4 + 6x^5,
201-
# as [(-0+0j), (2+0j), (3+0j), (8+0j), (6+0j), (8+0j)]
202-
print(x)
205+
print(x.product) # 2x + 3x^2 + 8x^3 + 4x^4 + 6x^5,
206+
# as [(-0+0j), (2+0j), (3+0j), (8+0j), (6+0j), (8+0j)]
207+
print(x)

0 commit comments

Comments
 (0)