// File: IlanPrice[P]Linear-Algebra-Library-in-Cplusplus/Convergence.cpp
// Function: main
int main(int argc, char* argv[])
{
double pi = 3.1415926535897;


// Set up matrix system

int n = 20;
Vector a1 = ones(n-1);
// disp(a1);
Matrix A = 4*eye(n) - diag(a1,1) - diag(a1,-1);
// disp(A);
Matrix B1 = eye(n);
Matrix C1 = kron(B1,A);
// disp(C1);
Matrix B2 = diag(a1,1) + diag(a1,-1);
Matrix C2 = kron(B2,-eye(n));
// disp(C2);
Matrix C = C1+C2;
// disp(C);
//RHS

Vector b(n*n);
double dx = 1.0/(n-1);
Vector xco(n);
Vector yco(n);
for (int i = 2; i<=n; i++)
{
  xco(i) = (i-1)*dx;
  yco(i) = (i-1)*dx;
}

disp(xco);

int fillcount = 1;

for(int i = 1; i<=n; i++)
{
  // std::cout<<i<<"\n";
  for (int j = 1; j<=n; j++)
  {
      b(fillcount) = ((pi*pi)*(xco(j)*xco(j))*(1-xco(j)) + 6*xco(j) - 2)*sin(pi*yco(i));
      // std::cout<<b(fillcount)<<"\n";
      fillcount += 1;
  }
}

b = dx*dx*b;
// disp(b);

// // apply and time methods

//Vector times(7);
////
//std::clock_t LUstart = std::clock();
//Vector x_lu = C/b;
//double lutime = (std::clock() - LUstart) / (double)CLOCKS_PER_SEC;
//std::cout << "Finished in " << lutime << " seconds [CPU Clock] " << std::endl;

// std::clock_t jacobistart = std::clock();
// Vector x_j = jacobi(C,b);
// double jacobitime = (std::clock() - jacobistart) / (double)CLOCKS_PER_SEC;
// std::cout << "Finished in " << jacobitime << " seconds [CPU Clock] " << std::endl;
//
// std::clock_t GSstart = std::clock();
// Vector x_gs = GS(C,b);
// double GStime = (std::clock() - GSstart) / (double)CLOCKS_PER_SEC;
// std::cout << "Finished in " << GStime << " seconds [CPU Clock] " << std::endl;
//
// std::clock_t SORstart = std::clock();
// Vector x_sor = SOR(C,b,1.7406);
// double SORtime = (std::clock() - SORstart) / (double)CLOCKS_PER_SEC;
// std::cout << "Finished in " << SORtime << " seconds [CPU Clock] " << std::endl;
//
// std::clock_t CGstart = std::clock();
// Vector x_cg = CG(C,b);
// double CGtime = (std::clock() - CGstart) / (double)CLOCKS_PER_SEC;
// std::cout << "Finished in " << CGtime << " seconds [CPU Clock] " << std::endl;
//
//
// std::clock_t GMRESstart = std::clock();
// Vector x_gmres = GMRES(C,b);
// double GMREStime = (std::clock() - GMRESstart) / (double)CLOCKS_PER_SEC;
// std::cout << "Finished in " << GMREStime << " seconds [CPU Clock] " << std::endl;
//
//
// std::clock_t MINRESstart = std::clock();
// Vector x_minres = MINRES(C,b);
// double MINREStime = (std::clock() - MINRESstart) / (double)CLOCKS_PER_SEC;
// std::cout << "Finished in " << MINREStime << " seconds [CPU Clock] " << std::endl;
// //
// // disp(x_gmres);
// times(1) = jacobitime; times(2) = GStime; times(3) = SORtime;
// times(4) = CGtime; times(5) = GMREStime; times(6) = MINREStime;
//times(7) = lutime;
//
//disp(times);

// std::ofstream CPUtimes;
// CPUtimes.open("CPUtimes.txt");
// CPUtimes << std::setprecision(15) << times << "\n";
// CPUtimes.close();



// // Convergence with mesh size


// std::ofstream MMSconvergence;
// MMSconvergence.open("MMSconvergence.txt");
//
// int n=5;
// for (int k = 1; k<=5; k++)
// {
//   Vector a1 = ones(n-1);
//   // disp(a1);
//   Matrix A = 4*eye(n) - diag(a1,1) - diag(a1,-1);
//   // disp(A);
//   Matrix B1 = eye(n);
//   Matrix C1 = kron(B1,A);
//   // disp(C1);
//   Matrix B2 = diag(a1,1) + diag(a1,-1);
//   Matrix C2 = kron(B2,-eye(n));
//   // disp(C2);
//   Matrix C = C1+C2;
//   // disp(C);
//   //RHS
//
//   Vector b(n*n);
//   double dx = 1.0/(n-1);
//   Vector xco(n);
//   Vector yco(n);
//   for (int i = 2; i<=n; i++)
//   {
//     xco(i) = (i-1)*dx;
//     yco(i) = (i-1)*dx;
//   }
//
//   disp(xco);
//
//   int fillcount = 1;
//
//   for(int i = 1; i<=n; i++)
//   {
//     std::cout<<i<<"\n";
//     for (int j = 1; j<=n; j++)
//     {
//         b(fillcount) = ((pi*pi)*(xco(j)*xco(j))*(1-xco(j)) + 6*xco(j) - 2)*sin(pi*yco(i));
//         std::cout<<b(fillcount)<<"\n";
//         fillcount += 1;
//     }
//   }
//
//   b = dx*dx*b;
//   Vector x = C/b;
//   MMSconvergence << std::setprecision(15) << x << "\n";
//   n +=5;
//   std::cout<< n << "\n";
// }
// MMSconvergence.close();


}

// Assembly:
subq $0x218, %rsp            # imm = 0x218
movl $0x0, 0x214(%rsp)
movl %edi, 0x210(%rsp)
movq %rsi, 0x208(%rsp)
movabsq $0x400921fb54442c46, %rax # imm = 0x400921FB54442C46
movq %rax, 0x200(%rsp)
movl $0x14, 0x1fc(%rsp)
movl 0x1fc(%rsp), %esi
decl %esi
leaq 0x1e8(%rsp), %rdi
callq 0x4570
movl 0x1fc(%rsp), %esi
leaq 0x1a8(%rsp), %rdi
callq 0xa210
jmp 0x244a
leaq 0x1b8(%rsp), %rdi
movsd 0xfbae(%rip), %xmm0     # 0x12008
leaq 0x1a8(%rsp), %rsi
callq 0x6ba0
jmp 0x2469
leaq 0x188(%rsp), %rdi
leaq 0x1e8(%rsp), %rsi
movl $0x1, %edx
callq 0xa3a0
jmp 0x2485
leaq 0x1c8(%rsp), %rdi
leaq 0x1b8(%rsp), %rsi
leaq 0x188(%rsp), %rdx
callq 0x6a60
jmp 0x24a4
leaq 0x178(%rsp), %rdi
leaq 0x1e8(%rsp), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0xa3a0
jmp 0x24c0
leaq 0x1d8(%rsp), %rdi
leaq 0x1c8(%rsp), %rsi
leaq 0x178(%rsp), %rdx
callq 0x6a60
jmp 0x24df
leaq 0x178(%rsp), %rdi
callq 0x5230
leaq 0x1c8(%rsp), %rdi
callq 0x5230
leaq 0x188(%rsp), %rdi
callq 0x5230
leaq 0x1b8(%rsp), %rdi
callq 0x5230
leaq 0x1a8(%rsp), %rdi
callq 0x5230
movl 0x1fc(%rsp), %esi
leaq 0x168(%rsp), %rdi
callq 0xa210
jmp 0x2536
leaq 0x158(%rsp), %rdi
leaq 0x168(%rsp), %rsi
leaq 0x1d8(%rsp), %rdx
callq 0x9cb0
jmp 0x2555
leaq 0x138(%rsp), %rdi
leaq 0x1e8(%rsp), %rsi
movl $0x1, %edx
callq 0xa3a0
jmp 0x2571
leaq 0x128(%rsp), %rdi
leaq 0x1e8(%rsp), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0xa3a0
jmp 0x258d
leaq 0x148(%rsp), %rdi
leaq 0x138(%rsp), %rsi
leaq 0x128(%rsp), %rdx
callq 0x6920
jmp 0x25ac
leaq 0x128(%rsp), %rdi
callq 0x5230
leaq 0x138(%rsp), %rdi
callq 0x5230
movl 0x1fc(%rsp), %esi
leaq 0xf8(%rsp), %rdi
callq 0xa210
jmp 0x25dc
leaq 0x108(%rsp), %rdi
leaq 0xf8(%rsp), %rsi
callq 0x9830
jmp 0x25f3
leaq 0x118(%rsp), %rdi
leaq 0x148(%rsp), %rsi
leaq 0x108(%rsp), %rdx
callq 0x9cb0
jmp 0x2612
leaq 0x108(%rsp), %rdi
callq 0x5230
leaq 0xf8(%rsp), %rdi
callq 0x5230
leaq 0xe8(%rsp), %rdi
leaq 0x158(%rsp), %rsi
leaq 0x118(%rsp), %rdx
callq 0x6920
jmp 0x264b
movl 0x1fc(%rsp), %esi
imull %esi, %esi
leaq 0xd8(%rsp), %rdi
callq 0x2d70
jmp 0x2664
movl 0x1fc(%rsp), %eax
decl %eax
cvtsi2sd %eax, %xmm1
movsd 0xf997(%rip), %xmm0     # 0x12010
divsd %xmm1, %xmm0
movsd %xmm0, 0xd0(%rsp)
movl 0x1fc(%rsp), %esi
leaq 0xc0(%rsp), %rdi
callq 0x2d70
jmp 0x269c
movl 0x1fc(%rsp), %esi
leaq 0xb0(%rsp), %rdi
callq 0x2d70
jmp 0x26b2
movl $0x2, 0xac(%rsp)
movl 0xac(%rsp), %eax
cmpl 0x1fc(%rsp), %eax
jg 0x29b8
movl 0xac(%rsp), %esi
movl %esi, %eax
decl %eax
cvtsi2sd %eax, %xmm0
movsd 0xd0(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0x78(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x3ee0
movq %rax, 0x80(%rsp)
jmp 0x270a
movq 0x80(%rsp), %rax
movsd 0x78(%rsp), %xmm0
movsd %xmm0, (%rax)
movl 0xac(%rsp), %esi
movl %esi, %eax
decl %eax
cvtsi2sd %eax, %xmm0
movsd 0xd0(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0x68(%rsp)
leaq 0xb0(%rsp), %rdi
callq 0x3ee0
movq %rax, 0x70(%rsp)
jmp 0x2752
movq 0x70(%rsp), %rax
movsd 0x68(%rsp), %xmm0
movsd %xmm0, (%rax)
movl 0xac(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xac(%rsp)
jmp 0x26bd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1a0(%rsp)
movl %eax, 0x19c(%rsp)
jmp 0x2d53
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1a0(%rsp)
movl %eax, 0x19c(%rsp)
jmp 0x2833
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1a0(%rsp)
movl %eax, 0x19c(%rsp)
jmp 0x2826
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1a0(%rsp)
movl %eax, 0x19c(%rsp)
jmp 0x2819
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1a0(%rsp)
movl %eax, 0x19c(%rsp)
jmp 0x280c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1a0(%rsp)
movl %eax, 0x19c(%rsp)
leaq 0x178(%rsp), %rdi
callq 0x5230
leaq 0x1c8(%rsp), %rdi
callq 0x5230
leaq 0x188(%rsp), %rdi
callq 0x5230
leaq 0x1b8(%rsp), %rdi
callq 0x5230
leaq 0x1a8(%rsp), %rdi
callq 0x5230
jmp 0x2d53
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1a0(%rsp)
movl %eax, 0x19c(%rsp)
jmp 0x2d46
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1a0(%rsp)
movl %eax, 0x19c(%rsp)
jmp 0x2d39
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1a0(%rsp)
movl %eax, 0x19c(%rsp)
jmp 0x2d2c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1a0(%rsp)
movl %eax, 0x19c(%rsp)
jmp 0x28c7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1a0(%rsp)
movl %eax, 0x19c(%rsp)
leaq 0x128(%rsp), %rdi
callq 0x5230
leaq 0x138(%rsp), %rdi
callq 0x5230
jmp 0x2d2c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1a0(%rsp)
movl %eax, 0x19c(%rsp)
jmp 0x2d1f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1a0(%rsp)
movl %eax, 0x19c(%rsp)
jmp 0x2929
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1a0(%rsp)
movl %eax, 0x19c(%rsp)
leaq 0x108(%rsp), %rdi
callq 0x5230
leaq 0xf8(%rsp), %rdi
callq 0x5230
jmp 0x2d1f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1a0(%rsp)
movl %eax, 0x19c(%rsp)
jmp 0x2d12
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1a0(%rsp)
movl %eax, 0x19c(%rsp)
jmp 0x2d05
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1a0(%rsp)
movl %eax, 0x19c(%rsp)
jmp 0x2cf8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1a0(%rsp)
movl %eax, 0x19c(%rsp)
jmp 0x2ceb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1a0(%rsp)
movl %eax, 0x19c(%rsp)
jmp 0x2cde
leaq 0xc0(%rsp), %rdi
callq 0x3fb0
jmp 0x29c7
movl $0x1, 0xa8(%rsp)
movl $0x1, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
cmpl 0x1fc(%rsp), %eax
jg 0x2bd3
movl $0x1, 0xa0(%rsp)
movl 0xa0(%rsp), %eax
cmpl 0x1fc(%rsp), %eax
jg 0x2bbb
movsd 0x200(%rsp), %xmm0
mulsd %xmm0, %xmm0
movsd %xmm0, 0x58(%rsp)
movl 0xa0(%rsp), %esi
leaq 0xc0(%rsp), %rdi
callq 0x3ee0
movq %rax, 0x60(%rsp)
jmp 0x2a3e
movq 0x60(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x48(%rsp)
movl 0xa0(%rsp), %esi
leaq 0xc0(%rsp), %rdi
callq 0x3ee0
movq %rax, 0x50(%rsp)
jmp 0x2a68
movsd 0x58(%rsp), %xmm0
movsd 0x48(%rsp), %xmm1
movq 0x50(%rsp), %rax
movsd (%rax), %xmm2
mulsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0x38(%rsp)
movl 0xa0(%rsp), %esi
leaq 0xc0(%rsp), %rdi
callq 0x3ee0
movq %rax, 0x40(%rsp)
jmp 0x2aa6
movq 0x40(%rsp), %rax
movsd (%rax), %xmm1
movsd 0xf559(%rip), %xmm0     # 0x12010
subsd %xmm1, %xmm0
movsd %xmm0, 0x28(%rsp)
movl 0xa0(%rsp), %esi
leaq 0xc0(%rsp), %rdi
callq 0x3ee0
movq %rax, 0x30(%rsp)
jmp 0x2adc
movsd 0x28(%rsp), %xmm2
movsd 0x38(%rsp), %xmm0
movq 0x30(%rsp), %rax
movsd (%rax), %xmm1
movsd 0xf51f(%rip), %xmm3     # 0x12018
mulsd %xmm3, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd 0xf513(%rip), %xmm1     # 0x12020
addsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0x200(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movl 0xa4(%rsp), %esi
leaq 0xb0(%rsp), %rdi
callq 0x3ee0
movq %rax, 0x20(%rsp)
jmp 0x2b41
movsd 0x18(%rsp), %xmm0
movq 0x20(%rsp), %rax
movsd (%rax), %xmm1
mulsd %xmm1, %xmm0
callq 0x2060
movaps %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, (%rsp)
movl 0xa8(%rsp), %esi
leaq 0xd8(%rsp), %rdi
callq 0x3ee0
movq %rax, 0x8(%rsp)
jmp 0x2b86
movq 0x8(%rsp), %rax
movsd (%rsp), %xmm0
movsd %xmm0, (%rax)
movl 0xa8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa8(%rsp)
movl 0xa0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa0(%rsp)
jmp 0x29fc
jmp 0x2bbd
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
jmp 0x29dd
movsd 0xd0(%rsp), %xmm0
mulsd %xmm0, %xmm0
movsd %xmm0, 0x88(%rsp)
leaq 0x90(%rsp), %rdi
leaq 0x88(%rsp), %rsi
leaq 0xd8(%rsp), %rdx
callq 0x3640
jmp 0x2c08
leaq 0xd8(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x39a0
jmp 0x2c1f
leaq 0x90(%rsp), %rdi
callq 0x2ea0
leaq 0xb0(%rsp), %rdi
callq 0x2ea0
leaq 0xc0(%rsp), %rdi
callq 0x2ea0
leaq 0xd8(%rsp), %rdi
callq 0x2ea0
leaq 0xe8(%rsp), %rdi
callq 0x5230
leaq 0x118(%rsp), %rdi
callq 0x5230
leaq 0x148(%rsp), %rdi
callq 0x5230
leaq 0x158(%rsp), %rdi
callq 0x5230
leaq 0x168(%rsp), %rdi
callq 0x5230
leaq 0x1d8(%rsp), %rdi
callq 0x5230
leaq 0x1e8(%rsp), %rdi
callq 0x2ea0
movl 0x214(%rsp), %eax
addq $0x218, %rsp            # imm = 0x218
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1a0(%rsp)
movl %eax, 0x19c(%rsp)
leaq 0x90(%rsp), %rdi
callq 0x2ea0
leaq 0xb0(%rsp), %rdi
callq 0x2ea0
leaq 0xc0(%rsp), %rdi
callq 0x2ea0
leaq 0xd8(%rsp), %rdi
callq 0x2ea0
leaq 0xe8(%rsp), %rdi
callq 0x5230
leaq 0x118(%rsp), %rdi
callq 0x5230
leaq 0x148(%rsp), %rdi
callq 0x5230
leaq 0x158(%rsp), %rdi
callq 0x5230
leaq 0x168(%rsp), %rdi
callq 0x5230
leaq 0x1d8(%rsp), %rdi
callq 0x5230
leaq 0x1e8(%rsp), %rdi
callq 0x2ea0
movq 0x1a0(%rsp), %rdi
callq 0x21b0
nopl (%rax)