Skip to content

Commit b172325

Browse files
committed
docs: Enhance polynomial functions with detailed documentation for inversion and multiplication
1 parent c782c8e commit b172325

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

weilycoder/poly/elementary_func/inverse.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
11
#ifndef WEILYCODER_POLY_ELEMENTARY_FUNC_INVERSE_HPP
22
#define WEILYCODER_POLY_ELEMENTARY_FUNC_INVERSE_HPP
33

4+
/**
5+
* @file inverse.hpp
6+
* @brief Polynomial Inversion functions using Newton's method and NTT.
7+
*/
8+
49
#include "../../number_theory/mod_utility.hpp"
510
#include "../ntt_convolve.hpp"
611
#include "multiply.hpp"
712
#include <stdexcept>
813
#include <vector>
914

1015
namespace weilycoder {
16+
/**
17+
* @brief Compute the inverse of a polynomial modulo x^n using Newton's method.
18+
* @tparam T Coefficient type.
19+
* @tparam MultiplyFunc Type of the multiplication function.
20+
* @tparam SubtractFunc Type of the subtraction function.
21+
* @tparam InverseFunc Type of the inversion function for coefficients.
22+
* @param a Coefficients of the polynomial to be inverted.
23+
* @param n Degree up to which the inverse is computed (result size will be n).
24+
* @param multiply Function to multiply two polynomials.
25+
* @param number_sub Function to subtract two coefficients.
26+
* @param number_inv Function to compute the multiplicative inverse of a coefficient.
27+
* @return Coefficients of the inverse polynomial modulo x^n.
28+
*/
1129
template <typename T, typename MultiplyFunc, typename SubtractFunc,
1230
typename InverseFunc>
1331
std::vector<T> poly_inv(const std::vector<T> &a, size_t n, MultiplyFunc multiply,
@@ -26,6 +44,14 @@ std::vector<T> poly_inv(const std::vector<T> &a, size_t n, MultiplyFunc multiply
2644
return res;
2745
}
2846

47+
/**
48+
* @brief Compute the inverse of a polynomial modulo x^n using NTT
49+
* under a given modulus.
50+
* @tparam mod Modulus for NTT.
51+
* @param a Coefficients of the polynomial to be inverted.
52+
* @param n Degree up to which the inverse is computed (result size will be n).
53+
* @return Coefficients of the inverse polynomial modulo x^n.
54+
*/
2955
template <uint64_t mod>
3056
std::vector<uint64_t> ntt_poly_inv(const std::vector<uint64_t> &a, size_t n) {
3157
return poly_inv<>(a, n, ntt_convolve<mod>, mod_sub<mod>, mod_inv<mod>);

weilycoder/poly/elementary_func/multiply.hpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,43 @@
11
#ifndef WEILYCODER_POLY_ELEMENTARY_FUNC_MULTIPLY_HPP
22
#define WEILYCODER_POLY_ELEMENTARY_FUNC_MULTIPLY_HPP
33

4+
/**
5+
* @file multiply.hpp
6+
* @brief Polynomial Multiplication Functions
7+
*/
8+
49
#include "../../number_theory/mod_utility.hpp"
510
#include "../ntt_convolve.hpp"
611
#include <vector>
712

813
namespace weilycoder {
14+
/**
15+
* @brief Multiply two polynomials using a provided multiplication function.
16+
* @tparam T Coefficient type.
17+
* @tparam MultiplyFunc Type of the multiplication function.
18+
* @param a Coefficients of the first polynomial.
19+
* @param b Coefficients of the second polynomial.
20+
* @param multiply Function to multiply two polynomials.
21+
* @return Coefficients of the resulting polynomial after multiplication.
22+
*/
923
template <typename T, typename MultiplyFunc>
1024
std::vector<T> poly_mul(const std::vector<T> &a, const std::vector<T> &b,
1125
MultiplyFunc multiply) {
1226
return multiply(a, b);
1327
}
1428

29+
/**
30+
* @brief Multiply two polynomials using a provided multiplication function,
31+
* limiting the result to degree n-1.
32+
* @tparam T Coefficient type.
33+
* @tparam MultiplyFunc Type of the multiplication function.
34+
* @param a Coefficients of the first polynomial.
35+
* @param b Coefficients of the second polynomial.
36+
* @param n Maximum degree of the resulting polynomial (result size will be n).
37+
* @param multiply Function to multiply two polynomials.
38+
* @return Coefficients of the resulting polynomial after multiplication,
39+
* limited to degree n-1.
40+
*/
1541
template <typename T, typename MultiplyFunc>
1642
std::vector<T> poly_mul(const std::vector<T> &a, const std::vector<T> &b, size_t n,
1743
MultiplyFunc multiply) {
@@ -21,6 +47,16 @@ std::vector<T> poly_mul(const std::vector<T> &a, const std::vector<T> &b, size_t
2147
return res;
2248
}
2349

50+
/**
51+
* @brief Multiply two polynomials using NTT under a given modulus,
52+
* limiting the result to degree n-1.
53+
* @tparam mod Modulus for NTT.
54+
* @param a Coefficients of the first polynomial.
55+
* @param b Coefficients of the second polynomial.
56+
* @param n Maximum degree of the resulting polynomial (result size will be n).
57+
* @return Coefficients of the resulting polynomial after multiplication,
58+
* limited to degree n-1.
59+
*/
2460
template <uint64_t mod>
2561
std::vector<uint64_t> ntt_poly_mul(const std::vector<uint64_t> &a,
2662
const std::vector<uint64_t> &b, size_t n) {

0 commit comments

Comments
 (0)