// File: yukimasano[P]cpp_matrix/Matrix.cpp
// Function: qr_q(Matrix&)
Matrix qr_q(Matrix& A){
	int m = ncols(A);
	Matrix R= A;
	Vector temp=ones(m);
	Matrix Q = diag(temp);
    for (int j = 0; j<m; j++){
    	Matrix x(m-j,1);
    	double len=0.0;
    	for (int l=j; l<m; l++){
    		x.set_val(l+1-j,1 ,R(l+1,j+1));
    		len+=R(l+1,j+1) * R(l+1,j+1);
    	}

    	len=sqrt(len);

    	int sign = (x(1,1) >= 0) - (x(1,1) < 0);
    	Matrix e(m-j,1);
    	e.set_val(1,1,1.0);

    	Matrix u = (sign*len*e + x);

    	// normalise u
    	len=0.0;
    	for (int i = 1; i<=m-j; i++){
    		len+=u(i,1)*u(i,1);
    	}
      if (len!=0){u= (1./len)*u;}
      //   	std::cout<<"here comes  u " << std::endl<< j<<"  "<< u << std::endl<<std::flush;

    	// SUBMULTIPLICATION    START /////////////////////////////
    	Matrix sub(m-j, m-j);
    	Matrix subQ(m,m-j);
    	for (int i = j; i<m; i++){
    		for (int h = j; h<m; h++){
    			sub.set_val(i+1 - j,h+1 - j, R(i+1,h+1));
    		}
    	}

    	for (int i = 0; i<m; i++){
			for (int h = j; h<m; h++){
				subQ.set_val(i+1,h+1-j,Q(i+1,h+1));
			}
		}

    	Matrix w=(u.T());
    	Matrix z= w*sub;
    	sub = sub -2.0*(u*z);

    	Vector temp2= ones(m-j);
    	Matrix subeye= diag(temp2);
    	Matrix temp3= subeye -2.0*(u*w);
    	subQ= subQ*(temp3);
    	for (int i = j; i<m; i++){
    		for (int h = j; h<m; h++){
    			R.set_val(i+1,h+1, sub(i+1- j,h+1- j));
    		}
    	}
      std::cout<<R<<std::flush<<std::endl;
    	for (int i = 0; i<m; i++){
  			for (int h = j; h<m; h++){
  				Q.set_val(i+1, h+1, subQ(i+1,h+1-j));
  			}
		  }
    }
    return Q;
}

// Assembly:
subq $0x288, %rsp            # imm = 0x288
movq %rdi, 0xb0(%rsp)
movq %rdi, %rax
movq %rax, 0xb8(%rsp)
movq %rdi, 0x280(%rsp)
movq %rsi, 0x278(%rsp)
movq 0x278(%rsp), %rdi
callq 0x3ad0
movl %eax, 0x274(%rsp)
movq 0x278(%rsp), %rsi
leaq 0x260(%rsp), %rdi
callq 0x5010
movl 0x274(%rsp), %esi
leaq 0x250(%rsp), %rdi
callq 0xe3d0
jmp 0xa189
movq 0xb0(%rsp), %rdi
movb $0x0, 0x243(%rsp)
leaq 0x250(%rsp), %rsi
callq 0x4f30
jmp 0xa1a8
movl $0x0, 0x23c(%rsp)
movl 0x23c(%rsp), %eax
cmpl 0x274(%rsp), %eax
jge 0xae9a
movl 0x274(%rsp), %esi
movl 0x23c(%rsp), %eax
subl %eax, %esi
leaq 0x228(%rsp), %rdi
movl $0x1, %edx
callq 0x3560
jmp 0xa1eb
xorps %xmm0, %xmm0
movsd %xmm0, 0x220(%rsp)
movl 0x23c(%rsp), %eax
movl %eax, 0x21c(%rsp)
movl 0x21c(%rsp), %eax
cmpl 0x274(%rsp), %eax
jge 0xa382
movl 0x21c(%rsp), %esi
incl %esi
movl 0x23c(%rsp), %edx
movl %esi, %eax
subl %edx, %eax
movl %eax, 0xa4(%rsp)
incl %edx
leaq 0x260(%rsp), %rdi
callq 0x36e0
movq %rax, 0xa8(%rsp)
jmp 0xa24d
movl 0xa4(%rsp), %esi
movq 0xa8(%rsp), %rax
movsd (%rax), %xmm0
leaq 0x228(%rsp), %rdi
movl $0x1, %edx
callq 0x47c0
movl 0x21c(%rsp), %esi
incl %esi
movl 0x23c(%rsp), %edx
incl %edx
leaq 0x260(%rsp), %rdi
callq 0x36e0
movq %rax, 0x98(%rsp)
jmp 0xa29b
movq 0x98(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x88(%rsp)
movl 0x21c(%rsp), %esi
incl %esi
movl 0x23c(%rsp), %edx
incl %edx
leaq 0x260(%rsp), %rdi
callq 0x36e0
movq %rax, 0x90(%rsp)
jmp 0xa2d9
movsd 0x88(%rsp), %xmm0
movq 0x90(%rsp), %rax
movsd (%rax), %xmm2
movsd 0x220(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x220(%rsp)
movl 0x21c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x21c(%rsp)
jmp 0xa205
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
jmp 0xaefd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
jmp 0xaef0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
jmp 0xaee3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
jmp 0xae8b
movsd 0x220(%rsp), %xmm0
callq 0x20d0
movsd %xmm0, 0x220(%rsp)
leaq 0x228(%rsp), %rdi
movl $0x1, %edx
movl %edx, %esi
callq 0x36e0
movq %rax, 0x80(%rsp)
jmp 0xa3b7
movq 0x80(%rsp), %rax
movsd (%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
setae %al
movzbl %al, %eax
movl %eax, 0x74(%rsp)
leaq 0x228(%rsp), %rdi
movl $0x1, %edx
movl %edx, %esi
callq 0x36e0
movq %rax, 0x78(%rsp)
jmp 0xa3ef
movl 0x74(%rsp), %eax
movq 0x78(%rsp), %rcx
movsd (%rcx), %xmm1
xorps %xmm0, %xmm0
ucomisd %xmm1, %xmm0
seta %cl
movzbl %cl, %ecx
subl %ecx, %eax
movl %eax, 0x218(%rsp)
movl 0x274(%rsp), %esi
movl 0x23c(%rsp), %eax
subl %eax, %esi
leaq 0x208(%rsp), %rdi
movl $0x1, %edx
callq 0x3560
jmp 0xa436
leaq 0x208(%rsp), %rdi
movq %rdi, 0x68(%rsp)
movl $0x1, %edx
movsd 0x4be0(%rip), %xmm0     # 0xf030
movl %edx, %esi
callq 0x47c0
movq 0x68(%rsp), %rdx
cvtsi2sdl 0x218(%rsp), %xmm0
movsd 0x220(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0x1e0(%rsp)
leaq 0x1e8(%rsp), %rdi
leaq 0x1e0(%rsp), %rsi
callq 0x4b60
jmp 0xa492
leaq 0x1f8(%rsp), %rdi
leaq 0x1e8(%rsp), %rsi
leaq 0x228(%rsp), %rdx
callq 0x3c80
jmp 0xa4b1
leaq 0x1e8(%rsp), %rdi
callq 0x3660
xorps %xmm0, %xmm0
movsd %xmm0, 0x220(%rsp)
movl $0x1, 0x1dc(%rsp)
movl 0x1dc(%rsp), %eax
movl 0x274(%rsp), %ecx
subl 0x23c(%rsp), %ecx
cmpl %ecx, %eax
jg 0xa5d8
movl 0x1dc(%rsp), %esi
leaq 0x1f8(%rsp), %rdi
movl $0x1, %edx
callq 0x36e0
movq %rax, 0x60(%rsp)
jmp 0xa512
movq 0x60(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x50(%rsp)
movl 0x1dc(%rsp), %esi
leaq 0x1f8(%rsp), %rdi
movl $0x1, %edx
callq 0x36e0
movq %rax, 0x58(%rsp)
jmp 0xa541
movsd 0x50(%rsp), %xmm0
movq 0x58(%rsp), %rax
movsd (%rax), %xmm2
movsd 0x220(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x220(%rsp)
movl 0x1dc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1dc(%rsp)
jmp 0xa4d5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
jmp 0xae7e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
leaq 0x1e8(%rsp), %rdi
callq 0x3660
jmp 0xae7e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
jmp 0xae71
movsd 0x220(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0xa5f1
jp 0xa5f1
jmp 0xa67a
movsd 0x220(%rsp), %xmm1
movsd 0x4a2e(%rip), %xmm0     # 0xf030
divsd %xmm1, %xmm0
movsd %xmm0, 0x1c0(%rsp)
leaq 0x1c8(%rsp), %rdi
leaq 0x1c0(%rsp), %rsi
leaq 0x1f8(%rsp), %rdx
callq 0x4b60
jmp 0xa62e
leaq 0x1f8(%rsp), %rdi
leaq 0x1c8(%rsp), %rsi
callq 0x41e0
jmp 0xa645
leaq 0x1c8(%rsp), %rdi
callq 0x3660
jmp 0xa67a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
leaq 0x1c8(%rsp), %rdi
callq 0x3660
jmp 0xae71
movl 0x274(%rsp), %edx
movl 0x23c(%rsp), %eax
subl %eax, %edx
leaq 0x1b0(%rsp), %rdi
movl %edx, %esi
callq 0x3560
jmp 0xa69b
movl 0x274(%rsp), %esi
movl 0x23c(%rsp), %eax
movl %esi, %edx
subl %eax, %edx
leaq 0x1a0(%rsp), %rdi
callq 0x3560
jmp 0xa6bc
movl 0x23c(%rsp), %eax
movl %eax, 0x19c(%rsp)
movl 0x19c(%rsp), %eax
cmpl 0x274(%rsp), %eax
jge 0xa7bb
movl 0x23c(%rsp), %eax
movl %eax, 0x198(%rsp)
movl 0x198(%rsp), %eax
cmpl 0x274(%rsp), %eax
jge 0xa7a3
movl 0x19c(%rsp), %esi
incl %esi
movl 0x23c(%rsp), %ecx
movl %esi, %eax
subl %ecx, %eax
movl %eax, 0x40(%rsp)
movl 0x198(%rsp), %edx
incl %edx
movl %edx, %eax
subl %ecx, %eax
movl %eax, 0x44(%rsp)
leaq 0x260(%rsp), %rdi
callq 0x36e0
movq %rax, 0x48(%rsp)
jmp 0xa73d
movl 0x44(%rsp), %edx
movl 0x40(%rsp), %esi
movq 0x48(%rsp), %rax
movsd (%rax), %xmm0
leaq 0x1b0(%rsp), %rdi
callq 0x47c0
movl 0x198(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x198(%rsp)
jmp 0xa6ec
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
jmp 0xae64
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
jmp 0xae57
jmp 0xa7a5
movl 0x19c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x19c(%rsp)
jmp 0xa6ca
movl $0x0, 0x194(%rsp)
movl 0x194(%rsp), %eax
cmpl 0x274(%rsp), %eax
jge 0xa87a
movl 0x23c(%rsp), %eax
movl %eax, 0x190(%rsp)
movl 0x190(%rsp), %eax
cmpl 0x274(%rsp), %eax
jge 0xa862
movq 0xb0(%rsp), %rdi
movl 0x194(%rsp), %esi
incl %esi
movl %esi, 0x30(%rsp)
movl 0x190(%rsp), %edx
incl %edx
movl 0x23c(%rsp), %ecx
movl %edx, %eax
subl %ecx, %eax
movl %eax, 0x34(%rsp)
callq 0x36e0
movq %rax, 0x38(%rsp)
jmp 0xa831
movl 0x34(%rsp), %edx
movl 0x30(%rsp), %esi
movq 0x38(%rsp), %rax
movsd (%rax), %xmm0
leaq 0x1a0(%rsp), %rdi
callq 0x47c0
movl 0x190(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x190(%rsp)
jmp 0xa7e8
jmp 0xa864
movl 0x194(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x194(%rsp)
jmp 0xa7c6
leaq 0x180(%rsp), %rdi
leaq 0x1f8(%rsp), %rsi
callq 0x39f0
jmp 0xa891
leaq 0x170(%rsp), %rdi
leaq 0x180(%rsp), %rsi
leaq 0x1b0(%rsp), %rdx
callq 0x45b0
jmp 0xa8b0
movabsq $0x4000000000000000, %rax # imm = 0x4000000000000000
movq %rax, 0x148(%rsp)
leaq 0x138(%rsp), %rdi
leaq 0x1f8(%rsp), %rsi
leaq 0x170(%rsp), %rdx
callq 0x45b0
jmp 0xa8e1
leaq 0x150(%rsp), %rdi
leaq 0x148(%rsp), %rsi
leaq 0x138(%rsp), %rdx
callq 0x4b60
jmp 0xa900
leaq 0x160(%rsp), %rdi
leaq 0x1b0(%rsp), %rsi
leaq 0x150(%rsp), %rdx
callq 0x3ff0
jmp 0xa91f
leaq 0x1b0(%rsp), %rdi
leaq 0x160(%rsp), %rsi
callq 0x41e0
jmp 0xa936
leaq 0x160(%rsp), %rdi
callq 0x3660
leaq 0x150(%rsp), %rdi
callq 0x3660
leaq 0x138(%rsp), %rdi
callq 0x3660
movl 0x274(%rsp), %esi
movl 0x23c(%rsp), %eax
subl %eax, %esi
leaq 0x128(%rsp), %rdi
callq 0xe3d0
jmp 0xa97c
leaq 0x118(%rsp), %rdi
leaq 0x128(%rsp), %rsi
callq 0x4f30
jmp 0xa993
movabsq $0x4000000000000000, %rax # imm = 0x4000000000000000
movq %rax, 0xf0(%rsp)
leaq 0xe0(%rsp), %rdi
leaq 0x1f8(%rsp), %rsi
leaq 0x180(%rsp), %rdx
callq 0x45b0
jmp 0xa9c4
leaq 0xf8(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
leaq 0xe0(%rsp), %rdx
callq 0x4b60
jmp 0xa9e3
leaq 0x108(%rsp), %rdi
leaq 0x118(%rsp), %rsi
leaq 0xf8(%rsp), %rdx
callq 0x3ff0
jmp 0xaa02
leaq 0xf8(%rsp), %rdi
callq 0x3660
leaq 0xe0(%rsp), %rdi
callq 0x3660
leaq 0xd0(%rsp), %rdi
leaq 0x1a0(%rsp), %rsi
leaq 0x108(%rsp), %rdx
callq 0x45b0
jmp 0xaa3b
leaq 0x1a0(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x41e0
jmp 0xaa52
leaq 0xd0(%rsp), %rdi
callq 0x3660
movl 0x23c(%rsp), %eax
movl %eax, 0xcc(%rsp)
movl 0xcc(%rsp), %eax
cmpl 0x274(%rsp), %eax
jge 0xac7d
movl 0x23c(%rsp), %eax
movl %eax, 0xc8(%rsp)
movl 0xc8(%rsp), %eax
cmpl 0x274(%rsp), %eax
jge 0xac65
movl 0xcc(%rsp), %esi
incl %esi
movl %esi, 0x20(%rsp)
movl 0xc8(%rsp), %edx
incl %edx
movl %edx, 0x24(%rsp)
movl 0x23c(%rsp), %eax
subl %eax, %esi
subl %eax, %edx
leaq 0x1b0(%rsp), %rdi
callq 0x36e0
movq %rax, 0x28(%rsp)
jmp 0xaadc
movl 0x24(%rsp), %edx
movl 0x20(%rsp), %esi
movq 0x28(%rsp), %rax
movsd (%rax), %xmm0
leaq 0x260(%rsp), %rdi
callq 0x47c0
movl 0xc8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc8(%rsp)
jmp 0xaa8f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
jmp 0xae4a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
jmp 0xae3d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
jmp 0xab99
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
jmp 0xab8c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
leaq 0x160(%rsp), %rdi
callq 0x3660
leaq 0x150(%rsp), %rdi
callq 0x3660
leaq 0x138(%rsp), %rdi
callq 0x3660
jmp 0xae3d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
jmp 0xae30
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
jmp 0xae23
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
jmp 0xac14
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
leaq 0xf8(%rsp), %rdi
callq 0x3660
leaq 0xe0(%rsp), %rdi
callq 0x3660
jmp 0xae23
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
jmp 0xae16
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
leaq 0xd0(%rsp), %rdi
callq 0x3660
jmp 0xae16
jmp 0xac67
movl 0xcc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xcc(%rsp)
jmp 0xaa6d
movq 0x7344(%rip), %rdi      # 0x11fc8
leaq 0x260(%rsp), %rsi
callq 0x4c40
movq %rax, 0x18(%rsp)
jmp 0xac98
movq 0x18(%rsp), %rdi
movq 0x732c(%rip), %rsi      # 0x11fd0
callq 0x2120
movq %rax, 0x10(%rsp)
jmp 0xacb0
movq 0x10(%rsp), %rdi
movq 0x72fc(%rip), %rsi      # 0x11fb8
callq 0x2120
jmp 0xacc3
movl $0x0, 0xc4(%rsp)
movl 0xc4(%rsp), %eax
cmpl 0x274(%rsp), %eax
jge 0xad7e
movl 0x23c(%rsp), %eax
movl %eax, 0xc0(%rsp)
movl 0xc0(%rsp), %eax
cmpl 0x274(%rsp), %eax
jge 0xad66
movl 0xc4(%rsp), %esi
incl %esi
movl %esi, (%rsp)
movl 0xc0(%rsp), %edx
incl %edx
movl %edx, 0x4(%rsp)
movl 0x23c(%rsp), %eax
subl %eax, %edx
leaq 0x1a0(%rsp), %rdi
callq 0x36e0
movq %rax, 0x8(%rsp)
jmp 0xad36
movl 0x4(%rsp), %edx
movl (%rsp), %esi
movq 0xb0(%rsp), %rdi
movq 0x8(%rsp), %rax
movsd (%rax), %xmm0
callq 0x47c0
movl 0xc0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc0(%rsp)
jmp 0xacf0
jmp 0xad68
movl 0xc4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc4(%rsp)
jmp 0xacce
leaq 0x108(%rsp), %rdi
callq 0x3660
leaq 0x118(%rsp), %rdi
callq 0x3660
leaq 0x128(%rsp), %rdi
callq 0xd170
leaq 0x170(%rsp), %rdi
callq 0x3660
leaq 0x180(%rsp), %rdi
callq 0x3660
leaq 0x1a0(%rsp), %rdi
callq 0x3660
leaq 0x1b0(%rsp), %rdi
callq 0x3660
leaq 0x1f8(%rsp), %rdi
callq 0x3660
leaq 0x208(%rsp), %rdi
callq 0x3660
leaq 0x228(%rsp), %rdi
callq 0x3660
movl 0x23c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x23c(%rsp)
jmp 0xa1b3
leaq 0x108(%rsp), %rdi
callq 0x3660
leaq 0x118(%rsp), %rdi
callq 0x3660
leaq 0x128(%rsp), %rdi
callq 0xd170
leaq 0x170(%rsp), %rdi
callq 0x3660
leaq 0x180(%rsp), %rdi
callq 0x3660
leaq 0x1a0(%rsp), %rdi
callq 0x3660
leaq 0x1b0(%rsp), %rdi
callq 0x3660
leaq 0x1f8(%rsp), %rdi
callq 0x3660
leaq 0x208(%rsp), %rdi
callq 0x3660
leaq 0x228(%rsp), %rdi
callq 0x3660
jmp 0xaee3
movb $0x1, 0x243(%rsp)
testb $0x1, 0x243(%rsp)
jne 0xaeb9
movq 0xb0(%rsp), %rdi
callq 0x3660
leaq 0x250(%rsp), %rdi
callq 0xd170
leaq 0x260(%rsp), %rdi
callq 0x3660
movq 0xb8(%rsp), %rax
addq $0x288, %rsp            # imm = 0x288
retq
movq 0xb0(%rsp), %rdi
callq 0x3660
leaq 0x250(%rsp), %rdi
callq 0xd170
leaq 0x260(%rsp), %rdi
callq 0x3660
movq 0x248(%rsp), %rdi
callq 0x21e0
nopw (%rax,%rax)