// File: 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
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xc0(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x18(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x20(%rbp)
movl $0x1, -0x24(%rbp)
movq -0xc0(%rbp), %rdi
movl -0x24(%rbp), %eax
movq %rax, -0xc8(%rbp)
callq 0x4230
movq %rax, %rcx
movq -0xc8(%rbp), %rax
cmpq %rcx, %rax
jae 0x2b61
movq -0xc0(%rbp), %rdi
movl -0x24(%rbp), %eax
movl %eax, %esi
callq 0x4260
movq -0xc0(%rbp), %rdi
movq %rax, -0x30(%rbp)
movl -0x24(%rbp), %eax
subl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
callq 0x4260
movq %rax, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq %rax, -0xd0(%rbp)
movq -0x38(%rbp), %rdi
callq 0x3f90
movq %rax, %rcx
movq -0xd0(%rbp), %rax
cmpq %rcx, %rax
jae 0x2b51
movq -0x38(%rbp), %rdi
movl -0x3c(%rbp), %eax
movl %eax, %esi
callq 0x3fb0
movq %rax, -0x48(%rbp)
movsd -0x18(%rbp), %xmm0
movsd %xmm0, -0xd8(%rbp)
movq -0x48(%rbp), %rdi
addq $0x10, %rdi
movslq -0xc(%rbp), %rsi
callq 0x3f10
movsd -0xd8(%rbp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, -0x18(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x2ae3
jmp 0x2b53
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x2a84
movsd -0x18(%rbp), %xmm0
movsd 0xb4aa(%rip), %xmm1     # 0xe018
divsd %xmm1, %xmm0
movsd %xmm0, -0x50(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x18(%rbp)
movl $0x1, -0x54(%rbp)
movq -0xc0(%rbp), %rdi
movl -0x54(%rbp), %eax
movq %rax, -0xe0(%rbp)
callq 0x4230
movq %rax, %rcx
movq -0xe0(%rbp), %rax
cmpq %rcx, %rax
jae 0x2c92
movq -0xc0(%rbp), %rdi
movl -0x54(%rbp), %eax
movl %eax, %esi
callq 0x4260
movq -0xc0(%rbp), %rdi
movq %rax, -0x60(%rbp)
movl -0x54(%rbp), %eax
subl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
callq 0x4260
movq %rax, -0x68(%rbp)
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
movq %rax, -0xe8(%rbp)
movq -0x68(%rbp), %rdi
callq 0x3f90
movq %rax, %rcx
movq -0xe8(%rbp), %rax
cmpq %rcx, %rax
jae 0x2c82
movq -0x68(%rbp), %rdi
movl -0x6c(%rbp), %eax
movl %eax, %esi
callq 0x3fb0
movq %rax, -0x78(%rbp)
movsd -0x50(%rbp), %xmm0
movsd %xmm0, -0xf0(%rbp)
movq -0x78(%rbp), %rdi
addq $0x10, %rdi
movslq -0xc(%rbp), %rsi
callq 0x3f10
movsd -0xf0(%rbp), %xmm1
movsd (%rax), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, (%rax)
movq -0x78(%rbp), %rdi
addq $0x10, %rdi
movslq -0xc(%rbp), %rsi
callq 0x3f10
movsd (%rax), %xmm0
movl $0x2, %edi
callq 0x4280
addsd -0x18(%rbp), %xmm0
movsd %xmm0, -0x18(%rbp)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x2be5
jmp 0x2c84
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x2b86
movl $0x1, -0x7c(%rbp)
movq -0xc0(%rbp), %rdi
movl -0x7c(%rbp), %eax
movq %rax, -0xf8(%rbp)
callq 0x4230
movq %rax, %rcx
movq -0xf8(%rbp), %rax
cmpq %rcx, %rax
jae 0x2e23
movq -0xc0(%rbp), %rdi
movl -0x7c(%rbp), %eax
movl %eax, %esi
callq 0x4260
movq -0xc0(%rbp), %rdi
movq %rax, -0x88(%rbp)
movl -0x7c(%rbp), %eax
subl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
callq 0x4260
movq %rax, -0x90(%rbp)
movl $0x0, -0x94(%rbp)
movl -0x94(%rbp), %eax
movq %rax, -0x100(%rbp)
movq -0x90(%rbp), %rdi
callq 0x3f90
movq %rax, %rcx
movq -0x100(%rbp), %rax
cmpq %rcx, %rax
jae 0x2e13
movq -0x90(%rbp), %rdi
movl -0x94(%rbp), %eax
movl %eax, %esi
callq 0x3fb0
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rdi
addq $0x10, %rdi
movslq -0xc(%rbp), %rsi
callq 0x3f10
movsd (%rax), %xmm0
movl $0x2, %edi
callq 0x4280
movsd %xmm0, -0xa8(%rbp)
movq -0xa0(%rbp), %rdi
addq $0x10, %rdi
movslq -0xc(%rbp), %rsi
callq 0x3f10
movsd (%rax), %xmm0
movsd %xmm0, -0x110(%rbp)
movsd -0x18(%rbp), %xmm0
callq 0x2150
movaps %xmm0, %xmm1
movsd -0x110(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0xb0(%rbp)
movsd -0xb0(%rbp), %xmm0
movsd %xmm0, -0x108(%rbp)
movq -0xa0(%rbp), %rdi
addq $0x10, %rdi
movslq -0xc(%rbp), %rsi
callq 0x3f10
movsd -0x108(%rbp), %xmm0
movsd %xmm0, (%rax)
movsd -0xb0(%rbp), %xmm0
movl $0x2, %edi
callq 0x4280
addsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0x2d01
jmp 0x2e15
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x2c99
movsd -0x20(%rbp), %xmm0
movq 0x121a9(%rip), %rdi     # 0x14fd8
callq 0x2320
movq %rax, %rdi
movq 0x12182(%rip), %rsi     # 0x14fc0
callq 0x2200
movsd 0xb1d5(%rip), %xmm0     # 0xe020
divsd -0x20(%rbp), %xmm0
movsd %xmm0, -0xb8(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x20(%rbp)
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nopl (%rax)