// File: /workspace/github_c_makefile_star2_O1/olcaytaner[P]Isell/matrix.c
// Function: pseudoinverse
matrix pseudoinverse(matrix a, double range, int* newdim)
{
	/*!Last Changed 02.02.2005*/
	int i;
	matrix u, v, s, partv, partu, transu, result, tmpresult;
	double* w, sum, sumnow;
 u = matrix_copy(a);
	w = safemalloc(u.col * sizeof(double), "pseuodoinverse", 5);
	v = svdcmp(u, u.row, u.col, w);   
	sum = 0;
	for (i = 0; i < u.col; i++)
		 sum += w[i];
	*newdim = u.col;
	sumnow = 0;
	for (i = 0; i < u.col; i++)
	 {
		 sumnow += w[i];
			if (sumnow / sum > range)
			 {
				 *newdim = i + 1;
				 break;
			 }
	 }
	partv = matrix_partial(v, 0, a.row - 1, 0, *newdim - 1);
	matrix_free(v);
	partu = matrix_partial(u, 0, a.row - 1, 0, *newdim - 1);
	matrix_free(u);
	transu = matrix_transpose(partu);
	matrix_free(partu);
	s = identity(*newdim);
	for (i = 0; i < *newdim; i++)
		 s.values[i][i] = 1 / w[i];
 tmpresult = matrix_multiply(partv, s);
	matrix_free(partv);
	matrix_free(s);
	safe_free(w);
	result = matrix_multiply(tmpresult, transu);
	matrix_free(tmpresult);
	matrix_free(transu);
	return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, %r14
movsd %xmm0, 0x8(%rsp)
movq %rdi, %r13
callq 0x3ca3c
movq %rax, %r15
movq %rdx, %rbp
movq %rax, %r12
shrq $0x20, %r12
movq %rax, %rdi
sarq $0x1d, %rdi
andq $-0x8, %rdi
leaq 0x6a607(%rip), %rsi     # 0xa6e90
movl $0x5, %edx
callq 0x390f5
movq %rax, %rbx
movq %r15, %rdi
movq %rbp, %rsi
movl %r15d, %edx
movl %r12d, %ecx
movq %rax, %r8
callq 0x3cabd
movq %rax, (%rsp)
movq %rdx, %rsi
xorpd %xmm0, %xmm0
testl %r12d, %r12d
jle 0x3c8c9
xorl %eax, %eax
addsd (%rbx,%rax,8), %xmm0
incq %rax
cmpq %rax, %r12
jne 0x3c8bc
movl %r12d, (%r14)
testl %r12d, %r12d
movsd 0x8(%rsp), %xmm3
jle 0x3c8ff
xorpd %xmm1, %xmm1
xorl %eax, %eax
addsd (%rbx,%rax,8), %xmm1
movapd %xmm1, %xmm2
divsd %xmm0, %xmm2
ucomisd %xmm3, %xmm2
ja 0x3c8fa
incq %rax
cmpq %rax, %r12
jne 0x3c8dd
jmp 0x3c8ff
incl %eax
movl %eax, (%r14)
decl %r13d
movl (%r14), %r9d
decl %r9d
xorl %edx, %edx
movl %r13d, %ecx
xorl %r8d, %r8d
movq %rsi, %r12
callq 0x3daf9
movq %rax, 0x8(%rsp)
movq %rdx, 0x20(%rsp)
movq %r12, %rdi
movq (%rsp), %rsi
callq 0x3915f
movl (%r14), %r9d
decl %r9d
movq %rbp, %rsi
xorl %edx, %edx
movl %r13d, %ecx
xorl %r8d, %r8d
callq 0x3daf9
movq %rax, %r12
movq %rbp, %rdi
movq %rdx, %rbp
movl %r15d, %esi
callq 0x3915f
movq %r12, %rdi
movq %rbp, %rsi
callq 0x3dbba
movq %rax, (%rsp)
movq %rdx, 0x18(%rsp)
movq %rbp, %rdi
movl %r12d, %esi
callq 0x3915f
movl (%r14), %edi
callq 0x3dc37
movq %rdx, %r15
movslq (%r14), %rsi
testq %rsi, %rsi
jle 0x3c9ab
xorl %ecx, %ecx
movsd 0x65b8f(%rip), %xmm0    # 0xa2520
movapd %xmm0, %xmm1
divsd (%rbx,%rcx,8), %xmm1
movq (%r15,%rcx,8), %rdx
movsd %xmm1, (%rdx,%rcx,8)
incq %rcx
cmpq %rcx, %rsi
jne 0x3c991
movq 0x8(%rsp), %r13
movq %r13, %rdi
movq 0x20(%rsp), %r12
movq %r12, %rsi
movq %rax, %rdx
movq %r15, %rcx
movq %rax, 0x10(%rsp)
callq 0x3dc8b
movq %rax, %r14
movq %rdx, %rbp
movq %r12, %rdi
movl %r13d, %esi
callq 0x3915f
movq %r15, %rdi
movq 0x10(%rsp), %rsi
callq 0x3915f
movq %rbx, %rdi
callq 0x39194
movq %r14, %rdi
movq %rbp, %rsi
movq (%rsp), %r12
movq %r12, %rdx
movq 0x18(%rsp), %r13
movq %r13, %rcx
callq 0x3dc8b
movq %rax, %rbx
movq %rdx, %r15
movq %rbp, %rdi
movl %r14d, %esi
callq 0x3915f
movq %r13, %rdi
movl %r12d, %esi
callq 0x3915f
movq %rbx, %rax
movq %r15, %rdx
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq