Skip to content

Commit

Permalink
Various NumPy 2.0 __repr__ / type related changes.
Browse files Browse the repository at this point in the history
I may walk back some of these, need to read more about the
__repr__ change.
  • Loading branch information
dhermes committed Jun 18, 2024
1 parent f1c4ce4 commit 5f15b14
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 39 deletions.
2 changes: 1 addition & 1 deletion docs/algorithms/curve-curve-intersection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Curve-Curve Intersection
return -np.inf
_, result = np.frexp(value)
# Shift [1/2, 1) --> [1, 2) borrows one from exponent
return result - 1
return int(result - 1)

The problem of intersecting two curves is a difficult one
in computational geometry. The :meth:`.Curve.intersect` method (when using
Expand Down
6 changes: 3 additions & 3 deletions src/python/bezier/curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def binary_exponent(value):
return -np.inf
_, result = np.frexp(value)
# Shift [1/2, 1) --> [1, 2) borrows one from exponent
return result - 1
return int(result - 1)
"""

import numpy as np
Expand Down Expand Up @@ -764,10 +764,10 @@ def specialize(self, start, end):
>>> left, right = curve.subdivide()
>>> also_left = curve.specialize(0.0, 0.5)
>>> np.all(also_left.nodes == left.nodes)
>>> bool(np.all(also_left.nodes == left.nodes))
True
>>> also_right = curve.specialize(0.5, 1.0)
>>> np.all(also_right.nodes == right.nodes)
>>> bool(np.all(also_right.nodes == right.nodes))
True
Args:
Expand Down
6 changes: 3 additions & 3 deletions src/python/bezier/hazmat/algebraic_intersection.py
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,7 @@ def lu_companion(top_row, value):
array([[ 1. , -0.5, 0. ],
[ 0. , 1. , -0.5],
[-3.5, 1. , -2. ]])
>>> np.linalg.norm(a_mat, ord=1)
>>> float(np.linalg.norm(a_mat, ord=1))
4.5
Args:
Expand All @@ -999,7 +999,7 @@ def lu_companion(top_row, value):
lu_mat = np.zeros((degree, degree), order="F")
if degree == 1:
lu_mat[0, 0] = top_row[0] - value
return lu_mat, abs(lu_mat[0, 0])
return lu_mat, float(abs(lu_mat[0, 0]))

# Column 0: Special case since it doesn't have ``-t`` above the diagonal.
horner_curr = top_row[0] - value
Expand All @@ -1022,7 +1022,7 @@ def lu_companion(top_row, value):
one_norm = max(one_norm, abs(value) + abs(curr_coeff))
lu_mat[last_row - 1, last_row] = -value
lu_mat[last_row, last_row] = horner_curr
return lu_mat, one_norm
return lu_mat, float(one_norm)


def _reciprocal_condition_number(lu_mat, one_norm):
Expand Down
4 changes: 2 additions & 2 deletions src/python/bezier/hazmat/clipping.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def compute_implicit_line(nodes):
coeff_b = delta[0]
# c = - ax - by = delta[1] x - delta[0] y
coeff_c = delta[1] * nodes[0, 0] - delta[0] * nodes[1, 0]
return coeff_a, coeff_b, coeff_c
return float(coeff_a), float(coeff_b), float(coeff_c)


def compute_fat_line(nodes):
Expand Down Expand Up @@ -172,7 +172,7 @@ def compute_fat_line(nodes):
d_min = curr_dist
elif curr_dist > d_max:
d_max = curr_dist
return coeff_a, coeff_b, coeff_c, d_min, d_max
return coeff_a, coeff_b, coeff_c, float(d_min), float(d_max)


def _update_parameters(s_min, s_max, start0, end0, start1, end1):
Expand Down
16 changes: 8 additions & 8 deletions src/python/bezier/hazmat/curve_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ def get_curvature(nodes, tangent_vec, s):
)
# NOTE: We convert to 1D to make sure NumPy uses vector norm.
curvature /= np.linalg.norm(tangent_vec[:, 0], ord=2) ** 3
return curvature
return float(curvature)


def newton_refine(nodes, point, s):
Expand Down Expand Up @@ -765,22 +765,22 @@ def newton_refine(nodes, point, s):
array([[0.],
[0.]])
>>> s_vals = [0.625, None, None, None, None, None]
>>> np.log2(abs(expected - s_vals[0]))
>>> float(np.log2(abs(expected - s_vals[0])))
-3.0
>>> s_vals[1] = newton_refine(nodes, point, s_vals[0])
>>> np.log2(abs(expected - s_vals[1]))
>>> float(np.log2(abs(expected - s_vals[1])))
-3.983...
>>> s_vals[2] = newton_refine(nodes, point, s_vals[1])
>>> np.log2(abs(expected - s_vals[2]))
>>> float(np.log2(abs(expected - s_vals[2])))
-4.979...
>>> s_vals[3] = newton_refine(nodes, point, s_vals[2])
>>> np.log2(abs(expected - s_vals[3]))
>>> float(np.log2(abs(expected - s_vals[3])))
-5.978...
>>> s_vals[4] = newton_refine(nodes, point, s_vals[3])
>>> np.log2(abs(expected - s_vals[4]))
>>> float(np.log2(abs(expected - s_vals[4])))
-6.978...
>>> s_vals[5] = newton_refine(nodes, point, s_vals[4])
>>> np.log2(abs(expected - s_vals[5]))
>>> float(np.log2(abs(expected - s_vals[5])))
-7.978...
.. testcleanup:: newton-refine-curve-cusp
Expand Down Expand Up @@ -842,7 +842,7 @@ def newton_refine(nodes, point, s):
delta_s = np.vdot(pt_delta[:, 0], derivative[:, 0]) / np.vdot(
derivative[:, 0], derivative[:, 0]
)
return s + delta_s
return float(s + delta_s)


def locate_point(nodes, point):
Expand Down
4 changes: 2 additions & 2 deletions src/python/bezier/hazmat/geometric_intersection.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def linearization_error(nodes):
# max_{0 <= s <= 1} s(1 - s)/2 = 1/8 = 0.125
multiplier = 0.125 * degree * (degree - 1)
# NOTE: worst_case is 1D due to np.max(), so this is the vector norm.
return multiplier * np.linalg.norm(worst_case, ord=2)
return float(multiplier * np.linalg.norm(worst_case, ord=2))


def segment_intersection(start0, end0, start1, end1):
Expand Down Expand Up @@ -408,7 +408,7 @@ def segment_intersection(start0, end0, start1, end1):
start_delta = start1 - start0
s = _py_helpers.cross_product(start_delta, delta1) / cross_d0_d1
t = _py_helpers.cross_product(start_delta, delta0) / cross_d0_d1
return s, t, True
return float(s), float(t), True


def parallel_lines_parameters(start0, end0, start1, end1):
Expand Down
34 changes: 17 additions & 17 deletions src/python/bezier/hazmat/intersection_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,16 +187,16 @@ def realroots(*coeffs):
>>> expected, = realroots(28, -30, 9, -1)
>>> s_vals = [0.625, None, None, None, None]
>>> t = 0.625
>>> np.log2(abs(expected - s_vals[0]))
>>> float(np.log2(abs(expected - s_vals[0])))
-4.399...
>>> s_vals[1], t = newton_refine(s_vals[0], nodes1, t, nodes2)
>>> np.log2(abs(expected - s_vals[1]))
>>> float(np.log2(abs(expected - s_vals[1])))
-7.901...
>>> s_vals[2], t = newton_refine(s_vals[1], nodes1, t, nodes2)
>>> np.log2(abs(expected - s_vals[2]))
>>> float(np.log2(abs(expected - s_vals[2])))
-16.010...
>>> s_vals[3], t = newton_refine(s_vals[2], nodes1, t, nodes2)
>>> np.log2(abs(expected - s_vals[3]))
>>> float(np.log2(abs(expected - s_vals[3])))
-32.110...
>>> s_vals[4], t = newton_refine(s_vals[3], nodes1, t, nodes2)
>>> np.allclose(s_vals[4], expected, rtol=6 * machine_eps, atol=0.0)
Expand Down Expand Up @@ -229,22 +229,22 @@ def realroots(*coeffs):
>>> expected = 0.5
>>> s_vals = [0.375, None, None, None, None, None]
>>> t = 0.375
>>> np.log2(abs(expected - s_vals[0]))
>>> float(np.log2(abs(expected - s_vals[0])))
-3.0
>>> s_vals[1], t = newton_refine(s_vals[0], nodes1, t, nodes2)
>>> np.log2(abs(expected - s_vals[1]))
>>> float(np.log2(abs(expected - s_vals[1])))
-4.0
>>> s_vals[2], t = newton_refine(s_vals[1], nodes1, t, nodes2)
>>> np.log2(abs(expected - s_vals[2]))
>>> float(np.log2(abs(expected - s_vals[2])))
-5.0
>>> s_vals[3], t = newton_refine(s_vals[2], nodes1, t, nodes2)
>>> np.log2(abs(expected - s_vals[3]))
>>> float(np.log2(abs(expected - s_vals[3])))
-6.0
>>> s_vals[4], t = newton_refine(s_vals[3], nodes1, t, nodes2)
>>> np.log2(abs(expected - s_vals[4]))
>>> float(np.log2(abs(expected - s_vals[4])))
-7.0
>>> s_vals[5], t = newton_refine(s_vals[4], nodes1, t, nodes2)
>>> np.log2(abs(expected - s_vals[5]))
>>> float(np.log2(abs(expected - s_vals[5])))
-8.0
.. testcleanup:: newton-refine3
Expand Down Expand Up @@ -276,12 +276,12 @@ def realroots(*coeffs):
.. doctest:: newton-refine3-continued
>>> s1 = t1 = 0.5 - 0.5**27
>>> np.log2(0.5 - s1)
>>> float(np.log2(0.5 - s1))
-27.0
>>> s2, t2 = newton_refine(s1, nodes1, t1, nodes2)
>>> s2 == t2
True
>>> np.log2(0.5 - s2)
>>> float(np.log2(0.5 - s2))
-28.0
>>> s3, t3 = newton_refine(s2, nodes1, t2, nodes2)
>>> s3 == t3 == s2
Expand Down Expand Up @@ -364,24 +364,24 @@ def modified_update(s, t):
RHS = helpers.matrix_product(DG_t, minus_G)
delta_params = np.linalg.solve(LHS, RHS)
delta_s, delta_t = delta_params.flatten()
return s + delta_s, t + delta_t
return float(s + delta_s), float(t + delta_t)
.. doctest:: newton-refine4
>>> s0, t0 = 0.375, 0.375
>>> np.log2(0.5 - s0)
>>> float(np.log2(0.5 - s0))
-3.0
>>> s1, t1 = modified_update(s0, t0)
>>> s1 == t1
True
>>> 1040.0 * s1
519.0
>>> np.log2(0.5 - s1)
>>> float(np.log2(0.5 - s1))
-10.022...
>>> s2, t2 = modified_update(s1, t1)
>>> s2 == t2
True
>>> np.log2(0.5 - s2)
>>> float(np.log2(0.5 - s2))
-31.067...
>>> s3, t3 = modified_update(s2, t2)
>>> s3 == t3 == 0.5
Expand Down Expand Up @@ -417,7 +417,7 @@ def modified_update(s, t):
if singular:
raise ValueError("Jacobian is singular.")

return s + delta_s, t + delta_t
return float(s + delta_s), float(t + delta_t)


class NewtonSimpleRoot: # pylint: disable=too-few-public-methods
Expand Down
4 changes: 2 additions & 2 deletions src/python/bezier/hazmat/triangle_intersection.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def newton_refine(nodes, degree, x_val, y_val, s, t):
>>> triangle.is_valid
True
>>> (x_val,), (y_val,) = triangle.evaluate_cartesian(0.25, 0.5)
>>> x_val, y_val
>>> float(x_val), float(y_val)
(1.25, 1.25)
>>> s, t = 0.5, 0.25
>>> new_s, new_t = newton_refine(nodes, 2, x_val, y_val, s, t)
Expand Down Expand Up @@ -215,7 +215,7 @@ def newton_refine(nodes, degree, x_val, y_val, s, t):
delta_s, delta_t = newton_refine_solve(
jac_both, x_val, triangle_x, y_val, triangle_y
)
return s + delta_s, t + delta_t
return float(s + delta_s), float(t + delta_t)


def update_locate_candidates(candidate, next_candidates, x_val, y_val, degree):
Expand Down
2 changes: 1 addition & 1 deletion src/python/bezier/triangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ def _compute_valid(self):
self._degree, supported=(1, 2, 3)
)

return poly_sign == 1
return bool(poly_sign == 1)

@property
def is_valid(self):
Expand Down

0 comments on commit 5f15b14

Please sign in to comment.