Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Le'ts runn this by hand

sage: A=random_matrix(QQ,2^5,2^5)
sage: B=random_matrix(QQ,2^5,2^5)

Note that

sage: A.parent()
Full MatrixSpace of 32 by 32 dense matrices over Rational Field

sage: n=A.nrows()
sage: C=matrix(n,n)

Note that

sage: C.parent() Full MatrixSpace of 32 by 32 dense matrices over Integer Ring

So all elements of C are defined as integers. But this cannot be satisfied if any of the elements of A*B cannoit be coerced to an integer. In our case (and using Sage's matrix product to be a bit faster) :

sage: any(map(lambda u:not(u.is_integer()), (A*B).list()))
True

There you have it : at least one element of AB is not an integer. Note that *some elements of A*B might be integers :

sage: any(map(lambda u:u.is_integer(), (A*B).list()))
True

A (relativelty) fixed version of your function might be :

def Produit(A,B):
    """ Product of two matrices A and B
    Works if and only if A and B have the same parent"""
    n=A.nrows()
    C=matrix(A.parent(), n,n)
    for i in [0..n-1]:
        for j in [0..n-1]:
            for k in [0..n-1]:
                C[i,j]=C[i,j]+A[i,k]*B[k,j]
    return C

HTH,