-
Notifications
You must be signed in to change notification settings - Fork 142
/
divrem.c
124 lines (101 loc) · 2.93 KB
/
divrem.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
Copyright (C) 2011 William Hart
Copyright (C) 2012, 2013 Fredrik Johansson
This file is part of Arb.
Arb is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License (LGPL) as published
by the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version. See <http://www.gnu.org/licenses/>.
*/
#include "arb_poly.h"
/* TODO: tighten this code */
void
_arb_poly_div(arb_ptr Q,
arb_srcptr A, slong lenA,
arb_srcptr B, slong lenB, slong prec)
{
slong lenQ, lenB2;
arb_ptr Arev, Brev;
lenQ = lenA - lenB + 1;
Arev = _arb_vec_init(2 * lenQ);
Brev = Arev + lenQ;
_arb_poly_reverse(Arev, A + (lenA - lenQ), lenQ, lenQ);
if (lenB >= lenQ)
{
_arb_poly_reverse(Brev, B + (lenB - lenQ), lenQ, lenQ);
lenB2 = lenQ;
}
else
{
_arb_poly_reverse(Brev, B, lenB, lenB);
lenB2 = lenB;
}
_arb_poly_div_series(Q, Arev, lenQ, Brev, lenB2, lenQ, prec);
_arb_poly_reverse(Q, Q, lenQ, lenQ);
_arb_vec_clear(Arev, 2 * lenQ);
}
void _arb_poly_divrem(arb_ptr Q, arb_ptr R,
arb_srcptr A, slong lenA,
arb_srcptr B, slong lenB, slong prec)
{
const slong lenQ = lenA - lenB + 1;
_arb_poly_div(Q, A, lenA, B, lenB, prec);
if (lenB > 1)
{
if (lenQ >= lenB - 1)
_arb_poly_mullow(R, Q, lenQ, B, lenB - 1, lenB - 1, prec);
else
_arb_poly_mullow(R, B, lenB - 1, Q, lenQ, lenB - 1, prec);
_arb_vec_sub(R, A, R, lenB - 1, prec);
}
}
void _arb_poly_rem(arb_ptr R,
arb_srcptr A, slong lenA,
arb_srcptr B, slong lenB, slong prec)
{
const slong lenQ = lenA - lenB + 1;
arb_ptr Q = _arb_vec_init(lenQ);
_arb_poly_divrem(Q, R, A, lenA, B, lenB, prec);
_arb_vec_clear(Q, lenQ);
}
int arb_poly_divrem(arb_poly_t Q, arb_poly_t R,
const arb_poly_t A, const arb_poly_t B, slong prec)
{
const slong lenA = A->length, lenB = B->length;
if (lenB == 0 || arb_contains_zero(B->coeffs + lenB - 1))
{
return 0;
}
if (lenA < lenB)
{
arb_poly_set(R, A);
arb_poly_zero(Q);
return 1;
}
if (Q == A || Q == B)
{
arb_poly_t T;
arb_poly_init(T);
arb_poly_divrem(T, R, A, B, prec);
arb_poly_swap(Q, T);
arb_poly_clear(T);
return 1;
}
if (R == A || R == B)
{
arb_poly_t U;
arb_poly_init(U);
arb_poly_divrem(Q, U, A, B, prec);
arb_poly_swap(R, U);
arb_poly_clear(U);
return 1;
}
arb_poly_fit_length(Q, lenA - lenB + 1);
arb_poly_fit_length(R, lenB - 1);
_arb_poly_divrem(Q->coeffs, R->coeffs, A->coeffs, lenA,
B->coeffs, lenB, prec);
_arb_poly_set_length(Q, lenA - lenB + 1);
_arb_poly_set_length(R, lenB - 1);
_arb_poly_normalise(R);
return 1;
}