tioga 1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (116) hide show
  1. data/Tioga_README +372 -0
  2. data/lgpl.txt +504 -0
  3. data/split/Dtable/defs.h +33 -0
  4. data/split/Dtable/dtable.c +1928 -0
  5. data/split/Dtable/dtable_intern.h +144 -0
  6. data/split/Dtable/dvector.h +61 -0
  7. data/split/Dtable/extconf.rb +4 -0
  8. data/split/Dtable/include/dtable.h +35 -0
  9. data/split/Dtable/lib/Dtable_extras.rb +90 -0
  10. data/split/Dtable/namespace.h +47 -0
  11. data/split/Dtable/safe_double.h +104 -0
  12. data/split/Dtable/symbols.c +92 -0
  13. data/split/Dtable/symbols.h +52 -0
  14. data/split/Dvector/defs.h +33 -0
  15. data/split/Dvector/dvector.c +5486 -0
  16. data/split/Dvector/dvector_intern.h +142 -0
  17. data/split/Dvector/extconf.rb +4 -0
  18. data/split/Dvector/include/dvector.h +61 -0
  19. data/split/Dvector/lib/Dvector_extras.rb +328 -0
  20. data/split/Dvector/lib/Numeric_extras.rb +134 -0
  21. data/split/Dvector/namespace.h +47 -0
  22. data/split/Dvector/safe_double.h +104 -0
  23. data/split/Dvector/symbols.c +92 -0
  24. data/split/Dvector/symbols.h +52 -0
  25. data/split/Flate/defs.h +33 -0
  26. data/split/Flate/extconf.rb +19 -0
  27. data/split/Flate/flate.c +156 -0
  28. data/split/Flate/flate_intern.h +97 -0
  29. data/split/Flate/include/flate.h +98 -0
  30. data/split/Flate/namespace.h +47 -0
  31. data/split/Flate/safe_double.h +104 -0
  32. data/split/Flate/symbols.c +92 -0
  33. data/split/Flate/symbols.h +52 -0
  34. data/split/Function/defs.h +33 -0
  35. data/split/Function/dvector.h +61 -0
  36. data/split/Function/extconf.rb +4 -0
  37. data/split/Function/function.c +988 -0
  38. data/split/Function/joint_qsort.c +258 -0
  39. data/split/Function/lib/Function_extras.rb +44 -0
  40. data/split/Function/namespace.h +47 -0
  41. data/split/Function/safe_double.h +104 -0
  42. data/split/Function/symbols.c +92 -0
  43. data/split/Function/symbols.h +52 -0
  44. data/split/Tioga/axes.c +774 -0
  45. data/split/Tioga/defs.h +33 -0
  46. data/split/Tioga/dtable.h +35 -0
  47. data/split/Tioga/dvector.h +61 -0
  48. data/split/Tioga/extconf.rb +4 -0
  49. data/split/Tioga/figures.c +672 -0
  50. data/split/Tioga/figures.h +855 -0
  51. data/split/Tioga/flate.h +98 -0
  52. data/split/Tioga/init.c +524 -0
  53. data/split/Tioga/lib/Arcs_and_Circles.rb +64 -0
  54. data/split/Tioga/lib/ColorConstants.rb +274 -0
  55. data/split/Tioga/lib/Colorbars.rb +10 -0
  56. data/split/Tioga/lib/Colormaps.rb +105 -0
  57. data/split/Tioga/lib/Coordinate_Conversions.rb +194 -0
  58. data/split/Tioga/lib/Creating_Paths.rb +94 -0
  59. data/split/Tioga/lib/Doc.rb +91 -0
  60. data/split/Tioga/lib/Executive.rb +515 -0
  61. data/split/Tioga/lib/FigMkr.rb +2224 -0
  62. data/split/Tioga/lib/FigureConstants.rb +125 -0
  63. data/split/Tioga/lib/Figures_and_Plots.rb +268 -0
  64. data/split/Tioga/lib/Images.rb +278 -0
  65. data/split/Tioga/lib/Legends.rb +190 -0
  66. data/split/Tioga/lib/MarkerConstants.rb +122 -0
  67. data/split/Tioga/lib/Markers.rb +129 -0
  68. data/split/Tioga/lib/Page_Frame_Bounds.rb +567 -0
  69. data/split/Tioga/lib/Rectangles.rb +94 -0
  70. data/split/Tioga/lib/Shading.rb +100 -0
  71. data/split/Tioga/lib/Special_Paths.rb +307 -0
  72. data/split/Tioga/lib/Strokes.rb +129 -0
  73. data/split/Tioga/lib/TeX_Text.rb +454 -0
  74. data/split/Tioga/lib/TexPreamble.rb +358 -0
  75. data/split/Tioga/lib/Titles_and_Labels.rb +306 -0
  76. data/split/Tioga/lib/Transparency.rb +89 -0
  77. data/split/Tioga/lib/Using_Paths.rb +164 -0
  78. data/split/Tioga/lib/Utils.rb +74 -0
  79. data/split/Tioga/lib/X_and_Y_Axes.rb +749 -0
  80. data/split/Tioga/lib/irb_tioga.rb +122 -0
  81. data/split/Tioga/lib/tioga.rb +1 -0
  82. data/split/Tioga/lib/tioga_ui.rb +5 -0
  83. data/split/Tioga/lib/tioga_ui_cmds.rb +793 -0
  84. data/split/Tioga/makers.c +989 -0
  85. data/split/Tioga/mk_tioga_sty.rb +53 -0
  86. data/split/Tioga/namespace.h +47 -0
  87. data/split/Tioga/pdf_font_dicts.c +18253 -0
  88. data/split/Tioga/pdfcolor.c +486 -0
  89. data/split/Tioga/pdfcoords.c +505 -0
  90. data/split/Tioga/pdffile.c +342 -0
  91. data/split/Tioga/pdfimage.c +536 -0
  92. data/split/Tioga/pdfpath.c +914 -0
  93. data/split/Tioga/pdfs.h +229 -0
  94. data/split/Tioga/pdftext.c +443 -0
  95. data/split/Tioga/safe_double.h +104 -0
  96. data/split/Tioga/symbols.c +92 -0
  97. data/split/Tioga/symbols.h +52 -0
  98. data/split/Tioga/texout.c +380 -0
  99. data/split/defs.h +33 -0
  100. data/split/extconf.rb +107 -0
  101. data/split/mkmf2.rb +1612 -0
  102. data/split/namespace.h +47 -0
  103. data/split/safe_double.h +104 -0
  104. data/split/scripts/tioga +4 -0
  105. data/split/symbols.c +92 -0
  106. data/split/symbols.h +52 -0
  107. data/tests/dtable_test.data +6 -0
  108. data/tests/dvector_read_test.data +1 -0
  109. data/tests/dvector_test.data +101 -0
  110. data/tests/tc_Dtable.rb +221 -0
  111. data/tests/tc_Dvector.rb +791 -0
  112. data/tests/tc_FMkr.rb +162 -0
  113. data/tests/tc_Flate.rb +45 -0
  114. data/tests/tc_Function.rb +111 -0
  115. data/tests/ts_Tioga.rb +38 -0
  116. metadata +163 -0
@@ -0,0 +1,791 @@
1
+ # tc_Dvector.rb
2
+
3
+ require 'Dobjects/Dvector'
4
+ require 'stringio'
5
+ require 'test/unit'
6
+
7
+ class TestDvector < Test::Unit::TestCase
8
+ include Dobjects
9
+
10
+ # Returns the real file name of a given file.
11
+ def real_file_name(file_name)
12
+ dir = File.dirname(__FILE__)
13
+ if dir.empty?
14
+ return file_name
15
+ else
16
+ return "#{dir}/#{file_name}"
17
+ end
18
+ end
19
+
20
+ def test_read_nasty_fortran_data
21
+ row = Dvector.read_row(real_file_name("dvector_read_test.data"))
22
+ assert_equal(4, row.size)
23
+ assert_equal(1.0, row[-1])
24
+ end
25
+
26
+ def test_vector_length
27
+ a = Dvector[3.0, 4.0]
28
+ assert_equal(a.vector_length, 5.0)
29
+ end
30
+
31
+ def test_tridag
32
+ a = Dvector[-0.5, -0.5, -0.5, -1.0/3.0, -0.5]
33
+ b = Dvector[1, 2, 3, -2, 1]
34
+ c = Dvector[-0.25, -1.0/3.0, -0.25, -0.25, -0.25]
35
+ r = 4514 * Dvector[1, 2, 3, 4, 5]
36
+ u = Dvector.new
37
+ u.tridag(a,b,c,r)
38
+ diffs = (u - Dvector[6226, 6848, 4665, -11884, 16628])*1e6
39
+ diffs.round!
40
+ assert_equal(Dvector[0,0,0,0,0], diffs)
41
+ end
42
+
43
+ def test_read_row
44
+ row = Dvector.read_row(real_file_name("dvector_test.data"))
45
+ assert_equal(2, row.size)
46
+ assert_equal(Dvector[23, 3], row)
47
+ row = Dvector.read_row(real_file_name("dvector_test.data"), 4)
48
+ assert_equal(4, row.size)
49
+ assert_equal(Dvector[0, -1.1, 2.2, -3.3], row)
50
+ end
51
+
52
+ def test_read_rows
53
+ rows = [Dvector.new, Dvector.new, Dvector.new, Dvector.new]
54
+ Dvector.read_rows(real_file_name("dvector_test.data"), rows)
55
+ assert_equal(2, rows[0].size)
56
+ assert_equal(Dvector[23, 3], rows[0])
57
+ assert_equal(4, rows[3].size)
58
+ assert_equal(Dvector[0, -1.1, 2.2, -3.3], rows[3])
59
+ end
60
+
61
+ def test_read
62
+ cols = Dvector.read(real_file_name("dvector_test.data"),nil,2)
63
+ assert_equal(4, cols.size)
64
+ col0 = cols[0]
65
+ col1 = cols[1]
66
+ col2 = cols[2]
67
+ col3 = cols[3]
68
+ assert_equal(100, col0.size)
69
+ assert_equal(100, col1.size)
70
+ assert_equal(100, col2.size)
71
+ assert_equal(100, col3.size)
72
+ assert_equal(0, col0[2])
73
+ assert_equal(-1.1, col1[2])
74
+ assert_equal(2.2, col2[2])
75
+ assert_equal(-3.3, col3[2])
76
+ cols = Dvector.read(real_file_name("dvector_test.data"),
77
+ [col0,col1,nil,col3],3,3)
78
+ assert_equal(3, col0.size)
79
+ assert_equal(3, col1.size)
80
+ assert_equal(100, col2.size)
81
+ assert_equal(3, col3.size)
82
+ assert_equal(0, col0[1])
83
+ assert_equal(-1.1, col1[1])
84
+ assert_equal(-3.3, col3[1])
85
+ end
86
+
87
+ def test_arith_ops
88
+ a = Dvector[11, 22, 33]
89
+ b = Dvector[6.3, -2.1, 3e-40]
90
+ a2 = a.to_a
91
+ b2 = b.to_a
92
+ assert_equal(a.add(4), a + 4)
93
+ assert_equal(a.sub(4), a - 4)
94
+ assert_equal(a.mul(4), a * 4)
95
+ assert_equal(a.div(4), a / 4)
96
+ assert_equal(a.pow(4), a ** 4)
97
+ assert_equal(a.mul(4), 4 * a)
98
+ assert_equal(a.add(4), 4 + a)
99
+ assert_equal(a.neg.add(4), 4 - a)
100
+ assert_equal(a.inv.mul(4), 4 / a)
101
+ assert_equal(a.as_exponent_of(4), 4 ** a)
102
+ assert_equal(a.add(3.5), a + 3.5)
103
+ assert_equal(a.sub(3.5), a - 3.5)
104
+ assert_equal(a.mul(3.5), a * 3.5)
105
+ assert_equal(a.div(3.5), a / 3.5)
106
+ assert_equal(a.pow(3.5), a ** 3.5)
107
+ assert_equal(a.mul(3.5), 3.5 * a)
108
+ assert_equal(a.add(3.5), 3.5 + a)
109
+ assert_equal(a.neg.add(3.5), 3.5 - a)
110
+ assert_equal(a.inv.mul(3.5), 3.5 / a)
111
+ assert_equal(a.as_exponent_of(3.5), 3.5 ** a)
112
+ a = Dvector[ 1.1, -5.7, 12.7 ]
113
+ assert_equal(Dvector[ 1.1 ** 3, (-5.7) ** 3, 12.7 ** 3 ], a.raised_to(3))
114
+ assert_equal(Dvector[ 1.1 ** 3, (-5.7) ** 3, 12.7 ** 3 ], a ** 3)
115
+ b = Dvector[ 7, 4, -2 ]
116
+ assert_equal(Dvector[ 1.1 ** 7, (-5.7) ** 4, 12.7 ** (-2) ], a.raised_to(b))
117
+ assert_equal(Dvector[ 1.1 ** 7, (-5.7) ** 4, 12.7 ** (-2) ], a ** b)
118
+ a = Dvector[ 2, -5, 12 ]
119
+ assert_equal(Dvector[ 3.8 ** 2, 3.8 ** (-5), 3.8 ** 12 ], a.as_exponent_of(3.8))
120
+ b = Dvector[ 7.1, 4.9, -10 ]
121
+ assert_equal(Dvector[ 7.1 ** 2, 4.9 ** (-5), (-10) ** 12 ], a.as_exponent_of(b))
122
+ end
123
+
124
+ def test_vec_math
125
+ a = Dvector[11, 22, 33]
126
+ b = Dvector[6.3, -2.1, 3e-40]
127
+ a2 = a.to_a
128
+ b2 = b.to_a
129
+ ans = 0
130
+ a.each { |x| ans = ans + x }
131
+ assert_equal(ans, a.sum)
132
+ ans = 0
133
+ a.each2(b) { |x,y| ans = ans + x*y }
134
+ assert_equal(ans, a.dot(b))
135
+ ans = []
136
+ a.each2(b) { |x,y| ans << (x + y) }
137
+ assert_equal(ans, a.add(b))
138
+ ans = []
139
+ a.each2(b) { |x,y| ans << (x - y) }
140
+ assert_equal(ans, a.sub(b))
141
+ ans = []
142
+ a.each2(b) { |x,y| ans << (x * y) }
143
+ assert_equal(ans, a.mul(b))
144
+ ans = []
145
+ a.each2(b) { |x,y| ans << (x / y) }
146
+ assert_equal(ans, a.div(b))
147
+ end
148
+
149
+ def test_math
150
+ a = Dvector[11, 22, 33]
151
+ b = Dvector[6.3, -2.1, 3e-40]
152
+ c = Dvector[0.6, -0.2, 1e-40]
153
+ d = Dvector[-0.5, 0, 0.5]
154
+ e = Dvector[1.25, 1.1, 1.5]
155
+ a2 = a.to_a
156
+ b2 = b.to_a
157
+ c2 = c.to_a
158
+ d2 = d.to_a
159
+ e2 = e.to_a
160
+ cutoff = 1e-10
161
+ y = 2.5
162
+ zeros = Dvector[0, 0, 0]
163
+ assert_equal(zeros, (a2.map{ |x| x.sin }.to_dvector - a.sin).trim)
164
+ assert_equal(zeros, (a2.map{ |x| x.cos }.to_dvector - a.cos).trim)
165
+ assert_equal(zeros, (a2.map{ |x| x.tan }.to_dvector - a.tan).trim)
166
+ assert_equal(zeros, (c2.map{ |x| x.asin }.to_dvector - c.asin).trim)
167
+ assert_equal(zeros, (c2.map{ |x| x.acos }.to_dvector - c.acos).trim)
168
+ assert_equal(zeros, (a2.map{ |x| x.atan }.to_dvector - a.atan).trim)
169
+ assert_equal(zeros, (a2.map{ |x| x.sinh }.to_dvector - a.sinh).trim)
170
+ assert_equal(zeros, (a2.map{ |x| x.cosh }.to_dvector - a.cosh).trim)
171
+ assert_equal(zeros, (a2.map{ |x| x.tanh }.to_dvector - a.tanh).trim)
172
+ assert_equal(zeros, (e2.map{ |x| x.asinh }.to_dvector - e.asinh).trim)
173
+ assert_equal(zeros, (e2.map{ |x| x.acosh }.to_dvector - e.acosh).trim)
174
+ assert_equal(zeros, (d2.map{ |x| x.atanh }.to_dvector - d.atanh).trim)
175
+ assert_equal(zeros, (a2.map{ |x| x.sqrt }.to_dvector - a.sqrt).trim)
176
+ assert_equal(zeros, (a2.map{ |x| x.log }.to_dvector - a.log).trim)
177
+ assert_equal(zeros, (a2.map{ |x| x.log10 }.to_dvector - a.log10).trim)
178
+ assert_equal(zeros, (b2.map{ |x| x.exp }.to_dvector - b.exp).trim)
179
+ assert_equal(zeros, (b2.map{ |x| x.exp10 }.to_dvector - b.exp10).trim)
180
+ assert_equal(zeros, (a2.map{ |x| x.raised_to(y) }.to_dvector - a.raised_to(y)).trim)
181
+ assert_equal(zeros, (a2.map{ |x| x.as_exponent_of(y) }.to_dvector - a.as_exponent_of(y)).trim)
182
+ assert_equal(zeros, (b2.map{ |x| x.trim(cutoff) }.to_dvector - b.trim(cutoff)).trim)
183
+ assert_equal(zeros, (a2.map{ |x| x.safe_log(cutoff) }.to_dvector - a.safe_log(cutoff)).trim)
184
+ assert_equal(zeros, (a2.map{ |x| x.safe_log10(cutoff) }.to_dvector - a.safe_log10(cutoff)).trim)
185
+ assert_equal(zeros, (b2.map{ |x| x.safe_inv(cutoff) }.to_dvector - b.safe_inv(cutoff)).trim)
186
+ assert_equal(zeros, (a2.map{ |x| x.safe_sqrt }.to_dvector - a.safe_sqrt).trim)
187
+ assert_equal(zeros, (a2.map{ |x| x.safe_asin }.to_dvector - a.safe_asin).trim)
188
+ assert_equal(zeros, (a2.map{ |x| x.safe_acos }.to_dvector - a.safe_acos).trim)
189
+ a = Dvector[ 1.1, 5.7, 12.7 ]
190
+ assert_equal(zeros, (Dvector[ (1.1).atan2(3.8), (5.7).atan2(3.8), (12.7).atan2(3.8) ] - a.atan2(3.8)).trim)
191
+ b = Dvector[ 7.1, 4.9, -10.1 ]
192
+ assert_equal(zeros, (Dvector[ (1.1).atan2(7.1), (5.7).atan2(4.9), (12.7).atan2(-10.1) ] - a.atan2(b)).trim)
193
+ end
194
+
195
+ def test_numeric
196
+ a = Dvector[11, 22, 33]
197
+ b = Dvector[6.3, -2.1, 3e-40]
198
+ a2 = a.to_a
199
+ b2 = b.to_a
200
+ assert_equal(a2.map{ |x| x.neg }, a.neg)
201
+ assert_equal(a2.map{ |x| x.inv }, a.inv)
202
+ assert_equal(b2.map{ |x| x.abs }, b.abs)
203
+ assert_equal(b2.map{ |x| x.ceil }, b.ceil)
204
+ assert_equal(b2.map{ |x| x.floor }, b.floor)
205
+ assert_equal(b2.map{ |x| x.round }, b.round)
206
+ assert_equal(b2.map{ |x| x.modulo(3) }, b.modulo(3))
207
+ assert_equal(b2.map{ |x| x.remainder(3) }, b.remainder(3))
208
+ a = Dvector[ 1.1, -5.7, 12.7 ]
209
+ b = Dvector[ 7.1, 4.9, -10.1 ]
210
+ assert_equal(Dvector[1.1.mod(7.1), -5.7.mod(4.9), 12.7.mod(-10.1)], a.mod(b))
211
+ assert_equal(Dvector[1.1.mod(7.1), -5.7.mod(4.9), 12.7.mod(-10.1)], a % b)
212
+ end
213
+
214
+ def test_where
215
+ a = Dvector[33, 11, 22, 22, 44, 17, 11, 33 ]
216
+ assert_equal(4, a.where_max)
217
+ assert_equal(1, a.where_min)
218
+ assert_equal(5, a.where_closest(16))
219
+ assert_equal(1, a.where_first_eq(11))
220
+ assert_equal(1, a.where_first_ne(33))
221
+ assert_equal(4, a.where_first_gt(35))
222
+ assert_equal(4, a.where_first_ge(44))
223
+ assert_equal(1, a.where_first_lt(12))
224
+ assert_equal(1, a.where_first_le(11))
225
+ i = a.where_first_lt(0)
226
+ assert_equal(nil, i)
227
+ assert_equal(6, a.where_last_eq(11))
228
+ assert_equal(6, a.where_last_ne(33))
229
+ assert_equal(4, a.where_last_gt(35))
230
+ assert_equal(4, a.where_last_ge(44))
231
+ assert_equal(6, a.where_last_lt(12))
232
+ assert_equal(6, a.where_last_le(11))
233
+ i = a.where_last_lt(0)
234
+ assert_equal(nil, i)
235
+ assert_equal(44, a.max)
236
+ assert_equal(11, a.min)
237
+ a = Dvector[ 1, 2, 3, 4, 5, 4, 3, 5, 2 ]
238
+ b = Dvector[ 8, 3, 0, 7 ]
239
+ assert_equal(0, a.min(b))
240
+ assert_equal(8, a.max(b))
241
+ assert_equal(0, Dvector.min_of_many([a,b]))
242
+ assert_equal(8, Dvector.max_of_many([a,b]))
243
+ end
244
+
245
+ def test_values_at
246
+ a = Dvector[ 1, 2, 3, 4, 5, 6 ]
247
+ assert_equal(Dvector[ 2, 4, 6 ], a.values_at(1, 3, 5))
248
+ assert_equal(Dvector[ 2, 4, 6 ], a.values_at(1, 3, 5, 7))
249
+ assert_equal(Dvector[ 6, 4, 2 ], a.values_at(-1, -3, -5, -7))
250
+ assert_equal(Dvector[ 2, 3, 4, 3, 4, 5 ], a.values_at(1..3, 2...5))
251
+ end
252
+
253
+ def test_select
254
+ a = Dvector[ 1, 2, 3, 4, 5, 6 ]
255
+ assert_equal(Dvector[2, 4, 6], a.select {|x| x.modulo(2) == 0 })
256
+ end
257
+
258
+ def test_unshift
259
+ a = Dvector[11, 22, 33]
260
+ assert_equal(Dvector[44, 55, 11, 22, 33], a.unshift(44, 55))
261
+ a = [ 2, 3, 4 ]
262
+ assert_equal(Dvector[ 1, 2, 3, 4 ], a.unshift(1))
263
+ assert_equal(Dvector[ -1, 0, 1, 2, 3, 4 ], a.unshift(-1, 0))
264
+ end
265
+
266
+ def test_uniq
267
+ a = Dvector[ 1.1, 3.8, 1.7, 3.8, 5 ]
268
+ assert_equal(Dvector[1.1, 1.7, 3.8, 5], a.uniq)
269
+ b = Dvector[ 1.1, 3.8, 1.7, 5 ]
270
+ assert_equal(b, b.uniq)
271
+ assert_equal(nil, b.uniq!)
272
+ end
273
+
274
+ def test_set
275
+ a = Dvector[33, 11, 22, 44, 17 ]
276
+ b = Dvector[2, 5, 1, -3, 9 ]
277
+ c = Dvector[2, 2, 2, 2, 2 ]
278
+ b.set(a)
279
+ assert_equal(a, b)
280
+ b.set(2)
281
+ assert_equal(c, b)
282
+ end
283
+
284
+ def test_min_gt
285
+ a = Dvector[2, 5, 1, -3, 9 ]
286
+ b = a.min_gt(0)
287
+ assert_equal(1, b)
288
+ b = a.min_gt(10)
289
+ assert_equal(nil, b)
290
+ end
291
+
292
+ def test_max_lt
293
+ a = Dvector[2, 5, 1, -3, 9 ]
294
+ b = a.max_lt(5)
295
+ assert_equal(2, b)
296
+ b = a.max_lt(-5)
297
+ assert_equal(nil, b)
298
+ end
299
+
300
+ def test_sort
301
+ a = Dvector[33, 11, 22, 44, 17 ]
302
+ assert_equal(Dvector[11, 17, 22, 33, 44], a.sort)
303
+ assert_equal(Dvector[44, 33, 22, 17, 11], a.sort {|x,y| y <=> x})
304
+ end
305
+
306
+ def test_shift
307
+ a = Dvector[11, 22, 33, 44 ]
308
+ assert_equal(11, a.shift)
309
+ assert_equal(Dvector[22, 33, 44], a)
310
+ end
311
+
312
+ def test_rindex
313
+ a = Dvector[11, 22, 33, 44, 33, 11 ]
314
+ assert(5,a.rindex(11))
315
+ assert(4,a.rindex(33))
316
+ assert_nil(a.rindex(32))
317
+ end
318
+
319
+ def test_reverse
320
+ a = Dvector[11]
321
+ assert_equal(Dvector[11], a.reverse)
322
+ a = Dvector[11, 22]
323
+ assert_equal(Dvector[22, 11], a.reverse)
324
+ a = Dvector[11, 22, 33]
325
+ assert_equal(Dvector[33, 22, 11], a.reverse)
326
+ end
327
+
328
+ def test_replace
329
+ a = Dvector[11, 22, 33]
330
+ assert_equal(Dvector[44, 55], a.replace(Dvector[44, 55]))
331
+ a = Dvector[11]
332
+ assert_equal(Dvector[44, 55], a.replace([44, 55]))
333
+ end
334
+
335
+ def test_push
336
+ a = Dvector[11, 22, 33]
337
+ assert_equal(Dvector[11, 22, 33, 44, 55], a.push(44, 55))
338
+ end
339
+
340
+ def test_pop
341
+ a = Dvector[11, 22, 33, 44 ]
342
+ assert_equal(44, a.pop)
343
+ assert_equal(Dvector[11, 22, 33], a)
344
+ a = Dvector[]
345
+ assert_equal(nil, a.pop)
346
+ end
347
+
348
+ def test_reject
349
+ a = Dvector[11, 22, 33, 44 ]
350
+ assert_equal(Dvector[11, 33], a.reject {|x| x.modulo(2) == 0})
351
+ end
352
+
353
+ def test_last
354
+ a = Dvector[11, 22, 33, 44 ]
355
+ assert_equal(44, a.last)
356
+ b = a.first(1)
357
+ assert_equal(1, b.length)
358
+ assert_equal(Dvector[44], a.last(1))
359
+ assert_equal(Dvector[22, 33, 44], a.last(3))
360
+ end
361
+
362
+ def test_join
363
+ a = Dvector[11, 22, 33, 44 ]
364
+ assert_equal("11 22 33 44", a.join)
365
+ assert_equal("11-22-33-44", a.join("-"))
366
+ end
367
+
368
+ def test_insert
369
+ a = Dvector[11, 22, 33, 44 ]
370
+ assert_equal(Dvector[11,22,99,33,44], a.insert(2,99))
371
+ assert_equal(Dvector[11,22,99,33,1,2,3,44], a.insert(-2, 1, 2, 3))
372
+ assert_equal(Dvector[11,22,99,33,1,2,3,44,1], a.insert(-1, 1))
373
+ a = Dvector[ 1, 2, 3 ]
374
+ assert_equal(Dvector[ 1, 2, 99, 3 ], a.insert(2, 99))
375
+ assert_equal(Dvector[ 1, 2, 99, 1, 2, 3, 3 ], a.insert(-2, 1, 2, 3))
376
+ assert_equal(Dvector[ 1, 2, 99, 1, 2, 3, 3, 0 ], a.insert(-1, 0))
377
+ end
378
+
379
+ def test_index
380
+ a = Dvector[11, 22, 33, 44 ]
381
+ assert(0,a.index(11))
382
+ assert(2,a.index(33))
383
+ assert_nil(a.index(32))
384
+ end
385
+
386
+ def test_include?
387
+ a = Dvector[11, 22, 33, 44 ]
388
+ assert(a.include?(11))
389
+ assert(!a.include?(12))
390
+
391
+ end
392
+
393
+ def test_fetch
394
+ a = Dvector[11, 22, 33, 44 ]
395
+ assert_equal(22, a.fetch(1))
396
+ assert_equal(44, a.fetch(-1))
397
+ assert_equal(33, a.fetch(-2, -1))
398
+ assert_equal(-1, a.fetch(4, -1))
399
+ assert_equal(-1, a.fetch(-8, -1))
400
+ assert_equal(16, a.fetch(4) {|i| i*i })
401
+ end
402
+
403
+ def test_fill
404
+ a = Dvector[11, 22, 33, 44 ]
405
+ assert_equal(Dvector[1, 1, 1, 1], a.fill(1))
406
+ assert_equal(Dvector[1, 1, -1, -1], a.fill(-1, 2, 2))
407
+ assert_equal(Dvector[9,9,-1,-1], a.fill(9,0..1))
408
+ assert_equal(Dvector[0,1,4,9], a.fill {|i| i*i})
409
+ assert_equal(Dvector[0,101,102,103],a.fill(-3) {|i| i + 100 })
410
+ a = Dvector[ 1, 2, 3, 4, 5 ]
411
+ assert_equal(Dvector[ -1, -1, -1, -1, -1 ], a.fill(-1))
412
+ assert_equal(Dvector[ -1, -1, 7, 7, -1 ], a.fill(7, 2, 2))
413
+ assert_equal(Dvector[ 8, 8, 7, 7, -1 ], a.fill(8, 0..1))
414
+ assert_equal(Dvector[0, 1, 4, 9, 16], a.fill {|i| i*i})
415
+ assert_equal(Dvector[0, 1, 4, 27, 64], a.fill(-2) {|i| i*i*i})
416
+ assert_equal(Dvector[0, 1, 4, 27, 64], a)
417
+ end
418
+
419
+ def test_first
420
+ a = Dvector[11, 22, 33, 44 ]
421
+ GC.start
422
+ assert_equal(11, a.first)
423
+ b = a.first(1)
424
+ assert_equal(0, Dvector[].length)
425
+ assert_equal(1, b.length)
426
+ assert_equal(Dvector[11], a.first(1))
427
+ assert_equal(Dvector[11, 22, 33], a.first(3))
428
+ end
429
+
430
+ def test_new
431
+ @dv = Dvector.new
432
+ assert(@dv.empty?)
433
+ @dv = Dvector.new(10)
434
+ assert_equal(10, @dv.length)
435
+ assert_equal(0, @dv[0])
436
+ @dv = Dvector.new(5,1)
437
+ assert_equal(5, @dv.length)
438
+ assert_equal(1, @dv[0])
439
+ @dv = Dvector.new(3) { |i| i*i }
440
+ assert_equal(3, @dv.length)
441
+ assert_equal(4, @dv[2])
442
+ @dv = Dvector.new(5)
443
+ @dv2 = Dvector.new(@dv)
444
+ assert_equal(@dv, @dv2)
445
+ assert_equal(Dvector[ -1, -1, -1 ], Dvector.new(3, -1))
446
+ assert_equal(Dvector[ 1, 2, 5 ], Dvector.new(3) {|i| i**2 + 1})
447
+ @dv.resize(10)
448
+ assert_equal(10, @dv.size)
449
+ assert_equal(0, @dv[9])
450
+ end
451
+
452
+ def test_append
453
+ @dv = Dvector[0] << 1 << 2 << 3
454
+ assert_equal(4, @dv.length)
455
+ assert_equal(2, @dv[2])
456
+ assert_equal(Dvector[0, 1, 2, 3], @dv)
457
+ end
458
+
459
+ def test_cmp
460
+ @dv = Dvector[0, 1, 2, 3]
461
+ @dv2 = Dvector[0, 1, 2]
462
+ assert_equal(1, @dv <=> @dv2)
463
+ assert_equal(-1, @dv2 <=> @dv)
464
+ @dv2 = Dvector[0, 1, 2, 3]
465
+ assert_equal(0, @dv <=> @dv2)
466
+ @dv2[0] = 4
467
+ assert_equal(-1, @dv <=> @dv2)
468
+ assert_equal(1, @dv2 <=> @dv)
469
+ assert_equal(-1, Dvector[ 1, 1, 2 ] <=> Dvector[ 1, 2, 3 ])
470
+ assert_equal(1, Dvector[ 1, 1, 2 ] <=> Dvector[ 1, 0, 3 ])
471
+ assert_equal(-1, Dvector[ 1, 1, 2 ] <=> Dvector[ 1, 1, 2, 3 ])
472
+ assert_equal(1, Dvector[ 1, 2, 3, 4, 5, 6 ] <=> Dvector[ 1, 2 ])
473
+ end
474
+
475
+ def test_eql
476
+ @dv = Dvector.new
477
+ assert_equal(0, @dv.length)
478
+ assert_not_equal(@dv, nil)
479
+ @dv = Dvector[0, 1, 2, 3]
480
+ assert_equal(4, @dv.length)
481
+ @dv2 = Dvector[0, 1, 2]
482
+ assert_equal(1, @dv[1])
483
+ assert_equal(1, @dv2[1])
484
+ assert_not_equal(@dv, @dv2)
485
+ @dv2 << 3
486
+ GC.start
487
+ assert_equal(4, @dv2.length)
488
+ assert_equal(3, @dv2[3])
489
+ assert_equal(@dv2, @dv)
490
+ assert(Dvector[1,2,3].eql?([1,2,3]))
491
+ end
492
+
493
+ def test_aref
494
+ a = Dvector[ 1, 2, 3, 4, 5 ]
495
+ assert_equal(6, a[2] + a[0] + a[1])
496
+ assert_equal(nil, a[6])
497
+ assert_equal(Dvector[ 2, 3 ], a[1, 2])
498
+ assert_equal(Dvector[ 2, 3, 4 ], a[1..3])
499
+ assert_equal(Dvector[ 5 ], a[4..7])
500
+ assert_equal(nil, a[6..10])
501
+ assert_equal(Dvector[ 3, 4, 5 ], a[-3, 3])
502
+ assert_equal(nil, a[5])
503
+ assert_equal(Dvector[], a[5, 1])
504
+ assert_equal(Dvector[], a[5..10])
505
+ end
506
+
507
+ def test_aset
508
+ @dv = Dvector[]
509
+ @dv[4] = 3.3
510
+ assert_equal(5, @dv.length)
511
+ assert_equal(3.3, @dv[4])
512
+ assert_equal(0, @dv[1])
513
+ @dv[-2] = 2
514
+ assert_equal(5, @dv.length)
515
+ assert_equal(2, @dv[3])
516
+ @dv[1,2] = [5, 4]
517
+ assert_equal(5, @dv.length)
518
+ assert_equal(5, @dv[1])
519
+ assert_equal(4, @dv[2])
520
+ assert_equal(2, @dv[3])
521
+ @dv[2..3] = nil
522
+ assert_equal(3, @dv.length)
523
+ assert_equal(3.3, @dv[-1])
524
+ @dv[1...3] = [6, 7, 8, 9]
525
+ assert_equal(5, @dv.length)
526
+ assert_equal(9, @dv[4])
527
+ a = Dvector.new
528
+ a[4] = 4
529
+ assert_equal(Dvector[ 0, 0, 0, 0, 4 ], a)
530
+ a[0, 3] = [ 1, 2, 3 ]
531
+ assert_equal(Dvector[ 1, 2, 3, 0, 4 ], a)
532
+ a[1..2] = [ 1, 2 ]
533
+ assert_equal(Dvector[ 1, 1, 2, 0, 4 ], a)
534
+ a[0, 2] = -1
535
+ assert_equal(Dvector[ -1, 2, 0, 4 ], a)
536
+ a[0..2] = 1
537
+ assert_equal(Dvector[ 1, 4 ], a)
538
+ a[-1] = 5
539
+ assert_equal(Dvector[ 1, 5 ], a)
540
+ a[1..-1] = nil
541
+ assert_equal(Dvector[ 1 ], a)
542
+ end
543
+
544
+ def test_clear
545
+ @dv = Dvector.new(10) {|i| i*3 }
546
+ assert_equal(3, @dv.at(1))
547
+ @dv.clear
548
+ assert_equal(0, @dv.size)
549
+ end
550
+
551
+ def test_collect!
552
+ @dv = Dvector.new(10) {|i| i*3 }
553
+ assert_equal(3, @dv.at(1))
554
+ @dv.collect! {|x| x-1 }
555
+ assert_equal(2, @dv.at(1))
556
+ end
557
+
558
+ def test_concat
559
+ @dv = Dvector.new
560
+ assert_equal(0, @dv.length)
561
+ @dv.concat(Dvector.new(2))
562
+ assert_equal(2, @dv.length)
563
+ @dv = Dvector.new(3)
564
+ @dv.concat(Dvector.new(2))
565
+ assert_equal(5, @dv.length)
566
+ end
567
+
568
+ def test_delete
569
+ @dv = Dvector.new(10) {|i| i*3 }
570
+ assert_equal(6, @dv[2])
571
+ assert_equal(6, @dv.delete(6))
572
+ assert_equal(9, @dv.length)
573
+ assert_equal(3, @dv[1])
574
+ assert_equal(9, @dv[2])
575
+ assert_equal(nil, @dv.delete(2))
576
+ assert_equal("not found", @dv.delete(2) { "not found" })
577
+ end
578
+
579
+ def test_delete_at
580
+ @dv = Dvector.new(10) {|i| i*3 }
581
+ assert_equal(6, @dv[2])
582
+ x = @dv.delete_at(2)
583
+ GC.start
584
+ assert_equal(6, x)
585
+ assert_equal(9, @dv.length)
586
+ assert_equal(3, @dv[1])
587
+ assert_equal(9, @dv[2])
588
+ assert_equal(nil, @dv.delete_at(20))
589
+ end
590
+
591
+ def test_slice!
592
+ a = Dvector.new(5) {|i| i*3 }
593
+ assert_equal(3, a.slice!(1))
594
+ assert_equal(Dvector[0, 6, 9, 12], a)
595
+ assert_equal(12, a.slice!(-1))
596
+ assert_equal(Dvector[0, 6, 9], a)
597
+ assert_equal(nil, a.slice!(100))
598
+ assert_equal(Dvector[0, 6, 9], a)
599
+ assert_equal(Dvector[6, 9], a.slice!(1..2))
600
+ assert_equal(Dvector[0], a)
601
+ assert_equal(Dvector[], a.slice!(1..2))
602
+ assert_equal(Dvector[0], a)
603
+ assert_equal(Dvector[0], a.slice!(0..2))
604
+ assert_equal(Dvector[], a)
605
+ end
606
+
607
+ def test_delete_if
608
+ @dv = Dvector.new(10) {|i| i*3 }
609
+ @dv.delete_if {|x| x > 2 && x < 8}
610
+ assert_equal(8, @dv.length)
611
+ assert_equal(9, @dv[1])
612
+ end
613
+
614
+ def test_at
615
+ @dv = Dvector.new(10) {|i| i*3 }
616
+ assert_equal(3, @dv.at(1))
617
+ end
618
+
619
+ def test_freeze
620
+ a = Dvector[1]
621
+ assert_equal(false, a.frozen?)
622
+ a.freeze
623
+ assert_equal(true, a.frozen?)
624
+ end
625
+
626
+ def test_each
627
+ t = 0
628
+ n = 0
629
+ @dv = Dvector.new(10) {|i| n = n + i; t = t + 2*i; 2*i }
630
+ s = 0
631
+ @dv.each {|x| s = s + x}
632
+ assert_equal(s, t)
633
+ s = 0
634
+ @dv.each_index { |i| s = s + i }
635
+ assert_equal(s, n)
636
+ @dv.each_with_index {|x,i| assert_equal(x, 2*i)}
637
+ @dv2 = []
638
+ @dv.reverse_each_with_index {|x,i| @dv2[9-i] = x}
639
+ assert_equal(10, @dv2.length)
640
+ assert_equal(@dv, @dv2.reverse)
641
+ @dv2 = []
642
+ @dv.reverse_each_with_index {|x,i| @dv2[i] = x}
643
+ assert_equal(10, @dv2.length)
644
+ @dv2.reverse!
645
+ assert_equal(10, @dv2.length)
646
+ @dv3 = @dv2.reverse
647
+ assert_equal(10, @dv3.length)
648
+ assert_equal(@dv, @dv3)
649
+ @dv.reverse!
650
+ @dv = Dvector[]
651
+ @dv = Dvector.new(10)
652
+ @dv1 = Dvector.new(10) {|i| 2*i }
653
+ @dv3.reverse!
654
+ dv4 = @dv.reverse
655
+ dv5 = dv4.reverse
656
+ dv5.reverse!
657
+ assert_equal(dv4, dv5)
658
+ end
659
+
660
+ def test_linear_interpolate
661
+ len = 11
662
+ xs = Dvector.new(len) {|i| i*1.0/(len-1) }
663
+ ys = Dvector.new(len) {|i| i*2.0/(len-1) }
664
+ ntest = 8
665
+ ntest.times do |i|
666
+ x = i*1.0/(ntest-1)
667
+ diff = 2*x - Dvector.linear_interpolate(x, xs, ys)
668
+ diff = 0 if diff.abs < 1e-10
669
+ assert_equal(0, diff)
670
+ end
671
+ end
672
+
673
+ def test_spline_interpolate
674
+ len = 8
675
+ xs = Dvector.new(len) {|i| i*1.0/(len-1) }
676
+ ys = Dvector.new(len) {|i| x =i*1.0/(len-1); (x-0.1)*(x-0.33)*(x-0.75) }
677
+ interpolant = Dvector.create_spline_interpolant(xs, ys, false, 0, false, 0)
678
+ ntest = 9
679
+ ntest.times do |i|
680
+ x = i*1.0/(ntest-1)
681
+ diff = (x-0.1)*(x-0.33)*(x-0.75) - Dvector.spline_interpolate(x, interpolant)
682
+ diff = 0 if diff.abs < 1e-2
683
+ assert_equal(0, diff)
684
+ #puts "#{i} #{x} #{diff}"
685
+ end
686
+ end
687
+
688
+ def test_bezier_control_points
689
+ dest = Dvector.new
690
+ delta_x = 1; x0 = 10; x1 = x0 + delta_x/3.0
691
+ x2 = x1 + delta_x/3.0; x3 = x0 + delta_x
692
+ y0 = 5; y1 = 8; y2 = 3; y3 = 7
693
+ c = 3.0*(y1-y0); b = 3.0*(y2-y1) - c; a = y3 - y0 - c - b
694
+ dest.make_bezier_control_points_for_cubic_in_x(x0, y0, delta_x, a, b, c)
695
+ assert_equal(6, dest.length)
696
+ assert((dest[0]-x1).abs < 1e-6)
697
+ assert((dest[1]-y1).abs < 1e-6)
698
+ assert((dest[2]-x2).abs < 1e-6)
699
+ assert((dest[3]-y2).abs < 1e-6)
700
+ assert((dest[4]-x3).abs < 1e-6)
701
+ assert((dest[5]-y3).abs < 1e-6)
702
+ end
703
+
704
+ FANCY_READ_TEXT = <<"EOT"
705
+ # some comments
706
+
707
+ # and a blank line above
708
+ 1.2 2.4
709
+ 1.3 2.4\t3.5
710
+ 1.2
711
+ EOT
712
+
713
+ FANCY_READ_TEXT_2 = <<"EOT"
714
+ # some comments
715
+
716
+ # and a blank line above
717
+ -1.2 2.4
718
+ -1.3 2.4\t3.5
719
+ -1.2
720
+ EOT
721
+
722
+ def test_fancy_read
723
+ stream = StringIO.new(FANCY_READ_TEXT)
724
+ cols = Dvector.fancy_read(stream, nil, 'default'=> 0.0)
725
+ cols2 = [Dvector[1.2, 1.3, 1.2], Dvector[2.4, 2.4, 0.0],
726
+ Dvector[0.0, 3.5, 0.0]]
727
+ 3.times do |i|
728
+ assert_equal(cols[i], cols2[i])
729
+ end
730
+
731
+ stream = StringIO.new(FANCY_READ_TEXT)
732
+ cols = Dvector.fancy_read(stream, [1,2], 'default'=> 0.0)
733
+ assert_equal(cols[0], cols2[1])
734
+ assert_equal(cols[1], cols2[2])
735
+
736
+ stream = StringIO.new(FANCY_READ_TEXT_2)
737
+ cols = Dvector.fancy_read(stream, nil, 'default'=> 0.0)
738
+ cols2 = [Dvector[-1.2, -1.3, -1.2], Dvector[2.4, 2.4, 0.0],
739
+ Dvector[0.0, 3.5, 0.0]]
740
+ assert_equal(cols, cols2)
741
+
742
+ end
743
+
744
+ def test_compute_formula
745
+ v = Dvector[1,2,3]
746
+ w = Dvector[3,2,1]
747
+ cal = Dvector.compute_formula("column[0] + column[1]", [v,w])
748
+ assert_equal(cal, Dvector[4,4,4])
749
+ cal = Dvector.compute_formula("sin(column[0]) " +
750
+ "** 2 + cos(column[0]) ** 2", [v,w])
751
+ cal.sub!(1)
752
+ cal.abs!
753
+ assert(cal.max < 1e-10) # We need this trick as computations are
754
+ # not perfectly precise.
755
+ end
756
+
757
+ def test_dirtyness
758
+ v = Dvector.new(10)
759
+ assert(! v.dirty?)
760
+ assert(v.clean?)
761
+ v[1] = 1
762
+ assert(v.dirty?)
763
+ v.dirty = false
764
+ assert(! v.dirty?)
765
+ end
766
+
767
+ def test_marshall
768
+ v = Dvector[1, 2, 3]
769
+ s = Marshal.dump(v)
770
+ v_bis = Marshal.restore(s)
771
+ assert_equal(v, v_bis)
772
+ end
773
+
774
+ def test_bounds
775
+ v = Dvector[0.0/0.0, 0.0/0.0, 1,2,4,5,9,0.0/0.0,0.1]
776
+ assert_equal(v.bounds, [0.1, 9])
777
+ end
778
+
779
+ def test_write_dvectors
780
+ a = Dvector[1,2,3]
781
+ b = Dvector[3,2,1]
782
+ out = StringIO.new("", "w")
783
+ Dvector.write(out, [a,b])
784
+ out.close
785
+ input = StringIO.new(out.string)
786
+ c,d = Dvector.fancy_read(input)
787
+ assert_equal(a,c)
788
+ assert_equal(b,d)
789
+ end
790
+
791
+ end