矩阵运算 Prolog

发布于 2024-11-04 03:56:11 字数 320 浏览 3 评论 0原文

您好,我需要一些 prolog 函数的帮助,请:

定义谓词:

行(X,N,C):C 是矩阵 X 的第 N 行。

列(X,N,C):C 是矩阵 X 的第 N 列.first_column

(X,C,R):矩阵X由第一列C和矩阵R的其余部分组成

。 对称(X):X是对角线对称的二次矩阵。

该矩阵是列表的列表:[[a,b,c],[d,e,f],[g,h,i]]>>>

          a b c
          d e f
          g h i

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

弄潮 2024-11-11 03:56:11

考虑:

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

测试:

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

对于行:

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

列:

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

第一列:

?- 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]].

最后,矩阵对称性由任何矩阵本身的转置定义。

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

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

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.
离不开的别离 2024-11-11 03:56:11

在 SWI-Prolog 中,您可以像这样定义行和列谓词:

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

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

请注意,如果仅给出矩阵,则使用这些定义您还可以生成行/列,例如

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

对称矩阵可以这样生成:

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

用法:

?- 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].

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].
蓝礼 2024-11-11 03:56:11

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

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

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

col(N,Matrix,Col) :-
        findall(E,(append(_,[Row|_],Matrix),nth1(N,Row,E)),Col).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文