darr = [];
data = [];
ndata = [];
odata = [];
npt = 50;
ndim = 100;
dtot=0;
data = randn(npt,ndim);

plot(data(:,1), data(:,2), 'x');

for i = 1:npt
	dist = 0;
	for d = 1:ndim
		dist = dist + (data(i,d)^2);
		if(d < ndim-1)
			ndata(i,d) = atan2(data(i,d), data(i,d+1));
		end
		if(d == ndim-1)
			ndata(i, d) = atan2(data(i,d+1), sqrt(dist));
		end
	end
	dist = sqrt(dist);
	ndata(i, ndim) = dist;
	darr(i) = dist;
	dtot = dtot+dist;
end

%polar(darr, 'x');
rad = dtot/npt
figure(3);
plot(ndata(:,1), ndata(:,2), 'x');

for i = 1:npt
	rad = ndata(i, ndim);
	odata(i,ndim) = rad.*sin(ndata(i,ndim-1));
	dist = odata(i,ndim)^2;
	odata(i,1) = rad.*cos(ndata(i,ndim-1)).*sin(ndata(i,1));
	for d = 2:(ndim-1)
		odata(i,d) = odata(i,d-1).*tan(ndata(i,d));
	end

%	for d = (ndim-1):-1:2;
%		odata(i, d) = cos(ndata(i,d-1)).*sqrt((rad-odata(i,d+1))*(rad+odata(i,d+1)));
%		odata(i, d) = cos(ndata(i,d-1)).*rad;
%		odata(i, d) = tan(ndata(i,d)).*odata(i,d+1);
%		odata(i, d) = rad.*cos(ndata(i,ndim-1)).*cos(ndata(i,d-1));
%		odata(i, d) = rad.*cos(ndata(i,ndim-1));
%		for m = d:-1:2
%			odata(i,d) = odata(i,d).*cos(ndata(i,d-1));
%		end
%		dist = dist + odata(i,d)^2;
%	end
%	odata(i,1) = rad.*cos(ndata(i,ndim-1)).*sin(ndata(i,1));
end

figure(1);
plot(data(:,12), data(:,13), 'redx')
figure(2);
plot(odata(:,12), odata(:,13), 'blueo');
err = data-odata;

