finished ContFrac implementation

This commit is contained in:
Jiri Pittner 2022-02-22 15:46:07 +01:00
parent a032344c66
commit 6c5f0eb68e
3 changed files with 178 additions and 22 deletions

View File

@ -47,15 +47,20 @@ static void cf_helper(ContFrac<T> *me, T p, T q, int level)
T div=p/q;
{
T rem=p%q;
if(rem) cf_helper(me,q,rem,level+1);
if(rem)
{
if(rem<0) {--div; rem+=q;} //prevent negative a_i i>0
cf_helper(me,q,rem,level+1);
}
else me->resize(level);
}
(*me)[level]=div;
}
template <typename T>
ContFrac<T>::ContFrac(const T p, const T q) : NRVec<T>()
ContFrac<T>::ContFrac(T p, T q) : NRVec<T>()
{
if(q<0) {p= -p; q= -q;}
cf_helper<T>(this,p,q,0);
}
@ -117,11 +122,33 @@ return x;
}
//compare assuming they are canonical
template <typename T>
T ContFrac<T>::compare(const ContFrac<T> &rhs) const
{
int l=length();
if(rhs.length()<l) l=rhs.length();
for(int i=0; i<=l; ++i)
{
T d=(*this)[i]-rhs[i];
if(d) return (i&1)? -d :d;
}
if(length()==rhs.length()) return 0;
else if(length()<rhs.length()) return (length()&1) ? 1 : -1;
else return (rhs.length()&1) ? -1 : 1;
}
template <typename T>
void ContFrac<T>::canonicalize()
{
int n=this->length();
if(n==0) return;
if(n>0 && (*this)[1]<0) //handle negative a_i i>0
{
for(int i=0; i<=n; ++i) (*this[i]) = -(*this[i]);
*this = -(*this);
}
this->copyonwrite();
if((*this)[n]==1) {(*this)[n]=0; ++(*this)[n-1];} //avoid deepest 1/1
for(int i=1; i<=n; ++i) //truncate if possible
@ -167,13 +194,18 @@ return hnew;
template <typename T>
bool Homographic<T>::outputready(T &z) const
bool Homographic<T>::outputready(T &z,bool first) const
{
bool inf=0;
T q0,q1;
if(v[1][0]==0) inf=1; else q0=v[0][0]/v[1][0];
if(v[1][1]==0) inf=1; else q1=v[0][1]/v[1][1];
if(!inf && q0==q1) {z=q0; return true;}
if(!inf && q0==q1)
{
z=q0;
if(first && q0<0) --z; //prevent negative a1 etc.
return true;
}
return false;
}
@ -190,6 +222,7 @@ ContFrac<T> Homographic<T>::value(const ContFrac<T>&x) const
Homographic<T> h(*this);
std::list<T> l;
bool first=true;
for(typename ContFrac<T>::iterator px=x.begin(); px!=x.beyondend(); ++px)
{
//digest next input term
@ -197,10 +230,11 @@ for(typename ContFrac<T>::iterator px=x.begin(); px!=x.beyondend(); ++px)
//output as much as possible
T out;
while(h.outputready(out))
while(h.outputready(out,first))
{
l.push_back(out);
h=h.output(out);
first=false;
}
//terminate if exhausted
@ -210,6 +244,11 @@ for(typename ContFrac<T>::iterator px=x.begin(); px!=x.beyondend(); ++px)
break;
}
}
if(l.back()==1) //simplify by removing a trailing 1
{
l.pop_back();
l.back()+=1;
}
return ContFrac<T>(l);
}
@ -273,7 +312,7 @@ return 1;
template <typename T>
bool BiHomographic<T>::outputready(T &z) const
bool BiHomographic<T>::outputready(T &z,bool first) const
{
T q[2][2];
for(int i=0; i<2; ++i) for(int j=0; j<2; ++j)
@ -283,6 +322,7 @@ for(int i=0; i<2; ++i) for(int j=0; j<2; ++j)
if(q[i][j]!=q[0][0]) return false;
}
z=q[0][0];
if(first && z<0) --z;
return true;
}
@ -303,6 +343,7 @@ std::list<T> l;
typename ContFrac<T>::iterator px=x.begin();
typename ContFrac<T>::iterator py=y.begin();
bool first=true;
do
{
//select next input term
@ -316,10 +357,11 @@ do
//output as much as possible
T out;
while(h.outputready(out))
while(h.outputready(out,first))
{
l.push_back(out);
h=h.output(out);
first=false;
}
//terminate if exhausted
@ -330,6 +372,13 @@ do
}
}
while(px!=x.beyondend() || py!=y.beyondend());
if(l.back()==1) //simplify by removing a trailing 1
{
l.pop_back();
l.back()+=1;
}
return ContFrac<T>(l);
}
@ -344,7 +393,7 @@ if(den<0)
den= -den;
}
T g=gcd(num,den);
if(MYABS(g)>1)
if(g>1)
{
num/=g;
den/=g;
@ -381,6 +430,7 @@ return *this;
}
//try avoiding overflows at the cost of speed
template <typename T>
Rational<T> Rational<T>::operator+(const Rational &rhs) const
{
@ -426,6 +476,45 @@ return *this;
}
//unary -
template <typename T>
ContFrac<T> ContFrac<T>::operator-() const
{
int l=length();
if(l==0)
{
ContFrac<T> r(0);
r[0]= -(*this)[0];
return r;
}
if((*this)[1]!=1)
{
ContFrac<T> r(l+1);
r[0]= -(*this)[0]-1;
r[1]= 1;
r[2]= (*this)[1]-1;
for(int i=2; i<=l; ++i) r[i+1] = (*this)[i];
return r;
}
else //a_1-1 == 0
{
if(l==1) //we have trailing 0, actually the input was not canonical
{
ContFrac<T> r(0);
r[0]= -(*this)[0]-1;
return r;
}
else
{
ContFrac<T> r(l-1);
r[0]= -(*this)[0]-1;
r[1]= 1+(*this)[2];
for(int i=3; i<=l; ++i) r[i-1] = (*this)[i];
return r;
}
}
}
/***************************************************************************//**

View File

@ -25,17 +25,16 @@
namespace LA {
//simple finite continued fraction class
//Support for rationals and a simple finite continued fraction class
//NOTE: 0 on any position >0 means actually infinity; simplify() shortens the vector
//presently implements just conversion to/from rationals and floats
//maybe implement arithmetic by Gosper's method cf. https://perl.plover.com/classes/cftalk/TALK
//
//includes Gosper's arithmetics - cf. https://perl.plover.com/classes/cftalk/TALK
//maybe implement the self-feeding Gosper's algorithm for sqrt(int)
//maybe do not interpret a_i=0 i>0 as termination???
template <typename T>
class ContFrac;
//@@@operator== > >= etc.
template <typename T>
class Rational {
public:
@ -45,7 +44,7 @@ public:
Rational() {};
Rational(const T p, const T q) : num(p),den(q) {};
explicit Rational(const T (&a)[2]) :num(a[0]), den(a[1]) {};
Rational(const ContFrac<T> &cf) {cf.convergent(&num,&den);};
explicit Rational(const ContFrac<T> &cf) {cf.convergent(&num,&den);};
void simplify();
//basic rational arithmetics
@ -67,7 +66,21 @@ public:
Rational & operator+=(const Rational &rhs) {*this = *this+rhs; return *this;};
Rational & operator-=(const Rational &rhs) {*this = *this-rhs; return *this;};
//combination with continued fractions
ContFrac<T> operator+(const ContFrac<T> &rhs) const {return rhs + *this;};
ContFrac<T> operator-(const ContFrac<T> &rhs) const {return -rhs + *this;};
ContFrac<T> operator*(const ContFrac<T> &rhs) const {return rhs * *this;};
ContFrac<T> operator/(const ContFrac<T> &rhs) const {return rhs.reciprocal() * *this;};
//relational operators, relying that operator- yields a form with a positive denominator
bool operator==(const Rational &rhs) const {Rational t= *this-rhs; return t.num==0;};
bool operator!=(const Rational &rhs) const {Rational t= *this-rhs; return t.num!=0;};
bool operator>=(const Rational &rhs) const {Rational t= *this-rhs; return t.num>=0;};
bool operator<=(const Rational &rhs) const {Rational t= *this-rhs; return t.num<=0;};
bool operator>(const Rational &rhs) const {Rational t= *this-rhs; return t.num>0;};
bool operator<(const Rational &rhs) const {Rational t= *this-rhs; return t.num<0;};
};
template <typename T>
@ -96,7 +109,6 @@ class BiHomographic;
//@@@operator== > >= etc.
template <typename T>
class ContFrac : public NRVec<T> {
private:
@ -106,16 +118,17 @@ public:
template<int SIZE> ContFrac(const T (&a)[SIZE]) : NRVec<T>(a) {};
ContFrac(const NRVec<T> &v) : NRVec<T>(v) {}; //allow implicit conversion from NRVec
ContFrac(const int n) : NRVec<T>(n+1) {};
ContFrac(double x, const int n, const T thres=0); //might yield a non-canonical form
explicit ContFrac(double x, const int n, const T thres=0); //WARNING: it might yield a non-canonical form
//we could make a template for analogous conversion from an arbitrary-precision type
ContFrac(const T p, const T q); //should yield a canonical form
ContFrac(const Rational<T> &r) : ContFrac(r.num,r.den) {};
ContFrac(T p, T q); //should yield a canonical form
explicit ContFrac(const Rational<T> &r) : ContFrac(r.num,r.den) {};
void canonicalize();
void convergent(T *p, T*q, const int trunc= -1) const;
Rational<T> rational(const int trunc= -1) const {T p,q; convergent(&p,&q,trunc); return Rational<T>(p,q);};
double value(const int trunc= -1) const; //we could make also a template usable with an arbitrary-precision type
ContFrac reciprocal() const;
ContFrac operator-() const; //unary minus
int length() const {return NRVec<T>::size()-1;};
void resize(const int n, const bool preserve=true)
{
@ -123,6 +136,7 @@ public:
NRVec<T>::resize(n+1,preserve);
if(preserve) for(int i=nold+1; i<=n;++i) (*this)[i]=0;
}
//arithmetics with a single ContFrac operand
ContFrac operator+(const Rational<T> &rhs) const {Homographic<T> h({{rhs.num,rhs.den},{rhs.den,0}}); return h.value(*this);};
ContFrac operator-(const Rational<T> &rhs) const {Homographic<T> h({{-rhs.num,rhs.den},{rhs.den,0}}); return h.value(*this);};
@ -142,6 +156,16 @@ public:
ContFrac operator*(const ContFrac &rhs) const {BiHomographic<T> h({{{0,0},{0,1}},{{1,0},{0,0}}}); return h.value(*this,rhs);};
ContFrac operator/(const ContFrac &rhs) const {BiHomographic<T> h({{{0,1},{0,0}},{{0,0},{1,0}}}); return h.value(*this,rhs);};
//relational operators, guaranteed only to work correctly for canonicalized CF!
T compare(const ContFrac &rhs) const;
bool operator==(const ContFrac &rhs) const {return compare(rhs)==0;};
bool operator>(const ContFrac &rhs) const {return compare(rhs)>0;};
bool operator<(const ContFrac &rhs) const {return rhs.operator>(*this);};
bool operator!=(const ContFrac &rhs) const {return ! this->operator==(rhs) ;}
bool operator<=(const ContFrac &rhs) const {return ! this->operator>(rhs) ;}
bool operator>=(const ContFrac &rhs) const {return ! this->operator<(rhs) ;}
//iterator
class iterator {
private:
@ -176,7 +200,7 @@ T v[2][2]; //{{a,b},{c,d}} for (a+b.x)/(c+d.x) i.e. [denominator][power_x]
Homographic input(const T &x, const bool inf) const;
Homographic output(const T &x) const;
bool outputready(T &x) const;
bool outputready(T &x, bool first) const;
bool terminate() const;
};
@ -195,7 +219,7 @@ T v[2][2][2]; //{{{a,b},{c,d}},{{e,f},{g,h}}} i.e.[denominator][power_y][power_
BiHomographic inputy(const T &y, const bool inf) const;
BiHomographic output(const T &z) const;
int inputselect() const;
bool outputready(T &x) const;
bool outputready(T &x,bool first) const;
bool terminate() const;
};

45
t.cc
View File

@ -2483,7 +2483,7 @@ ContFrac<int> z= x*Rational<int>({2,3});
cout<<Rational<int>(z)<<endl;
}
if(1)
if(0)
{
ContFrac<int> x(11,101);
ContFrac<int> v(3,7);
@ -2497,6 +2497,49 @@ cout<<Rational<int>(zz)<<endl;
cout<<(Rational<int>(x)+3)*(Rational<int>(v)+4)/(Rational<int>(x)-Rational<int>(v))<<endl;
}
if(0)
{
double x;
cin >>x;
ContFrac<int> xx(x,15,100000);
cout <<xx<<endl;
ContFrac<int> xm= -xx;
cout<< xm<<endl;
cout<<xx+xm<<endl;
}
if(0)
{
Rational<int> x;
cin >>x;
ContFrac<int> xx(x);
cout<<xx;
ContFrac<int> xm= -xx;
cout<< xm;
ContFrac<int> yy(-x);
cout<<yy;
ContFrac<int> ym= -yy;
cout<< ym;
cout <<"ZEROs\n"<<xx+xm<<" "<<yy+ym<<" "<<xx+yy<<" "<<xm+ym<<endl;
}
if(1)
{
double x;
cin >>x;
ContFrac<int> xx(x,25,100000);
ContFrac<int> xx1(x+1e-4,25,100000);
ContFrac<int> xx2(x-1e-4,25,100000);
xx.canonicalize();
xx1.canonicalize();
xx2.canonicalize();
cout<<"small "<<xx2<<endl;
cout<<"middle "<<xx<<endl;
cout<<"big "<<xx1<<endl;
cout << "TEST "<<(xx<xx1) <<" "<<(xx>xx2) <<endl;
}
}