Matrix.inv_mod gives multiplicative inverse in python2.7 OSX but not in python3.5 WIN #12019

Open
chartl opened this Issue Jan 4, 2017 · 0 comments

Projects

None yet

1 participant

@chartl
chartl commented Jan 4, 2017 edited

Note: The sympy versions are different as well, with the newer version demonstrating the (apparent?) error

import numpy as np
from sympy import Matrix

# works

F = np.array([[2, 7, 1, 4], [7, 2, 7, 1], [4, 7, 2, 7], [1, 4, 7, 2]], dtype=int)
G = np.array([[int(c) for c in r] for r in Matrix(F).inv_mod(11).tolist()], dtype=int)
np.dot(G, F) % 11

# doesn't
X = np.array(
[[2, 8, 17, 2, 20, 12, 5, 4, 17, 4, 13, 2],
 [4, 2, 8, 17, 2, 20, 12, 5, 4, 17, 4, 13],
 [2, 4, 2, 8, 17, 2, 20, 12, 5, 4, 17, 4],
 [13, 2, 4, 2, 8, 17, 2, 20, 12, 5, 4, 17],
 [4, 13, 2, 4, 2, 8, 17, 2, 20, 12, 5, 4],
 [17, 4, 13, 2, 4, 2, 8, 17, 2, 20, 12, 5],
 [4, 17, 4, 13, 2, 4, 2, 8, 17, 2, 20, 12],
 [5, 4, 17, 4, 13, 2, 4, 2, 8, 17, 2, 20],
 [12, 5, 4, 17, 4, 13, 2, 4, 2, 8, 17, 2],
 [20, 12, 5, 4, 17, 4, 13, 2, 4, 2, 8, 17],
 [2, 20, 12, 5, 4, 17, 4, 13, 2, 4, 2, 8],
 [17, 2, 20, 12, 5, 4, 17, 4, 13, 2, 4, 2]], dtype=int)
Y = np.array([[int(c) for c in r] for r in Matrix(X).inv_mod(19).tolist()], dtype=int)
np.dot(Y, X) % 19

With OSX:

$ pip freeze | egrep "numpy|scipy|sympy"
numpy==1.10.4
scipy==0.16.0
sympy==0.7.6.1

Python 2.7.11 |Anaconda 2.4.0 (x86_64)| (default, Dec  6 2015, 18:57:58) 
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin

>>> np.dot(G, F) % 11
array([[1, 0, 0, 0],
       [0, 1, 0, 0],
       [0, 0, 1, 0],
       [0, 0, 0, 1]])

>>> np.dot(Y, X) % 19
array([[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]])

but on Windows:

Python 3.5.2 |Anaconda 4.2.0 (64-bit)| (default, Jul  5 2016, 11:41:13) [MSC v.1
900 64 bit (AMD64)] on win32
>>> numpy.__version__
'1.11.1'
>>> scipy.__version__
'0.18.1'
>>> sympy.__version__
'1.0'

>>> np.dot(G, F) % 11
array([[1, 0, 0, 0],
       [0, 1, 0, 0],
       [0, 0, 1, 0],
       [0, 0, 0, 1]], dtype=int32)

>>> np.dot(Y, X) % 19
array([[ 7,  1,  8,  4,  5,  1,  2, 12, 10, 10, 12,  7],
       [ 7,  8,  1,  7, 15,  7, 18,  8, 16, 13, 11, 12],
       [ 8,  6,  6,  1, 18, 18, 12,  7,  4,  6,  2,  2],
       [11, 15,  7,  4,  6, 10, 13, 12, 14, 18,  5,  6],
       [11,  0, 10, 11,  3,  2, 16, 15,  5, 12,  0, 12],
       [12, 17,  3,  1,  6,  6, 16, 14, 14, 12, 17, 10],
       [14, 15,  4, 13, 16, 13, 16,  2, 10,  7, 10,  2],
       [18,  2, 11,  8,  0, 16, 13, 17,  9, 10,  7,  0],
       [16,  5, 11,  6, 12,  9,  6,  6, 10,  4,  0, 10],
       [18, 10, 12,  9, 13, 15, 13,  0, 15, 17,  3, 11],
       [ 6,  8,  2, 13, 18,  6, 11, 14,  7,  4, 10, 18],
       [12,  4,  9,  3, 17, 15,  7, 15,  2,  6, 16,  3]], dtype=int32)

@chartl chartl changed the title from mod_inv gives multiplicative inverse in python2.7 OSX but not in python3.5 WIN to Matrix.inv_mod gives multiplicative inverse in python2.7 OSX but not in python3.5 WIN Jan 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment