// File: /workspace/github_cpp_makefile_star2_O3/godofecht[P]Slow-Feature-Analysis/./Network.cpp
// Function: Network::NormalizeWeights(int)
void Network::NormalizeWeights(int connection_index)
{
	double sum_weights_squared = 0.0f;
	double checksum = 0.0f;
	for (unsigned layerNum = 1; layerNum < m_layers.size(); layerNum++) {
		Layer& layer = m_layers[layerNum];
		Layer& prevLayer = m_layers[layerNum - 1];




		for (unsigned n = 0; n < prevLayer.size(); n++) {
			Neuron* neuron = &(prevLayer[n]);

			sum_weights_squared = sum_weights_squared +neuron->m_outputWeights[connection_index].weight;// pow(neuron->m_outputWeights[0].weight,2);
		}
	}
	double average = sum_weights_squared/101.0f;
	sum_weights_squared = 0.0f;
	for (unsigned layerNum = 1; layerNum < m_layers.size(); layerNum++) {
		Layer& layer = m_layers[layerNum];
		Layer& prevLayer = m_layers[layerNum - 1];




		for (unsigned n = 0; n < prevLayer.size(); n++) {
			Neuron* neuron = &(prevLayer[n]);
			neuron->m_outputWeights[connection_index].weight -= average;
			sum_weights_squared += pow(neuron->m_outputWeights[connection_index].weight,2);
			// pow(neuron->m_outputWeights[0].weight,2);
		}
	}

	for (unsigned layerNum = 1; layerNum < m_layers.size(); layerNum++) {
		Layer& layer = m_layers[layerNum];
		Layer& prevLayer = m_layers[layerNum - 1];

		for (unsigned n = 0; n < prevLayer.size(); n++) {
			Neuron* neuron = &(prevLayer[n]);
			double weight_squared = pow(neuron->m_outputWeights[connection_index].weight,2);
			double newWeight = neuron->m_outputWeights[connection_index].weight/sqrt(sum_weights_squared);
			neuron->m_outputWeights[connection_index].weight = newWeight;
		//	cout<<newWeight<<endl;
	//			cout<<neuron->m_outputWeights[0].weight<<endl;
			checksum+= pow(newWeight,2);
		}

	}
	cout<<checksum<<endl;
	double checksumfactor = 1.0f/checksum;
	checksum = 0.0f;

	/*
	for (unsigned layerNum = 1; layerNum < m_layers.size(); layerNum++) {
		Layer& layer = m_layers[layerNum];
		Layer& prevLayer = m_layers[layerNum - 1];

		for (unsigned n = 0; n < prevLayer.size(); n++) {
			Neuron* neuron = &(prevLayer[n]);
			double weight_squared = pow(neuron->m_outputWeights[0].weight,2);
			double newWeight =(neuron->m_outputWeights[0].weight*sqrt(checksumfactor));// sqrt(1.0f-sum_weights_squared + weight_squared);
			neuron->m_outputWeights[0].weight = newWeight;
		//	cout<<newWeight<<endl;
	//			cout<<neuron->m_outputWeights[0].weight<<endl;
			checksum+= pow(newWeight,2);
		}
		double checksumfactor = 1.0f/ checksum;
		cout<<checksum<<endl;
	}
	*/
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq (%rdi), %rax
movq %rdi, -0x30(%rbp)
movq 0x8(%rdi), %rcx
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rdi # imm = 0xAAAAAAAAAAAAAAAB
imulq %rdi, %rdx
xorpd %xmm1, %xmm1
cmpq $0x2, %rdx
jb 0x29e6
movslq %esi, %r15
movl $0x1, %esi
xorpd %xmm0, %xmm0
shlq $0x4, %r15
leal -0x1(%rsi), %edi
leaq (%rdi,%rdi,2), %r8
movq (%rax,%r8,8), %rdi
movq 0x8(%rax,%r8,8), %r8
subq %rdi, %r8
je 0x2873
sarq $0x6, %r8
addq $0x10, %rdi
movl $0x1, %r9d
movq (%rdi), %r10
addsd (%r10,%r15), %xmm0
movl %r9d, %r10d
addq $0x40, %rdi
incl %r9d
cmpq %r10, %r8
ja 0x285b
incl %esi
cmpq %rsi, %rdx
ja 0x2838
xorpd %xmm1, %xmm1
cmpq $0x2, %rdx
jb 0x29e6
divsd 0x4788(%rip), %xmm0     # 0x7018
movl $0x1, %esi
xorpd %xmm2, %xmm2
leal -0x1(%rsi), %edi
leaq (%rdi,%rdi,2), %r8
movq (%rax,%r8,8), %rdi
movq 0x8(%rax,%r8,8), %r8
subq %rdi, %r8
je 0x28e6
sarq $0x6, %r8
addq $0x10, %rdi
movl $0x1, %r9d
movq (%rdi), %r10
movsd (%r10,%r15), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, (%r10,%r15)
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm2
movl %r9d, %r10d
addq $0x40, %rdi
incl %r9d
cmpq %r10, %r8
ja 0x28bc
incl %esi
cmpq %rsi, %rdx
ja 0x2899
xorpd %xmm1, %xmm1
cmpq $0x2, %rdx
jb 0x29e6
movl $0x1, %edi
xorpd %xmm3, %xmm3
sqrtsd %xmm2, %xmm4
movsd %xmm4, -0x40(%rbp)
leal -0x1(%rdi), %edx
leaq (%rdx,%rdx,2), %rdx
movq (%rax,%rdx,8), %rbx
cmpq %rbx, 0x8(%rax,%rdx,8)
je 0x29c3
movq %rdi, -0x38(%rbp)
leaq (%rax,%rdx,8), %r13
movl $0x1, %r14d
xorl %r12d, %r12d
movsd %xmm2, -0x48(%rbp)
shlq $0x6, %r12
movq 0x10(%rbx,%r12), %rax
movsd (%rax,%r15), %xmm5
ucomisd %xmm3, %xmm2
movapd %xmm4, %xmm0
jae 0x297d
movapd %xmm2, %xmm0
movsd %xmm1, -0x58(%rbp)
movsd %xmm5, -0x50(%rbp)
callq 0x2130
movsd -0x50(%rbp), %xmm5
movsd -0x40(%rbp), %xmm4
xorpd %xmm3, %xmm3
movsd -0x58(%rbp), %xmm1
movsd -0x48(%rbp), %xmm2
divsd %xmm0, %xmm5
leaq (%rbx,%r12), %rax
addq $0x10, %rax
movq (%rax), %rax
movsd %xmm5, (%rax,%r15)
mulsd %xmm5, %xmm5
addsd %xmm5, %xmm1
movl %r14d, %r12d
movq (%r13), %rbx
movq 0x8(%r13), %rax
subq %rbx, %rax
sarq $0x6, %rax
incl %r14d
cmpq %r12, %rax
ja 0x2939
movq -0x30(%rbp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
movq -0x38(%rbp), %rdi
incl %edi
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rsi # imm = 0xAAAAAAAAAAAAAAAB
imulq %rsi, %rdx
cmpq %rdi, %rdx
ja 0x290d
movq 0x75eb(%rip), %rdi      # 0x9fd8
movapd %xmm1, %xmm0
callq 0x2250
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21c0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2100