multiarray 0.14.1 → 0.14.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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.14.1'
9
+ PKG_VERSION = '0.14.2'
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
@@ -539,6 +539,10 @@ class Array
539
539
 
540
540
  end
541
541
 
542
+ begin
543
+ require 'continuation'
544
+ rescue Exception
545
+ end
542
546
  require 'complex'
543
547
  require 'malloc'
544
548
  require 'rbconfig'
@@ -644,7 +648,8 @@ module Hornetseye
644
648
  options = shape.last.is_a?( Hash ) ? shape.pop : {}
645
649
  arity = options[ :arity ] || [ action.arity, shape.size ].max
646
650
  if arity <= 0
647
- action.call
651
+ term = action.call
652
+ term.is_a?( Node ) ? term.to_type( term.typecode.maxint ) : term
648
653
  else
649
654
  index = Variable.new shape.empty? ? Hornetseye::INDEX( nil ) :
650
655
  Hornetseye::INDEX( shape.pop )
@@ -329,7 +329,7 @@ module Hornetseye
329
329
  #
330
330
  # @return [Object] Sum of array.
331
331
  def sum
332
- inject { |a,b| a + b }
332
+ Hornetseye::lazy { to_type typecode.maxint }.inject { |a,b| a + b }
333
333
  end
334
334
 
335
335
  # Find range of values of array
@@ -502,11 +502,6 @@ module Hornetseye
502
502
  #
503
503
  # @return [Node] The result of the lookup operation.
504
504
  def warp( *field )
505
-
506
- #elsif shape.first > lut.dimension or dimension == 1
507
- # reshape( *( [ 1 ] + shape ) ).map lut, options
508
-
509
-
510
505
  options = field.last.is_a?( Hash ) ? field.pop : {}
511
506
  options = { :safe => true, :default => typecode.default }.merge options
512
507
  if options[ :safe ]
@@ -538,6 +533,57 @@ module Hornetseye
538
533
  left
539
534
  end
540
535
 
536
+ # Mirror the array
537
+ #
538
+ # @param [Array<Integer>] dimensions The dimensions which should be flipped.
539
+ #
540
+ # @return [Node] The result of flipping the dimensions.
541
+ def flip( *dimensions )
542
+ field = ( 0 ... dimension ).collect do |i|
543
+ if dimensions.member? i
544
+ Hornetseye::lazy( *shape ) { |*args| shape[i] - 1 - args[i] }
545
+ else
546
+ Hornetseye::lazy( *shape ) { |*args| args[i] }
547
+ end
548
+ end
549
+ warp *field
550
+ end
551
+
552
+ def shift( *offset )
553
+ if offset.size != shape.size
554
+ raise "#{offset.size} offset(s) were given but array has " +
555
+ "#{shape.size} dimensions"
556
+ end
557
+ retval = array_type.new
558
+ target, source, open, close = [], [], [], []
559
+ ( shape.size - 1 ).step( 0, -1 ) do |i|
560
+ callcc do |pass|
561
+ delta = offset[i] % shape[i]
562
+ source[i] = 0 ... shape[i] - delta
563
+ target[i] = delta ... shape[i]
564
+ callcc do |c|
565
+ open[i] = c
566
+ pass.call
567
+ end
568
+ source[i] = shape[i] - delta ... shape[i]
569
+ target[i] = 0 ... delta
570
+ callcc do |c|
571
+ open[i] = c
572
+ pass.call
573
+ end
574
+ close[i].call
575
+ end
576
+ end
577
+ retval[ *target ] = self[ *source ]
578
+ for i in 0 ... shape.size
579
+ callcc do |c|
580
+ close[i] = c
581
+ open[i].call
582
+ end
583
+ end
584
+ retval
585
+ end
586
+
541
587
  end
542
588
 
543
589
  class Node
@@ -479,6 +479,24 @@ class TC_MultiArray < Test::Unit::TestCase
479
479
  assert_raise( RuntimeError ) { [ S[ 0, 1 ], S[ 0, 1 ] ].lut S[ 1, 2 ] }
480
480
  end
481
481
 
482
+ def test_flip
483
+ [ O, I ].each do |t|
484
+ assert_equal M( t, 3, 2 )[ [ 3, 2, 1 ], [ 6, 5, 4 ] ],
485
+ M( t, 3, 2 )[ [ 1, 2, 3 ], [ 4, 5, 6 ] ].flip( 0 )
486
+ assert_equal M( t, 3, 2 )[ [ 4, 5, 6 ], [ 1, 2, 3 ] ],
487
+ M( t, 3, 2 )[ [ 1, 2, 3 ], [ 4, 5, 6 ] ].flip( 1 )
488
+ assert_equal M( t, 3, 2 )[ [ 6, 5, 4 ], [ 3, 2, 1 ] ],
489
+ M( t, 3, 2 )[ [ 1, 2, 3 ], [ 4, 5, 6 ] ].flip( 0, 1 )
490
+ end
491
+ end
492
+
493
+ def test_shift
494
+ [ O, I ].each do |t|
495
+ assert_equal M( t, 2, 2 )[ [ 4, 3 ], [ 2, 1 ] ],
496
+ M( t, 2, 2 )[ [ 1, 2 ], [ 3, 4 ] ].shift( 1, 1 )
497
+ end
498
+ end
499
+
482
500
  def test_zero
483
501
  assert_equal M[ [ false, true ], [ true, false ] ],
484
502
  M[ [ -1, 0 ], [ 0, 1 ] ].zero?
data/test/tc_sequence.rb CHANGED
@@ -286,11 +286,16 @@ class TC_Sequence < Test::Unit::TestCase
286
286
 
287
287
  def test_sum
288
288
  [ S( O, 3 ), S( I, 3 ) ].each do |t|
289
+ assert_equal 6, t[ 1, 2, 3 ].sum
289
290
  assert_equal 6, sum { |i| t[ 1, 2, 3 ][ i ] }
290
291
  assert_equal [ 1, 2, 3 ], sum { || t[ 1, 2, 3 ] }.to_a
291
292
  end
293
+ assert_equal C( 3, 5, 8 ), S[ C( 1, 2, 3 ), C( 2, 3, 5 ) ].sum
292
294
  assert_equal C( 3, 5, 8 ), sum { |i| S[ C( 1, 2, 3 ), C( 2, 3, 5 ) ][i] }
295
+ assert_equal X( 4, 6 ), S[ X( 1, 2 ), X( 3, 4 ) ].sum
293
296
  assert_equal X( 4, 6 ), sum { |i| S[ X( 1, 2 ), X( 3, 4 ) ][i] }
297
+ assert_equal 384, S[ 128, 128, 128 ].sum
298
+ assert_equal 384, sum { |i| S[ 128, 128, 128 ][i] }
294
299
  end
295
300
 
296
301
  def test_min
@@ -375,6 +380,12 @@ class TC_Sequence < Test::Unit::TestCase
375
380
  # end
376
381
  #end
377
382
 
383
+ def test_flip
384
+ [ O, I ].each do |t|
385
+ assert_equal S( t, 3 )[ 3, 2, 1 ], S( t, 3 )[ 1, 2, 3 ].flip( 0 )
386
+ end
387
+ end
388
+
378
389
  def test_zero
379
390
  [ S( O, 3 ), S( I, 3 ) ].each do |t|
380
391
  assert_equal [ false, true, false ], t[ -1, 0, 1 ].zero?.to_a
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 14
8
- - 1
9
- version: 0.14.1
8
+ - 2
9
+ version: 0.14.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Jan Wedekind
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-12-15 00:00:00 +00:00
17
+ date: 2010-12-17 00:00:00 +00:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency