aboutsummaryrefslogtreecommitdiff
path: root/semestr-3/anm/pracowniaPOP/prog/program.jl
blob: d75f82b6283ea887c09e7c991aed4d550d325d35 (plain)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
using Printf
 
# stałe dla CORDIC'A
global C_ITERATIONS = 30
global CORDIC_MUL_POW = 30
global CORDIC_MUL = 2.0^CORDIC_MUL_POW
global CORDIC_ATANS = [843314857, 497837829, 263043837, 133525159, 67021687, 33543516, 16775851,
                8388437, 4194283, 2097149, 1048576, 524288, 262144, 131072, 65536, 32768, 16384, 8192, 4096,
                2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2]
global CORDIC_F = 1768195363
global CORDIC_F_INV = 652032874
 
# stałe dla obliczania szeregiem Taylora
global T_ITERATIONS = 15
global HYPERBOLIC_MAX = 1
 
# liczenie szeregu Taylora
function series(x, parity, change_sign, iterations)
    res = zero(x)
    elem = one(x)
    if parity == 1
        elem = x
    end
    i = parity + 1
    while i <= 2*iterations + parity
        res += elem
        elem *= change_sign*x*x/(i*(i+1))
        i += 2
    end
    return res
end
 
# generyczna funkcja stosująca wzory redukcyjne, licząca sin(x)
# za pomocą podanych funkcji sin_fun, cos_fun 
function gen_sin(x, iterations, sin_fun, cos_fun)
    # sin(-x) = sin(x)
    if x < 0
        return -gen_sin(-x, iterations, sin_fun, cos_fun)
    end
    x = mod2pi(x)
    # sin(π + x) = -sin(x)
    if x > pi
        return -gen_sin(x-pi, iterations, sin_fun, cos_fun)
    end
    # sin(π/2 + x) = cos(x)
    if x > pi/2
        return gen_cos(x-pi/2, iterations, sin_fun, cos_fun)
    end
    # sin(π/2 - x) = cos(x)
    if x > pi/4
        return gen_cos(pi/2-x, iterations, sin_fun, cos_fun)
    end
    return sin_fun(x, iterations)
end
 
# generyczna funkcja stosująca wzory redukcyjne, licząca cos(x)
# za pomocą podanych funkcji sin_fun, cos_fun 
function gen_cos(x, iterations, sin_fun, cos_fun)
    # cos(-x) = cos(x)
    if x < 0
        return gen_cos(-x, iterations, sin_fun, cos_fun)
    end
    x = mod2pi(x)
    # cos(π + x) = -cos(x)
    if x > pi
        return -gen_cos(x-pi, iterations, sin_fun, cos_fun)
    end
    # cos(π/2 + x) = -sin(x)
    if x > pi/2
        return -gen_sin(x-pi/2, iterations, sin_fun, cos_fun)
    end
    # cos(π/2 - x) = sin(x)
    if x > pi/4
        return gen_sin(pi/2-x, iterations, sin_fun, cos_fun)
    end
    return cos_fun(x, iterations)
end
 
# sin dla liczb rzeczywistych [Taylor]
function real_sin(r, iterations)
    return series(r, 1, -1, iterations)
end
 
# cos dla liczb rzeczywistych [Taylor]
function real_cos(r, iterations)
    return series(r, 0, -1, iterations)
end
 
# sinh [Taylor]
function real_sinh(r, iterations)
    # sinh(1000) jest za duży by reprezentować go we Float64
    if r > 1000
        return Inf
    end
    if r < -1000
        return -Inf
    end
    if r == 0
        return Float64(0)
    end
    # dla dużych liczb korzystamy ze wzoru:
    # sinh(2r) = 2 * cosh(r) * sinh(r)
    if abs(r) > HYPERBOLIC_MAX
        return 2*real_sinh(r/2, iterations)*real_cosh(r/2, iterations)
    end
    return series(r, 1, 1, iterations)
end
 
# cosh [Taylor]
function real_cosh(r, iterations)
    # cosh(1000) jest za duży by reprezentować go we Float64
    if abs(r) > 1000
        return Inf
    end
    if r == 1
        return Float64(1)
    end
    # dla dużych liczb korzystamy ze wzoru:
    # cosh(2r) = cosh(r)^2 + sinh(r)^2
    if abs(r) > HYPERBOLIC_MAX
        s = real_sinh(r/2, iterations)
        c = real_cosh(r/2, iterations)
        return s*s+c*c
    end
    return series(r, 0, 1, iterations)
end
 
# sin dla liczb zespolonych [Taylor]
function complex_sin(a, b, iterations)
    # sin(a + bi) = sin(a) * cosh(b) + i(cos(a) * sinh(b))
    return (gen_sin(a, iterations, real_sin, real_cos)*real_cosh(b, iterations), 
            gen_cos(a, iterations, real_sin, real_cos)*real_sinh(b, iterations)) 
end
 
# cos dla liczb zespolonych [Taylor]
function complex_cos(a, b, iterations)
    # cos(a + bi) = cos(a) * cosh(b) - i(sin(a) * sinh(b))
    return (real_cos(a, iterations)*real_cosh(b, iterations),
            -real_sin(a, iterations)*real_sinh(b, iterations))
end
 
# funkcja sin dla użytkownika [Taylor]
function taylor_sin(a, b)
    return complex_sin(a, b, T_ITERATIONS)
end
 
# funkcja cos dla użytkownika [Taylor]
function taylor_cos(a, b)
    return complex_cos(a, b, T_ITERATIONS)
end
 
# funkcja sinh dla użytkownika [Taylor]
function taylor_sinh(r)
    return real_sinh(r, T_ITERATIONS)
end
 
# funkcja cosh dla użytkownika [Taylor]
function taylor_cosh(r)
    return real_cosh(r, T_ITERATIONS)
end
 
# preprocesing [CORDIC]
function preprocess_atan(iterations)
    global CORDIC_MUL
    atan2pow = Array{Float64}(undef, iterations)
    @printf("CORDIC_ATANS = [")
    for i in 1:iterations
        atan2pow[i] = round(atan(1.0 / Float64(BigInt(2)^(i - 1))) * CORDIC_MUL)
        @printf("%d", atan2pow[i])  
        if i < iterations
            @printf(", ")
        end
    end
    @printf("]\n")
end
 
 
# preprocesing [CORDIC]
function preprocess_scaling_factor(iterations)
    CORDIC_F = 1.0
    for i in 0:iterations
        CORDIC_F *= sqrt(1. + 1. / Float64(BigInt(2)^(2 * i)))
    end
    @printf("CORDIC_F = %d\nCORDIC_F_INV = %d\n", round(CORDIC_F * CORDIC_MUL), round(CORDIC_MUL / CORDIC_F))
end
 
 
# funkcja licząca zarówno cosx oraz sinx algorytmem CORDIC
function approx_trig(x, iterations)
    global CORDIC_ATANS
    global CORDIC_F_INV
    X = CORDIC_F_INV
    Y = 0
    Z = round(x * CORDIC_MUL)
    s = 1
    # Proces iteracyjny algorytmu CORDIC
    for i in 0:(iterations - 1)
        tempX = X
        if Z == 0
            break
        end
        if Z >= 0
            X -= s * (Y >> i)
            Y += s * (tempX >> i)
            Z -= s * CORDIC_ATANS[i + 1]
        else
            X += s * (Y >> i)
            Y -= s * (tempX >> i)
            Z += s * CORDIC_ATANS[i + 1]
        end
    end
 
    return (Float64(X) / CORDIC_MUL, Float64(Y) / CORDIC_MUL)
end
 
# wyciąganie sin z approx_trig [CORDIC] 
function approx_sin(x, iterations)
    return approx_trig(x, iterations)[2]
end
 
 
# wyciąganie cos z approx_trig [CORDIC]
function approx_cos(x, iterations)
    return approx_trig(x, iterations)[1]
end
 
# funkcja sin dla użytkownika [CORDIC]
function cordic_sin(x)
    return gen_sin(x, C_ITERATIONS, approx_sin, approx_cos)
end
 
# funkcja cos dla użytkownika [CORDIC]
function cordic_cos(x)
    return gen_cos(x, C_ITERATIONS, approx_sin, approx_cos)
end
 
# uruchamianie preprocesingu [CORDIC]
# funkcja wypisuje kod w języku Julia na ekran, który potem po prostu wkleiliśmy do pliku źródłowego
# oblicza stałe potrzebne do obliczania funkcji trygonometrycznych metodą CORDIC
function preprocess_cordic()
    println("Preprocessing CORDIC constants.")
    preprocess_atan(CORDIC_MUL_POW)
    preprocess_scaling_factor(CORDIC_MUL_POW)
end
 
# sinh bez stosowania wzorów redukcyjnych [Taylor]
function sinh_no_reduction(x, iterations)
    return series(x, 1, 1, iterations)
end
 
# cosh bez stosowania wzorów redukcyjnych [Taylor]
function cosh_no_reduction(x, iterations)
    return series(x, 0, 1, iterations)
end
 
# sin bez stosowania wzorów redukcyjnych [Taylor]
function taylor_sin_no_reduction(x, y)
    # sin(a + bi) = sin(a) * cosh(b) + i(cos(a) * sinh(b))
    # wykonujemy odpowiednio (10a + 10), (10b + 10) iteracji - szereg Tylora
    # powinien dobrze przybliżać funkcje trygonometryczne dla takiej liczby wyrazów
    return (real_sin(x, 10*round(x)+10) * cosh_no_reduction(y, 10*round(y)+10),
            real_cos(x, 10*round(x)+10) * sinh_no_reduction(y, 10*round(y)+10))
end
 
# zmiana liczby iteracji [Taylor]
function set_taylor_iterations(x)
    global T_ITERATIONS = x    
end
 
# zmiana liczby iteracji [CORDIC]
function set_cordic_iterations(x)
    global C_ITERATIONS = x    
end