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
/*
Copyright (C) 2022 Fredrik Johansson
This file is part of FLINT.
FLINT 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 3 of the License, or
(at your option) any later version. See <https://www.gnu.org/licenses/>.
*/
#include "fmpzi.h"
slong
fmpzi_remove_one_plus_i(fmpzi_t res, const fmpzi_t x)
{
slong s, t;
int odd;
if (fmpzi_is_zero(x))
{
fmpzi_zero(res);
return 0;
}
if (fmpz_is_zero(fmpzi_imagref(x)))
{
s = fmpz_val2(fmpzi_realref(x));
odd = 0;
}
else if (fmpz_is_zero(fmpzi_realref(x)))
{
s = fmpz_val2(fmpzi_imagref(x));
odd = 0;
}
else
{
s = fmpz_val2(fmpzi_realref(x));
t = fmpz_val2(fmpzi_imagref(x));
if (s == t)
{
odd = 1;
}
else
{
s = FLINT_MIN(s, t);
odd = 0;
}
}
if (s == 0)
{
fmpzi_mul_i_pow_si(res, x, -s);
}
else
{
fmpz_tdiv_q_2exp(fmpzi_realref(res), fmpzi_realref(x), s);
fmpz_tdiv_q_2exp(fmpzi_imagref(res), fmpzi_imagref(x), s);
fmpzi_mul_i_pow_si(res, res, -s);
}
if (odd)
{
/* (a+bi) / (1+i) = ((a+b)/2) + ((b-a)/2)i */
fmpz_t t;
fmpz_init(t);
fmpz_add(t, fmpzi_realref(res), fmpzi_imagref(res));
fmpz_sub(fmpzi_imagref(res), fmpzi_imagref(res), fmpzi_realref(res));
fmpz_tdiv_q_2exp(fmpzi_realref(res), t, 1);
fmpz_tdiv_q_2exp(fmpzi_imagref(res), fmpzi_imagref(res), 1);
fmpz_clear(t);
}
return 2 * s + odd;
}