more_math 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,4 +1,5 @@
1
1
  .*.sw[pon]
2
+ .AppleDouble
2
3
  .rvmrc
3
4
  Gemfile.lock
4
5
  coverage
data/.travis.yml CHANGED
@@ -4,5 +4,10 @@ rvm:
4
4
  - 1.9.3
5
5
  - ruby-head
6
6
  - ree
7
- - rbx
8
- - jruby
7
+ - rbx-18mode
8
+ - rbx-19mode
9
+ - jruby-18mode
10
+ - jruby-19mode
11
+ matrix:
12
+ allow_failures:
13
+ - rvm: ruby-head
data/CHANGES CHANGED
@@ -1,3 +1,6 @@
1
+ 2012-10-31 (0.1.0)
2
+ * Included the Permutation features identity, power, and from_mapping
3
+ contributed by Pramukta Kumar <prak@mac.com>
1
4
  2011-12-25 (0.0.4)
2
5
  * Support simplecov
3
6
  * Add more tests
data/Rakefile CHANGED
@@ -10,11 +10,12 @@ GemHadar do
10
10
  summary 'Library that provides more mathematics.'
11
11
  description 'Library that provides more mathematical functions/algorithms than standard Ruby.'
12
12
  test_dir 'tests'
13
- ignore '.*.sw[pon]', 'pkg', 'Gemfile.lock', 'coverage', '.rvmrc'
13
+ ignore '.*.sw[pon]', 'pkg', 'Gemfile.lock', 'coverage', '.rvmrc', '.AppleDouble'
14
14
  readme 'README.rdoc'
15
15
  title "#{name.camelize} -- More Math in Ruby"
16
16
 
17
17
  dependency 'tins', '~>0.3'
18
+ development_dependency 'utils'
18
19
 
19
20
  install_library do
20
21
  libdir = CONFIG["sitelibdir"]
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.4
1
+ 0.1.0
@@ -18,6 +18,7 @@ module MoreMath
18
18
  # in the range of <code>0</code> and <code>indices.size - 1</code>. This is
19
19
  # for example the result of a call to the Permutation#value method.
20
20
  def self.from_value(indices)
21
+ indices = indices.clone
21
22
  obj = new(indices.size)
22
23
  obj.instance_eval do
23
24
  self.rank = rank_indices(indices)
@@ -50,6 +51,53 @@ module MoreMath
50
51
  perm
51
52
  end
52
53
 
54
+ # Shortcut to generate the identity permutation that has
55
+ # Permutation#size <code>n</code>.
56
+ def self.identity(n)
57
+ new(n)
58
+ end
59
+
60
+ # Returns the identity permutation that has the same Permutation#size as this
61
+ # instance.
62
+ def identity
63
+ self.class.new(size)
64
+ end
65
+
66
+ # Builds a permutation that maps <code>a</code> into <code>b</code>.
67
+ # Both arguments must be the same length and must contain the same
68
+ # elements. If these arrays contain duplicate elements, the solution
69
+ # will not be unique.
70
+ def self.for_mapping(a, b)
71
+ a.size == b.size or
72
+ raise ArgumentError, "Initial and final lists must be the same length"
73
+
74
+ lookup = Hash.new { |h, k| h[k] = [] }
75
+ a.size.times { |i| lookup[a[i]] <<= i }
76
+
77
+ value = Array.new(b.size) do |i|
78
+ e = b[i]
79
+ lookup[e].pop or raise ArgumentError, "no corresponding element for #{e.inspect}"
80
+ end
81
+ Permutation.from_value value
82
+ end
83
+
84
+ # Computes the nth power (ie the nth repeated permutation) of this instance.
85
+ # Negative powers are taken to be powers of the inverse.
86
+ def power(n)
87
+ if n.respond_to?(:to_int)
88
+ n = n.to_int
89
+ else
90
+ raise TypeError, "#{n.inspect} cannot be converted to an integer"
91
+ end
92
+ if n >= 0
93
+ (1..n).inject(identity) { |p, e| p * self }
94
+ else # negative powers are taken to be powers of the inverse
95
+ -power(-n)
96
+ end
97
+ end
98
+
99
+ alias ** power
100
+
53
101
  # Assigns <code>m</code> to the rank attribute of this Permutation
54
102
  # instance. That implies that the indices produced by a call to the
55
103
  # Permutation#value method of this instance is the permutation ranked with
@@ -149,7 +197,7 @@ module MoreMath
149
197
  def compose(other)
150
198
  size == other.size or raise ArgumentError,
151
199
  "permutations of unequal sizes cannot be composed!"
152
- indices = self.value
200
+ indices = value
153
201
  composed = other.value.map { |i| indices[i] }
154
202
  klon = clone
155
203
  klon.rank = rank_indices(composed)
@@ -227,6 +275,8 @@ module MoreMath
227
275
  @@fcache[n]
228
276
  end
229
277
 
278
+ # Rank the indices of the permutation +p+. Beware that this method may
279
+ # change its argument +p+.
230
280
  def rank_indices(p)
231
281
  result = 0
232
282
  for i in 0...size
@@ -238,6 +288,8 @@ module MoreMath
238
288
  result
239
289
  end
240
290
 
291
+ # Unrank the rank +m+, that is create a permutation of the appropriate size
292
+ # and rank as an array of indices and return it.
241
293
  def unrank_indices(m)
242
294
  result = Array.new(size, 0)
243
295
  for i in 0...size
@@ -1,6 +1,6 @@
1
1
  module MoreMath
2
2
  # MoreMath version
3
- VERSION = '0.0.4'
3
+ VERSION = '0.1.0'
4
4
  VERSION_ARRAY = VERSION.split(/\./).map { |x| x.to_i } # :nodoc:
5
5
  VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
6
6
  VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
data/more_math.gemspec CHANGED
@@ -2,34 +2,37 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "more_math"
5
- s.version = "0.0.4"
5
+ s.version = "0.1.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Florian Frank"]
9
- s.date = "2011-12-25"
9
+ s.date = "2012-11-01"
10
10
  s.description = "Library that provides more mathematical functions/algorithms than standard Ruby."
11
11
  s.email = "flori@ping.de"
12
- s.extra_rdoc_files = ["README.rdoc", "lib/more_math/cantor_pairing_function.rb", "lib/more_math/version.rb", "lib/more_math/functions.rb", "lib/more_math/sequence.rb", "lib/more_math/linear_regression.rb", "lib/more_math/string_numeral.rb", "lib/more_math/ranking_common.rb", "lib/more_math/subset.rb", "lib/more_math/histogram.rb", "lib/more_math/constants/functions_constants.rb", "lib/more_math/numberify_string_function.rb", "lib/more_math/distributions.rb", "lib/more_math/exceptions.rb", "lib/more_math/newton_bisection.rb", "lib/more_math/permutation.rb", "lib/more_math/continued_fraction.rb", "lib/more_math.rb"]
12
+ s.extra_rdoc_files = ["README.rdoc", "lib/more_math/cantor_pairing_function.rb", "lib/more_math/constants/functions_constants.rb", "lib/more_math/continued_fraction.rb", "lib/more_math/distributions.rb", "lib/more_math/exceptions.rb", "lib/more_math/functions.rb", "lib/more_math/histogram.rb", "lib/more_math/linear_regression.rb", "lib/more_math/newton_bisection.rb", "lib/more_math/numberify_string_function.rb", "lib/more_math/permutation.rb", "lib/more_math/ranking_common.rb", "lib/more_math/sequence.rb", "lib/more_math/string_numeral.rb", "lib/more_math/subset.rb", "lib/more_math/version.rb", "lib/more_math.rb"]
13
13
  s.files = [".gitignore", ".travis.yml", "CHANGES", "Gemfile", "LICENSE", "README.rdoc", "Rakefile", "VERSION", "lib/more_math.rb", "lib/more_math/cantor_pairing_function.rb", "lib/more_math/constants/functions_constants.rb", "lib/more_math/continued_fraction.rb", "lib/more_math/distributions.rb", "lib/more_math/exceptions.rb", "lib/more_math/functions.rb", "lib/more_math/histogram.rb", "lib/more_math/linear_regression.rb", "lib/more_math/newton_bisection.rb", "lib/more_math/numberify_string_function.rb", "lib/more_math/permutation.rb", "lib/more_math/ranking_common.rb", "lib/more_math/sequence.rb", "lib/more_math/string_numeral.rb", "lib/more_math/subset.rb", "lib/more_math/version.rb", "more_math.gemspec", "tests/cantor_pairing_function_test.rb", "tests/continued_fraction_test.rb", "tests/distribution_test.rb", "tests/functions_test.rb", "tests/histogram_test.rb", "tests/newton_bisection_test.rb", "tests/numberify_string_function_test.rb", "tests/permutation_test.rb", "tests/sequence_test.rb", "tests/string_numeral_test.rb", "tests/subset_test.rb", "tests/test_helper.rb"]
14
14
  s.homepage = "http://flori.github.com/more_math"
15
15
  s.rdoc_options = ["--title", "MoreMath -- More Math in Ruby", "--main", "README.rdoc"]
16
16
  s.require_paths = ["lib"]
17
- s.rubygems_version = "1.8.13"
17
+ s.rubygems_version = "1.8.24"
18
18
  s.summary = "Library that provides more mathematics."
19
- s.test_files = ["tests/subset_test.rb", "tests/sequence_test.rb", "tests/histogram_test.rb", "tests/string_numeral_test.rb", "tests/cantor_pairing_function_test.rb", "tests/distribution_test.rb", "tests/permutation_test.rb", "tests/continued_fraction_test.rb", "tests/numberify_string_function_test.rb", "tests/functions_test.rb", "tests/test_helper.rb", "tests/newton_bisection_test.rb"]
19
+ s.test_files = ["tests/cantor_pairing_function_test.rb", "tests/continued_fraction_test.rb", "tests/distribution_test.rb", "tests/functions_test.rb", "tests/histogram_test.rb", "tests/newton_bisection_test.rb", "tests/numberify_string_function_test.rb", "tests/permutation_test.rb", "tests/sequence_test.rb", "tests/string_numeral_test.rb", "tests/subset_test.rb", "tests/test_helper.rb"]
20
20
 
21
21
  if s.respond_to? :specification_version then
22
22
  s.specification_version = 3
23
23
 
24
24
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
25
- s.add_development_dependency(%q<gem_hadar>, ["~> 0.1.4"])
25
+ s.add_development_dependency(%q<gem_hadar>, ["~> 0.1.8"])
26
+ s.add_development_dependency(%q<utils>, [">= 0"])
26
27
  s.add_runtime_dependency(%q<tins>, ["~> 0.3"])
27
28
  else
28
- s.add_dependency(%q<gem_hadar>, ["~> 0.1.4"])
29
+ s.add_dependency(%q<gem_hadar>, ["~> 0.1.8"])
30
+ s.add_dependency(%q<utils>, [">= 0"])
29
31
  s.add_dependency(%q<tins>, ["~> 0.3"])
30
32
  end
31
33
  else
32
- s.add_dependency(%q<gem_hadar>, ["~> 0.1.4"])
34
+ s.add_dependency(%q<gem_hadar>, ["~> 0.1.8"])
35
+ s.add_dependency(%q<utils>, [">= 0"])
33
36
  s.add_dependency(%q<tins>, ["~> 0.3"])
34
37
  end
35
38
  end
@@ -194,8 +194,7 @@ class PermutationTest < Test::Unit::TestCase
194
194
  def test_project
195
195
  too_big = Array.new(10)
196
196
  @perms.each_with_index do |perms, i|
197
- assert_equal(@projected[i],
198
- perms.map { |p| p.project(@projected[i][0]) })
197
+ assert_equal(@projected[i], perms.map { |p| p.project(@projected[i][0]) })
199
198
  assert_raises(ArgumentError) { perms.project }
200
199
  assert_raises(ArgumentError) { perms.project(too_big) }
201
200
  end
@@ -251,4 +250,62 @@ class PermutationTest < Test::Unit::TestCase
251
250
  perm.map { |p| p.odd? })
252
251
  end
253
252
  end
253
+
254
+ def test_identity
255
+ a = ("A".."Z").to_a
256
+ b = a.shuffle
257
+ p = Permutation.for_mapping(a, b)
258
+ assert_equal(Permutation.identity(p.size), Permutation.from_value((0..25).to_a))
259
+ assert_equal(Permutation.identity(p.size), p.identity)
260
+ assert_equal(p * -p, p.identity)
261
+ end
262
+
263
+ def test_for_mapping_basic
264
+ a = (0..99).to_a
265
+ b = a.shuffle
266
+ ref = Permutation.from_value(b)
267
+ assert_equal(Permutation.for_mapping(a, b), ref)
268
+ end
269
+
270
+
271
+ def test_for_mapping_random
272
+ a = ("A".."Z").to_a
273
+ b = a.shuffle
274
+ p = Permutation.for_mapping(a, b)
275
+ assert_equal(p.project(a), b)
276
+ 3.times { a.push("Z") }
277
+ b = a.shuffle
278
+ p = Permutation.for_mapping(a, b)
279
+ assert_equal(p.project(a), b)
280
+ end
281
+
282
+ def test_for_mappings_random_objects
283
+ a = [Array.new, Hash.new, nil, 0]
284
+ b = [Hash.new, nil, Array.new, 0]
285
+ p = Permutation.for_mapping(a, b)
286
+ assert_equal(p.value, [1, 2, 0, 3])
287
+ end
288
+
289
+ def test_mapping_for_non_arrays
290
+ a = "abcdef"
291
+ b = a.split('').shuffle * ''
292
+ p = Permutation.for_mapping(a, b)
293
+ assert_equal(p.project(a), b)
294
+ end
295
+
296
+ def test_power
297
+ 4.times do |i|
298
+ perms = Permutation.new(i)
299
+ perms.each do |perm|
300
+ assert_equal(perm * perm, perm ** 2)
301
+ assert_equal(perm * perm * perm, perm ** 3)
302
+ assert_equal(perm * perm * perm * perm, perm ** 4)
303
+ assert_equal(perm, perm ** 1)
304
+ assert_equal(Permutation.identity(i), perm ** 0)
305
+ assert_equal(-perm, perm ** -1)
306
+ assert_equal(-perm * -perm, perm ** -2)
307
+ end
308
+ end
309
+ assert_raise(TypeError) { Permutation.new(4).random ** 'nix' }
310
+ end
254
311
  end
metadata CHANGED
@@ -1,79 +1,89 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: more_math
3
- version: !ruby/object:Gem::Version
4
- hash: 23
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 0
9
- - 4
10
- version: 0.0.4
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Florian Frank
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-12-25 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2012-11-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: gem_hadar
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.1.8
22
+ type: :development
22
23
  prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
24
25
  none: false
25
- requirements:
26
+ requirements:
26
27
  - - ~>
27
- - !ruby/object:Gem::Version
28
- hash: 19
29
- segments:
30
- - 0
31
- - 1
32
- - 4
33
- version: 0.1.4
28
+ - !ruby/object:Gem::Version
29
+ version: 0.1.8
30
+ - !ruby/object:Gem::Dependency
31
+ name: utils
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
34
38
  type: :development
35
- version_requirements: *id001
36
- - !ruby/object:Gem::Dependency
37
- name: tins
38
39
  prerelease: false
39
- requirement: &id002 !ruby/object:Gem::Requirement
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: tins
48
+ requirement: !ruby/object:Gem::Requirement
40
49
  none: false
41
- requirements:
50
+ requirements:
42
51
  - - ~>
43
- - !ruby/object:Gem::Version
44
- hash: 13
45
- segments:
46
- - 0
47
- - 3
48
- version: "0.3"
52
+ - !ruby/object:Gem::Version
53
+ version: '0.3'
49
54
  type: :runtime
50
- version_requirements: *id002
51
- description: Library that provides more mathematical functions/algorithms than standard Ruby.
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '0.3'
62
+ description: Library that provides more mathematical functions/algorithms than standard
63
+ Ruby.
52
64
  email: flori@ping.de
53
65
  executables: []
54
-
55
66
  extensions: []
56
-
57
- extra_rdoc_files:
67
+ extra_rdoc_files:
58
68
  - README.rdoc
59
69
  - lib/more_math/cantor_pairing_function.rb
60
- - lib/more_math/version.rb
61
- - lib/more_math/functions.rb
62
- - lib/more_math/sequence.rb
63
- - lib/more_math/linear_regression.rb
64
- - lib/more_math/string_numeral.rb
65
- - lib/more_math/ranking_common.rb
66
- - lib/more_math/subset.rb
67
- - lib/more_math/histogram.rb
68
70
  - lib/more_math/constants/functions_constants.rb
69
- - lib/more_math/numberify_string_function.rb
71
+ - lib/more_math/continued_fraction.rb
70
72
  - lib/more_math/distributions.rb
71
73
  - lib/more_math/exceptions.rb
74
+ - lib/more_math/functions.rb
75
+ - lib/more_math/histogram.rb
76
+ - lib/more_math/linear_regression.rb
72
77
  - lib/more_math/newton_bisection.rb
78
+ - lib/more_math/numberify_string_function.rb
73
79
  - lib/more_math/permutation.rb
74
- - lib/more_math/continued_fraction.rb
80
+ - lib/more_math/ranking_common.rb
81
+ - lib/more_math/sequence.rb
82
+ - lib/more_math/string_numeral.rb
83
+ - lib/more_math/subset.rb
84
+ - lib/more_math/version.rb
75
85
  - lib/more_math.rb
76
- files:
86
+ files:
77
87
  - .gitignore
78
88
  - .travis.yml
79
89
  - CHANGES
@@ -114,50 +124,45 @@ files:
114
124
  - tests/test_helper.rb
115
125
  homepage: http://flori.github.com/more_math
116
126
  licenses: []
117
-
118
127
  post_install_message:
119
- rdoc_options:
128
+ rdoc_options:
120
129
  - --title
121
130
  - MoreMath -- More Math in Ruby
122
131
  - --main
123
132
  - README.rdoc
124
- require_paths:
133
+ require_paths:
125
134
  - lib
126
- required_ruby_version: !ruby/object:Gem::Requirement
135
+ required_ruby_version: !ruby/object:Gem::Requirement
127
136
  none: false
128
- requirements:
129
- - - ">="
130
- - !ruby/object:Gem::Version
131
- hash: 3
132
- segments:
137
+ requirements:
138
+ - - ! '>='
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ segments:
133
142
  - 0
134
- version: "0"
135
- required_rubygems_version: !ruby/object:Gem::Requirement
143
+ hash: 654456015096180665
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
145
  none: false
137
- requirements:
138
- - - ">="
139
- - !ruby/object:Gem::Version
140
- hash: 3
141
- segments:
142
- - 0
143
- version: "0"
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
144
150
  requirements: []
145
-
146
151
  rubyforge_project:
147
- rubygems_version: 1.8.13
152
+ rubygems_version: 1.8.24
148
153
  signing_key:
149
154
  specification_version: 3
150
155
  summary: Library that provides more mathematics.
151
- test_files:
152
- - tests/subset_test.rb
153
- - tests/sequence_test.rb
154
- - tests/histogram_test.rb
155
- - tests/string_numeral_test.rb
156
+ test_files:
156
157
  - tests/cantor_pairing_function_test.rb
157
- - tests/distribution_test.rb
158
- - tests/permutation_test.rb
159
158
  - tests/continued_fraction_test.rb
160
- - tests/numberify_string_function_test.rb
159
+ - tests/distribution_test.rb
161
160
  - tests/functions_test.rb
162
- - tests/test_helper.rb
161
+ - tests/histogram_test.rb
163
162
  - tests/newton_bisection_test.rb
163
+ - tests/numberify_string_function_test.rb
164
+ - tests/permutation_test.rb
165
+ - tests/sequence_test.rb
166
+ - tests/string_numeral_test.rb
167
+ - tests/subset_test.rb
168
+ - tests/test_helper.rb