// File: /workspace/github_c_makefile_star2_O3/olcaytaner[P]Isell/multivariatetree.c
// Function: best_multivariate_split_lda_with_svd
int best_multivariate_split_lda_with_svd(Instanceptr positives, Instanceptr negatives, matrix* w, double* w0, MULTIVARIATE_TYPE multivariate_type, int* newdim, double variance_explained)
{
 /*!Last Changed 12.01.2009 added pooled_covariance*/
 /*!Last Changed 21.09.2004*/
 int positivecount, negativecount;
 Instance meanpositive, meannegative; 
 /* 1 x 1 matrices*/
 matrix splitmatrix;
 /* 1 x d matrices*/
 matrix meanposmatrix, meannegmatrix, meandifference, meansum;
 /* d x d matrices*/
 matrix sw, transposeresult, covariancenegative, covariancepositive, totalcovariance;
 double posratio, negratio;
 positivecount = data_size(positives);
 negativecount = data_size(negatives);
 if (positivecount <= 1 || negativecount <= 1)
  {
   *w = identity(1);
   return 0;
  }
 posratio = (positivecount + 0.0) / (positivecount + negativecount);
 negratio = (negativecount + 0.0) / (positivecount + negativecount);
 if (multivariate_type == MULTIVARIATE_LINEAR)
  {
   meanpositive = find_mean(positives);
   meannegative = find_mean(negatives);
   meanposmatrix = instance_to_matrix(&meanpositive);
   meannegmatrix = instance_to_matrix(&meannegative);
   covariancepositive = covariance(positives, meanpositive);
   covariancenegative = covariance(negatives, meannegative);
   safe_free(meanpositive.values);
   safe_free(meannegative.values);
  }
 else /*QUADRATIC*/
  {   
   meanposmatrix = find_mean_2d(positives);
   meannegmatrix = find_mean_2d(negatives);
   covariancepositive = covariance_2d(positives, meanposmatrix);
   covariancenegative = covariance_2d(negatives, meannegmatrix);
  }
 totalcovariance = pooled_covariance(covariancepositive, positivecount, covariancenegative, negativecount); 
 matrix_free(covariancepositive);
 matrix_free(covariancenegative);
 sw = pseudoinverse(totalcovariance, variance_explained, newdim);
 matrix_free(totalcovariance);
 meandifference = matrix_difference(meanposmatrix, meannegmatrix);
 *w = matrix_multiply(meandifference, sw);
 matrix_free(sw);
 matrix_free(meandifference);
 meansum = matrix_sum(meanposmatrix, meannegmatrix);
 matrix_free(meanposmatrix);
 matrix_free(meannegmatrix);
 transposeresult = matrix_transpose(*w);
 splitmatrix = matrix_multiply(meansum, transposeresult);
 matrix_free(meansum);
 matrix_free(transposeresult);
 *w0 = -(-0.5*splitmatrix.values[0][0]-log(negratio / posratio));
 matrix_free(splitmatrix);
 return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movsd %xmm0, 0x40(%rsp)
movq %r9, 0x30(%rsp)
movl %r8d, %ebx
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, %r13
movq %rdi, %rbp
callq 0x30309
movl %eax, %r12d
movq %r13, %rdi
callq 0x30309
movl %r12d, %edx
movl %eax, %r12d
cmpl $0x2, %edx
setl %al
cmpl $0x2, %r12d
setl %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x4d851
movl $0x1, %edi
callq 0x3eddb
movq %rax, (%r15)
movq %rdx, 0x8(%r15)
xorl %eax, %eax
jmp 0x4db4a
movq %r15, 0x88(%rsp)
movq %r14, 0x90(%rsp)
cvtsi2sd %edx, %xmm1
movl %edx, 0x4c(%rsp)
movl %edx, %eax
addl %r12d, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd %xmm0, %xmm1
movsd %xmm1, 0x80(%rsp)
xorps %xmm1, %xmm1
cvtsi2sd %r12d, %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x78(%rsp)
cmpl $-0x2, %ebx
jne 0x4d959
leaq 0xc8(%rsp), %r15
movq %r15, %rdi
movq %rbp, %rsi
callq 0x1247b
leaq 0x98(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x1247b
movq %r15, %rdi
callq 0x2f0c1
movq %rax, 0x70(%rsp)
movq %rdx, 0x68(%rsp)
movq %r14, %rdi
callq 0x2f0c1
movq %rax, 0x38(%rsp)
movq %rdx, 0x50(%rsp)
movups (%r15), %xmm0
movups 0x10(%r15), %xmm1
movups 0x20(%r15), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %rbp, %rdi
callq 0x11398
movq %rax, %rbp
movq %rdx, %rbx
movupd (%r14), %xmm0
movupd 0x10(%r14), %xmm1
movups 0x20(%r14), %xmm2
movups %xmm2, 0x20(%rsp)
movupd %xmm1, 0x10(%rsp)
movupd %xmm0, (%rsp)
movq %r13, %rdi
callq 0x11398
movq %rax, %r13
movq %rdx, %r14
movq (%r15), %rdi
callq 0x3a180
leaq 0x98(%rsp), %rax
movq (%rax), %rdi
callq 0x3a180
jmp 0x4d9b0
movq %rbp, %rdi
callq 0x121a7
movq %rax, %rbx
movq %rdx, %r14
movq %r13, %rdi
callq 0x121a7
movq %rax, %r15
movq %rdx, 0x50(%rsp)
movq %rbp, %rdi
movq %rbx, 0x70(%rsp)
movq %rbx, %rsi
movq %r14, 0x68(%rsp)
movq %r14, %rdx
callq 0x11049
movq %rax, %rbp
movq %rdx, %rbx
movq %r13, %rdi
movq %r15, 0x38(%rsp)
movq %r15, %rsi
movq 0x50(%rsp), %rdx
callq 0x11049
movq %rax, %r13
movq %rdx, %r14
movq %rbp, %rdi
movq %rbx, %rsi
movl 0x4c(%rsp), %edx
movq %r13, %rcx
movq %r14, %r8
movl %r12d, %r9d
callq 0x40497
movq %rax, %r15
movq %rdx, %r12
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x3ed51
movq %r13, %rdi
movq %r14, %rsi
callq 0x3ed51
movq %r15, %rdi
movq %r12, %rsi
movsd 0x40(%rsp), %xmm0
movq 0x30(%rsp), %rdx
callq 0x3d8dc
movq %rax, %r13
movq %rax, 0x30(%rsp)
movq %rdx, %r14
movq %r15, %rdi
movq %r12, %rsi
callq 0x3ed51
movq 0x70(%rsp), %rbp
movq %rbp, %rdi
movq 0x68(%rsp), %rbx
movq %rbx, %rsi
movq 0x38(%rsp), %rdx
movq 0x50(%rsp), %rcx
callq 0x3faf0
movq %rax, %r15
movq %rdx, %r12
movq %rax, %rdi
movq %rdx, %rsi
movq %r13, %rdx
movq %r14, %rcx
callq 0x3ee2f
movq 0x88(%rsp), %r13
movq %rax, (%r13)
movq %rdx, 0x8(%r13)
movq 0x30(%rsp), %rdi
movq %r14, %rsi
callq 0x3ed51
movq %r15, %rdi
movq %r12, %rsi
callq 0x3ed51
movq %rbp, %rdi
movq %rbx, %rsi
movq 0x38(%rsp), %r14
movq %r14, %rdx
movq 0x50(%rsp), %r15
movq %r15, %rcx
callq 0x3f9ef
movq %rax, 0x30(%rsp)
movq %rdx, 0x40(%rsp)
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x3ed51
movq %r14, %rdi
movq %r15, %rsi
callq 0x3ed51
movq (%r13), %rdi
movq 0x8(%r13), %rsi
callq 0x3ed5e
movq %rax, %r15
movq %rdx, %r12
movq 0x30(%rsp), %rbx
movq %rbx, %rdi
movq 0x40(%rsp), %r14
movq %r14, %rsi
movq %rax, %rdx
movq %r12, %rcx
callq 0x3ee2f
movq %rax, %r13
movq %rdx, %rbp
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3ed51
movq %r15, %rdi
movq %r12, %rsi
callq 0x3ed51
movq (%rbp), %rax
movsd (%rax), %xmm0
movaps %xmm0, 0x50(%rsp)
movsd 0x78(%rsp), %xmm0
divsd 0x80(%rsp), %xmm0
callq 0x4160
movapd 0x50(%rsp), %xmm1
mulsd 0x582ae(%rip), %xmm1    # 0xa5dd0
subsd %xmm0, %xmm1
xorpd 0x586a2(%rip), %xmm1    # 0xa61d0
movq 0x90(%rsp), %rax
movlpd %xmm1, (%rax)
movq %r13, %rdi
movq %rbp, %rsi
callq 0x3ed51
movl $0x1, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq