progressing on contfrac
This commit is contained in:
parent
0e8c20770f
commit
e67e6a5797
78
contfrac.cc
78
contfrac.cc
@ -20,6 +20,7 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <list>
|
||||
|
||||
|
||||
namespace LA {
|
||||
@ -132,6 +133,67 @@ for(int i=1; i<=n; ++i) //truncate if possible
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
ContFrac<T> Homographic<T>::value(const ContFrac<T>&x) const
|
||||
{
|
||||
Homographic<T> h(*this);
|
||||
|
||||
std::list<T> l;
|
||||
for(int i=0; i<=x.length()+1; ++i) //scan all input
|
||||
{
|
||||
//digest next input term
|
||||
Homographic<T> hnew;
|
||||
if(i>x.length()||i!=0&&x[i]==0) //input is infinity
|
||||
{
|
||||
hnew.x[0][0]=hnew.x[0][1]=h.x[0][1];
|
||||
hnew.x[1][0]=hnew.x[1][1]=h.x[1][1];
|
||||
}
|
||||
else
|
||||
{
|
||||
hnew.x[0][0]=h.x[0][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[1][1]=h.x[1][0]+h.x[1][1]*x[i];
|
||||
}
|
||||
|
||||
//std::cout<<"hnew\n"<< hnew.x[0][0]<<" "<< hnew.x[0][1]<<"\n"<< hnew.x[1][0]<<" "<< hnew.x[1][1]<<"\n";
|
||||
|
||||
//check if we are ready to output
|
||||
bool inf=0;
|
||||
T q0,q1;
|
||||
if(hnew.x[1][0]==0) inf=1; else q0=hnew.x[0][0]/hnew.x[1][0];
|
||||
if(hnew.x[1][1]==0) inf=1; else q1=hnew.x[0][1]/hnew.x[1][1];
|
||||
while(!inf && q0==q1) //ready to output
|
||||
{
|
||||
l.push_back(q0);
|
||||
//std::cout <<"out "<<q0<<std::endl;
|
||||
Homographic<T> hnew2;
|
||||
hnew2.x[0][0]=hnew.x[1][0];
|
||||
hnew2.x[0][1]=hnew.x[1][1];
|
||||
hnew2.x[1][0]=hnew.x[0][0]-hnew.x[1][0]*q0;
|
||||
hnew2.x[1][1]=hnew.x[0][1]-hnew.x[1][1]*q0;
|
||||
//std::cout<<"hnew2\n"<< hnew2.x[0][0]<<" "<< hnew2.x[0][1]<<"\n"<< hnew2.x[1][0]<<" "<< hnew2.x[1][1]<<"\n";
|
||||
hnew=hnew2;
|
||||
inf=0;
|
||||
if(hnew.x[1][0]==0) inf=1; else q0=hnew.x[0][0]/hnew.x[1][0];
|
||||
if(hnew.x[1][1]==0) inf=1; else q1=hnew.x[0][1]/hnew.x[1][1];
|
||||
}
|
||||
//termination
|
||||
if(hnew.x[1][0]==0&&hnew.x[1][1]==0) //terminate
|
||||
{
|
||||
//std::cout<<"terminate at "<<i<<"\n";
|
||||
if(i<=x.length()) laerror("unexpected termination in Homographic::value");
|
||||
break;
|
||||
}
|
||||
h=hnew;
|
||||
}
|
||||
//std::cout <<"size= "<<l.size()<<std::endl;
|
||||
return ContFrac<T>(l);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/***************************************************************************//**
|
||||
* forced instantization in the corresponding object file
|
||||
******************************************************************************/
|
||||
@ -142,6 +204,22 @@ template class ContFrac<unsigned long>;
|
||||
template class ContFrac<long long>;
|
||||
template class ContFrac<unsigned long long>;
|
||||
|
||||
template class Homographic<int>;
|
||||
template class Homographic<unsigned int>;
|
||||
template class Homographic<long>;
|
||||
template class Homographic<unsigned long>;
|
||||
template class Homographic<long long>;
|
||||
template class Homographic<unsigned long long>;
|
||||
|
||||
template class BiHomographic<int>;
|
||||
template class BiHomographic<unsigned int>;
|
||||
template class BiHomographic<long>;
|
||||
template class BiHomographic<unsigned long>;
|
||||
template class BiHomographic<long long>;
|
||||
template class BiHomographic<unsigned long long>;
|
||||
|
||||
|
||||
|
||||
|
||||
#define INSTANTIZE(T) \
|
||||
|
||||
|
55
contfrac.h
55
contfrac.h
@ -29,7 +29,12 @@ namespace LA {
|
||||
//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
|
||||
//
|
||||
|
||||
template <typename T>
|
||||
class ContFrac;
|
||||
|
||||
//@@@basic rational arithmetics
|
||||
template <typename T>
|
||||
class Rational {
|
||||
public:
|
||||
@ -37,8 +42,26 @@ public:
|
||||
T den;
|
||||
|
||||
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);};
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
std::ostream & operator<<(std::ostream &s, const Rational<T> &x)
|
||||
{
|
||||
s<<x.num<<"/"<<x.den;
|
||||
return s;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
class Homographic;
|
||||
|
||||
template <typename T>
|
||||
class BiHomographic;
|
||||
|
||||
|
||||
//@@@implement iterator and rewrite Homographic<T>::value
|
||||
|
||||
template <typename T>
|
||||
class ContFrac : public NRVec<T> {
|
||||
private:
|
||||
@ -50,7 +73,7 @@ public:
|
||||
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(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 convergent(T *p, T*q, const int trunc= -1) const;
|
||||
@ -64,9 +87,39 @@ public:
|
||||
NRVec<T>::resize(n+1,preserve);
|
||||
if(preserve) for(int i=nold+1; i<=n;++i) (*this)[i]=0;
|
||||
}
|
||||
|
||||
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.den},{rhs.num,0}}); return h.value(*this);};
|
||||
|
||||
|
||||
};
|
||||
|
||||
//for Gosper's arithmetic
|
||||
|
||||
template <typename T>
|
||||
class Homographic {
|
||||
public:
|
||||
T x[2][2]; //{{a,b},{c,d}} for (a+b.z)/(c+d.z)
|
||||
|
||||
Homographic(){};
|
||||
explicit Homographic(const T (&a)[2][2]) {memcpy(x,a,2*2*sizeof(T));};
|
||||
ContFrac<T> value(const ContFrac<T>&x) const;
|
||||
|
||||
};
|
||||
|
||||
|
||||
template <typename T>
|
||||
class BiHomographic {
|
||||
public:
|
||||
T x[2][2][2];
|
||||
|
||||
BiHomographic(){};
|
||||
explicit BiHomographic(const T (&a)[2][2][2]) {memcpy(x,a,2*2*2*sizeof(T));};
|
||||
ContFrac<T> value(const ContFrac<T>&x, const ContFrac<T>&y) const;
|
||||
};
|
||||
|
||||
|
||||
|
||||
}//namespace
|
||||
|
@ -19,6 +19,7 @@
|
||||
#include "permutation.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <list>
|
||||
|
||||
|
||||
namespace LA {
|
||||
|
12
t.cc
12
t.cc
@ -2431,7 +2431,7 @@ NRMat<double> mm=m.permuted_rows(p);
|
||||
cout <<mm;
|
||||
}
|
||||
|
||||
if(1)
|
||||
if(0)
|
||||
{
|
||||
double x;
|
||||
cin >>x;
|
||||
@ -2463,5 +2463,15 @@ double zzz=zz.value();
|
||||
cout <<z<<" "<<zzz<<endl;
|
||||
}
|
||||
|
||||
if(1)
|
||||
{
|
||||
Rational<int> r({11,101});
|
||||
ContFrac<int> x(r);
|
||||
ContFrac<int> y= x+Rational<int>({2,3});
|
||||
cout<<Rational<int>(y)<<endl;
|
||||
ContFrac<int> z= x*Rational<int>({2,3});
|
||||
cout<<Rational<int>(z)<<endl;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user