Consider
uu=[
[ ((1, 2), (3, -2)), ((1, 3), (3, -3))],
[ ((1, -1), (2, 1))],
[((1, 1), (2, -1)), ((1, -1), (2, 1))],
[ ((2, -2), (4, 2)), ((2, -3), (4, 3))],
[((3, 1), (4, -1)), ((3, 4), (4, -4))],
[((3, 1), (4, -1)), ((3, 2), (4, -2)), ((3, 3), (4, -3))]
]
and the following algorithm
function sum_exp(uu)
sum_dict = Dict{Int, Int}()
for pairs in uu
for pair in pairs
first_elem = pair[1]
second_elem = pair[2]
if haskey(sum_dict, first_elem)
sum_dict[first_elem] += second_elem
else
sum_dict[first_elem] = second_elem
end
end
end
return sum_dict
end
function cartesian_product
(uu)
res = []
g = Iterators.product(uu...)
for gi in g
if all(values(sum_exp(gi)) .== 0)
push!(res, gi)
end
end
return res
end
This code works fine but it is slow and take a lot of memory.
I suspect the root cause lies in the execution of g = Iterators.product(uu...)
prior to calculating sum_exp(gi)) .== 0
within the cartesian_product
function
Code Functionality:
- Each tuple within
uu
represents a polynomial, with((1, 2), (3, -2))
corresponding tox1^2 * x3^(-2)
. - The code aims to multiply each tuple in
uu[1]
by all tuples withinuu[i]
fori > 1
. - The
sum_exp
function returns terms with zero exponents.