如何在 prolog 中转置矩阵

发布于 2024-10-04 02:38:32 字数 136 浏览 0 评论 0原文

如何将 [[1,2,3][4,5,6][6,7,8]] 之类的列表转置为 [[1,4,6], [2,7,8],[3,6,9]]

描述它:我想将矩阵向左翻转 90 度。我怎样才能做到这一点?

How can I transpose a list like [[1,2,3][4,5,6][6,7,8]] to [[1,4,6],[2,7,8],[3,6,9]]?

To depict it: I'd like to flip the matrix 90 degree to the left. How can I do that?

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

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

发布评论

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

评论(8

新雨望断虹 2024-10-11 02:38:32

不确定你的例子是否正确,但我明白了。

如果使用 SWI-PROLOG,您可以使用 CLPFD 模块,如下所示:

< code>:- use_module(library(clpfd)).

允许您使用 transpose/2 谓词,如下所示:

1 ?- transpose([[1,2,3],[4,5,6],[6,7,8]], X).
X = [[1, 4, 6], [2, 5, 7], [3, 6, 8]].

否则(如果没有 SWI-PROLOG),您可以简单地使用此实现(恰好是 SWI 的 clpfd 中的旧版本):

transpose([], []).
transpose([F|Fs], Ts) :-
    transpose(F, [F|Fs], Ts).

transpose([], _, []).
transpose([_|Rs], Ms, [Ts|Tss]) :-
        lists_firsts_rests(Ms, Ts, Ms1),
        transpose(Rs, Ms1, Tss).

lists_firsts_rests([], [], []).
lists_firsts_rests([[F|Os]|Rest], [F|Fs], [Os|Oss]) :-
        lists_firsts_rests(Rest, Fs, Oss).

有关使用内置的foldl和maplist的更新版本,请参阅clpfd.pl

Not sure your example is correct, but I get the idea.

If using SWI-PROLOG, you can use the CLPFD module, like so:

:- use_module(library(clpfd)).

Allowing you to use the transpose/2 predicate, like this:

1 ?- transpose([[1,2,3],[4,5,6],[6,7,8]], X).
X = [[1, 4, 6], [2, 5, 7], [3, 6, 8]].

Otherwise (if no SWI-PROLOG), you could simply use this implementation (which happened to be an old one in SWI's clpfd):

transpose([], []).
transpose([F|Fs], Ts) :-
    transpose(F, [F|Fs], Ts).

transpose([], _, []).
transpose([_|Rs], Ms, [Ts|Tss]) :-
        lists_firsts_rests(Ms, Ts, Ms1),
        transpose(Rs, Ms1, Tss).

lists_firsts_rests([], [], []).
lists_firsts_rests([[F|Os]|Rest], [F|Fs], [Os|Oss]) :-
        lists_firsts_rests(Rest, Fs, Oss).

For an updated version which uses foldl and maplist built-ins, see clpfd.pl.

牵强ㄟ 2024-10-11 02:38:32

这是我能想到的最小的解决方案。

代码

transpose([[]|_], []).
transpose(Matrix, [Row|Rows]) :- transpose_1st_col(Matrix, Row, RestMatrix),
                                 transpose(RestMatrix, Rows).
transpose_1st_col([], [], []).
transpose_1st_col([[H|T]|Rows], [H|Hs], [T|Ts]) :- transpose_1st_col(Rows, Hs, Ts).

测试

:- transpose([[1,2,3],
              [4,5,6],
              [7,8,9]], R),
   print(R).

打印:

[[1,4,7],
 [2,5,8],
 [3,6,9]]

解释

它的工作方式是 transpose 将递归调用 transpose_1st_col 来提取并转置矩阵的第一列。例如:

:- transpose_1st_col([[1,2,3],
                      [4,5,6],
                      [7,8,9]], Row, RestMatrix),
   print(Row),
   print(RestMatrix).

将打印

[1,4,7]

and

[[2,3],
 [5,6],
 [8,9]]

重复此操作,直到输入矩阵为空,此时所有列都已转置。然后将转置的列连接到转置的矩阵中。

This is the smallest solution I could come up with.

Code

transpose([[]|_], []).
transpose(Matrix, [Row|Rows]) :- transpose_1st_col(Matrix, Row, RestMatrix),
                                 transpose(RestMatrix, Rows).
transpose_1st_col([], [], []).
transpose_1st_col([[H|T]|Rows], [H|Hs], [T|Ts]) :- transpose_1st_col(Rows, Hs, Ts).

Test

:- transpose([[1,2,3],
              [4,5,6],
              [7,8,9]], R),
   print(R).

Prints:

[[1,4,7],
 [2,5,8],
 [3,6,9]]

Explanation

The way it works is that transpose will recursively call transpose_1st_col which extracts and transposes the first column of the matrix. For example:

:- transpose_1st_col([[1,2,3],
                      [4,5,6],
                      [7,8,9]], Row, RestMatrix),
   print(Row),
   print(RestMatrix).

will print

[1,4,7]

and

[[2,3],
 [5,6],
 [8,9]]

This is repeated until the input matrix is empty, at which point all columns have been transposed. The transposed columns are then joined into the transposed matrix.

相守太难 2024-10-11 02:38:32

这是较大答案的片段

% transposed(+A, ?B) iff matrix B is transposed matrix A
transposed(A, B) :- transposed(A, [], B).
transposed(M, X, X) :- empty(M), !.
transposed(M, A, X) :- columns(M, Hs, Ts), transposed(Ts, [Hs|A], X).

% empty(+A) iff A is empty list or a list of empty lists
empty([[]|A]) :- empty(A).
empty([]).

% columns(+M, ?Hs, ?Ts) iff Hs is the first column
%   of matrix M and Ts is the rest of matrix M
columns([[Rh|Rt]|Rs], [Rh|Hs], [Rt|Ts]) :- columns(Rs, Hs, Ts).
columns([[]], [], []).
columns([], [], []).

Here's a fragment of a larger answer:

% transposed(+A, ?B) iff matrix B is transposed matrix A
transposed(A, B) :- transposed(A, [], B).
transposed(M, X, X) :- empty(M), !.
transposed(M, A, X) :- columns(M, Hs, Ts), transposed(Ts, [Hs|A], X).

% empty(+A) iff A is empty list or a list of empty lists
empty([[]|A]) :- empty(A).
empty([]).

% columns(+M, ?Hs, ?Ts) iff Hs is the first column
%   of matrix M and Ts is the rest of matrix M
columns([[Rh|Rt]|Rs], [Rh|Hs], [Rt|Ts]) :- columns(Rs, Hs, Ts).
columns([[]], [], []).
columns([], [], []).
节枝 2024-10-11 02:38:32

另一种简单的方法:

transpose(M0, M) :-
    nonvar(M0),
    findall(L, maplist(nth1(_), M0, L), M).

?- transpose([[1,2,3],[4,5,6],[7,8,9]], M). 
M = [[1, 4, 7], [2, 5, 8], [3, 6, 9]]. `

Another simple approach:

transpose(M0, M) :-
    nonvar(M0),
    findall(L, maplist(nth1(_), M0, L), M).

?- transpose([[1,2,3],[4,5,6],[7,8,9]], M). 
M = [[1, 4, 7], [2, 5, 8], [3, 6, 9]]. `
断舍离 2024-10-11 02:38:32

更简单的方法:

trans(M, [P|T]):- first(M, P, A), trans(A, T).
trans(Empty, []):- empty(Empty).

empty([[]|T]):- empty(T).
empty([[]]).

first([[P|A]|R], [P|Ps], [A|As]):- first(R, Ps, As).
first([], [], []).

也高效

[debug] 36 ?- time(trans([[1,2,3],[4,5,6],[7,8,9]],A)).
% 21 inferences, 0.000 CPU in 0.000 seconds (?% CPU, Infinite Lips)
A = [[1,4,7],[2,5,8],[3,6,9]] ;
% 12 inferences, 0.000 CPU in 0.000 seconds (?% CPU, Infinite Lips)
false.

simpler approach:

trans(M, [P|T]):- first(M, P, A), trans(A, T).
trans(Empty, []):- empty(Empty).

empty([[]|T]):- empty(T).
empty([[]]).

first([[P|A]|R], [P|Ps], [A|As]):- first(R, Ps, As).
first([], [], []).

efficient also

[debug] 36 ?- time(trans([[1,2,3],[4,5,6],[7,8,9]],A)).
% 21 inferences, 0.000 CPU in 0.000 seconds (?% CPU, Infinite Lips)
A = [[1,4,7],[2,5,8],[3,6,9]] ;
% 12 inferences, 0.000 CPU in 0.000 seconds (?% CPU, Infinite Lips)
false.
始于初秋 2024-10-11 02:38:32

迭代方法:

trans([H|R],[H1|R1]):-trans2([H|R],[H|R],[],[H1|R1],0),!.
trans2([A|_],_,_,[],N):-length(A,N).
trans2(M,[],H1,[H1|R1],N):-N1 is N+1, trans2(M,M,[],R1,N1).
trans2(M,[H|R],L,[H1|R1],N):-nth0(N,H,X),
   append(L,[X],L1),trans2(M,R,L1,[H1|R1],N).

An iterative approach:

trans([H|R],[H1|R1]):-trans2([H|R],[H|R],[],[H1|R1],0),!.
trans2([A|_],_,_,[],N):-length(A,N).
trans2(M,[],H1,[H1|R1],N):-N1 is N+1, trans2(M,M,[],R1,N1).
trans2(M,[H|R],L,[H1|R1],N):-nth0(N,H,X),
   append(L,[X],L1),trans2(M,R,L1,[H1|R1],N).
古镇旧梦 2024-10-11 02:38:32

我的解决方案带有全名,以便更好地理解:

% emptyMatrix(Line, EmptyMatrix)
emptyMatrix([],[]).
emptyMatrix([_|T1],[[]|T2]):-emptyMatrix(T1,T2).
% only length of parameter 'Line' is interesting. It ignores its content.    

% appendElement(Element, InputList, OutputList)
appendElement(E,[],[E]).
appendElement(E,[H|T],[H|L]):-appendElement(E,T,L).

% appendTransposed(NestedList, InputMatrix, OutputMatrix)
appendTransposed([],[],[]).
appendTransposed([X|T1],[],[[X]|T3]):-appendTransposed(T1,[],T3).
appendTransposed([X|T1],[R|T2],[C|T3]):-appendElement(X,R,C),appendTransposed(T1,T2,T3).

% transposeMatrix(InputMatrix, TransposedMatrix)
transposeMatrix([L|M],T):-emptyMatrix(L,A),transpose([L|M],T,A).
transpose([],T,T).
transpose([L|M],T,A):-appendTransposed(L,A,B),transpose(M,T,B).

“行”可以是列或行。

这个想法在于将元素附加到空矩阵的列表中。
(例如第一行的所有元素 = 所有列的第一个元素
=>第 i-n 行的所有元素 = 所有列的第 i-n 个元素)

它在我的机器上工作,正如此会话协议向我显示的那样:

5 ?- transposeMatrix([[1,2],[3,4]],T).
T = [[1, 3], [2, 4]] ;
false.

6 ?- transposeMatrix([[1],[2]],T).
T = [[1, 2]] ;
false.

7 ?- transposeMatrix([[1,2,3],[4,5,6]],T).
T = [[1, 4], [2, 5], [3, 6]] ;
false.

8 ?- transposeMatrix([[1]],T).
T = [[1]] ;
false.

My solution with full names for a better understanding:

% emptyMatrix(Line, EmptyMatrix)
emptyMatrix([],[]).
emptyMatrix([_|T1],[[]|T2]):-emptyMatrix(T1,T2).
% only length of parameter 'Line' is interesting. It ignores its content.    

% appendElement(Element, InputList, OutputList)
appendElement(E,[],[E]).
appendElement(E,[H|T],[H|L]):-appendElement(E,T,L).

% appendTransposed(NestedList, InputMatrix, OutputMatrix)
appendTransposed([],[],[]).
appendTransposed([X|T1],[],[[X]|T3]):-appendTransposed(T1,[],T3).
appendTransposed([X|T1],[R|T2],[C|T3]):-appendElement(X,R,C),appendTransposed(T1,T2,T3).

% transposeMatrix(InputMatrix, TransposedMatrix)
transposeMatrix([L|M],T):-emptyMatrix(L,A),transpose([L|M],T,A).
transpose([],T,T).
transpose([L|M],T,A):-appendTransposed(L,A,B),transpose(M,T,B).

A 'line' can be a col or a row.

The idea lies in appending the elements into the lists of an empty matrix.
(e.g. all elements of the first row = the first elements of all cols
=> all elements of the first i-nth row = the i-nth elements of all cols)

It works on my machine as this session protocol shows to me:

5 ?- transposeMatrix([[1,2],[3,4]],T).
T = [[1, 3], [2, 4]] ;
false.

6 ?- transposeMatrix([[1],[2]],T).
T = [[1, 2]] ;
false.

7 ?- transposeMatrix([[1,2,3],[4,5,6]],T).
T = [[1, 4], [2, 5], [3, 6]] ;
false.

8 ?- transposeMatrix([[1]],T).
T = [[1]] ;
false.
万劫不复 2024-10-11 02:38:32

另一种方法:

delete_one_list([], []).
delete_one_list([[_|L]|LLs], [L|Ls]) :-
  delete_one_list(LLs, Ls).

transpose_helper([], []).
transpose_helper([[X|_]|Xs], [X|Ys]) :-
  transpose_helper(Xs, Ys).

transpose([[]|_], []).
transpose(List, [L|Ls]) :-
  transpose_helper(List, L),
  delete_one_list(List, NewList),
  transpose(NewList, Ls).

Another approach:

delete_one_list([], []).
delete_one_list([[_|L]|LLs], [L|Ls]) :-
  delete_one_list(LLs, Ls).

transpose_helper([], []).
transpose_helper([[X|_]|Xs], [X|Ys]) :-
  transpose_helper(Xs, Ys).

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