function [s1_c0, c1_s0, fx0, fz0, fx1, fz1]= diag_ckt_invar2_aux(M, hx0, hz0, hx1, hz1)
%This is an auxilliary function used in diag_ckt_invar2() and in
%other functions that do similar diagonalizations
[U,D,V] = svd(M);
%Even though M real
%U and V need not be.
%check that they are indeed real.
if((norm(imag(U))>1e-10)|(norm(imag(V))>1e-10))
error("U or V is not real");
end
Vh = V';
%fx1,fz1,fx0,fz0 real because U and V are
fz1 = hz1*U(1,1) + hx1*U(2,1);
fx1 = hz1*U(1,2) + hx1*U(2,2);
fx0 = Vh(1,1)*hx0 + Vh(1,2)*hz0;
fz0 = Vh(2,1)*hx0 + Vh(2,2)*hz0;
s1_c0 = D(1,1);
c1_s0 = D(2,2);
%note that cross(fz1,fx1) = det(U)*cross(hz1,hx1).
%{hx1,hy1,hz1} is a right-handed basis, but
%if det(U)<0, {fx1,fy,fz} becomes a left-handed one.
%U is a real matrix, but is it a complex data type?
if(det(real(U))<0)
fz1=-fz1;
s1_c0 = -s1_c0;
end
%note that cross(fz0,fx0) = det(Vh)*cross(hz0,hx0).
%{hx0,hy0,hz0} is a right-handed basis but
%if det(Vh)<0, {fx0,fy0,fz0} becomes a left-handed one.
%V is a real matrix, but is it a complex data type?
if(det(real(Vh))<0)
fx0=-fx0;
s1_c0 = -s1_c0;
end