求矩阵的三次方的总和

Find sum of the third square of the matrix

我需要求出矩阵三次方的和。这意味着有像

这样的矩阵
0 0 0 0
0 0 0 0
1 1 0 0
1 1 0 0

结果必须是 4

我想我设法找到了我正在寻找的一维列表中元素的总和,但我无法用二维列表解决这个问题
现在我做了这样的东西

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).

非常感谢您的帮助。

对于任何具有(偶数边长)的方阵,您可以将矩阵分成两半,然后对后半部分中的每一行求和一半的项目,最后汇总这些部分和:

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

样本运行:

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

此过程不适用于奇数边矩阵,因为我不确定那里的第三象限是什么。

另一个可能的解决方案(对于偶数阶方阵):

%        1 2 3 4 5 6
%      1 . . . . . .
%      2 . . . . . .
%      3 . . . . . .
%   Hu 4 x x x . . .
%      5 x x x . . .
%   N  6 x x x . . .
%        1  Hl

sum3q(K, S) :-
    matrix_data(K, Mat),
    length(Mat, N),
    Hl is N / 2,
    Hu is Hl + 1,
    findall( X,
             ( between(Hu, N, I),
               nth1(I, Mat, Row),
               between(1, Hl, J),
               nth1(J, Row, X) ),
             Xs ),
    sumlist(Xs, S).

示例:

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

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

查询:

?- sum3q(1, S).
S = 4.

?- sum3q(2, S).
S = 9.