Skip to content

Commit

Permalink
Update handling of generic parameters to generic function for complex…
Browse files Browse the repository at this point in the history
… roots.
  • Loading branch information
BrentSeidel committed May 19, 2024
1 parent 83b7113 commit 316baeb
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/BBS-Numerical-roots_complex.adb
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ package body BBS.Numerical.roots_complex is
e : cmplx.complex;
root : cmplx.complex;
temp : cmplx.complex;
nTwo : constant cmplx.complex := (r => -2.0, i => 0.0);
nTwo : constant cmplx.complex := (-2.0)*cmplx.one;
atemp : ada_cmplx.Complex;
begin
err := none;
for i in 0 .. limit loop
b := delta2 + (step2 * d_small);
discriminant := b*b - (4.0*d_small*test(x2));
atemp := (discriminant.r, discriminant.i);
atemp := (roots_complex.F(discriminant.r), roots_complex.F(discriminant.i));
atemp := cmplx_elem.Sqrt(atemp);
d_big := (r => ada_cmplx.Re(atemp), i => ada_cmplx.Im(atemp));
d_big := (r => cmplx.F(ada_cmplx.Re(atemp)), i => cmplx.F(ada_cmplx.Im(atemp)));
if cmplx.magnitude(b - d_big) < cmplx.magnitude(b + d_big) then
e := b + d_big;
else
Expand Down
5 changes: 3 additions & 2 deletions src/BBS-Numerical-roots_complex.ads
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
with BBS.Numerical.complex_real;
generic
type F is digits <>;
with package cmplx is new BBS.Numerical.complex_real(<>);
use type cmplx.complex;
use type cmplx.F;
package BBS.Numerical.roots_complex is
package cmplx is new BBS.Numerical.complex_real(f'Base);
use type cmplx.complex;
type errors is (none, bad_args, no_solution);
type test_func is access function (x : cmplx.complex) return cmplx.complex;
--
Expand Down
3 changes: 3 additions & 0 deletions src/BBS-Numerical-vector_real.ads
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ package BBS.Numerical.vector_real is
with pre => (Left'First = Right'First) and (Left'Last = Right'Last);
function "-" (Left, Right : vect) return vect
with pre => (Left'First = Right'First) and (Left'Last = Right'Last);
--
-- Vector dot product
--
function "*" (Left, Right : vect) return f'Base
with pre => (Left'First = Right'First) and (Left'Last = Right'Last);
function "*" (Left : f; Right : vect) return vect;
Expand Down
15 changes: 8 additions & 7 deletions test/test_root.adb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ with BBS.Numerical.roots_complex;

procedure test_root is
subtype real is Long_Float;
package cmplx is new BBS.Numerical.complex_real(real);
use type cmplx.complex;
package root is new BBS.Numerical.roots_real(real);
package croot is new BBS.Numerical.roots_complex(real);
use type croot.cmplx.complex;
package croot is new BBS.Numerical.roots_complex(F => real, cmplx => cmplx);
package float_io is new Ada.Text_IO.Float_IO(real);
package elem is new Ada.Numerics.Generic_Elementary_Functions(real);

Expand All @@ -25,19 +26,19 @@ procedure test_root is
--
-- This function has no real roots - only two complex ones.
--
function f3(x : croot.cmplx.Complex) return croot.cmplx.Complex is
function f3(x : cmplx.Complex) return cmplx.Complex is
begin
return x*x + croot.cmplx.one;
return x*x + cmplx.one;
end;

r : real;
l : real;
u : real;
err : root.errors;
cerr : croot.errors;
cr : croot.cmplx.Complex;
cl : croot.cmplx.Complex;
cu : croot.cmplx.Complex;
cr : cmplx.Complex;
cl : cmplx.Complex;
cu : cmplx.Complex;
begin
Ada.Text_IO.Put_Line("Testing some of the numerical routines.");
--
Expand Down

0 comments on commit 316baeb

Please sign in to comment.