
#ifndef _WIN32
#include "../include/lminmaxgd.h"
#else
#include "..\include\lminmaxgd.h"

#endif
#include <time.h>
using namespace std;
#include <iostream>
#include <time.h>
//#include "ominus.cpp"
//#include "expand.cpp"

#include "..\src\gd.cpp"
#include "..\src\poly.cpp"
#include "..\src\serieodotinprogress.cpp"
#include "..\src\smatrix.cpp"
#include "..\src\tools.cpp"



double inline min(double a,double b)
{
		if(a>b) return b;
		else return a;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////

int main()
{

	gd temp_gd;

	int time_length;
	int index;


    smatrix A1(2,2),B1(2,1),C1(1,2),AS1(2,2),ASB1(2,1);
    smatrix A2(2,2),B2(2,1),C2(1,2),AS2(2,2),ASB2(2,1);
    smatrix A3(2,2),B3(2,1),C3(1,2),AS3(2,2),ASB3(2,1);
    smatrix A4(2,2),B4(2,1),C4(1,2),AS4(2,2),ASB4(2,1);
    smatrix X1,X2,X3,X4;
    smatrix beta(1,1);
    smatrix u1_star,x1_star;
    smatrix u2_star,x2_star;

    A1(1,0)=gd(0,3);
    A2(1,0)=gd(0,5);
    A3(1,0)=gd(0,6);
    A4(1,0)=gd(0,4);
    // A1(0,1)=gd(2,2);
    // A2(0,1)=gd(2,2);
    // A3(0,1)=gd(2,2);
    // A4(0,1)=gd(2,2);
    B1(0,0)=gd(0,0);
    B2(0,0)=gd(0,0);
    B3(0,0)=gd(0,0);
    B4(0,0)=gd(0,0);
    beta(0,0)=gd(2,2);
    AS1=star(A1);
    AS2=star(A2);
    cout<<"AS2 "<<AS2<<endl; getchar();
    AS3=star(A3);
    AS4=star(A4);
    ASB1=otimes(AS1,B1);
    ASB2=otimes(AS2,B2);
    ASB3=otimes(AS3,B3);
    ASB4=otimes(AS4,B4);

    poly u1,u2,u3,u4;
    smatrix su1(1,1),su2(1,1),su3(1,1),su4(1,1);

    u1.init(0,0)(1,5)(2,7)(3,10)(4,12)(5,15)(6,infinity);
   // u1.init(0,0)(1,1)(3,3)(6,infinity);
    u2.init(0,0)(2,1)(3,3)(4,infinity);
    u3.init(0,0)(3,3)(5,infinity);
    u4.init(0,0)(2,5)(7,infinity);

    su1(0,0).init(epsilon,u1,e);
    su2(0,0).init(epsilon,u2,e);
    su3(0,0).init(epsilon,u3,e);
    su4(0,0).init(epsilon,u4,e);
    X1=otimes(ASB1,su1);
    X2=otimes(ASB2,su2);

     std::cout<<"X1  "<<std::endl<<X1<<std::endl;

int step=1;
    serie x1_1=X1(0,0);
    gd shift(0,3);
    serie x2_1=X1(1,0);
    serie x1_2=X2(0,0);
std::cout<<"x1_1  "<<x1_1<<std::endl;
std::cout<<"x2_1  "<<x2_1<<std::endl;
std::cout<<"x1_2  "<<x1_2<<std::endl;
    serie temp,temp1;
    std::cout<<"AS2(1,0)  "<<std::endl<<AS2(1,0)<<std::endl;
    std::cout<<"beta(0,0)  "<<beta(0,0)<<std::endl;
poly p1;
poly p2;
     //
   do{
       temp=x1_2;
temp1=otimes(AS2(1,0),temp);
cout<<"temp1"<<temp1<<endl;
temp1=odot(x2_1,temp1);

temp1=otimes(beta(0,0),temp1);
cout<<"temp1"<<temp1<<endl;
p1=oplus(temp1.getp(),temp1.getq());
p2=oplus(x1_1.getp(),x1_1.getq());
p1=fracodotflat(p1,p2); // is only working for polynomial until now
temp1.init(p1,p1,e);
cout<<"temp1"<<temp1<<endl;
x1_2=oplus(temp1,su2(0,0));

cout<<" x1_2 "<<x1_2;
getchar();
   }
while(!(temp==x1_2));

cout<<" temp "<<temp;


/*betah2=otimes(H2,x3poly);
cout << "betah2_1"<< betah2 <<endl;

betah2=odot(betah2,x2starpoly);
cout << "betah2_2"<< betah2 <<endl;

betah2=otimes(SR_tf,betah2);
cout << "betah2_3"<< betah2 <<endl;

betah2=fracodotflat(betah2,x1starpoly);
cout << "betah2_4"<< betah2 <<endl;
betah2=oplus(betah2,x3poly);

betah2=x3poly;
cout << "betah2_5"<< betah2 <<endl;

b3=odot(x3poly,x1starpoly);
cout << "b3_1"<< b3 <<endl;
b3=frac(b3,SR_tf);
b3=prcaus(b3);
cout << "b3_2"<< b3 <<endl;
b3=fracodotsharp(b3,x2starpoly);
b3=prcaus(b3);
cout << "b3_3"<< b3 <<endl;


b3=frac(b3,H2);
b3=prcaus(b3);
x3poly=b3;
cout << "b3_4"<< b3 <<endl;
betah2=inf(betah2,b3);
if(!(x3poly==betah2)){
x3poly=betah2;
}
else
step=11;

step ++;
} while ( step <10);
poly u2star;
u2star=frac(y2star,H2);

if (step==12)
{
     std::cout<<"x3poly is ok"<<std::endl<<x3poly<<std::endl;
          std::cout<<"u2star"<<std::endl<<u2star<<std::endl;
          x3x1=odot(x3poly,x1starpoly);
betah2=otimes(H2,x3poly);
betah2=odot(betah2,x2starpoly);
betah2=otimes(SR_tf,betah2);

if (betah2==x3x1)
cout << "uquality is achieved "<< x3x1 << endl;


}

x3x1=odot(u2star,x1starpoly);
betah2=otimes(H2,u2star);
betah2=odot(betah2,x2starpoly);
betah2=otimes(SR_tf,betah2);
if (betah2==x3x1)
cout << "u2star "<< u2star;
*/
}
