multiarray 0.24.0 → 0.25.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -6,7 +6,7 @@ require 'rake/packagetask'
6
6
  require 'rbconfig'
7
7
 
8
8
  PKG_NAME = 'multiarray'
9
- PKG_VERSION = '0.24.0'
9
+ PKG_VERSION = '0.25.0'
10
10
  RB_FILES = FileList[ 'lib/**/*.rb' ]
11
11
  TC_FILES = FileList[ 'test/tc_*.rb' ]
12
12
  TS_FILES = FileList[ 'test/ts_*.rb' ]
data/lib/multiarray.rb CHANGED
@@ -808,33 +808,85 @@ module Hornetseye
808
808
 
809
809
  module_function :finalise
810
810
 
811
- # Method for summing values
811
+ # Method for specifying injections in a different way
812
812
  #
813
- # @param [Array<Integer>] *shape Optional shape of result. The method
814
- # attempts to infer the shape if not specified.
815
- # @yield Operation returning array elements.
813
+ # @overload inject(*shape, op, &action)
814
+ # @param [Array<Integer>] *shape Optional shape of result.
815
+ # @param [Proc] op Block of injection.
816
+ # @yield Operation returning array elements.
816
817
  #
817
818
  # @return [Object,Node] Sum of values.
818
- def sum( *shape, &action )
819
- options = shape.last.is_a?( Hash ) ? shape.pop : {}
820
- arity = options[ :arity ] || [ action.arity, shape.size ].max
819
+ #
820
+ # @private
821
+ def inject(*shape, &action)
822
+ op = shape.pop
823
+ options = shape.last.is_a?(Hash) ? shape.pop : {}
824
+ arity = options[:arity] || [action.arity, shape.size].max
821
825
  if arity <= 0
822
826
  term = action.call
823
827
  term.matched? ? term.to_type(term.typecode.maxint) : term
824
828
  else
825
- index = Variable.new shape.empty? ? Hornetseye::INDEX( nil ) :
826
- Hornetseye::INDEX( shape.pop )
827
- term = sum *( shape + [ :arity => arity - 1 ] ) do |*args|
828
- action.call *( args + [ index ] )
829
+ index = Variable.new shape.empty? ? Hornetseye::INDEX(nil) :
830
+ Hornetseye::INDEX(shape.pop)
831
+ term = inject *(shape + [:arity => arity - 1] + [op]) do |*args|
832
+ action.call *(args + [index])
829
833
  end
830
834
  var1 = Variable.new term.typecode
831
835
  var2 = Variable.new term.typecode
832
- Inject.new( term, index, nil, var1 + var2, var1, var2 ).force
836
+ Inject.new(term, index, nil, op.call(var1, var2), var1, var2).force
833
837
  end
834
838
  end
835
839
 
840
+ module_function :inject
841
+
842
+ # Method for summing values
843
+ #
844
+ # @param [Array<Integer>] *shape Optional shape of result.
845
+ # @yield Operation returning array elements.
846
+ #
847
+ # @return [Object,Node] Sum of values.
848
+ def sum(*shape, &action)
849
+ inject *(shape + [proc { |a,b| a + b }]), &action
850
+ end
851
+
836
852
  module_function :sum
837
853
 
854
+ # Method for computing product of values
855
+ #
856
+ # @param [Array<Integer>] *shape Optional shape of result.
857
+ # @yield Operation returning array elements.
858
+ #
859
+ # @return [Object,Node] Product of values.
860
+ def prod(*shape, &action)
861
+ inject *(shape + [proc { |a,b| a * b }]), &action
862
+ end
863
+
864
+ module_function :prod
865
+
866
+ # Method for computing minimum of values
867
+ #
868
+ # @param [Array<Integer>] *shape Optional shape of result.
869
+ # @yield Operation returning array elements.
870
+ #
871
+ # @return [Object,Node] Minimum of values.
872
+ def min(*shape, &action)
873
+ inject *(shape + [proc { |a,b| a.minor b }]), &action
874
+ end
875
+
876
+ module_function :min
877
+
878
+ # Method for computing maximum of values
879
+ #
880
+ # @param [Array<Integer>] *shape Optional shape of result.
881
+ # @yield Operation returning array elements.
882
+ #
883
+ # @return [Object,Node] Maximum of values.
884
+ def max(*shape, &action)
885
+ inject *(shape + [proc { |a,b| a.major b }]), &action
886
+ end
887
+
888
+ module_function :max
889
+
838
890
  def argument(block, options = {}, &action)
839
891
  arity = options[:arity] || action.arity
840
892
  if arity > 0
@@ -859,8 +911,10 @@ module Hornetseye
859
911
  elsif sub_arg.first.is_a? Integer
860
912
  sub_arg + [lut[*sub_arg]]
861
913
  else
862
- id = lazy(*sub_arg.first.shape) { |*i| i.last }
863
- sub_arg + [lut.warp(*(sub_arg + [id]))]
914
+ id = (sub_arg.size ... lut.dimension).collect do |i|
915
+ lazy(*sub_arg.first.shape) { |*j| j[i - lut.dimension + sub_arg.first.dimension] }
916
+ end
917
+ sub_arg + [lut.warp(*(sub_arg + id))]
864
918
  end
865
919
  else
866
920
  []
@@ -438,6 +438,20 @@ module Hornetseye
438
438
  Hornetseye::lazy { to_type typecode.maxint }.inject :+
439
439
  end
440
440
 
441
+ # Compute product of array
442
+ #
443
+ # @return [Object] Product of array.
444
+ def prod
445
+ Hornetseye::lazy { to_type typecode.maxint }.inject :*
446
+ end
447
+
448
+ # Compute average of array
449
+ #
450
+ # @return [Object] Mean of array.
451
+ def mean
452
+ sum / size
453
+ end
454
+
441
455
  # Find range of values of array
442
456
  #
443
457
  # @return [Object] Range of values of array.
@@ -491,6 +505,29 @@ module Hornetseye
491
505
  collect { |x| x.major( range.begin ).minor range.end }
492
506
  end
493
507
 
508
+ # Stretch values from one range to another
509
+ #
510
+ # @param [Range] from Target range of values.
511
+ # @param [Range] to Source range of values.
512
+ #
513
+ # @return [Node] Array with stretched values.
514
+ def stretch(from = 0 .. 0xFF, to = 0 .. 0xFF)
515
+ if from.exclude_end?
516
+ raise "Stretching does not support ranges with end value " +
517
+ "excluded (such as #{from})"
518
+ end
519
+ if to.exclude_end?
520
+ raise "Stretching does not support ranges with end value " +
521
+ "excluded (such as #{to})"
522
+ end
523
+ if from.last != from.first
524
+ factor = (to.last - to.first).to_f / (from.last - from.first)
525
+ collect { |x| ((x - from.first) * factor).major(to.first).minor to.last }
526
+ else
527
+ (self <= from.first).conditional to.first, to.last
528
+ end
529
+ end
530
+
494
531
  # Fill array with a value
495
532
  #
496
533
  # @param [Object] value Value to fill array with.
@@ -949,5 +949,17 @@ module Math
949
949
  module_function :sqrt_without_rgb
950
950
  module_function :sqrt
951
951
 
952
+ def log_with_rgb(c)
953
+ if c.is_a? Hornetseye::RGB
954
+ Hornetseye::RGB.new log( c.r ), log( c.g ), log( c.b )
955
+ else
956
+ log_without_rgb c
957
+ end
958
+ end
959
+
960
+ alias_method_chain :log, :rgb
961
+ module_function :log_without_rgb
962
+ module_function :log
963
+
952
964
  end
953
965
 
@@ -369,16 +369,16 @@ class TC_MultiArray < Test::Unit::TestCase
369
369
  assert_equal [ [ 1, 2, 3 ] , [ 4, 5, 6 ] ], sum { || m }.to_a
370
370
  end
371
371
 
372
- def test_argmin
373
- assert_equal [[0, 0, 1]],
374
- argmin { |i| M[[1, 2, 3], [4, 3, 2]][i] }.collect { |x| x.to_a }
375
- assert_equal [2, 1], argmin { |i,j| M[[1, 2, 3], [4, 3, 0]][i,j] }
372
+ def test_argmax
373
+ assert_equal [[1, 1, 0]],
374
+ argmax { |i| M[[1, 2, 3], [4, 3, 2]][i] }.collect { |x| x.to_a }
375
+ assert_equal [0, 1], argmax { |i,j| M[[1, 2, 3], [4, 3, 0]][i,j] }
376
376
  m = M[[[1,0,3,4],[5,4,3,2],[1,2,1,0]],[[3,2,4,1],[7,4,8,2],[2,1,9,1]]]
377
- assert_equal [[[0, 0, 0, 1], [0, 0, 0, 0], [0, 1, 0, 0]]],
378
- argmin { |i| m[i] }.collect { |x| x.to_a }
379
- assert_equal [[0, 0, 2, 2], [0, 0, 0, 0]],
380
- argmin { |i,j| m[i,j] }.collect { |x| x.to_a }
381
- assert_equal [1, 0, 0], argmin { |i,j,k| m[i,j,k] }
377
+ assert_equal [[[1, 1, 1, 0], [1, 0, 1, 0], [1, 0, 1, 1]]],
378
+ argmax { |i| m[i] }.collect { |x| x.to_a }
379
+ assert_equal [[1, 1, 2, 0], [1, 0, 1, 0]],
380
+ argmax { |i,j| m[i,j] }.collect { |x| x.to_a }
381
+ assert_equal [2, 2, 1], argmax { |i,j,k| m[i,j,k] }
382
382
  end
383
383
 
384
384
  def test_min
data/test/tc_sequence.rb CHANGED
@@ -48,6 +48,18 @@ class TC_Sequence < Test::Unit::TestCase
48
48
  Hornetseye::sum *args, &action
49
49
  end
50
50
 
51
+ def prod( *args, &action )
52
+ Hornetseye::prod *args, &action
53
+ end
54
+
55
+ def min( *args, &action )
56
+ Hornetseye::min *args, &action
57
+ end
58
+
59
+ def max( *args, &action )
60
+ Hornetseye::max *args, &action
61
+ end
62
+
51
63
  def argmin( *args, &action )
52
64
  Hornetseye::argmin *args, &action
53
65
  end
@@ -315,6 +327,17 @@ class TC_Sequence < Test::Unit::TestCase
315
327
  assert_equal 384, sum { |i| S[ 128, 128, 128 ][i] }
316
328
  end
317
329
 
330
+ def test_prod
331
+ [ S(O), S(I) ].each do |t|
332
+ assert_equal 6, t[ 1, 2, 3 ].prod
333
+ assert_equal 6, prod { |i| t[ 1, 2, 3 ][ i ] }
334
+ end
335
+ end
336
+
337
+ def test_mean
338
+ assert_equal 2, S(I)[1, 2, 3].mean
339
+ end
340
+
318
341
  def test_argmin
319
342
  [S(O), S(I)].each do |t|
320
343
  assert_equal [], argmin { t[1, 0, 2] }
@@ -334,6 +357,7 @@ class TC_Sequence < Test::Unit::TestCase
334
357
  def test_min
335
358
  [O, I].each do |t|
336
359
  assert_equal 2, S(t)[4, 2, 3].min
360
+ assert_equal 2, min { |i| S(t)[4, 2, 3][i] }
337
361
  end
338
362
  assert_equal C(1, 2, 1), S[C(1, 2, 3), C(3, 2, 1)].min
339
363
  end
@@ -341,6 +365,7 @@ class TC_Sequence < Test::Unit::TestCase
341
365
  def test_max
342
366
  [O, I].each do |t|
343
367
  assert_equal 4, S(t)[4, 2, 3].max
368
+ assert_equal 4, max { |i| S(t)[4, 2, 3][i] }
344
369
  end
345
370
  assert_equal C(3, 2, 3), S[C(1, 2, 3), C(3, 2, 1)].max
346
371
  end
@@ -359,6 +384,13 @@ class TC_Sequence < Test::Unit::TestCase
359
384
  assert_equal [ C( 3, 4, 5 ) ], S[ C( 2, 4, 6 ) ].clip( 3 .. 5 ).to_a
360
385
  end
361
386
 
387
+ def test_stretch
388
+ assert_equal [0.0, 127.5, 255.0], S[1, 2, 3].stretch(1 .. 3).to_a
389
+ assert_equal [0, 0, 255], S[1, 2, 3].stretch(2 .. 2).to_a
390
+ assert_equal [0.0, 0.5, 1.0], S[1, 2, 3].stretch(1 .. 3, 0 .. 1).to_a
391
+ assert_equal [0, 0, 1], S[1, 2, 3].stretch(2 .. 2, 0 .. 1).to_a
392
+ end
393
+
362
394
  def test_sum
363
395
  [ S(O), S(I) ].each do |t|
364
396
  assert_equal 9, t[ 4, 2, 3 ].sum
@@ -823,6 +855,12 @@ class TC_Sequence < Test::Unit::TestCase
823
855
  assert_in_delta x.real, y.real, 1.0e-5
824
856
  assert_in_delta x.imag, y.imag, 1.0e-5
825
857
  end
858
+ [ Math.log( C( 1, 2, 3 ) ), Math.log( C( 2, 4, 6 ) ) ].
859
+ zip( Math.log( S[ C( 1, 2, 3 ), C( 2, 4, 6 ) ] ).to_a ).each do |x,y|
860
+ assert_in_delta x.r, y.r, 1.0e-5
861
+ assert_in_delta x.g, y.g, 1.0e-5
862
+ assert_in_delta x.b, y.b, 1.0e-5
863
+ end
826
864
  end
827
865
 
828
866
  def test_log10
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: multiarray
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.24.0
5
+ version: 0.25.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jan Wedekind
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-07-01 00:00:00 Z
13
+ date: 2011-09-06 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: malloc