Talk:Defactoring algorithms: Difference between revisions

re
No edit summary
 
Line 59: Line 59:
from sympy import Matrix, normalforms
from sympy import Matrix, normalforms


def __hnf (main):
def __hnf_col (main):
     return np.flip (np.array (normalforms.hermite_normal_form (Matrix (np.flip (main)).T).T, dtype = int))
     return np.flip (np.array (normalforms.hermite_normal_form (Matrix (np.flip (main))), dtype = int))


def __sat (main):
def __sat (main):
     return np.rint (linalg.inv (__hnf (main.T).T) @ main).astype (int)
     return np.rint (linalg.inv (__hnf_col (main)) @ main).astype (int)
</syntaxhighlight>
</syntaxhighlight>


Line 132: Line 132:
def __sat (main):
def __sat (main):
     r = Matrix (main).rank ()
     r = Matrix (main).rank ()
     unimodular = __hnf (np.hstack ((main.T, np.eye (main.shape[1], dtype = int))))[:, r:]
     unimodular = __hnf_col (np.vstack ((main, np.eye (main.shape[1], dtype = int))))[r:]
     return np.rint (linalg.inv (unimodular).T[:r]).astype (int)
     return np.rint (linalg.inv (unimodular)[:r]).astype (int)
</syntaxhighlight>
</syntaxhighlight>


Line 141: Line 141:
def __sat (main):
def __sat (main):
     r = Matrix (main).rank ()
     r = Matrix (main).rank ()
     return np.rint (linalg.inv (__hnf (main.T)[:r].T) @ main).astype (int)
     return np.rint (linalg.inv (__hnf_col (main)[:, :r]) @ main).astype (int)
</syntaxhighlight>
</syntaxhighlight>


:::::::: Note the slice and thus computing the rank are redundant; they're there just for futureproofness.  
:::::::: Note the slice and thus computing the rank are redundant; they're there just for futureproofness.  


:::::::: [[User:FloraC|FloraC]] ([[User talk:FloraC|talk]]) 10:58, 11 February 2023 (UTC)
:::::::: [[User:FloraC|FloraC]] ([[User talk:FloraC|talk]]) 10:58, 11 February 2023 (UTC)
 
:::::::: Edit: update the code to adopt column-style HNF so as to get rid of all the transpositions.
 
:::::::: [[User:FloraC|FloraC]] ([[User talk:FloraC|talk]]) 07:08, 15 February 2023 (UTC)
Return to "Defactoring algorithms" page.