epitools 0.5.30 → 0.5.31
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/lib/epitools/autoloads.rb +1 -0
- data/lib/epitools/core_ext/hash.rb +10 -0
- data/lib/epitools/core_ext/misc.rb +66 -0
- data/spec/core_ext_spec.rb +26 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a5e37455ce36afd3a7cb73f380868b578559ad94
|
4
|
+
data.tar.gz: 8e76e98be9a930e15e0dedf8ae17311fc292aa6d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d26e9c55fe26a3bd6450b7a8f5be8d4dc9386e40cc859ab539043793f3ac683384de944238dfcbdfe4d9de9841279a24be4cc7921c1159663eab109329bec5d0
|
7
|
+
data.tar.gz: 18bf8b39c8458f5215a3a5abe54c79e8336923e9c7dc11c93f328ba66da9d5873de81fb1ff9985836566795408694572c5c35e622ac7e122dba64cf37d433f32
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.5.
|
1
|
+
0.5.31
|
data/lib/epitools/autoloads.rb
CHANGED
@@ -89,6 +89,16 @@ class Hash
|
|
89
89
|
new {|h,k| h[k] = [] }
|
90
90
|
end
|
91
91
|
|
92
|
+
#
|
93
|
+
# Returns a new Hash whose values default to empty sets. (Good for collecting unique things!)
|
94
|
+
#
|
95
|
+
# eg:
|
96
|
+
# Hash.of_sets[:yays] << "Yay!"
|
97
|
+
#
|
98
|
+
def self.of_sets
|
99
|
+
new {|h,k| h[k] = Set.new }
|
100
|
+
end
|
101
|
+
|
92
102
|
#
|
93
103
|
# Returns a new Hash whose values default to 0. (Good for counting things!)
|
94
104
|
#
|
@@ -244,6 +244,72 @@ module ObjectSpace
|
|
244
244
|
end
|
245
245
|
|
246
246
|
|
247
|
+
#
|
248
|
+
# Matrix extensions
|
249
|
+
#
|
250
|
+
class Matrix
|
251
|
+
|
252
|
+
#
|
253
|
+
# Create a matrix of the specified size full of random numbers.
|
254
|
+
#
|
255
|
+
def self.random(*dims)
|
256
|
+
build(*dims) { rand }
|
257
|
+
end
|
258
|
+
|
259
|
+
|
260
|
+
#
|
261
|
+
# The size of the matrix, returned as `[rows, columns]`.
|
262
|
+
#
|
263
|
+
def size
|
264
|
+
[row_size, column_size]
|
265
|
+
end
|
266
|
+
|
267
|
+
#
|
268
|
+
# Print the matrix to the STDOUT.
|
269
|
+
#
|
270
|
+
def print(header=true, separator=" ")
|
271
|
+
max_width = map {|num| num.to_s.size }.max
|
272
|
+
|
273
|
+
case first
|
274
|
+
when Integer
|
275
|
+
justify = :rjust
|
276
|
+
when Float
|
277
|
+
justify = :ljust
|
278
|
+
else
|
279
|
+
raise "Unknown matrix element type: #{first.class}"
|
280
|
+
end
|
281
|
+
|
282
|
+
# print it!
|
283
|
+
puts "#{size.join("x")} matrix:" if header
|
284
|
+
|
285
|
+
rows.each do |row|
|
286
|
+
puts " " + row.map { |n| n.to_s.send(justify, max_width) }.join(separator)
|
287
|
+
end
|
288
|
+
|
289
|
+
puts
|
290
|
+
end
|
291
|
+
|
292
|
+
alias_method :draw, :print
|
293
|
+
|
294
|
+
#
|
295
|
+
# Allow mathematical operations (*, /, +, -) with a regular number on the right side.
|
296
|
+
#
|
297
|
+
# eg: Matrix.zero(3) + 5
|
298
|
+
#
|
299
|
+
%w[* / + -].each do |op|
|
300
|
+
class_eval %{
|
301
|
+
def #{op}(other)
|
302
|
+
case other
|
303
|
+
when Fixnum, Float
|
304
|
+
map { |e| e #{op} other }
|
305
|
+
else
|
306
|
+
super(other)
|
307
|
+
end
|
308
|
+
end
|
309
|
+
}
|
310
|
+
end
|
311
|
+
|
312
|
+
end
|
247
313
|
|
248
314
|
#
|
249
315
|
# Flush standard input's buffer.
|
data/spec/core_ext_spec.rb
CHANGED
@@ -621,6 +621,32 @@ describe ObjectSpace do
|
|
621
621
|
|
622
622
|
end
|
623
623
|
|
624
|
+
describe Matrix do
|
625
|
+
|
626
|
+
it "draws a matrix" do
|
627
|
+
Matrix.identity(3).draw
|
628
|
+
Matrix.random(3).draw
|
629
|
+
(Matrix.identity(3) + 102321 ).draw
|
630
|
+
end
|
631
|
+
|
632
|
+
it "makes random matrices" do
|
633
|
+
m = Matrix.random(4)
|
634
|
+
m.size.should == [4,4]
|
635
|
+
|
636
|
+
m = Matrix.random(3,2)
|
637
|
+
m.size.should == [3,2]
|
638
|
+
end
|
639
|
+
|
640
|
+
it "does scalar things" do
|
641
|
+
m = Matrix[[1,2,3]]
|
642
|
+
(m + 1).to_a.should == [[2,3,4]]
|
643
|
+
(m - 1).to_a.should == [[0,1,2]]
|
644
|
+
(m * 2).to_a.should == [[2,4,6]]
|
645
|
+
(m / 2.0).to_a.should == [[0.5,1,1.5]]
|
646
|
+
end
|
647
|
+
|
648
|
+
end
|
649
|
+
|
624
650
|
describe "truthiness" do
|
625
651
|
|
626
652
|
it "is truthy!" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: epitools
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.31
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- epitron
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-09-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|