Silicium
No description available
Install / Use
/learn @mmcs-ruby/SiliciumREADME
Silicium
Ruby Math Library written as exercise by MMCS students.
Installation
Add this line to your application's Gemfile:
gem 'silicium'
And then execute:
$ bundle
Or install it yourself as:
$ gem install silicium
Usage
Graphs
Graph initialization
To create an empty graph just initialize an object:
g = OrientedGraph.new
g = UnorientedGraph.new
Of course, you can determine vertices (name them whatever you want!). To do that, write something like:
g = OrientedGraph.new([{v: 0, i: [:one]},
{v: :one, i: [0, 'two']},
{v: 'two', i: [0, 'two']}])
You have to pass an Array of Hashes, each hash consists of pair of keys:
- v: vertex name;
- i:
Arrayof adjacent vertices
Same goes for the case with unoriented graph (note that missing edges will be added automatically):
g = UnorientedGraph.new([{v: 0, i: [:one]},
{v: :one, i: [0, 'two']},
{v: 'two', i: [0, 'two']}])
=======
Graph Methods:
- Add vertex to your graph:
g.add_vertex!(Vertex)
- Add edge to your graph:
g.add_edge!(vertex_from, vertex_to)
- Get vertices adjacted with vertex:
g.adjacted_with(vertex)
- Set label for the edge:
g.label_edge!(vertex_from, vertex_to, label)
- Get label for the edge:
g.get_edge_label(vertex_from, vertex_to)
- Set label for the vertex:
g.label_vertex!(vertex, label)
- Get label for the vertex:
g.get_vertex_label(vertex)
- Get number of vertices:
g.vertex_number
- Get number of edges:
g.edge_number
- Get number of vertex labels:
g.vertex_label_number
- Get number of vertex edges:
g.edge_label_number
- Check whether graph contains vertex:
g.has_vertex?(vertex)
- Check whether graph contains edge:
g.has_edge?(vertex_from, vertex_to)
- Delete vertex:
g.delete_vertex!(vertex)
- Delete edge:
g.delete_edge!(vertex_from, vertex_to)
- Get array of vertices:
g.vertices
Graph algorithms:
- Check whether graph is connected:
g.connected?(graph)
- Breadth-First Search:
g.breadth_first_search?(graph, starting_vertex, searching_vertex)
- Algorithm of Dijkstra:
g.dijkstra_algorythm!(graph, starting_vertex)
- Find Strongly Connected Components:
g.find_strongly_connected_components
-
Algorithm of Dijkstra: dijkstra_algorythm!(graph, starting_vertex)
-
Topological sort
Description
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.
How to use
For you to have a topologically sorted graph, you need to create an object of the class Graph:
graph = Graph.new
Then you need to add vertices to this graph using the class Node:
graph.nodes << (node1 = Node.new(1))
graph.nodes << (node2 = Node.new(2))
Due to the fact that only a directed graph can be sorted topologically, it is necessary to add an edge:
graph.add_edge(node1, node2)
And finally you can type:
TopologicalSortClass.new(graph)
Result
The result for TopologicalSortClass.new(graph).post_order.map(&:to_s) is [2, 1]
Algorithm of Dijkstra: dijkstra_algorythm!(graph, starting_vertex)
Algorithm of Kruskal: kruskal_mst(graph)
GraphVisualiser
Set window size
change_window_size(1000, 600)
Set graph
graph = OrientedGraph.new([{v: :one, i: [:one, :two, :four]},
{v: :two, i:[ :one, :two]},
{v: :five, i:[ :one,:three, :four]},
{v: :four, i:[ :one, :four]},
{v: :three, i:[ :one, :two]}])
set_graph(graph)
Show your graph
show_window
Result

Plotter
Determine your function
def fn(x)
x**2
end
Set scale
# 1 unit is equal 40 pixels
set_scale(40)
Draw you function
draw_fn(-20, 20) {|args| fn(args)}
Show your plot
show_window
Result
Numerical integration
Library Numerical integration
includes methods for numerical integration of functions, such as 3/8 method, Simpson method, left, right and middle rectangle methods and trapezoid method.
Each function accepts 4 parameters, such as left and right integration boundaries, default accuracy of 0.0001 and the function itself.
Example: three_eights_integration(4, 5, 0.01) { |x| 1 / x } or three_eights_integration(4, 5) { |x| 1 / x }
For example, to integrate 1 / x in between [4, 5] using the 3/8 method, you need to use:
NumericalIntegration.three_eights_integration(4, 5) { |x| 1 / x }
using the Simpson's method:
NumericalIntegration.simpson_integration(4, 5) { |x| 1 / x }
using the left rectangle method:
NumericalIntegration.left_rect_integration(4, 5) { |x| 1 / x }
using the right rectangle method:
NumericalIntegration.right_rect_integration(4, 5) { |x| 1 / x }
using the middle rectangle method:
NumericalIntegration.middle_rectangles(4, 5) { |x| 1 / x }
using the trapezoid method:
NumericalIntegration.trapezoid(4, 5) { |x| 1 / x }
Polynomial interpolation
Library polynomial_interpolation
includes methods for two types of polynomial such
Lagrange polynomial and Newton polynomial
Each function accepts 3 parameters, such as array of data points, array returned by function and the node to interpolate.
using the lagrange_polynomials method:
lagrange_polynomials([-1, 0, 1, 4], [-7, -1, 1, 43], 2 )
using the newton_polynomials method:
newton_polynomials([-1, 0, 1, 2], [-9, -4, 11, 78], 0.1 )
###Geometry Module with geometry functions and geometry structures How to initialize the line with two points:
line = Line2dCanon.new(point1, point2)
How to initialize the line with coefficients:
line.initialize_with_coefficients(a, b, c)
How to check if two lines are parallel:
line1.parallel?(line2)
How to check if two lines are intersecting:
line1.intersecting?(line2)
How to check if two lines are perpendicular:
line1.perpendicular?(line2)
How to get the distance between two parallel lines:
line1.distance_between_parallel_lines(line2)
How to check if the point is on segment:
line.check_point_on_segment(point)
How to check if array of points is on the same line:
line.array_of_points_is_on_line(array_of_points)
How to get a distance from point to line:
distance_point_to_line(point)
How to get a distance from point to plane:
plane.distance_point_to_plane(point)
How to check if the point is on plane:
plane.point_is_on_plane?(point)
How to initialize a plane with 3 points:
plane = Plane3d.new(point1, point2, point3)
How to initialize a plane with coefficients:
plane.initialize_with_coefficients(a,b,c,d)
How to get the distance between parallel planes:
plane1.distance_between_parallel_planes(plane2)
How to check if two planes are perpendicular:
perpendicular?(other_plane)
How to check if two planes are intersecting in 3-dimensional space:
plane1.intersecting?(plane2)
How to check if two planes are parallel in 3-dimensional space:
plane1.parallel?(plane2)
How to get a normal vector:
norm = vector_a.norm_vector(point2, point3)
How to check if two vectors are collinear:
vector1.collinear?(vector2)
How to get a vector multiplication of two vectors:
vector1.vector_multiplication(vector2)
Theory of probability
Combinatorics
Module with usual combinatorics formulas
factorial(5) # 5! = 120
combination(n, k) # C(n, k) = n! / (k! * (n-k)!)
arrangement(n, k) # A(n, k) = n! / (n - k)!
Module Dice
Module describing both ordinary and unique dices
You can initialize a Polyhedron by two ways
first: by number - Polyhedron.new(6) - creates polyhedron with 6 sides [1,2,3,4,5,6]
second: by array - Polyhedron.new([1,3,5]) - creates polyhedron with 3 sides [1,3,5]
class Polyhedron
csides # sides number
sides # array of sides
throw # method of random getting on of the Polyhedron's sides
Example
d = Polyhedron.new(8)
d.csides # 8
d.sides # [1,2,3,4,5,6,7,8]
d.throw # getting random side (from 1 to 8)
d1 = Polyhedron.new([1,3,5,6])
d1.csides # 4
d1.sides # [1,3,5,6]
d1.throw # getting random side (from 1 or 3 or 5 or 8)
Class PolyhedronSet
You can initialize PolyhedronSet by array of:
Polyhedrons
Number of Polyhedron's sides
Array of sides
class PolyhedronSet
percentage # hash with chances of getting definite score
throw # method of getting points from throwing polyhedrons
make_graph_by_plotter # creating graph introducing chances of getting score
Example
s = PolyhedronSet.new([6, [1,2,3,4,5,6], Polyhedron.new(6)])
s.percentage # {3=>0.004629629629629629, 4=>0.013888888888888888, 5=>0.027777777777777776, 6=>0.046296296296296294,
# 7=>0.06944444444444445, 8=>0.097222222

