// File: /workspace/github_cpp_makefile_star2_O2/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), %r14
movq %rdi, -0x30(%rbp)
movq 0x8(%rdi), %rcx
movq %rcx, %rax
subq %r14, %rax
pushq $0x18
popq %rdi
cqto
idivq %rdi
movslq %esi, %r12
xorpd %xmm0, %xmm0
pushq $0x1
popq %rdx
shlq $0x4, %r12
movl %edx, %esi
cmpq %rsi, %rax
jbe 0x2780
leal -0x1(%rdx), %esi
imulq $0x18, %rsi, %rdi
movq (%r14,%rdi), %rsi
movq 0x8(%r14,%rdi), %rdi
subq %rsi, %rdi
sarq $0x6, %rdi
xorl %r8d, %r8d
movl %r8d, %r8d
cmpq %r8, %rdi
jbe 0x277c
movq %r8, %r9
shlq $0x6, %r9
movq 0x10(%rsi,%r9), %r9
addsd (%r9,%r12), %xmm0
incl %r8d
jmp 0x275d
incl %edx
jmp 0x273c
divsd 0x3890(%rip), %xmm0     # 0x6018
xorpd %xmm2, %xmm2
pushq $0x1
popq %rdx
movl %edx, %esi
cmpq %rsi, %rax
jbe 0x27e5
leal -0x1(%rdx), %esi
imulq $0x18, %rsi, %rdi
movq (%r14,%rdi), %rsi
movq 0x8(%r14,%rdi), %rdi
subq %rsi, %rdi
sarq $0x6, %rdi
xorl %r8d, %r8d
movl %r8d, %r8d
cmpq %r8, %rdi
jbe 0x27e1
movq %r8, %r9
shlq $0x6, %r9
movq 0x10(%rsi,%r9), %r9
movsd (%r9,%r12), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, (%r9,%r12)
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm2
incl %r8d
jmp 0x27b0
incl %edx
jmp 0x278f
xorpd %xmm1, %xmm1
pushq $0x1
popq %rdi
sqrtsd %xmm2, %xmm4
xorpd %xmm3, %xmm3
movsd %xmm2, -0x48(%rbp)
movsd %xmm4, -0x40(%rbp)
movl %edi, %esi
subq %r14, %rcx
movq %rcx, %rax
cqto
pushq $0x18
popq %rcx
idivq %rcx
cmpq %rsi, %rax
jbe 0x28bd
movq %rdi, -0x38(%rbp)
leal -0x1(%rdi), %eax
imulq $0x18, %rax, %rax
addq %rax, %r14
xorl %r15d, %r15d
movl %r15d, %r15d
movq (%r14), %rbx
movq 0x8(%r14), %rax
subq %rbx, %rax
sarq $0x6, %rax
cmpq %r15, %rax
jbe 0x28a7
movq %r15, %r13
shlq $0x6, %r13
movq 0x10(%rbx,%r13), %rax
movsd (%rax,%r12), %xmm5
ucomisd %xmm1, %xmm2
movapd %xmm4, %xmm0
jae 0x2885
movapd %xmm2, %xmm0
movsd %xmm3, -0x58(%rbp)
movsd %xmm5, -0x50(%rbp)
callq 0x2110
movsd -0x50(%rbp), %xmm5
movsd -0x40(%rbp), %xmm4
movsd -0x58(%rbp), %xmm3
xorpd %xmm1, %xmm1
movsd -0x48(%rbp), %xmm2
divsd %xmm0, %xmm5
leaq (%rbx,%r13), %rax
addq $0x10, %rax
movq (%rax), %rax
movsd %xmm5, (%rax,%r12)
mulsd %xmm5, %xmm5
addsd %xmm5, %xmm3
incl %r15d
jmp 0x2828
movq -0x38(%rbp), %rdi
incl %edi
movq -0x30(%rbp), %rax
movq (%rax), %r14
movq 0x8(%rax), %rcx
jmp 0x27fe
movq 0x6714(%rip), %rdi      # 0x8fd8
movapd %xmm3, %xmm0
callq 0x2240
movq %rax, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x20b0
nop