Skip to content

Commit

Permalink
Merge pull request #26598 from haru-44/minmax
Browse files Browse the repository at this point in the history
Rewriting to `min` and `max` functions
  • Loading branch information
smichr committed May 15, 2024
2 parents 031fb01 + 02595bb commit 521193b
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 52 deletions.
11 changes: 1 addition & 10 deletions sympy/categories/diagram_drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1058,16 +1058,7 @@ def _sequential_layout(diagram, merged_morphisms):
# graph of ``merged_morphisms``.
adjlists = DiagramGrid._get_undirected_graph(objects, merged_morphisms)

# Find an object with the minimal degree. This is going to be
# the root.
root = sorted_objects[0]
mindegree = len(adjlists[root])
for obj in sorted_objects:
current_degree = len(adjlists[obj])
if current_degree < mindegree:
root = obj
mindegree = current_degree

root = min(sorted_objects, key=lambda x: len(adjlists[x]))
grid = _GrowableGrid(1, 1)
grid[0, 0] = root

Expand Down
20 changes: 8 additions & 12 deletions sympy/combinatorics/permutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2127,7 +2127,7 @@ def descents(self):
pos = [i for i in range(len(a) - 1) if a[i] > a[i + 1]]
return pos

def max(self):
def max(self) -> int:
"""
The maximum element moved by the permutation.
Expand All @@ -2144,14 +2144,12 @@ def max(self):
min, descents, ascents, inversions
"""
max = 0
a = self.array_form
for i in range(len(a)):
if a[i] != i and a[i] > max:
max = a[i]
return max
if not a:
return 0
return max(_a for i, _a in enumerate(a) if _a != i)

def min(self):
def min(self) -> int:
"""
The minimum element moved by the permutation.
Expand All @@ -2169,11 +2167,9 @@ def min(self):
max, descents, ascents, inversions
"""
a = self.array_form
min = len(a)
for i in range(len(a)):
if a[i] != i and a[i] < min:
min = a[i]
return min
if not a:
return 0
return min(_a for i, _a in enumerate(a) if _a != i)

def inversions(self):
"""
Expand Down
11 changes: 3 additions & 8 deletions sympy/holonomic/holonomic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1177,8 +1177,7 @@ def degree(self):
"""
Returns the highest power of `x` in the annihilator.
"""
sol = [i.degree() for i in self.annihilator.listofpoly]
return max(sol)
return max(i.degree() for i in self.annihilator.listofpoly)

def composition(self, expr, *args, **kwargs):
"""
Expand Down Expand Up @@ -1764,15 +1763,11 @@ def _pole_degree(poly):
else:
return 0

degree = [j.degree() for j in list_coeff]
degree = max(degree)
degree = max(j.degree() for j in list_coeff)
inf = 10 * (max(1, degree) + max(1, self.annihilator.order))

deg = lambda q: inf if q.is_zero else _pole_degree(q)
b = deg(list_coeff[0])

for j in range(1, len(list_coeff)):
b = min(b, deg(list_coeff[j]) - j)
b = min(deg(q) - j for j, q in enumerate(list_coeff))

for i, j in enumerate(list_coeff):
listofdmp = j.all_coeffs()
Expand Down
10 changes: 1 addition & 9 deletions sympy/solvers/diophantine/diophantine.py
Original file line number Diff line number Diff line change
Expand Up @@ -836,15 +836,7 @@ def unpack_sol(sol):
if not any(coeff[i**2] for i in var):
if coeff[x*z]:
sols = diophantine(coeff[x*y]*x + coeff[y*z]*z - x*z)
s = sols.pop()
min_sum = abs(s[0]) + abs(s[1])

for r in sols:
m = abs(r[0]) + abs(r[1])
if m < min_sum:
s = r
min_sum = m

s = min(sols, key=lambda r: abs(r[0]) + abs(r[1]))
result.add(_remove_gcd(s[0], -coeff[x*z], s[1]))
return result

Expand Down
14 changes: 1 addition & 13 deletions sympy/solvers/simplex.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,20 +149,8 @@ def _pivot(M, i, j):

def _choose_pivot_row(A, B, candidate_rows, pivot_col, Y):
# Choose row with smallest ratio
first_row = candidate_rows[0]
min_ratio = B[first_row] / A[first_row, pivot_col]
min_rows = [first_row]
for i in candidate_rows[1:]:
ratio = B[i] / A[i, pivot_col]
if ratio < min_ratio:
min_ratio = ratio
min_rows = [i]
elif ratio == min_ratio:
min_rows.append(i)

# If there are ties, pick using Bland's rule
_, row = min((Y[i], i) for i in min_rows)
return row
return min(candidate_rows, key=lambda i: (B[i] / A[i, pivot_col], Y[i]))


def _simplex(A, B, C, D=None, dual=False):
Expand Down

0 comments on commit 521193b

Please sign in to comment.