nianek
nianek

Reputation: 11

Matrix operations Prolog

Hi I need a help with some prolog functions, please:

Define predicates:

row(X,N,C): C is the row N of matrix X.

column(X,N,C): C is the column N of matrix X.

first_column(X,C,R): the matrix X is formed by first column C and the rest of matrix R.

symmetrical(X): X is a quadratic matrix symmetrical to the diagonal.

The matrix is a list of lists: [[a,b,c],[d,e,f],[g,h,i]] >>>

          a b c
          d e f
          g h i

Upvotes: 0

Views: 12144

Answers (3)

user206428
user206428

Reputation:

Consider:

row(M, N, Row) :-
    nth1(N, M, Row).

column(M, N, Col) :-
    transpose(M, MT),
    row(MT, N, Col).

symmetrical(M) :-
    transpose(M, M).

transpose([[]|_], []) :- !.
transpose([[I|Is]|Rs], [Col|MT]) :-
    first_column([[I|Is]|Rs], Col, [Is|NRs]),
    transpose([Is|NRs], MT).

first_column([], [], []).
first_column([[]|_], [], []).
first_column([[I|Is]|Rs], [I|Col], [Is|Rest]) :-
    first_column(Rs, Col, Rest).

Testing with:

matrix([[a,b,c],[d,e,f],[g,h,i]]).

For rows:

 ?- matrix(M), row(M, N, Row).
M = [[a, b, c], [d, e, f], [g, h, i]],
N = 1,
Row = [a, b, c] ;
M = [[a, b, c], [d, e, f], [g, h, i]],
N = 2,
Row = [d, e, f] ;
M = [[a, b, c], [d, e, f], [g, h, i]],
N = 3,
Row = [g, h, i] ;
false.

Columns:

?- matrix(M), column(M, N, Col).
M = [[a, b, c], [d, e, f], [g, h, i]],
N = 1,
Col = [a, d, g] ;
M = [[a, b, c], [d, e, f], [g, h, i]],
N = 2,
Col = [b, e, h] ;
M = [[a, b, c], [d, e, f], [g, h, i]],
N = 3,
Col = [c, f, i] ;
false.

First column:

?- matrix(M), first_column(M, C, R).
M = [[a, b, c], [d, e, f], [g, h, i]],
C = [a, d, g],
R = [[b, c], [e, f], [h, i]].

Finally, matrix symmetry is defined by any matrix which is the transposition of itself.

 ?- matrix(M), symmetrical(M).
false.

?- symmetrical([[a,b,c],[b,d,e],[c,e,f]]).
true.

Upvotes: 5

Kaarel
Kaarel

Reputation: 10672

In SWI-Prolog you could define the row and column predicates like this:

row(N, Matrix, Row) :-
    nth1(N, Matrix, Row).

col(N, Matrix, Col) :-
    maplist(nth1(N), Matrix, Col).

Note that using these definitions you can also generate the rows/columns if only the Matrix is given, e.g.

?- col(N, [[a, b], [c, d]], Col).
N = 1,
Col = [a, c] ;
N = 2,
Col = [b, d] ;
false.

Symmetric matrices could be generated like this:

% Generates matrix elements
element(RowN-ColN, Matrix, El) :-
    row(RowN, Matrix, Row),
    nth1(ColN, Row, El).

% Generates matrix symmetric elements, i.e. where Aij = Aji.
symmetric_element(Matrix, RowN-ColN) :-
    element(RowN-ColN, Matrix, El),
    element(ColN-RowN, Matrix, El).

% Generates row-colum indices for the upper triangle.
get_index_pair(N, RowN-ColN) :-
    between(1, N, RowN),
    succ(RowN, RowN1),
    between(RowN1, N, ColN).

% Generates matrixes where every element is symmetric.
symmetric(Matrix) :-
    length(Matrix, N),
    findall(IndexPair, get_index_pair(N, IndexPair), IndexPairs),
    maplist(symmetric_element(Matrix), IndexPairs).

Usage:

?- Matrix = [[a, b, c], Row2, Row3], symmetric(Matrix), numbervars(Matrix, 0, _).
Matrix = [[a, b, c], [b, A, B|C], [c, B|D]],
Row2 = [b, A, B|C],
Row3 = [c, B|D].

Upvotes: 3

尾崎隆大
尾崎隆大

Reputation: 158


row(N,Matrix,Row) :-
        nth1(N,Matrix,Row).

col(N,Matrix,Col) :-
        findall(E,(append(_,[Row|_],Matrix),nth1(N,Row,E)),Col).

Upvotes: 0

Related Questions