Continued fraction/Arithmetic/G(matrix ng, continued fraction n1, continued fraction n2): Difference between revisions

Line 432:
extern fn cf_get_at_size : {i : int} (cf, size_t i) -> integer
extern fn cf_get_at_int : {i : nat} (cf, int i) -> integer
 
extern fn cf2generator : cf -> cf_generator
 
(* The precedence of the overloads has to exceed that of ref[] *)
Line 469 ⟶ 471:
 
implement cf_get_at_int (cf, i) = cf_get_at_size (cf, g1i2u i)
 
implement
cf2generator cf =
let
val state_refi : ref stateSize_t = ref (i2sz 0)
in
lam () =>
let
val term = xsource cf[!i]
in
!i := succ !i;
val term = ysource j
end
end
 
end (* local *)
Line 781 ⟶ 797:
cf_make
let
typedefval stateng : ref ng8 = '(ref ng8,
and xsource : ref cf_generator = ref (cf2generator x)
Size_t -<cloref1> integer,
and ysource : ref cf_generator = ref (cf2generator y)
Size_t -<cloref1> integer,
Size_t,
Size_t)
 
fn xsource (i : Size_t) :<cloref1> integer = x[i]
fn ysource (i : Size_t) :<cloref1> integer = y[i]
 
fn neginf_source (i : Size_t) :<cloref1> integer =
neginf<integerknd> ()
 
fn xsourceneginf_source (i : Size_t) :<cloref1> integer = x[i]neginf<integerknd> ()
val state_ref : ref state =
ref '(ng8, xsource, ysource, i2sz 0, i2sz 0)
in
lam () =<cloref1>
let
fnx
looprecurs () : integer =
let
val '(@(a12, a1, a2, a, b12, b1, b2, b), = !ng
val bz = iseqz xsource, ysource, i, j) = !state_refb
and b1z = iseqz Size_t,b1
and b2z = iseqz Size_t)b2
and b12z = iseqz loop ()b12
in
if iseqz b12b12z * iseqz b1b1z * iseqz b2b2z * iseqz bbz then
neginf<integerknd> ()
else if iseqz bbz * iseqz b2b2z then
absorb_x_term ()
else if iseqz bbz + iseqz b2b2z then
absorb_y_term ()
else if iseqz b1b1z then
absorb_x_term ()
else
Line 818 ⟶ 828:
and @(q2, r2) = a2 divrem b2
and @(q12, r12) =
(if isneqz b12~b12z then
a12 divrem b12
else
@(neginf (), neginf ())) : @(integer, integer)
in
if (isneqz~b12z) b12* (q = q1) * (q = q2) * (q = q12) then
* (q = q1) * (q = q2) * (q = q12) then
begin (* Output a term. *)
!state_refng := @(b12, b1, b2, b, r12, r1, r2, r);
'(@(b12, b1, b2, b, r12, r1, r2, r),
xsource, ysource, i, j);
q
end
Line 849 ⟶ 856:
absorb_x_term () : integer =
let
val '(@(a12, a1, a2, a, b12, b1, b2, b), = !ng
val term = (!xsource,) ysource, i, j() = !state_ref
val term = xsource i
in
if term <> neginf<integerknd> () then
Line 857 ⟶ 863:
try
let
val a12a12_ = a2 +! (a12 *! term)
and a1a1_ = a +! (a1 *! term)
and a2a2_ = a12
and aa_ = a1
and b12b12_ = b2 +! (b12 *! term)
and b1b1_ = b +! (b1 *! term)
and b2b2_ = b12
and bb_ = b1
in
!state_refng := @(a12_, a1_, a2_, a_,
'(@(a12, a1, a2, a, b12 b12_, b1b1_, b2b2_, bb_),;
xsource, ysource, succ i, j);
(* Be aware: this is not a tail recursion! *)
looprecurs ()
end
with
Line 877 ⟶ 882:
(* Replace the sources with ones that return no
terms. (You have to replace BOTH sources.) *)
!state_refng := @(a12, a1, a12, a1, b12, b1, b12, b1);
!xsource := '(@(a12, a1, a12, a1, b12, b1, b12, b1),neginf_source;
!ysource := neginf_source, neginf_source, succ i, j);
looprecurs ()
end
end
else
begin
!state_refng := @(a12, a1, a12, a1, b12, b1, b12, b1);
recurs '(@(a12, a1, a12, a1, b12, b1, b12, b1),
xsource, ysource, succ i, j);
loop ()
end
end
Line 894 ⟶ 897:
absorb_y_term () : integer =
let
val '(@(a12, a1, a2, a, b12, b1, b2, b), = !ng
val term = xsource, (!ysource,) i, j() = !state_ref
val term = ysource j
in
if term <> neginf<integerknd> () then
Line 902 ⟶ 904:
try
let
val a12a12_ = a1 +! (a12 *! term)
and a1a1_ = a12
and a2a2_ = a +! (a2 *! term)
and aa_ = a2
and b12b12_ = b1 +! (b12 *! term)
and b1b1_ = b12
and b2b2_ = b +! (b2 *! term)
and bb_ = b2
in
!state_refng := @(a12_, a1_, a2_, a_,
'(@(a12, a1, a2, a, b12 b12_, b1b1_, b2b2_, bb_),;
xsource, ysource, i, succ j);
(* Be aware: this is not a tail recursion! *)
looprecurs ()
end
with
Line 922 ⟶ 923:
(* Replace the sources with ones that return no
terms. (You have to replace BOTH sources.) *)
!state_refng := @(a12, a12, a2, a2, b12, b12, b2, b2);
!xsource := '(@(a12, a12, a2, a2, b12, b12, b2, b2),neginf_source;
!ysource := neginf_source, neginf_source, i, succ j);
looprecurs ()
end
end
else
begin
!state_refng := @(a12, a12, a2, a2, b12, b12, b2, b2);
recurs '(@(a12, a12, a2, a2, b12, b12, b2, b2),
xsource, ysource, i, succ j);
loop ()
end
end
in
looprecurs ()
end
end
1,448

edits