forked from flintlib/arb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapprox_mul.c
139 lines (121 loc) · 3.6 KB
/
approx_mul.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
Copyright (C) 2018 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_mat.h"
void
arb_mat_approx_mul_classical(arb_mat_t C, const arb_mat_t A, const arb_mat_t B, slong prec)
{
slong ar, br, bc, i, j, k;
ar = arb_mat_nrows(A);
br = arb_mat_nrows(B);
bc = arb_mat_ncols(B);
if (br == 0)
{
arb_mat_zero(C);
return;
}
if (A == C || B == C)
{
arb_mat_t T;
arb_mat_init(T, ar, bc);
arb_mat_approx_mul_classical(T, A, B, prec);
arb_mat_swap_entrywise(T, C);
arb_mat_clear(T);
return;
}
if (br <= 2)
{
for (i = 0; i < ar; i++)
{
for (j = 0; j < bc; j++)
{
arf_mul(arb_midref(arb_mat_entry(C, i, j)),
arb_midref(arb_mat_entry(A, i, 0)),
arb_midref(arb_mat_entry(B, 0, j)), prec, ARB_RND);
for (k = 1; k < br; k++)
{
arf_addmul(arb_midref(arb_mat_entry(C, i, j)),
arb_midref(arb_mat_entry(A, i, k)),
arb_midref(arb_mat_entry(B, k, j)), prec, ARB_RND);
}
}
}
}
else
{
arb_ptr tmp;
TMP_INIT;
TMP_START;
tmp = TMP_ALLOC(sizeof(arb_struct) * br * bc);
for (i = 0; i < br; i++)
for (j = 0; j < bc; j++)
tmp[j * br + i] = *arb_mat_entry(B, i, j);
for (i = 0; i < ar; i++)
{
for (j = 0; j < bc; j++)
{
arb_approx_dot(arb_mat_entry(C, i, j), NULL, 0,
A->rows[i], 1, tmp + j * br, 1, br, prec);
}
}
TMP_END;
}
}
void
arb_mat_approx_mul(arb_mat_t C, const arb_mat_t A, const arb_mat_t B, slong prec)
{
slong cutoff;
/* todo: detect small-integer matrices */
if (prec <= 2 * FLINT_BITS)
cutoff = 120;
else if (prec <= 16 * FLINT_BITS)
cutoff = 60;
else
cutoff = 40;
if (arb_mat_nrows(A) <= cutoff || arb_mat_ncols(A) <= cutoff ||
arb_mat_ncols(B) <= cutoff)
{
arb_mat_approx_mul_classical(C, A, B, prec);
}
else
{
if (arb_mat_is_exact(A) && arb_mat_is_exact(B))
{
arb_mat_mul(C, A, B, prec);
}
else
{
arb_mat_t AM, BM;
if (arb_mat_is_exact(A))
{
arb_mat_init(BM, arb_mat_nrows(B), arb_mat_ncols(B));
arb_mat_get_mid(BM, B);
arb_mat_mul(C, A, BM, prec);
arb_mat_clear(BM);
}
else if (arb_mat_is_exact(B))
{
arb_mat_init(AM, arb_mat_nrows(A), arb_mat_ncols(A));
arb_mat_get_mid(AM, A);
arb_mat_mul(C, AM, B, prec);
arb_mat_clear(AM);
}
else
{
arb_mat_init(BM, arb_mat_nrows(B), arb_mat_ncols(B));
arb_mat_get_mid(BM, B);
arb_mat_init(AM, arb_mat_nrows(A), arb_mat_ncols(A));
arb_mat_get_mid(AM, A);
arb_mat_mul(C, AM, BM, prec);
arb_mat_clear(AM);
arb_mat_clear(BM);
}
}
arb_mat_get_mid(C, C);
}
}