comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: X : Real_Vector := Solve(A => A, X => B); -- Why X as argument name?
Date: Mon, 24 Dec 2012 10:33:13 +0200
Date: 2012-12-24T10:33:13+02:00	[thread overview]
Message-ID: <ajqia9FfiahU1@mid.individual.net> (raw)
In-Reply-To: <871uegmmiq.fsf@katthult.thorslund.org>

On 12-12-24 00:28 , Gustaf Thorslund wrote:
> -- why_x.adb by confused Gustaf Thorslund
> 
> with Ada.Numerics.Real_Arrays; use Ada.Numerics.Real_Arrays;
> with Ada.Text_IO; use Ada.Text_IO;
> 
> procedure Why_X is
>    -- Let's say we have an equation Ax = b according to the notation
>    from
>    -- http://en.wikipedia.org/wiki/Matrix_(mathematics)#Linear_equations
>    -- where A and b are known while x is unknown.
>    
>    A : Real_Matrix(1..1, 1..1) := (others => (others => 2.0));
>    B : Real_Vector(1..1) := (others => 12.0);
>    
>    -- x is found using 
>    -- function Solve (A : Real_Matrix; X : Real_Vector) return
>    -- Real_Vector;
>    X : Real_Vector := Solve(A => A, X => B); -- Why X as argument name?


Or you could ask, why use "X" as the name of a local variable?

>    
> begin
>    for I in X'Range loop
>       Put_Line(Float'Image(X(I)));
>    end loop;
> end Why_X;
> 
> -- Executing the program gives
> -- $ ./why_x 
> --  6.00000E+00
> -- So we found the expected value of x.

Do you see that as a problem? :-)

> -- Was it a misstake to use X as argument name in Solve?

Do you mean, is it an error that your program uses "X" as the name of a
local variable, when it also calls a function that has a formal
parameter called "X"?

No, it is not an error and is quite legal. The compiler knows from the
named-association syntax ("X => ...") that "X" refers to the name of a
formal parameter.

Or do you mean to ask if it was a mistake in the definition of
Ada.Numerics.Real_Arrays.Solve to give the second formal parameter the
name "X", given that "X" is a common name in client code?

That is a matter of coding style and habit. (I would probably have
chosen the name "B" for the second parameter, since "X" suggests
"unknowns" to me, while "A" and "B" suggest "knowns".) And most coding
style guidelines recommend against single-letter names like "X" in
application programs, so "X" should not really be a common name.

Or do you mean to ask if it is mistake that your program uses the name
"X" for a local variable, which the reader can confuse with the formal
parameter "X"?

If you have no important reason to call your local variable "X", it
would be slightly clearer to choose a different name, at least for
readers not yet familiar with Ada.

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
      .      @       .



  reply	other threads:[~2012-12-24  8:33 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-12-23 22:28 X : Real_Vector := Solve(A => A, X => B); -- Why X as argument name? Gustaf Thorslund
2012-12-24  8:33 ` Niklas Holsti [this message]
2012-12-24 11:50   ` Simon Wright
2012-12-24 12:58     ` Niklas Holsti
2012-12-24 21:25     ` Gustaf Thorslund
2012-12-24 20:42   ` Gustaf Thorslund
2012-12-24 20:10 ` jpwoodruff
replies disabled

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