
#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 "expandorigin.cpp"
//#include "..\src\gd.cpp"
//#include "..\src\poly.cpp"
//#include "..\src\serie.cpp"
//#include "..\src\smatrix.cpp"
//#include "..\src\tools.cpp"
#include "gd.cpp"
#include "poly.cpp"
#include "serie.cpp"
#include "smatrix.cpp"
#include "tools.cpp"


double inline min(double a,double b)
{
		if(a>b) return b;
		else return a;
}
/////////////////////////////////////////////////////////////////////
/* Example :  Hardouin L., Cottenceau B., Lagrange S., Le Corronc E.

Performance a&nalysis of linear systems over idempotent semiring with additive inputs
Wodes 2008, Goteborg, Sweden
	*/
/////////////////////////////////////////////////////////////////////

int main()
{

	gd temp_gd;
	poly temp,SR_tf1,SR_tf2,poly1,poly2,poly3,temp1,temp2,s3,s4;
	serie temp_serie,temp_serie2,serie1,serie2,serie,s1,s2;//,s3,s4;
	smatrix transfunction,temp_matrix;
	int time_length;
	int index;
	token_num* token_info;
	token_num* window_info;
	int window_length;
    smatrix A1(4,4),B1(4,1),C1(1,4),AS1(4,4);
    smatrix A2(4,4),B2(4,1),C2(1,4),AS2(4,4);
    smatrix u1_star,x1_star;
    smatrix u2_star,x2_star;

    smatrix z1(1,1);//,s3(1,1), s4(1,1);
    smatrix z2(1,1);
/////////////////////////////////////////////////////////////
    SR_tf1.init(2,3);
    SR_tf2.init(1,1);

  // temp1.init(0,0)(2,2)(7,5)(13,infinity);
    temp1.init(0,0)(2,2)(3,4)(7,infinity);
    //s1(0,0).init(epsilon,temp1);
 temp2.init(0,1)(1,3)(2,8)(4,infinity);
   //   temp2.init(0,3)(2,8)(4,infinity);

 //   temp2.init(0,3)(4,8)(8,infinity);

    s1.init(epsilon,temp1,e);
    s2.init(epsilon,temp2,e);
    //s2(0,0).init(epsilon,temp2);
    s3=odotsharp(temp1,temp2);
    s4=odotflat(temp1,temp2);
    temp=odot(s4,temp2);
//temp=inf(temp,temp1);
       std::cout << " s1 odotsharp s2:" << std::endl << s3<< std::endl;
       std::cout << " temp:" << std::endl << temp<< std::endl;
       temp=inf(temp,temp1);
       std::cout << " temp:" << std::endl << temp<< std::endl;
//        std::cout << " s1 odotsharp s2 odot s2:" << std::endl << temp<< std::endl;
//        temp=odot(s4,temp2);
//temp=oplus(temp,temp1);
               std::cout << " s1 odotflat:" << std::endl << s4<< std::endl;
//                std::cout << " s1 odotflat s2 odot s2:" << std::endl << temp<< std::endl;




    temp.init(0,10)(1,11)(2,30)(3,45)(6,infinity);
    z1(0,0).init(epsilon,temp,e);
    temp.init(0,32)(1,38)(2,44)(5,infinity);
    z2(0,0).init(epsilon,temp,e);

    A1(0,1).init(temp.init(2,3),epsilon,e);
    A1(1,0).init(temp.init(0,5),epsilon,e);
    A1(2,1).init(temp.init(0,1),epsilon,e);
    A1(3,2).init(temp.init(0,3),epsilon,e);
    A1(2,3).init(temp.init(1,1),epsilon,e);

    A2(0,1).init(temp.init(2,3),epsilon,e);
    A2(1,0).init(temp.init(0,7),epsilon,e);
    A2(2,1).init(temp.init(0,2),epsilon,e);
    A2(3,2).init(temp.init(0,4),epsilon,e);
    A2(2,3).init(temp.init(1,1),epsilon,e);


    B1(0,0).init(epsilon,e,e);
    B2(0,0).init(epsilon,e,e);

    C1(0,3).init(epsilon,e,e);
    C2(0,3).init(epsilon,e,e);
//////////////////////////////////////////////////////////////
    AS1 = star(A1);
    AS2 = star(A2);
    temp_matrix = otimes(C1,AS1);
    transfunction = otimes(temp_matrix,B1);
    std::cout << " H1 :" << std::endl << transfunction<< std::endl;
        u1_star = lfrac(z1,transfunction);
    temp_matrix = otimes(C2,AS2);
    transfunction = otimes(temp_matrix,B2);
        std::cout << " H2 :" << std::endl << transfunction<< std::endl;

    u2_star = lfrac(z2,transfunction);

    temp_matrix = otimes(AS1,B1);
    x1_star = otimes(temp_matrix,u1_star);
        std::cout<<"u1* : "<<std::endl<<u1_star<<std::endl;
        std::cout<<"x* : "<<std::endl<<x1_star<<std::endl;
        std::cout<<"y* : "<<std::endl<<x1_star(3,0)<<std::endl;
        std::cout<<"z1* : "<<std::endl<<z1<<std::endl;


    temp_matrix = otimes(AS2,B2);
    x2_star = otimes(temp_matrix,u2_star);
        std::cout<<"u2* : "<<std::endl<<u2_star<<std::endl;
        std::cout<<"x'* : "<<std::endl<<x2_star<<std::endl;
        std::cout<<"y'* : "<<std::endl<<x2_star(3,0)<<std::endl;
        std::cout<<"z2* : "<<std::endl<<z2<<std::endl;
///////////////////////////////////////////////////////////////

poly z1poly, z2poly,b3,b4;
z2poly.init(0,32)(1,38)(2,44)(5,infinity);
z1poly.init(0,10)(1,11)(2,30)(3,45)(6,infinity);

 poly x23poly,x24poly,betah2,x3x1,x13starpoly,x23starpoly,x14starpoly,x24starpoly,G11,G22,G12,G21,x21poly,x22poly,x22starpoly,x21starpoly,x12starpoly,x11starpoly;
 poly x11poly,x12poly,x13poly,x14poly,P21,betah1, P22;
    G22.init(0,4);
    G12.init(0,3);
    G11.init(0,5);
    G21.init(0,7);
    P21.init(0,0);
    P22.init(0,2);

 //   x23poly=frac(z2poly,H2);
  x21starpoly=oplus(x2_star(0,0).getp(),x2_star(0,0).getq());
   x22starpoly=oplus(x2_star(1,0).getp(),x2_star(1,0).getq());
 x23starpoly=oplus(x2_star(2,0).getp(),x2_star(2,0).getq());
  x24starpoly=oplus(x2_star(3,0).getp(),x2_star(3,0).getq());
 x24poly=x24starpoly;
 x21poly=x21starpoly;
  x22poly=x22starpoly;
 x23poly=x23starpoly;

    x14starpoly=oplus(x1_star(3,0).getp(),x1_star(3,0).getq());
        x13starpoly=oplus(x1_star(2,0).getp(),x1_star(2,0).getq());
   x11starpoly=oplus(x1_star(0,0).getp(),x1_star(0,0).getq());
        x12starpoly=oplus(x1_star(1,0).getp(),x1_star(1,0).getq());
         x14poly=x14starpoly;
 x11poly=x11starpoly;
  x12poly=x12starpoly;
 x13poly=x13starpoly;


 int i=0;

int step=1;


  //   x4=z2;
     //
   do{
   i++;

//cout << "i"<< i <<endl;

betah1= x21poly; // prevoius value of x22
//x21poly=frac(x22poly,G21);
b3=odot(x21poly,x11starpoly);
b3=frac(b3,SR_tf1);
b3=fracodotsharp(b3,x12starpoly);
//cout << "b3"<< b3 <<endl;

b3=frac(b3,G21);

b4=otimes(P22,G21);
b4=otimes(b4,x21poly);
b4=odot(b4,x13starpoly);
b4=frac(b4,SR_tf2);
b4=fracodotsharp(b4,x14starpoly);
temp=otimes(G22,P22);
temp=otimes(temp,G21);
b4=frac(b4,temp);

betah1=inf(betah1,b3);
betah1=inf(betah1,b4);
if(!(x21poly==betah1)){
x21poly=betah1;
}
else
step=21;

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

if (step==22)
{
     std::cout<<"x21poly is ok"<<std::endl<<x21poly<<std::endl;

}


}
