// File: /workspace/github_c_makefile_star2_O1/olcaytaner[P]Isell/multivariatetree.c
// Function: best_multivariate_split_lda_with_pca
int best_multivariate_split_lda_with_pca(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, newdimension, dim, matrixdim;
 Instance meanpositive, meannegative; 
 /* 1 x 1 matrices*/
 matrix splitmatrix;
 /* 1 x d matrices*/
 matrix meanposmatrix, meannegmatrix, meandifference, meansum;
 /* 1 x k matrices*/
 matrix bmatrix, tmpresult2, tmpresult3;
 /* k x 1 matrices*/
 matrix transposeresult;
 /* d x d matrices*/
 matrix covariancenegative, covariancepositive, totalcovariance, tmpeigenvec;
 /* d x k matrices*/
 matrix eigenvec;
 /* k x d matrices*/
 matrix transposeeigenvec, tmpresult;
 /* k x k matrices*/
 matrix sw;
 double* eigenval = NULL, posratio, negratio;
 positivecount = data_size(positives);
 negativecount = data_size(negatives);
 dim = current_dataset->multifeaturecount - 1;
 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);
   matrixdim = dim;
   safe_free(meanpositive.values);
   safe_free(meannegative.values);
  }
 else /*QUADRATIC*/
  {   
   meanposmatrix = find_mean_2d(positives);
   meannegmatrix = find_mean_2d(negatives);
   matrixdim = multifeaturecount_2d(current_dataset);
   covariancepositive = covariance_2d(positives, meanposmatrix);
   covariancenegative = covariance_2d(negatives, meannegmatrix);
  }
 totalcovariance = pooled_covariance(covariancepositive, positivecount, covariancenegative, negativecount);
 matrix_free(covariancepositive);
 matrix_free(covariancenegative);
 eigenval = find_eigenvalues_eigenvectors(&tmpeigenvec, totalcovariance, &newdimension, variance_explained);
 safe_free(eigenval);
 *newdim = newdimension + 1;
 eigenvec = matrix_partial(tmpeigenvec, 0, matrixdim - 1, 0, newdimension);
 matrix_free(tmpeigenvec);
 transposeeigenvec = matrix_transpose(eigenvec);
 tmpresult = matrix_multiply(transposeeigenvec, totalcovariance);
 matrix_free(totalcovariance);
 sw = matrix_multiply(tmpresult, eigenvec);
 matrix_free(tmpresult);
 meandifference = matrix_difference(meanposmatrix, meannegmatrix);
 bmatrix = matrix_multiply(meandifference, eigenvec);
 matrix_free(meandifference);
 matrix_inverse(&sw);
 tmpresult2 = matrix_multiply(bmatrix, sw);
 *w = matrix_multiply(tmpresult2, transposeeigenvec);
 matrix_free(transposeeigenvec);
 matrix_free(bmatrix);
 matrix_free(sw);
 meansum = matrix_sum(meanposmatrix, meannegmatrix);
 matrix_free(meanposmatrix);
 matrix_free(meannegmatrix);
 tmpresult3 = matrix_multiply(meansum, eigenvec);
 matrix_free(eigenvec);
 matrix_free(meansum);
 transposeresult = matrix_transpose(tmpresult2);
 matrix_free(tmpresult2);
 splitmatrix = matrix_multiply(tmpresult3, transposeresult);
 matrix_free(tmpresult3);
 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 $0x138, %rsp            # imm = 0x138
movsd %xmm0, 0x48(%rsp)
movq %r9, 0x50(%rsp)
movl %r8d, %r14d
movq %rcx, %r12
movq %rdx, %rbx
movq %rsi, %r13
movq %rdi, %rbp
callq 0x2fa14
movl %eax, %r15d
movq %r13, %rdi
callq 0x2fa14
movl %r15d, %edx
movl %eax, %r15d
cmpl $0x2, %edx
setl %al
cmpl $0x2, %r15d
setl %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x4beaf
movl $0x1, %edi
callq 0x3dc37
movq %rax, (%rbx)
movq %rdx, 0x8(%rbx)
xorl %eax, %eax
jmp 0x4c32f
movq %rbx, 0x90(%rsp)
movq %r12, 0xa8(%rsp)
leaq 0x869aa(%rip), %rcx     # 0xd2870
cvtsi2sd %edx, %xmm1
movl %edx, 0x40(%rsp)
movl %edx, %eax
addl %r15d, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd %xmm0, %xmm1
movsd %xmm1, 0xa0(%rsp)
xorps %xmm1, %xmm1
cvtsi2sd %r15d, %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x98(%rsp)
cmpl $-0x2, %r14d
jne 0x4bfce
movq (%rcx), %rax
movl 0x1c0(%rax), %eax
decl %eax
movl %eax, 0x38(%rsp)
leaq 0xd8(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x12519
leaq 0x108(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x12519
movq %rbx, %rdi
callq 0x2e851
movq %rax, 0x68(%rsp)
movq %rdx, 0x60(%rsp)
movq %r14, %rdi
callq 0x2e851
movq %rax, 0x58(%rsp)
movq %rdx, 0x70(%rsp)
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movups 0x20(%rbx), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %rbp, %rdi
callq 0x113c9
movq %rax, %rbx
movq %rdx, %rbp
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 0x113c9
movq %rax, %r13
movq %rdx, %r12
leaq 0xd8(%rsp), %rax
movq (%rax), %rdi
callq 0x39194
movq (%r14), %rdi
callq 0x39194
jmp 0x4c038
movq %rbp, %rdi
callq 0x12201
movq %rax, %rbx
movq %rdx, %r14
movq %r13, %rdi
callq 0x12201
movq %rax, %r12
movq %rdx, 0x70(%rsp)
leaq 0x8687d(%rip), %rax     # 0xd2870
movq (%rax), %rdi
callq 0x13726
movl %eax, 0x38(%rsp)
movq %rbp, %rdi
movq %rbx, 0x68(%rsp)
movq %rbx, %rsi
movq %r14, 0x60(%rsp)
movq %r14, %rdx
callq 0x11072
movq %rax, %rbx
movq %rdx, %rbp
movq %r13, %rdi
movq %r12, 0x58(%rsp)
movq %r12, %rsi
movq 0x70(%rsp), %rdx
callq 0x11072
movq %rax, %r13
movq %rdx, %r12
movq %rbx, %rdi
movq %rbp, %rsi
movl 0x40(%rsp), %edx
movq %r13, %rcx
movq %r12, %r8
movl %r15d, %r9d
callq 0x3f13e
movq %rbp, %rsi
movq %rax, %rbp
movq %rdx, %r14
movq %rbx, %rdi
callq 0x3dbad
movq %r13, %rdi
movq %r12, %rsi
callq 0x3dbad
leaq 0xc8(%rsp), %rbx
leaq 0xb4(%rsp), %r15
movq %rbx, %rdi
movq %rbp, %rsi
movq %r14, %rdx
movq %r15, %rcx
movsd 0x48(%rsp), %xmm0
callq 0x415a9
movq %rax, %rdi
callq 0x39194
movl (%r15), %r9d
leal 0x1(%r9), %eax
movq 0x50(%rsp), %rcx
movl %eax, (%rcx)
movl 0x38(%rsp), %ecx
decl %ecx
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0x3daf9
movq %rax, %r15
movq %rdx, %r13
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
callq 0x3dbad
movq %r15, %rdi
movq %r15, %rbx
movq %r13, %rsi
callq 0x3dbba
movq %rax, 0x40(%rsp)
movq %rdx, 0x38(%rsp)
movq %rax, %rdi
movq %rdx, %rsi
movq %rbp, %rdx
movq %r14, %rcx
callq 0x3dc8b
movq %rax, %r15
movq %rdx, %r12
movq %rbp, %rdi
movq %r14, %rsi
callq 0x3dbad
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rdx
movq %rbx, 0x48(%rsp)
movq %r13, 0x50(%rsp)
movq %r13, %rcx
callq 0x3dc8b
leaq 0xb8(%rsp), %rcx
movq %rax, (%rcx)
movq %rdx, 0x8(%rcx)
movq %r15, %rdi
movq %r12, %rsi
callq 0x3dbad
movq 0x68(%rsp), %rdi
movq 0x60(%rsp), %rsi
movq 0x58(%rsp), %rdx
movq 0x70(%rsp), %rcx
callq 0x3e799
movq %rax, %r14
movq %rdx, %rbp
movq %rax, %rdi
movq %rdx, %rsi
movq %rbx, %rdx
movq %r13, %rcx
callq 0x3dc8b
movq %rax, %r15
movq %rdx, %r12
movq %r14, %rdi
movq %rbp, %rsi
callq 0x3dbad
leaq 0xb8(%rsp), %rbx
movq %rbx, %rdi
callq 0x3f648
movq (%rbx), %rdx
movq 0x8(%rbx), %rcx
movq %rbx, %r13
movq %r15, %rdi
movq %r12, %rsi
callq 0x3dc8b
movq %rax, 0x80(%rsp)
movq %rdx, 0x88(%rsp)
movq %rax, %rdi
movq %rdx, %rsi
movq 0x40(%rsp), %rbx
movq %rbx, %rdx
movq 0x38(%rsp), %r14
movq %r14, %rcx
callq 0x3dc8b
movq 0x90(%rsp), %rcx
movq %rax, (%rcx)
movq %rdx, 0x8(%rcx)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3dbad
movq %r15, %rdi
movq %r12, %rsi
callq 0x3dbad
movq (%r13), %rdi
movq 0x8(%r13), %rsi
callq 0x3dbad
movq 0x68(%rsp), %r15
movq %r15, %rdi
movq 0x60(%rsp), %rbp
movq %rbp, %rsi
movq 0x58(%rsp), %rbx
movq %rbx, %rdx
movq 0x70(%rsp), %r14
movq %r14, %rcx
callq 0x3e6a0
movq %rax, %r12
movq %rdx, %r13
movq %r15, %rdi
movq %rbp, %rsi
callq 0x3dbad
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3dbad
movq %r12, %rdi
movq %r13, %rsi
movq 0x48(%rsp), %rbp
movq %rbp, %rdx
movq 0x50(%rsp), %r14
movq %r14, %rcx
callq 0x3dc8b
movq %rax, %rbx
movq %rdx, %r15
movq %rbp, %rdi
movq %r14, %rsi
callq 0x3dbad
movq %r12, %rdi
movq %r13, %rsi
callq 0x3dbad
movq 0x80(%rsp), %rbp
movq %rbp, %rdi
movq 0x88(%rsp), %r14
movq %r14, %rsi
callq 0x3dbba
movq %rax, %r12
movq %rdx, %r13
movq %rbp, %rdi
movq %r14, %rsi
callq 0x3dbad
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %r13, %rcx
callq 0x3dc8b
movq %rax, %r14
movq %rdx, %rbp
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3dbad
movq %r12, %rdi
movq %r13, %rsi
callq 0x3dbad
movq (%rbp), %rax
movsd (%rax), %xmm0
movaps %xmm0, 0x70(%rsp)
movsd 0x98(%rsp), %xmm0
divsd 0xa0(%rsp), %xmm0
callq 0x4160
movapd 0x70(%rsp), %xmm1
mulsd 0x56ac9(%rip), %xmm1    # 0xa2dd0
subsd %xmm0, %xmm1
xorpd 0x56ebd(%rip), %xmm1    # 0xa31d0
movq 0xa8(%rsp), %rax
movlpd %xmm1, (%rax)
movq %r14, %rdi
movq %rbp, %rsi
callq 0x3dbad
movl $0x1, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq