// File: /workspace/github_c_makefile_star2_O3/olcaytaner[P]Isell/mlp.c
// Function: hessian_matrix_cls
matrix hessian_matrix_cls(Mlpnetwork N, Instanceptr train)
{
	/*!Last Changed 18.06.2005*/
	int weightcount, firstlayer, k, h, hprime, i, iprime, index1, index2, classno;
	double mul, yk, zh, sum1, sum2, zhprime, xi, xiprime, vkh, vkhprime;
	Instanceptr tmp;
	matrix result, testY, testH, inst;
	weightcount = number_of_weights_in_mlp_network(N);
	result = matrix_alloc(weightcount, weightcount);
	testY = matrix_alloc(1, N.P.classnum);
	testH = matrix_alloc(1, N.P.hidden[0]);
	firstlayer = N.weights[0].row * N.weights[0].col;
	tmp = train;
	while (tmp)
	 {
		 classno = give_classno(tmp);
		 inst = instance_to_matrix(tmp);
  	calculate_hidden_values(inst.values[0], N.weights[0], &testH);
		 calculate_output_values_cls(testH.values[0], N.weights[1], &testY);
			/*Both in the second layer*/
   for (k = 0; k < N.weights[1].row; k++)
			 {
					yk = testY.values[0][k];
				 for (h = 0; h < N.weights[1].col; h++)
					 {
						 zh = testH.values[0][h];
  					for (hprime = 0; hprime < N.weights[1].col; hprime++)
							 {
									zhprime = testH.values[0][hprime];
									index1 = firstlayer + k * N.weights[1].col + h;
									index2 = firstlayer + k * N.weights[1].col + hprime;
									result.values[index1][index2] += zh * zhprime * yk * (1 - yk);
							 }
					 }
			 }
   /*Both in the first layer*/
			index1 = 0;
			for (h = 0; h < N.weights[0].row; h++)
			 {
					zh = testH.values[0][h];
				 for (i = 0; i < N.weights[0].col; i++)
					 {
						 xi = inst.values[0][i];
						 index2 = 0;
						 for (hprime = 0; hprime < N.weights[0].row; hprime++)
							 {
								 zhprime = testH.values[0][hprime];
								 if (h == hprime)
									 {
           sum1 = 0;
											for (k = 0; k < N.weights[1].row; k++)
											 {
												 vkhprime = N.weights[1].values[k][hprime + 1];
													yk = testY.values[0][k];
												 if (k == classno)
														 sum1 += vkhprime * (yk - 1);
													else
														 sum1 += vkhprime * yk;
											 }
											sum1 = sum1 * (1 - 2 * zhprime);
									 }
									else
										 sum1 = 0;
									sum2 = 0;
         for (k = 0; k < N.weights[1].row; k++)
									 {
										 vkh = N.weights[1].values[k][h + 1];
											vkhprime = N.weights[1].values[k][hprime + 1];
											yk = testY.values[0][k];
											sum2 += vkh * vkhprime * yk * (1 - yk);
									 }
         sum2 = zh * (1 - zh) * sum2;
								 for (iprime = 0; iprime < N.weights[0].col; iprime++)
									 { 
										 xiprime = inst.values[0][iprime];
    							mul = xi * xiprime * zhprime * (1 - zhprime);
           result.values[index1][index2] += mul * (sum1 + sum2);
										 index2++;
									 }
							 }
						 index1++;
					 }
			 }
   /*One weight in each layer*/
			index1 = 0;
			for (h = 0; h < N.weights[0].row; h++)
			 {
				 zh = testH.values[0][h];
				 for (i = 0; i < N.weights[0].col; i++)
					 {
						 index2 = firstlayer;
							xi = inst.values[0][i];
							mul = xi * zh * (1 - zh);
						 for (k = 0; k < N.weights[1].row; k++)
							 {
								 yk = testY.values[0][k];
								 for (hprime = 0; hprime < N.weights[1].col; hprime++)
									 {
										 if (h == hprime - 1)
												 if (k == classno)
  												 sum1 = yk - 1;
													else
														 sum1 = yk;
											else
											  sum1 = 0;
											zhprime = testH.values[0][hprime];
											vkh = N.weights[1].values[k][h + 1];
											sum2 = zhprime * vkh * yk * (1 - yk);
           result.values[index1][index2] += mul * (sum1 + sum2);
											result.values[index2][index1] += mul * (sum1 + sum2);
										 index2++;
									 }
							 }
							index1++;
					 }
			 }
   matrix_free(inst);
		 tmp = tmp->next;
	 }
	matrix_free(testY);
	matrix_free(testH);
	return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
leaq 0xe0(%rsp), %rcx
movl 0x5c(%rcx), %eax
movq (%rcx), %rbx
movq %rdi, %r14
testl %eax, %eax
js 0x447ec
shlq $0x4, %rax
addq $0x10, %rax
xorl %ecx, %ecx
xorl %edi, %edi
movl 0x4(%rbx,%rcx), %edx
imull (%rbx,%rcx), %edx
addl %edx, %edi
addq $0x10, %rcx
cmpq %rcx, %rax
jne 0x447d7
jmp 0x447ee
xorl %edi, %edi
movl %edi, %esi
callq 0x3d79d
movq %rax, 0x58(%rsp)
movq %rdx, 0x20(%rsp)
leaq 0xe0(%rsp), %r15
movl 0x8(%r15), %esi
movl $0x1, %edi
callq 0x3d79d
movq %rax, 0x50(%rsp)
movq %rax, 0x98(%rsp)
movq %rdx, 0x18(%rsp)
movq %rdx, 0xa0(%rsp)
movl 0x50(%r15), %esi
movl $0x1, %edi
callq 0x3d79d
movq %rax, 0x48(%rsp)
movq %rax, 0x88(%rsp)
movq %rdx, 0x8(%rsp)
movq %rdx, 0x90(%rsp)
testq %r14, %r14
je 0x44ce7
movq %r14, %rdi
movl 0x4(%rbx), %eax
imull (%rbx), %eax
movl %eax, 0x3c(%rsp)
cltq
movq %rax, 0x78(%rsp)
leaq (,%rax,8), %rax
movq %rax, 0x60(%rsp)
movq %rdi, %rbx
callq 0x2f33a
movl %eax, %ebp
movq %rbx, 0x70(%rsp)
movq %rbx, %rdi
callq 0x2f0c1
movq %rax, 0x68(%rsp)
movq %rdx, 0x28(%rsp)
movq (%rdx), %rdi
leaq 0xe0(%rsp), %rax
movq (%rax), %rbx
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
leaq 0x88(%rsp), %rcx
callq 0x44d21
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movq 0x10(%rbx), %rsi
movq 0x18(%rbx), %rdx
leaq 0x98(%rsp), %rcx
callq 0x4521e
movq %rbx, (%rsp)
movl 0x10(%rbx), %eax
testl %eax, %eax
movq 0x18(%rsp), %rbx
movsd 0x60c28(%rip), %xmm8    # 0xa5520
jle 0x449a0
movq (%rbx), %rcx
movq (%rsp), %rdx
movl 0x14(%rdx), %edx
movslq %edx, %rsi
leaq (,%rsi,8), %rdi
movq 0x60(%rsp), %r8
xorl %r9d, %r9d
testl %edx, %edx
jle 0x44991
movsd (%rcx,%r9,8), %xmm0
movq 0x8(%rsp), %r10
movq (%r10), %r10
movq %r9, %r11
imulq %rsi, %r11
addq 0x78(%rsp), %r11
movapd %xmm8, %xmm1
subsd %xmm0, %xmm1
movq 0x20(%rsp), %rbx
leaq (%rbx,%r11,8), %r11
movq 0x18(%rsp), %rbx
xorl %r14d, %r14d
movsd (%r10,%r14,8), %xmm2
movq (%r11,%r14,8), %r15
addq %r8, %r15
xorl %r12d, %r12d
movsd (%r10,%r12,8), %xmm3
mulsd %xmm2, %xmm3
mulsd %xmm0, %xmm3
mulsd %xmm1, %xmm3
addsd (%r15,%r12,8), %xmm3
movsd %xmm3, (%r15,%r12,8)
incq %r12
cmpq %r12, %rdx
jne 0x44963
incq %r14
cmpq %rdx, %r14
jne 0x44953
incq %r9
addq %rdi, %r8
cmpq %rax, %r9
jne 0x4491b
movq (%rsp), %rcx
movslq (%rcx), %rcx
testq %rcx, %rcx
movsd 0x613f5(%rip), %xmm9    # 0xa5da8
jle 0x44cc6
movq 0x8(%rsp), %rdx
movq (%rdx), %rsi
movq (%rsp), %rdx
movl 0x4(%rdx), %edi
movl %ebp, %edx
xorl %r8d, %r8d
xorl %r11d, %r11d
testl %edi, %edi
jle 0x44b43
movsd (%rsi,%r8,8), %xmm1
movq 0x28(%rsp), %r9
movq (%r9), %r10
movapd %xmm8, %xmm0
subsd %xmm1, %xmm0
mulsd %xmm1, %xmm0
movslq %r11d, %r11
xorl %r9d, %r9d
movq %r9, 0x10(%rsp)
movsd (%r10,%r9,8), %xmm1
movq 0x20(%rsp), %r9
movq %r11, 0x30(%rsp)
movq (%r9,%r11,8), %r14
xorl %ebp, %ebp
xorl %r12d, %r12d
movsd (%rsi,%rbp,8), %xmm2
xorpd %xmm3, %xmm3
xorpd %xmm4, %xmm4
cmpq %rbp, %r8
jne 0x44a8a
testl %eax, %eax
jle 0x44a71
movq (%rsp), %r9
movq 0x18(%r9), %r15
movq (%rbx), %r13
xorpd %xmm5, %xmm5
xorl %r9d, %r9d
movsd (%r13,%r9,8), %xmm6
movapd %xmm6, %xmm4
addsd %xmm9, %xmm4
cmpq %r9, %rdx
je 0x44a58
movapd %xmm6, %xmm4
movq (%r15,%r9,8), %r11
mulsd 0x8(%r11,%r8,8), %xmm4
addsd %xmm4, %xmm5
incq %r9
cmpq %r9, %rax
jne 0x44a3f
jmp 0x44a75
xorpd %xmm5, %xmm5
movapd %xmm2, %xmm6
addsd %xmm2, %xmm6
movapd %xmm8, %xmm4
subsd %xmm6, %xmm4
mulsd %xmm5, %xmm4
testl %eax, %eax
jle 0x44ad6
movq (%rsp), %r9
movq 0x18(%r9), %r15
movq (%rbx), %r13
xorpd %xmm3, %xmm3
xorl %r9d, %r9d
movq (%r15,%r9,8), %r11
movsd 0x8(%r11,%r8,8), %xmm5
mulsd 0x8(%r11,%rbp,8), %xmm5
movsd (%r13,%r9,8), %xmm6
mulsd %xmm6, %xmm5
movapd %xmm8, %xmm7
subsd %xmm6, %xmm7
mulsd %xmm5, %xmm7
addsd %xmm7, %xmm3
incq %r9
cmpq %r9, %rax
jne 0x44aa0
mulsd %xmm0, %xmm3
addsd %xmm3, %xmm4
movapd %xmm8, %xmm3
subsd %xmm2, %xmm3
movslq %r12d, %r12
leaq (%r14,%r12,8), %r15
xorl %r9d, %r9d
movsd (%r10,%r9,8), %xmm5
mulsd %xmm1, %xmm5
mulsd %xmm2, %xmm5
mulsd %xmm3, %xmm5
mulsd %xmm4, %xmm5
addsd (%r15,%r9,8), %xmm5
movsd %xmm5, (%r15,%r9,8)
incq %r9
cmpq %r9, %rdi
jne 0x44af1
incq %rbp
addq %r9, %r12
cmpq %rcx, %rbp
jne 0x44a17
movq 0x30(%rsp), %r11
incq %r11
movq 0x10(%rsp), %r9
incq %r9
cmpq %rdi, %r9
jne 0x449f9
incq %r8
cmpq %rcx, %r8
jne 0x449d0
testl %ecx, %ecx
jle 0x44cc6
movq 0x8(%rsp), %rsi
movq (%rsi), %rsi
movq (%rsp), %rdi
movl 0x4(%rdi), %edi
movq %rdi, 0x40(%rsp)
xorl %r8d, %r8d
xorl %r9d, %r9d
cmpl $0x0, 0x40(%rsp)
jle 0x44cba
movsd (%rsi,%r8,8), %xmm0
movq 0x28(%rsp), %rdi
movq (%rdi), %rdi
movq %rdi, 0x80(%rsp)
movapd %xmm8, %xmm1
subsd %xmm0, %xmm1
movslq %r9d, %r9
leaq 0x1(%r8), %r11
movq $0x0, 0x10(%rsp)
testl %eax, %eax
jle 0x44c9f
movq 0x80(%rsp), %rdi
movq 0x10(%rsp), %r10
movsd (%rdi,%r10,8), %xmm2
mulsd %xmm0, %xmm2
mulsd %xmm1, %xmm2
movq (%rbx), %rdi
movq %rdi, 0x30(%rsp)
movq (%rsp), %rdi
movl 0x14(%rdi), %ebp
xorl %r10d, %r10d
movl 0x3c(%rsp), %edi
testl %ebp, %ebp
jle 0x44c93
movq 0x30(%rsp), %rbx
movsd (%rbx,%r10,8), %xmm3
movapd %xmm3, %xmm4
addsd %xmm9, %xmm4
cmpq %rdx, %r10
je 0x44c09
movapd %xmm3, %xmm4
movq (%rsp), %rbx
movq 0x18(%rbx), %r14
movq (%r14,%r10,8), %r15
movapd %xmm8, %xmm5
subsd %xmm3, %xmm5
movslq %edi, %r12
leaq (,%r12,8), %r14
movq 0x20(%rsp), %rbx
addq (%rbx,%r9,8), %r14
leaq (%rbx,%r12,8), %r12
xorl %r13d, %r13d
movapd %xmm4, %xmm6
cmpq %r13, %r11
je 0x44c46
xorpd %xmm6, %xmm6
movsd (%rsi,%r13,8), %xmm7
mulsd 0x8(%r15,%r8,8), %xmm7
mulsd %xmm3, %xmm7
mulsd %xmm5, %xmm7
addsd %xmm6, %xmm7
mulsd %xmm2, %xmm7
movsd (%r14,%r13,8), %xmm6
addsd %xmm7, %xmm6
movsd %xmm6, (%r14,%r13,8)
movq (%r12,%r13,8), %rbx
addsd (%rbx,%r9,8), %xmm7
movsd %xmm7, (%rbx,%r9,8)
incq %r13
cmpq %r13, %rbp
jne 0x44c39
addl %r13d, %edi
movq 0x18(%rsp), %rbx
incq %r10
cmpq %rax, %r10
jne 0x44be4
incq %r9
movq 0x10(%rsp), %r10
incq %r10
movq %r10, 0x10(%rsp)
cmpq 0x40(%rsp), %r10
jne 0x44bab
incq %r8
cmpq %rcx, %r8
jne 0x44b71
movq 0x68(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x3ed51
movq 0x70(%rsp), %rdi
movq 0x10(%rdi), %rdi
testq %rdi, %rdi
jne 0x44881
movq 0x50(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x3ed51
movq 0x48(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x3ed51
movq 0x58(%rsp), %rax
movq 0x20(%rsp), %rdx
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq