Skip to content

Commit b482f4c

Browse files
authored
Merge pull request #3 from vinhig/to-string
Implement a readable matrix representation
2 parents dec30bd + 26389cf commit b482f4c

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

src/crystaledge/matrix.cr

+30-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ module CrystalEdge
168168
h.times do |c|
169169
cell = T.new 0
170170
width.times do |o|
171-
cell += self[o,c] * other[r, o]
171+
cell += self[o, c] * other[r, o]
172172
end
173173
result[r, c] = cell
174174
end
@@ -295,6 +295,35 @@ module CrystalEdge
295295
def {{key}}!(*values)
296296
copy_from {{key}}(*values)
297297
end
298+
299+
# Return string representation of the matrix
300+
def to_s
301+
# Format matrix by taking longest number
302+
longer = 0
303+
W.times do |row|
304+
H.times do |col|
305+
if longer < "#{@matrix[index(row, col)]} ".size
306+
longer = "#{@matrix[index(row, col)]} ".size
307+
end
308+
end
309+
end
310+
# Printing matrix
311+
W.times do |row|
312+
str = "| "
313+
H.times do |col|
314+
if col != H-1
315+
str += "#{@matrix[index(row, col)]} "
316+
else
317+
str += "#{@matrix[index(row, col)]}"
318+
end
319+
(longer - "#{@matrix[index(row, col)]} ".size).times do
320+
str += " "
321+
end
322+
end
323+
str += " |"
324+
return str
325+
end
326+
end
298327
{% end %}
299328
end
300329

0 commit comments

Comments
 (0)