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
data/README.md
CHANGED
@@ -1,46 +1,588 @@
|
|
1
|
-
[](https://badge.fury.io/rb/silicium)
|
2
|
+
[](https://travis-ci.org/mmcs-ruby/silicium)
|
3
|
+
[](https://codeclimate.com/github/mmcs-ruby/silicium/maintainability)
|
4
|
+
[](https://codeclimate.com/github/mmcs-ruby/silicium/test_coverage)
|
5
|
+
|
6
|
+
# Silicium
|
7
|
+
|
8
|
+
Ruby Math Library written as exercise by MMCS students.
|
9
|
+
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'silicium'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install silicium
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
### Graphs
|
29
|
+
|
30
|
+
#### Graph initialization
|
31
|
+
To create an empty graph just initialize an object:
|
32
|
+
```ruby
|
33
|
+
g = OrientedGraph.new
|
34
|
+
g = UnorientedGraph.new
|
35
|
+
````
|
36
|
+
Of course, you can determine vertices (name them whatever you want!). To do that, write something like:
|
37
|
+
```ruby
|
38
|
+
g = OrientedGraph.new([{v: 0, i: [:one]},
|
39
|
+
{v: :one, i: [0, 'two']},
|
40
|
+
{v: 'two', i: [0, 'two']}])
|
41
|
+
```
|
42
|
+
You have to pass an `Array` of `Hashes`, each hash consists of pair of keys:
|
43
|
+
* v: vertex name;
|
44
|
+
* i: `Array` of adjacent vertices
|
45
|
+
|
46
|
+
Same goes for the case with unoriented graph (note that missing edges will be added automatically):
|
47
|
+
```ruby
|
48
|
+
g = UnorientedGraph.new([{v: 0, i: [:one]},
|
49
|
+
{v: :one, i: [0, 'two']},
|
50
|
+
{v: 'two', i: [0, 'two']}])
|
51
|
+
```
|
52
|
+
|
53
|
+
=======
|
54
|
+
#### Graph Methods:
|
55
|
+
* Add vertex to your graph:
|
56
|
+
```ruby
|
57
|
+
g.add_vertex!(Vertex)
|
58
|
+
```
|
59
|
+
* Add edge to your graph:
|
60
|
+
```ruby
|
61
|
+
g.add_edge!(vertex_from, vertex_to)
|
62
|
+
```
|
63
|
+
* Get vertices adjacted with vertex:
|
64
|
+
```ruby
|
65
|
+
g.adjacted_with(vertex)
|
66
|
+
```
|
67
|
+
* Set label for the edge:
|
68
|
+
```ruby
|
69
|
+
g.label_edge!(vertex_from, vertex_to, label)
|
70
|
+
```
|
71
|
+
* Get label for the edge:
|
72
|
+
```ruby
|
73
|
+
g.get_edge_label(vertex_from, vertex_to)
|
74
|
+
```
|
75
|
+
* Set label for the vertex:
|
76
|
+
```ruby
|
77
|
+
g.label_vertex!(vertex, label)
|
78
|
+
```
|
79
|
+
* Get label for the vertex:
|
80
|
+
```ruby
|
81
|
+
g.get_vertex_label(vertex)
|
82
|
+
```
|
83
|
+
* Get number of vertices:
|
84
|
+
```ruby
|
85
|
+
g.vertex_number
|
86
|
+
```
|
87
|
+
* Get number of edges:
|
88
|
+
```ruby
|
89
|
+
g.edge_number
|
90
|
+
```
|
91
|
+
* Get number of vertex labels:
|
92
|
+
```ruby
|
93
|
+
g.vertex_label_number
|
94
|
+
```
|
95
|
+
* Get number of vertex edges:
|
96
|
+
```ruby
|
97
|
+
g.edge_label_number
|
98
|
+
```
|
99
|
+
* Check whether graph contains vertex:
|
100
|
+
```ruby
|
101
|
+
g.has_vertex?(vertex)
|
102
|
+
```
|
103
|
+
* Check whether graph contains edge:
|
104
|
+
```ruby
|
105
|
+
g.has_edge?(vertex_from, vertex_to)
|
106
|
+
```
|
107
|
+
* Delete vertex:
|
108
|
+
```ruby
|
109
|
+
g.delete_vertex!(vertex)
|
110
|
+
```
|
111
|
+
* Delete edge:
|
112
|
+
```ruby
|
113
|
+
g.delete_edge!(vertex_from, vertex_to)
|
114
|
+
```
|
115
|
+
* Get array of vertices:
|
116
|
+
```ruby
|
117
|
+
g.vertices
|
118
|
+
```
|
119
|
+
|
120
|
+
#### Graph algorithms:
|
121
|
+
|
122
|
+
* Check whether graph is connected:
|
123
|
+
```ruby
|
124
|
+
g.connected?(graph)
|
125
|
+
```
|
126
|
+
* Breadth-First Search:
|
127
|
+
```ruby
|
128
|
+
g.breadth_first_search?(graph, starting_vertex, searching_vertex)
|
129
|
+
```
|
130
|
+
* Algorithm of Dijkstra:
|
131
|
+
```ruby
|
132
|
+
g.dijkstra_algorythm!(graph, starting_vertex)
|
133
|
+
```
|
134
|
+
* Find Strongly Connected Components:
|
135
|
+
```ruby
|
136
|
+
g.find_strongly_connected_components
|
137
|
+
```
|
138
|
+
* Algorithm of Dijkstra: dijkstra_algorythm!(graph, starting_vertex)
|
139
|
+
|
140
|
+
|
141
|
+
* Topological sort
|
142
|
+
|
143
|
+
#### Description
|
144
|
+
Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering of vertices such that for every directed edge *u v*, vertex *u* comes before *v* in the ordering.
|
145
|
+
|
146
|
+
#### How to use
|
147
|
+
For you to have a topologically sorted graph, you need to create an object of the class ```Graph```:
|
148
|
+
``` ruby
|
149
|
+
graph = Graph.new
|
150
|
+
```
|
151
|
+
Then you need to add vertices to this graph using the class ```Node```:
|
152
|
+
``` ruby
|
153
|
+
graph.nodes << (node1 = Node.new(1))
|
154
|
+
graph.nodes << (node2 = Node.new(2))
|
155
|
+
```
|
156
|
+
Due to the fact that only a directed graph can be sorted topologically, it is necessary to add an edge:
|
157
|
+
``` ruby
|
158
|
+
graph.add_edge(node1, node2)
|
159
|
+
```
|
160
|
+
And finally you can type:
|
161
|
+
``` ruby
|
162
|
+
TopologicalSortClass.new(graph)
|
163
|
+
```
|
164
|
+
|
165
|
+
#### Result
|
166
|
+
The result for ```TopologicalSortClass.new(graph).post_order.map(&:to_s)``` is [2, 1]
|
167
|
+
|
168
|
+
|
169
|
+
Algorithm of Dijkstra: dijkstra_algorythm!(graph, starting_vertex)
|
170
|
+
|
171
|
+
Algorithm of Kruskal: kruskal_mst(graph)
|
172
|
+
|
173
|
+
### GraphVisualiser
|
174
|
+
|
175
|
+
#### Set window size
|
176
|
+
|
177
|
+
```ruby
|
178
|
+
change_window_size(1000, 600)
|
179
|
+
```
|
180
|
+
|
181
|
+
#### Set graph
|
182
|
+
|
183
|
+
```ruby
|
184
|
+
graph = OrientedGraph.new([{v: :one, i: [:one, :two, :four]},
|
185
|
+
{v: :two, i:[ :one, :two]},
|
186
|
+
{v: :five, i:[ :one,:three, :four]},
|
187
|
+
{v: :four, i:[ :one, :four]},
|
188
|
+
{v: :three, i:[ :one, :two]}])
|
189
|
+
set_graph(graph)
|
190
|
+
```
|
191
|
+
|
192
|
+
#### Show your graph
|
193
|
+
|
194
|
+
```ruby
|
195
|
+
show_window
|
196
|
+
```
|
197
|
+
|
198
|
+
#### Result
|
199
|
+
|
200
|
+

|
201
|
+
|
202
|
+
|
203
|
+
|
204
|
+
### Plotter
|
205
|
+
|
206
|
+
#### Determine your function
|
207
|
+
|
208
|
+
```ruby
|
209
|
+
def fn(x)
|
210
|
+
x**2
|
211
|
+
end
|
212
|
+
```
|
213
|
+
|
214
|
+
#### Set scale
|
215
|
+
|
216
|
+
```ruby
|
217
|
+
# 1 unit is equal 40 pixels
|
218
|
+
set_scale(40)
|
219
|
+
```
|
220
|
+
|
221
|
+
#### Draw you function
|
222
|
+
|
223
|
+
```ruby
|
224
|
+
draw_fn(-20, 20) {|args| fn(args)}
|
225
|
+
```
|
226
|
+
|
227
|
+
#### Show your plot
|
228
|
+
|
229
|
+
```ruby
|
230
|
+
show_window
|
231
|
+
```
|
232
|
+
|
233
|
+
#### Result
|
234
|
+
|
235
|
+

|
236
|
+
=======
|
237
|
+
### Numerical integration
|
238
|
+
|
239
|
+
Library `Numerical integration`
|
240
|
+
includes methods for numerical integration of functions, such as 3/8 method, Simpson method, left, right and middle rectangle methods and trapezoid method.
|
241
|
+
|
242
|
+
Each function accepts 4 parameters, such as left and right integration boundaries, default accuracy of 0.0001 and the function itself.
|
243
|
+
Example: `three_eights_integration(4, 5, 0.01) { |x| 1 / x }` or `three_eights_integration(4, 5) { |x| 1 / x }`
|
244
|
+
|
245
|
+
For example, to integrate 1 / x in between [4, 5] using the 3/8 method, you need to use:
|
246
|
+
`NumericalIntegration.three_eights_integration(4, 5) { |x| 1 / x }`
|
247
|
+
|
248
|
+
using the Simpson's method:
|
249
|
+
`NumericalIntegration.simpson_integration(4, 5) { |x| 1 / x }`
|
250
|
+
|
251
|
+
using the left rectangle method:
|
252
|
+
`NumericalIntegration.left_rect_integration(4, 5) { |x| 1 / x }`
|
253
|
+
|
254
|
+
using the right rectangle method:
|
255
|
+
`NumericalIntegration.right_rect_integration(4, 5) { |x| 1 / x }`
|
256
|
+
|
257
|
+
using the middle rectangle method:
|
258
|
+
`NumericalIntegration.middle_rectangles(4, 5) { |x| 1 / x }`
|
259
|
+
|
260
|
+
using the trapezoid method:
|
261
|
+
`NumericalIntegration.trapezoid(4, 5) { |x| 1 / x }`
|
262
|
+
|
263
|
+
|
264
|
+
### Polynomial interpolation
|
265
|
+
Library `polynomial_interpolation`
|
266
|
+
includes methods for two types of polynomial such
|
267
|
+
Lagrange polynomial and Newton polynomial
|
268
|
+
|
269
|
+
Each function accepts 3 parameters, such as
|
270
|
+
array of data points, array returned by function
|
271
|
+
and the node to interpolate.
|
272
|
+
|
273
|
+
using the lagrange_polynomials method:
|
274
|
+
`lagrange_polynomials([-1, 0, 1, 4], [-7, -1, 1, 43], 2 )`
|
275
|
+
|
276
|
+
using the newton_polynomials method:
|
277
|
+
`newton_polynomials([-1, 0, 1, 2], [-9, -4, 11, 78], 0.1 )`
|
278
|
+
|
279
|
+
|
280
|
+
###Geometry
|
281
|
+
Module with geometry functions and geometry structures
|
282
|
+
How to initialize the line with two points:
|
283
|
+
```
|
284
|
+
line = Line2dCanon.new(point1, point2)
|
285
|
+
```
|
286
|
+
How to initialize the line with coefficients:
|
287
|
+
```
|
288
|
+
line.initialize_with_coefficients(a, b, c)
|
289
|
+
```
|
290
|
+
How to check if two lines are parallel:
|
291
|
+
```
|
292
|
+
line1.parallel?(line2)
|
293
|
+
```
|
294
|
+
How to check if two lines are intersecting:
|
295
|
+
```
|
296
|
+
line1.intersecting?(line2)
|
297
|
+
```
|
298
|
+
How to check if two lines are perpendicular:
|
299
|
+
```
|
300
|
+
line1.perpendicular?(line2)
|
301
|
+
```
|
302
|
+
How to get the distance between two parallel lines:
|
303
|
+
```
|
304
|
+
line1.distance_between_parallel_lines(line2)
|
305
|
+
```
|
306
|
+
How to check if the point is on segment:
|
307
|
+
```
|
308
|
+
line.check_point_on_segment(point)
|
309
|
+
```
|
310
|
+
How to check if array of points is on the same line:
|
311
|
+
```
|
312
|
+
line.array_of_points_is_on_line(array_of_points)
|
313
|
+
```
|
314
|
+
How to get a distance from point to line:
|
315
|
+
```
|
316
|
+
distance_point_to_line(point)
|
317
|
+
```
|
318
|
+
How to get a distance from point to plane:
|
319
|
+
```
|
320
|
+
plane.distance_point_to_plane(point)
|
321
|
+
```
|
322
|
+
How to check if the point is on plane:
|
323
|
+
```
|
324
|
+
plane.point_is_on_plane?(point)
|
325
|
+
```
|
326
|
+
|
327
|
+
How to initialize a plane with 3 points:
|
328
|
+
```
|
329
|
+
plane = Plane3d.new(point1, point2, point3)
|
330
|
+
```
|
331
|
+
How to initialize a plane with coefficients:
|
332
|
+
```
|
333
|
+
plane.initialize_with_coefficients(a,b,c,d)
|
334
|
+
```
|
335
|
+
How to get the distance between parallel planes:
|
336
|
+
```
|
337
|
+
plane1.distance_between_parallel_planes(plane2)
|
338
|
+
```
|
339
|
+
How to check if two planes are perpendicular:
|
340
|
+
```
|
341
|
+
perpendicular?(other_plane)
|
342
|
+
```
|
343
|
+
How to check if two planes are intersecting in 3-dimensional space:
|
344
|
+
```
|
345
|
+
plane1.intersecting?(plane2)
|
346
|
+
```
|
347
|
+
How to check if two planes are parallel in 3-dimensional space:
|
348
|
+
```
|
349
|
+
plane1.parallel?(plane2)
|
350
|
+
```
|
351
|
+
How to get a normal vector:
|
352
|
+
```
|
353
|
+
norm = vector_a.norm_vector(point2, point3)
|
354
|
+
```
|
355
|
+
How to check if two vectors are collinear:
|
356
|
+
|
357
|
+
```
|
358
|
+
vector1.collinear?(vector2)
|
359
|
+
```
|
360
|
+
How to get a vector multiplication of two vectors:
|
361
|
+
```
|
362
|
+
vector1.vector_multiplication(vector2)
|
363
|
+
```
|
364
|
+
|
365
|
+
|
366
|
+
### Theory of probability
|
367
|
+
|
368
|
+
#### Combinatorics
|
369
|
+
Module with usual combinatorics formulas
|
370
|
+
```
|
371
|
+
factorial(5) # 5! = 120
|
372
|
+
combination(n, k) # C(n, k) = n! / (k! * (n-k)!)
|
373
|
+
arrangement(n, k) # A(n, k) = n! / (n - k)!
|
374
|
+
```
|
375
|
+
#### Module Dice
|
376
|
+
|
377
|
+
Module describing both ordinary and unique dices
|
378
|
+
|
379
|
+
You can initialize a Polyhedron by two ways
|
380
|
+
|
381
|
+
first: by number - Polyhedron.new(6) - creates polyhedron with 6 sides [1,2,3,4,5,6]
|
382
|
+
|
383
|
+
second: by array - Polyhedron.new([1,3,5]) - creates polyhedron with 3 sides [1,3,5]
|
384
|
+
```
|
385
|
+
class Polyhedron
|
386
|
+
csides # sides number
|
387
|
+
sides # array of sides
|
388
|
+
throw # method of random getting on of the Polyhedron's sides
|
389
|
+
```
|
390
|
+
|
391
|
+
Example
|
392
|
+
|
393
|
+
```
|
394
|
+
d = Polyhedron.new(8)
|
395
|
+
d.csides # 8
|
396
|
+
d.sides # [1,2,3,4,5,6,7,8]
|
397
|
+
d.throw # getting random side (from 1 to 8)
|
398
|
+
|
399
|
+
d1 = Polyhedron.new([1,3,5,6])
|
400
|
+
d1.csides # 4
|
401
|
+
d1.sides # [1,3,5,6]
|
402
|
+
d1.throw # getting random side (from 1 or 3 or 5 or 8)
|
403
|
+
```
|
404
|
+
|
405
|
+
#### Class PolyhedronSet
|
406
|
+
|
407
|
+
You can initialize PolyhedronSet by array of:
|
408
|
+
|
409
|
+
Polyhedrons
|
410
|
+
|
411
|
+
Number of Polyhedron's sides
|
412
|
+
|
413
|
+
Array of sides
|
414
|
+
```
|
415
|
+
class PolyhedronSet
|
416
|
+
percentage # hash with chances of getting definite score
|
417
|
+
throw # method of getting points from throwing polyhedrons
|
418
|
+
make_graph_by_plotter # creating graph introducing chances of getting score
|
419
|
+
```
|
420
|
+
|
421
|
+
Example
|
422
|
+
|
423
|
+
```
|
424
|
+
s = PolyhedronSet.new([6, [1,2,3,4,5,6], Polyhedron.new(6)])
|
425
|
+
|
426
|
+
s.percentage # {3=>0.004629629629629629, 4=>0.013888888888888888, 5=>0.027777777777777776, 6=>0.046296296296296294,
|
427
|
+
# 7=>0.06944444444444445, 8=>0.09722222222222222, 9=>0.11574074074074074,
|
428
|
+
# 10=>0.125, 11=>0.125, 12=>0.11574074074074074, 13=>0.09722222222222222, 14=>0.06944444444444445,
|
429
|
+
# 15=>0.046296296296296294, 16=>0.027777777777777776, 17=>0.013888888888888888, 18=>0.004629629629629629}
|
430
|
+
|
431
|
+
s.throw # getting random score (from 3 to 18)
|
432
|
+
|
433
|
+
s.make_graph_by_plotter(xsize, ysize) # creates a graph in 'tmp/percentage.png'
|
434
|
+
```
|
435
|
+
## Module BernoulliTrials
|
436
|
+
|
437
|
+
Module allows find the probability of an event occurring a certain number of times for any number of independent trials.
|
438
|
+
|
439
|
+
```
|
440
|
+
n - count of independent trials
|
441
|
+
k - count of successful events
|
442
|
+
p - probability of succesful event (k / n)
|
443
|
+
q - probability of bad event (1 - p)
|
444
|
+
|
445
|
+
We have either the probability of event (p) or datas to calculate it (p = suc / all)
|
446
|
+
|
447
|
+
For small n probability is calculated by the Bernoulli formula C(n,k) * (p ^ k) * (q ^ (n-k))
|
448
|
+
For big n probability is calsulated by the Laplace theorem f((k - n*p)/sqrt(n*p*q)) / sqrt(n*p*q)
|
449
|
+
Auxiliary Gaussian function F(x) = exp(-(x^2/2)) / sqrt(2*PI), F(-x) = F(x)
|
450
|
+
|
451
|
+
Laplace theorem give satisfactory approximation for n*p*q > 9
|
452
|
+
```
|
453
|
+
|
454
|
+
Example
|
455
|
+
|
456
|
+
```
|
457
|
+
--- Number 1 ---
|
458
|
+
Probability of making a detail of excellent quality is 0.75.
|
459
|
+
Probability that out of 400 parts, 280 will be of high quality.
|
460
|
+
|
461
|
+
n = 400, k = 280, p = 0.75, q = 0.25
|
462
|
+
|
463
|
+
n * p * q > 9, that Laplace theorem
|
464
|
+
|
465
|
+
F((280-300) / sqrt(75)) = F(-2.31) = F(2.31) = F(exp(-(2.31^2)/2) / sqrt(2*3.14)) = 0.0277
|
466
|
+
P = 0.0277 / sqrt(75) = 0.0032
|
467
|
+
|
468
|
+
--- Number 2 ---
|
469
|
+
Of 100 batteries, 7 breaks down during a year of storage.
|
470
|
+
Choose 5 batteries at random.
|
471
|
+
Probability that among them 3 are serviceable.
|
472
|
+
|
473
|
+
n = 5, k = 3, all = 100, suc = 7
|
474
|
+
p = 7 / 100 = 0.07, q = 0.93
|
475
|
+
|
476
|
+
n * p * q < 9, that Bernoulli formula
|
477
|
+
P = C(5,3) * (0.93^3) * (0.07^2) = 0.0394
|
478
|
+
```
|
479
|
+
|
480
|
+
|
481
|
+
### Matrix
|
482
|
+
|
483
|
+
#### Method Gauss and Kramer
|
484
|
+
|
485
|
+
We have added Two methods for solving a system of linear equations: Gauss and Kramer.
|
486
|
+
|
487
|
+
The Gauss method is implemented as a function, and the Kramer rule is implemented as a method for the Matrix class.
|
488
|
+
|
489
|
+
To use the Gauss method, you need to call it with a single argument-the matrix whose roots you want to find.
|
490
|
+
|
491
|
+
##### Example
|
492
|
+
```ruby
|
493
|
+
gauss_method_sol(Matrix[[1,2,3,4,5],[0,1,-1,2,3],[0,1,-1,2,3],[0,2,-2,4,6]].row_vectors
|
494
|
+
```
|
495
|
+
##### Answer
|
496
|
+
```ruby
|
497
|
+
[-1,3,0,0]
|
498
|
+
```
|
499
|
+
|
500
|
+
To use Kramer's rule, you need to call it as a method of the Matrix class with an array argument containing the values of each expression of a system of linear equations
|
501
|
+
##### Example
|
502
|
+
```ruby
|
503
|
+
Matrix[[2, -5, 3], [4, 1, 4], [1, 2, -8]].kramer([7,21,-11]
|
504
|
+
```
|
505
|
+
##### Answer
|
506
|
+
```ruby
|
507
|
+
[3,1,2]
|
508
|
+
```
|
509
|
+
|
510
|
+
### Machine Learnign Algorithms
|
511
|
+
|
512
|
+
### Backpropogation
|
513
|
+
When you need to compute a gradient value for a really huge expression, that a good practise to use a backpropogation algorithm to enhance the speed and quality of work. First, you needed a construct a Computational Graph, what makes our works more effective than it will be by using a common Gradient Decent
|
514
|
+
```ruby
|
515
|
+
my_graph = Comp_Graph.new("(x*W1+b1)*W2+b2")
|
516
|
+
```
|
517
|
+
Than, we initialize our parametrs:
|
518
|
+
```ruby
|
519
|
+
variables = Hash["x",1.0,"W1",1.0,"b1",1.0,"W2",1.0,"b2",1.0]
|
520
|
+
```
|
521
|
+
Finally, we can start to start training! The values will pass forward throw the graph and return the result of results of neural net(in theory)
|
522
|
+
```ruby
|
523
|
+
computed_value = my_graph.ForwardPass(variables)
|
524
|
+
```
|
525
|
+
|
526
|
+
When it's done, we can use it to compute the curreny of result by loss function(at this example it's just a half of difference between values) and than start to move back, but now we compute the gradient value
|
527
|
+
```ruby
|
528
|
+
trivial_loss = (expected_value - computed_value) * 0.5
|
529
|
+
grad = my_graph.BackwardPass(trivial_loss)
|
530
|
+
```
|
531
|
+
|
532
|
+
That's it! The last thing to do is apply gradient value to inserted parametrs, depended on value of learning speed(learn_rate)
|
533
|
+
```ruby
|
534
|
+
learn_rate = 0.01
|
535
|
+
variables["W1"] += grad["W1"]*learn_rate
|
536
|
+
variables["W2"] += grad["W2"]*learn_rate
|
537
|
+
variables["b1"] += grad["b1"]*learn_rate
|
538
|
+
variables["b2"] += grad["b2"]*learn_rate
|
539
|
+
```
|
540
|
+
After a lot of repeating we will move closer to the perfect values of hyperparametrs in the net
|
541
|
+
|
542
|
+
### Optimization
|
543
|
+
|
544
|
+
#### Karatsuba multiplication
|
545
|
+
The Karatsuba algorithm is a fast multiplication algorithm. It reduces the multiplication of two n-digit numbers to at most ) single-digit multiplications in general. It is therefore faster than the traditional algorithm, which requires ) single-digit products.
|
546
|
+
|
547
|
+
##### Example:
|
548
|
+
```ruby
|
549
|
+
karatsuba(15, 15) #returns 225
|
550
|
+
```
|
551
|
+
|
552
|
+
|
553
|
+
|
554
|
+
## Development
|
555
|
+
|
556
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
557
|
+
|
558
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
559
|
+
|
560
|
+
## Contributing
|
561
|
+
|
562
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/mmcs-ruby/silicium. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
563
|
+
|
564
|
+
## License
|
565
|
+
|
566
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
567
|
+
|
568
|
+
## Code of Conduct
|
569
|
+
|
570
|
+
Everyone interacting in the Silicium project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/silicium/blob/master/CODE_OF_CONDUCT.md).
|
571
|
+
|
572
|
+
### Method Gauss–Seidel
|
573
|
+
Use the-Gauss Seidel Method to solve a system of linear equations
|
574
|
+
|
575
|
+
Members containing x are written to an array of arrays in a. Free members are written in b. Condition for ending the Seidel iteration process when the epsilon accuracy is reached.
|
576
|
+
|
577
|
+
Example
|
578
|
+
```
|
579
|
+
gauss_seidel(a,b,eps)
|
580
|
+
g = gauss_seidel(([[0.13,0.22,-0.33,-0.07],[0,0.45,-0.23,0.07],[0.11,0,-0.08,0.18],[0.08,0.09,0.33,0.21]]),[-0.11,0.33,-0.85,1.7], 0.001)
|
581
|
+
|
582
|
+
```
|
583
|
+
|
584
|
+
Answer:
|
585
|
+
|
586
|
+
```
|
587
|
+
g = [-1,1,9,-6]
|
588
|
+
```
|