firefrorefiddle
firefrorefiddle

Reputation: 3805

Summing multiple values in Clingo

I am computing the sums of multiple fields in clingo:

x(1,2,3).
x(4,5,6).
x(7,8,9).

y(A,B,C) :-
    A = #sum { A1, B1, C1 : x(A1,B1,C1) },
    B = #sum { B1, A1, C1 : x(A1,B1,C1) },
    C = #sum { C1, A1, B1 : x(A1,B1,C1) }.

This works. The output is:

x(1,2,3) x(4,5,6) x(7,8,9) y(12,15,18)

However, it is tedious. What I would like very much is to sum them all at once (non-working):

z(A,B,C) :- (A,B,C) = #sum {(A1,B1,C1) : x(A1,B1,C1)}.

Output is:

test.lp:10:30-38: info: tuple ignored:
  (1,2,3)

test.lp:10:30-38: info: tuple ignored:
  (4,5,6)

test.lp:10:30-38: info: tuple ignored:
  (7,8,9)

Is there a way to simplify my first approach?

Upvotes: 0

Views: 1219

Answers (1)

Vijaya Pandey
Vijaya Pandey

Reputation: 4282

To simplify your approach, instead of doing:

y(A,B,C) :-
    A = #sum { A1, B1, C1 : x(A1,B1,C1) },
    B = #sum { B1, A1, C1 : x(A1,B1,C1) },
    C = #sum { C1, A1, B1 : x(A1,B1,C1) }.

you can simply do:

y(A, B, C) :- A = #sum{(X) : x(X,_,_)}, B = #sum{(X) : x(_,X,_)}, C = #sum{(X) : x(_,_,X)}.

Output:

enter image description here

Upvotes: 1

Related Questions