Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # TODO: Write documentation for `Sample`
- module Sample
- VERSION = "0.1.0"
- class Parser
- property path : String
- getter size : Int32
- getter matrix : Array(Array(Float64))
- getter vec : Array(Float64)
- def initialize(path : String)
- @path = path
- @size = 0
- @matrix = Array.new(@size) { |i| Array.new(@size) { |j| 0.0 } }
- @vec = Array.new(@size) { |i| 0.0 }
- end
- def decode
- file = File.read_lines(@path)
- @size = file[0].to_i
- @matrix = Array.new(@size) { |i| Array.new(@size) { |j| 0.0 } }
- @vec = Array.new(@size) { |i| 0.0 }
- @size.times do |i|
- vec = file[i + 1].split(" ").map { |n| n.to_f }
- @size.times do |j|
- @matrix[i][j] = vec[j]
- end
- end
- vec = file[@size + 1].split(" ").map { |n| n.to_f }
- @size.times do |j|
- @vec[j] = vec[j]
- end
- end
- end
- class Matryx
- property matrix : Array(Array(Float64))
- property vec : Array(Float64)
- property aS : Array(Float64)
- def initialize(matrix : Array(Array(Float64)), vec : Array(Float64), size : Int32)
- @matrix = matrix
- @vec = vec
- @aS = Array.new(size) { |i| 0.0 }
- @size = size
- end
- def to_s
- text = @size.to_s + " \n"
- 0..@size.times do |i|
- text += @matrix[i].map { |n| n.to_s }.reduce { |acc, j| acc + " " + j }
- text += "\n"
- end
- text += @vec.map { |n| n.to_s }.reduce { |acc, j| acc + " " + j }
- end
- end
- class A
- def initialize(row : Int32, column : Int32, matryx : Matryx)
- @row = row
- @column = column
- @matryx = matryx
- end
- def calculate
- @matryx.aS[@row] = @matryx.matrix[@row][@column] / @matryx.matrix[@column][@column]
- :ok
- end
- end
- class BC
- def initialize(originalRow : Int32, currentRow : Int32, column : Int32, matryx : Matryx)
- @originalRow = originalRow
- @currentRow = currentRow
- @column = column
- @matryx = matryx
- end
- def calculateMatrix
- @matryx.matrix[@currentRow][@column] -= @matryx.aS[@currentRow] * @matryx.matrix[@originalRow][@column]
- :ok
- end
- def calculateVec
- @matryx.vec[@currentRow] -= @matryx.aS[@currentRow] * @matryx.vec[@originalRow]
- :ok
- end
- end
- class Normalizer
- def initialize(row : Int32, matryx : Matryx)
- @row = row
- @matryx = matryx
- end
- def normalize
- @matryx.vec[@row] /= @matryx.matrix[@row][@row]
- @matryx.matrix[@row][@row] = 1.0
- :ok
- end
- end
- channel = Channel(Symbol).new
- # person = channel.receive
- parser = Parser.new "input/in.txt"
- parser.decode
- size = parser.size
- matrix = parser.matrix
- vec = parser.vec
- matryx = Matryx.new(matrix, vec, size)
- 0..size.times do |column|
- matryx.aS = Array.new(size) { |i| 0.0 }
- 0..size.times do |row|
- if row != column
- spawn do
- channel.send(A.new(row, column, matryx).calculate)
- end
- end
- end
- 0..size.times do |row|
- if row != column
- channel.receive
- end
- end
- 0..size.times do |row|
- if row != column
- 0..size.times do |col2|
- spawn do
- channel.send(BC.new(column, row, col2, matryx).calculateMatrix)
- end
- end
- spawn do
- channel.send(BC.new(column, row, 0, matryx).calculateVec)
- end
- end
- end
- 0..size.times do |row|
- if row != column
- 0..size.times do |col2|
- channel.receive
- end
- channel.receive
- end
- end
- end
- 0..size.times do |row|
- spawn do
- channel.send(Normalizer.new(row, matryx).normalize)
- end
- end
- 0..size.times do |row|
- channel.receive
- end
- File.write("input/out.txt", matryx.to_s)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement