// File: /workspace/github_c_makefile_star2_O2/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, 0x10(%rsp)
movq %rdi, %r13
callq 0x383f4
movq %rax, %r15
movq %rdx, %r12
movq %rax, %rbp
shrq $0x20, %rbp
movq %rax, %rdi
sarq $0x1d, %rdi
andq $-0x8, %rdi
leaq 0x61c40(%rip), %rsi     # 0x99e60
pushq $0x5
popq %rdx
callq 0x34f59
movq %rax, %rbx
movq %r15, %rdi
movq %r12, 0x18(%rsp)
movq %r12, %rsi
movq %r15, (%rsp)
movl %r15d, %edx
movl %ebp, %ecx
movq %rax, %r8
callq 0x38462
movq %rax, 0x8(%rsp)
movq %rdx, %rsi
xorl %ecx, %ecx
testl %ebp, %ebp
movl $0x0, %eax
cmovgl %ebp, %eax
xorpd %xmm0, %xmm0
cmpq %rcx, %rax
je 0x3826e
addsd (%rbx,%rcx,8), %xmm0
incq %rcx
jmp 0x3825f
movq %r14, %r15
movl %ebp, (%r14)
xorpd %xmm1, %xmm1
xorl %r9d, %r9d
movsd 0x10(%rsp), %xmm3
cmpq %r9, %rax
je 0x3829f
addsd (%rbx,%r9,8), %xmm1
movapd %xmm1, %xmm2
divsd %xmm0, %xmm2
ucomisd %xmm3, %xmm2
ja 0x382a6
incq %r9
jmp 0x38281
decl %ebp
movl %ebp, %r9d
jmp 0x382ad
leal 0x1(%r9), %eax
movl %eax, (%r15)
decl %r13d
xorl %r12d, %r12d
xorl %edx, %edx
movl %r13d, %ecx
xorl %r8d, %r8d
movq %rsi, %r14
callq 0x39225
movq %rax, 0x10(%rsp)
movq %rdx, 0x20(%rsp)
movq %r14, %rdi
movq 0x8(%rsp), %rsi
callq 0x34fc3
movl (%r15), %r9d
decl %r9d
movq 0x18(%rsp), %r14
movq %r14, %rsi
xorl %edx, %edx
movl %r13d, %ecx
xorl %r8d, %r8d
callq 0x39225
movq %rax, %r13
movq %rdx, %rbp
movq %r14, %rdi
movq (%rsp), %rsi
callq 0x34fc3
movq %r13, %rdi
movq %rbp, %rsi
callq 0x392ac
movq %rax, 0x18(%rsp)
movq %rdx, 0x8(%rsp)
movq %rbp, %rdi
movl %r13d, %esi
callq 0x34fc3
movq %r15, %r14
movl (%r15), %edi
callq 0x3930e
movq %rdx, %r15
movl (%r14), %edx
testl %edx, %edx
cmovlel %r12d, %edx
movsd 0x5d1da(%rip), %xmm0    # 0x95520
cmpq %r12, %rdx
je 0x38364
movapd %xmm0, %xmm1
divsd (%rbx,%r12,8), %xmm1
movq (%r15,%r12,8), %rcx
movsd %xmm1, (%rcx,%r12,8)
incq %r12
jmp 0x38346
movq 0x10(%rsp), %r12
movq %r12, %rdi
movq 0x20(%rsp), %r13
movq %r13, %rsi
movq %rax, %rdx
movq %r15, %rcx
movq %rax, (%rsp)
callq 0x3933d
movq %rax, %r14
movq %rdx, %rbp
movq %r13, %rdi
movl %r12d, %esi
callq 0x34fc3
movq %r15, %rdi
movq (%rsp), %rsi
callq 0x34fc3
movq %rbx, %rdi
callq 0x34ff7
movq %r14, %rdi
movq %rbp, %rsi
movq 0x18(%rsp), %r12
movq %r12, %rdx
movq 0x8(%rsp), %r13
movq %r13, %rcx
callq 0x3933d
movq %rax, %rbx
movq %rdx, %r15
movq %rbp, %rdi
movl %r14d, %esi
callq 0x34fc3
movq %r13, %rdi
movl %r12d, %esi
callq 0x34fc3
movq %rbx, %rax
movq %r15, %rdx
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq