energy_market 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,597 @@
1
+ require 'test/unit'
2
+ require 'energy_market'
3
+ require 'time'
4
+
5
+ class TestVector < Test::Unit::TestCase
6
+
7
+ def setup
8
+ Time.zone = "Rome"
9
+ @array = [3, 6, 7, -34, 5.5, 0, 8]
10
+ end
11
+
12
+ def test_zero_vector
13
+ a = [2, 3, 5, 7, 11]
14
+
15
+ v1 = EnergyMarket::Vector.new
16
+ v2 = EnergyMarket::Vector.new("2013-01-01").data(a)
17
+ v12 = v1+v2
18
+ assert_equal(v2.start_time, v12.start_time)
19
+ assert_equal(a, v12.v)
20
+
21
+ v1 = EnergyMarket::Vector.new
22
+ v2 = EnergyMarket::Vector.new("2013-01-01").data(a)
23
+ v12 = v1*v2
24
+ assert_equal(v2.start_time, v12.start_time)
25
+ assert_equal(a, v12.v)
26
+
27
+ end
28
+
29
+ def test_initialize_start_time
30
+ v1 = EnergyMarket::Vector.new("2013-01-01 00:00:00")
31
+ v2 = EnergyMarket::Vector.new("2013-01-01 00:00")
32
+ assert_equal(v1.start_time, v2.start_time)
33
+
34
+ v2 = EnergyMarket::Vector.new("2013-01-01 00")
35
+ assert_equal(v1.start_time, v2.start_time)
36
+
37
+ v2 = EnergyMarket::Vector.new("2013-01-01")
38
+ assert_equal(v1.start_time, v2.start_time)
39
+
40
+ v2 = EnergyMarket::Vector.new("2013-01")
41
+ assert_equal(v1.start_time, v2.start_time)
42
+
43
+ v2 = EnergyMarket::Vector.new("2013")
44
+ assert_equal(v1.start_time, v2.start_time)
45
+
46
+ Time.zone = "London"
47
+ v2 = EnergyMarket::Vector.new("2013")
48
+ assert_not_equal(v2.start_time, v1.start_time)
49
+
50
+ Time.zone = "Rome"
51
+ v1 = EnergyMarket::Vector.new
52
+ # v2 = EnergyMarket::Vector.new(Time.zone.now.strftime("%Y-%m-%d %H"))
53
+ assert_equal(nil, v1.start_time)
54
+
55
+ # v1 = EnergyMarket::Vector.new(Time.now)
56
+ # v2 = EnergyMarket::Vector.new(Time.zone.now)
57
+ # assert_equal(v1.start_time, v2.start_time)
58
+ end
59
+
60
+
61
+
62
+ def test_initialize_start_time_flooring
63
+ v1 = EnergyMarket::Vector.new("2013-05-03 02")
64
+ v2 = EnergyMarket::Vector.new("2013-05-03 02:53:18")
65
+ assert_equal(v1.start_time, v2.start_time)
66
+
67
+ v2 = EnergyMarket::Vector.new("2013-05-03 02:53")
68
+ assert_equal(v1.start_time, v2.start_time)
69
+
70
+ v1 = EnergyMarket::Vector.new("2013-05-03")
71
+ v2 = EnergyMarket::Vector.new("2013-05-03 02:53:18", {unit: :day})
72
+ assert_equal(v1.start_time, v2.start_time)
73
+
74
+ v1 = EnergyMarket::Vector.new("2013-05-01")
75
+ v2 = EnergyMarket::Vector.new("2013-05-03 02:53:18", {unit: :month})
76
+ assert_equal(v1.start_time, v2.start_time)
77
+
78
+ v1 = EnergyMarket::Vector.new("2013-01-01")
79
+ v2 = EnergyMarket::Vector.new("2013-05-03 02:53:18", {unit: :year})
80
+ assert_equal(v1.start_time, v2.start_time)
81
+ end
82
+
83
+
84
+
85
+ def test_initialize_start_time_zones
86
+ v1 = EnergyMarket::Vector.new("2013-05-03 02:53:18", :zone => "Rome")
87
+ v2 = EnergyMarket::Vector.new("2013-05-03 01:53:18", :zone => "London")
88
+ assert_equal(v1.start_time, v2.start_time)
89
+ v2 = EnergyMarket::Vector.new("2013-05-03 04:53:18", :zone => "Moscow")
90
+ assert_equal(v1.start_time, v2.start_time)
91
+ puts Time.zone
92
+ end
93
+
94
+
95
+ def test_cloning
96
+ v1 = EnergyMarket::Vector.new("2013-05-03 02:53:18", :zone => "Rome")
97
+ v1.data(@array)
98
+ assert_not_same(v1.v, v1.clone.v)
99
+
100
+ v2 = EnergyMarket::Vector.new("2013-05-03 02:53:18", :zone => "Rome")
101
+ v2.data(@array)
102
+ assert(v1.v.equal? v2.v) # !!! is the same obj
103
+ @array << 6.6
104
+ assert(@array.equal? v1.v) # !!! is the same obj
105
+ assert(@array.equal? v2.v) # !!! is the same obj
106
+ end
107
+
108
+ def test_dataize
109
+ arr = [3, 5, 7]
110
+ v1 = EnergyMarket::Vector.new("2013-05-01")
111
+ v1.data(arr, :day)
112
+ i = 0
113
+ arr.each do |v|
114
+ 24.times do
115
+ assert_equal(v, v1.v[i])
116
+ i += 1
117
+ end
118
+ end
119
+
120
+ arr = [3, 5, 7]
121
+ v1 = EnergyMarket::Vector.new("2013-04-01")
122
+ v1.data(arr, :month)
123
+ i = 0
124
+ month_days = [30, 31, 30]
125
+ arr.each_with_index do |v, j|
126
+ (month_days[j]*24).times do
127
+ assert_equal(v, v1.v[i])
128
+ i += 1
129
+ end
130
+ end
131
+
132
+ v = 3.5
133
+ v1 = EnergyMarket::Vector.new("2013-01-01")
134
+ v1.data(v, :year)
135
+ assert_equal(8760, v1.v.size)
136
+ v1.v.each do |e|
137
+ assert_equal(v, e)
138
+ end
139
+
140
+ v = 4.5
141
+ v1 = EnergyMarket::Vector.new("2013-05-01")
142
+ v1.data(v, :year) # set values to v to the end of year
143
+ n = (Time.zone.parse("2014-01-01") - Time.zone.parse("2013-05-01"))/3600
144
+ assert_equal(n.to_i, v1.v.size)
145
+ v1.v.each do |e|
146
+ assert_equal(v, e)
147
+ end
148
+
149
+ end
150
+
151
+ def test_set_all_to
152
+ v = 13.2
153
+ arr = [3, 5, 7]
154
+ v1 = EnergyMarket::Vector.new("2013-05-01")
155
+ v1.data(arr, :day)
156
+ v1.set_all_to(v)
157
+ assert_equal(24*arr.size, v1.size)
158
+ v1.v.each do |e|
159
+ assert_equal(v, e)
160
+ end
161
+ end
162
+
163
+
164
+ def test_sum
165
+ a = [0, 1.2, 3.7, -3.9, 9.735, -0.432]
166
+ v = EnergyMarket::Vector.new("2013").data(a)
167
+ assert_equal(1.2+3.7-3.9+9.735-0.432, v.sum)
168
+ assert_equal(1.2+3.7+9.735, v.sum(:values => :positive))
169
+ assert_equal(1.2+3.7+9.735, v.sum(:values => :not_negative))
170
+ assert_equal(-3.9-0.432, v.sum(:values => :negative))
171
+ assert_equal(-3.9-0.432, v.sum(:values => :not_positive))
172
+ assert_equal(1.2+3.7-3.9+9.735-0.432, v.sum(:values => :all))
173
+ assert_equal(1.2+3.7-3.9+9.735-0.432, v.sum(:values => :not_zero))
174
+ assert_raise ArgumentError do
175
+ v.sum(:values => :nope)
176
+ end
177
+
178
+ v2 = EnergyMarket::Vector.new("2013")
179
+ assert_equal(0.0, v2.sum)
180
+ end
181
+
182
+ def test_count
183
+ a = [0, 1.2, 3.7, -3.9, 9.735, -0.432]
184
+ v = EnergyMarket::Vector.new("2013").data(a)
185
+ assert_equal(a.size, v.count)
186
+ assert_equal(3, v.count(:values => :positive))
187
+ assert_equal(4, v.count(:values => :not_negative))
188
+ assert_equal(2, v.count(:values => :negative))
189
+ assert_equal(3, v.count(:values => :not_positive))
190
+ assert_equal(6, v.count(:values => :all))
191
+ assert_equal(5, v.count(:values => :not_zero))
192
+ assert_raise ArgumentError do
193
+ v.count(:values => :nope)
194
+ end
195
+
196
+ v2 = EnergyMarket::Vector.new("2013")
197
+ assert_equal(0.0, v2.count)
198
+ end
199
+
200
+ def assert_close_to(f1, f2, delta=0.0001)
201
+ diff = (f1-f2).abs
202
+ diff<delta
203
+ end
204
+
205
+ def test_mean
206
+ a = [0, 10.2, 3.4, -4.3, 8.9, -0.2]
207
+ v = EnergyMarket::Vector.new("2013").data(a)
208
+ assert_close_to(3.0, v.mean)
209
+ assert_close_to((10.2+3.4+8.9)/3.0, v.mean(:values => :positive))
210
+ assert_close_to((0+10.2+3.4-4.3+8.9-0.2)/4.0, v.mean(:values => :not_negative))
211
+ assert_close_to((-4.3-0.2)/2.0, v.mean(:values => :negative))
212
+ assert_close_to((0-4.3-0.2)/3.0, v.mean(:values => :not_positive))
213
+ assert_close_to(3.0, v.mean(:values => :all))
214
+ assert_close_to((10.2+3.4-4.3+8.9-0.2)/5.0, v.mean(:values => :not_zero))
215
+ assert_raise ArgumentError do
216
+ v.mean(:values => :nope)
217
+ end
218
+
219
+ v2 = EnergyMarket::Vector.new("2013")
220
+ assert_equal(nil, v2.mean)
221
+ end
222
+
223
+ def test_minimum_value
224
+ a = [0, 10.2, 3.4, -4.3, 8.9, -0.2, nil]
225
+ v = EnergyMarket::Vector.new("2013").data(a)
226
+ assert_equal(-4.3, v.minimum_value)
227
+
228
+ v2 = EnergyMarket::Vector.new("2013")
229
+ assert_equal(nil, v2.minimum_value)
230
+ end
231
+
232
+ def test_maximum_value
233
+ a = [0, 10.2, 3.4, -4.3, 8.9, -0.2, nil]
234
+ v = EnergyMarket::Vector.new("2013").data(a)
235
+ assert_equal(10.2, v.maximum_value)
236
+
237
+ v2 = EnergyMarket::Vector.new("2013")
238
+ assert_equal(nil, v2.maximum_value)
239
+ end
240
+
241
+ def test_round
242
+ a = [0, 10.244321, 3.000001, -4.500001, -1.75326, nil]
243
+ v = EnergyMarket::Vector.new("2013").data(a)
244
+ v.round!(2)
245
+ assert_equal([0, 10.24, 3.0, -4.50, -1.75, nil], v.v)
246
+
247
+ v2 = EnergyMarket::Vector.new("2013")
248
+ v2.round!(3)
249
+ assert_equal([], v2.v)
250
+ end
251
+
252
+ # TODO
253
+ def test_xy_array
254
+ a = [0, 10.244321, 3.000001, -4.500001, -1.75326, nil]
255
+ v = EnergyMarket::Vector.new("2013").data(a)
256
+
257
+ end
258
+
259
+ def test_end_time
260
+ a = [1,2,3,4,5,6]
261
+ v = EnergyMarket::Vector.new("2013").data(a)
262
+ t = Time.zone.parse("2013-01-01 00:00:00") + (a.size-1).hours
263
+ assert_equal(t, v.end_time)
264
+
265
+ # empty array
266
+ v = EnergyMarket::Vector.new("2013")
267
+ assert_nil(v.end_time)
268
+
269
+ # one element array
270
+ v = EnergyMarket::Vector.new("2013").data([4])
271
+ assert_equal(v.start_time, v.end_time)
272
+ end
273
+
274
+ def test_aligned_with
275
+ # same timezone
276
+ a1 = [0, 10.244321, 3.000001, -4.500001, -1.75326, nil]
277
+ v1 = EnergyMarket::Vector.new("2013").data(a1)
278
+ a2 = [0, 0, 0, 0, 1, nil]
279
+ v2 = EnergyMarket::Vector.new("2013").data(a2)
280
+ assert(v1.aligned_with? v2)
281
+
282
+ # different timezone
283
+ a1 = [0, 10.244321, 3.000001, -4.500001, -1.75326, nil]
284
+ v1 = EnergyMarket::Vector.new("2013", :zone => "Rome").data(a1)
285
+ a2 = [0, 0, 0, 0, 1, nil]
286
+ v2 = EnergyMarket::Vector.new("2013", :zone => "London").data(a2)
287
+ assert(!v1.aligned_with?(v2))
288
+ end
289
+
290
+
291
+ def test_align_with
292
+ # [1,2,3,4,5,6]
293
+ # [1,2,3,4,5]
294
+ # -------------
295
+ # [1,2,3,4,5]
296
+ v1 = EnergyMarket::Vector.new("2013").data([1,2,3,4,5,6])
297
+ v2 = EnergyMarket::Vector.new("2013").data([1,2,3,4,5])
298
+ v1.align_with(v2)
299
+ assert_equal(5, v1.size)
300
+ assert_equal(v1.start_time, v2.start_time)
301
+ assert_equal(v1.end_time, v2.end_time)
302
+
303
+ # [1,2,3,4,5]
304
+ # [1,2,3,4,5,6]
305
+ # -------------
306
+ # [1,2,3,4,5]
307
+ v1 = EnergyMarket::Vector.new("2013").data([1,2,3,4,5])
308
+ v2 = EnergyMarket::Vector.new("2013").data([1,2,3,4,5,6])
309
+ v1.align_with(v2)
310
+ assert_equal(5, v1.size)
311
+ assert_equal(v1.start_time, v2.start_time)
312
+ assert_not_equal(v1.end_time, v2.end_time)
313
+
314
+ # [1,2,3,4,5]
315
+ # [1,2,3,4,5]
316
+ # -------------
317
+ # [1,2,3,4,5]
318
+ v1 = EnergyMarket::Vector.new("2013").data([1,2,3,4,5])
319
+ v2 = EnergyMarket::Vector.new("2013").data([1,2,3,4,5])
320
+ v1.align_with(v2)
321
+ assert_equal(5, v1.size)
322
+ assert_equal(v1.start_time, v2.start_time)
323
+ assert_equal(v1.end_time, v2.end_time)
324
+
325
+
326
+ # [1, 1, 1, 1, 1]
327
+ # [2, 2, 2, 2, 2, 2]
328
+ # [0, 0, 0]
329
+ v1 = EnergyMarket::Vector.new("2013").data([1,2,3,4,5,6])
330
+ v2 = EnergyMarket::Vector.new("2013").data([1,2,3,4,5])
331
+
332
+
333
+
334
+ # same start_time, different array size
335
+ v1 = EnergyMarket::Vector.new("2013").data([1,2,3,4,5,6])
336
+ v2 = EnergyMarket::Vector.new("2013").data([1,2,3,4,5])
337
+ assert_not_equal(v1.end_time, v2.end_time)
338
+ v1.align_with(v2)
339
+ assert_equal(5, v1.size)
340
+ assert_equal(v1.end_time, v2.end_time)
341
+
342
+ v1 = EnergyMarket::Vector.new("2013").data([1,2,3,4,5,6])
343
+ v1.align_with(EnergyMarket::Vector.new("2013").data([0]))
344
+ assert_equal(1, v1.size)
345
+
346
+ v1 = EnergyMarket::Vector.new("2013").data([1,2,3,4,5,6])
347
+ v1.align_with(EnergyMarket::Vector.new("2013"))
348
+ assert_equal(0, v1.size)
349
+
350
+
351
+ # different start_time (1)
352
+ a1 = [0, 10.244321, 3.000001, -4.500001, -1.75326, nil]
353
+ v1 = EnergyMarket::Vector.new("2013-01-01 00:00").data(a1)
354
+ a2 = [0, 0, nil]
355
+ v2 = EnergyMarket::Vector.new("2013-01-01 02:00").data(a2)
356
+
357
+ assert_not_equal(v1.start_time, v2.start_time)
358
+ assert_not_equal(v1.end_time, v2.end_time)
359
+ assert_not_equal(v1.size, v2.size)
360
+
361
+ v1.align_with(v2)
362
+ assert_equal(v1.start_time, v2.start_time)
363
+ assert_equal(v1.end_time, v2.end_time)
364
+ assert_equal(v1.size, v2.size)
365
+
366
+ # different start_time (2)
367
+ # [1, 1, 1]
368
+ # [2, 2, 2, 2, 2, 2]
369
+ # [0]
370
+ a1 = [1, 2, nil]
371
+ v1 = EnergyMarket::Vector.new("2013-01-01 00:00").data(a1)
372
+ a2 = [1,2,3,4,5,6]
373
+ v2 = EnergyMarket::Vector.new("2013-01-01 02:00").data(a2)
374
+
375
+ assert_not_equal(v1.start_time, v2.start_time)
376
+ assert_not_equal(v1.end_time, v2.end_time)
377
+ assert_not_equal(v1.size, v2.size)
378
+
379
+ v1.align_with(v2)
380
+ assert_equal(v1.start_time, v2.start_time)
381
+ assert_not_equal(v1.end_time, v2.end_time)
382
+ assert_equal(1, v1.size)
383
+
384
+ # different start_time (3)
385
+ # [1, 1, 1]
386
+ # [2, 2, 2, 2, 2, 2]
387
+ # [0, 0, 0]
388
+ a1 = [0, 0, nil]
389
+ v1 = EnergyMarket::Vector.new("2013-01-01 02:00").data(a1)
390
+ a2 = [0, 10.244321, 3.000001, -4.500001, -1.75326, nil]
391
+ v2 = EnergyMarket::Vector.new("2013-01-01 00:00").data(a2)
392
+
393
+ assert_not_equal(v1.start_time, v2.start_time)
394
+ assert_not_equal(v1.end_time, v2.end_time)
395
+ assert_not_equal(v1.size, v2.size)
396
+
397
+ v1.align_with(v2)
398
+ assert_not_equal(v1.start_time, v2.start_time)
399
+ assert_not_equal(v1.end_time, v2.end_time)
400
+ assert_equal(3, v1.size)
401
+
402
+
403
+ v1 = EnergyMarket::Vector.new("2013-01-01").data([2, 3, 5, 7, 11])
404
+ v2 = EnergyMarket::Vector.new("2013-01-02").data([2, 3, 5, 7, 11])
405
+ v1.align_with(v2)
406
+
407
+
408
+ v1 = EnergyMarket::Vector.new("2013-01-01 02:00")
409
+ a = [0, 10.244321, 3.000001, -4.500001, -1.75326, nil]
410
+ v2 = EnergyMarket::Vector.new("2013-01-01 00:00").data(a)
411
+ v1.align_with(v2)
412
+ assert_equal(0, v1.size)
413
+ assert_equal(2, v1.start_time.hour)
414
+
415
+ v1 = EnergyMarket::Vector.new("2013").data([1,2,3])
416
+ v2 = EnergyMarket::Vector.new("2013")
417
+ v1.align_with(v2)
418
+ end
419
+
420
+ def test_size
421
+ v1 = EnergyMarket::Vector.new("2013-01-01 02:00")
422
+ assert_equal(0, v1.size)
423
+
424
+ a2 = [0, 10.244321, 3.000001, -4.500001, -1.75326, nil]
425
+ v2 = EnergyMarket::Vector.new("2013-01-01 00:00").data(a2)
426
+ assert_equal(a2.size, v2.size)
427
+ end
428
+
429
+ def test_until_the_end_of_the_year
430
+ a1 = [2, 3, 5, 7, 11]
431
+ v1 = EnergyMarket::Vector.new("2013-12-01").data(a1)
432
+ v1.until_the_end_of_the_year
433
+ v = v1.v
434
+ assert_equal(24*31, v.size)
435
+ assert_equal(a1+[0.0, 0.0], v[0...7])
436
+
437
+ v1 = EnergyMarket::Vector.new("2013")
438
+ v1.until_the_end_of_the_year(1.0)
439
+ assert_equal(8760, v1.size)
440
+ assert_equal(8760, v1.sum)
441
+ end
442
+
443
+
444
+ def operation_w_scalar(operation, k)
445
+ a = [2, 3, 5, 7, 11]
446
+ v = EnergyMarket::Vector.new("2013-01-01").data(a)
447
+ r = v.send(operation, k)
448
+ assert_equal(a.collect{|e| e.to_f.send(operation, k)}, r.v)
449
+ end
450
+
451
+
452
+ def test_plus
453
+ a1 = [2, 3, 5, 7, 11]
454
+ v1 = EnergyMarket::Vector.new("2013-01-01").data(a1)
455
+ a2 = [2, 3, 5, 7, 11]
456
+ v2 = EnergyMarket::Vector.new("2013-01-01 02:00").data(a2)
457
+
458
+ r12 = v1+v2
459
+ assert_equal([5+2, 7+3, 11+5], r12.v)
460
+
461
+ operation_w_scalar(:+, 5)
462
+ operation_w_scalar(:+, 5.8)
463
+
464
+ v3 = EnergyMarket::Vector.new("2013-01-01")
465
+ r13 = v1+v3
466
+ assert_equal([], r13.v)
467
+
468
+ r31 = v3+v1
469
+ assert_equal([], r31.v)
470
+
471
+
472
+ v1 = EnergyMarket::Vector.new("2013-01-01").data([2, 3, 5, 7, 11])
473
+ v2 = EnergyMarket::Vector.new("2013-01-02").data([2, 3, 5, 7, 11])
474
+ r12 = v1+v2
475
+ assert_equal([], r12.v)
476
+ end
477
+
478
+
479
+ def test_minus
480
+ a1 = [2, 3, 5, 7, 11]
481
+ v1 = EnergyMarket::Vector.new("2013-01-01").data(a1)
482
+ a2 = [2, 3, 5, 7, 11]
483
+ v2 = EnergyMarket::Vector.new("2013-01-01 02:00").data(a2)
484
+
485
+ r12 = v1-v2
486
+ assert_equal([5-2, 7-3, 11-5], r12.v)
487
+
488
+ operation_w_scalar(:-, 5)
489
+ operation_w_scalar(:-, 5.8)
490
+
491
+
492
+ v3 = EnergyMarket::Vector.new("2013-01-01")
493
+ r13 = v1-v3
494
+ assert_equal([], r13.v)
495
+
496
+ r31 = v3-v1
497
+ assert_equal([], r31.v)
498
+
499
+ v1 = EnergyMarket::Vector.new("2013-01-01").data([2, 3, 5, 7, 11])
500
+ v2 = EnergyMarket::Vector.new("2013-01-02").data([2, 3, 5, 7, 11])
501
+ r12 = v1+v2
502
+ assert_equal([], r12.v)
503
+ end
504
+
505
+
506
+ def test_multiply
507
+ a1 = [2, 3, 5, 7, 11]
508
+ v1 = EnergyMarket::Vector.new("2013-01-01").data(a1)
509
+ a2 = [2, 3, 5, 7, 11]
510
+ v2 = EnergyMarket::Vector.new("2013-01-01 02:00").data(a2)
511
+
512
+ r12 = v1*v2
513
+ assert_equal([5*2, 7*3, 11*5], r12.v)
514
+
515
+ operation_w_scalar(:*, 5)
516
+ operation_w_scalar(:*, 5.8)
517
+
518
+ v3 = EnergyMarket::Vector.new("2013-01-01")
519
+ r13 = v1*v3
520
+ assert_equal([], r13.v)
521
+
522
+ r31 = v3*v1
523
+ assert_equal([], r31.v)
524
+
525
+ v1 = EnergyMarket::Vector.new("2013-01-01").data([2, 3, 5, 7, 11])
526
+ v2 = EnergyMarket::Vector.new("2013-01-02").data([2, 3, 5, 7, 11])
527
+ r12 = v1*v2
528
+ assert_equal([], r12.v)
529
+ end
530
+
531
+
532
+ def test_divided_by
533
+ a1 = [2, 3, 5, 7, 11]
534
+ v1 = EnergyMarket::Vector.new("2013-01-01").data(a1)
535
+ a2 = [2, 3, 5, 7, 11]
536
+ v2 = EnergyMarket::Vector.new("2013-01-01 02:00").data(a2)
537
+
538
+ r12 = v1/v2
539
+ assert_equal([5.0/2, 7.0/3, 11.0/5], r12.v)
540
+
541
+ operation_w_scalar(:/, 5)
542
+ operation_w_scalar(:/, 5.8)
543
+
544
+ v3 = EnergyMarket::Vector.new("2013-01-01")
545
+ r13 = v1/v3
546
+ assert_equal([], r13.v)
547
+
548
+ r31 = v3/v1
549
+ assert_equal([], r31.v)
550
+
551
+ v1 = EnergyMarket::Vector.new("2013-01-01").data([2, 3, 5, 7, 11])
552
+ v2 = EnergyMarket::Vector.new("2013-01-02").data([2, 3, 5, 7, 11])
553
+ r12 = v1/v2
554
+ assert_equal([], r12.v)
555
+ end
556
+
557
+ def test_value
558
+ a1 = [2, 3, 5, 7, 11]
559
+ v1 = EnergyMarket::Vector.new("2013-01-01").data(a1)
560
+ a1.each_with_index do |e, i|
561
+ assert_equal(e, v1.value(i))
562
+ end
563
+
564
+ assert_nil(EnergyMarket::Vector.new("2013-01-01").value(0))
565
+ end
566
+
567
+ def test_first_values
568
+ a1 = [2, 3, 5, 7, 11]
569
+ v1 = EnergyMarket::Vector.new("2013-01-01").data(a1)
570
+ a1.size.times do |i|
571
+ assert_equal(a1[0, i], v1.first_values(i))
572
+ end
573
+
574
+ assert_equal([], EnergyMarket::Vector.new("2013-01-01").first_values(2))
575
+ end
576
+
577
+ def test_set_value
578
+ a1 = [2, 3, 5, 7, 11]
579
+ v1 = EnergyMarket::Vector.new("2013-01-01").data(a1)
580
+ v1.set_value(0, 1000)
581
+ assert_equal(1000, v1.v[0])
582
+
583
+ v2 = EnergyMarket::Vector.new("2013-01-01")
584
+ v2.set_value(2, 1000)
585
+ assert_equal(1000, v2.v[2])
586
+ assert_equal(1000, v2.value(2))
587
+ assert_nil(v2.value(0))
588
+ assert_nil(v2.value(1))
589
+ end
590
+
591
+ def test_min_max
592
+ v1 = EnergyMarket::Vector.new("2013").data([1,2 ,3,4,5,6])
593
+ v2 = EnergyMarket::Vector.new("2013").data([4,0.5,3,9,0,7])
594
+ assert_equal([1,0.5,3,4,0,6], v1.min(v2).v)
595
+ assert_equal([4,2,3,9,5,7], v1.max(v2).v)
596
+ end
597
+ end
data/use/use.rb ADDED
@@ -0,0 +1,79 @@
1
+ require_relative 'electricity_market'
2
+
3
+ start_date = "2013-01-01"
4
+ zone = "Rome"
5
+ array = [3, 6, 7, -34, 5.5, 0, 8]
6
+
7
+ v = EnergyMarket::Vector.new(start_date, zone).data(array)
8
+
9
+
10
+ # puts v
11
+ # puts v.size
12
+
13
+ # puts v.sum
14
+
15
+ # v.until_the_end_of_the_year(1.0)
16
+ # puts v.size
17
+ # puts v
18
+
19
+ # v.set_all_to(0.0)
20
+ # puts v
21
+
22
+ # ---- Initialize --------
23
+
24
+ Time.zone = "Rome"
25
+ # All the following are the same:
26
+ EnergyMarket::Vector.new
27
+ EnergyMarket::Vector.new(Time.zone.now)
28
+
29
+
30
+ # All the following are the same:
31
+ EnergyMarket::Vector.new("13") # no! Wed, 13 Nov 2013 00:00:00 CET +01:00
32
+ EnergyMarket::Vector.new("2013")
33
+ EnergyMarket::Vector.new("2013-01")
34
+ EnergyMarket::Vector.new("2013-01-01")
35
+ EnergyMarket::Vector.new("2013-01-01 00")
36
+ EnergyMarket::Vector.new("2013-01-01 00:00")
37
+ EnergyMarket::Vector.new("2013-01-01 00:00:00")
38
+ EnergyMarket::Vector.new("2013-01-01 00:23:45")
39
+
40
+ # All the following are the same:
41
+ EnergyMarket::Vector.new("2013-01-01")
42
+ EnergyMarket::Vector.new("2013-01-01", "Rome")
43
+
44
+
45
+ # ---- Insert data --------
46
+
47
+ v = EnergyMarket::Vector.new("2013-01-01").data([2.3, 1.1, 4])
48
+ # solo 3 valori/ore
49
+ v.until_the_end_of_the_year(1.0) # di default mette 0.0
50
+ # 8760 valori/ore: [2.3, 1.1, 4, 1.0, 1.0, 1.0, 1.0, ...]
51
+
52
+ v = EnergyMarket::Vector.new("2013-01-01").data([2.3, 1.1, 4])
53
+
54
+ EnergyMarket::Vector.new(start_date, zone).data([1.0, 2.0], :day)
55
+ # tutte le ore del 1 gennaio valorizzate a 1.0, tutte quelle del 2 gennaio a 2.0. Nient'altro
56
+
57
+ EnergyMarket::Vector.new(start_date, zone).data([1.0, 2.0], :month)
58
+ # tutte le ore di gennaio valorizzate a 1.0, tutte quelle di febbraio a 2.0. Nient'altro
59
+
60
+ EnergyMarket::Vector.new(start_date, zone).data([5.3], :year)
61
+ # oppure
62
+ EnergyMarket::Vector.new(start_date, zone).data(5.3, :year)
63
+
64
+
65
+
66
+
67
+ EnergyMarket::Vector.new("2013").until_the_end_of_the_year(default_value = 3.4)
68
+ # will prepare a 8760 elements vector with value=3.4
69
+
70
+
71
+ EnergyMarket::Vector.new("2013").data([1,2,3,4,5]).set_all_to(4.0)
72
+ # [4.0, 4.0, 4.0, 4.0, 4.0]
73
+
74
+ v1 = EnergyMarket::Vector.new("2013").until_the_end_of_the_year(default_value = 3.4)
75
+ v2 = EnergyMarket::Vector.new("2013").until_the_end_of_the_year(default_value = 1.6)
76
+ v3 = (v1+v2)*2
77
+ puts v3.maximum_value
78
+ puts v3.minimum_value
79
+