I have got thsi program that is meant to help with a coursework project but i do not understand it fully, and consequently its not much help.
The initial section confuses me the most. Is this just simple definitions for double arrays?
I have annotated the lines in the code which esape me most. could any one possibly quote these lines with understandable annotations, or helpful suggestions?
#include <iostream>
#include <complex>
//* This section below in green confuses me the most
using namespace std;
#define EPS 0.001
complex<double> P(complex<double> a[], int n, complex<double> x);
complex<double> P1(complex<double> a[], int n, complex<double> x);
complex<double> P2(complex<double> a[], int n, complex<double> x);
complex<double> findRoot(complex<double> a[],int n);
complex<double> power(complex<double> x,int p);
complex<double> *roots(complex <double> a[], int n);
double magn(complex<double> x);
int main()
{
int n;
double tmp;
cout<<"Enter range of polynom:";
cin>>n;
complex<double> *a=new complex<double>[n+1];
for(int i=0;i<=n;i++)
{
cout<<"Enter a["<<n-i<<"]:"; //* I have never used out with brackets and speach marks before. what does this do?
cin>>tmp;
a[i]=complex<double>(tmp,0);
}
complex<double> *x;
x=roots(a,n);
for(i=0;i<n;i++)
cout<<"x("<<i+1<<")="<<x[i]<<endl;
system("pause");
return 0;
}
complex<double> P(complex<double> a[], int n, complex<double> x)
{
complex<double> sum=complex<double>(0,0);
for(int i=0;i<=n;i++)
{
sum+=a[i]*power(x,n-i);
}
return sum;
}
complex<double> P1(complex<double> a[], int n, complex<double> x)
{
complex<double> *b=new complex<double>[n];
for(int i=0;i<n;i++)
{
b[i]=a[i]*complex<double>((n-i),0);
}
return P(b,n-1,x);
}
complex<double> P2(complex<double> a[], int n, complex<double> x)
{
complex<double> *b=new complex<double>[n-1];
for(int i=0;i<n-1;i++)
{
b[i]=a[i]*complex<double>((n-i),0)*complex<double>((n-i-1),0);
}
return P(b,n-2,x);
}
complex<double> power(complex<double> x,int p)
{
complex<double> tmp=x;
if(p==0)
return complex<double>(1,0);
for(int i=0;i<p-1;i++)
{
tmp*=x;
}
return tmp;
}
complex<double> findRoot(complex<double> a[],int n)
{
complex<double> z0,z1,al,bet,gam,A,B,C,nk,nc,d,c1,c2;
z0=complex<double>(0,0);
int j;
nk=complex<double>((n-1),0);
nc=complex<double>(n,0);
do
{
al=a[0];
bet=complex<double>(0,0);
gam=complex<double>(0,0);
for(j=1;j<=n;j++)
{
gam=z0*gam+bet;
bet=z0*bet+al;
al=z0*al+a[j];
}
if(magn(al)<EPS) // is EPS just a low number used for comparisons? break;
A=-bet/al;
B=A*A-complex<double>(2,0)*gam/al;
C=sqrt(nk*(nc*B-A*A));
c1=A+C;
c2=A-C;
if(magn(c1)<magn(c2))
c1=c2;
d=nc/c1;
z0=z0+d;
}while(magn(d)>EPS);
return z0;
}
double magn(complex<double> x)
{
return sqrt(x.real()*x.real()+x.imag()*x.imag());
}
complex<double> *roots(complex <double> a[], int n)
{
complex<double> *xr=new complex<double>[n];
complex<double> *b=new complex<double>[n+1];
int i,j,k=n;
for(j=0;j<=n;j++)
b[j]=a[j];
for(i=0;i<n;i++)
{
xr[i]=findRoot(b,k);
for(j=1;j<k;j++)
{
b[j]+=b[j-1]*xr[i];
}
k--;
}
return xr;
}
Is it possible to describe this in a way an idiot could understand? I get the basics of the loops, but struggle more with the arrays and the maths.
Thanks. PabloD