|
29 | 29 | # https://www.gnu.org/licenses/ |
30 | 30 | # **************************************************************************** |
31 | 31 | from collections.abc import Iterator |
| 32 | +from math import acos, cos, pi, sin |
32 | 33 | from typing import Self |
33 | | -from math import acos, cos, sin, pi |
34 | 34 |
|
35 | 35 | from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets |
36 | | -from sage.combinat.binary_tree import from_tamari_sorting_tuple, BinaryTree |
| 36 | +from sage.combinat.binary_tree import BinaryTree, from_tamari_sorting_tuple |
37 | 37 | from sage.combinat.dyck_word import DyckWords |
38 | 38 | from sage.combinat.integer_lists.invlex import IntegerListsLex |
39 | | -from sage.combinat.interval_posets import (TamariIntervalPoset, |
40 | | - TamariIntervalPosets) |
41 | | -from sage.combinat.ordered_tree import OrderedTree, LabelledOrderedTree |
| 39 | +from sage.combinat.interval_posets import TamariIntervalPoset, TamariIntervalPosets |
| 40 | +from sage.combinat.ordered_tree import LabelledOrderedTree, OrderedTree |
42 | 41 | from sage.misc.inherit_comparison import InheritComparisonClasscallMetaclass |
43 | 42 | from sage.misc.latex import latex |
44 | 43 | from sage.misc.lazy_attribute import lazy_attribute |
45 | | -from sage.misc.prandom import uniform, randrange |
| 44 | +from sage.misc.prandom import randrange, uniform |
46 | 45 | from sage.plot.arc import arc |
47 | 46 | from sage.plot.arrow import arrow2d |
48 | 47 | from sage.plot.bezier_path import bezier_path |
@@ -1473,10 +1472,9 @@ def aux(tree, isroot=False): |
1473 | 1472 | # at the root: one bud at the beginning or the end |
1474 | 1473 | if len(idx) != 1 or (idx[0] != 0 and idx[0] != len(tree) - 1): |
1475 | 1474 | return False |
1476 | | - else: |
1477 | | - # otherwise: two buds consecutive |
1478 | | - if len(idx) != 2 or idx[1] - idx[0] != 1: |
1479 | | - return False |
| 1475 | + # otherwise: two buds consecutive |
| 1476 | + elif len(idx) != 2 or idx[1] - idx[0] != 1: |
| 1477 | + return False |
1480 | 1478 | for st in tree: |
1481 | 1479 | if st and not aux(st): # an internal node failing the test |
1482 | 1480 | return False |
@@ -1577,7 +1575,7 @@ def gen_comb(n: int, k: int) -> list[int]: |
1577 | 1575 | return s |
1578 | 1576 |
|
1579 | 1577 | @staticmethod |
1580 | | - def cutting(cardlist: list[float], size: int) -> list[(float, int)]: |
| 1578 | + def cutting(cardlist: list[float], size: int) -> list[tuple[float, int]]: |
1581 | 1579 | """ |
1582 | 1580 | Utility function to generate the cutting ratio list according to a |
1583 | 1581 | list of (relative) count of objects of sizes from ``0`` to ``size``. |
@@ -1621,7 +1619,7 @@ def cutting(cardlist: list[float], size: int) -> list[(float, int)]: |
1621 | 1619 | # check list size |
1622 | 1620 | if len(cardlist) != size + 1: |
1623 | 1621 | raise ValueError("invalid parameter: l does not have correct size.") |
1624 | | - cutting: list[(float, int)] = [] |
| 1622 | + cutting: list[tuple[float, int]] = [] |
1625 | 1623 | for i in range(size + 1): |
1626 | 1624 | cutting.append((cardlist[i] * cardlist[size - i], i)) |
1627 | 1625 | # sort with decreasing probability for efficient random generation |
@@ -2004,13 +2002,12 @@ def __path_to_tree(path: list[int]) -> TamariBlossomingTree: |
2004 | 2002 | for step in path: |
2005 | 2003 | if step == 3: # new node |
2006 | 2004 | stack.append([0, []]) |
2007 | | - else: # depending on type |
2008 | | - if stack[-1][0] < 2: # add bud |
2009 | | - stack[-1][0] += 1 |
2010 | | - stack[-1][1].append([]) |
2011 | | - else: # subtree completed |
2012 | | - subtree = stack.pop()[1] |
2013 | | - stack[-1][1].append(subtree) |
| 2005 | + elif stack[-1][0] < 2: # add bud |
| 2006 | + stack[-1][0] += 1 |
| 2007 | + stack[-1][1].append([]) |
| 2008 | + else: # subtree completed |
| 2009 | + subtree = stack.pop()[1] |
| 2010 | + stack[-1][1].append(subtree) |
2014 | 2011 | # Get the tree (list of subtrees for the moment) |
2015 | 2012 | stack = stack[-1][1][0] |
2016 | 2013 | # pop the last bud, which is always the last child |
@@ -2311,14 +2308,13 @@ def random_element(self) -> TamariBlossomingTree: |
2311 | 2308 | for step in path: |
2312 | 2309 | if step == 2: # new nodes |
2313 | 2310 | stack.append([0, []]) |
2314 | | - else: # depending on type |
2315 | | - if stack[-1][0] == 0: # add two buds |
2316 | | - stack[-1][0] = 1 |
2317 | | - stack[-1][1].append([]) |
2318 | | - stack[-1][1].append([]) |
2319 | | - else: # subtree completed |
2320 | | - subtree = stack.pop()[1] |
2321 | | - stack[-1][1].append(subtree) |
| 2311 | + elif stack[-1][0] == 0: # add two buds |
| 2312 | + stack[-1][0] = 1 |
| 2313 | + stack[-1][1].append([]) |
| 2314 | + stack[-1][1].append([]) |
| 2315 | + else: # subtree completed |
| 2316 | + subtree = stack.pop()[1] |
| 2317 | + stack[-1][1].append(subtree) |
2322 | 2318 | tree = stack[-1][1] |
2323 | 2319 | tree.append([]) # add the extra bud besides the root |
2324 | 2320 | return TamariBlossomingTree._from_plane_tree(tree, skip_check=True, |
|
0 commit comments