wector 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3ad1ed315bccca62b4e2e13998f1b6650e57568a
4
- data.tar.gz: 84d317bc05507b498d6600563e3b7b389fb2feac
3
+ metadata.gz: af75df7e08d5f55b00bab2d8d60045b3dba237a6
4
+ data.tar.gz: f91fcb8821ba7bc30342c9c299f7a70f3c32c544
5
5
  SHA512:
6
- metadata.gz: a869f2c0b7a53e01e15649dd5d8c670f55dcf36cbe0bac907ef8e8ba05f73832dfbb95b51bd31d7e0f6b502281f42b9ad9980b3320acebc2f6d323d96602898f
7
- data.tar.gz: 79aa29c909682638bb53c57df5402661c9a006695c23c5e1de0fc246498ae3d1e6b12adeea587194c341dc057f485bbf8133501b561bd9efeb31e4e7c6b5df1f
6
+ metadata.gz: dd1090d7821a26e93789b20d2e9b45559d968caf97bbe2a981d7c5a6d130fa63cf509fcf1301a24754ddca1181ee9022031b596680827bb566ae691c04889fe7
7
+ data.tar.gz: d14805939d4f2d016d3b2ddfb0d6364e6198c5b6789d2cc48c00c35de21b20756fcc3e200dfcb9b4d110a1e3bf69a43afc26ce463a88000687b132e6125a36de
data/Rakefile CHANGED
@@ -1 +1,9 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ end
7
+
8
+ desc "Run tests"
9
+ task :default => :test
data/lib/wector.rb CHANGED
@@ -1,62 +1,678 @@
1
1
  class Wector < Array
2
- def [](*args)
3
- args.each do |arg|
4
- raise "Non-numeric item" unless arg.is_a?(Integer) or arg.is_a?(Float)
5
- unshift arg
6
- end
7
- end
8
- end
9
-
10
- # Comparison operators
11
- [
12
- :*,:+,:/,:-,:**,:%,:^,:==,:===,:<=>,:>,:>=,:<,:<=,:~,:&,:|
13
- ].map { |v| v.to_s }.each do |attr_name|
14
- Wector.class_eval %Q{
15
- def #{attr_name}(other)
16
- if other.is_a? Array
17
- raise "Incorrect Dimensions" unless self.size == other.size
18
- other = other.dup
19
- self.class.new(map { |i| i #{attr_name} other.shift })
20
- elsif other.is_a?(Integer) or other.is_a?(Float)
21
- self.class.new(map { |i| i #{attr_name} other })
22
- else
23
- super
24
- end
25
- end
26
- }
27
- end
28
-
29
- # Comparison methods
30
- [
31
- :-@, :+@, :modulo, :coerce, :div, :divmod, :eql?, :fdiv, :quo, :remainder,
32
- :round
33
- ].map { |v| v.to_s }.each do |attr_name|
34
- Wector.class_eval %Q{
35
- def #{attr_name}(other)
36
- if other.is_a? Array
37
- raise "Incorrect Dimensions" unless self.size == other.size
38
- other = other.dup
39
- self.class.new(map { |i| i.#{attr_name} other.shift })
40
- elsif other.is_a?(Integer) or other.is_a?(Float)
41
- self.class.new(map { |i| i.#{attr_name} other })
42
- else
43
- super
44
- end
45
- end
46
- }
47
- end
2
+ def [](*args)
3
+ args.each do |arg|
4
+ raise "Non-numeric item" unless arg.is_a?(Integer) or arg.is_a?(Float)
5
+ unshift arg
6
+ end
7
+ end
48
8
 
49
- # Transform methods
50
- [
51
- :ceil, :abs, :abs2, :magnitude, :arg, :angle, :phase, :conj, :conjugate,
52
- :denominator, :floor, :imag, :imaginary, :integer?, :nonzero?, :numerator,
53
- :polar, :real, :real?, :rect, :to_c, :to_int, :truncate, :zero?
54
- ].map { |v| v.to_s }.each do |attr_name|
55
- Wector.class_eval %Q{
56
- def #{attr_name}
57
- self.class.new(map { |i| i.#{attr_name} })
58
- end
59
- }
9
+ #
10
+ # wector.~
11
+ #
12
+ def ~
13
+ self.class.new(map { |i| ~i })
14
+ end
15
+
16
+ #
17
+ # wector.-
18
+ #
19
+ def -@
20
+ self.class.new(map { |i| -i })
21
+ end
22
+
23
+ #
24
+ # wector.+
25
+ #
26
+ def +@
27
+ self.class.new(map { |i| +i })
28
+ end
29
+
30
+ # Operators
31
+ #
32
+ # wector.*
33
+ #
34
+ # Arguments:
35
+ # other: (Array)
36
+ #
37
+ def *(other)
38
+ if other.is_a? Array
39
+ raise "Incorrect Dimensions" unless self.size == other.size
40
+ other = other.dup
41
+ self.class.new(map { |i| i * other.shift })
42
+ elsif other.is_a?(Integer) or other.is_a?(Float)
43
+ self.class.new(map { |i| i * other })
44
+ else
45
+ super
46
+ end
47
+ end
48
+
49
+ #
50
+ # wector.+
51
+ #
52
+ # Arguments:
53
+ # other: (Array)
54
+ #
55
+ def +(other)
56
+ if other.is_a? Array
57
+ raise "Incorrect Dimensions" unless self.size == other.size
58
+ other = other.dup
59
+ self.class.new(map { |i| i + other.shift })
60
+ elsif other.is_a?(Integer) or other.is_a?(Float)
61
+ self.class.new(map { |i| i + other })
62
+ else
63
+ super
64
+ end
65
+ end
66
+
67
+ #
68
+ # wector./
69
+ #
70
+ # Arguments:
71
+ # other: (Array)
72
+ #
73
+ def /(other)
74
+ if other.is_a? Array
75
+ raise "Incorrect Dimensions" unless self.size == other.size
76
+ other = other.dup
77
+ self.class.new(map { |i| i / other.shift })
78
+ elsif other.is_a?(Integer) or other.is_a?(Float)
79
+ self.class.new(map { |i| i / other })
80
+ else
81
+ super
82
+ end
83
+ end
84
+
85
+ #
86
+ # wector.-
87
+ #
88
+ # Arguments:
89
+ # other: (Array)
90
+ #
91
+ def -(other)
92
+ if other.is_a? Array
93
+ raise "Incorrect Dimensions" unless self.size == other.size
94
+ other = other.dup
95
+ self.class.new(map { |i| i - other.shift })
96
+ elsif other.is_a?(Integer) or other.is_a?(Float)
97
+ self.class.new(map { |i| i - other })
98
+ else
99
+ super
100
+ end
101
+ end
102
+
103
+ #
104
+ # wector.**
105
+ #
106
+ # Arguments:
107
+ # other: (Array)
108
+ #
109
+ def **(other)
110
+ if other.is_a? Array
111
+ raise "Incorrect Dimensions" unless self.size == other.size
112
+ other = other.dup
113
+ self.class.new(map { |i| i ** other.shift })
114
+ elsif other.is_a?(Integer) or other.is_a?(Float)
115
+ self.class.new(map { |i| i ** other })
116
+ else
117
+ super
118
+ end
119
+ end
120
+
121
+ #
122
+ # wector.%
123
+ #
124
+ # Arguments:
125
+ # other: (Array)
126
+ #
127
+ def %(other)
128
+ if other.is_a? Array
129
+ raise "Incorrect Dimensions" unless self.size == other.size
130
+ other = other.dup
131
+ self.class.new(map { |i| i % other.shift })
132
+ elsif other.is_a?(Integer) or other.is_a?(Float)
133
+ self.class.new(map { |i| i % other })
134
+ else
135
+ super
136
+ end
137
+ end
138
+
139
+ #
140
+ # wector.==
141
+ #
142
+ # Arguments:
143
+ # other: (Array)
144
+ #
145
+ def ==(other)
146
+ if other.is_a? Array
147
+ raise "Incorrect Dimensions" unless self.size == other.size
148
+ other = other.dup
149
+ self.class.new(map { |i| i == other.shift })
150
+ elsif other.is_a?(Integer) or other.is_a?(Float)
151
+ self.class.new(map { |i| i == other })
152
+ else
153
+ super
154
+ end
155
+ end
156
+
157
+ #
158
+ # wector.===
159
+ #
160
+ # Arguments:
161
+ # other: (Array)
162
+ #
163
+ def ===(other)
164
+ if other.is_a? Array
165
+ raise "Incorrect Dimensions" unless self.size == other.size
166
+ other = other.dup
167
+ self.class.new(map { |i| i === other.shift })
168
+ elsif other.is_a?(Integer) or other.is_a?(Float)
169
+ self.class.new(map { |i| i === other })
170
+ else
171
+ super
172
+ end
173
+ end
174
+
175
+ #
176
+ # wector.<=>
177
+ #
178
+ # Arguments:
179
+ # other: (Array)
180
+ #
181
+ def <=>(other)
182
+ if other.is_a? Array
183
+ raise "Incorrect Dimensions" unless self.size == other.size
184
+ other = other.dup
185
+ self.class.new(map { |i| i <=> other.shift })
186
+ elsif other.is_a?(Integer) or other.is_a?(Float)
187
+ self.class.new(map { |i| i <=> other })
188
+ else
189
+ super
190
+ end
191
+ end
192
+
193
+ #
194
+ # wector.>
195
+ #
196
+ # Arguments:
197
+ # other: (Array)
198
+ #
199
+ def >(other)
200
+ if other.is_a? Array
201
+ raise "Incorrect Dimensions" unless self.size == other.size
202
+ other = other.dup
203
+ self.class.new(map { |i| i > other.shift })
204
+ elsif other.is_a?(Integer) or other.is_a?(Float)
205
+ self.class.new(map { |i| i > other })
206
+ else
207
+ super
208
+ end
209
+ end
210
+
211
+ #
212
+ # wector.>=
213
+ #
214
+ # Arguments:
215
+ # other: (Array)
216
+ #
217
+ def >=(other)
218
+ if other.is_a? Array
219
+ raise "Incorrect Dimensions" unless self.size == other.size
220
+ other = other.dup
221
+ self.class.new(map { |i| i >= other.shift })
222
+ elsif other.is_a?(Integer) or other.is_a?(Float)
223
+ self.class.new(map { |i| i >= other })
224
+ else
225
+ super
226
+ end
227
+ end
228
+
229
+ #
230
+ # wector.<
231
+ #
232
+ # Arguments:
233
+ # other: (Array)
234
+ #
235
+ def <(other)
236
+ if other.is_a? Array
237
+ raise "Incorrect Dimensions" unless self.size == other.size
238
+ other = other.dup
239
+ self.class.new(map { |i| i < other.shift })
240
+ elsif other.is_a?(Integer) or other.is_a?(Float)
241
+ self.class.new(map { |i| i < other })
242
+ else
243
+ super
244
+ end
245
+ end
246
+
247
+ #
248
+ # wector.<=
249
+ #
250
+ # Arguments:
251
+ # other: (Array)
252
+ #
253
+ def <=(other)
254
+ if other.is_a? Array
255
+ raise "Incorrect Dimensions" unless self.size == other.size
256
+ other = other.dup
257
+ self.class.new(map { |i| i <= other.shift })
258
+ elsif other.is_a?(Integer) or other.is_a?(Float)
259
+ self.class.new(map { |i| i <= other })
260
+ else
261
+ super
262
+ end
263
+ end
264
+
265
+
266
+ #
267
+ # wector.modulo
268
+ #
269
+ # Arguments:
270
+ # other: (Array)
271
+ #
272
+ def modulo(other)
273
+ if other.is_a? Array
274
+ raise "Incorrect Dimensions" unless self.size == other.size
275
+ other = other.dup
276
+ self.class.new(map { |i| i.modulo other.shift })
277
+ elsif other.is_a?(Integer) or other.is_a?(Float)
278
+ self.class.new(map { |i| i.modulo other })
279
+ else
280
+ super
281
+ end
282
+ end
283
+
284
+ #
285
+ # wector.coerce
286
+ #
287
+ # Arguments:
288
+ # other: (Array)
289
+ #
290
+ def coerce(other)
291
+ if other.is_a? Array
292
+ raise "Incorrect Dimensions" unless self.size == other.size
293
+ other = other.dup
294
+ self.class.new(map { |i| i.coerce other.shift })
295
+ elsif other.is_a?(Integer) or other.is_a?(Float)
296
+ self.class.new(map { |i| i.coerce other })
297
+ else
298
+ super
299
+ end
300
+ end
301
+
302
+ #
303
+ # wector.div
304
+ #
305
+ # Arguments:
306
+ # other: (Array)
307
+ #
308
+ def div(other)
309
+ if other.is_a? Array
310
+ raise "Incorrect Dimensions" unless self.size == other.size
311
+ other = other.dup
312
+ self.class.new(map { |i| i.div other.shift })
313
+ elsif other.is_a?(Integer) or other.is_a?(Float)
314
+ self.class.new(map { |i| i.div other })
315
+ else
316
+ super
317
+ end
318
+ end
319
+
320
+ #
321
+ # wector.divmod
322
+ #
323
+ # Arguments:
324
+ # other: (Array)
325
+ #
326
+ def divmod(other)
327
+ if other.is_a? Array
328
+ raise "Incorrect Dimensions" unless self.size == other.size
329
+ other = other.dup
330
+ self.class.new(map { |i| i.divmod other.shift })
331
+ elsif other.is_a?(Integer) or other.is_a?(Float)
332
+ self.class.new(map { |i| i.divmod other })
333
+ else
334
+ super
335
+ end
336
+ end
337
+
338
+ #
339
+ # wector.eql?
340
+ #
341
+ # Arguments:
342
+ # other: (Array)
343
+ #
344
+ def eql?(other)
345
+ if other.is_a? Array
346
+ raise "Incorrect Dimensions" unless self.size == other.size
347
+ other = other.dup
348
+ self.class.new(map { |i| i.eql? other.shift })
349
+ elsif other.is_a?(Integer) or other.is_a?(Float)
350
+ self.class.new(map { |i| i.eql? other })
351
+ else
352
+ super
353
+ end
354
+ end
355
+
356
+ #
357
+ # wector.fdiv
358
+ #
359
+ # Arguments:
360
+ # other: (Array)
361
+ #
362
+ def fdiv(other)
363
+ if other.is_a? Array
364
+ raise "Incorrect Dimensions" unless self.size == other.size
365
+ other = other.dup
366
+ self.class.new(map { |i| i.fdiv other.shift })
367
+ elsif other.is_a?(Integer) or other.is_a?(Float)
368
+ self.class.new(map { |i| i.fdiv other })
369
+ else
370
+ super
371
+ end
372
+ end
373
+
374
+ #
375
+ # wector.gcd
376
+ #
377
+ # Arguments:
378
+ # other: (Array)
379
+ #
380
+ def gcd(other)
381
+ if other.is_a? Array
382
+ raise "Incorrect Dimensions" unless self.size == other.size
383
+ other = other.dup
384
+ self.class.new(map { |i| i.gcd other.shift })
385
+ elsif other.is_a?(Integer) or other.is_a?(Float)
386
+ self.class.new(map { |i| i.gcd other })
387
+ else
388
+ super
389
+ end
390
+ end
391
+
392
+ #
393
+ # wector.gcdlcm
394
+ #
395
+ # Arguments:
396
+ # other: (Array)
397
+ #
398
+ def gcdlcm(other)
399
+ if other.is_a? Array
400
+ raise "Incorrect Dimensions" unless self.size == other.size
401
+ other = other.dup
402
+ self.class.new(map { |i| i.gcdlcm other.shift })
403
+ elsif other.is_a?(Integer) or other.is_a?(Float)
404
+ self.class.new(map { |i| i.gcdlcm other })
405
+ else
406
+ super
407
+ end
408
+ end
409
+
410
+ #
411
+ # wector.lcm
412
+ #
413
+ # Arguments:
414
+ # other: (Array)
415
+ #
416
+ def lcm(other)
417
+ if other.is_a? Array
418
+ raise "Incorrect Dimensions" unless self.size == other.size
419
+ other = other.dup
420
+ self.class.new(map { |i| i.lcm other.shift })
421
+ elsif other.is_a?(Integer) or other.is_a?(Float)
422
+ self.class.new(map { |i| i.lcm other })
423
+ else
424
+ super
425
+ end
426
+ end
427
+
428
+ #
429
+ # wector.quo
430
+ #
431
+ # Arguments:
432
+ # other: (Array)
433
+ #
434
+ def quo(other)
435
+ if other.is_a? Array
436
+ raise "Incorrect Dimensions" unless self.size == other.size
437
+ other = other.dup
438
+ self.class.new(map { |i| i.quo other.shift })
439
+ elsif other.is_a?(Integer) or other.is_a?(Float)
440
+ self.class.new(map { |i| i.quo other })
441
+ else
442
+ super
443
+ end
444
+ end
445
+
446
+ #
447
+ # wector.rationalize
448
+ #
449
+ # Arguments:
450
+ # other: (Array)
451
+ #
452
+ def rationalize(other)
453
+ if other.is_a? Array
454
+ raise "Incorrect Dimensions" unless self.size == other.size
455
+ other = other.dup
456
+ self.class.new(map { |i| i.rationalize other.shift })
457
+ elsif other.is_a?(Integer) or other.is_a?(Float)
458
+ self.class.new(map { |i| i.rationalize other })
459
+ else
460
+ super
461
+ end
462
+ end
463
+
464
+ #
465
+ # wector.remainder
466
+ #
467
+ # Arguments:
468
+ # other: (Array)
469
+ #
470
+ def remainder(other)
471
+ if other.is_a? Array
472
+ raise "Incorrect Dimensions" unless self.size == other.size
473
+ other = other.dup
474
+ self.class.new(map { |i| i.remainder other.shift })
475
+ elsif other.is_a?(Integer) or other.is_a?(Float)
476
+ self.class.new(map { |i| i.remainder other })
477
+ else
478
+ super
479
+ end
480
+ end
481
+
482
+ #
483
+ # wector.round
484
+ #
485
+ # Arguments:
486
+ # other: (Array)
487
+ #
488
+ def round(other)
489
+ if other.is_a? Array
490
+ raise "Incorrect Dimensions" unless self.size == other.size
491
+ other = other.dup
492
+ self.class.new(map { |i| i.round other.shift })
493
+ elsif other.is_a?(Integer) or other.is_a?(Float)
494
+ self.class.new(map { |i| i.round other })
495
+ else
496
+ super
497
+ end
498
+ end
499
+
500
+
501
+ #
502
+ # wector.ceil
503
+ #
504
+ def ceil
505
+ self.class.new(map { |i| i.ceil })
506
+ end
507
+
508
+ #
509
+ # wector.abs
510
+ #
511
+ def abs
512
+ self.class.new(map { |i| i.abs })
513
+ end
514
+
515
+ #
516
+ # wector.abs2
517
+ #
518
+ def abs2
519
+ self.class.new(map { |i| i.abs2 })
520
+ end
521
+
522
+ #
523
+ # wector.magnitude
524
+ #
525
+ def magnitude
526
+ self.class.new(map { |i| i.magnitude })
527
+ end
528
+
529
+ #
530
+ # wector.arg
531
+ #
532
+ def arg
533
+ self.class.new(map { |i| i.arg })
534
+ end
535
+
536
+ #
537
+ # wector.angle
538
+ #
539
+ def angle
540
+ self.class.new(map { |i| i.angle })
541
+ end
542
+
543
+ #
544
+ # wector.phase
545
+ #
546
+ def phase
547
+ self.class.new(map { |i| i.phase })
548
+ end
549
+
550
+ #
551
+ # wector.conj
552
+ #
553
+ def conj
554
+ self.class.new(map { |i| i.conj })
555
+ end
556
+
557
+ #
558
+ # wector.conjugate
559
+ #
560
+ def conjugate
561
+ self.class.new(map { |i| i.conjugate })
562
+ end
563
+
564
+ #
565
+ # wector.denominator
566
+ #
567
+ def denominator
568
+ self.class.new(map { |i| i.denominator })
569
+ end
570
+
571
+ #
572
+ # wector.even?
573
+ #
574
+ def even?
575
+ self.class.new(map { |i| i.even? })
576
+ end
577
+
578
+ #
579
+ # wector.floor
580
+ #
581
+ def floor
582
+ self.class.new(map { |i| i.floor })
583
+ end
584
+
585
+ #
586
+ # wector.imag
587
+ #
588
+ def imag
589
+ self.class.new(map { |i| i.imag })
590
+ end
591
+
592
+ #
593
+ # wector.imaginary
594
+ #
595
+ def imaginary
596
+ self.class.new(map { |i| i.imaginary })
597
+ end
598
+
599
+ #
600
+ # wector.integer?
601
+ #
602
+ def integer?
603
+ self.class.new(map { |i| i.integer? })
604
+ end
605
+
606
+ #
607
+ # wector.nonzero?
608
+ #
609
+ def nonzero?
610
+ self.class.new(map { |i| i.nonzero? })
611
+ end
612
+
613
+ #
614
+ # wector.odd?
615
+ #
616
+ def odd?
617
+ self.class.new(map { |i| i.odd? })
618
+ end
619
+
620
+ #
621
+ # wector.numerator
622
+ #
623
+ def numerator
624
+ self.class.new(map { |i| i.numerator })
625
+ end
626
+
627
+ #
628
+ # wector.polar
629
+ #
630
+ def polar
631
+ self.class.new(map { |i| i.polar })
632
+ end
633
+
634
+ #
635
+ # wector.pred
636
+ #
637
+ def pred
638
+ self.class.new(map { |i| i.pred })
639
+ end
640
+
641
+ #
642
+ # wector.real
643
+ #
644
+ def real
645
+ self.class.new(map { |i| i.real })
646
+ end
647
+
648
+ #
649
+ # wector.real?
650
+ #
651
+ def real?
652
+ self.class.new(map { |i| i.real? })
653
+ end
654
+
655
+ #
656
+ # wector.rect
657
+ #
658
+ def rect
659
+ self.class.new(map { |i| i.rect })
660
+ end
661
+
662
+ #
663
+ # wector.truncate
664
+ #
665
+ def truncate
666
+ self.class.new(map { |i| i.truncate })
667
+ end
668
+
669
+ #
670
+ # wector.zero?
671
+ #
672
+ def zero?
673
+ self.class.new(map { |i| i.zero? })
674
+ end
675
+
60
676
  end
61
677
 
62
678
  # Add interface to convert arrays to vectors
data/src/build.rb ADDED
@@ -0,0 +1,55 @@
1
+ require 'erb'
2
+
3
+ operator_methods = [
4
+ :*,:+,:/,:-,:**,:%,:==,:===,:<=>,:>,:>=,:<,:<=
5
+ ]
6
+
7
+ comparison_methods = [
8
+ :modulo, :coerce, :div, :divmod, :eql?, :fdiv, :gcd, :gcdlcm, :lcm, :quo,
9
+ :rationalize, :remainder, :round
10
+ ]
11
+
12
+ transform_methods = [
13
+ :ceil, :abs, :abs2, :magnitude, :arg, :angle, :phase, :conj, :conjugate,
14
+ :denominator, :even?, :floor, :imag, :imaginary, :integer?, :nonzero?,
15
+ :odd?, :numerator, :polar, :pred, :real, :real?, :rect, :truncate, :zero?
16
+ ]
17
+
18
+ operator_tests = {
19
+ # Basic math
20
+ multiplication: '*',
21
+ subtraction: '-',
22
+ addition: '+',
23
+ division: '/',
24
+
25
+ # Complex math
26
+ exponent: '**',
27
+ modulus: '%',
28
+
29
+ # Comparisons
30
+ greater: '>',
31
+ lesser: '<',
32
+ equal: '==',
33
+ strict_equal: '===',
34
+ greater_or_equal: '>=',
35
+ lesser_or_equal: '<=',
36
+ greater_equal_or_lesser: '<=>'
37
+ }
38
+
39
+ unary_operator_tests = {
40
+ inversion: '~',
41
+ minus: '-',
42
+ plus: '+'
43
+ }
44
+
45
+ base_folder = File.join(File.dirname(__FILE__), '..')
46
+
47
+ # Generate lib/wector.rb
48
+ File.open(File.join(base_folder, 'lib', 'wector.rb'), 'w') do |file|
49
+ file.write ERB.new(File.read('wector.erb'), 0, "%<>").result
50
+ end
51
+
52
+ # Generate tests/basic.rb
53
+ File.open(File.join(base_folder, 'test', 'test_basic.rb'), 'w') do |file|
54
+ file.write ERB.new(File.read('test.erb'), 0, "%<>").result
55
+ end
data/src/test.erb ADDED
@@ -0,0 +1,37 @@
1
+ require 'test/unit'
2
+ require 'wector'
3
+
4
+ class BasicTest < Test::Unit::TestCase
5
+
6
+ def test_construction_and_conversion
7
+ assert_equal Wector.new([1,2,3]), [1,2,3].to_wector
8
+ end
9
+
10
+ def test_insertion
11
+ foo = [1,2,3].to_wector
12
+ foo[3] = 4
13
+ foo << 5
14
+ assert_equal foo, [1,2,3,4,5]
15
+ end
16
+ <% operator_tests.each do |name, op| %>
17
+ def test_<%= name %>
18
+ nums = (1..100).to_a.to_wector
19
+ assert_equal (nums <%= op %> nums), nums.map { |v| v <%= op %> v }
20
+ end
21
+ <% end %><% unary_operator_tests.each do |name, op| %>
22
+ def test_<%= name %>
23
+ nums = (1..100).to_a.to_wector
24
+ assert_equal (<%= op %>nums), nums.map { |v| <%= op %>v }
25
+ end
26
+ <% end %><% comparison_methods.each do |name| %>
27
+ def test_<%= name %>
28
+ nums = (1..100).to_a.to_wector
29
+ assert_equal (nums.<%= name %> nums), nums.map { |v| v.<%= name %> v }
30
+ end
31
+ <% end %><% transform_methods.each do |name| %>
32
+ def test_<%= name %>
33
+ nums = (1..100).to_a.to_wector
34
+ assert_equal (nums.<%= name %>), nums.map { |v| v.<%= name %> }
35
+ end
36
+ <% end %>
37
+ end
data/src/wector.erb ADDED
@@ -0,0 +1,80 @@
1
+ class Wector < Array
2
+
3
+ #
4
+ # wector.[]
5
+ #
6
+ def [](*args)
7
+ args.each do |arg|
8
+ raise "Non-numeric item" unless arg.is_a?(Integer) or arg.is_a?(Float)
9
+ unshift arg
10
+ end
11
+ end
12
+
13
+ #
14
+ # wector.~
15
+ #
16
+ def ~
17
+ self.class.new(map { |i| ~i })
18
+ end
19
+ <% [:-, :+].map { |v| v.to_s }.each do |attr_name| %>
20
+ #
21
+ # wector.<%= attr_name %>
22
+ #
23
+ def <%= attr_name %>@
24
+ self.class.new(map { |i| <%= attr_name %>i })
25
+ end
26
+ <% end %>
27
+ # Operators<% operator_methods.map { |v| v.to_s }.each do |attr_name| %>
28
+ #
29
+ # wector.<%= attr_name %>
30
+ #
31
+ # Arguments:
32
+ # other: (Array)
33
+ #
34
+ def <%= attr_name %>(other)
35
+ if other.is_a? Array
36
+ raise "Incorrect Dimensions" unless self.size == other.size
37
+ other = other.dup
38
+ self.class.new(map { |i| i <%= attr_name %> other.shift })
39
+ elsif other.is_a?(Integer) or other.is_a?(Float)
40
+ self.class.new(map { |i| i <%= attr_name %> other })
41
+ else
42
+ super
43
+ end
44
+ end
45
+ <% end %>
46
+ <% comparison_methods.map { |v| v.to_s }.each do |attr_name| %>
47
+ #
48
+ # wector.<%= attr_name %>
49
+ #
50
+ # Arguments:
51
+ # other: (Array)
52
+ #
53
+ def <%= attr_name %>(other)
54
+ if other.is_a? Array
55
+ raise "Incorrect Dimensions" unless self.size == other.size
56
+ other = other.dup
57
+ self.class.new(map { |i| i.<%= attr_name %> other.shift })
58
+ elsif other.is_a?(Integer) or other.is_a?(Float)
59
+ self.class.new(map { |i| i.<%= attr_name %> other })
60
+ else
61
+ super
62
+ end
63
+ end
64
+ <% end %>
65
+ <% transform_methods.map { |v| v.to_s }.each do |attr_name| %>
66
+ #
67
+ # wector.<%= attr_name %>
68
+ #
69
+ def <%= attr_name %>
70
+ self.class.new(map { |i| i.<%= attr_name %> })
71
+ end
72
+ <% end %>
73
+ end
74
+
75
+ # Add interface to convert arrays to vectors
76
+ class Array
77
+ def to_wector
78
+ Wector.new(self)
79
+ end
80
+ end
@@ -0,0 +1,287 @@
1
+ require 'test/unit'
2
+ require 'wector'
3
+
4
+ class BasicTest < Test::Unit::TestCase
5
+
6
+ def test_construction_and_conversion
7
+ assert_equal Wector.new([1,2,3]), [1,2,3].to_wector
8
+ end
9
+
10
+ def test_insertion
11
+ foo = [1,2,3].to_wector
12
+ foo[3] = 4
13
+ foo << 5
14
+ assert_equal foo, [1,2,3,4,5]
15
+ end
16
+
17
+ def test_multiplication
18
+ nums = (1..100).to_a.to_wector
19
+ assert_equal (nums * nums), nums.map { |v| v * v }
20
+ end
21
+
22
+ def test_subtraction
23
+ nums = (1..100).to_a.to_wector
24
+ assert_equal (nums - nums), nums.map { |v| v - v }
25
+ end
26
+
27
+ def test_addition
28
+ nums = (1..100).to_a.to_wector
29
+ assert_equal (nums + nums), nums.map { |v| v + v }
30
+ end
31
+
32
+ def test_division
33
+ nums = (1..100).to_a.to_wector
34
+ assert_equal (nums / nums), nums.map { |v| v / v }
35
+ end
36
+
37
+ def test_exponent
38
+ nums = (1..100).to_a.to_wector
39
+ assert_equal (nums ** nums), nums.map { |v| v ** v }
40
+ end
41
+
42
+ def test_modulus
43
+ nums = (1..100).to_a.to_wector
44
+ assert_equal (nums % nums), nums.map { |v| v % v }
45
+ end
46
+
47
+ def test_greater
48
+ nums = (1..100).to_a.to_wector
49
+ assert_equal (nums > nums), nums.map { |v| v > v }
50
+ end
51
+
52
+ def test_lesser
53
+ nums = (1..100).to_a.to_wector
54
+ assert_equal (nums < nums), nums.map { |v| v < v }
55
+ end
56
+
57
+ def test_equal
58
+ nums = (1..100).to_a.to_wector
59
+ assert_equal (nums == nums), nums.map { |v| v == v }
60
+ end
61
+
62
+ def test_strict_equal
63
+ nums = (1..100).to_a.to_wector
64
+ assert_equal (nums === nums), nums.map { |v| v === v }
65
+ end
66
+
67
+ def test_greater_or_equal
68
+ nums = (1..100).to_a.to_wector
69
+ assert_equal (nums >= nums), nums.map { |v| v >= v }
70
+ end
71
+
72
+ def test_lesser_or_equal
73
+ nums = (1..100).to_a.to_wector
74
+ assert_equal (nums <= nums), nums.map { |v| v <= v }
75
+ end
76
+
77
+ def test_greater_equal_or_lesser
78
+ nums = (1..100).to_a.to_wector
79
+ assert_equal (nums <=> nums), nums.map { |v| v <=> v }
80
+ end
81
+
82
+ def test_inversion
83
+ nums = (1..100).to_a.to_wector
84
+ assert_equal (~nums), nums.map { |v| ~v }
85
+ end
86
+
87
+ def test_minus
88
+ nums = (1..100).to_a.to_wector
89
+ assert_equal (-nums), nums.map { |v| -v }
90
+ end
91
+
92
+ def test_plus
93
+ nums = (1..100).to_a.to_wector
94
+ assert_equal (+nums), nums.map { |v| +v }
95
+ end
96
+
97
+ def test_modulo
98
+ nums = (1..100).to_a.to_wector
99
+ assert_equal (nums.modulo nums), nums.map { |v| v.modulo v }
100
+ end
101
+
102
+ def test_coerce
103
+ nums = (1..100).to_a.to_wector
104
+ assert_equal (nums.coerce nums), nums.map { |v| v.coerce v }
105
+ end
106
+
107
+ def test_div
108
+ nums = (1..100).to_a.to_wector
109
+ assert_equal (nums.div nums), nums.map { |v| v.div v }
110
+ end
111
+
112
+ def test_divmod
113
+ nums = (1..100).to_a.to_wector
114
+ assert_equal (nums.divmod nums), nums.map { |v| v.divmod v }
115
+ end
116
+
117
+ def test_eql?
118
+ nums = (1..100).to_a.to_wector
119
+ assert_equal (nums.eql? nums), nums.map { |v| v.eql? v }
120
+ end
121
+
122
+ def test_fdiv
123
+ nums = (1..100).to_a.to_wector
124
+ assert_equal (nums.fdiv nums), nums.map { |v| v.fdiv v }
125
+ end
126
+
127
+ def test_gcd
128
+ nums = (1..100).to_a.to_wector
129
+ assert_equal (nums.gcd nums), nums.map { |v| v.gcd v }
130
+ end
131
+
132
+ def test_gcdlcm
133
+ nums = (1..100).to_a.to_wector
134
+ assert_equal (nums.gcdlcm nums), nums.map { |v| v.gcdlcm v }
135
+ end
136
+
137
+ def test_lcm
138
+ nums = (1..100).to_a.to_wector
139
+ assert_equal (nums.lcm nums), nums.map { |v| v.lcm v }
140
+ end
141
+
142
+ def test_quo
143
+ nums = (1..100).to_a.to_wector
144
+ assert_equal (nums.quo nums), nums.map { |v| v.quo v }
145
+ end
146
+
147
+ def test_rationalize
148
+ nums = (1..100).to_a.to_wector
149
+ assert_equal (nums.rationalize nums), nums.map { |v| v.rationalize v }
150
+ end
151
+
152
+ def test_remainder
153
+ nums = (1..100).to_a.to_wector
154
+ assert_equal (nums.remainder nums), nums.map { |v| v.remainder v }
155
+ end
156
+
157
+ def test_round
158
+ nums = (1..100).to_a.to_wector
159
+ assert_equal (nums.round nums), nums.map { |v| v.round v }
160
+ end
161
+
162
+ def test_ceil
163
+ nums = (1..100).to_a.to_wector
164
+ assert_equal (nums.ceil), nums.map { |v| v.ceil }
165
+ end
166
+
167
+ def test_abs
168
+ nums = (1..100).to_a.to_wector
169
+ assert_equal (nums.abs), nums.map { |v| v.abs }
170
+ end
171
+
172
+ def test_abs2
173
+ nums = (1..100).to_a.to_wector
174
+ assert_equal (nums.abs2), nums.map { |v| v.abs2 }
175
+ end
176
+
177
+ def test_magnitude
178
+ nums = (1..100).to_a.to_wector
179
+ assert_equal (nums.magnitude), nums.map { |v| v.magnitude }
180
+ end
181
+
182
+ def test_arg
183
+ nums = (1..100).to_a.to_wector
184
+ assert_equal (nums.arg), nums.map { |v| v.arg }
185
+ end
186
+
187
+ def test_angle
188
+ nums = (1..100).to_a.to_wector
189
+ assert_equal (nums.angle), nums.map { |v| v.angle }
190
+ end
191
+
192
+ def test_phase
193
+ nums = (1..100).to_a.to_wector
194
+ assert_equal (nums.phase), nums.map { |v| v.phase }
195
+ end
196
+
197
+ def test_conj
198
+ nums = (1..100).to_a.to_wector
199
+ assert_equal (nums.conj), nums.map { |v| v.conj }
200
+ end
201
+
202
+ def test_conjugate
203
+ nums = (1..100).to_a.to_wector
204
+ assert_equal (nums.conjugate), nums.map { |v| v.conjugate }
205
+ end
206
+
207
+ def test_denominator
208
+ nums = (1..100).to_a.to_wector
209
+ assert_equal (nums.denominator), nums.map { |v| v.denominator }
210
+ end
211
+
212
+ def test_even?
213
+ nums = (1..100).to_a.to_wector
214
+ assert_equal (nums.even?), nums.map { |v| v.even? }
215
+ end
216
+
217
+ def test_floor
218
+ nums = (1..100).to_a.to_wector
219
+ assert_equal (nums.floor), nums.map { |v| v.floor }
220
+ end
221
+
222
+ def test_imag
223
+ nums = (1..100).to_a.to_wector
224
+ assert_equal (nums.imag), nums.map { |v| v.imag }
225
+ end
226
+
227
+ def test_imaginary
228
+ nums = (1..100).to_a.to_wector
229
+ assert_equal (nums.imaginary), nums.map { |v| v.imaginary }
230
+ end
231
+
232
+ def test_integer?
233
+ nums = (1..100).to_a.to_wector
234
+ assert_equal (nums.integer?), nums.map { |v| v.integer? }
235
+ end
236
+
237
+ def test_nonzero?
238
+ nums = (1..100).to_a.to_wector
239
+ assert_equal (nums.nonzero?), nums.map { |v| v.nonzero? }
240
+ end
241
+
242
+ def test_odd?
243
+ nums = (1..100).to_a.to_wector
244
+ assert_equal (nums.odd?), nums.map { |v| v.odd? }
245
+ end
246
+
247
+ def test_numerator
248
+ nums = (1..100).to_a.to_wector
249
+ assert_equal (nums.numerator), nums.map { |v| v.numerator }
250
+ end
251
+
252
+ def test_polar
253
+ nums = (1..100).to_a.to_wector
254
+ assert_equal (nums.polar), nums.map { |v| v.polar }
255
+ end
256
+
257
+ def test_pred
258
+ nums = (1..100).to_a.to_wector
259
+ assert_equal (nums.pred), nums.map { |v| v.pred }
260
+ end
261
+
262
+ def test_real
263
+ nums = (1..100).to_a.to_wector
264
+ assert_equal (nums.real), nums.map { |v| v.real }
265
+ end
266
+
267
+ def test_real?
268
+ nums = (1..100).to_a.to_wector
269
+ assert_equal (nums.real?), nums.map { |v| v.real? }
270
+ end
271
+
272
+ def test_rect
273
+ nums = (1..100).to_a.to_wector
274
+ assert_equal (nums.rect), nums.map { |v| v.rect }
275
+ end
276
+
277
+ def test_truncate
278
+ nums = (1..100).to_a.to_wector
279
+ assert_equal (nums.truncate), nums.map { |v| v.truncate }
280
+ end
281
+
282
+ def test_zero?
283
+ nums = (1..100).to_a.to_wector
284
+ assert_equal (nums.zero?), nums.map { |v| v.zero? }
285
+ end
286
+
287
+ end
data/wector.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  # coding: utf-8
2
2
  Gem::Specification.new do |spec|
3
3
  spec.name = "wector"
4
- spec.version = "0.0.2"
4
+ spec.version = "0.0.3"
5
5
  spec.authors = ["Stephen Belanger"]
6
6
  spec.email = ["admin@stephenbelanger.com"]
7
7
  spec.description = %q{
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wector
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen Belanger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-04-03 00:00:00.000000000 Z
11
+ date: 2013-04-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -53,6 +53,10 @@ files:
53
53
  - README.md
54
54
  - Rakefile
55
55
  - lib/wector.rb
56
+ - src/build.rb
57
+ - src/test.erb
58
+ - src/wector.erb
59
+ - test/test_basic.rb
56
60
  - wector.gemspec
57
61
  homepage: ''
58
62
  licenses:
@@ -78,4 +82,5 @@ rubygems_version: 2.0.0
78
82
  signing_key:
79
83
  specification_version: 4
80
84
  summary: Wector makes vector math dead simple
81
- test_files: []
85
+ test_files:
86
+ - test/test_basic.rb