2012-04-29 69 views
9

是否有內置的方式在Ruby中打印可讀的矩陣?在Ruby中打印可讀的矩陣

例如

require 'matrix' 
m1 = Matrix[[1,2], [3,4]] 
print m1 

,並讓它在REPL顯示

=> 1 2 
    3 4 

代替:

=> Matrix[[1,2][3,4]] 

Ruby的文檔,以便matrix使它看起來像這就是應該顯示發生,但那不是我所看到的。我知道寫一個函數來做這件事是微不足道的,但如果有一種「正確」的方式,我寧願學習!

回答

8

你可以把它轉換成數組:

m1.to_a.each {|r| puts r.inspect} 

=> [1, 2] 
    [3, 4] 

編輯:

這裏是一個 「點免費」 版本:

puts m1.to_a.map(&:inspect) 
1

您可以使用each_slice法合併column_size方法。

m1.each_slice(m1.column_size) {|r| p r } 
=> [1,2] 
    [3,4] 
3

我無法讓它看起來像文檔,所以我寫了一個功能,你完成相同的任務。

require 'matrix' 

m1 = Matrix[[1,2],[3,4],[5,6]] 

class Matrix 
    def to_readable 
    i = 0 
    self.each do |number| 
     print number.to_s + " " 
     i+= 1 
     if i == self.column_size 
     print "\n" 
     i = 0 
     end 
    end 
    end 
end 

m1.to_readable 

=> 1 2 
    3 4 
    5 6 
3

聲明:我是NMatrix的首席開發人員。

它在NMatrix微不足道。只要做matrix.pretty_print

的列不乾淨對齊,但會很容易解決,我們很樂意這種效果的任何貢獻。

順便說一句,很高興看到這裏的研究員VT人。 =)

+2

我一定會看看SciRuby/NMatrix。我是Ruby的新手,所以我希望將它用於一個畢業設計項目來學習更多。 Ruby中的標準矩陣是不可變的這一事實導致我只是默認回到C#。不知道第三方庫是否會與我的教授一起飛行,並處於緊縮狀態。另外 - 我們在VT同年畢業,非常酷,看到你在UT。我是Math/CS,可能有興趣借貸給你的項目,我會給你留言。 – vpiTriumph 2012-05-04 19:42:43

1

好的,我是一個Ruby編程中的新手。我只是做了第一次入侵,但碰巧我遇到了同樣的問題,並採取了這種快速的方法。 適用於標準的矩陣庫,並將打印格式相同的列。

class Matrix 
    def to_readable 
    column_counter = 0 
    columns_arrays = [] 
    while column_counter < self.column_size 
    maximum_length = 0 
    self.column(column_counter).each do |column_element|# Get maximal size 
     length = column_element.to_s.size 
     if length > maximal_length 
     maximum_length = length 
     end 
    end # now we've got the maximum size 
    column_array = [] 
    self.column(column_counter).each do |column_element| # Add needed spaces to equalize each column 
     element_string = column_element.to_s 
     element_size = element_string.size 
     space_needed = maximal_length - element_size +1 
     if space_needed > 0 
     space_needed.times {element_string.prepend " "} 
     if column_counter == 0 
      element_string.prepend "[" 
     else 
      element_string.prepend "," 
     end 
     end 
     column_array << element_string 
    end 
    columns_arrays << column_array # Now columns contains equal size strings 
    column_counter += 1 
    end 
    row_counter = 0 
    while row_counter < self.row_size 
    columns_arrays.each do |column| 
     element = column[row_counter] 
     print element #Each column yield the correspondant row in order 
    end 
    print "]\n" 
    row_counter += 1 
    end 
end 
end 

任何更正或升級歡迎!

1

這是爲我工作

require 'matrix' 

class Matrix 

    def print 
    matrix = self.to_a 
    field_size = matrix.flatten.collect{|i|i.to_s.size}.max 
    matrix.each do |row| 
     puts (row.collect{|i| ' ' * (field_size - i.to_s.size) + i.to_s}).join(' ')  
    end 
    end 

end 

m = Matrix[[1,23,3],[123,64.5, 2],[0,0,0]] 
m.print 
0

這裏是我的回答:

require 'matrix' 

class Matrix 
    def to_pretty_s 
     s = "" 
     i = 0 
     while i < self.column_size 
      s += "\n" if i != 0 
      j = 0 
      while j < self.row_size 
       s += ' ' if j != 0 
       s += self.element(i, j).to_s 
       j += 1 
      end 
      i += 1 
     end 
     s 
    end 
end 

m = Matrix[[0, 3], [3, 4]] 

puts m # same as 'puts m.to_s' 
# Matrix[[0, 3], [3, 4]] 

puts m.to_pretty_s 
# 0 3 
# 3 4 

p m.to_pretty_s 
# "0 3\n3 4" 

你可以使用Matrix#to_pretty_s以獲取格式的字符串漂亮。

0

這樣做沒有內置的Ruby方式。但是,我創建了一個模塊,可以將其包含在Matrix中,其中包括一個方法readable。你可以找到這個代碼here,但它也在下面的代碼塊中。

require 'matrix' 

module ReadableArrays 
    def readable(factor: 1, method: :rjust) 
     repr = to_a.map { |row| 
      row.map(&:inspect) 
     } 

     column_widths = repr.transpose.map { |col| 
      col.map(&:size).max + factor 
     } 

     res = "" 
     repr.each { |row| 
      row.each_with_index { |el, j| 
       res += el.send method, column_widths[j] 
      } 
      res += "\n" 
     } 
     res.chomp 
    end 
end 

## example usage ## 
class Matrix 
    include ReadableArrays 
end 
class Array 
    include ReadableArrays 
end 

arr = [[1, 20, 3], [20, 3, 19], [-32, 3, 5]] 
mat = Matrix[*arr] 

p arr 
#=> [[1, 20, 3], [20, 3, 19], [-2, 3, 5]] 
p mat 
#=> Matrix[[1, 20, 3], [20, 3, 19], [-2, 3, 5]] 

puts arr.readable 
#=> 
# 1 20 3 
# 20 3 19 
# -32 3 5 
puts mat.readable 
#=> 
# 1 20 3 
# 20 3 19 
# -32 3 5 
puts mat.readable(method: :ljust) 
#=> 
# 1 20 3 
# 20 3 19 
# -32 3 5 
puts mat.readable(method: :center) 
#=> 
# 1 20 3 
# 20 3 19 
# -32 3 5