multiarray 0.15.3 → 0.16.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.15.3'
9
+ PKG_VERSION = '0.16.0'
10
10
  RB_FILES = FileList[ 'lib/**/*.rb' ]
11
11
  TC_FILES = FileList[ 'test/tc_*.rb' ]
12
12
  TS_FILES = FileList[ 'test/ts_*.rb' ]
@@ -418,6 +418,38 @@ class Fixnum
418
418
 
419
419
  end
420
420
 
421
+ # Generate random number
422
+ #
423
+ # Generate a random number greater or equal to zero and lower than this number.
424
+ #
425
+ # @return [Integer] A random number.
426
+ def lrand
427
+ Kernel.rand self
428
+ end
429
+
430
+ # Generate random number
431
+ #
432
+ # Generate a random number greater or equal to zero and lower than this number.
433
+ #
434
+ # @return [Float] A random number.
435
+ def drand
436
+ self * Kernel.rand
437
+ end
438
+
439
+ end
440
+
441
+ # +Float+ is extend with a few methods
442
+ class Float
443
+
444
+ # Generate random number
445
+ #
446
+ # Generate a random number greater or equal to zero and lower than this number.
447
+ #
448
+ # @return [Float] A random number.
449
+ def drand
450
+ self * Kernel.rand
451
+ end
452
+
421
453
  end
422
454
 
423
455
  # +Range+ is extended with a few methods
@@ -555,6 +587,7 @@ require 'multiarray/node'
555
587
  require 'multiarray/element'
556
588
  require 'multiarray/composite'
557
589
  require 'multiarray/store'
590
+ require 'multiarray/random'
558
591
  require 'multiarray/object'
559
592
  require 'multiarray/index'
560
593
  require 'multiarray/bool'
@@ -207,6 +207,38 @@ inline void *mallocToPtr( VALUE rbMalloc )
207
207
  return retVal;
208
208
  }
209
209
 
210
+ static unsigned long make_mask( unsigned long x )
211
+ {
212
+ x = x | x >> 1;
213
+ x = x | x >> 2;
214
+ x = x | x >> 4;
215
+ x = x | x >> 8;
216
+ x = x | x >> 16;
217
+ #if 4 < SIZEOF_LONG
218
+ x = x | x >> 32;
219
+ #endif
220
+ return x;
221
+ }
222
+
223
+ static unsigned long limited_rand( unsigned long limit )
224
+ {
225
+ int i;
226
+ unsigned long mask, val;
227
+ if ( limit < 2 ) return 0;
228
+ mask = make_mask( limit - 1 );
229
+ retry:
230
+ val = 0;
231
+ for ( i = SIZEOF_LONG / 4 - 1; 0 <= i; i-- ) {
232
+ if ( ( mask >> ( i * 32 ) ) & 0xffffffff ) {
233
+ val |= (unsigned long)rb_genrand_int32() << ( i * 32 );
234
+ val &= mask;
235
+ if ( limit <= val )
236
+ goto retry;
237
+ };
238
+ };
239
+ return val;
240
+ }
241
+
210
242
  #{@instructions}
211
243
 
212
244
  #{@wrappers}
@@ -434,6 +434,24 @@ module Hornetseye
434
434
  end
435
435
  end
436
436
 
437
+ # Generate floating point random number
438
+ #
439
+ # @return [GCCValue] C value refering to the result.
440
+ #
441
+ # @private
442
+ def drand
443
+ GCCValue.new @function, "( rb_genrand_real() * ( #{self} ) )"
444
+ end
445
+
446
+ # Generate integer random number
447
+ #
448
+ # @return [GCCValue] C value refering to the result.
449
+ #
450
+ # @private
451
+ def lrand
452
+ GCCValue.new @function, "limited_rand( #{self} )"
453
+ end
454
+
437
455
  # Generate code for selecting larger value
438
456
  #
439
457
  # @param [Object,GCCValue] other Second operand for binary operation.
@@ -0,0 +1,128 @@
1
+ # multiarray - Lazy multi-dimensional arrays for Ruby
2
+ # Copyright (C) 2010 Jan Wedekind
3
+ #
4
+ # This program is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # This program is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
+
17
+ module Hornetseye
18
+
19
+ # Class for generating random number arrays
20
+ class Random < Node
21
+
22
+ # Constructor
23
+ #
24
+ # @param [Node] dest Target array to write array to.
25
+ # @param [Node] n Upper boundary for random value.
26
+ #
27
+ # @private
28
+ def initialize( dest, n )
29
+ @dest, @n = dest, n
30
+ end
31
+
32
+ # Get unique descriptor of this object
33
+ #
34
+ # @param [Hash] hash Labels for any variables.
35
+ #
36
+ # @return [String] Descriptor of this object,
37
+ #
38
+ # @private
39
+ def descriptor( hash )
40
+ "Random(#{@dest.descriptor( hash )},#{@n.descriptor( hash )})"
41
+ end
42
+
43
+ # Get type of result of delayed operation
44
+ #
45
+ # @return [Class] Type of result.
46
+ #
47
+ # @private
48
+ def array_type
49
+ @dest.array_type
50
+ end
51
+
52
+ # Reevaluate computation
53
+ #
54
+ # @return [Node,Object] Result of computation
55
+ #
56
+ # @see #force
57
+ #
58
+ # @private
59
+ def demand
60
+ if variables.empty?
61
+ if dimension > 0
62
+ shape.last.times do |i|
63
+ dest = @dest.element INT.new( i )
64
+ Random.new( dest, @n ).demand
65
+ end
66
+ else
67
+ if @n.typecode < INT_ or ( @n.typecode < OBJECT and @n.get.is_a? Integer )
68
+ @dest.store @n.typecode.new( @n.get.lrand )
69
+ else
70
+ @dest.store @n.typecode.new( @n.get.drand )
71
+ end
72
+ end
73
+ @dest
74
+ else
75
+ super
76
+ end
77
+ end
78
+
79
+ # Substitute variables
80
+ #
81
+ # Substitute the variables with the values given in the hash.
82
+ #
83
+ # @param [Hash] hash Substitutions to apply.
84
+ #
85
+ # @return [Node] Term with substitutions applied.
86
+ #
87
+ # @private
88
+ def subst( hash )
89
+ self.class.new @dest.subst( hash ), @n.subst( hash )
90
+ end
91
+
92
+ # Get variables contained in this term
93
+ #
94
+ # @return [Set] Returns list of variables.
95
+ #
96
+ # @private
97
+ def variables
98
+ @dest.variables + @n.variables
99
+ end
100
+
101
+ # Strip of all values
102
+ #
103
+ # Split up into variables, values, and a term where all values have been
104
+ # replaced with variables.
105
+ #
106
+ # @return [Array<Array,Node>] Returns an array of variables, an array of
107
+ # values, and the term based on variables.
108
+ #
109
+ # @private
110
+ def strip
111
+ vars1, values1, term1 = @dest.strip
112
+ vars2, values2, term2 = @n.strip
113
+ return vars1 + vars2, values1 + values2, Random.new( term1, term2 )
114
+ end
115
+
116
+ # Check whether this term is compilable
117
+ #
118
+ # @return [Boolean] Returns whether this term is compilable.
119
+ #
120
+ # @private
121
+ def compilable?
122
+ @dest.compilable? and @n.compilable?
123
+ end
124
+
125
+ end
126
+
127
+ end
128
+
@@ -117,6 +117,17 @@ module Hornetseye
117
117
  end
118
118
  end
119
119
 
120
+ def random( n = 1 )
121
+ n = typecode.maxint.new n unless n.is_a? Node
122
+ retval = new
123
+ unless compilable? and dimension > 0
124
+ Random.new( retval, n ).demand
125
+ else
126
+ GCCFunction.run Random.new( retval, n )
127
+ end
128
+ retval
129
+ end
130
+
120
131
  # Construct native array from Ruby array
121
132
  #
122
133
  # @param [Array<Object>] args Array with Ruby values.
@@ -26,6 +26,7 @@ class TC_Sequence < Test::Unit::TestCase
26
26
  O = Hornetseye::OBJECT
27
27
  B = Hornetseye::BOOL
28
28
  I = Hornetseye::INT
29
+ F = Hornetseye::DFLOAT
29
30
  S = Hornetseye::Sequence
30
31
  C = Hornetseye::INTRGB
31
32
  X = Hornetseye::DCOMPLEX
@@ -76,6 +77,18 @@ class TC_Sequence < Test::Unit::TestCase
76
77
  S( C, 2 ).indgen( C( 1, 2, 3 ), C( 2, 3, 4 ) )
77
78
  end
78
79
 
80
+ def test_sequence_random
81
+ r = S( O, 100 ).random( 10 ).range
82
+ assert r.begin >= 0
83
+ assert r.end < 10
84
+ r = S( I, 100 ).random( 10 ).range
85
+ assert r.begin >= 0
86
+ assert r.end < 10
87
+ r = S( F, 100 ).random( 10.0 ).range
88
+ assert r.begin >= 0
89
+ assert r.end < 10
90
+ end
91
+
79
92
  def test_sequence_at
80
93
  assert_equal "Sequence(INT,3):\n[ 1, 2, 3 ]",
81
94
  S( I, 3 )[ 1, 2, 3 ].inspect
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: multiarray
3
3
  version: !ruby/object:Gem::Version
4
- hash: 37
5
4
  prerelease: false
6
5
  segments:
7
6
  - 0
8
- - 15
9
- - 3
10
- version: 0.15.3
7
+ - 16
8
+ - 0
9
+ version: 0.16.0
11
10
  platform: ruby
12
11
  authors:
13
12
  - Jan Wedekind
@@ -26,7 +25,6 @@ dependencies:
26
25
  requirements:
27
26
  - - ~>
28
27
  - !ruby/object:Gem::Version
29
- hash: 13
30
28
  segments:
31
29
  - 1
32
30
  - 1
@@ -41,7 +39,6 @@ dependencies:
41
39
  requirements:
42
40
  - - ">="
43
41
  - !ruby/object:Gem::Version
44
- hash: 3
45
42
  segments:
46
43
  - 0
47
44
  version: "0"
@@ -92,6 +89,7 @@ files:
92
89
  - lib/multiarray/lookup.rb
93
90
  - lib/multiarray/gccvalue.rb
94
91
  - lib/multiarray/complex.rb
92
+ - lib/multiarray/random.rb
95
93
  - lib/multiarray/lut.rb
96
94
  - lib/multiarray.rb
97
95
  - test/ts_multiarray.rb
@@ -117,7 +115,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
117
115
  requirements:
118
116
  - - ">="
119
117
  - !ruby/object:Gem::Version
120
- hash: 3
121
118
  segments:
122
119
  - 0
123
120
  version: "0"
@@ -126,7 +123,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
126
123
  requirements:
127
124
  - - ">="
128
125
  - !ruby/object:Gem::Version
129
- hash: 3
130
126
  segments:
131
127
  - 0
132
128
  version: "0"