silicium 0.0.14 → 0.0.22
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.codeclimate.yml +4 -0
- data/.gitignore +13 -11
- data/.rakeTasks +8 -0
- data/.travis.yml +17 -3
- data/CODE_OF_CONDUCT.md +74 -74
- data/Gemfile +8 -4
- data/LICENSE.txt +21 -21
- data/Makefile +269 -0
- data/README.md +588 -44
- data/Rakefile +17 -10
- data/bin/console +14 -14
- data/bin/setup +8 -8
- data/docs/Object.html +117 -0
- data/docs/README_md.html +142 -0
- data/docs/Silicium/Combinatorics.html +270 -0
- data/docs/Silicium/Dice/Polyhedron.html +315 -0
- data/docs/Silicium/Dice/PolyhedronSet.html +321 -0
- data/docs/Silicium/Dice.html +99 -0
- data/docs/Silicium/Error.html +106 -0
- data/docs/Silicium/Geometry/Line2dCanon.html +243 -0
- data/docs/Silicium/Geometry/VariablesOrderException.html +106 -0
- data/docs/Silicium/Geometry.html +940 -0
- data/docs/Silicium/GraphVisualizer.html +226 -0
- data/docs/Silicium/Graphs/GraphError.html +106 -0
- data/docs/Silicium/Graphs/OrientedGraph.html +901 -0
- data/docs/Silicium/Graphs/UnorientedGraph.html +237 -0
- data/docs/Silicium/Graphs.html +374 -0
- data/docs/Silicium/IntegralDoesntExistError.html +106 -0
- data/docs/Silicium/NumericalIntegration.html +521 -0
- data/docs/Silicium/Optimization.html +629 -0
- data/docs/Silicium/Plotter/Image.html +297 -0
- data/docs/Silicium/Plotter.html +186 -0
- data/docs/Silicium.html +101 -0
- data/docs/created.rid +9 -0
- data/docs/css/fonts.css +167 -0
- data/docs/css/rdoc.css +619 -0
- data/docs/fonts/Lato-Light.ttf +0 -0
- data/docs/fonts/Lato-LightItalic.ttf +0 -0
- data/docs/fonts/Lato-Regular.ttf +0 -0
- data/docs/fonts/Lato-RegularItalic.ttf +0 -0
- data/docs/fonts/SourceCodePro-Bold.ttf +0 -0
- data/docs/fonts/SourceCodePro-Regular.ttf +0 -0
- data/docs/images/add.png +0 -0
- data/docs/images/arrow_up.png +0 -0
- data/docs/images/brick.png +0 -0
- data/docs/images/brick_link.png +0 -0
- data/docs/images/bug.png +0 -0
- data/docs/images/bullet_black.png +0 -0
- data/docs/images/bullet_toggle_minus.png +0 -0
- data/docs/images/bullet_toggle_plus.png +0 -0
- data/docs/images/date.png +0 -0
- data/docs/images/delete.png +0 -0
- data/docs/images/find.png +0 -0
- data/docs/images/loadingAnimation.gif +0 -0
- data/docs/images/macFFBgHack.png +0 -0
- data/docs/images/package.png +0 -0
- data/docs/images/page_green.png +0 -0
- data/docs/images/page_white_text.png +0 -0
- data/docs/images/page_white_width.png +0 -0
- data/docs/images/plugin.png +0 -0
- data/docs/images/ruby.png +0 -0
- data/docs/images/tag_blue.png +0 -0
- data/docs/images/tag_green.png +0 -0
- data/docs/images/transparent.png +0 -0
- data/docs/images/wrench.png +0 -0
- data/docs/images/wrench_orange.png +0 -0
- data/docs/images/zoom.png +0 -0
- data/docs/index.html +134 -0
- data/docs/js/darkfish.js +84 -0
- data/docs/js/navigation.js +105 -0
- data/docs/js/navigation.js.gz +0 -0
- data/docs/js/search.js +110 -0
- data/docs/js/search_index.js +1 -0
- data/docs/js/search_index.js.gz +0 -0
- data/docs/js/searcher.js +229 -0
- data/docs/js/searcher.js.gz +0 -0
- data/docs/table_of_contents.html +697 -0
- 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 -0
- data/lib/geometry3d.rb +270 -0
- data/lib/graph/dfs.rb +41 -0
- data/lib/graph/kruskal.rb +36 -0
- data/lib/graph/scc.rb +97 -0
- data/lib/graph.rb +350 -0
- data/lib/graph_visualizer.rb +286 -0
- data/lib/ml_algorithms.rb +181 -0
- data/lib/numerical_integration.rb +184 -0
- data/lib/optimization.rb +208 -0
- data/lib/plotter.rb +258 -0
- 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 +29 -6
- data/lib/theory_of_probability.rb +240 -0
- data/lib/topological_sort.rb +50 -0
- data/oriented_graph.png +0 -0
- data/plot.png +0 -0
- data/silicium.gemspec +4 -3
- metadata +122 -12
@@ -0,0 +1,76 @@
|
|
1
|
+
module Silicium
|
2
|
+
require_relative 'sugar'
|
3
|
+
require_relative 'conversions'
|
4
|
+
require_relative 'trans'
|
5
|
+
require_relative 'adding'
|
6
|
+
require_relative 'multi'
|
7
|
+
|
8
|
+
# here goes the sparse module
|
9
|
+
module Sparse
|
10
|
+
# here goes the Sparse class
|
11
|
+
class SparseMatrix
|
12
|
+
attr_reader :triplets
|
13
|
+
attr_reader :n
|
14
|
+
attr_reader :m
|
15
|
+
##
|
16
|
+
# @param [Integer] rows - Count of rows
|
17
|
+
# @param [Integer] cols - Count of columns
|
18
|
+
#
|
19
|
+
# Initialize matrix with count of rows and columns
|
20
|
+
def initialize(rows, cols)
|
21
|
+
@n = rows
|
22
|
+
@m = cols
|
23
|
+
@triplets = []
|
24
|
+
end
|
25
|
+
|
26
|
+
##
|
27
|
+
# @return [SparseMatrix::Object] - Returns a copy of a SparseMatrix object
|
28
|
+
#
|
29
|
+
# Creates a copy of matrix object
|
30
|
+
def copy
|
31
|
+
new = SparseMatrix.new(@n, @m)
|
32
|
+
triplets.each do |triplet|
|
33
|
+
new.add(triplet[0], triplet[1], triplet[2])
|
34
|
+
end
|
35
|
+
new
|
36
|
+
end
|
37
|
+
|
38
|
+
##
|
39
|
+
# @param [Integer] i_pos - The i position of an element
|
40
|
+
# @param [Integer] j_pos - The j position of an element
|
41
|
+
# @param [Integer] elem - The value of an element to be added
|
42
|
+
#
|
43
|
+
# Adds an element to matrix by its position and value
|
44
|
+
def add(i_pos, j_pos, elem)
|
45
|
+
if i_pos > @n || j_pos > @m || i_pos.negative? || j_pos.negative?
|
46
|
+
raise 'out of range'
|
47
|
+
end
|
48
|
+
|
49
|
+
f = false
|
50
|
+
@triplets.each do |item|
|
51
|
+
if item[0] == i_pos && item[1] == j_pos
|
52
|
+
item = [i_pos, j_pos, elem]
|
53
|
+
f = true
|
54
|
+
break
|
55
|
+
end
|
56
|
+
end
|
57
|
+
@triplets.push([i_pos, j_pos, elem]) unless f
|
58
|
+
end
|
59
|
+
|
60
|
+
##
|
61
|
+
# @param [Integer] i_pos - The i position of an element
|
62
|
+
# @param [Integer] j_pos - The j position of an element
|
63
|
+
# @return [Integer] The element on i,j position
|
64
|
+
#
|
65
|
+
# Returns an element by its position
|
66
|
+
def get(i_pos, j_pos)
|
67
|
+
triplets.each do |triplet|
|
68
|
+
if triplet[0] == i_pos && triplet[1] == j_pos
|
69
|
+
return triplet[2]
|
70
|
+
end
|
71
|
+
end
|
72
|
+
0
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Silicium
|
2
|
+
module Sparse
|
3
|
+
# addition for SparseMatrix class
|
4
|
+
class SparseMatrix
|
5
|
+
##
|
6
|
+
# @param [SparseMatrix] other - second matrix for adding
|
7
|
+
# @raise [ArgumentError] If the size of the first matrix doesn't match the size of the second matrix
|
8
|
+
# @return [SparseMatrix] Matrix as the sum of the other two matrices
|
9
|
+
#
|
10
|
+
# Makes the sum of two matrix
|
11
|
+
def +(other)
|
12
|
+
adding(other)
|
13
|
+
end
|
14
|
+
|
15
|
+
##
|
16
|
+
# @param [SparseMatrix] other - second matrix for adding
|
17
|
+
# @raise [ArgumentError] If the size of the first matrix doesn't match the size of the second matrix
|
18
|
+
# @return [SparseMatrix] Matrix as the sum of the other two matrices
|
19
|
+
#
|
20
|
+
# Makes the sum of two matrix
|
21
|
+
def -(other)
|
22
|
+
adding(other.mult_by_num(-1))
|
23
|
+
end
|
24
|
+
|
25
|
+
##
|
26
|
+
# @param [SparseMatrix::Object] other - A matrix to multiply to
|
27
|
+
# @raise [ArgumentError] if count of columns of right matrix
|
28
|
+
# doesn't match count of rows of left matrix
|
29
|
+
#
|
30
|
+
# Returns a matrix in its regular view but multiplied by other matrix
|
31
|
+
def *(other)
|
32
|
+
multiply(other)
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -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.22"
|
3
|
+
end
|
data/lib/silicium.rb
CHANGED
@@ -1,6 +1,29 @@
|
|
1
|
-
require
|
2
|
-
|
3
|
-
module Silicium
|
4
|
-
class Error < StandardError; end
|
5
|
-
|
6
|
-
|
1
|
+
require 'silicium/version'
|
2
|
+
|
3
|
+
module Silicium
|
4
|
+
class Error < StandardError; end
|
5
|
+
|
6
|
+
require_relative 'geometry'
|
7
|
+
require_relative 'graph'
|
8
|
+
|
9
|
+
|
10
|
+
require_relative 'algebra'
|
11
|
+
require_relative 'algebra_diff'
|
12
|
+
require_relative './geometry'
|
13
|
+
require_relative 'geometry3d'
|
14
|
+
require_relative './graph'
|
15
|
+
require_relative 'graph_visualizer'
|
16
|
+
require_relative 'ml_algorithms'
|
17
|
+
require_relative 'numerical_integration'
|
18
|
+
require_relative 'optimization'
|
19
|
+
require_relative 'plotter'
|
20
|
+
require_relative 'polynomial_division'
|
21
|
+
require_relative 'polynomial_interpolation'
|
22
|
+
require_relative 'regression'
|
23
|
+
require_relative 'theory_of_probability'
|
24
|
+
require_relative 'topological_sort'
|
25
|
+
require_relative 'silicium/sparse'
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
end
|
@@ -0,0 +1,240 @@
|
|
1
|
+
require 'plotter'
|
2
|
+
require 'chunky_png'
|
3
|
+
|
4
|
+
include Silicium::Plotter
|
5
|
+
|
6
|
+
module Combinatorics
|
7
|
+
|
8
|
+
def factorial(n)
|
9
|
+
(1..n).inject(:*) || 1
|
10
|
+
end
|
11
|
+
|
12
|
+
##
|
13
|
+
#Function C(n,k)
|
14
|
+
def combination(n, k)
|
15
|
+
f = fact(n, k)
|
16
|
+
if n < k or k <= 0
|
17
|
+
-1
|
18
|
+
else
|
19
|
+
f[0] / (f[2] * f[1])
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
##
|
24
|
+
#Function A(n,k)
|
25
|
+
def arrangement(n, k)
|
26
|
+
f = fact(n, k)
|
27
|
+
if n < k or k <= 0
|
28
|
+
-1
|
29
|
+
else
|
30
|
+
f[0] / f[1]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def fact(n, k)
|
37
|
+
res = [1,1,1]
|
38
|
+
fact_n_greater_1(n, k, res) if n > 1
|
39
|
+
res
|
40
|
+
end
|
41
|
+
|
42
|
+
def fact_n_greater_1(n,k, res)
|
43
|
+
c = 1
|
44
|
+
for i in 2..n
|
45
|
+
c *= i
|
46
|
+
determining_i([i, n, k, c], res)
|
47
|
+
end
|
48
|
+
res[0] = c
|
49
|
+
end
|
50
|
+
|
51
|
+
def determining_i(arr, res)
|
52
|
+
res[1] = arr[3] if arr[0] == arr[1] - arr[2]
|
53
|
+
res[2] = arr[3] if arr[0] == arr[2]
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
module Dice
|
59
|
+
|
60
|
+
##
|
61
|
+
# Class represents a polyhedron
|
62
|
+
# csides - number or sides
|
63
|
+
# sides - array of sides(unusual for custom polyhedrons)
|
64
|
+
class Polyhedron
|
65
|
+
|
66
|
+
def csides
|
67
|
+
@csides
|
68
|
+
end
|
69
|
+
|
70
|
+
def sides
|
71
|
+
@sides
|
72
|
+
end
|
73
|
+
|
74
|
+
def to_s
|
75
|
+
sides
|
76
|
+
end
|
77
|
+
|
78
|
+
##
|
79
|
+
# initializing polyhedron's variables
|
80
|
+
# there are two ways how to create it
|
81
|
+
# 1: by number (6) - creates polyhedron with 6 sides [1,2,3,4,5,6]
|
82
|
+
# 2: by array ([1,3,5]) - creates polyhedron with 3 sides [1,3,5]
|
83
|
+
def initialize(sides)
|
84
|
+
@csides = 1
|
85
|
+
@sides = [1]
|
86
|
+
if sides.class == Integer and sides > 1
|
87
|
+
@csides = sides
|
88
|
+
(2..sides).each {|i| @sides << i}
|
89
|
+
elsif sides.class == Array and sides.size > 0
|
90
|
+
@csides = sides.size
|
91
|
+
@sides = sides.sort
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
##
|
96
|
+
# ability to throw a polyhedron
|
97
|
+
def throw
|
98
|
+
@sides[rand(0..@csides-1)]
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
##
|
103
|
+
# Class represents a PolyhedronsSet
|
104
|
+
class PolyhedronSet
|
105
|
+
|
106
|
+
def initialize(arr)
|
107
|
+
@pons = parse_pons(arr).sort_by{|item| -item.csides}
|
108
|
+
@percentage = count_chance_sum
|
109
|
+
end
|
110
|
+
|
111
|
+
# hash with chances of getting definite score
|
112
|
+
def percentage
|
113
|
+
@percentage
|
114
|
+
end
|
115
|
+
|
116
|
+
##
|
117
|
+
# returns array of polyhedrons
|
118
|
+
def to_s
|
119
|
+
@pons.map {|item| item.to_s}
|
120
|
+
end
|
121
|
+
|
122
|
+
##
|
123
|
+
# ability to throw a polyhedron set using hash of chances
|
124
|
+
def throw
|
125
|
+
sum = 0
|
126
|
+
r = rand
|
127
|
+
@percentage.each do |item|
|
128
|
+
sum += item[1]
|
129
|
+
if sum >= r
|
130
|
+
item[0]
|
131
|
+
break
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
##
|
137
|
+
# creating a graph representing chances of getting points
|
138
|
+
def make_graph_by_plotter(x = percentage.size * 10, y = percentage.size * 10)
|
139
|
+
filename = 'tmp/percentage.png'
|
140
|
+
File.delete(filename) if File.exist?(filename)
|
141
|
+
plotter = Image.new(x, y)
|
142
|
+
plotter.bar_chart(percentage, 1, color('red @ 1.0'))
|
143
|
+
plotter.export(filename)
|
144
|
+
end
|
145
|
+
|
146
|
+
private
|
147
|
+
|
148
|
+
def parse_pons(arr)
|
149
|
+
res = []
|
150
|
+
arr.each do |item|
|
151
|
+
if item.is_a?(Integer) or item.is_a?(Array)
|
152
|
+
res << Polyhedron.new(item)
|
153
|
+
elsif item.is_a?(Polyhedron)
|
154
|
+
res << item
|
155
|
+
end
|
156
|
+
end
|
157
|
+
res
|
158
|
+
end
|
159
|
+
|
160
|
+
def count_chance_sum_chances_step(arr1, arr2, arr3, h)
|
161
|
+
n = 0
|
162
|
+
m = 0
|
163
|
+
sum = 0
|
164
|
+
q = Queue.new
|
165
|
+
h1 = {}
|
166
|
+
while m < arr2.size
|
167
|
+
sum = m_0([sum, n, m], q, h, arr1)
|
168
|
+
sum -= q.pop if q.size > arr2.size || m > 0
|
169
|
+
h1[arr1[n] + arr2[m]] = sum
|
170
|
+
arr3 << (arr1[n] + arr2[m])
|
171
|
+
nmarr = n_less_arr1_size(n, arr1, m)
|
172
|
+
n, m = nmarr[0], nmarr[1]
|
173
|
+
end
|
174
|
+
h1
|
175
|
+
end
|
176
|
+
|
177
|
+
def m_0(arr, q, h, arr1)
|
178
|
+
if arr[2] == 0
|
179
|
+
a = h[arr1[arr[1]]]
|
180
|
+
q << a
|
181
|
+
arr[0] += a
|
182
|
+
end
|
183
|
+
arr[0]
|
184
|
+
end
|
185
|
+
|
186
|
+
def n_less_arr1_size(n, arr1, m)
|
187
|
+
if n < arr1.size - 1
|
188
|
+
n += 1
|
189
|
+
else
|
190
|
+
m += 1
|
191
|
+
end
|
192
|
+
[n,m]
|
193
|
+
end
|
194
|
+
|
195
|
+
def count_chance_sum
|
196
|
+
h = {}
|
197
|
+
@pons[0].sides.each { |item| h[item] = 1 }
|
198
|
+
arr3 = @pons[0].sides
|
199
|
+
for i in 1..@pons.size - 1
|
200
|
+
arr1 = arr3
|
201
|
+
arr3 = []
|
202
|
+
arr2 = @pons[i].sides
|
203
|
+
h1 = count_chance_sum_chances_step(arr1, arr2, arr3, h)
|
204
|
+
h = h1
|
205
|
+
end
|
206
|
+
res = {}
|
207
|
+
fchance = @pons.inject(1) { |mult, item| mult * item.csides }
|
208
|
+
arr3.each {|item| res[item] = Float(h[item]) / fchance}
|
209
|
+
res
|
210
|
+
end
|
211
|
+
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
module BernoulliTrials
|
216
|
+
|
217
|
+
include Combinatorics
|
218
|
+
include Math
|
219
|
+
|
220
|
+
def bernoulli_formula_and_laplace_theorem(n, k, all, suc = -1)
|
221
|
+
if suc != -1
|
222
|
+
p = suc.fdiv all
|
223
|
+
else
|
224
|
+
p = all
|
225
|
+
end
|
226
|
+
q = 1 - p
|
227
|
+
if n * p * q < 9 # Laplace theorem give satisfactory approximation for n*p*q > 9, else Bernoulli formula
|
228
|
+
combination(n, k) * (p ** k) * (q ** (n - k)) # Bernoulli formula C(n,k) * (p ^ k) * (q ^ (n-k))
|
229
|
+
else
|
230
|
+
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)
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
234
|
+
def gaussian_function(x)
|
235
|
+
if x < 0 # φ(-x) = φ(x)
|
236
|
+
x * -1
|
237
|
+
end
|
238
|
+
exp(-(x ** 2) / 2).fdiv sqrt(2 * PI) # φ(x) = exp(-(x^2/2)) / sqrt(2*π)
|
239
|
+
end
|
240
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require "set"
|
2
|
+
|
3
|
+
module Silicium
|
4
|
+
module Graphs
|
5
|
+
class Graph
|
6
|
+
attr_accessor :nodes
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@nodes = []
|
10
|
+
end
|
11
|
+
|
12
|
+
def add_edge(from, to)
|
13
|
+
from.adjacents << to
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class Node
|
18
|
+
attr_accessor :name, :adjacents
|
19
|
+
|
20
|
+
def initialize(name)
|
21
|
+
# using Set instead of an Array to avoid duplications
|
22
|
+
@adjacents = Set.new
|
23
|
+
@name = name
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_s
|
27
|
+
@name
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class TopologicalSortClass
|
32
|
+
attr_accessor :post_order
|
33
|
+
|
34
|
+
def initialize(graph)
|
35
|
+
@post_order = []
|
36
|
+
@visited = []
|
37
|
+
|
38
|
+
graph.nodes.each { |node| dfs(node) unless @visited.include?(node)}
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
def dfs(node)
|
43
|
+
@visited << node
|
44
|
+
node.adjacents.each { |adj_node| dfs(adj_node) unless @visited.include?(adj_node)}
|
45
|
+
|
46
|
+
@post_order << node
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|