Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gr_poly_scalar_mul #2191

Merged
merged 1 commit into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions doc/source/gr_poly.rst
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,14 @@ Arithmetic
int gr_poly_mullow(gr_poly_t res, const gr_poly_t poly1, const gr_poly_t poly2, slong len, gr_ctx_t ctx)

.. function:: int gr_poly_mul_scalar(gr_poly_t res, const gr_poly_t poly, gr_srcptr c, gr_ctx_t ctx)
int gr_poly_scalar_mul(gr_poly_t res, gr_srcptr c, const gr_poly_t poly, gr_ctx_t ctx)
int gr_poly_mul_ui(gr_poly_t res, const gr_poly_t poly, ulong c, gr_ctx_t ctx)
int gr_poly_mul_si(gr_poly_t res, const gr_poly_t poly, slong c, gr_ctx_t ctx)
int gr_poly_mul_fmpz(gr_poly_t res, const gr_poly_t poly, const fmpz c, gr_ctx_t ctx)
int gr_poly_mul_fmpq(gr_poly_t res, const gr_poly_t poly, const fmpq c, gr_ctx_t ctx)

Sets *res* to *poly* multiplied by the scalar *c* which must be
an element of or coercible to the coefficient ring.
Sets *res* to *poly* multiplied by the scalar *c* (or the scalar *c* multiplied by *poly*)
which must be an element of or coercible to the coefficient ring.

.. function:: int _gr_poly_mul_karatsuba(gr_ptr res, gr_srcptr poly1, slong len1, gr_srcptr poly2, slong len2, gr_ctx_t ctx)
int gr_poly_mul_karatsuba(gr_poly_t res, const gr_poly_t poly1, const gr_poly_t poly2, gr_ctx_t ctx)
Expand Down
38 changes: 33 additions & 5 deletions src/gr/polynomial.c
Original file line number Diff line number Diff line change
Expand Up @@ -416,17 +416,17 @@
}

int
polynomial_mul_other(gr_poly_t res, const gr_poly_t f, gr_srcptr x, gr_ctx_t x_ctx, gr_ctx_t ctx)
polynomial_mul_other(gr_poly_t res, const gr_poly_t poly, gr_srcptr x, gr_ctx_t x_ctx, gr_ctx_t ctx)
{
if (x_ctx == POLYNOMIAL_ELEM_CTX(ctx))
{
return gr_poly_mul_scalar(res, f, x, x_ctx);
return gr_poly_mul_scalar(res, poly, x, x_ctx);

Check warning on line 423 in src/gr/polynomial.c

View check run for this annotation

Codecov / codecov/patch

src/gr/polynomial.c#L423

Added line #L423 was not covered by tests
}
else if (x_ctx->which_ring == GR_CTX_GR_POLY &&
else if (x_ctx->which_ring == GR_CTX_GR_POLY &&
POLYNOMIAL_ELEM_CTX(x_ctx) == POLYNOMIAL_ELEM_CTX(ctx) &&
!strcmp(POLYNOMIAL_CTX(x_ctx)->var, POLYNOMIAL_CTX(ctx)->var))
{
return polynomial_mul(res, f, x, ctx);
return polynomial_mul(res, poly, x, ctx);
}
else
{
Expand All @@ -436,7 +436,34 @@
polynomial_init(t, ctx);
status = polynomial_set_other(t, x, x_ctx, ctx);
if (status == GR_SUCCESS)
status = polynomial_mul(res, f, t, ctx);
status = polynomial_mul(res, poly, t, ctx);
polynomial_clear(t, ctx);
return status;
}
}

int
polynomial_other_mul(gr_poly_t res, gr_srcptr x, gr_ctx_t x_ctx, const gr_poly_t poly, gr_ctx_t ctx)
{
if (x_ctx == POLYNOMIAL_ELEM_CTX(ctx))
{
return gr_poly_scalar_mul(res, x, poly, x_ctx);
}
else if (x_ctx->which_ring == GR_CTX_GR_POLY &&
POLYNOMIAL_ELEM_CTX(x_ctx) == POLYNOMIAL_ELEM_CTX(ctx) &&
!strcmp(POLYNOMIAL_CTX(x_ctx)->var, POLYNOMIAL_CTX(ctx)->var))
{
return polynomial_mul(res, x, poly, ctx);
}
else
{
gr_poly_t t;
int status = GR_SUCCESS;

polynomial_init(t, ctx);
status = polynomial_set_other(t, x, x_ctx, ctx);
if (status == GR_SUCCESS)
status = polynomial_mul(res, t, poly, ctx);
polynomial_clear(t, ctx);
return status;
}
Expand Down Expand Up @@ -601,6 +628,7 @@
{GR_METHOD_SUB, (gr_funcptr) polynomial_sub},
{GR_METHOD_MUL, (gr_funcptr) polynomial_mul},
{GR_METHOD_MUL_OTHER, (gr_funcptr) polynomial_mul_other},
{GR_METHOD_OTHER_MUL, (gr_funcptr) polynomial_other_mul},
{GR_METHOD_MUL_UI, (gr_funcptr) polynomial_mul_ui},
{GR_METHOD_MUL_SI, (gr_funcptr) polynomial_mul_si},
{GR_METHOD_MUL_FMPZ, (gr_funcptr) polynomial_mul_fmpz},
Expand Down
1 change: 1 addition & 0 deletions src/gr_poly.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ WARN_UNUSED_RESULT int _gr_poly_mullow_generic(gr_ptr res, gr_srcptr poly1, slon
GR_POLY_INLINE WARN_UNUSED_RESULT int _gr_poly_mullow(gr_ptr res, gr_srcptr poly1, slong len1, gr_srcptr poly2, slong len2, slong len, gr_ctx_t ctx) { return GR_POLY_BINARY_TRUNC_OP(ctx, POLY_MULLOW)(res, poly1, len1, poly2, len2, len, ctx); }
WARN_UNUSED_RESULT int gr_poly_mullow(gr_poly_t res, const gr_poly_t poly1, const gr_poly_t poly2, slong n, gr_ctx_t ctx);
WARN_UNUSED_RESULT int gr_poly_mul_scalar(gr_poly_t res, const gr_poly_t poly, gr_srcptr c, gr_ctx_t ctx);
WARN_UNUSED_RESULT int gr_poly_scalar_mul(gr_poly_t res, gr_srcptr c, const gr_poly_t poly, gr_ctx_t ctx);
WARN_UNUSED_RESULT int gr_poly_mul_ui(gr_poly_t res, const gr_poly_t poly, ulong c, gr_ctx_t ctx);
WARN_UNUSED_RESULT int gr_poly_mul_si(gr_poly_t res, const gr_poly_t poly, slong c, gr_ctx_t ctx);
WARN_UNUSED_RESULT int gr_poly_mul_fmpz(gr_poly_t res, const gr_poly_t poly, const fmpz_t c, gr_ctx_t ctx);
Expand Down
20 changes: 20 additions & 0 deletions src/gr_poly/mul_scalar.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,26 @@
return status;
}

int
gr_poly_scalar_mul(gr_poly_t res, gr_srcptr c, const gr_poly_t poly, gr_ctx_t ctx)
{
int status;
slong len = poly->length;

if (len == 0 || gr_is_zero(c, ctx) == T_TRUE)
return gr_poly_zero(res, ctx);

Check warning on line 43 in src/gr_poly/mul_scalar.c

View check run for this annotation

Codecov / codecov/patch

src/gr_poly/mul_scalar.c#L43

Added line #L43 was not covered by tests

if (res != poly)
{
gr_poly_fit_length(res, len, ctx);
_gr_poly_set_length(res, len, ctx);
}

status = _gr_scalar_mul_vec(res->coeffs, c, poly->coeffs, len, ctx);
_gr_poly_normalise(res, ctx);
return status;
}

int
gr_poly_mul_ui(gr_poly_t res, const gr_poly_t poly, ulong c, gr_ctx_t ctx)
{
Expand Down
Loading