// Rascal, the Advanced Scientific CALculator // Copyright (C) 2001, Sebastian Ritterbusch (Rascal@Ritterbusch.de) // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more detauls. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // #ifndef TAYLOR_HPP_INCLUDED #define TAYLOR_HPP_INCLUDED #include template class mtaylor { private: int n; T *a; public: mtaylor(void) : n(0),a(0) {} mtaylor(T x,int iN=1) :a(new T[iN]) { if(a) n=iN; else n=0; int i; if(n) a[0]=x; for(i=1;i1) a[1]=y; for(i=2;i & b) : a(new T[b.n]) { if(a) n=b.n; else n=0; int i; for(i=0;i & operator =(const mtaylor & b) { T * new_a=new T[b.n]; n=new_a?b.n:0; int i; for(i=0;i & operator +=(mtaylor & a,const mtaylor & b) { int i; a.minsize(b.n); for(i=0;i & operator +=(mtaylor & a,const T & b) { if(a.n) a.a[0]=a.a[0]+b; return a;} friend inline mtaylor operator +(mtaylor a,const mtaylor & b) { return a+=b; } friend inline mtaylor operator +(mtaylor a,const T & b) { return a+=b; } friend inline mtaylor operator +(const T & b,mtaylor a) { return a+=b; } friend inline mtaylor operator -(mtaylor a) { int i; for(i=0;i & operator -=(mtaylor & a,const mtaylor & b) { int i; a.minsize(b.n);for(i=0;i & operator -=(mtaylor & a,const T & b) { if(a.n) a.a[0]=a.a[0]-b; return a;} friend inline mtaylor operator -(mtaylor a,const mtaylor & b) { return a-=b; } friend inline mtaylor operator -(mtaylor a,const T & b) { return a-=b; } friend inline mtaylor operator -(const T & b,mtaylor a) { return -(a-=b); } friend inline mtaylor & operator *=(mtaylor & a,const mtaylor & b) { int i,j; a.minsize(b.n); for(i=a.n-1;i>=0;i--) { a.a[i]=a.a[i]*b.a[0]; for(j=1;j<=i && j & operator *=(mtaylor & a,const T & b) { int i;for(i=0;i operator *(mtaylor a,const mtaylor & b) { return a*=b; } friend inline mtaylor operator *(mtaylor a,const T & b) { return a*=b; } friend inline mtaylor operator *(const T & b,mtaylor a) { return a*=b; } friend inline mtaylor & operator /=(mtaylor & a,const mtaylor & b) { int i,j; a.minsize(b.n); for(i=0;i & operator /=(mtaylor & a,const T & b) { int i; for(i=0;i operator /(mtaylor a,const mtaylor & b) { return a/=b; } friend inline mtaylor operator /(mtaylor a,const T & b) { return a/=b; } friend inline mtaylor operator /(const T & b,mtaylor a) { return mtaylor(b,a.n)/a; } friend inline ostream & operator <<(ostream & o,const mtaylor & a) { int i; for(i=0;i & a,const mtaylor & b) { return (a.n!=0) && (b.n!=0) && (a.a[0]==b.a[0]); } friend inline int operator!=(const mtaylor & a,const mtaylor & b) { return (a.n==0) || (b.n==0) || (a.a[0]!=b.a[0]); } friend inline mtaylor sqrt(const mtaylor & a) { if(a.n<=0) return a; // nonsense mtaylor result(sqrt(a.a[0]),a.n); int k,j; T factor=T(1)/(T(2)*result.a[0]); for(k=1;k sqr(const mtaylor & a) { if(a.n<=0) return a; // nonsense mtaylor result(sqr(a.a[0]),a.n); int k,j; for(k=1;k exp(const mtaylor & a) { if(a.n<=0) return a; // nonsense mtaylor result(exp(a.a[0]),a.n); int k,j; for(k=1;k sin(const mtaylor & a) { if(a.n<=0) return a; // nonsense mtaylor sresult(sin(a.a[0]),a.n); mtaylor cresult(cos(a.a[0]),a.n); int k,j; for(k=1;k cos(const mtaylor & a) { if(a.n<=0) return a; // nonsense mtaylor sresult(sin(a.a[0]),a.n); mtaylor cresult(cos(a.a[0]),a.n); int k,j; for(k=1;k tan(const mtaylor & a) { if(a.n<=0) return a; // nonsense mtaylor sresult(sin(a.a[0]),a.n); mtaylor cresult(cos(a.a[0]),a.n); int k,j; for(k=1;k cot(const mtaylor & a) { if(a.n<=0) return a; // nonsense mtaylor sresult(sin(a.a[0]),a.n); mtaylor cresult(cos(a.a[0]),a.n); int k,j; for(k=1;k sinh(const mtaylor & a) { if(a.n<=0) return a; // nonsense mtaylor sresult(sinh(a.a[0]),a.n); mtaylor cresult(cosh(a.a[0]),a.n); int k,j; for(k=1;k cosh(const mtaylor & a) { if(a.n<=0) return a; // nonsense mtaylor sresult(sinh(a.a[0]),a.n); mtaylor cresult(cosh(a.a[0]),a.n); int k,j; for(k=1;k tanh(const mtaylor & a) { if(a.n<=0) return a; // nonsense mtaylor sresult(sinh(a.a[0]),a.n); mtaylor cresult(cosh(a.a[0]),a.n); int k,j; for(k=1;k coth(const mtaylor & a) { if(a.n<=0) return a; // nonsense mtaylor sresult(sinh(a.a[0]),a.n); mtaylor cresult(cosh(a.a[0]),a.n); int k,j; for(k=1;k log(const mtaylor & a) { if(a.n<=0) return a; // nonsense mtaylor result(log(a.a[0]),a.n); int k,j; for(k=1;k asin(const mtaylor & a) { if(a.n<=0) return a; // nonsense mtaylor result(asin(a.a[0]),a.n); mtaylor invdiff(sqrt(T(1)-a*a)); int k,j; for(k=1;k acos(const mtaylor & a) { if(a.n<=0) return a; // nonsense mtaylor result(acos(a.a[0]),a.n); mtaylor invdiff(-sqrt(T(1)-a*a)); int k,j; for(k=1;k atan(const mtaylor & a) { if(a.n<=0) return a; // nonsense mtaylor result(atan(a.a[0]),a.n); mtaylor invdiff(T(1)+a*a); int k,j; for(k=1;k acot(const mtaylor & a) { if(a.n<=0) return a; // nonsense mtaylor result(acot(a.a[0]),a.n); mtaylor invdiff(T(-1)-a*a); int k,j; for(k=1;k asinh(const mtaylor & a) { return log(a+sqrt(a*a+T(1))); } friend inline mtaylor acosh(const mtaylor & a) { return log(a+sqrt(a*a-T(1))); } friend inline mtaylor atanh(const mtaylor & a) { return log((T(1)+a)/(T(1)-a))/T(2); } friend inline mtaylor acoth(const mtaylor & a) { return log((a+T(1))/(a-T(1)))/T(2); } friend inline mtaylor pow(const mtaylor & a,const T &b) { // if(a.n>1) // a.a[1]=b*a.a[1]*pow(a.a[0],b-1); // a.a[0]=pow(a.a[0],b); // return a; return exp(b*log(a)); } friend inline mtaylor power(const mtaylor & a,int b) { // if(a.n>1) // a.a[1]=b*a.a[1]*pow(a.a[0],b-1); // a.a[0]=pow(a.a[0],b); // return a; if(b<0) return power(1/a,-b); if(b==1) return a; if(b==0) return mtaylor(1); if(b&1) return a*power(a,b-1); else return sqr(power(a,b/2)); } }; #endif