如果在Prolog中有虚数,如何计算向量积?

时间:2013-01-22 01:41:06

标签: vector prolog

我试图在Prolog中乘以两个向量,但是,如果这些向量包含虚数,我就无法使其工作。到目前为止我的代码:

vector_product([X|Xs],[Y|Ys],OP) :-
    inner(Xs,Ys,OP1),
    OP is X*Y+OP1.
vector_product([],[],0).

1 个答案:

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