// File: yukimasano[P]cpp_matrix/Matrix.cpp
// Function: Jacobi(Matrix&, Vector&, int&)
Vector Jacobi(Matrix& A, Vector& v, int& count){
	assert(nrows(A) == ncols(A));
	int m =nrows(A);
	// we want a square system

	// INITIALISATION
	Matrix T=A;
	for (int k=1; k<=m; k++){
		T.set_val(k,k, (1./A(k,k)));
	}
	Vector temp = ones(m);
	Matrix eye = diag(temp);
  Vector c = T*v;
	T =  (eye - T);
	double tol = 1.e-14*v.norm();
	Vector x=c;
	//std::cout<<" before the while loop "<<(A*x -c).norm() << std::endl<<std::flush;
  Vector r = (A*x -v);
	while (r.norm() > tol){
		count+=1;
		x = T*x + c;
    r = (A*x -v);
	//	std::cout<<(A*x -v).norm() << std::endl<<std::flush;
	}
  //	std::cout<<count<<std::endl<<std::flush;

	return x;
}

// Assembly:
subq $0x138, %rsp            # imm = 0x138
movq %rdi, 0x20(%rsp)
movq %rdi, %rax
movq %rax, 0x28(%rsp)
movq %rdi, 0x130(%rsp)
movq %rsi, 0x128(%rsp)
movq %rdx, 0x120(%rsp)
movq %rcx, 0x118(%rsp)
movq 0x128(%rsp), %rdi
callq 0x3ac0
movl %eax, 0x34(%rsp)
movq 0x128(%rsp), %rdi
callq 0x3ad0
movl %eax, %ecx
movl 0x34(%rsp), %eax
cmpl %ecx, %eax
jne 0x8fce
jmp 0x8fed
leaq 0x633d(%rip), %rdi      # 0xf312
leaq 0x616d(%rip), %rsi      # 0xf149
movl $0x29a, %edx            # imm = 0x29A
leaq 0x63c1(%rip), %rcx      # 0xf3a9
callq 0x2090
movq 0x128(%rsp), %rdi
callq 0x3ac0
movl %eax, 0x114(%rsp)
movq 0x128(%rsp), %rsi
leaq 0x100(%rsp), %rdi
callq 0x5010
movl $0x1, 0xfc(%rsp)
movl 0xfc(%rsp), %eax
cmpl 0x114(%rsp), %eax
jg 0x90aa
movl 0xfc(%rsp), %edx
movl %edx, 0x10(%rsp)
movl %edx, %eax
movl %eax, 0x14(%rsp)
movq 0x128(%rsp), %rdi
movl %edx, %esi
callq 0x36e0
movq %rax, 0x18(%rsp)
jmp 0x9058
movl 0x10(%rsp), %edx
movl 0x14(%rsp), %esi
movq 0x18(%rsp), %rax
movsd 0x5fc3(%rip), %xmm0     # 0xf030
divsd (%rax), %xmm0
leaq 0x100(%rsp), %rdi
callq 0x47c0
movl 0xfc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xfc(%rsp)
jmp 0x9021
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf0(%rsp)
movl %eax, 0xec(%rsp)
jmp 0x94ac
movl 0x114(%rsp), %esi
leaq 0xd8(%rsp), %rdi
callq 0xe3d0
jmp 0x90c0
leaq 0xc8(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
callq 0x4f30
jmp 0x90d7
movq 0x120(%rsp), %rdx
leaq 0xb8(%rsp), %rdi
leaq 0x100(%rsp), %rsi
callq 0x4800
jmp 0x90f6
leaq 0xa8(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
leaq 0x100(%rsp), %rdx
callq 0x3ff0
jmp 0x9115
leaq 0x100(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x41e0
jmp 0x912c
leaq 0xa8(%rsp), %rdi
callq 0x3660
movq 0x120(%rsp), %rdi
movl $0x2, %esi
callq 0xe320
movsd %xmm0, 0x8(%rsp)
jmp 0x9153
movq 0x20(%rsp), %rdi
movsd 0x8(%rsp), %xmm0
movsd 0x5f2a(%rip), %xmm1     # 0xf090
mulsd %xmm1, %xmm0
movsd %xmm0, 0xa0(%rsp)
movb $0x0, 0x9f(%rsp)
leaq 0xb8(%rsp), %rsi
callq 0xd0d0
jmp 0x918a
movq 0x20(%rsp), %rdx
movq 0x128(%rsp), %rsi
leaq 0x78(%rsp), %rdi
callq 0x4800
jmp 0x91a3
movq 0x120(%rsp), %rdx
leaq 0x88(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0xd470
jmp 0x91bf
leaq 0x78(%rsp), %rdi
callq 0xd170
leaq 0x88(%rsp), %rdi
movl $0x2, %esi
callq 0xe320
movsd %xmm0, (%rsp)
jmp 0x91e2
movsd (%rsp), %xmm0
ucomisd 0xa0(%rsp), %xmm0
jbe 0x9400
movq 0x20(%rsp), %rdx
movq 0x118(%rsp), %rax
movl (%rax), %ecx
incl %ecx
movl %ecx, (%rax)
leaq 0x58(%rsp), %rdi
leaq 0x100(%rsp), %rsi
callq 0x4800
jmp 0x921d
leaq 0x68(%rsp), %rdi
leaq 0x58(%rsp), %rsi
leaq 0xb8(%rsp), %rdx
callq 0xd1a0
jmp 0x9236
movq 0x20(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0xdc70
jmp 0x9247
leaq 0x68(%rsp), %rdi
callq 0xd170
leaq 0x58(%rsp), %rdi
callq 0xd170
movq 0x20(%rsp), %rdx
movq 0x128(%rsp), %rsi
leaq 0x38(%rsp), %rdi
callq 0x4800
jmp 0x9274
movq 0x120(%rsp), %rdx
leaq 0x48(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0xd470
jmp 0x928d
leaq 0x88(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0xdc70
jmp 0x92a1
leaq 0x48(%rsp), %rdi
callq 0xd170
leaq 0x38(%rsp), %rdi
callq 0xd170
jmp 0x91c9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf0(%rsp)
movl %eax, 0xec(%rsp)
jmp 0x949f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf0(%rsp)
movl %eax, 0xec(%rsp)
jmp 0x9492
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf0(%rsp)
movl %eax, 0xec(%rsp)
jmp 0x9485
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf0(%rsp)
movl %eax, 0xec(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0x3660
jmp 0x9485
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf0(%rsp)
movl %eax, 0xec(%rsp)
jmp 0x947b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf0(%rsp)
movl %eax, 0xec(%rsp)
leaq 0x78(%rsp), %rdi
callq 0xd170
jmp 0x947b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf0(%rsp)
movl %eax, 0xec(%rsp)
jmp 0x9421
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf0(%rsp)
movl %eax, 0xec(%rsp)
jmp 0x93b4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf0(%rsp)
movl %eax, 0xec(%rsp)
leaq 0x68(%rsp), %rdi
callq 0xd170
leaq 0x58(%rsp), %rdi
callq 0xd170
jmp 0x9421
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf0(%rsp)
movl %eax, 0xec(%rsp)
jmp 0x93f4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf0(%rsp)
movl %eax, 0xec(%rsp)
leaq 0x48(%rsp), %rdi
callq 0xd170
leaq 0x38(%rsp), %rdi
callq 0xd170
jmp 0x9421
movb $0x1, 0x9f(%rsp)
leaq 0x88(%rsp), %rdi
callq 0xd170
testb $0x1, 0x9f(%rsp)
jne 0x943a
jmp 0x9430
leaq 0x88(%rsp), %rdi
callq 0xd170
jmp 0x947b
movq 0x20(%rsp), %rdi
callq 0xd170
leaq 0xb8(%rsp), %rdi
callq 0xd170
leaq 0xc8(%rsp), %rdi
callq 0x3660
leaq 0xd8(%rsp), %rdi
callq 0xd170
leaq 0x100(%rsp), %rdi
callq 0x3660
movq 0x28(%rsp), %rax
addq $0x138, %rsp            # imm = 0x138
retq
movq 0x20(%rsp), %rdi
callq 0xd170
leaq 0xb8(%rsp), %rdi
callq 0xd170
leaq 0xc8(%rsp), %rdi
callq 0x3660
leaq 0xd8(%rsp), %rdi
callq 0xd170
leaq 0x100(%rsp), %rdi
callq 0x3660
movq 0xf0(%rsp), %rdi
callq 0x21e0
nopw %cs:(%rax,%rax)