Cartesian 0.2.3 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/cartesian.rb +12 -5
- data/lib/cartesian_iterator.rb +9 -3
- data/lib/extensions.rb +12 -0
- data/lib/grid_search.rb +39 -0
- data/tests/tc_cartesian.rb +26 -1
- data/tests/tc_extensions.rb +12 -0
- data/tests/tc_grid_search.rb +15 -0
- metadata +9 -6
- data/lib/URL desafio.txt +0 -1
data/lib/cartesian.rb
CHANGED
@@ -138,12 +138,19 @@ module Cartesian
|
|
138
138
|
# It also works with Range objects.
|
139
139
|
#
|
140
140
|
def **(fixnum)
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
141
|
+
if fixnum < 0
|
142
|
+
raise ArgumentError, "power must be non-negative"
|
143
|
+
elsif fixnum == 0
|
144
|
+
return []
|
145
|
+
elsif fixnum == 1
|
146
|
+
return self
|
147
|
+
else
|
148
|
+
iter = CartesianIterator.new(self, self)
|
149
|
+
(fixnum-2).times do
|
150
|
+
iter.x(self)
|
151
|
+
end
|
152
|
+
iter
|
145
153
|
end
|
146
|
-
iter
|
147
154
|
end
|
148
155
|
alias :power! :**
|
149
156
|
end
|
data/lib/cartesian_iterator.rb
CHANGED
@@ -1,4 +1,9 @@
|
|
1
|
-
|
1
|
+
require 'grid_search'
|
2
|
+
|
3
|
+
class CartesianIterator
|
4
|
+
|
5
|
+
include GridSearch
|
6
|
+
|
2
7
|
def initialize(foo, bar)
|
3
8
|
@lists = []
|
4
9
|
@tot_iter = 1
|
@@ -7,7 +12,7 @@ class CartesianIterator
|
|
7
12
|
end
|
8
13
|
|
9
14
|
def x(other)
|
10
|
-
@lists << other.to_a
|
15
|
+
@lists << other.to_a.dup
|
11
16
|
@tot_iter *= @lists[-1].size
|
12
17
|
self
|
13
18
|
end
|
@@ -41,7 +46,8 @@ class CartesianIterator
|
|
41
46
|
array = []
|
42
47
|
self.each {|*element| array << element }
|
43
48
|
array
|
44
|
-
end
|
49
|
+
end
|
50
|
+
|
45
51
|
end
|
46
52
|
|
47
53
|
module Iterable
|
data/lib/extensions.rb
ADDED
data/lib/grid_search.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
module GridSearch
|
2
|
+
|
3
|
+
# Finds the argument which maximizes the function given in the block trhu grid-search maximization.
|
4
|
+
# [-1,0,1,2].argmax {|x| x**2 } #=> 2
|
5
|
+
#
|
6
|
+
def argmax(&block)
|
7
|
+
argbest(:>, &block)
|
8
|
+
end
|
9
|
+
|
10
|
+
# Finds the argument which minimizes the function given in the block trhu grid-search minimization.
|
11
|
+
# [-1,0,1,2].argmin {|x| x**2 } #=> 0
|
12
|
+
#
|
13
|
+
def argmin(&block)
|
14
|
+
argbest(:<, &block)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def argbest(cmp)
|
20
|
+
best_arg, best_val = nil, nil
|
21
|
+
self.each do |*curr_arg|
|
22
|
+
curr_val = yield(*curr_arg)
|
23
|
+
if best_val.nil? || curr_val.send(cmp, best_val)
|
24
|
+
best_val = curr_val
|
25
|
+
best_arg = curr_arg
|
26
|
+
end
|
27
|
+
end
|
28
|
+
best_arg
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
module Enumerable
|
34
|
+
include GridSearch
|
35
|
+
end
|
36
|
+
|
37
|
+
class Array
|
38
|
+
include GridSearch
|
39
|
+
end
|
data/tests/tc_cartesian.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
require 'test/unit'
|
2
2
|
|
3
3
|
$:.unshift File.join(File.dirname(__FILE__), "..", "lib")
|
4
|
-
require 'cartesian'
|
4
|
+
require 'cartesian'
|
5
|
+
require 'extensions'
|
5
6
|
|
6
7
|
class TestCartesian < Test::Unit::TestCase
|
7
8
|
def test_arrays
|
@@ -26,5 +27,29 @@ class TestCartesian < Test::Unit::TestCase
|
|
26
27
|
expected = [[1, "a"], [1, "b"], [1, "c"], [2, "a"], [2, "b"],
|
27
28
|
[2, "c"], [3, "a"], [3, "b"], [3, "c"]]
|
28
29
|
assert(foo.x(bar).to_a == expected)
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_power
|
33
|
+
ary = [1,2,3]
|
34
|
+
assert_raise(ArgumentError) { ary**(-1) }
|
35
|
+
assert_equal [], ary**0
|
36
|
+
assert_equal ary, ary**1
|
37
|
+
expected = [[0, 0, 0], [0, 0, 1], [0, 1, 0],\
|
38
|
+
[0, 1, 1], [1, 0, 0], [1, 0, 1],\
|
39
|
+
[1, 1, 0], [1, 1, 1]]
|
40
|
+
assert_equal expected, ([0,1]**3).to_a
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_argmin
|
44
|
+
assert_equal [0,0], ((-3..3)**2).argmin {|x,y| x**2+y**2 }
|
29
45
|
end
|
46
|
+
|
47
|
+
def test_argmax
|
48
|
+
values = []
|
49
|
+
-3.step(3, 0.25) {|val| values << val }
|
50
|
+
x, y = (values**2).argmax {|x,y| x**2+y**2 }
|
51
|
+
assert x.among?([-3,3])
|
52
|
+
assert y.among?([-3,3])
|
53
|
+
end
|
54
|
+
|
30
55
|
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
$:.unshift File.join(File.dirname(__FILE__), "..", "lib")
|
4
|
+
require 'extensions'
|
5
|
+
|
6
|
+
class TestExtensions < Test::Unit::TestCase
|
7
|
+
def test_among?
|
8
|
+
assert 1.among?([1,2,3])
|
9
|
+
assert ! 7.among?([1,2,3])
|
10
|
+
assert (3.0).among?([1,2,3])
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
$:.unshift File.join(File.dirname(__FILE__), "..", "lib")
|
4
|
+
require 'grid_search'
|
5
|
+
|
6
|
+
class TestCartesian < Test::Unit::TestCase
|
7
|
+
def test_argmin
|
8
|
+
assert_equal 0, *[-1,0,1].argmin {|x| x**2 }
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_argmax
|
12
|
+
assert_equal 0, *[-2,-1,0].argmax {|x| x**3 }
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
metadata
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.9.
|
2
|
+
rubygems_version: 0.9.2
|
3
3
|
specification_version: 1
|
4
4
|
name: Cartesian
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.
|
7
|
-
date: 2007-
|
6
|
+
version: 0.3.0
|
7
|
+
date: 2007-10-29 00:00:00 -03:00
|
8
8
|
summary: The Cartesian module provide methods for the calculation of the cartesian producted between two enumberable objects. It can also be easily mixed in into any enumberable class, i.e. any class with Enumerable module mixed in.
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -29,11 +29,14 @@ post_install_message:
|
|
29
29
|
authors:
|
30
30
|
- Adriano Brito Mitre
|
31
31
|
files:
|
32
|
-
- tests/benchmark.rb
|
33
32
|
- tests/tc_cartesian.rb
|
34
|
-
-
|
33
|
+
- tests/benchmark.rb
|
34
|
+
- tests/tc_extensions.rb
|
35
|
+
- tests/tc_grid_search.rb
|
35
36
|
- lib/cartesian_iterator.rb
|
36
|
-
- lib/
|
37
|
+
- lib/cartesian.rb
|
38
|
+
- lib/grid_search.rb
|
39
|
+
- lib/extensions.rb
|
37
40
|
test_files:
|
38
41
|
- tests/tc_cartesian.rb
|
39
42
|
rdoc_options: []
|
data/lib/URL desafio.txt
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
http://www.ic.unicamp.br/~vignatti/desafio2.html
|