comp.lang.ada
 help / color / mirror / Atom feed
* Arctan: to use with single or with double arguments?
@ 2010-09-24 21:32 Ada novice
  2010-09-24 21:55 ` Jeffrey Carter
  0 siblings, 1 reply; 16+ messages in thread
From: Ada novice @ 2010-09-24 21:32 UTC (permalink / raw)


Hi,
    I'm computing a Jacobian matrix (having elements as partial
derivatives) and some matrix elements have expressions like

Z = Force * Sin (A)  where A is the inverse tangent of some function,
and this function has a numerator Y and a denominator X. So I have

Z_Method1 := Force * Sin(Arctan(Y, X))

using Arctan with double arguments or we get

Z_Method2 := Force * Sin(Arctan(Y / X))

using Arctan with single a argument.

Now I know that Arctan(Y, X)  gives the result between -pi to pi while
Arctan(Y / X)  gives the result between -pi/2 to pi/2.

For some cases of X and Y, I could see that Z_Method1 won't be equal
to Z_Method2. In fact, Z_Method1 differs from Z_Method2 in the sign in
the following 2 cases:

---------------------------------------------------------------
CASE 1: BOTH X AND Y -VE: ANSWER IS BETWEEN  -PI AND -PI/2
y is numerator and x is denominator
---------------------------------------------------------------
y =  -3.000
x =  -1.000
Two arguments Arctan(y,x)---Answer is between -pi to pi
Angle in rad is  -1.893 and Angle in degrees is -108.435
Sine is  -0.949
---------------------------------------------------------------
One argument Arctan(y/x)---Answer is between -pi/2 to pi/2
Angle in rad is   1.249 and Angle in degrees is 71.565
Sine is   0.949

and in the 2nd case:

---------------------------------------------------------------
CASE 2: X = -VE AND Y +VE: ANSWER IS BETWEEN PI/2 AND PI
y is numerator and x is denominator
---------------------------------------------------------------
y =   3.000
x =  -1.000
Two arguments Arctan(y,x)---Answer is between -pi to pi
Angle in rad is   1.893 and Angle in degrees is 108.435
Sine is   0.949
---------------------------------------------------------------
One argument Arctan(y/x)---Answer is between -pi/2 to pi/2
Angle in rad is  -1.249 and Angle in degrees is -71.565
Sine is  -0.949


Both Z_Method1 and Z_Method2 are computing Z = Force * Sin (A) as
mentioned above. Now, a Jacobian matrix is normally used in a
linearization procedure when trying to "simplify" a nonlinear system.
This Jacobian matrix can be consequently be solved for its eigenvalues
to determine the stability of the system.

In the 2 cases above I get Z = Force * Sin (A) with different signs.
Which of Z_Method1 and Z_Method2 to choose? Z_Method1 with the double
arguments in the Arctan function or Z_Method2 with the single argument
in the Arctan function?


Thanks a lot...







^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Arctan: to use with single or with double arguments?
  2010-09-24 21:32 Arctan: to use with single or with double arguments? Ada novice
@ 2010-09-24 21:55 ` Jeffrey Carter
  2010-09-25  8:44   ` Ada novice
  0 siblings, 1 reply; 16+ messages in thread
From: Jeffrey Carter @ 2010-09-24 21:55 UTC (permalink / raw)


On 09/24/2010 02:32 PM, Ada novice wrote:
>
> Now I know that Arctan(Y, X)  gives the result between -pi to pi while
> Arctan(Y / X)  gives the result between -pi/2 to pi/2.

There is no one-parameter Arctan defined in 
Ada.Numerics.Generic_Elementary_Functions; the parameter X has a default of 1.0, 
so what you're really calling is

Arctan (Y / X, 1.0);

Since the X parameter is positive, it's clear the result will be in quadrant I 
or IV, or -Pi/2 to Pi/2. Giving both parameters thus seems more accurate, as it 
correctly handles cases in all 4 quadrants. In addition, it is defined for X = 
0.0 (except when Y = 0.0).

HTH.

-- 
Jeff Carter
"We use a large, vibrating egg."
Annie Hall
44



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Arctan: to use with single or with double arguments?
  2010-09-24 21:55 ` Jeffrey Carter
@ 2010-09-25  8:44   ` Ada novice
  2010-09-25 17:31     ` Jeffrey Carter
  0 siblings, 1 reply; 16+ messages in thread
From: Ada novice @ 2010-09-25  8:44 UTC (permalink / raw)


On Sep 24, 11:55 pm, Jeffrey Carter
<spam.jrcarter....@spam.not.acm.org> wrote:
> On 09/24/2010 02:32 PM, Ada novice wrote:
>
>
>
> > Now I know that Arctan(Y, X)  gives the result between -pi to pi while
> > Arctan(Y / X)  gives the result between -pi/2 to pi/2.
>
> There is no one-parameter Arctan defined in
> Ada.Numerics.Generic_Elementary_Functions; the parameter X has a default of 1.0,
> so what you're really calling is
>
> Arctan (Y / X, 1.0);
>

Thanks. This is true. The default value is 1.0 .



> Since the X parameter is positive, it's clear the result will be in quadrant I
> or IV, or -Pi/2 to Pi/2. Giving both parameters thus seems more accurate, as it
> correctly handles cases in all 4 quadrants. In addition, it is defined for X =
> 0.0 (except when Y = 0.0).
>
> HTH.

I understand that using Arctan (Y, X) gives the right quadrant. And
the sine of the
Arctan result will depend on the quadrant location of (X, Y). The case
with Arctan (Y/X, 1.0) gives
the principal value of the Arctan function i.e. between -pi/2 and pi/
2.

In my expression: Z = Force * Sin (A) then it was unclear to me if A
in Sin (A) should be limited to -pi/2 to
pi/2 or can can it from to -pi to pi.




^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Arctan: to use with single or with double arguments?
  2010-09-25  8:44   ` Ada novice
@ 2010-09-25 17:31     ` Jeffrey Carter
  2010-09-25 21:34       ` Ada novice
  0 siblings, 1 reply; 16+ messages in thread
From: Jeffrey Carter @ 2010-09-25 17:31 UTC (permalink / raw)


On 09/25/2010 01:44 AM, Ada novice wrote:
>
> In my expression: Z = Force * Sin (A) then it was unclear to me if A
> in Sin (A) should be limited to -pi/2 to
> pi/2 or can can it from to -pi to pi.

OK. I can talk about what Ada provides, but I can't help you with domain questions.

-- 
Jeff Carter
"We'll make Rock Ridge think it's a chicken
that got caught in a tractor's nuts!"
Blazing Saddles
87



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Arctan: to use with single or with double arguments?
  2010-09-25 17:31     ` Jeffrey Carter
@ 2010-09-25 21:34       ` Ada novice
  2010-09-26  4:02         ` jpwoodruff
  2010-09-26  7:31         ` Jeffrey Carter
  0 siblings, 2 replies; 16+ messages in thread
From: Ada novice @ 2010-09-25 21:34 UTC (permalink / raw)


On Sep 25, 7:31 pm, Jeffrey Carter
<spam.jrcarter....@spam.not.acm.org> wrote:
> On 09/25/2010 01:44 AM, Ada novice wrote:
>
>
>
> > In my expression: Z = Force * Sin (A) then it was unclear to me if A
> > in Sin (A) should be limited to -pi/2 to
> > pi/2 or can can it from to -pi to pi.
>
> OK. I can talk about what Ada provides, but I can't help you with domain questions.
>
> --
> Jeff Carter
> "We'll make Rock Ridge think it's a chicken
> that got caught in a tractor's nuts!"
> Blazing Saddles
> 87

It's ok. There's a good article on Arctan and Arctan2 at:

http://en.wikipedia.org/wiki/Atan2

After reading that article, I decided to use Arctan2. I don't really
know why Arctan exists in the first place :).

Cheers
YC



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Arctan: to use with single or with double arguments?
  2010-09-25 21:34       ` Ada novice
@ 2010-09-26  4:02         ` jpwoodruff
  2010-09-26  8:21           ` Ada novice
  2010-09-26  7:31         ` Jeffrey Carter
  1 sibling, 1 reply; 16+ messages in thread
From: jpwoodruff @ 2010-09-26  4:02 UTC (permalink / raw)


On Sep 25, 2:34 pm, Ada novice <ycalleecha...@gmx.com> wrote:
> On Sep 25, 7:31 pm, Jeffrey Carter
>
>
>
> <spam.jrcarter....@spam.not.acm.org> wrote:
> > On 09/25/2010 01:44 AM, Ada novice wrote:
>
> > > In my expression: Z = Force * Sin (A) then it was unclear to me if A
> > > in Sin (A) should be limited to -pi/2 to
> > > pi/2 or can can it from to -pi to pi.
>
> > OK. I can talk about what Ada provides, but I can't help you with domain questions.
>
> > --
> > Jeff Carter
> > "We'll make Rock Ridge think it's a chicken
> > that got caught in a tractor's nuts!"
> > Blazing Saddles
> > 87
>
> It's ok. There's a good article on Arctan and Arctan2 at:
>
> http://en.wikipedia.org/wiki/Atan2
>
> After reading that article, I decided to use Arctan2. I don't really
> know why Arctan exists in the first place :).
>
> Cheers
> YC

I concur with your choice for atan2.

My argument (which I just now finished, too late to be interesting)
is to consider the physical situation.  I presume that you are
integrating physical differential equations.  If that's true then
"Force" has a physical meaning as a vector quantity and its direction
is preserved in the terms you're computing.

If my presumption is right, the vector expression of each Jacobian
term also has a direction and might be in any quadrant.  Of course
your specification - your particular domain - might be more
constrained, but even so the equations probably make sense in
each quadrant.

So my suggestion is to use two-parameter arctan, and interpret the
quadrant of the result, and thus the sign of the sine, in the physical
sense.  If there is no mistake, your differential operator works even
outside the particular domain where you are solving your problem.

Best wishes
John



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Arctan: to use with single or with double arguments?
  2010-09-25 21:34       ` Ada novice
  2010-09-26  4:02         ` jpwoodruff
@ 2010-09-26  7:31         ` Jeffrey Carter
  2010-09-26  8:24           ` Ada novice
  1 sibling, 1 reply; 16+ messages in thread
From: Jeffrey Carter @ 2010-09-26  7:31 UTC (permalink / raw)


On 09/25/2010 02:34 PM, Ada novice wrote:
>
> After reading that article, I decided to use Arctan2. I don't really
> know why Arctan exists in the first place :).

I presume because there are some cases where one has a single-valued tangent 
that my be used in Arctan (Tangent), but not X and Y values for Arctan (Y, X).

-- 
Jeff Carter
"We'll make Rock Ridge think it's a chicken
that got caught in a tractor's nuts!"
Blazing Saddles
87



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Arctan: to use with single or with double arguments?
  2010-09-26  4:02         ` jpwoodruff
@ 2010-09-26  8:21           ` Ada novice
  0 siblings, 0 replies; 16+ messages in thread
From: Ada novice @ 2010-09-26  8:21 UTC (permalink / raw)


On Sep 26, 6:02 am, jpwoodruff <jpwoodr...@gmail.com> wrote:

> So my suggestion is to use two-parameter arctan, and interpret the
> quadrant of the result, and thus the sign of the sine, in the physical
> sense.  If there is no mistake, your differential operator works even
> outside the particular domain where you are solving your problem.
>
> Best wishes
> John

Thanks. Yes it makes sense that I stick to the two-parameter arctan.
I'm working with a rotating system
and quadrant information is important. It's amazing to see
how different software treats the order of the two-parameter arctan
differently. If I'm right
C is like Ada but in MathCad, the order of the arguments is reversed.




^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Arctan: to use with single or with double arguments?
  2010-09-26  7:31         ` Jeffrey Carter
@ 2010-09-26  8:24           ` Ada novice
  2012-02-11  0:26             ` Jack Mitchell
  0 siblings, 1 reply; 16+ messages in thread
From: Ada novice @ 2010-09-26  8:24 UTC (permalink / raw)


On Sep 26, 9:31 am, Jeffrey Carter
<spam.jrcarter....@spam.not.acm.org> wrote:
> I presume because there are some cases where one has a single-valued tangent
> that my be used in Arctan (Tangent), but not X and Y values for Arctan (Y, X).
>
> --
> Jeff Carter
> "We'll make Rock Ridge think it's a chicken
> that got caught in a tractor's nuts!"
> Blazing Saddles
> 87

Yes I guess so. Even simple things can easily create confusion. The
Arctan and Arctan2 functions
are well-explained in Norman Cohen's Ada book on page 90.




^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Arctan: to use with single or with double arguments?
  2010-09-26  8:24           ` Ada novice
@ 2012-02-11  0:26             ` Jack Mitchell
  2012-02-11  1:11               ` Adam Beneschan
  0 siblings, 1 reply; 16+ messages in thread
From: Jack Mitchell @ 2012-02-11  0:26 UTC (permalink / raw)


Am I missing something - surely :-

        z = Force * SIN(ATAN2(x,y))

Is the same as :-

        z = Force * x / sqrt( x^2 + y^2 )

This should be much faster (and probably more accurate)
Perhaps I have got x & y swapped but you see my point.



In message
<dddf2cc2-640c-4878-8ce9-bf9ba13b3736@f6g2000yqa.googlegroups.com>, Ada
novice <ycalleecharan@gmx.com> writes
>On Sep 26, 9:31�am, Jeffrey Carter
><spam.jrcarter....@spam.not.acm.org> wrote:
>> I presume because there are some cases where one has a single-valued tangent
>> that my be used in Arctan (Tangent), but not X and Y values for Arctan
>>(Y, X).
>>
>> --
>> Jeff Carter
>> "We'll make Rock Ridge think it's a chicken
>> that got caught in a tractor's nuts!"
>> Blazing Saddles
>> 87
>
>Yes I guess so. Even simple things can easily create confusion. The
>Arctan and Arctan2 functions
>are well-explained in Norman Cohen's Ada book on page 90.
>

-- 
Jack Mitchell



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Arctan: to use with single or with double arguments?
  2012-02-11  0:26             ` Jack Mitchell
@ 2012-02-11  1:11               ` Adam Beneschan
  2012-02-11  8:53                 ` Simon Wright
  0 siblings, 1 reply; 16+ messages in thread
From: Adam Beneschan @ 2012-02-11  1:11 UTC (permalink / raw)


On Feb 10, 4:26 pm, Jack Mitchell <j...@home9999.plus.com> wrote:
> Am I missing something - surely :-
>
>         z = Force * SIN(ATAN2(x,y))
>
> Is the same as :-
>
>         z = Force * x / sqrt( x^2 + y^2 )
>
> This should be much faster (and probably more accurate)
> Perhaps I have got x & y swapped but you see my point.

Yep, the arguments to Arctan in Ada (and I believe the arguments to
ATAN2 in Fortran) are
(Y, X).  So what you should have said (since this is an Ada newsgroup)
is

   z := Force * Sin(Arctan(Y, X));

is the same as

   z := Force * Y / Sqrt (X**2 + Y**2);

I did have to rewrite this so that I wouldn't go nuts trying to switch
the X and Y axes on a diagram.  But it appears that you're right.  The
sign is correct, i.e. Sin(Arctan(Y, X)) is negative when Y is negative
and positive when Y is positive.  Similarly, Cos(Arctan(Y,X)) is the
same as X / Sqrt (X**2 + Y**2).

                         -- Adam





^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Arctan: to use with single or with double arguments?
  2012-02-11  1:11               ` Adam Beneschan
@ 2012-02-11  8:53                 ` Simon Wright
  2012-02-11 10:46                   ` AdaMagica
  0 siblings, 1 reply; 16+ messages in thread
From: Simon Wright @ 2012-02-11  8:53 UTC (permalink / raw)


Adam Beneschan <adam@irvine.com> writes:

> Yep, the arguments to Arctan in Ada (and I believe the arguments to
> ATAN2 in Fortran) are (Y, X).

Which presents a fine opportunity for confusion when you're dealing with
Bearing (zero at North, positive clockwise); you say Arctan (X, Y).



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Arctan: to use with single or with double arguments?
  2012-02-11  8:53                 ` Simon Wright
@ 2012-02-11 10:46                   ` AdaMagica
  2012-02-11 15:29                     ` Simon Wright
  0 siblings, 1 reply; 16+ messages in thread
From: AdaMagica @ 2012-02-11 10:46 UTC (permalink / raw)


On 11 Feb., 09:53, Simon Wright <si...@pushface.org> wrote:
> Adam Beneschan <a...@irvine.com> writes:
> > Yep, the arguments to Arctan in Ada (and I believe the arguments to
> > ATAN2 in Fortran) are (Y, X).
>
> Which presents a fine opportunity for confusion when you're dealing with
> Bearing (zero at North, positive clockwise); you say Arctan (X, Y).

In this case, you always should use named associations to make the
coordinate system clear - that's what named association is for.

In code reviews, I wouldn't let Arctan (a, b) pass, but always force
Arctan (Y => ..., X => ...).

It's a severe bug to use positional association for Arctan!



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Arctan: to use with single or with double arguments?
  2012-02-11 10:46                   ` AdaMagica
@ 2012-02-11 15:29                     ` Simon Wright
  2012-02-11 16:21                       ` Georg Bauhaus
  0 siblings, 1 reply; 16+ messages in thread
From: Simon Wright @ 2012-02-11 15:29 UTC (permalink / raw)


AdaMagica <christ-usch.grein@t-online.de> writes:

> It's a severe bug to use positional association for Arctan!

Only a bug if you get it wrong. Otherwise it's a meta-bug.

Not sure that Arctan (Y => X, X => Y) is exactly _clear_.



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Arctan: to use with single or with double arguments?
  2012-02-11 15:29                     ` Simon Wright
@ 2012-02-11 16:21                       ` Georg Bauhaus
  2012-02-11 16:47                         ` Simon Wright
  0 siblings, 1 reply; 16+ messages in thread
From: Georg Bauhaus @ 2012-02-11 16:21 UTC (permalink / raw)


On 11.02.12 16:29, Simon Wright wrote:
> AdaMagica<christ-usch.grein@t-online.de>  writes:
>
>> It's a severe bug to use positional association for Arctan!
>
> Only a bug if you get it wrong. Otherwise it's a meta-bug.
>
> Not sure that Arctan (Y =>  X, X =>  Y) is exactly _clear_.

No clarity at all when the brain switches between coordinate
system conventions, row or column major order, and similar
non-Ada ways of expressing things.  Since the semantics of
any program calling Arctan is not changed if the parameter
name X becomes <<unambiguously-denoting-what-we-mean>>, thus
since there will be an absolutely safe change set if such
a name were introduced, isn't it about time to get rid of
names that only a high school delusion holds up against?



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Arctan: to use with single or with double arguments?
  2012-02-11 16:21                       ` Georg Bauhaus
@ 2012-02-11 16:47                         ` Simon Wright
  0 siblings, 0 replies; 16+ messages in thread
From: Simon Wright @ 2012-02-11 16:47 UTC (permalink / raw)


Georg Bauhaus <rm.dash-bauhaus@futureapps.de> writes:

> On 11.02.12 16:29, Simon Wright wrote:
>> AdaMagica<christ-usch.grein@t-online.de>  writes:
>>
>>> It's a severe bug to use positional association for Arctan!
>>
>> Only a bug if you get it wrong. Otherwise it's a meta-bug.
>>
>> Not sure that Arctan (Y =>  X, X =>  Y) is exactly _clear_.
>
> No clarity at all when the brain switches between coordinate
> system conventions, row or column major order, and similar
> non-Ada ways of expressing things.  Since the semantics of
> any program calling Arctan is not changed if the parameter
> name X becomes <<unambiguously-denoting-what-we-mean>>, thus
> since there will be an absolutely safe change set if such
> a name were introduced, isn't it about time to get rid of
> names that only a high school delusion holds up against?

In Ada2012,

   with Ada.Numerics.Elementary_Functions;
   package Naval_Angles is
      function Bearing (Northings : Float;
                        Eastings  : Float) return Float is
         (Ada.Numerics.Elementary_Functions.Arctan (Y => Eastings,
                                                    X => Northings));
   end Naval_Angles;

(not sure that 'bearing' is the right word, since the same coordinate
system is used for 'heading' and 'course' ...)



^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2012-02-11 16:47 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-09-24 21:32 Arctan: to use with single or with double arguments? Ada novice
2010-09-24 21:55 ` Jeffrey Carter
2010-09-25  8:44   ` Ada novice
2010-09-25 17:31     ` Jeffrey Carter
2010-09-25 21:34       ` Ada novice
2010-09-26  4:02         ` jpwoodruff
2010-09-26  8:21           ` Ada novice
2010-09-26  7:31         ` Jeffrey Carter
2010-09-26  8:24           ` Ada novice
2012-02-11  0:26             ` Jack Mitchell
2012-02-11  1:11               ` Adam Beneschan
2012-02-11  8:53                 ` Simon Wright
2012-02-11 10:46                   ` AdaMagica
2012-02-11 15:29                     ` Simon Wright
2012-02-11 16:21                       ` Georg Bauhaus
2012-02-11 16:47                         ` Simon Wright

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox