Skip to content

Commit c424e8b

Browse files
committed
Fix some more numpy tests
1 parent 8711705 commit c424e8b

File tree

8 files changed

+32
-32
lines changed

8 files changed

+32
-32
lines changed

data_structures/heap/binomial_heap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ def delete_min(self):
354354
# Merge heaps
355355
self.merge_heaps(new_heap)
356356

357-
return min_value
357+
return int(min_value)
358358

359359
def pre_order(self):
360360
"""

electronics/circular_convolution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def circular_convolution(self) -> list[float]:
9191
final_signal = np.matmul(np.transpose(matrix), np.transpose(self.first_signal))
9292

9393
# rounding-off to two decimal places
94-
return [round(i, 2) for i in final_signal]
94+
return [float(round(i, 2)) for i in final_signal]
9595

9696

9797
if __name__ == "__main__":

fractals/julia_sets.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
def eval_exponential(c_parameter: complex, z_values: np.ndarray) -> np.ndarray:
4141
"""
4242
Evaluate $e^z + c$.
43-
>>> eval_exponential(0, 0)
43+
>>> float(eval_exponential(0, 0))
4444
1.0
4545
>>> abs(eval_exponential(1, np.pi*1.j)) < 1e-15
4646
True
@@ -101,17 +101,17 @@ def iterate_function(
101101
>>> np.round(iterate_function(eval_quadratic_polynomial,
102102
... 0,
103103
... 3,
104-
... np.array([0,1,2]))[0])
104+
... complex(np.array([0,1,2]))[0]))
105105
0j
106106
>>> np.round(iterate_function(eval_quadratic_polynomial,
107107
... 0,
108108
... 3,
109-
... np.array([0,1,2]))[1])
109+
... complex(np.array([0,1,2]))[1]))
110110
(1+0j)
111111
>>> np.round(iterate_function(eval_quadratic_polynomial,
112112
... 0,
113113
... 3,
114-
... np.array([0,1,2]))[2])
114+
... complex(np.array([0,1,2]))[2]))
115115
(256+0j)
116116
"""
117117

graphics/bezier_curve.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ def basis_function(self, t: float) -> list[float]:
3030
returns the x, y values of basis function at time t
3131
3232
>>> curve = BezierCurve([(1,1), (1,2)])
33-
>>> curve.basis_function(0)
33+
>>> [float(x) for x in curve.basis_function(0)]
3434
[1.0, 0.0]
35-
>>> curve.basis_function(1)
35+
>>> [float(x) for x in curve.basis_function(1)]
3636
[0.0, 1.0]
3737
"""
3838
assert 0 <= t <= 1, "Time t must be between 0 and 1."
@@ -55,9 +55,9 @@ def bezier_curve_function(self, t: float) -> tuple[float, float]:
5555
The last point in the curve is when t = 1.
5656
5757
>>> curve = BezierCurve([(1,1), (1,2)])
58-
>>> curve.bezier_curve_function(0)
58+
>>> [float(x) for x in curve.bezier_curve_function(0)]
5959
(1.0, 1.0)
60-
>>> curve.bezier_curve_function(1)
60+
>>> [float(x) for x in curve.bezier_curve_function(1)]
6161
(1.0, 2.0)
6262
"""
6363

graphs/dijkstra_binary_grid.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def dijkstra(
6969
x, y = predecessors[x, y]
7070
path.append(source) # add the source manually
7171
path.reverse()
72-
return matrix[destination], path
72+
return float(matrix[destination]), path
7373

7474
for i in range(len(dx)):
7575
nx, ny = x + dx[i], y + dy[i]

linear_algebra/src/power_iteration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def power_iteration(
7878
if is_complex:
7979
lambda_ = np.real(lambda_)
8080

81-
return lambda_, vector
81+
return float(lambda_), vector
8282

8383

8484
def test_power_iteration() -> None:

linear_programming/simplex.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ def generate_col_titles(self) -> list[str]:
107107

108108
def find_pivot(self) -> tuple[Any, Any]:
109109
"""Finds the pivot row and column.
110-
>>> Tableau(np.array([[-2,1,0,0,0], [3,1,1,0,6], [1,2,0,1,7.]]),
111-
... 2, 0).find_pivot()
110+
>>> tuple(int(x) for x in Tableau(np.array([[-2,1,0,0,0], [3,1,1,0,6],
111+
... [1,2,0,1,7.]]), 2, 0).find_pivot()
112112
(1, 0)
113113
"""
114114
objective = self.objectives[-1]
@@ -215,56 +215,56 @@ def run_simplex(self) -> dict[Any, Any]:
215215
Max: x1 + x2
216216
ST: x1 + 3x2 <= 4
217217
3x1 + x2 <= 4
218-
>>> Tableau(np.array([[-1,-1,0,0,0],[1,3,1,0,4],[3,1,0,1,4.]]),
219-
... 2, 0).run_simplex()
218+
>>> {key: float(value) for key, value in Tableau(np.array([[-1,-1,0,0,0],
219+
... [1,3,1,0,4],[3,1,0,1,4.]]), 2, 0).run_simplex().items()}
220220
{'P': 2.0, 'x1': 1.0, 'x2': 1.0}
221221
222222
# Standard linear program with 3 variables:
223223
Max: 3x1 + x2 + 3x3
224224
ST: 2x1 + x2 + x3 ≤ 2
225225
x1 + 2x2 + 3x3 ≤ 5
226226
2x1 + 2x2 + x3 ≤ 6
227-
>>> Tableau(np.array([
227+
>>> {key: float(value) for key, value in Tableau(np.array([
228228
... [-3,-1,-3,0,0,0,0],
229229
... [2,1,1,1,0,0,2],
230230
... [1,2,3,0,1,0,5],
231231
... [2,2,1,0,0,1,6.]
232-
... ]),3,0).run_simplex() # doctest: +ELLIPSIS
232+
... ]),3,0).run_simplex().items()} # doctest: +ELLIPSIS
233233
{'P': 5.4, 'x1': 0.199..., 'x3': 1.6}
234234
235235
236236
# Optimal tableau input:
237-
>>> Tableau(np.array([
237+
>>> {key: float(value) for key, value in Tableau(np.array([
238238
... [0, 0, 0.25, 0.25, 2],
239239
... [0, 1, 0.375, -0.125, 1],
240240
... [1, 0, -0.125, 0.375, 1]
241-
... ]), 2, 0).run_simplex()
241+
... ]), 2, 0).run_simplex().items()}
242242
{'P': 2.0, 'x1': 1.0, 'x2': 1.0}
243243
244244
# Non-standard: >= constraints
245245
Max: 2x1 + 3x2 + x3
246246
ST: x1 + x2 + x3 <= 40
247247
2x1 + x2 - x3 >= 10
248248
- x2 + x3 >= 10
249-
>>> Tableau(np.array([
249+
>>> {key: float(value) for key, value in Tableau(np.array([
250250
... [2, 0, 0, 0, -1, -1, 0, 0, 20],
251251
... [-2, -3, -1, 0, 0, 0, 0, 0, 0],
252252
... [1, 1, 1, 1, 0, 0, 0, 0, 40],
253253
... [2, 1, -1, 0, -1, 0, 1, 0, 10],
254254
... [0, -1, 1, 0, 0, -1, 0, 1, 10.]
255-
... ]), 3, 2).run_simplex()
255+
... ]), 3, 2).run_simplex().items()}
256256
{'P': 70.0, 'x1': 10.0, 'x2': 10.0, 'x3': 20.0}
257257
258258
# Non standard: minimisation and equalities
259259
Min: x1 + x2
260260
ST: 2x1 + x2 = 12
261261
6x1 + 5x2 = 40
262-
>>> Tableau(np.array([
262+
>>> {key: float(value) for key, value in Tableau(np.array([
263263
... [8, 6, 0, 0, 52],
264264
... [1, 1, 0, 0, 0],
265265
... [2, 1, 1, 0, 12],
266266
... [6, 5, 0, 1, 40.],
267-
... ]), 2, 2).run_simplex()
267+
... ]), 2, 2).run_simplex().items()}
268268
{'P': 7.0, 'x1': 5.0, 'x2': 2.0}
269269
270270
@@ -275,15 +275,15 @@ def run_simplex(self) -> dict[Any, Any]:
275275
2x1 + 4x2 <= 48
276276
x1 + x2 >= 10
277277
x1 >= 2
278-
>>> Tableau(np.array([
278+
>>> {key: float(value) for key, value in Tableau(np.array([
279279
... [2, 1, 0, 0, 0, -1, -1, 0, 0, 12.0],
280280
... [-8, -6, 0, 0, 0, 0, 0, 0, 0, 0.0],
281281
... [1, 3, 1, 0, 0, 0, 0, 0, 0, 33.0],
282282
... [4, 2, 0, 1, 0, 0, 0, 0, 0, 60.0],
283283
... [2, 4, 0, 0, 1, 0, 0, 0, 0, 48.0],
284284
... [1, 1, 0, 0, 0, -1, 0, 1, 0, 10.0],
285285
... [1, 0, 0, 0, 0, 0, -1, 0, 1, 2.0]
286-
... ]), 2, 2).run_simplex() # doctest: +ELLIPSIS
286+
... ]), 2, 2).run_simplex().items()} # doctest: +ELLIPSIS
287287
{'P': 132.0, 'x1': 12.000... 'x2': 5.999...}
288288
"""
289289
# Stop simplex algorithm from cycling.
@@ -307,11 +307,11 @@ def run_simplex(self) -> dict[Any, Any]:
307307
def interpret_tableau(self) -> dict[str, float]:
308308
"""Given the final tableau, add the corresponding values of the basic
309309
decision variables to the `output_dict`
310-
>>> Tableau(np.array([
310+
>>> {key: float(value) for key, value in Tableau(np.array([
311311
... [0,0,0.875,0.375,5],
312312
... [0,1,0.375,-0.125,1],
313313
... [1,0,-0.125,0.375,1]
314-
... ]),2, 0).interpret_tableau()
314+
... ]),2, 0).interpret_tableau().items()}
315315
{'P': 5.0, 'x1': 1.0, 'x2': 1.0}
316316
"""
317317
# P = RHS of final tableau

machine_learning/decision_tree.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ def mean_squared_error(self, labels, prediction):
2626
>>> tester = DecisionTree()
2727
>>> test_labels = np.array([1,2,3,4,5,6,7,8,9,10])
2828
>>> test_prediction = float(6)
29-
>>> tester.mean_squared_error(test_labels, test_prediction) == (
29+
>>> bool(tester.mean_squared_error(test_labels, test_prediction) == (
3030
... TestDecisionTree.helper_mean_squared_error_test(test_labels,
31-
... test_prediction))
31+
... test_prediction)))
3232
True
3333
>>> test_labels = np.array([1,2,3])
3434
>>> test_prediction = float(2)
35-
>>> tester.mean_squared_error(test_labels, test_prediction) == (
35+
>>> bool(tester.mean_squared_error(test_labels, test_prediction) == (
3636
... TestDecisionTree.helper_mean_squared_error_test(test_labels,
37-
... test_prediction))
37+
... test_prediction)))
3838
True
3939
"""
4040
if labels.ndim != 1:

0 commit comments

Comments
 (0)