4
행렬을 곱하는 코드를 발견했습니다.프롤로그에서 행렬 곱셈
% SWI-Prolog has transpose/2 in its clpfd library
:- use_module(library(clpfd)).
% N is the dot product of lists V1 and V2.
dot(V1, V2, N) :- maplist(product,V1,V2,P), sumlist(P,N).
product(N1,N2,N3) :- N3 is N1*N2.
% Matrix multiplication with matrices represented
% as lists of lists. M3 is the product of M1 and M2
mmult(M1, M2, M3) :- transpose(M2,MT), maplist(mm_helper(MT), M1, M3).
mm_helper(M2, I1, M3) :- maplist(dot(I1), M2, M3).
내가 입력 한 경우 : mult([[1,2],[3,4]],[[5,6],[7,8]],X).
다음 내가 X = [[19, 22], [43, 50]].
얻을 그러나 나는 X = [[1*5+2*7, 1*6+2*8], [3*5+4*7, 3*6+4*8]] .
P.S.을 얻을 수있는 방법 나는 프롤로그를 처음 보았습니다. 감사합니다.
는 어떻게 sumlist/2 변경해야합니까? (+ 대신 *의 펑를 이용하여 이번에) sumlist/2 타고 대신하여 연산 식을 평가/2이고, 용어로서 합계 자체를 나타낸다 : –
의 변화와 유사하게 I는 보였다. – mat
당신은 보여줄 수 있습니까? 나는 이해하지 못한다 ... –