Skip to content

Commit

Permalink
Add unary minus function for real and complex polynomials.
Browse files Browse the repository at this point in the history
  • Loading branch information
BrentSeidel committed May 25, 2024
1 parent d515329 commit d24a19c
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 11 deletions.
9 changes: 9 additions & 0 deletions src/BBS-Numerical-polynomial_complex.adb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ package body BBS.Numerical.polynomial_complex is
end;
end;
--
function "-" (Right : poly) return poly is
res : poly(Right'Range);
begin
for i in Right'Range loop
res(i) := -Right(i);
end loop;
return res;
end;
--
function "*" (Left, Right : poly) return poly is
limit : constant Natural := Left'Last + Right'Last;
begin
Expand Down
2 changes: 2 additions & 0 deletions src/BBS-Numerical-polynomial_complex.ads
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ package BBS.Numerical.polynomial_complex is
with pre => (Left'First = 0) and (Right'First = 0);
function "-" (Left, Right : poly) return poly
with pre => (Left'First = 0) and (Right'First = 0);
function "-" (Right : poly) return poly
with pre => (Right'First = 0);
function "*" (Left, Right : poly) return poly
with pre => (Left'First = 0) and (Right'First = 0);
function "*" (Left : cmplx.Complex; Right : poly) return poly
Expand Down
9 changes: 9 additions & 0 deletions src/BBS-Numerical-polynomial_real.adb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ package body BBS.Numerical.polynomial_real is
end;
end;
--
function "-" (Right : poly) return poly is
res : poly(Right'Range);
begin
for i in Right'Range loop
res(i) := -Right(i);
end loop;
return res;
end;
--
function "*" (Left, Right : poly) return poly is
limit : constant Natural := Left'Last + Right'Last;
begin
Expand Down
2 changes: 2 additions & 0 deletions src/BBS-Numerical-polynomial_real.ads
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ package BBS.Numerical.polynomial_real is
with pre => (Left'First = 0) and (Right'First = 0);
function "-" (Left, Right : poly) return poly
with pre => (Left'First = 0) and (Right'First = 0);
function "-" (Right : poly) return poly
with pre => (Right'First = 0);
function "*" (Left, Right : poly) return poly
with pre => (Left'First = 0) and (Right'First = 0);
function "*" (Left : f; Right : poly) return poly
Expand Down
2 changes: 1 addition & 1 deletion test/test_poly.adb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ begin
Ada.Text_IO.Put(" p4 = p1*p2 = ");
poly.print(p4, 1, 2, 0);
Ada.Text_IO.New_Line;
p5 := (-1.0)*p2;
p5 := -p2;
Ada.Text_IO.Put(" p5 = -p2 = ");
poly.print(p5, 1, 2, 0);
Ada.Text_IO.New_Line;
Expand Down
19 changes: 9 additions & 10 deletions test/test_poly_cmplx.adb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ procedure test_poly_cmplx is
r : cmplx.Complex;
l : cmplx.Complex;
u : cmplx.Complex;
cmOne : constant cmplx.Complex := (-1.0, 0.0); -- Complex minus one
err : croot.errors;
iter : Positive;

Expand Down Expand Up @@ -81,7 +80,7 @@ begin
Ada.Text_IO.Put(" p4 = p1*p2 = ");
cpoly.print(p4, 1, 2, 0);
Ada.Text_IO.New_Line;
p5 := cmOne*p2;
p5 := -p2;
Ada.Text_IO.Put(" p5 = -p2 = ");
cpoly.print(p5, 1, 2, 0);
Ada.Text_IO.New_Line;
Expand All @@ -98,15 +97,15 @@ begin
x := (real(i)*0.1, 0.0);
cmplx_put(x, 3, 2, 0);
Ada.Text_IO.Put(" ");
cmplx_put(cpoly.evaluate(p1, x), 3, 6, 0);
cmplx_put(cpoly.evaluate(p1, x), 2, 3, 0);
Ada.Text_IO.Put(" ");
cmplx_put(cpoly.evaluate(p2, x), 3, 6, 0);
cmplx_put(cpoly.evaluate(p2, x), 2, 3, 0);
Ada.Text_IO.Put(" ");
cmplx_put(cpoly.evaluate(p3, x), 3, 6, 0);
cmplx_put(cpoly.evaluate(p3, x), 2, 3, 0);
Ada.Text_IO.Put(" ");
cmplx_put(cpoly.evaluate(p4, x), 3, 6, 0);
cmplx_put(cpoly.evaluate(p4, x), 2, 3, 0);
Ada.Text_IO.Put(" ");
cmplx_put(cpoly.evaluate(p5, x), 3, 6, 0);
cmplx_put(cpoly.evaluate(p5, x), 2, 3, 0);
Ada.Text_IO.New_Line;
end loop;
--
Expand All @@ -118,7 +117,7 @@ begin
u := (-3.1, 0.0);
iter := 20;
r := croot.mueller(t0'Access, l, u, iter, err);
Ada.Text_IO.Put(" After " & Positive'image(iter) & " iterations, Mueller gives root at");
Ada.Text_IO.Put(" After " & Positive'image(iter) & " iterations, Mueller gives root at ");
cmplx_put(r, 2, 9, 0);
Ada.Text_IO.Put(", in range ");
cmplx_put(l, 2, 6, 0);
Expand All @@ -134,7 +133,7 @@ begin
u := (-0.9, 0.0);
iter := 13;
r := croot.mueller(t1'Access, l, u, iter, err);
Ada.Text_IO.Put(" After " & Positive'image(iter) & " iterations, Mueller gives root at");
Ada.Text_IO.Put(" After " & Positive'image(iter) & " iterations, Mueller gives root at ");
cmplx_put(r, 2, 9, 0);
Ada.Text_IO.Put(", in range ");
cmplx_put(l, 2, 6, 0);
Expand All @@ -150,7 +149,7 @@ begin
u := (0.0, 0.0);
iter := 13;
r := croot.mueller(t2'Access, l, u, iter, err);
Ada.Text_IO.Put(" After " & Positive'image(iter) & " iterations, Mueller gives root at");
Ada.Text_IO.Put(" After " & Positive'image(iter) & " iterations, Mueller gives root at ");
cmplx_put(r, 2, 9, 0);
Ada.Text_IO.Put(", in range ");
cmplx_put(l, 2, 6, 0);
Expand Down

0 comments on commit d24a19c

Please sign in to comment.