[37] | 1 | % vgg_wedge Wedge product of N-1 N-vectors (generalization of cross product).
|
---|
| 2 | %
|
---|
| 3 | % Y = vgg_wedge(X) Wedge product of columns/rows of X.
|
---|
| 4 | % Y ... double (1,N).
|
---|
| 5 | % X ... double (N,N-1).
|
---|
| 6 | % It is Y = X(:,1) \wedge X(:,2) \wedge ... \wedge X(:,N-1). For N=3,
|
---|
| 7 | % wedge product is the same as cross (vector) product. E.g., for N=4,
|
---|
| 8 | % wedge product of three 4-vectors is in fact computation of a plane in
|
---|
| 9 | % 3-D projective space from 3 points in the plane.
|
---|
| 10 | % The sign is chosen so that for any square matrix X it is: det(X) == wedge(X(:,1:end-1))*X(:,end)
|
---|
| 11 | % Works also dually for
|
---|
| 12 | % Y ... double (N,1).
|
---|
| 13 | % X ... double (N-1,N).
|
---|
| 14 | %
|
---|
| 15 | % Y = vgg_wedge(X_1,X_2,...,X_{N-1}) Wedge product for each (N-1)-tuple of corresponding columns of X_n.
|
---|
| 16 | % X_n ... double (N,K)
|
---|
| 17 | % Y ... double (K,N)
|
---|
| 18 | % Equivalent to
|
---|
| 19 | % for k = 1:K, Y(k,:) = wedge([X_1(:,k) ... X_{N-1}(:,k)]); end
|
---|
| 20 | % E.g.: wedge(X1,X2) is the same as cross(X1,X2)' but faster.
|
---|
| 21 | % Dual form is not available.
|
---|
| 22 |
|
---|
| 23 | function Y = vgg_wedge(varargin)
|
---|
| 24 |
|
---|
| 25 | if nargin == 1
|
---|
| 26 |
|
---|
| 27 | X = varargin{1};
|
---|
| 28 |
|
---|
| 29 | [N,Nm1] = size(X);
|
---|
| 30 | if Nm1>N
|
---|
| 31 | Y = vgg_wedge(X')';
|
---|
| 32 | return
|
---|
| 33 | end
|
---|
| 34 |
|
---|
| 35 | switch N
|
---|
| 36 | case 3 % make it faster for special case N==3
|
---|
| 37 | Y = [X(2,1).*X(3,2)-X(3,1).*X(2,2),...
|
---|
| 38 | X(3,1).*X(1,2)-X(1,1).*X(3,2),...
|
---|
| 39 | X(1,1).*X(2,2)-X(2,1).*X(1,2)];
|
---|
| 40 | otherwise
|
---|
| 41 | for n = 1:N
|
---|
| 42 | Y(n) = (-1)^(n+N)*det(X([1:n-1 n+1:N],:));
|
---|
| 43 | end
|
---|
| 44 | end
|
---|
| 45 |
|
---|
| 46 | else
|
---|
| 47 |
|
---|
| 48 | N = nargin + 1;
|
---|
| 49 | switch N
|
---|
| 50 | case 3 % make it faster for special case N==3
|
---|
| 51 | X1 = varargin{1};
|
---|
| 52 | X2 = varargin{2};
|
---|
| 53 | Y = [(X1(2,:).*X2(3,:)-X1(3,:).*X2(2,:))',...
|
---|
| 54 | (X1(3,:).*X2(1,:)-X1(1,:).*X2(3,:))',...
|
---|
| 55 | (X1(1,:).*X2(2,:)-X1(2,:).*X2(1,:))'];
|
---|
| 56 | otherwise
|
---|
| 57 | for k = 1:size(varargin{1},2)
|
---|
| 58 | for n = 1:N-1
|
---|
| 59 | X(:,n) = varargin{n}(:,k);
|
---|
| 60 | end
|
---|
| 61 | Y(k,:) = vgg_wedge(X);
|
---|
| 62 | end
|
---|
| 63 | end
|
---|
| 64 |
|
---|
| 65 | end
|
---|
| 66 |
|
---|
| 67 |
|
---|
| 68 | return |
---|