working on contfrac
This commit is contained in:
parent
e67e6a5797
commit
b4aaa77da4
114
contfrac.cc
114
contfrac.cc
@ -17,6 +17,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "contfrac.h"
|
#include "contfrac.h"
|
||||||
|
#include "permutation.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
@ -121,6 +122,7 @@ void ContFrac<T>::canonicalize()
|
|||||||
{
|
{
|
||||||
int n=this->length();
|
int n=this->length();
|
||||||
if(n==0) return;
|
if(n==0) return;
|
||||||
|
this->copyonwrite();
|
||||||
if((*this)[n]==1) {(*this)[n]=0; ++(*this)[n-1];} //avoid deepest 1/1
|
if((*this)[n]==1) {(*this)[n]=0; ++(*this)[n-1];} //avoid deepest 1/1
|
||||||
for(int i=1; i<=n; ++i) //truncate if possible
|
for(int i=1; i<=n; ++i) //truncate if possible
|
||||||
{
|
{
|
||||||
@ -139,11 +141,13 @@ ContFrac<T> Homographic<T>::value(const ContFrac<T>&x) const
|
|||||||
Homographic<T> h(*this);
|
Homographic<T> h(*this);
|
||||||
|
|
||||||
std::list<T> l;
|
std::list<T> l;
|
||||||
for(int i=0; i<=x.length()+1; ++i) //scan all input
|
typename ContFrac<T>::iterator px=x.begin();
|
||||||
|
|
||||||
|
do //scan all input
|
||||||
{
|
{
|
||||||
//digest next input term
|
//digest next input term
|
||||||
Homographic<T> hnew;
|
Homographic<T> hnew;
|
||||||
if(i>x.length()||i!=0&&x[i]==0) //input is infinity
|
if(px==x.end()|| px!=x.begin()&& *px==0) //input is infinity
|
||||||
{
|
{
|
||||||
hnew.x[0][0]=hnew.x[0][1]=h.x[0][1];
|
hnew.x[0][0]=hnew.x[0][1]=h.x[0][1];
|
||||||
hnew.x[1][0]=hnew.x[1][1]=h.x[1][1];
|
hnew.x[1][0]=hnew.x[1][1]=h.x[1][1];
|
||||||
@ -152,8 +156,8 @@ for(int i=0; i<=x.length()+1; ++i) //scan all input
|
|||||||
{
|
{
|
||||||
hnew.x[0][0]=h.x[0][1];
|
hnew.x[0][0]=h.x[0][1];
|
||||||
hnew.x[1][0]=h.x[1][1];
|
hnew.x[1][0]=h.x[1][1];
|
||||||
hnew.x[0][1]=h.x[0][0]+h.x[0][1]*x[i];
|
hnew.x[0][1]=h.x[0][0]+h.x[0][1]* *px;
|
||||||
hnew.x[1][1]=h.x[1][0]+h.x[1][1]*x[i];
|
hnew.x[1][1]=h.x[1][0]+h.x[1][1]* *px;
|
||||||
}
|
}
|
||||||
|
|
||||||
//std::cout<<"hnew\n"<< hnew.x[0][0]<<" "<< hnew.x[0][1]<<"\n"<< hnew.x[1][0]<<" "<< hnew.x[1][1]<<"\n";
|
//std::cout<<"hnew\n"<< hnew.x[0][0]<<" "<< hnew.x[0][1]<<"\n"<< hnew.x[1][0]<<" "<< hnew.x[1][1]<<"\n";
|
||||||
@ -182,21 +186,119 @@ for(int i=0; i<=x.length()+1; ++i) //scan all input
|
|||||||
if(hnew.x[1][0]==0&&hnew.x[1][1]==0) //terminate
|
if(hnew.x[1][0]==0&&hnew.x[1][1]==0) //terminate
|
||||||
{
|
{
|
||||||
//std::cout<<"terminate at "<<i<<"\n";
|
//std::cout<<"terminate at "<<i<<"\n";
|
||||||
if(i<=x.length()) laerror("unexpected termination in Homographic::value");
|
if(px!=x.end()) laerror("unexpected termination in Homographic::value");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
h=hnew;
|
h=hnew;
|
||||||
}
|
++px;
|
||||||
|
} while(px!=x.beyondend());
|
||||||
//std::cout <<"size= "<<l.size()<<std::endl;
|
//std::cout <<"size= "<<l.size()<<std::endl;
|
||||||
return ContFrac<T>(l);
|
return ContFrac<T>(l);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void Rational<T>::simplify()
|
||||||
|
{
|
||||||
|
if(den<0)
|
||||||
|
{
|
||||||
|
num= -num;
|
||||||
|
den= -den;
|
||||||
|
}
|
||||||
|
T g=gcd(num,den);
|
||||||
|
if(g>1)
|
||||||
|
{
|
||||||
|
num/=g;
|
||||||
|
den/=g;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
Rational<T> & Rational<T>::operator*=(const T &rhs)
|
||||||
|
{
|
||||||
|
T r=rhs;
|
||||||
|
T g=gcd(r,den);
|
||||||
|
if(g>1)
|
||||||
|
{
|
||||||
|
r/=g;
|
||||||
|
den/=g;
|
||||||
|
}
|
||||||
|
num*=r;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
Rational<T> & Rational<T>::operator/=(const T &rhs)
|
||||||
|
{
|
||||||
|
T r=rhs;
|
||||||
|
T g=gcd(r,num);
|
||||||
|
if(g>1)
|
||||||
|
{
|
||||||
|
r/=g;
|
||||||
|
num/=g;
|
||||||
|
}
|
||||||
|
den*=r;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
Rational<T> Rational<T>::operator+(const Rational &rhs) const
|
||||||
|
{
|
||||||
|
Rational r;
|
||||||
|
r.den = lcm(den,rhs.den);
|
||||||
|
r.num = num*(r.den/den) + rhs.num*(r.den/rhs.den);
|
||||||
|
r.simplify();
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
Rational<T> Rational<T>::operator-(const Rational &rhs) const
|
||||||
|
{
|
||||||
|
Rational r;
|
||||||
|
r.den = lcm(den,rhs.den);
|
||||||
|
r.num = num*(r.den/den) - rhs.num*(r.den/rhs.den);
|
||||||
|
r.simplify();
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
Rational<T> & Rational<T>::operator*=(const Rational &rhs)
|
||||||
|
{
|
||||||
|
Rational r(rhs);
|
||||||
|
T g;
|
||||||
|
g=gcd(num,r.den);
|
||||||
|
if(g>1)
|
||||||
|
{
|
||||||
|
num/=g;
|
||||||
|
r.den/=g;
|
||||||
|
}
|
||||||
|
g=gcd(den,r.num);
|
||||||
|
if(g>1)
|
||||||
|
{
|
||||||
|
den/=g;
|
||||||
|
r.num/=g;
|
||||||
|
}
|
||||||
|
num*=r.num;
|
||||||
|
den*=r.den;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/***************************************************************************//**
|
/***************************************************************************//**
|
||||||
* forced instantization in the corresponding object file
|
* forced instantization in the corresponding object file
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
|
template class Rational<int>;
|
||||||
|
template class Rational<unsigned int>;
|
||||||
|
template class Rational<long>;
|
||||||
|
template class Rational<unsigned long>;
|
||||||
|
template class Rational<long long>;
|
||||||
|
template class Rational<unsigned long long>;
|
||||||
|
|
||||||
template class ContFrac<int>;
|
template class ContFrac<int>;
|
||||||
template class ContFrac<unsigned int>;
|
template class ContFrac<unsigned int>;
|
||||||
template class ContFrac<long>;
|
template class ContFrac<long>;
|
||||||
|
72
contfrac.h
72
contfrac.h
@ -34,16 +34,38 @@ namespace LA {
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
class ContFrac;
|
class ContFrac;
|
||||||
|
|
||||||
//@@@basic rational arithmetics
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class Rational {
|
class Rational {
|
||||||
public:
|
public:
|
||||||
T num;
|
T num;
|
||||||
T den;
|
T den;
|
||||||
|
|
||||||
|
Rational() {};
|
||||||
Rational(const T p, const T q) : num(p),den(q) {};
|
Rational(const T p, const T q) : num(p),den(q) {};
|
||||||
explicit Rational(const T (&a)[2]) :num(a[0]), den(a[1]) {};
|
explicit Rational(const T (&a)[2]) :num(a[0]), den(a[1]) {};
|
||||||
Rational(const ContFrac<T> &cf) {cf.convergent(&num,&den);};
|
Rational(const ContFrac<T> &cf) {cf.convergent(&num,&den);};
|
||||||
|
void simplify();
|
||||||
|
|
||||||
|
//basic rational arithmetics
|
||||||
|
Rational operator-() const {return Rational(-num,den);};
|
||||||
|
Rational & operator+=(const T &rhs) {num+=den*rhs; return *this;};
|
||||||
|
Rational & operator-=(const T &rhs) {num-=den*rhs; return *this;};
|
||||||
|
Rational & operator*=(const T &rhs);
|
||||||
|
Rational & operator/=(const T &rhs);
|
||||||
|
Rational operator+(const T &rhs) const {Rational r(*this); return r+=rhs;};
|
||||||
|
Rational operator-(const T &rhs) const {Rational r(*this); return r-=rhs;};
|
||||||
|
Rational operator*(const T &rhs) const {Rational r(*this); return r*=rhs;};
|
||||||
|
Rational operator/(const T &rhs) const {Rational r(*this); return r/=rhs;};
|
||||||
|
Rational & operator*=(const Rational &rhs);
|
||||||
|
Rational & operator/=(const Rational &rhs) {return (*this)*=Rational(rhs.den,rhs.num);};
|
||||||
|
Rational operator+(const Rational &rhs) const;
|
||||||
|
Rational operator-(const Rational &rhs) const;
|
||||||
|
Rational operator*(const Rational &rhs) const {Rational r(*this); return r*=rhs;};
|
||||||
|
Rational operator/(const Rational &rhs) const {Rational r(*this); return r/=rhs;};
|
||||||
|
Rational & operator+=(const Rational &rhs) {*this = *this+rhs; return *this;};
|
||||||
|
Rational & operator-=(const Rational &rhs) {*this = *this-rhs; return *this;};
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
@ -53,6 +75,16 @@ s<<x.num<<"/"<<x.den;
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
std::istream & operator>>(std::istream &s, Rational<T> &x)
|
||||||
|
{
|
||||||
|
char c;
|
||||||
|
s>>x.num>>c>>x.den;
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class Homographic;
|
class Homographic;
|
||||||
|
|
||||||
@ -60,7 +92,6 @@ template <typename T>
|
|||||||
class BiHomographic;
|
class BiHomographic;
|
||||||
|
|
||||||
|
|
||||||
//@@@implement iterator and rewrite Homographic<T>::value
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class ContFrac : public NRVec<T> {
|
class ContFrac : public NRVec<T> {
|
||||||
@ -71,14 +102,15 @@ public:
|
|||||||
template<int SIZE> ContFrac(const T (&a)[SIZE]) : NRVec<T>(a) {};
|
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 NRVec<T> &v) : NRVec<T>(v) {}; //allow implicit conversion from NRVec
|
||||||
ContFrac(const int n) : NRVec<T>(n+1) {};
|
ContFrac(const int n) : NRVec<T>(n+1) {};
|
||||||
ContFrac(double x, const int n, const T thres=0); //might yield a non-canonical form
|
ContFrac(double x, const int n, const T thres=0); //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 T p, const T q); //should yield a canonical form
|
||||||
ContFrac(const Rational<T> &r) : ContFrac(r.num,r.den) {};
|
ContFrac(const Rational<T> &r) : ContFrac(r.num,r.den) {};
|
||||||
|
|
||||||
void canonicalize();
|
void canonicalize();
|
||||||
void convergent(T *p, T*q, const int trunc= -1) const;
|
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);};
|
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;
|
double value(const int trunc= -1) const; //we could make also a template usable with an arbitrary-precision type
|
||||||
ContFrac reciprocal() const;
|
ContFrac reciprocal() const;
|
||||||
int length() const {return NRVec<T>::size()-1;};
|
int length() const {return NRVec<T>::size()-1;};
|
||||||
void resize(const int n, const bool preserve=true)
|
void resize(const int n, const bool preserve=true)
|
||||||
@ -87,13 +119,41 @@ public:
|
|||||||
NRVec<T>::resize(n+1,preserve);
|
NRVec<T>::resize(n+1,preserve);
|
||||||
if(preserve) for(int i=nold+1; i<=n;++i) (*this)[i]=0;
|
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);};
|
||||||
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);};
|
||||||
ContFrac operator*(const Rational<T> &rhs) const {Homographic<T> h({{0,rhs.num},{rhs.den,0}}); return h.value(*this);};
|
ContFrac operator*(const Rational<T> &rhs) const {Homographic<T> h({{0,rhs.num},{rhs.den,0}}); return h.value(*this);};
|
||||||
ContFrac operator/(const Rational<T> &rhs) const {Homographic<T> h({{0,rhs.den},{rhs.num,0}}); return h.value(*this);};
|
ContFrac operator/(const Rational<T> &rhs) const {Homographic<T> h({{0,rhs.den},{rhs.num,0}}); return h.value(*this);};
|
||||||
|
|
||||||
|
ContFrac & operator+=(const T &rhs) {this->copyonwrite(); (*this)[0]+=rhs; return *this;};
|
||||||
|
ContFrac & operator-=(const T &rhs) {this->copyonwrite(); (*this)[0]-=rhs; return *this;};
|
||||||
|
ContFrac operator+(const T &rhs) const {ContFrac r(*this); r+=rhs; return r;};
|
||||||
|
ContFrac operator-(const T &rhs) const {ContFrac r(*this); r-=rhs; return r;};
|
||||||
|
ContFrac operator*(const T &rhs) const {Homographic<T> h({{0,rhs},{1,0}}); return h.value(*this);};
|
||||||
|
ContFrac operator/(const T &rhs) const {Homographic<T> h({{0,1},{rhs,0}}); return h.value(*this);};
|
||||||
|
|
||||||
|
//arithmetics with two ContFrac operands
|
||||||
|
//@@@
|
||||||
|
|
||||||
|
//iterator
|
||||||
|
class iterator {
|
||||||
|
private:
|
||||||
|
T *p;
|
||||||
|
public:
|
||||||
|
iterator() {};
|
||||||
|
~iterator() {};
|
||||||
|
iterator(T *v): p(v) {};
|
||||||
|
bool operator==(const iterator rhs) const {return p==rhs.p;}
|
||||||
|
bool operator!=(const iterator rhs) const {return p!=rhs.p;}
|
||||||
|
iterator operator++() {return ++p;}
|
||||||
|
iterator operator++(int) {return p++;}
|
||||||
|
T& operator*() const {return *p;}
|
||||||
|
T *operator->() const {return p;}
|
||||||
|
};
|
||||||
|
iterator begin() const {return NRVec<T>::v;}
|
||||||
|
iterator end() const {return NRVec<T>::v+NRVec<T>::nn;}
|
||||||
|
iterator beyondend() const {return NRVec<T>::v+NRVec<T>::nn+1;}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
//for Gosper's arithmetic
|
//for Gosper's arithmetic
|
||||||
|
@ -119,7 +119,7 @@ return big;
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
inline T lcm(T a, T b)
|
inline T lcm(T a, T b)
|
||||||
{
|
{
|
||||||
return (a*b)/gcd(a,b);
|
return (a/gcd(a,b))*b;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
12
t.cc
12
t.cc
@ -2431,6 +2431,16 @@ NRMat<double> mm=m.permuted_rows(p);
|
|||||||
cout <<mm;
|
cout <<mm;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(1)
|
||||||
|
{
|
||||||
|
Rational<int> p,q;
|
||||||
|
cin>>p>>q;
|
||||||
|
cout <<p+q<<endl;
|
||||||
|
cout <<p-q<<endl;
|
||||||
|
cout <<p*q<<endl;
|
||||||
|
cout <<p/q<<endl;
|
||||||
|
}
|
||||||
|
|
||||||
if(0)
|
if(0)
|
||||||
{
|
{
|
||||||
double x;
|
double x;
|
||||||
@ -2463,7 +2473,7 @@ double zzz=zz.value();
|
|||||||
cout <<z<<" "<<zzz<<endl;
|
cout <<z<<" "<<zzz<<endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(1)
|
if(0)
|
||||||
{
|
{
|
||||||
Rational<int> r({11,101});
|
Rational<int> r({11,101});
|
||||||
ContFrac<int> x(r);
|
ContFrac<int> x(r);
|
||||||
|
Loading…
Reference in New Issue
Block a user