Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve handling of strings supplied as matrix entries #39505

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

DaveWitteMorris
Copy link
Member

Fixes #34821. The construction matrix(ZZ, nrows=2, entries=["12", "12"]) is ambiguous, because the string "12" could be interpreted either as the single integer 12 or as a sequence of two single-digit integers: that is, entries could be interpreted as either [12, 12] or [[1,2], [1,2]]. To avoid this problem, the sequence_type method of MatrixArgs (in sage.matrix.args.pyx) does not allow strings as the entries of certain matrices with only one column (and in some other situations):

# Blacklist strings, we don't want them to be considered a sequence
        return MA_ENTRIES_UNKNOWN

However, this special treatment of one-column matrices results in inconsistent (and very puzzling) behaviour, such as the following:

# current behaviour
sage: matrix(ZZ, 1,2, ["1", "2"])
[1 2]
sage: matrix(ZZ, 2,1, ["1", "2"])
TypeError: unable to convert ['1', '2'] to a matrix

This PR eliminates the inconsistency, by interpreting the string as a scalar (i.e., an element of the ring) in this situation, which is the same interpretation as when there is more than one column.

# after this PR
sage: matrix(ZZ, 2,1, ["1", "2"])
[1]
[2]

The PR also makes a similar change in the get_type method, in order to eliminate the following TypeError:

# current behaviour
sage: matrix(ZZ, 2, 2, 3)
[3 0]
[0 3]
sage: matrix(ZZ, 2, 2, "3")
TypeError: unable to convert '3' to a matrix

Associated doctests are added/updated.

📝 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

Copy link

github-actions bot commented Feb 12, 2025

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

@orlitzky
Copy link
Contributor

It's crazy to accept strings at all, but better to be consistently crazy in that case Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create single-column matrix over polynomial ring containing monomials
2 participants