// File: /workspace/github_cpp_makefile_star2_O1/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
subq %rax, %rcx
sarq $0x3, %rcx
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rdx, %rcx
xorpd %xmm2, %xmm2
xorpd %xmm0, %xmm0
cmpq $0x2, %rcx
jb 0x2878
movslq %esi, %rdx
movl $0x1, %edi
shlq $0x4, %rdx
leal -0x1(%rdi), %r8d
leaq (%r8,%r8,2), %r9
movq (%rax,%r9,8), %r8
movq 0x8(%rax,%r9,8), %r9
subq %r8, %r9
je 0x2871
sarq $0x6, %r9
addq $0x10, %r8
movl $0x1, %r10d
movq (%r8), %r11
addsd (%r11,%rdx), %xmm0
movl %r10d, %r11d
addq $0x40, %r8
incl %r10d
cmpq %r11, %r9
ja 0x2859
incl %edi
cmpq %rdi, %rcx
ja 0x2835
movq -0x30(%rbp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rdx, %rcx
cmpq $0x2, %rcx
jb 0x290b
divsd 0x4772(%rip), %xmm0     # 0x7018
movslq %esi, %rdx
movl $0x1, %edi
xorpd %xmm2, %xmm2
shlq $0x4, %rdx
leal -0x1(%rdi), %r8d
leaq (%r8,%r8,2), %r9
movq (%rax,%r9,8), %r8
movq 0x8(%rax,%r9,8), %r9
subq %r8, %r9
je 0x2904
sarq $0x6, %r9
addq $0x10, %r8
movl $0x1, %r10d
movq (%r8), %r11
movsd (%r11,%rdx), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, (%r11,%rdx)
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm2
movl %r10d, %r11d
addq $0x40, %r8
incl %r10d
cmpq %r11, %r9
ja 0x28da
incl %edi
cmpq %rdi, %rcx
ja 0x28b6
movq -0x30(%rbp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rdx, %rcx
xorpd %xmm1, %xmm1
cmpq $0x2, %rcx
jb 0x2a28
movslq %esi, %r15
movl $0x1, %esi
xorpd %xmm3, %xmm3
shlq $0x4, %r15
sqrtsd %xmm2, %xmm4
movsd %xmm2, -0x48(%rbp)
movsd %xmm4, -0x40(%rbp)
movq %rsi, -0x38(%rbp)
leal -0x1(%rsi), %ecx
leaq (%rcx,%rcx,2), %rcx
movq (%rax,%rcx,8), %rbx
cmpq %rbx, 0x8(%rax,%rcx,8)
je 0x29f9
leaq (%rax,%rcx,8), %r13
movl $0x1, %r14d
xorl %r12d, %r12d
shlq $0x6, %r12
movq 0x10(%rbx,%r12), %rax
movsd (%rax,%r15), %xmm5
ucomisd %xmm3, %xmm2
movapd %xmm4, %xmm0
jae 0x29c2
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 0x297e
movq -0x38(%rbp), %rsi
incl %esi
movq -0x30(%rbp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rdx, %rcx
cmpq %rsi, %rcx
ja 0x2957
movq 0x75a9(%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