function [r,s,t] = xyztorst(x,y,z)

% from (x,y,t) in equilateral tetrahedron to (r,s,t) coordinates in standard tetrahedron

N_p = length(x);

v1 = [-1, -1/sqrt(3), -1/sqrt(6)]'; v2 = [1, -1/sqrt(3), -1/sqrt(6)]';
v3 = [0, 2/sqrt(3), -1/sqrt(6)]'; v4 = [0, 0, 3/sqrt(6)]';

% postup podle Wikipedie
A = [v1 - v4, v2 - v4, v3 - v4];
b = [x;y;z] - v4 * ones(1,N_p);

lambdas = A\b;
lambda3 = lambdas(1,:); lambda4 = lambdas(2,:); lambda2 = lambdas(3,:);
lambda1 = 1 - lambda2 - lambda3 - lambda4;

r = -lambda3 + lambda4 - lambda2 - lambda1;
s = -lambda3 - lambda4 + lambda2 - lambda1;
t = -lambda3 - lambda4 - lambda2 + lambda1;

% postup pomoci vztahu pro bod vyjadreny pomoci r,s,t
%A = 0.5 * [v2 - v1, v3 - v1, v4 - v1];
%b = [x;y;z] - 0.5 * (-v1 + v2 + v3 + v4) * ones(1,N_p);
%rst = A\b;
%r = rst(1,:); s = rst(2,:); t = rst(3,:);

end