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

Matrices can be "constructed" from matrices of wrong dimensions #10793

Closed
vbraun opened this issue Feb 17, 2011 · 26 comments
Closed

Matrices can be "constructed" from matrices of wrong dimensions #10793

vbraun opened this issue Feb 17, 2011 · 26 comments

Comments

@vbraun
Copy link
Member

vbraun commented Feb 17, 2011

Let's make a matrix and use it to define a morphism:

sage: projection = matrix(ZZ,[[1,0,0],[0,1,0]])
sage: projection
[1 0 0]
[0 1 0]
sage: H = Hom(ZZ^3, ZZ^2)
sage: H(projection)
Free module morphism defined by the matrix
[1 0]
[0 0]
[1 0]
Domain: Ambient free module of rank 3 over the principal ideal domain ...
Codomain: Ambient free module of rank 2 over the principal ideal domain ...

As we see, the matrix of the morphism is very unlikely to be what it should be. Here is the source of the problem:

sage: projection.parent()
Full MatrixSpace of 2 by 3 dense matrices over Integer Ring
sage: M = MatrixSpace(ZZ, 3 , 2)
sage: M
Full MatrixSpace of 3 by 2 dense matrices over Integer Ring
sage: M(projection)
[1 0]
[0 0]
[1 0]

So the matrix space converts the input to the matrix no matter what (same with matrix command, but inside morphisms matrix spaces are used). I suppose this will work any time the number of entries in the original and in the destination is matching. I think that if one really wants to do it, then this one is very welcome to insert an explicit conversion of a matrix to a list and then back to a matrix, but the above should raise exceptions.

Apply trac_10793_bug_in_matrix_construction.patch, trac_10793_fixing_existing_bugs.patch

Depends on #11200
Depends on #11552

CC: @rbeezer @kcrisman

Component: linear algebra

Keywords: sd31

Author: Andrey Novoseltsev

Reviewer: Volker Braun

Merged: sage-4.7.2.alpha2

Issue created by migration from https://trac.sagemath.org/ticket/10793

@novoselt

This comment has been minimized.

@novoselt
Copy link
Member

comment:1

I have completely replaced the original Volker's description since the problem is unrelated to fan morphisms themselves.

Also I think that this is an extremely dangerous bug and will take the liberty to elevate its priority...

@novoselt novoselt assigned jasongrout and williamstein and unassigned aghitza Feb 17, 2011
@novoselt novoselt changed the title FanMorphism defined by matrices are dangerous Matrices can be "constructed" from matrices of wrong dimensions Feb 17, 2011
@jasongrout
Copy link
Member

comment:2

So it seems that the problem here is that in converting to an element of the matrix space, it views the entries of the matrix (or indeed, the entries of a nested list as well) as a flattened list:

sage: M
Full MatrixSpace of 3 by 2 dense matrices over Integer Ring
sage: M([[1,2,3],[4,5,6]])
[1 2]
[3 4]
[5 6]

So I guess you're saying that MatrixSpace shouldn't flatten the list, but instead should throw an error?

@jasongrout
Copy link
Member

comment:3

Around line 361 in matrix/matrix_space.py, we see:

        if isinstance(entries, (list, tuple)) and len(entries) > 0 and \
           sage.modules.free_module_element.is_FreeModuleElement(entries[0]):
            #Try to determine whether or not the entries should
            #be rows or columns
            if rows is None:
                #If the matrix is square, default to rows
                if self.__ncols == self.__nrows:
                    rows = True
                elif len(entries[0]) == self.__ncols:
                    rows = True
                elif len(entries[0]) == self.__nrows:
                    rows = False
                else:
                    raise ValueError, "incorrect dimensions"
            
            if self.__is_sparse:
                e = {}
                zero = self.base_ring()(0)
                for i in xrange(len(entries)):
                    for j, x in entries[i].iteritems():
                        if x != zero:
                            if rows:
                                e[(i,j)] = x
                            else:
                                e[(j,i)] = x
                entries = e
            else:
                entries = sum([v.list() for v in entries],[])

So:

  1. I think it tries to be too intelligent about guessing whether you have rows or columns. That leads to inconsistent behavior when you have code dealing with different matrix spaces with different dimensions.

  2. It indeed does flatten the list if all else fails (that's the sum([v.list()... line). I agree that that looks dangerous as well.

@novoselt
Copy link
Member

comment:4

Replying to @jasongrout:

So it seems that the problem here is that in converting to an element of the matrix space, it views the entries of the matrix (or indeed, the entries of a nested list as well) as a flattened list:

sage: M
Full MatrixSpace of 3 by 2 dense matrices over Integer Ring
sage: M([[1,2,3],[4,5,6]])
[1 2]
[3 4]
[5 6]

So I guess you're saying that MatrixSpace shouldn't flatten the list, but instead should throw an error?

Absolutely! I see no reason why such flattening can make sense, so even if there some - I think they should do it explicitly.

I find it a bit confusing that Sage uses right matrix action, so in the description I tend to think that projection itself is the matrix of the morphism, not its transpose. I suspect I am not the only one who can fall into this trap: it is accepted as an input, but some other not-very-related matrix is then used in the morphism.

@novoselt
Copy link
Member

comment:5

Hi Rob, this is the ticket I was talking about!

@novoselt
Copy link
Member

@novoselt
Copy link
Member

Author: Andrey Novoseltsev

@novoselt
Copy link
Member

Changed keywords from none to sd31

@novoselt
Copy link
Member

Work Issues: doctest failures

@novoselt
Copy link
Member

comment:8

There are doctest failures that have to be addressed. I suspect that all places that are affected were just kind of wrong. For Nx1 and 1xN matrices nothing too horrible is going on, hopefully there are no other cases.

@novoselt
Copy link
Member

comment:9

Here is the list of offenders:

	sage -t -long devel/sage-main/sage/crypto/mq/sr.py # 15 doctests failed
	sage -t -long devel/sage-main/sage/interfaces/sage0.py # 2 doctests failed
	sage -t -long devel/sage-main/sage/crypto/mq/mpolynomialsystem.py # 50 doctests failed
	sage -t -long devel/sage-main/sage/geometry/fan_morphism.py # 4 doctests failed
	sage -t -long devel/sage-main/sage/rings/polynomial/multi_polynomial_sequence.py # 33 doctests failed
	sage -t -long devel/sage-main/sage/categories/map.pyx # 16 doctests failed
	sage -t -long devel/sage-main/sage/interfaces/magma.py # 2 doctests failed
	sage -t -long devel/sage-main/sage/modules/matrix_morphism.py # 9 doctests failed

IMHO, all these files are full of bugs!

@novoselt
Copy link
Member

comment:10

OK, it turned out to be not as scary as it seemed to me originally. Most of the errors were due to forgetting that matrices act from the right. In crypto I have added explicit conversion of matrices to lists since it seems that they wanted it. I don't understand that code to make sure that this is indeed correct, but at the very least I didn't introduce a new bug ;-)

@novoselt
Copy link
Member

Changed work issues from doctest failures to none

@novoselt
Copy link
Member

novoselt commented Jul 6, 2011

comment:11

Patches should apply fine on top of sage-4.7.1.alpha4, as #11200 (and other patches modifying fan morphisms) was merged.

@novoselt
Copy link
Member

novoselt commented Jul 6, 2011

Dependencies: #11200

@vbraun

This comment has been minimized.

@vbraun
Copy link
Member Author

vbraun commented Aug 14, 2011

comment:13

I totally forgot that we haven't merged this ticket yet. Applies fine on Sage-4.7.1.rc2. Positive review.

@vbraun
Copy link
Member Author

vbraun commented Aug 14, 2011

Reviewer: Volker Braun

@jdemeyer
Copy link

comment:14

This patch conflicts with #11552. Since the latter is older, I propose that this patch is rebased to #11552.

@novoselt
Copy link
Member

comment:15

Attachment: trac_10793_fixing_existing_bugs.patch.gz

Rebased on top of #11552, needs review.

@novoselt
Copy link
Member

Changed dependencies from #11200 to #11200, #11552

@rbeezer
Copy link
Mannequin

rbeezer mannequin commented Aug 17, 2011

comment:16

I think that if a rebase is straightforward (ie there is no real change to code), then it does not need to be reviewed. I can't tell here, since the rebased patch replaced the old patch.

No matter - I've applied it and done minimal tests. All looks good. I'm running all tests right now and then will flip to positive review.

I'm glad this got fixed. While doing work on free module morphisms I ran into this frequently, where domains and codomains got confused, and so on. I'm surprised it survived this long. Thanks Volker and Andrey for the fix!

Rob

@novoselt
Copy link
Member

comment:17

It was straightforward, your patch added spaces after commas in two lines that I have altered...

@rbeezer
Copy link
Mannequin

rbeezer mannequin commented Aug 18, 2011

comment:18

Sorry. ;-) Thanks for the rebase. Passes all tests.

Rob

@jdemeyer
Copy link

Merged: sage-4.7.2.alpha2

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

No branches or pull requests

6 participants