Search code examples
prolog

Find sum of the third square of the matrix


I need to find the sum of the third square of the matrix. That means that there is matrix like

0 0 0 0
0 0 0 0
1 1 0 0
1 1 0 0

and the result must be 4

I think that I manage to find sum of the elements in the 1D list that I'm looking for, but I can't solve this problem with 2D lists
Right now I've made somethig like this

matrix_data(1,[[0,0,0,0],
               [0,0,0,0],
               [1,1,0,0],
               [1,1,0,0]]).

%Start task
%+Test matrix#, -Sum
matrix_main(K,S):-
    matrix_data(K,Ms),
    length(Ms,N),
    matrix_pro(Ms, 1,N,0,S).

%Procedure to go through rows and get sum of each
%+Matrix, +Current row ,+Rows Counter, +Accumulator, -Sum
matrix_pro([R|Rs],Cr,Size,Acc,S):-
    print('    Enter matrix_pro   '),
    row_sum(R, Cr, 1,Size,S11),
    print('   Sum is S11 ' + S11),
    Cr1 is Cr + 1,
    Acc1 is Acc +S11,
    matrix_pro(Rs,Cr1,Size,Acc1,S).

matrix_pro([],_,_,S,S).

%Calculate the sum of each element that we need in a row
%List, +Curent row, +Current index/column in list, +Length, sum
row_sum([],_,_,_,0).

row_sum([H|T], Cr, Index, Size, Sum):-
    Cr>= Size/2,
    Line is Size/2,
    Line =< Cr,
    Index =< Line,
    Index1 is Index + 1,
    row_sum(T, Cr, Index1, Size, S1),
    Sum is S1 + H.

row_sum([_|T], Cr, Index, Size, Sum):-
    print('  Entering with no sum   '),
   % Line is Size /2,
    print(' Houston?? '),
    Index > Size/2,
    print('Aaaaaa'),
    Index1 is Index + 1,
    row_sum(T,Cr,Index1,Size,Sum).

I would really appreciate your help.


Solution

  • For any square matrix and with (even length sides) you may split the matrix in two halves, then for every row in the second half sum half items and finally aggregate those partial sums:

    matrix_data(1,[[0,0,0,0],
                   [0,0,0,0],
                   [1,1,0,0],
                   [1,1,0,0]]).
    
    sum_third_quadrant(K, Sum):-
      matrix_data(K, Matrix),
      length(Matrix, Len),
      HalfLen is Len/2,
      length(Upper, HalfLen),      % each one of these lists
      length(Lower, HalfLen),      % will have half the length of the matrix side
      length(UpperRow, HalfLen),   % and will be filled with free variables
      length(LowerRow, HalfLen),   % that will be unified by append/3
      append(Upper, Lower, Matrix), % Split Matrix in two halves
      findall(HalfRowSum,
              ( member(Row, Lower),  % For every Row in lower half matrix
                append(LowerRow, UpperRow, Row),  % Split Row y two halves
                sumlist(LowerRow, HalfRowSum)   % And compute the sum of the lower half in HalfrowSum
              ),
              HalfSums),  %HalfSums is a list of the sum of each half row
      sumlist(HalfSums, Sum).  % Now sum the partial sums
    

    Sample run:

    ?- sum_third_quadrant(1, Sum).
    Sum = 4.
    

    This procedure will not work for odd length sided matrices as I am unsure what would be the third quadrant there.