forked from Z3Prover/z3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrcf.cpp
176 lines (160 loc) · 4.98 KB
/
rcf.cpp
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*++
Copyright (c) 2013 Microsoft Corporation
Module Name:
rcf.cpp
Abstract:
Testing RCF module
Author:
Leonardo (leonardo) 2013-01-04
Notes:
--*/
#include "math/realclosure/realclosure.h"
#include "math/realclosure/mpz_matrix.h"
#include "util/rlimit.h"
static void tst1() {
unsynch_mpq_manager qm;
reslimit rl;
rcmanager m(rl, qm);
scoped_rcnumeral a(m);
#if 0
a = 10;
std::cout << sym_pp(a) << std::endl;
std::cout << sym_pp(eps) << std::endl;
std::cout << interval_pp(a) << std::endl;
std::cout << interval_pp(eps) << std::endl;
#endif
scoped_rcnumeral eps(m);
m.mk_infinitesimal(eps);
mpq aux;
qm.set(aux, 1, 3);
m.set(a, aux);
#if 0
std::cout << interval_pp(a) << std::endl;
std::cout << decimal_pp(eps, 4) << std::endl;
std::cout << decimal_pp(a) << std::endl;
std::cout << a + eps << std::endl;
std::cout << a * eps << std::endl;
std::cout << (a + eps)*eps - eps << std::endl;
#endif
std::cout << interval_pp(a - eps*2) << std::endl;
std::cout << interval_pp(eps + 1) << std::endl;
scoped_rcnumeral t(m);
t = (a - eps*2) / (eps + 1);
std::cout << t << std::endl;
std::cout << t * (eps + 1) << std::endl;
a = 10;
std::cout << (a + eps > a) << std::endl;
scoped_rcnumeral pi(m);
m.mk_pi(pi);
std::cout << pi + 1 << std::endl;
std::cout << decimal_pp(pi) << std::endl;
std::cout << decimal_pp(pi + 1) << std::endl;
scoped_rcnumeral e(m);
m.mk_e(e);
t = e + (pi + 1)*2;
std::cout << t << std::endl;
std::cout << decimal_pp(t, 10) << std::endl;
std::cout << (eps + 1 > 1) << std::endl;
std::cout << interval_pp((a + eps)/(a - eps)) << std::endl;
}
static void tst2() {
enable_trace("mpz_matrix");
unsynch_mpq_manager nm;
small_object_allocator allocator;
mpz_matrix_manager mm(nm, allocator);
scoped_mpz_matrix A(mm);
mm.mk(3, 3, A);
// Matrix
// 1 1 1
// 0 1 -1
// 0 1 1
A.set(0, 0, 1); A.set(0, 1, 1); A.set(0, 2, 1);
A.set(1, 0, 0); A.set(1, 1, 1); A.set(1, 2, -1);
A.set(2, 0, 0); A.set(2, 1, 1); A.set(2, 2, 1);
std::cout << A;
{
int b[3];
int c[3] = { 10, -2, 8 };
std::cout << "solve: " << mm.solve(A, b, c) << "\n";
for (unsigned i = 0; i < 3; i++) std::cout << b[i] << " ";
std::cout << "\n";
}
scoped_mpz_matrix A2(mm);
mm.tensor_product(A, A, A2);
std::cout << A2;
scoped_mpz_matrix B(mm);
unsigned cols[] = { 1, 3, 7, 8 };
mm.filter_cols(A2, 4, cols, B);
std::cout << B;
scoped_mpz_matrix C(mm);
unsigned perm[] = { 8, 7, 6, 5, 4, 3, 2, 1, 0 };
mm.permute_rows(B, perm, C);
std::cout << C;
}
static void tst_solve(unsigned n, int _A[], int _b[], int _c[], bool solved) {
unsynch_mpq_manager nm;
small_object_allocator allocator;
mpz_matrix_manager mm(nm, allocator);
scoped_mpz_matrix A(mm);
mm.mk(n, n, A);
for (unsigned i = 0; i < n; i++)
for (unsigned j = 0; j < n; j++)
A.set(i, j, _A[i*n + j]);
svector<int> b;
b.resize(n, 0);
if (mm.solve(A, b.data(), _c)) {
ENSURE(solved);
for (unsigned i = 0; i < n; i++) {
ENSURE(b[i] == _b[i]);
}
}
else {
ENSURE(!solved);
}
}
static void tst_lin_indep(unsigned m, unsigned n, int _A[], unsigned ex_sz, unsigned ex_r[]) {
unsynch_mpq_manager nm;
small_object_allocator allocator;
mpz_matrix_manager mm(nm, allocator);
scoped_mpz_matrix A(mm);
mm.mk(m, n, A);
for (unsigned i = 0; i < m; i++)
for (unsigned j = 0; j < n; j++)
A.set(i, j, _A[i*n + j]);
unsigned_vector r;
r.resize(A.n());
scoped_mpz_matrix B(mm);
mm.linear_independent_rows(A, r.data(), B);
for (unsigned i = 0; i < ex_sz; i++) {
ENSURE(r[i] == ex_r[i]);
}
}
static void tst_denominators() {
reslimit rl;
unsynch_mpq_manager qm;
rcmanager m(rl, qm);
scoped_rcnumeral a(m);
scoped_rcnumeral t(m);
scoped_rcnumeral eps(m);
m.mk_pi(a);
m.inv(a);
m.mk_infinitesimal(eps);
t = (a - eps*2) / (a*eps + 1);
// t = t + a * 2;
scoped_rcnumeral n(m), d(m);
std::cout << t << "\n";
m.clean_denominators(t, n, d);
std::cout << "---->\n" << n << "\n" << d << "\n";
}
void tst_rcf() {
enable_trace("rcf_clean");
enable_trace("rcf_clean_bug");
tst_denominators();
tst1();
tst2();
{ int A[] = {0, 1, 1, 1, 0, 1, 1, 1, -1}; int c[] = {10, 4, -4}; int b[] = {-2, 4, 6}; tst_solve(3, A, b, c, true); }
{ int A[] = {1, 1, 1, 0, 1, 1, 0, 1, 1}; int c[] = {3, 2, 2}; int b[] = {1, 1, 1}; tst_solve(3, A, b, c, false); }
{ int A[] = {1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, -1}; unsigned r[] = {0, 1, 4}; tst_lin_indep(5, 3, A, 3, r); }
{ int A[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, -1}; unsigned r[] = {0, 4}; tst_lin_indep(5, 3, A, 2, r); }
{ int A[] = {1, 1, 1, 1, 1, 1, 1, 0, 1, 2, 1, 2, 3, 1, 3}; unsigned r[] = {0, 2}; tst_lin_indep(5, 3, A, 2, r); }
}