Skip to content

Conversation

@Marcel583
Copy link
Member

@Marcel583 Marcel583 commented Mar 21, 2025

Description

This PR implements the standard Rem trait for Z, PolyOverZ, MatZ and MatPolyOverZ, which we are currently missing.

Testing

  • I added basic working examples (possibly in doc-comment)
  • I added tests for large (pointer representation) values
  • I triggered all possible errors in my test in every possible way
  • I included tests for all reasonable edge cases

Checklist:

  • I have performed a self-review of my own code
    • The code provides good readability and maintainability s.t. it fulfills best practices like talking code, modularity, ...
      • The chosen implementation is not more complex than it has to be
    • My code should work as intended and no side effects occur (e.g. memory leaks)
    • The doc comments fit our style guide
    • I have credited related sources if needed

@Marcel583 Marcel583 self-assigned this Mar 21, 2025
@Marcel583 Marcel583 added the enhancement📈 New feature label Mar 21, 2025
@Marcel583 Marcel583 marked this pull request as ready for review March 24, 2025 09:42
@Marcel583 Marcel583 requested review from jnsiemer and removed request for jnsiemer March 24, 2025 09:42
Copy link
Member

@Marvin-Beckmann Marvin-Beckmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you considered to use the modulus as a type with which you can reduce? To me that would feel natural to be possible.

For the modulus we also have a lot of conversions, so maybe there is a native way to do it, and potentially even have some speedups as I suggested. Please check if that is possible somewhere

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please mention that the remainder trait is a standard trait we are missing in the description. This gives the PR more reasoning

Comment on lines +55 to +57
let mut entry = unsafe { self.get_entry_unchecked(i, j) };
entry = entry % modulus;
unsafe { out.set_entry_unchecked(i, j, entry) };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with FLINT you can directly write the result of the modulus operation in the matrix and save yourself one clone for each entry. I would opt for that improvement

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to clone the value, as fmpz_poly_scalar_mod_fmpz needs a constant fmpz_poly as a second input, not a mutually one

Comment on lines +48 to +65
assert!(modulus > &1, "Modulus can not be smaller than 2.");
let num_cols = self.get_num_columns();
let num_rows = self.get_num_rows();

let mut out = MatZ::new(num_rows, num_cols);
unsafe { fmpz_mat_scalar_smod(&mut out.matrix, &self.matrix, &modulus.value) };

for i in 0..num_rows {
for j in 0..num_cols {
let entry = unsafe { out.get_entry_unchecked(i, j) };
if entry < 0 {
unsafe { out.set_entry_unchecked(i, j, entry + modulus) };
}
}
}

out
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please evaluate whether it is faster to do this or:

fmpz_mod_mat_set_fmpz_mat

where you convert the input into a Moduuls.
To me it would also feel quite natural to have an input that would be a modulus

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method is in FLINT but not in flintsys

Copy link
Member

@Marvin-Beckmann Marvin-Beckmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sure to fix the indentation, but looks good and reasonable otherwise.

@Marcel583 Marcel583 merged commit 193abc5 into dev Apr 17, 2025
2 checks passed
@Marcel583 Marcel583 deleted the Rem_trait_Z branch April 17, 2025 17:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement📈 New feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants