我试图在Prolog中乘以两个向量,但是,如果这些向量包含虚数,我就无法使其工作。到目前为止我的代码:
vector_product([X|Xs],[Y|Ys],OP) :-
inner(Xs,Ys,OP1),
OP is X*Y+OP1.
vector_product([],[],0).
答案 0 :(得分:3)
看看这对你有帮助......
来自Wikipedia的公式:
% (a+bi) + (c+di) = (a+c) + (b+d)i
c_sum((A,B), (C,D), (E,F)) :- E is A+C, F is B+D.
% (a+bi) (c+di) = (ac-bd) + (bc+ad)i
c_mul((A,B), (C,D), (E,F)) :- E is A*C - B*D, F is B*C + A*D.
数字表示为(Real, Imaginary)
。
vector_product([X|Xs], [Y|Ys], OP) :-
vector_product(Xs, Ys, OP1),
c_mul(X, Y, M),
c_sum(M, OP1, OP).
vector_product([], [], (0,0)).