# v_atan2sc

## PURPOSE

V_ATAN2SC sin and cosine of atan(y/x) [S,C,R,T]=(Y,X)

## SYNOPSIS

function [s,c,r,t]=v_atan2sc(y,x)

## DESCRIPTION

    V_ATAN2SC    sin and cosine of atan(y/x) [S,C,R,T]=(Y,X)

     Outputs:
        s    sin(t) where tan(t) = y/x
        c    cos(t) where tan(t) = y/x
        r    sqrt(x^2 + y^2)
        t    arctan of y/x

     Note y and x can be arrays but must be the same size. The outputs will
     all be the same size as y.

## CROSS-REFERENCE INFORMATION

This function calls:

This function is called by:

- v_rotpl2ro V_ROTPL2RO find matrix to rotate in the plane containing u and v r=[u,v,t]
- v_rotro2eu V_ROTRO2EU converts a 3x3 rotation matrix into the corresponding euler angles
- v_rotro2pl V_ROTRO2PL find the plane and rotation angle of a rotation matrix [u,v,t]=r

## SOURCE CODE

    0001 function [s,c,r,t]=v_atan2sc(y,x)
    0002 %V_ATAN2SC    sin and cosine of atan(y/x) [S,C,R,T]=(Y,X)
    0003 %
    0004 % Outputs:
    0005 %    s    sin(t) where tan(t) = y/x
    0006 %    c    cos(t) where tan(t) = y/x
    0007 %    r    sqrt(x^2 + y^2)
    0008 %    t    arctan of y/x
    0009 %
    0010 % Note y and x can be arrays but must be the same size. The outputs will
    0011 % all be the same size as y.
    0012
    0013 %      Copyright (C) Mike Brookes 2007
    0014 %      Version: $Id: v_atan2sc.m 10865 2018-09-21 17:22:45Z dmb $
    0015 %
    0016 %   VOICEBOX is a MATLAB toolbox for speech processing.
    0017 %   Home page: http://www.ee.ic.ac.uk/hp/staff/dmb/voicebox/voicebox.html
    0018 %
    0019 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0020 %   This program is free software; you can redistribute it and/or modify
    0021 %   it under the terms of the GNU Lesser General Public License as published by
    0022 %   the Free Software Foundation; either version 3 of the License, or
    0023 %   (at your option) any later version.
    0024 %
    0025 %   This program is distributed in the hope that it will be useful,
    0026 %   but WITHOUT ANY WARRANTY; without even the implied warranty of
    0027 %   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    0028 %   GNU Lesser General Public License for more details.
    0029 %
    0030 %   You can obtain a copy of the GNU Lesser General Public License from
    0031 %   https://www.gnu.org/licenses/ .
    0032 %
    0033 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0034
    0035 sz=size(y);
    0036 s=zeros(sz);
    0037 c=NaN(sz);
    0038 r=zeros(sz);
    0039 t=NaN(sz);
    0040 m=y==0;
    0041 if any(m(:)) % handle case when y=0 and possibly x=0 also
    0042     t(m)=(x(m)<0);
    0043     c(m)=1-2*t(m);
    0044     r(m)=abs(x(m));
    0045     t(m)=t(m)*pi;
    0046 end
    0047 m=abs(y)>abs(x) & isnan(c);
    0048 if any(m(:))
    0049     q = x(m)./y(m);
    0050     u = sqrt(1+q.^2).*sign(y(m)); % avoids underflow even if x and y are very small
    0051     s(m) = 1./u;
    0052     c(m) = s(m).*q;
    0053     r(m) = y(m).*u;
    0054 end
    0055 m=isnan(c);
    0056 if any(m(:))
    0057     q = y(m)./x(m);
    0058     u = sqrt(1+q.^2).*sign(x(m));
    0059     c(m) = 1./u;
    0060     s(m) = c(m).*q;
    0061     r(m) = x(m).*u;
    0062 end
    0063 m=isnan(t);
    0064 if nargout>3 && any(m(:))
    0065     t(m)=atan2(s(m),c(m));
    0066 end