Skip to content

Commit

Permalink
Reformatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
MathieuDutSik committed Oct 10, 2023
1 parent 3e73a95 commit f6a3383
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 110 deletions.
41 changes: 18 additions & 23 deletions src_matrix/MAT_Matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,12 @@ MyMatrix<T2> UniversalMatrixConversion(MyMatrix<T1> const &M) {
}

template <typename T2, typename T1>
std::vector<MyMatrix<T2>> UniversalStdVectorMatrixConversion(std::vector<MyMatrix<T1>> const &ListM) {
std::vector<MyMatrix<T2>>
UniversalStdVectorMatrixConversion(std::vector<MyMatrix<T1>> const &ListM) {
size_t n_mat = ListM.size();
std::vector<MyMatrix<T2>> ListM_ret(n_mat);
for (size_t i_mat = 0; i_mat < n_mat; i_mat++)
ListM_ret[i_mat] = UniversalMatrixConversion<T2,T1>(ListM[i_mat]);
ListM_ret[i_mat] = UniversalMatrixConversion<T2, T1>(ListM[i_mat]);
return ListM_ret;
}

Expand Down Expand Up @@ -579,7 +580,6 @@ void WriteVectorGAP(std::ostream &os, MyVector<T> const &TheVec) {
os << " ]";
}


template <typename T>
void WriteVectorFile(std::string const &eFile, MyVector<T> const &TheV) {
std::ofstream os(eFile);
Expand Down Expand Up @@ -1217,7 +1217,8 @@ MyMatrix<T> NullspaceTrMat_Kernel(size_t nbRow, size_t nbCol, F f) {

//
template <typename T, typename F>
MyMatrix<T> NullspaceTrMatTarget_Kernel(size_t nbRow, size_t nbCol, size_t target_zero, F f) {
MyMatrix<T> NullspaceTrMatTarget_Kernel(size_t nbRow, size_t nbCol,
size_t target_zero, F f) {
static_assert(is_ring_field<T>::value,
"Requires T to be a field in NullspaceTrMat_Kernel");
size_t target_rank = nbCol - target_zero;
Expand Down Expand Up @@ -1294,7 +1295,6 @@ MyMatrix<T> NullspaceTrMatTarget_Kernel(size_t nbRow, size_t nbCol, size_t targe
return NSP;
}


template <typename T, typename F>
MyVector<T> NullspaceTrMatTargetOne_Kernel(size_t nbRow, size_t nbCol, F f) {
static_assert(is_ring_field<T>::value,
Expand Down Expand Up @@ -1357,7 +1357,7 @@ MyVector<T> NullspaceTrMatTargetOne_Kernel(size_t nbRow, size_t nbCol, F f) {
// The target was not achieved, we get a larger kernel than expected.
// We select one vector and it has to be processed down the line
MyVector<T> Vzero = ZeroVector<T>(nbCol);
auto set_vzero=[&]() -> void {
auto set_vzero = [&]() -> void {
for (size_t iCol = 0; iCol < nbCol; iCol++) {
if (ListColSelect01[iCol] == 0) {
Vzero(iCol) = -1;
Expand All @@ -1373,9 +1373,6 @@ MyVector<T> NullspaceTrMatTargetOne_Kernel(size_t nbRow, size_t nbCol, F f) {
return Vzero;
}




template <typename T>
inline typename std::enable_if<is_ring_field<T>::value, MyMatrix<T>>::type
NullspaceTrMat(MyMatrix<T> const &Input) {
Expand Down Expand Up @@ -1588,26 +1585,25 @@ DeterminantMat(MyMatrix<T> const &Input) {
// A significantly slower algorithm for computing the determinant.
// It is good as a control for the above method and can also be used
// for consistency checks of arithmetics.
template <typename T>
T DeterminantMatPermutation(MyMatrix<T> const& A) {
template <typename T> T DeterminantMatPermutation(MyMatrix<T> const &A) {
int n = A.rows();
if (n == 0)
return T(1);
std::vector<int> s(n);
for (int i=0; i<n; i++)
for (int i = 0; i < n; i++)
s[i] = i;
T TheDet(0);
do {
T eProd(1);
for (int u=0; u<n; u++)
eProd *= A(u,s[u]);
for (int u = 0; u < n; u++)
eProd *= A(u, s[u]);
int eSign = 1;
for (int i=0; i<n; i++)
for (int j=i+1; j<n; j++)
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
if (s[j] < s[i])
eSign = -eSign;
TheDet += eSign * eProd;
} while(std::next_permutation(s.begin(), s.end()));
} while (std::next_permutation(s.begin(), s.end()));
return TheDet;
}

Expand Down Expand Up @@ -2679,8 +2675,8 @@ template <typename T> struct ContainerMatrix {
}
}
ContainerMatrix(MyMatrix<T> const &_mat)
: mat(_mat), n_rows(mat.rows()), n_cols(mat.cols()),
V1(n_cols), V2(n_cols) {
: mat(_mat), n_rows(mat.rows()), n_cols(mat.cols()), V1(n_cols),
V2(n_cols) {
v_test = MyVector<T>(n_cols);
std::function<size_t(size_t)> fct_hash = [&](size_t idx) -> size_t {
set_v(V1, idx);
Expand Down Expand Up @@ -2709,13 +2705,12 @@ template <typename T> struct ContainerMatrix {
size_t idx = *iter;
return idx;
}
std::optional<size_t> GetIdx_v(MyVector<T> const& V) {
std::optional<size_t> GetIdx_v(MyVector<T> const &V) {
v_test = V;
return GetIdx();
}
template<typename F>
std::optional<size_t> GetIdx_f(F f) {
for (size_t i=0; i<n_cols; i++)
template <typename F> std::optional<size_t> GetIdx_f(F f) {
for (size_t i = 0; i < n_cols; i++)
v_test(i) = f(i);
return GetIdx();
}
Expand Down
89 changes: 50 additions & 39 deletions src_matrix/MAT_MatrixInt.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,42 +238,48 @@ template <typename T> T ComputeLCM(std::vector<T> const &eVect) {

template <typename T, typename Tint>
MyVector<Tint> RescaleVec(MyVector<T> const &v) {
static_assert(is_implementation_of_Q<T>::value, "Requires T to be an implementation of Q");
static_assert(is_implementation_of_Q<Tint>::value || is_implementation_of_Z<Tint>::value, "Requires Tint to be an implementation of Z or Q");
static_assert(is_implementation_of_Q<T>::value,
"Requires T to be an implementation of Q");
static_assert(is_implementation_of_Q<Tint>::value ||
is_implementation_of_Z<Tint>::value,
"Requires Tint to be an implementation of Z or Q");
int cols = v.size();
std::vector<Tint> dens(cols,1);
std::vector<Tint> dens(cols, 1);
MyVector<Tint> vret = MyVector<Tint>(cols);
for( int iCol = 0; iCol < cols; iCol++){
for (int iCol = 0; iCol < cols; iCol++) {
dens[iCol] = v(iCol).get_den();
}
Tint scale = LCMlist(dens);
for( int iCol = 0; iCol < cols; iCol++) {
for (int iCol = 0; iCol < cols; iCol++) {
vret(iCol) = (scale / v(iCol).get_den()) * v(iCol).get_num();
}
return vret;
}

template <typename T, typename Tint>
MyMatrix<Tint> RescaleRows(MyMatrix<T> const &M) {
static_assert(is_implementation_of_Q<T>::value, "Requires T to be an implementation of Q");
static_assert(is_implementation_of_Q<Tint>::value || is_implementation_of_Z<Tint>::value, "Requires Tint to be an implementation of Z or Q");
static_assert(is_implementation_of_Q<T>::value,
"Requires T to be an implementation of Q");
static_assert(is_implementation_of_Q<Tint>::value ||
is_implementation_of_Z<Tint>::value,
"Requires Tint to be an implementation of Z or Q");
int rows = M.rows();
int cols = M.cols();
std::vector<Tint> dens(cols,1);
std::vector<Tint> dens(cols, 1);
MyMatrix<Tint> Mret = MyMatrix<Tint>(rows, cols);
for( int iRow = 0; iRow < rows; iRow++) {
for( int iCol = 0; iCol < cols; iCol++){
for (int iRow = 0; iRow < rows; iRow++) {
for (int iCol = 0; iCol < cols; iCol++) {
dens[iCol] = M(iRow, iCol).get_den();
}
Tint scale = LCMlist(dens);
for( int iCol = 0; iCol < cols; iCol++) {
Mret(iRow, iCol) = (scale / M(iRow,iCol).get_den()) * M(iRow,iCol).get_num();
for (int iCol = 0; iCol < cols; iCol++) {
Mret(iRow, iCol) =
(scale / M(iRow, iCol).get_den()) * M(iRow, iCol).get_num();
}
}
return Mret;
}


template <typename T> struct FractionMatrix {
T TheMult;
MyMatrix<T> TheMat;
Expand Down Expand Up @@ -317,7 +323,7 @@ CanonicalizationSmallestCoefficientMatrixPlusCoeff(MyMatrix<T> const &M) {
};
T the_sma = 1; // Just to shut up warnings. Will not be used
bool IsAssigned = false;
auto f_insert=[&](T const&input) -> void {
auto f_insert = [&](T const &input) -> void {
T val = get_abs(input);
if (val > 0) {
if (!IsAssigned) {
Expand All @@ -333,7 +339,8 @@ CanonicalizationSmallestCoefficientMatrixPlusCoeff(MyMatrix<T> const &M) {
for (int iRow = 0; iRow < nbRow; iRow++)
f_insert(M(iRow, iCol));
if (!IsAssigned) {
std::cerr << "Failed to find a non-zero value for M, so impossible to canonicalize\n";
std::cerr << "Failed to find a non-zero value for M, so impossible to "
"canonicalize\n";
throw TerminalException{1};
}
MyMatrix<T> M2 = M / the_sma;
Expand All @@ -358,7 +365,7 @@ CanonicalizationSmallestCoefficientVectorPlusCoeff(MyVector<T> const &V) {
};
T the_sma = 1; // Just to shut up warnings. Will not be used
bool IsAssigned = false;
auto f_insert=[&](T const&input) -> void {
auto f_insert = [&](T const &input) -> void {
T val = get_abs(input);
if (val > 0) {
if (!IsAssigned) {
Expand All @@ -373,7 +380,8 @@ CanonicalizationSmallestCoefficientVectorPlusCoeff(MyVector<T> const &V) {
for (int i = 0; i < n; i++)
f_insert(V(i));
if (!IsAssigned) {
std::cerr << "Failed to find a non-zero value for V, so impossible to canonicalize\n";
std::cerr << "Failed to find a non-zero value for V, so impossible to "
"canonicalize\n";
throw TerminalException{1};
}
MyVector<T> V2 = V / the_sma;
Expand Down Expand Up @@ -412,62 +420,65 @@ FractionVector<T> RemoveFractionVectorPlusCoeff(MyVector<T> const &V) {
return {TheMult, std::move(Vret)};
}

template<typename T>
MyVector<typename underlying_ring<T>::ring_type> NonUniqueRescaleVecRing(MyVector<T> const& V) {
// It is non-unique because if we have V = (4, 6, 8) then we return (4, 6, 8) while for
// RemoveFraction it returns (2,3,4).
template <typename T>
MyVector<typename underlying_ring<T>::ring_type>
NonUniqueRescaleVecRing(MyVector<T> const &V) {
// It is non-unique because if we have V = (4, 6, 8) then we return (4, 6, 8)
// while for RemoveFraction it returns (2,3,4).
int n = V.size();
using Tring = typename underlying_ring<T>::ring_type;
std::vector<Tring> Lden(n);
for (int i=0; i<n; i++) {
for (int i = 0; i < n; i++) {
Lden[i] = GetDenominator_z(V(i));
}
Tring scale = LCMlist(Lden);
MyVector<Tring> Vret(n);
for (int i=0; i<n; i++) {
for (int i = 0; i < n; i++) {
Vret(i) = (scale / Lden[i]) * GetNumerator_z(V(i));
}
return Vret;
}

template<typename T>
MyMatrix<typename underlying_ring<T>::ring_type> NonUniqueRescaleRowsRing(MyMatrix<T> const& M) {
template <typename T>
MyMatrix<typename underlying_ring<T>::ring_type>
NonUniqueRescaleRowsRing(MyMatrix<T> const &M) {
int nbRow = M.rows();
int nbCol = M.cols();
using Tring = typename underlying_ring<T>::ring_type;
MyMatrix<Tring> Mret(nbRow, nbCol);
std::vector<Tring> dens(nbCol);
for (int iRow=0; iRow<nbRow; iRow++) {
for (int iCol=0; iCol<nbCol; iCol++) {
dens[iCol] = GetDenominator_z(M(iRow,iCol));
for (int iRow = 0; iRow < nbRow; iRow++) {
for (int iCol = 0; iCol < nbCol; iCol++) {
dens[iCol] = GetDenominator_z(M(iRow, iCol));
}
Tring scale = LCMlist(dens);
for (int iCol=0; iCol<nbCol; iCol++) {
Mret(iRow,iCol) = (scale / dens[iCol]) * GetNumerator_z(M(iRow,iCol));
for (int iCol = 0; iCol < nbCol; iCol++) {
Mret(iRow, iCol) = (scale / dens[iCol]) * GetNumerator_z(M(iRow, iCol));
}
}
return Mret;
}

template<typename T>
MyMatrix<typename underlying_ring<T>::ring_type> UniqueRescaleRowsRing(MyMatrix<T> const& M) {
template <typename T>
MyMatrix<typename underlying_ring<T>::ring_type>
UniqueRescaleRowsRing(MyMatrix<T> const &M) {
int nbRow = M.rows();
int nbCol = M.cols();
using Tring = typename underlying_ring<T>::ring_type;
MyMatrix<Tring> Mret(nbRow, nbCol);
std::vector<Tring> dens(nbCol), Vret(nbCol);
for (int iRow=0; iRow<nbRow; iRow++) {
for (int iCol=0; iCol<nbCol; iCol++) {
dens[iCol] = GetDenominator_z(M(iRow,iCol));
for (int iRow = 0; iRow < nbRow; iRow++) {
for (int iCol = 0; iCol < nbCol; iCol++) {
dens[iCol] = GetDenominator_z(M(iRow, iCol));
}
Tring scale = LCMlist(dens);
for (int iCol=0; iCol<nbCol; iCol++) {
Vret[iCol] = (scale / dens[iCol]) * GetNumerator_z(M(iRow,iCol));
for (int iCol = 0; iCol < nbCol; iCol++) {
Vret[iCol] = (scale / dens[iCol]) * GetNumerator_z(M(iRow, iCol));
}
Tring eGCD = Vret[0];
for (int iCol=1; iCol<nbCol; iCol++)
for (int iCol = 1; iCol < nbCol; iCol++)
eGCD = GcdPair(eGCD, Vret[iCol]);
for (int iCol=0; iCol<nbCol; iCol++) {
for (int iCol = 0; iCol < nbCol; iCol++) {
Mret(iRow, iCol) = Vret[iCol] / eGCD;
}
}
Expand Down
Loading

0 comments on commit f6a3383

Please sign in to comment.