Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Nov 20, 2020. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathigsl.lua
More file actions
Latest commit
210 lines (180 loc) · 4.68 KB
/
Copy pathigsl.lua
File metadata and controls
210 lines (180 loc) · 4.68 KB
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
-- igsl.lua
--
-- Copyright (C) 2009 Francesco Abbate
--
-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation; either version 3 of the License, or (at
-- your option) any later version.
--
-- This program is distributed in the hope that it will be useful, but
-- WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-- General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program; if not, write to the Free Software
-- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
--
ifnothave_complexthen
conj= |x| x
real=conj
imag= |x| 0
end
localcat=table.concat
localfmt=string.format
localfunctionpush(ls, e)
ls[#ls+1] =e
returnls
end
functionmatrix_f_set(m, f)
localr, c=m:dims()
fori=1, rdo
forj=1, cdo
localz=f(i, j)
m:set(i, j, z)
end
end
returnm
end
functionmatrix_reduce(m, f, accu)
localr, c=m:dims()
fori=1, rdo
forj=1, cdo
accu=f(accu, m:get(i, j))
end
end
returnaccu
end
functionmatrix_reduce_rowcol(m, fcol, ac0, frow, ar0)
localr, c=m:dims()
localgetnew=type(ar0) =='table' and || {} or || ar0
fori=1, rdo
localar=getnew()
forj=1, cdo
localz=m:get(i,j)
ar=frow(ar, z)
end
ac0=fcol(ac0, ar)
end
returnac0
end
localfunctiontostring_eps(z, eps)
locala, b=real(z), imag(z)
localf= |x| fmt('%g', x)
locals=abs(a) >epsandf(a) or''
ifb>epsthen
localsign= (s=='' and'' or'+')
s=s..fmt('%s%si', sign, f(b))
elseifb<-epsthen
s=s..fmt('-%si', f(-b))
end
returns=='' and'0' ors
end
localfunctionmatrix_from_table(ctor, t)
returnmatrix_f_set(ctor(#t, #t[1]), |i,j| t[i][j])
end
localfunctionvector_from_table(ctor, t)
localv=ctor (#t, 1)
fori, xinipairs(t) dov:set(i,1, x) end
returnv
end
functionvector(t)
returnvector_from_table(new, t)
end
functioncvector(t)
returnvector_from_table(cnew, t)
end
functionmatrix(t)
returnmatrix_from_table(new, t)
end
functioncmatrix(t)
returnmatrix_from_table(cnew, t)
end
functionmatrix_print(m)
localeps=m:norm() *1e-8
localfwidth=function(w, val)
localln=#tostring_eps(val, eps)
return (ln>wandlnorw)
end
localwidth=matrix_reduce(m, fwidth, 0)
localpad= |s| string.rep('', width-#s) ..s
locallines=
matrix_reduce_rowcol(m,
|lns, ln| push(lns, '[ ' ..cat(ln, '') ..' ]'),
{},
|ln, z| push(ln, pad(tostring_eps(z, eps))),
{})
returncat(lines, '\n')
end
functiont(m)
localr, c=m:dims()
returnnew(c, r, |i,j| m:get(j,i))
end
functionh(m)
localr, c=m:dims()
returncnew(c, r, |i,j| conj(m:get(j,i)))
end
functiondiag(v)
localn=v:dims()
returnnew(n, n, |i,j| i==jandv:get(i,1) or0)
end
functionunit(n)
returnnew(n, n, |i,j| i==jand1or0)
end
functionmatrix_norm(m)
localsq=matrix_reduce(m, |p, z| p+z*conj(z), 0)
returnsqrt(sq)
end
functionmatrix_columns (m, cstart, cnb)
localr=m:dims()
returnm:slice(1, cstart, r, cnb)
end
functionmatrix_row_print(m)
localeps=m:norm() *1e-8
localf= |p, z| push(p, tostring_eps(z, eps))
returncat(matrix_reduce(m, f, {}), ', ')
end
functionset(d, s)
matrix_f_set(d, |i,j| s:get(i,j))
end
functionnull(m)
matrix_f_set(m, |i,j| 0)
end
localfunctionadd_matrix_method(s, m)
Matrix[s] =m
ifhave_complexthen
cMatrix[s] =m
end
end
functionode_iter(s, t0, y0, t1, tstep)
s:set(t0, y0)
returnfunction()
localt, y=s.t, s.y
ift<t1then
s:evolve(t1, tstep)
returnt, y
end
end
end
localfunctionhc_reduce(hc, f, accu)
localn=hc.length
fori=0, ndoaccu=f(accu, hc:get(i)) end
returnaccu
end
localfunctionhc_print(hc)
localeps=1e-8*hc_reduce(hc, |p,z| p+z*conj(z), 0)
localf= |p,z| push(p, fmt('%6i: %s', #p, tostring_eps(z, eps)))
returncat(hc_reduce(hc, f, {}), '\n')
end
ifhave_complexthen
FFT_hc_mixed_radix.__tostring=hc_print
FFT_hc_radix2.__tostring=hc_print
end
ODE.iter=ode_iter
ifhave_complexthencODE.iter=ode_iterend
add_matrix_method('rowiter', matrix_rowiter)
add_matrix_method('__tostring', matrix_print)
add_matrix_method('norm', matrix_norm)
add_matrix_method('columns', matrix_columns)
add_matrix_method('row_print', matrix_row_print)