// File: /workspace/github_c_makefile_star2_O3/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, %r12
movsd %xmm0, (%rsp)
movq %rdi, 0x10(%rsp)
callq 0x3dae3
movq %rax, %r15
movq %rdx, %r14
movq %rax, %rbp
shrq $0x20, %rbp
movq %rax, %rdi
sarq $0x1d, %rdi
andq $-0x8, %rdi
leaq 0x6c615(%rip), %rsi     # 0xa9f30
movl $0x5, %edx
callq 0x3a0e1
movq %rax, %rbx
movq %r15, %rdi
movq %r14, 0x18(%rsp)
movq %r14, %rsi
movl %r15d, %edx
movl %ebp, %ecx
movq %rax, %r8
callq 0x3db64
movq %rax, 0x8(%rsp)
movq %rdx, %r13
testl %ebp, %ebp
jle 0x3d993
xorpd %xmm0, %xmm0
xorl %eax, %eax
addsd (%rbx,%rax,8), %xmm0
incq %rax
cmpq %rax, %rbp
jne 0x3d952
movl %ebp, (%r12)
testl %ebp, %ebp
movsd (%rsp), %xmm3
jle 0x3d997
xorpd %xmm1, %xmm1
xorl %eax, %eax
addsd (%rbx,%rax,8), %xmm1
movapd %xmm1, %xmm2
divsd %xmm0, %xmm2
ucomisd %xmm3, %xmm2
ja 0x3d98f
incq %rax
cmpq %rax, %rbp
jne 0x3d972
jmp 0x3d997
incl %eax
movl %eax, %ebp
movl %ebp, (%r12)
movq 0x10(%rsp), %rax
leal -0x1(%rax), %r14d
decl %ebp
movq %r13, %rsi
xorl %edx, %edx
movl %r14d, %ecx
xorl %r8d, %r8d
movl %ebp, %r9d
callq 0x3ec9d
movq %rax, 0x10(%rsp)
movq %rdx, (%rsp)
movq %r13, %rdi
movq 0x8(%rsp), %rsi
callq 0x3a14b
movl (%r12), %r9d
decl %r9d
movq 0x18(%rsp), %r13
movq %r13, %rsi
xorl %edx, %edx
movl %r14d, %ecx
xorl %r8d, %r8d
callq 0x3ec9d
movq %rax, %r14
movq %rdx, %rbp
movq %r13, %rdi
movl %r15d, %esi
callq 0x3a14b
movq %r14, %rdi
movq %rbp, %rsi
callq 0x3ed5e
movq %rax, 0x18(%rsp)
movq %rdx, 0x8(%rsp)
movq %rbp, %rdi
movl %r14d, %esi
callq 0x3a14b
movl (%r12), %edi
callq 0x3eddb
movq %rdx, %rbp
movslq (%r12), %rsi
testq %rsi, %rsi
jle 0x3da52
xorl %ecx, %ecx
movsd 0x67ae9(%rip), %xmm0    # 0xa5520
movapd %xmm0, %xmm1
divsd (%rbx,%rcx,8), %xmm1
movq (%rbp,%rcx,8), %rdx
movsd %xmm1, (%rdx,%rcx,8)
incq %rcx
cmpq %rcx, %rsi
jne 0x3da37
movq 0x10(%rsp), %r13
movq %r13, %rdi
movq (%rsp), %r14
movq %r14, %rsi
movq %rax, %rdx
movq %rbp, %rcx
movq %rax, 0x20(%rsp)
callq 0x3ee2f
movq %rax, %r15
movq %rdx, %r12
movq %r14, %rdi
movl %r13d, %esi
callq 0x3a14b
movq %rbp, %rdi
movq 0x20(%rsp), %rsi
callq 0x3a14b
movq %rbx, %rdi
callq 0x3a180
movq %r15, %rdi
movq %r12, %rsi
movq 0x18(%rsp), %r13
movq %r13, %rdx
movq 0x8(%rsp), %rbp
movq %rbp, %rcx
callq 0x3ee2f
movq %rax, %rbx
movq %rdx, %r14
movq %r12, %rdi
movl %r15d, %esi
callq 0x3a14b
movq %rbp, %rdi
movl %r13d, %esi
callq 0x3a14b
movq %rbx, %rax
movq %r14, %rdx
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq