The functions in the NumTheoryQQ
file are predominantly basic
operations from number theory which involve rational numbers (rather
than just integers). Please recall that computational number theory is
not the primary remit of CoCoALib, so do not expect to find a
complete collection of operations here -- you would do better to
look at Victor Shoup's NTL (Number Theory Library), or PARI/GP,
or some other specialized library/system.
Several of these functions give errors if they are handed unsuitable values:
unless otherwise indicated below the error is of type ERR::BadArg
.
Recall that any real number has an expansion as a continued fraction (e.g. see Hardy & Wright for definition and many properties). This expansion is finite for any rational number. We adopt the following conventions which guarantee that the expansion is unique:
ContFracIter(q)
constructs a new continued fraction iterator object
IsEnded(CFIter)
true iff the iterator has moved past the last "partial quotient"
*CFIter
gives the current "partial quotient" as a ZZ
(or throws ERR::IterEnded
)
++CFIter
move to next "partial quotient" (or throws ERR::IterEnded
)
CFApproximantsIter(q)
construct a new continued fraction approximant iterator
IsEnded(CFAIter)
true iff the iterator has moved past the last "partial quotient"
*CFAIter
give the current continued fraction approximant as a QQ
(or throws ERR::IterEnded
)
++CFAIter
move to next approximant (or throws ERR::IterEnded
)
It is all pretty simple. The only tricky part is that the "end" of
the ContFracIter
is represented by both myFrac
and myQuot
being
zero. This means that a newly created iterator for zero is already ended.
CFApproximantsIter
delegates most of the work to ContFracIter
.
How should these iterators be printed out???
ContFracIter
could be rather more efficient for rationals having
very large numerator and denominator. One way would be to compute with
num and den divided by the same large factor (probably a power of 2),
and taking care to monitor how accurate these "scaled" num and den are.
I'll wait until there is a real need before implementing (as I expect
it will turn out a bit messy).
CFApproximantsIter::operator++()
should be made more efficient.