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 <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include <list>
|
||||||
|
|
||||||
|
|
||||||
namespace LA {
|
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
|
* forced instantization in the corresponding object file
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
@ -142,6 +204,22 @@ template class ContFrac<unsigned long>;
|
|||||||
template class ContFrac<long long>;
|
template class ContFrac<long long>;
|
||||||
template class ContFrac<unsigned 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) \
|
#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
|
//NOTE: 0 on any position >0 means actually infinity; simplify() shortens the vector
|
||||||
//presently implements just conversion to/from rationals and floats
|
//presently implements just conversion to/from rationals and floats
|
||||||
//maybe implement arithmetic by Gosper's method cf. https://perl.plover.com/classes/cftalk/TALK
|
//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>
|
template <typename T>
|
||||||
class Rational {
|
class Rational {
|
||||||
public:
|
public:
|
||||||
@ -37,8 +42,26 @@ public:
|
|||||||
T den;
|
T den;
|
||||||
|
|
||||||
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]) {};
|
||||||
|
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>
|
template <typename T>
|
||||||
class ContFrac : public NRVec<T> {
|
class ContFrac : public NRVec<T> {
|
||||||
private:
|
private:
|
||||||
@ -50,7 +73,7 @@ public:
|
|||||||
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
|
||||||
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;
|
||||||
@ -65,6 +88,36 @@ public:
|
|||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
#include "permutation.h"
|
#include "permutation.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <list>
|
||||||
|
|
||||||
|
|
||||||
namespace LA {
|
namespace LA {
|
||||||
|
12
t.cc
12
t.cc
@ -2431,7 +2431,7 @@ NRMat<double> mm=m.permuted_rows(p);
|
|||||||
cout <<mm;
|
cout <<mm;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(1)
|
if(0)
|
||||||
{
|
{
|
||||||
double x;
|
double x;
|
||||||
cin >>x;
|
cin >>x;
|
||||||
@ -2463,5 +2463,15 @@ double zzz=zz.value();
|
|||||||
cout <<z<<" "<<zzz<<endl;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
3
vec.cc
3
vec.cc
@ -899,9 +899,8 @@ return -1;
|
|||||||
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
NRVec<T>::NRVec(const std::list<T> l)
|
NRVec<T>::NRVec(const std::list<T> l) : NRVec<T>(l.size())
|
||||||
{
|
{
|
||||||
resize(l.size());
|
|
||||||
int ii=0;
|
int ii=0;
|
||||||
for(typename std::list<T>::const_iterator i=l.begin(); i!=l.end(); ++i) (*this)[ii++] = *i;
|
for(typename std::list<T>::const_iterator i=l.begin(); i!=l.end(); ++i) (*this)[ii++] = *i;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user