-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathidentify.mac
186 lines (168 loc) · 5.59 KB
/
identify.mac
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
177
178
179
180
181
182
183
184
185
186
/***********************************
**
** Integer relations package (identify.mac)
**
** Copyright: 2006, Andrej Vodopivec <[email protected]>
** Licence: GPL
**
** identify.mac implements identify function, which is used
** to identify floating point numbers as functions of known constatns.
**
** For detailed description see
**
** P.Borwein, K.G.Hare, A.Meichsner: Reverse symbolic computations, the
** identify function.
**
***************************
Examples:
(%i2) float(%e^2+%e-1);
(%o2) 9.107337927389695
(%i3) identify(%);
(%o3) %e^2+%e-1
(%i4) float(sin(%pi/30));
(%o4) 0.10452846326765
(%i5) identify(%);
(%o5) (sqrt(sqrt(5)-1)*sqrt(15))/(4*sqrt(2)*5^(1/4))-sqrt(5)/8-1/8
(%i6) float(%pi*%e/2);
(%o6) 4.269867111336783
(%i7) identify(%);
(%o7) (%e*%pi)/2
***********************/
define_variable(identify_sum_constants,
[%pi, %e, sqrt(2), sqrt(3), sqrt(5), log(2), log(3)], list)$
define_variable(identify_product_constants,
[%pi, %e, sqrt(2), sqrt(3), sqrt(5), log(2), log(3)], list)$
define_variable(identify_powersum_constants,
[%pi, %e, log(2), log(3)], list)$
define_variable(identify_extend,
[], list)$
define_variable(identify_powersum_max_degree,
3, fixnum)$
define_variable(identify_powersum_min_degree,
2, fixnum)$
define_variable(identify_algebraic_degree,
4, fixnum)$
if get(integer_relation, version)=false then load("integer_relation")$
identify_integer_relation(x, [treshold]) := block(
[relation : integer_relation(x)],
if length(treshold)#1 then treshold:100 else treshold:treshold[1],
if relation=false then return(false),
if apply("+", (map(abs, relation)))>treshold then return(false),
relation
)$
identify(x) :=
if atom(x) then (
if floatnump(x) or bfloatp(x) then block(
[res],
if errcatch (
res : identify_as_algebraic(x)
) = [] then res : false,
if res#false then return(res),
if errcatch (
res : identify_as_sum(x)
) = [] then res : false,
if res#false then return(res),
if errcatch (
res : identify_as_product(x)
) = [] then res : false,
if res#false then return(res),
if errcatch (
res : identify_as_powersum(x)
) = [] then res : false,
if res#false then return(res),
x
)
else x
)
else
map(identify, x)$
identify_as_sum(x) := block(
[floats, relation, res : false, i,
identify_sum_constants : append(identify_sum_constants, identify_extend)],
if floatnump(x) then floats : append([x], float(identify_sum_constants))
else (
x : bfloat(x),
if not(bfloatp(x)) then error("Argument to `identify_as_sum' is not a float!"),
floats : append([x], bfloat(identify_sum_constants))
),
relation : identify_integer_relation(floats),
if relation#false then (
res : 0,
for i:1 thru length(identify_sum_constants) do (
res : res + relation[i+1]*identify_sum_constants[i]
),
res : -res/relation[1]
),
res
)$
identify_as_algebraic(x) := block(
[floats, relation, res : false, i, pol, %x%, dif, r, rr, ratprint:false, sol],
if floatnump(x) then floats : makelist(x^i, i, 0, identify_algebraic_degree)
else (
x : bfloat(x),
if not(bfloatp(x)) then error("Argument to `identify_as_algebraic' is not a float!"),
floats : makelist(x^i, i, 0, identify_algebraic_degree)
),
relation : identify_integer_relation(floats, 100*identify_algebraic_degree),
if relation#false then (
pol : makelist(%x%^i, i, 0, identify_algebraic_degree) . relation,
sol : solve(pol, %x%),
for r in sol do (
r : rhs(r),
if numberp(rectform(float(r))) then (
if floatnump(x) then rr : float(r) else rr : bfloat(r),
if not(floatnump(r)) and cabs(rr-x)<10^-14 then res : r
)
)
),
res
)$
identify_as_product(x) := block(
[floats, relation, res : false,
identify_product_constants : append(identify_product_constants, identify_extend)],
if floatnump(x) then floats : append([log(abs(x))], float(map(log, identify_product_constants)))
else (
x : bfloat(x),
if not(bfloatp(x)) then error("Argument to `identify_as_sum' is not a float!"),
floats : append([log(abs(x))], bfloat(map(log, identify_product_constants)))
),
relation : identify_integer_relation(floats),
if relation#false then (
res : 1,
for i:1 thru length(identify_product_constants) do (
res : res * identify_product_constants[i]^relation[i+1]
),
res : res^(-1/relation[1])
)
else return(false),
if x<0 then
-res
else
res
)$
identify_as_powersum(x) := block(
[res : false, floats, relation,
identify_powersum_constants : append(identify_powersum_constants, identify_extend),
min_deg : identify_powersum_min_degree,
max_deg : identify_powersum_max_degree],
if not(floatnump(x)) then (
x : bfloat(x),
if not(bfloatp(x)) then error("Argument to `identify_as_powersum' is not a float!")
),
res : for c in identify_powersum_constants do (
if floatnump(x) then
floats : makelist(float(c^i), i, -min_deg, max_deg)
else
floats : makelist(bfloat(c^i), i, -min_deg, max_deg),
floats : append([x], floats),
relation : identify_integer_relation(floats),
if relation#false and relation[1]#0 then (
res : makelist(-relation[i+1]/relation[1], i,
1, min_deg + max_deg + 1),
res : res . makelist(c^i, i, -min_deg, max_deg),
return(res)
)
),
if res=done then return(false),
res
)$