Advertisement
Agno

Untitled

Nov 24th, 2019
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. # TODO: Write documentation for `Sample`
  2. module Sample
  3. VERSION = "0.1.0"
  4.  
  5. class Parser
  6. property path : String
  7. getter size : Int32
  8. getter matrix : Array(Array(Float64))
  9. getter vec : Array(Float64)
  10.  
  11. def initialize(path : String)
  12. @path = path
  13. @size = 0
  14. @matrix = Array.new(@size) { |i| Array.new(@size) { |j| 0.0 } }
  15. @vec = Array.new(@size) { |i| 0.0 }
  16. end
  17.  
  18. def decode
  19. file = File.read_lines(@path)
  20. @size = file[0].to_i
  21. @matrix = Array.new(@size) { |i| Array.new(@size) { |j| 0.0 } }
  22. @vec = Array.new(@size) { |i| 0.0 }
  23. @size.times do |i|
  24. vec = file[i + 1].split(" ").map { |n| n.to_f }
  25. @size.times do |j|
  26. @matrix[i][j] = vec[j]
  27. end
  28. end
  29.  
  30. vec = file[@size + 1].split(" ").map { |n| n.to_f }
  31. @size.times do |j|
  32. @vec[j] = vec[j]
  33. end
  34. end
  35. end
  36.  
  37. class Matryx
  38. property matrix : Array(Array(Float64))
  39. property vec : Array(Float64)
  40. property aS : Array(Float64)
  41.  
  42. def initialize(matrix : Array(Array(Float64)), vec : Array(Float64), size : Int32)
  43. @matrix = matrix
  44. @vec = vec
  45. @aS = Array.new(size) { |i| 0.0 }
  46. @size = size
  47. end
  48.  
  49. def to_s
  50. text = @size.to_s + " \n"
  51. 0..@size.times do |i|
  52. text += @matrix[i].map { |n| n.to_s }.reduce { |acc, j| acc + " " + j }
  53. text += "\n"
  54. end
  55. text += @vec.map { |n| n.to_s }.reduce { |acc, j| acc + " " + j }
  56. end
  57. end
  58.  
  59. class A
  60. def initialize(row : Int32, column : Int32, matryx : Matryx)
  61. @row = row
  62. @column = column
  63. @matryx = matryx
  64. end
  65.  
  66. def calculate
  67. @matryx.aS[@row] = @matryx.matrix[@row][@column] / @matryx.matrix[@column][@column]
  68. :ok
  69. end
  70. end
  71.  
  72. class BC
  73. def initialize(originalRow : Int32, currentRow : Int32, column : Int32, matryx : Matryx)
  74. @originalRow = originalRow
  75. @currentRow = currentRow
  76. @column = column
  77. @matryx = matryx
  78. end
  79.  
  80. def calculateMatrix
  81. @matryx.matrix[@currentRow][@column] -= @matryx.aS[@currentRow] * @matryx.matrix[@originalRow][@column]
  82. :ok
  83. end
  84.  
  85. def calculateVec
  86. @matryx.vec[@currentRow] -= @matryx.aS[@currentRow] * @matryx.vec[@originalRow]
  87. :ok
  88. end
  89. end
  90.  
  91. class Normalizer
  92. def initialize(row : Int32, matryx : Matryx)
  93. @row = row
  94. @matryx = matryx
  95. end
  96.  
  97. def normalize
  98. @matryx.vec[@row] /= @matryx.matrix[@row][@row]
  99. @matryx.matrix[@row][@row] = 1.0
  100. :ok
  101. end
  102. end
  103.  
  104. channel = Channel(Symbol).new
  105.  
  106. # person = channel.receive
  107.  
  108. parser = Parser.new "input/in.txt"
  109. parser.decode
  110. size = parser.size
  111. matrix = parser.matrix
  112. vec = parser.vec
  113. matryx = Matryx.new(matrix, vec, size)
  114.  
  115. 0..size.times do |column|
  116. matryx.aS = Array.new(size) { |i| 0.0 }
  117.  
  118. 0..size.times do |row|
  119. if row != column
  120. spawn do
  121. channel.send(A.new(row, column, matryx).calculate)
  122. end
  123. end
  124. end
  125.  
  126. 0..size.times do |row|
  127. if row != column
  128. channel.receive
  129. end
  130. end
  131.  
  132. 0..size.times do |row|
  133. if row != column
  134. 0..size.times do |col2|
  135. spawn do
  136. channel.send(BC.new(column, row, col2, matryx).calculateMatrix)
  137. end
  138. end
  139. spawn do
  140. channel.send(BC.new(column, row, 0, matryx).calculateVec)
  141. end
  142. end
  143. end
  144.  
  145. 0..size.times do |row|
  146. if row != column
  147. 0..size.times do |col2|
  148. channel.receive
  149. end
  150. channel.receive
  151. end
  152. end
  153. end
  154.  
  155. 0..size.times do |row|
  156. spawn do
  157. channel.send(Normalizer.new(row, matryx).normalize)
  158. end
  159. end
  160.  
  161. 0..size.times do |row|
  162. channel.receive
  163. end
  164.  
  165. File.write("input/out.txt", matryx.to_s)
  166. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement