Method: LogicTools#to_sum_product_array

Defined in:
lib/logic_tools/minimal_column_covers.rb

#to_sum_product_array(product) ⇒ Object

Converts a product of sum to a sum of product.

NOTE: * Both the input are outputs are represented as array of arrays.


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/logic_tools/minimal_column_covers.rb', line 13

def to_sum_product_array(product)
    return product[0].map {|term| [term] } if product.size == 1
    # Generate the initial terms.
    sum = product[0].product(product[1]) 
    sum.each {|term| term.sort!.uniq! }
    sum.uniq!
    # Fill then with each factor to the resulting sum of product.
    # print "sum = #{sum}, product=#{product}\n"
    (2..(product.size-1)).each do |i|
        sum.map! do |term|
            # # print "mapping #{product[i]}\n"
            set = []
            product[i].each do |fact|
                if term.include?(fact) then
                    set << term unless set.include?(term)
                else
                    nterm = term.clone
                    nterm << fact
                    nterm.sort!
                    set << nterm
                end
            end
            set
        end
        sum.flatten!(1)
        # print "then sum=#{sum}\n"
        sum.uniq!
        # print "now sum=#{sum}\n"
        # pid, size = `ps ax -o pid,rss | grep -E "^[[:space:]]*#{$$}"`.strip.split.map(&:to_i)
        # print "memory usage=#{size}\n"
    end
    # print "\n"
    return sum
end