-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.pl
More file actions
70 lines (57 loc) · 2.14 KB
/
vector.pl
File metadata and controls
70 lines (57 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**/
:- [fields].
:- [listHelper].
/*
* Defining Vectors and their Properties.
* */
% vector(Entries, Field, Dimension) - succeeds if the vector with given Entries
% is of the correct Dimension and Field.
vector(Entries, Field, Dimension) :-
len(Entries, Dimension),
in_field(Entries, Field).
% vdot(Vector, Vdot) - succeeds if Vdot is the dot product of the vector Vector,
% and if Vector is in fact a valid Vector.
vdot(vector(Entries, Field, Dimension), Vdot) :-
vector(Entries, Field, Dimension),
sum_entries(Entries, Vdot),
in_field(Entries, Field).
% vsum(V1, V2, Res) - succeeds if V1, V2, Res are valid vectors and V1+V2=Res.
vsum(vector(E1, F0, D),
vector(E2, F1, D),
vector(R, F, D)) :-
vector(E1, F0, D),
vector(E2, F1, D),
add_scalars(E1, E2, R),
extension_field(F0, F1, F).
% scalar_product(C, V0, V) - succeeds if V0, V1 are valid vectors and C*V0=V1.
scalar_product(Scalar,
vector(E0, F0, D),
vector(E1, F1, D)) :-
vector(E0, F0, D),
mult_scalars(Scalar, E0, E1),
field(Scalar, Fs),
extension_field(Fs, F0, F1).
% norm2(Vector, N) - succeeds if N is the square of the norm of a valid Vector.
norm2(vector([H|T], F, D), Norm) :-
pow2(H, H2),
D1 is D-1,
norm2(vector(T, F, D1), Norm1),
pow2(Norm1, N1Sqr),
add_scalar(H2, N1Sqr, Norm).
norm2(vector([H], _, 1), Norm) :- pow2(H, Norm).
% vector_proj(V, W, Res) - succeeds if Res is the projection of V
% W is a basis of dimension D. W is a matrix.
vector_proj(V, [W|T], Res) :-
dot_product(V,W,VdotW),
scalar_product(W, VdotW, W1dotV),
vector_proj(V, T, Nextres),
vsum(W1dotV, Nextres, Res).
vector_proj(vector(E, F, D), [], vector(Zeros, F, D) :- zerovector(D, Zeros).
zerovector(2, vector([0,0], F, 2)).
zerovector(3, vector([0,0,0], F, 3)).
zerovector(0, vector([],F,D)).
zerovector(D, vector([0|Z], F, N)) :- zerovector(Dn, vector(Z, F, N)), Dn is D-1.
dot_product(vector([H1|T1], F, D), vector([H2|T2], F, D), Res) :-
dot_product(vector(T1, F, Dn), vector(T2, F, Dn), Nextres),
Res is H1 * H2 + Nextres.
dot_product(vector([], F, D), vector([], F, D), 0).