function b = canonical_class_vec(a)

%Reduces a class vector to its canonical form.

%Input "a" is 4d real column vector, 
%a(1:3) is a class vector and  a(4) is a global phase.

%Output "b" is the new 4d real column vector,
%b(1:3) is a canonical vector equivalent to a(1:3) and b(4)=a(4). 
%b will satisfy:
%pi/2 > bx >= by >= bz >= 0
%pi/2 >= bx + by
%if bz==0, then pi/4 >= bx

b=a;
pih = pi/2;
for j=1:3
	if(b(j)<0)
		while(b(j)<0)
			b(j) = b(j) + pih;
		end
	end
	if(b(j)>=pih)
		while(b(j)>=pih)
			b(j) = b(j) - pih;
		end
	end
end

b(1:3)=flipud(sort(b(1:3)));%now b in increasing oreder

%a good example for the raison d'etre of the next if() is 
%b1=pi/2-eps, b2=pi/2-2eps, b3=pi/2-3eps, 
%where eps>0 and small
if(b(1)+b(2)>pih)
	old_b1= b(1);
	b(1) = pih - b(2);
	b(2) = pih - old_b1;
	%at this point b(1)>b(2) but b(3) may be larger than b(1) or b(2)
	if(b(3)>b(2))
		b(1:3)=flipud(sort(b(1:3)));
	end
end

if(b(3)<1e-14 & b(1)>pi/4)
	b(1) = pih - b(1);
end
	

