appmath 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,130 @@
1
+ #!/usr/bin/env ruby
2
+ =begin rdoc
3
+ ruby
4
+
5
+ Ulrich Mutze, www.ulrichmutze.de
6
+
7
+ Studying the conserved quantities in 2-dimensional Kepler motion
8
+
9
+ =end
10
+
11
+ =begin
12
+ Copyright (C) 2008 Ulrich Mutze
13
+
14
+ This program is free software: you can redistribute it and/or modify
15
+ it under the terms of the GNU General Public License as published by
16
+ the Free Software Foundation, either version 3 of the License, or
17
+ (at your option) any later version.
18
+
19
+ This program is distributed in the hope that it will be useful,
20
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
21
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
+ GNU General Public License for more details.
23
+
24
+ You should have received a copy of the GNU General Public License
25
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
26
+ =end
27
+
28
+ require File.join(File.dirname(__FILE__), '../lib/graph')
29
+ require File.join(File.dirname(__FILE__), '../lib/kepler_2d')
30
+ #require 'graph'
31
+ #require 'kepler_2d'
32
+
33
+ include AppMath
34
+
35
+ ###################### input area ########################################
36
+
37
+ # selection parameter, presently only sel = 1
38
+ # See explanations in the corresponding 'when' statement line
39
+ sel = 1
40
+ prec = 6
41
+
42
+ ################### end of input area ####################################
43
+
44
+ case sel
45
+
46
+ when 1 # Test of class Kep2D
47
+ R.prec = prec
48
+
49
+ # These three numbers give all reals of the application the right type.
50
+ pi = R.pi
51
+ zero = R.c0
52
+ r = R.c 5
53
+ g = R.c 10
54
+ # No symbol R needed in what follows.
55
+
56
+ puts "r.class = " + r.class.to_s
57
+ puts "pi.class = " + pi.class.to_s
58
+
59
+ vr = (g/r).sqrt
60
+ v = R2.new(zero,vr)
61
+ p = R2.new(r,zero)
62
+ t_tot = pi*r*2/vr
63
+ omega = (pi * 2)/t_tot
64
+ n = 100
65
+ dt = t_tot/(n-1)
66
+
67
+ sys = Kep2D.new(p,v,g)
68
+ e0 = sys.energy
69
+ am0 = sys.ang_mom
70
+ le0 = sys.lenz
71
+
72
+ arr_x = Array.new
73
+ arr_y = Array.new
74
+ arr_e = Array.new
75
+ arr_am = Array.new
76
+ arr_le_x = Array.new
77
+ arr_le_y = Array.new
78
+ arr_i = Array.new
79
+
80
+ for i in 1..n
81
+ sys.step!(dt)
82
+ alpha = sys.get_t * omega
83
+ arr_x << (sys.get_x - r * alpha.cos ).to_f
84
+ arr_y << (sys.get_y - r * alpha.sin).to_f
85
+ arr_e << (sys.energy - e0).to_f
86
+ arr_am << (sys.ang_mom - am0).to_f
87
+ arr_le_x << (sys.lenz - le0).x.to_f
88
+ arr_le_y << (sys.lenz - le0).y.to_f
89
+ arr_i << i.to_f
90
+ end
91
+
92
+ root = TkRoot.new{
93
+ title "Test of class Kep2D at precision #{R.prec}"
94
+ }
95
+
96
+ nx=1200
97
+ ny=700
98
+
99
+ px = 3
100
+ py = 2
101
+
102
+ grs = Graphs.new(root,px,py,nx,ny)
103
+
104
+ gr1 = grs.at(0,0)
105
+ gr2 = grs.at(0,1)
106
+ gr3 = grs.at(0,2)
107
+ gr4 = grs.at(1,0)
108
+ gr5 = grs.at(1,1)
109
+ gr6 = grs.at(1,2)
110
+
111
+ gr1.grid_color = 'white'
112
+ gr1.bgr_color = 'lightgray'
113
+ gr2.grid_color = 'white'
114
+ gr2.bgr_color = 'lightgray'
115
+ gr3.grid_color = 'white'
116
+ gr3.bgr_color = 'lightgray'
117
+
118
+ gr1.draw(arr_i, arr_le_x, 'red')
119
+ gr2.draw(arr_i, arr_le_y, 'cyan')
120
+ gr3.draw(arr_le_x, arr_le_y, 'blue')
121
+ gr4.draw(arr_i, arr_e, 'white')
122
+ gr5.draw(arr_i, arr_am, 'magenta')
123
+
124
+ Tk.mainloop
125
+
126
+ else
127
+
128
+ puts "unvalid selection, nothing to do"
129
+
130
+ end
@@ -0,0 +1,193 @@
1
+ #!/usr/bin/env ruby
2
+ =begin rdoc
3
+ ruby
4
+
5
+ Ulrich Mutze www.ulrichmutze.de
6
+
7
+ 2008-11-30
8
+
9
+ Experimentation with Singular Value Decomposition in R
10
+
11
+ =end
12
+
13
+ =begin
14
+ Copyright (C) 2008 Ulrich Mutze
15
+
16
+ This program is free software: you can redistribute it and/or modify
17
+ it under the terms of the GNU General Public License as published by
18
+ the Free Software Foundation, either version 3 of the License, or
19
+ (at your option) any later version.
20
+
21
+ This program is distributed in the hope that it will be useful,
22
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
23
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
+ GNU General Public License for more details.
25
+
26
+ You should have received a copy of the GNU General Public License
27
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
28
+ =end
29
+
30
+ require File.join(File.dirname(__FILE__), '../lib/linalg')
31
+ require File.join(File.dirname(__FILE__), '../lib/cnum')
32
+
33
+ include AppMath
34
+
35
+ ###################### input area ########################################
36
+
37
+ # selection parameter
38
+ # n = 2 test class Vet
39
+ # n = 3 test class Mat and it is the most telling of the tests
40
+ sel = 3
41
+
42
+ # prec = 0 for Float computation , ... = 20 (e.g. precision 20, i.e. 20
43
+ # decimal places).
44
+ prec = 0
45
+
46
+ # We treat a random (n,n)-matrix for sel = 3 and a random n-vector
47
+ # for sel = 2.
48
+ n = 50
49
+
50
+ verbose = false
51
+ complex = false
52
+
53
+ ################### end of input area ####################################
54
+
55
+
56
+ R.prec = prec
57
+
58
+ case sel
59
+ when 1
60
+ n = 100
61
+ zero = complex ? C.zero : R.zero
62
+
63
+ t1 = Time.now
64
+ a = Mat.tob(n,12, complex)
65
+ w = Vec.new(n,zero)
66
+ v = Mat.new(n,n,zero)
67
+
68
+ if verbose
69
+ a.prn("a_input")
70
+ end
71
+ aMem = a.clone
72
+
73
+ if complex
74
+ csvdcmp(a,w,v)
75
+ else
76
+ Mat.svdcmp(a,w,v)
77
+ end
78
+
79
+ u = a
80
+
81
+ aRec = u * Mat.new(w) * v.trp
82
+
83
+ utu = u.trp * u
84
+ vtv = v.trp * v
85
+
86
+ utu1 = utu.to_1
87
+ vtv1 = vtv.to_1
88
+
89
+ if verbose
90
+ utu.prn("utu")
91
+ vtv.prn("vtv")
92
+ end
93
+
94
+ s = R.c0
95
+
96
+ l = aMem
97
+ r = aRec
98
+ ds = l.dis(r)
99
+ ds.prn("ds1")
100
+ s += ds
101
+
102
+ l = utu
103
+ r = utu1
104
+ ds = l.dis(r)
105
+ ds.prn("ds2")
106
+ s += ds
107
+
108
+ l = vtv
109
+ r = vtv1
110
+ ds = l.dis(r)
111
+ ds.prn("ds3")
112
+ s += ds
113
+
114
+ t2 = Time.now
115
+
116
+ if verbose
117
+ a.prn("a_output")
118
+ puts
119
+ w.prn("w_output")
120
+ puts
121
+ v.prn("v_output")
122
+ end
123
+ puts
124
+ s.prn("error sum")
125
+ (t2-t1).prn("computation time")
126
+
127
+ when 2
128
+ Vec.test(n,verbose,complex)
129
+
130
+ when 3
131
+ Mat.test(n,verbose,complex)
132
+
133
+ when 4
134
+ R.test(137,verbose)
135
+
136
+ when 5
137
+ C.test(137,verbose)
138
+
139
+ when 6
140
+ n = 16
141
+ w = Vec.tob(n,137)
142
+ w.prn("w")
143
+ wpi = w.pseudo_inv
144
+ wpi.prn("wpi")
145
+
146
+ when 7
147
+ t1 = Time.now
148
+ x = R.new("1.23456789E123")
149
+ diff = (x.sin**2 + x.cos**2 - 1).abs.log10.round
150
+ t2 = Time.now
151
+ dt = t2-t1
152
+ puts "diff = #{diff}, computation time was #{dt} s"
153
+
154
+ when 8
155
+ # input
156
+ prec = 9
157
+ dim = 32
158
+ i = 137
159
+ dig_round = 6
160
+ complex = false
161
+ # computation
162
+ R.prec = prec
163
+ x = Vec.tob(dim,i,complex)
164
+ x.each{ |x|
165
+ puts x.round(dig_round).to_s
166
+ }
167
+
168
+ when 9
169
+ prec = "f"
170
+ R.prec = prec
171
+ n = 20
172
+ for i in 1..n
173
+ # puts R.ran(i).to_s
174
+ puts R.tob(i).to_s
175
+ end
176
+
177
+ when 10
178
+ for i in 0..8
179
+ R.prec = 20*i
180
+ # some computation, e. g.
181
+ x = [R.c2, R.i2, R.i(0.33333),R.ran(137), R.tob(3)]
182
+ puts "precision set to #{R.prec}"
183
+ x.each{ |x|
184
+ diff = x.sin**2 +x.cos**2 - 1
185
+ puts "x = #{x.round(6)}, diff = #{diff}"
186
+ }
187
+ end
188
+
189
+ else
190
+ puts "Not even under construction"
191
+
192
+ end # case
193
+
@@ -0,0 +1,199 @@
1
+ #!/usr/bin/env ruby
2
+ =begin rdoc
3
+ ruby
4
+
5
+ Ulrich Mutze, www.ulrichmutze.de
6
+
7
+ Simple test application for real and complex numbers and for graphical
8
+ representation of curves.
9
+
10
+ =end
11
+
12
+ =begin
13
+ Copyright (C) 2008 Ulrich Mutze
14
+
15
+ This program is free software: you can redistribute it and/or modify
16
+ it under the terms of the GNU General Public License as published by
17
+ the Free Software Foundation, either version 3 of the License, or
18
+ (at your option) any later version.
19
+
20
+ This program is distributed in the hope that it will be useful,
21
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
22
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
+ GNU General Public License for more details.
24
+
25
+ You should have received a copy of the GNU General Public License
26
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
27
+ =end
28
+
29
+ =begin
30
+ History:
31
+ 2008-11-01
32
+ Experimentation with BigDecimal
33
+ The most useful thing to do seems to create a class of reals
34
+ of arbitrary precission.
35
+ Unfortunately many functions are implemented by the
36
+ most simple power series expansion. These have to reimplemented to become
37
+ useful.
38
+ I should do this! action!
39
+ UM 2008-11-04: essentially done! For final testing I should have a
40
+ graphical test facility which I'm developing presently
41
+ Aritmetics seems to be pretty fast
42
+ Add the case R=Float for @@dig=0 (or <= 0 ?) action! done
43
+ perhaps 10^x as primary and e^x=exp(x) as derived from it, could be faster
44
+ and more accurate action! done
45
+ =end
46
+
47
+ require File.join(File.dirname(__FILE__), '../lib/graph')
48
+ require File.join(File.dirname(__FILE__), '../lib/cnum')
49
+ #require 'graph'
50
+ #require 'cnum'
51
+
52
+ include AppMath
53
+
54
+
55
+ ###################### input area ########################################
56
+ # selection parameter
57
+ # See explanations in the corresponding 'when' statement line
58
+ sel = 1
59
+
60
+ ################### end of input area ####################################
61
+ case sel
62
+ when 1 # Test of classes Graphs and R and function objects
63
+
64
+ # precision for the overall computation
65
+ prec1 = 0
66
+ # precision for the red curves
67
+ prec2 = 2
68
+ # pixels in x-direction
69
+ nx = 1200
70
+ # pixels in y-direction
71
+ ny = 750
72
+ # Number of points of each function graph
73
+ n = 100
74
+ # The functions are represented over the x-interval [ -x_max , + x_max ]
75
+ x_max = 8.5
76
+
77
+ ##### end of input for sel = 1 #########################################
78
+
79
+ R.prec = prec1
80
+
81
+ root = TkRoot.new{
82
+ t2 = prec2 == 2 ? " (slide-rule precision) " : ""
83
+ title "Functions evaluated at precision #{prec2}" + t2
84
+ }
85
+
86
+ px = 3 # number of sub-windows is 6 = 3 * 2
87
+ py = 2
88
+
89
+ grs = Graphs.new(root,px,py,nx,ny)
90
+
91
+ gr1 = grs.at(0,0)
92
+ gr2 = grs.at(0,1)
93
+ gr3 = grs.at(0,2)
94
+ gr4 = grs.at(1,0)
95
+ gr5 = grs.at(1,1)
96
+ gr6 = grs.at(1,2)
97
+
98
+ auto = true
99
+ color1 = 'white'
100
+
101
+ xl=R.c(x_max)
102
+ ivx=Iv.new(-xl,xl)
103
+ gr1.set_size_x!(ivx)
104
+ gr2.set_size_x!(ivx)
105
+ gr3.set_size_x!(ivx)
106
+ gr4.set_size_x!(ivx*2)
107
+ gr5.set_size_x!(ivx*2)
108
+ gr6.set_size_x!(ivx)
109
+ gr1.draw_func(Exp.new,n,color1,auto)
110
+ gr2.draw_func(Sin.new,n,color1,auto)
111
+ gr3.draw_func(Cos.new,n,color1,auto)
112
+ gr4.draw_func(Log.new,n,color1,auto)
113
+ gr5.draw_func(Erfc.new,n,color1,auto)
114
+ gr6.draw_func(ArcTan.new,n,color1,auto)
115
+
116
+
117
+ # The algorithms for the functions work even for
118
+ # prec2 = 1. Of course, the x-values have to be taken from
119
+ # a more accurate computation (with precision prec1).
120
+ auto = false
121
+ color2 = 'red'
122
+
123
+ gr1.text_color = gr1.bgr_color
124
+ gr2.text_color = gr2.bgr_color
125
+ gr3.text_color = gr3.bgr_color
126
+ gr4.text_color = gr4.bgr_color
127
+ gr5.text_color = gr5.bgr_color
128
+ gr6.text_color = gr6.bgr_color
129
+ gr1.draw_func(Exp.new(prec2),n,color2,auto)
130
+ gr2.draw_func(Sin.new(prec2),n,color2,auto)
131
+ gr3.draw_func(Cos.new(prec2),n,color2,auto)
132
+ gr4.draw_func(Log.new(prec2),n,color2,auto)
133
+ gr5.draw_func(Erfc.new(prec2),n,color2,auto)
134
+ gr6.draw_func(ArcTan.new(prec2),n,color2,auto)
135
+
136
+ Tk.mainloop
137
+
138
+ when 2 # test of R.test, changing the accuracy from call to call
139
+ count = R.c0
140
+ s = R.c0
141
+ verbose = false
142
+ # cases = [-1]
143
+ # param = [137]
144
+ cases = [0,20,40]
145
+ param = [0, 137]
146
+ cases.each{ |x|
147
+ param.each{ |y|
148
+ R.prec = x
149
+ d = R.test(y,verbose)
150
+ s += d
151
+ count += 1
152
+ }
153
+ }
154
+ res = s/count
155
+ puts "res = " + res.to_s
156
+
157
+ when 3 # simple computations with default accuracy
158
+ x = R.new(-100)
159
+ y = x.exp
160
+ puts "y="+y.to_s
161
+
162
+ a = R.new(250)
163
+ d = a.sin**2 + a.cos**2 - 1
164
+ puts d.to_s
165
+
166
+ when 4 # testing complex arithmetics and functions
167
+ dig = 100
168
+ R.prec = dig
169
+ C.test(137,false)
170
+
171
+ when 5 # testing input formats
172
+ R.prec = "F"
173
+ x = [R.c("3.45E-45"), R.c(2), R.c(1.25e13), R.c("4000.1e-1")]
174
+ x.each{ |x|
175
+ puts " x = #{x}"
176
+ }
177
+
178
+ when 6 # testing R.new
179
+ R.prec = 6
180
+ x = R.new("-6.6666666666666666")
181
+ puts "x=" + x.to_s
182
+ R.prec = 40
183
+ i = 10000000
184
+ j = 123456789
185
+ a = R.new(i * j)
186
+ b = R.new(1e-23)
187
+ c = R.new("-117.07e99")
188
+ R.prec = 100 # same as R.prec = 1e2
189
+ d = R.new(1e66)
190
+ e = R.new("1E666")
191
+ arr = [a,b,c,d,e]
192
+ arr.each{ |x|
193
+ x.prn("#{x}")
194
+ }
195
+
196
+ else
197
+ puts "unvalid selection, nothing to do"
198
+
199
+ end