Skip to content

Commit 83913af

Browse files
committed
Add gr_derivative, with implementations for polynomial rings
1 parent 40e69c2 commit 83913af

File tree

8 files changed

+64
-0
lines changed

8 files changed

+64
-0
lines changed

doc/source/gr.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,14 @@ Powering
739739
should override these methods with faster versions or
740740
to support more general notions of exponentiation when possible.
741741

742+
Derivative
743+
........................................................................
744+
745+
.. function:: int gr_derivative(gr_ptr res, gr_srcptr x, slong var, gr_ctx_t ctx)
746+
747+
Sets *res* to the derivative of *x* with respect to the variable of index *var*.
748+
The variables here are the generators of *ctx*.
749+
742750
Square roots
743751
........................................................................
744752

src/gr.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,8 @@ typedef enum
265265
GR_METHOD_POW_OTHER,
266266
GR_METHOD_OTHER_POW,
267267

268+
GR_METHOD_DERIVATIVE,
269+
268270
GR_METHOD_IS_SQUARE,
269271
GR_METHOD_SQRT,
270272
GR_METHOD_RSQRT,
@@ -1130,6 +1132,8 @@ GR_INLINE WARN_UNUSED_RESULT int gr_pow_fmpq(gr_ptr res, gr_srcptr x, const fmpq
11301132
GR_INLINE WARN_UNUSED_RESULT int gr_pow_other(gr_ptr res, gr_srcptr x, gr_srcptr y, gr_ctx_t y_ctx, gr_ctx_t ctx) { return GR_BINARY_OP_OTHER(ctx, POW_OTHER)(res, x, y, y_ctx, ctx); }
11311133
GR_INLINE WARN_UNUSED_RESULT int gr_other_pow(gr_ptr res, gr_srcptr x, gr_ctx_t x_ctx, gr_srcptr y, gr_ctx_t ctx) { return GR_OTHER_BINARY_OP(ctx, OTHER_POW)(res, x, x_ctx, y, ctx); }
11321134

1135+
GR_INLINE WARN_UNUSED_RESULT int gr_derivative(gr_ptr res, gr_srcptr x, slong var, gr_ctx_t ctx) { return GR_BINARY_OP_SI(ctx, DERIVATIVE)(res, x, var, ctx); }
1136+
11331137
GR_INLINE WARN_UNUSED_RESULT int gr_sqrt(gr_ptr res, gr_srcptr x, gr_ctx_t ctx) { return GR_UNARY_OP(ctx, SQRT)(res, x, ctx); }
11341138
GR_INLINE WARN_UNUSED_RESULT int gr_rsqrt(gr_ptr res, gr_srcptr x, gr_ctx_t ctx) { return GR_UNARY_OP(ctx, RSQRT)(res, x, ctx); }
11351139
GR_INLINE truth_t gr_is_square(gr_srcptr x, gr_ctx_t ctx) { return GR_UNARY_PREDICATE(ctx, IS_SQUARE)(x, ctx); }

src/gr/fmpq_poly.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,17 @@ _gr_fmpq_poly_pow_fmpz(fmpq_poly_t res, const fmpq_poly_t x, const fmpz_t exp, g
639639
}
640640
}
641641

642+
int
643+
_gr_fmpq_poly_derivative(fmpq_poly_t res, const fmpq_poly_t x, slong var, const gr_ctx_t ctx)
644+
{
645+
if (var == 0)
646+
{
647+
fmpq_poly_derivative(res, x);
648+
return GR_SUCCESS;
649+
}
650+
return GR_DOMAIN;
651+
}
652+
642653
int
643654
_gr_fmpq_poly_numerator(fmpq_poly_t res, const fmpq_poly_t x, const gr_ctx_t ctx)
644655
{
@@ -799,6 +810,7 @@ gr_method_tab_input _fmpq_poly_methods_input[] =
799810
{GR_METHOD_POW_UI, (gr_funcptr) _gr_fmpq_poly_pow_ui},
800811
{GR_METHOD_POW_SI, (gr_funcptr) _gr_fmpq_poly_pow_si},
801812
{GR_METHOD_POW_FMPZ, (gr_funcptr) _gr_fmpq_poly_pow_fmpz},
813+
{GR_METHOD_DERIVATIVE, (gr_funcptr) _gr_fmpq_poly_derivative},
802814
{GR_METHOD_NUMERATOR, (gr_funcptr) _gr_fmpq_poly_numerator},
803815
{GR_METHOD_DENOMINATOR, (gr_funcptr) _gr_fmpq_poly_denominator},
804816
{GR_METHOD_CANONICAL_ASSOCIATE, (gr_funcptr) _gr_fmpq_poly_canonical_associate},

src/gr/fmpz_mpoly.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,17 @@ _gr_fmpz_mpoly_pow_fmpz(fmpz_mpoly_t res, const fmpz_mpoly_t poly1, const fmpz_t
492492
return GR_UNABLE;
493493
}
494494

495+
int
496+
_gr_fmpz_mpoly_derivative(fmpz_mpoly_t res, const fmpz_mpoly_t poly, slong var, gr_ctx_t ctx)
497+
{
498+
if (var >= 0 && var < MPOLYNOMIAL_MCTX(ctx)->minfo->nvars)
499+
{
500+
fmpz_mpoly_derivative(res, poly, var, MPOLYNOMIAL_MCTX(ctx));
501+
return GR_SUCCESS;
502+
}
503+
return GR_DOMAIN;
504+
}
505+
495506
truth_t
496507
_gr_fmpz_mpoly_is_square(const fmpz_mpoly_t poly, gr_ctx_t ctx)
497508
{
@@ -643,6 +654,7 @@ gr_method_tab_input _gr_fmpz_mpoly_methods_input[] =
643654
{GR_METHOD_IS_INVERTIBLE, (gr_funcptr) _gr_fmpz_mpoly_is_invertible},
644655
{GR_METHOD_POW_UI, (gr_funcptr) _gr_fmpz_mpoly_pow_ui},
645656
{GR_METHOD_POW_FMPZ, (gr_funcptr) _gr_fmpz_mpoly_pow_fmpz},
657+
{GR_METHOD_DERIVATIVE, (gr_funcptr) _gr_fmpz_mpoly_derivative},
646658
{GR_METHOD_SQRT, (gr_funcptr) _gr_fmpz_mpoly_sqrt},
647659
{GR_METHOD_IS_SQUARE, (gr_funcptr) _gr_fmpz_mpoly_is_square},
648660
{GR_METHOD_CANONICAL_ASSOCIATE, (gr_funcptr) _gr_fmpz_mpoly_canonical_associate},

src/gr/fmpz_poly.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,17 @@ _gr_fmpz_poly_pow_fmpz(fmpz_poly_t res, const fmpz_poly_t x, const fmpz_t exp, c
707707
}
708708
}
709709

710+
int
711+
_gr_fmpz_poly_derivative(fmpz_poly_t res, const fmpz_poly_t x, slong var, const gr_ctx_t ctx)
712+
{
713+
if (var == 0)
714+
{
715+
fmpz_poly_derivative(res, x);
716+
return GR_SUCCESS;
717+
}
718+
return GR_DOMAIN;
719+
}
720+
710721
truth_t
711722
_gr_fmpz_poly_is_square(const fmpz_poly_t x, const gr_ctx_t ctx)
712723
{
@@ -904,6 +915,7 @@ gr_method_tab_input _fmpz_poly_methods_input[] =
904915
{GR_METHOD_POW_UI, (gr_funcptr) _gr_fmpz_poly_pow_ui},
905916
{GR_METHOD_POW_SI, (gr_funcptr) _gr_fmpz_poly_pow_si},
906917
{GR_METHOD_POW_FMPZ, (gr_funcptr) _gr_fmpz_poly_pow_fmpz},
918+
{GR_METHOD_DERIVATIVE, (gr_funcptr) _gr_fmpz_poly_derivative},
907919
{GR_METHOD_IS_SQUARE, (gr_funcptr) _gr_fmpz_poly_is_square},
908920
{GR_METHOD_SQRT, (gr_funcptr) _gr_fmpz_poly_sqrt},
909921
{GR_METHOD_RSQRT, (gr_funcptr) _gr_fmpz_poly_rsqrt},

src/gr/polynomial.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,12 @@ polynomial_pow_si(gr_poly_t res, const gr_poly_t poly, slong exp, gr_ctx_t ctx)
669669
return status;
670670
}
671671

672+
int
673+
polynomial_derivative(gr_poly_t res, const gr_poly_t poly, slong var, gr_ctx_t ctx)
674+
{
675+
return (var == 0) ? gr_poly_derivative(res, poly, POLYNOMIAL_ELEM_CTX(ctx)) : GR_DOMAIN;
676+
}
677+
672678
int
673679
polynomial_gcd(gr_poly_t res, const gr_poly_t x, const gr_poly_t y, const gr_ctx_t ctx)
674680
{
@@ -808,6 +814,8 @@ gr_method_tab_input _gr_poly_methods_input[] =
808814
{GR_METHOD_DIVEXACT, (gr_funcptr) polynomial_divexact},
809815
{GR_METHOD_INV, (gr_funcptr) polynomial_inv},
810816

817+
{GR_METHOD_DERIVATIVE, (gr_funcptr) polynomial_derivative},
818+
811819
{GR_METHOD_EUCLIDEAN_DIV, (gr_funcptr) polynomial_euclidean_div},
812820
{GR_METHOD_EUCLIDEAN_REM, (gr_funcptr) polynomial_euclidean_rem},
813821
{GR_METHOD_EUCLIDEAN_DIVREM, (gr_funcptr) polynomial_euclidean_divrem},

src/gr_generic/generic.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,11 @@ int gr_generic_other_pow(gr_ptr res, gr_srcptr x, gr_ctx_t x_ctx, gr_srcptr y, g
12681268
return status;
12691269
}
12701270

1271+
int gr_generic_derivative(gr_ptr res, gr_srcptr x, slong var, gr_ctx_t ctx)
1272+
{
1273+
return GR_UNABLE;
1274+
}
1275+
12711276
int gr_generic_numerator(gr_ptr res, gr_srcptr x, gr_ctx_t ctx)
12721277
{
12731278
return gr_set(res, x, ctx);
@@ -2794,6 +2799,8 @@ const gr_method_tab_input _gr_generic_methods[] =
27942799
{GR_METHOD_POW_FMPQ, (gr_funcptr) gr_generic_pow_fmpq},
27952800
{GR_METHOD_OTHER_POW, (gr_funcptr) gr_generic_other_pow},
27962801

2802+
{GR_METHOD_DERIVATIVE, (gr_funcptr) gr_generic_derivative},
2803+
27972804
{GR_METHOD_IS_SQUARE, (gr_funcptr) gr_generic_is_square},
27982805
{GR_METHOD_SQRT, (gr_funcptr) gr_generic_sqrt},
27992806
{GR_METHOD_RSQRT, (gr_funcptr) gr_generic_rsqrt},

src/gr_mpoly/ctx.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ gr_method_tab_input _gr_mpoly_methods_input[] =
355355
{GR_METHOD_MUL_FMPQ, (gr_funcptr) gr_mpoly_mul_fmpq},
356356
{GR_METHOD_INV, (gr_funcptr) gr_mpoly_inv},
357357
{GR_METHOD_CANONICAL_ASSOCIATE, (gr_funcptr) gr_mpoly_canonical_associate},
358+
{GR_METHOD_DERIVATIVE, (gr_funcptr) gr_mpoly_derivative},
358359
{0, (gr_funcptr) NULL},
359360
};
360361

0 commit comments

Comments
 (0)