Agno

Untitled

Nov 24th, 2019
550
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. text += @matrix[i].map { |n| n.to_s }.reduce { |acc, j| acc + " " + j }
  52. text += "\n"
  53. end
  54. text += @vec.map { |n| n.to_s }.reduce { |acc, j| acc + " " + j }
  55. end
  56. end
  57.  
  58. class A
  59. def initialize(row : Int32, column : Int32, matryx : Matryx)
  60. @row = row
  61. @column = column
  62. @matryx = matryx
  63. end
  64.  
  65. def calculate
  66. @matryx.aS[@row] = @matryx.matrix[@row][@column] / @matryx.matrix[@column][@column]
  67. :ok
  68. end
  69. end
  70.  
  71. class BC
  72. def initialize(originalRow : Int32, currentRow : Int32, column : Int32, matryx : Matryx)
  73. @originalRow = originalRow
  74. @currentRow = currentRow
  75. @column = column
  76. @matryx = matryx
  77. end
  78.  
  79. def calculateMatrix
  80. @matryx.matrix[@currentRow][@column] -= @matryx.aS[@currentRow] * @matryx.matrix[@originalRow][@column]
  81. :ok
  82. end
  83.  
  84. def calculateVec
  85. @matryx.vec[@currentRow] -= @matryx.aS[@currentRow] * @matryx.vec[@originalRow]
  86. :ok
  87. end
  88. end
  89.  
  90. class Normalizer
  91. def initialize(row : Int32, matryx : Matryx)
  92. @row = row
  93. @matryx = matryx
  94. end
  95.  
  96. def normalize
  97. @matryx.vec[@row] /= @matryx.matrix[@row][@row]
  98. @matryx.matrix[@row][@row] = 1.0
  99. :ok
  100. end
  101. end
  102.  
  103. channel = Channel(Symbol).new
  104.  
  105. # person = channel.receive
  106.  
  107. parser = Parser.new "input/in.txt"
  108. parser.decode
  109. size = parser.size
  110. matrix = parser.matrix
  111. vec = parser.vec
  112. matryx = Matryx.new(matrix, vec, size)
  113.  
  114. 0..size.times do |column|
  115. matryx.aS = Array.new(size) { |i| 0.0 }
  116.  
  117. 0..size.times do |row|
  118. if row != column
  119. spawn do
  120. channel.send(A.new(row, column, matryx).calculate)
  121. end
  122. end
  123. end
  124.  
  125. 0..size.times do |row|
  126. if row != column
  127. channel.receive
  128. end
  129. end
  130.  
  131. 0..size.times do |row|
  132. if row != column
  133. 0..size.times do |col2|
  134. spawn do
  135. channel.send(BC.new(column, row, col2, matryx).calculateMatrix)
  136. end
  137. end
  138. spawn do
  139. channel.send(BC.new(column, row, 0, matryx).calculateVec)
  140. end
  141. end
  142. end
  143.  
  144. 0..size.times do |row|
  145. if row != column
  146. 0..size.times do |col2|
  147. channel.receive
  148. end
  149. channel.receive
  150. end
  151. end
  152. end
  153.  
  154. 0..size.times do |row|
  155. spawn do
  156. channel.send(Normalizer.new(row, matryx).normalize)
  157. end
  158. end
  159.  
  160. 0..size.times do |row|
  161. channel.receive
  162. end
  163.  
  164. File.write("input/out.txt", matryx.to_s)
  165. end
Advertisement
Add Comment
Please, Sign In to add comment