Skip to content

Prevent order mismatch in words function#41197

Merged
vbraun merged 18 commits intosagemath:developfrom
cxzhong:patch-4
Feb 1, 2026
Merged

Prevent order mismatch in words function#41197
vbraun merged 18 commits intosagemath:developfrom
cxzhong:patch-4

Conversation

@cxzhong
Copy link
Copy Markdown
Contributor

@cxzhong cxzhong commented Nov 18, 2025

Updated error handling for morphism composition to provide clearer messages regarding codomain and domain compatibility.
Fix #40391
I think it may be strict in mathematical definition. But it is useful to prevent order mismatch. to get some error results in this issue.
And for Words("ab"), we think b>a, for Words("ba"), we think a>b, so they are different order in sage's words system.
We prevent words order mismatch behavior to output some mathematical incorrect output

📝 Checklist

  • The title is concise and informative.
  • The description explains in detail what this PR is about.
  • I have linked a relevant issue or discussion.
  • I have created tests covering the changes.
  • I have updated the documentation and checked the documentation preview.

⌛ Dependencies

@cxzhong cxzhong requested a review from Copilot November 18, 2025 14:36
@cxzhong cxzhong changed the title Added domain/codomain validation in the __mul__ method Added domain/codomain validation in the __mul__ method in words Nov 18, 2025
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 1 out of 1 changed files in this pull request and generated 7 comments.

Comments suppressed due to low confidence (1)

src/sage/combinat/words/morphism.py:893

  • This method raises ValueError - should raise an ArithmeticError or return NotImplemented instead.
    This method raises ValueError - should raise an ArithmeticError or return NotImplemented instead.
    This method raises ValueError - should raise an ArithmeticError or return NotImplemented instead.
    def __mul__(self, other):

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/sage/combinat/words/morphism.py Outdated
Comment thread src/sage/combinat/words/morphism.py Outdated
Comment thread src/sage/combinat/words/morphism.py Outdated
Comment thread src/sage/combinat/words/morphism.py Outdated
Comment thread src/sage/combinat/words/morphism.py Outdated
Comment thread src/sage/combinat/words/morphism.py Outdated
Comment thread src/sage/combinat/words/morphism.py Outdated
@cxzhong cxzhong closed this Nov 19, 2025
@cxzhong cxzhong reopened this Nov 19, 2025
@cxzhong cxzhong changed the title Added domain/codomain validation in the __mul__ method in words Prevent order mismatch in words function Nov 19, 2025
@cxzhong cxzhong requested review from dimpase and orlitzky November 19, 2025 07:47
@cxzhong
Copy link
Copy Markdown
Contributor Author

cxzhong commented Nov 19, 2025

We should put the _is_alphabet_included_with_order logic in this or the alphabet file?

cxzhong and others added 11 commits December 3, 2025 21:17
Updated error handling for morphism composition to provide clearer messages regarding codomain and domain compatibility.
Refactor composition validation logic in morphism.py.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Clarify error message for morphism order mismatch.
Updated error messages to provide clearer feedback when the codomain alphabet is not included in the domain alphabet with the correct ordering.
@github-actions
Copy link
Copy Markdown

github-actions bot commented Dec 7, 2025

Documentation preview for this PR (built with commit 6680870; changes) is ready! 🎉
This preview will update shortly after each push to this PR.

@cxzhong
Copy link
Copy Markdown
Contributor Author

cxzhong commented Dec 24, 2025

@videlec Can you help me review this?

@videlec
Copy link
Copy Markdown
Contributor

videlec commented Dec 26, 2025

It feels like this logic should be higher up in the category hierarchy. Namely, it sounds like you are checking whether a given poset (which here is totally ordered) is a sub-poset of another one. There could be a method in TotallyOrderedFiniteSet, for example TotallyOrderedFiniteSet.is_totally_ordered_subset? But that would not suffice for something like Words(ZZ).

Another argument to think about a more generic place to have such implementation is that specialized classes could implement faster checks. One very common use case is an alphabet which is a subset of either roman letters or integers (with the induced orders).

Comment thread src/sage/combinat/words/morphism.py Outdated
source_list = list(source_alphabet)
target_list = list(target_alphabet)

if not all(a in target_alphabet for a in source_list):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test would be much faster if target_alphabet was a set.
Since you also need the positions in the alphabet, you should directly define the mapping target_positions and use it here instead of target_alphabet.

Comment thread src/sage/combinat/words/morphism.py Outdated
source_positions = [target_positions[letter] for letter in source_list]

# Check if positions are in increasing order
return all(source_positions[i] < source_positions[i+1]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could also be return all(p < q for p, q in zip(source_positions, source_positions[1:]))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be less efficient though as source_positions[1:] makes a copy.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right.
Another proposal to check that source_alphabet is a subset of target_alphabet and that the elements in source_alphabet appear in the same order in target_alphabet is:

        targets = list(target_alphabet)
        n_targets = len(targets)
        idx = 0
        for a in source_alphabet:
            while idx < n_targets and targets[idx] != a:
                idx += 1
            if idx == n_targets:
                return False
        return True

def is_self_composable(self):
r"""
Return whether the codomain of ``self`` is contained in the domain.
Return whether the codomain of ``self`` is contained in the domain
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check whether...

Comment thread src/sage/combinat/words/morphism.py Outdated

def _is_alphabet_included_with_order(self, source_alphabet, target_alphabet):
"""
Check if ``source_alphabet`` is included in ``target_alphabet`` with the correct ordering.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ensure alignment on 80 columns.

Comment thread src/sage/combinat/words/morphism.py Outdated
Traceback (most recent call last):
...
KeyError: 'b'
ValueError: the codomain alphabet of the second morphism must be included in the domain alphabet of the first morphism with the same ordering
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can align on 80 columns

Copy link
Copy Markdown
Collaborator

@dcoudert dcoudert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@dcoudert
Copy link
Copy Markdown
Collaborator

@videlec, let us know if you prefer another solution.

@videlec
Copy link
Copy Markdown
Contributor

videlec commented Jan 26, 2026

@videlec, let us know if you prefer another solution.

I think this is not the long-term way to solve the issue. However it does improve the situation! I definitely agree with the positive review.

vbraun pushed a commit to vbraun/sage that referenced this pull request Jan 30, 2026
sagemathgh-41197: Prevent order mismatch in words function
    
Updated error handling for morphism composition to provide clearer
messages regarding codomain and domain compatibility.
Fix sagemath#40391
I think it may be strict in mathematical definition. But it is useful to
prevent order mismatch. to get some error results in this issue.
And for ``Words("ab")``, we think ``b>a``, for ``Words("ba")``, we think
``a>b``, so they are different order in sage's words system.
We prevent words order mismatch behavior to output some mathematical
incorrect output
<!-- ^ Please provide a concise and informative title. -->
<!-- ^ Don't put issue numbers in the title, do this in the PR
description below. -->
<!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method
to calculate 1 + 2". -->
<!-- v Describe your changes below in detail. -->
<!-- v Why is this change required? What problem does it solve? -->
<!-- v If this PR resolves an open issue, please link to it here. For
example, "Fixes sagemath#12345". -->



### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [ ] The title is concise and informative.
- [ ] The description explains in detail what this PR is about.
- [ ] I have linked a relevant issue or discussion.
- [ ] I have created tests covering the changes.
- [ ] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->
    
URL: sagemath#41197
Reported by: Chenxin Zhong
Reviewer(s): Copilot, David Coudert, Vincent Delecroix
@vbraun vbraun merged commit d4948b9 into sagemath:develop Feb 1, 2026
22 of 24 checks passed
@cxzhong cxzhong deleted the patch-4 branch February 2, 2026 05:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

contradiction in the behavior of WordMorphism

5 participants