silicium 0.0.20 → 0.0.21
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.
- checksums.yaml +4 -4
- data/.codeclimate.yml +3 -3
- data/.gitignore +13 -13
- data/.rakeTasks +8 -0
- data/.travis.yml +28 -25
- data/CODE_OF_CONDUCT.md +74 -74
- data/Gemfile +8 -8
- data/LICENSE.txt +21 -21
- data/Makefile +269 -269
- data/README.md +588 -46
- data/Rakefile +16 -16
- data/bin/console +14 -14
- data/bin/setup +8 -8
- data/docs/Object.html +117 -117
- data/docs/README_md.html +142 -142
- data/docs/Silicium/Combinatorics.html +270 -270
- data/docs/Silicium/Dice/Polyhedron.html +315 -315
- data/docs/Silicium/Dice/PolyhedronSet.html +321 -321
- data/docs/Silicium/Dice.html +99 -99
- data/docs/Silicium/Error.html +106 -106
- data/docs/Silicium/Geometry/Line2dCanon.html +243 -243
- data/docs/Silicium/Geometry/VariablesOrderException.html +106 -106
- data/docs/Silicium/Geometry.html +940 -940
- data/docs/Silicium/GraphVisualizer.html +226 -0
- data/docs/Silicium/Graphs/GraphError.html +106 -106
- data/docs/Silicium/Graphs/OrientedGraph.html +901 -775
- data/docs/Silicium/Graphs/UnorientedGraph.html +237 -284
- data/docs/Silicium/Graphs.html +374 -164
- data/docs/Silicium/IntegralDoesntExistError.html +106 -106
- data/docs/Silicium/NumericalIntegration.html +521 -521
- data/docs/Silicium/Optimization.html +629 -639
- data/docs/Silicium/Plotter/Image.html +297 -297
- data/docs/Silicium/Plotter.html +186 -186
- data/docs/Silicium.html +101 -101
- data/docs/created.rid +9 -9
- data/docs/css/fonts.css +167 -167
- data/docs/css/rdoc.css +619 -619
- data/docs/index.html +134 -132
- data/docs/js/darkfish.js +84 -84
- data/docs/js/navigation.js +105 -105
- data/docs/js/search.js +110 -110
- data/docs/js/search_index.js +1 -1
- data/docs/js/search_index.js.gz +0 -0
- data/docs/js/searcher.js +229 -229
- data/docs/table_of_contents.html +697 -608
- data/lib/algebra.rb +452 -0
- data/lib/algebra_diff.rb +258 -0
- data/lib/geometry/figure.rb +62 -0
- data/lib/geometry.rb +290 -236
- data/lib/geometry3d.rb +270 -0
- data/lib/graph/dfs.rb +42 -0
- data/lib/graph/kruskal.rb +36 -0
- data/lib/graph/scc.rb +97 -0
- data/lib/graph.rb +350 -164
- data/lib/graph_visualizer.rb +287 -0
- data/lib/ml_algorithms.rb +181 -0
- data/lib/numerical_integration.rb +184 -147
- data/lib/optimization.rb +209 -144
- data/lib/plotter.rb +256 -96
- data/lib/polynomial_division.rb +132 -0
- data/lib/polynomial_interpolation.rb +94 -0
- data/lib/regression.rb +120 -0
- data/lib/silicium/adding.rb +37 -0
- data/lib/silicium/conversions.rb +23 -0
- data/lib/silicium/multi.rb +82 -0
- data/lib/silicium/sparse.rb +76 -0
- data/lib/silicium/sugar.rb +37 -0
- data/lib/silicium/trans.rb +26 -0
- data/lib/silicium/version.rb +3 -3
- data/lib/silicium.rb +5 -5
- data/lib/theory_of_probability.rb +240 -226
- data/lib/topological_sort.rb +50 -0
- data/oriented_graph.png +0 -0
- data/plot.png +0 -0
- data/silicium.gemspec +38 -39
- metadata +38 -16
@@ -0,0 +1,26 @@
|
|
1
|
+
module Silicium
|
2
|
+
module Sparse
|
3
|
+
# here goes tha addition to SparseMatrix class
|
4
|
+
class SparseMatrix
|
5
|
+
##
|
6
|
+
# Returns a transposed copy of matrix
|
7
|
+
def transpose
|
8
|
+
new = copy
|
9
|
+
new.triplets.each do |triplet|
|
10
|
+
triplet[0], triplet[1] = triplet[1], triplet[0]
|
11
|
+
end
|
12
|
+
new
|
13
|
+
end
|
14
|
+
|
15
|
+
##
|
16
|
+
# Transposes matrix
|
17
|
+
def transpose!
|
18
|
+
triplets.each do |triplet|
|
19
|
+
triplet[0], triplet[1] = triplet[1], triplet[0]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
|
data/lib/silicium/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
module Silicium
|
2
|
-
VERSION = "0.0.
|
3
|
-
end
|
1
|
+
module Silicium
|
2
|
+
VERSION = "0.0.21"
|
3
|
+
end
|
data/lib/silicium.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
require "silicium/version"
|
2
|
-
|
3
|
-
module Silicium
|
4
|
-
class Error < StandardError; end
|
5
|
-
end
|
1
|
+
require "silicium/version"
|
2
|
+
|
3
|
+
module Silicium
|
4
|
+
class Error < StandardError; end
|
5
|
+
end
|
@@ -1,227 +1,241 @@
|
|
1
|
-
require 'silicium'
|
2
|
-
require 'plotter'
|
3
|
-
require 'chunky_png'
|
4
|
-
|
5
|
-
include Silicium::Plotter
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
m =
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
[
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
1
|
+
require 'silicium'
|
2
|
+
require 'plotter'
|
3
|
+
require 'chunky_png'
|
4
|
+
|
5
|
+
include Silicium::Plotter
|
6
|
+
|
7
|
+
module Combinatorics
|
8
|
+
|
9
|
+
def factorial(n)
|
10
|
+
(1..n).inject(:*) || 1
|
11
|
+
end
|
12
|
+
|
13
|
+
##
|
14
|
+
#Function C(n,k)
|
15
|
+
def combination(n, k)
|
16
|
+
f = fact(n, k)
|
17
|
+
if n < k or k <= 0
|
18
|
+
-1
|
19
|
+
else
|
20
|
+
f[0] / (f[2] * f[1])
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
##
|
25
|
+
#Function A(n,k)
|
26
|
+
def arrangement(n, k)
|
27
|
+
f = fact(n, k)
|
28
|
+
if n < k or k <= 0
|
29
|
+
-1
|
30
|
+
else
|
31
|
+
f[0] / f[1]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def fact(n, k)
|
38
|
+
res = [1,1,1]
|
39
|
+
fact_n_greater_1(n, k, res) if n > 1
|
40
|
+
res
|
41
|
+
end
|
42
|
+
|
43
|
+
def fact_n_greater_1(n,k, res)
|
44
|
+
c = 1
|
45
|
+
for i in 2..n
|
46
|
+
c *= i
|
47
|
+
determining_i([i, n, k, c], res)
|
48
|
+
end
|
49
|
+
res[0] = c
|
50
|
+
end
|
51
|
+
|
52
|
+
def determining_i(arr, res)
|
53
|
+
res[1] = arr[3] if arr[0] == arr[1] - arr[2]
|
54
|
+
res[2] = arr[3] if arr[0] == arr[2]
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
module Dice
|
60
|
+
|
61
|
+
##
|
62
|
+
# Class represents a polyhedron
|
63
|
+
# csides - number or sides
|
64
|
+
# sides - array of sides(unusual for custom polyhedrons)
|
65
|
+
class Polyhedron
|
66
|
+
|
67
|
+
def csides
|
68
|
+
@csides
|
69
|
+
end
|
70
|
+
|
71
|
+
def sides
|
72
|
+
@sides
|
73
|
+
end
|
74
|
+
|
75
|
+
def to_s
|
76
|
+
sides
|
77
|
+
end
|
78
|
+
|
79
|
+
##
|
80
|
+
# initializing polyhedron's variables
|
81
|
+
# there are two ways how to create it
|
82
|
+
# 1: by number (6) - creates polyhedron with 6 sides [1,2,3,4,5,6]
|
83
|
+
# 2: by array ([1,3,5]) - creates polyhedron with 3 sides [1,3,5]
|
84
|
+
def initialize(sides)
|
85
|
+
@csides = 1
|
86
|
+
@sides = [1]
|
87
|
+
if sides.class == Integer and sides > 1
|
88
|
+
@csides = sides
|
89
|
+
(2..sides).each {|i| @sides << i}
|
90
|
+
elsif sides.class == Array and sides.size > 0
|
91
|
+
@csides = sides.size
|
92
|
+
@sides = sides.sort
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
##
|
97
|
+
# ability to throw a polyhedron
|
98
|
+
def throw
|
99
|
+
@sides[rand(0..@csides-1)]
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
##
|
104
|
+
# Class represents a PolyhedronsSet
|
105
|
+
class PolyhedronSet
|
106
|
+
|
107
|
+
def initialize(arr)
|
108
|
+
@pons = parse_pons(arr).sort_by{|item| -item.csides}
|
109
|
+
@percentage = count_chance_sum
|
110
|
+
end
|
111
|
+
|
112
|
+
# hash with chances of getting definite score
|
113
|
+
def percentage
|
114
|
+
@percentage
|
115
|
+
end
|
116
|
+
|
117
|
+
##
|
118
|
+
# returns array of polyhedrons
|
119
|
+
def to_s
|
120
|
+
@pons.map {|item| item.to_s}
|
121
|
+
end
|
122
|
+
|
123
|
+
##
|
124
|
+
# ability to throw a polyhedron set using hash of chances
|
125
|
+
def throw
|
126
|
+
sum = 0
|
127
|
+
r = rand
|
128
|
+
@percentage.each do |item|
|
129
|
+
sum += item[1]
|
130
|
+
if sum >= r
|
131
|
+
item[0]
|
132
|
+
break
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
##
|
138
|
+
# creating a graph representing chances of getting points
|
139
|
+
def make_graph_by_plotter(x = percentage.size * 10, y = percentage.size * 10)
|
140
|
+
filename = 'tmp/percentage.png'
|
141
|
+
File.delete(filename) if File.exist?(filename)
|
142
|
+
plotter = Image.new(x, y)
|
143
|
+
plotter.bar_chart(percentage, 1, color('red @ 1.0'))
|
144
|
+
plotter.export(filename)
|
145
|
+
end
|
146
|
+
|
147
|
+
private
|
148
|
+
|
149
|
+
def parse_pons(arr)
|
150
|
+
res = []
|
151
|
+
arr.each do |item|
|
152
|
+
if item.is_a?(Integer) or item.is_a?(Array)
|
153
|
+
res << Polyhedron.new(item)
|
154
|
+
elsif item.is_a?(Polyhedron)
|
155
|
+
res << item
|
156
|
+
end
|
157
|
+
end
|
158
|
+
res
|
159
|
+
end
|
160
|
+
|
161
|
+
def count_chance_sum_chances_step(arr1, arr2, arr3, h)
|
162
|
+
n = 0
|
163
|
+
m = 0
|
164
|
+
sum = 0
|
165
|
+
q = Queue.new
|
166
|
+
h1 = {}
|
167
|
+
while m < arr2.size
|
168
|
+
sum = m_0([sum, n, m], q, h, arr1)
|
169
|
+
sum -= q.pop if q.size > arr2.size || m > 0
|
170
|
+
h1[arr1[n] + arr2[m]] = sum
|
171
|
+
arr3 << (arr1[n] + arr2[m])
|
172
|
+
nmarr = n_less_arr1_size(n, arr1, m)
|
173
|
+
n, m = nmarr[0], nmarr[1]
|
174
|
+
end
|
175
|
+
h1
|
176
|
+
end
|
177
|
+
|
178
|
+
def m_0(arr, q, h, arr1)
|
179
|
+
if arr[2] == 0
|
180
|
+
a = h[arr1[arr[1]]]
|
181
|
+
q << a
|
182
|
+
arr[0] += a
|
183
|
+
end
|
184
|
+
arr[0]
|
185
|
+
end
|
186
|
+
|
187
|
+
def n_less_arr1_size(n, arr1, m)
|
188
|
+
if n < arr1.size - 1
|
189
|
+
n += 1
|
190
|
+
else
|
191
|
+
m += 1
|
192
|
+
end
|
193
|
+
[n,m]
|
194
|
+
end
|
195
|
+
|
196
|
+
def count_chance_sum
|
197
|
+
h = {}
|
198
|
+
@pons[0].sides.each { |item| h[item] = 1 }
|
199
|
+
arr3 = @pons[0].sides
|
200
|
+
for i in 1..@pons.size - 1
|
201
|
+
arr1 = arr3
|
202
|
+
arr3 = []
|
203
|
+
arr2 = @pons[i].sides
|
204
|
+
h1 = count_chance_sum_chances_step(arr1, arr2, arr3, h)
|
205
|
+
h = h1
|
206
|
+
end
|
207
|
+
res = {}
|
208
|
+
fchance = @pons.inject(1) { |mult, item| mult * item.csides }
|
209
|
+
arr3.each {|item| res[item] = Float(h[item]) / fchance}
|
210
|
+
res
|
211
|
+
end
|
212
|
+
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
216
|
+
module BernoulliTrials
|
217
|
+
|
218
|
+
include Combinatorics
|
219
|
+
include Math
|
220
|
+
|
221
|
+
def bernoulli_formula_and_laplace_theorem(n, k, all, suc = -1)
|
222
|
+
if suc != -1
|
223
|
+
p = suc.fdiv all
|
224
|
+
else
|
225
|
+
p = all
|
226
|
+
end
|
227
|
+
q = 1 - p
|
228
|
+
if n * p * q < 9 # Laplace theorem give satisfactory approximation for n*p*q > 9, else Bernoulli formula
|
229
|
+
combination(n, k) * (p ** k) * (q ** (n - k)) # Bernoulli formula C(n,k) * (p ^ k) * (q ^ (n-k))
|
230
|
+
else
|
231
|
+
gaussian_function((k - n * p).fdiv sqrt(n * p * q)).fdiv(sqrt(n * p * q)) # Laplace theorem φ((k - n*p)/sqrt(n*p*q)) / sqrt(n*p*q)
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
def gaussian_function(x)
|
236
|
+
if x < 0 # φ(-x) = φ(x)
|
237
|
+
x * -1
|
238
|
+
end
|
239
|
+
a = exp(-(x ** 2) / 2).fdiv sqrt(2 * PI) # φ(x) = exp(-(x^2/2)) / sqrt(2*π)
|
240
|
+
end
|
227
241
|
end
|