// File: /workspace/github_cpp_makefile_star2_O1/leonn[P]neuralNet/source/net.cpp
// Function: Net::backPropagation(std::vector<double, std::allocator<double>> const&, long double const&, long double const&)
void Net::backPropagation(const vector<double> &targetValues,const long double &eta ,const long double &alpha){

	//-----Calculate overall net error (RMS-root mean square error - of output neuron errors)
	Layer &outputLayer = layers.back();

	//overwall net error
	error=0.0;

 	for(unsigned n = 0; n < outputLayer.size() -1; n++){
 		long double delta= targetValues[n] - outputLayer[n].getOutputValue();
 		error+=delta*delta;
 	}
 	error*=0.5;

 	//error/=(* numPatterns); //get average error squared
	//error/=outputLayer.size()-1; //get average error squared
    //------ Implement a recent average measurement
    recentAverageError = error;

    //------ Calculate output layer gradients
	for(unsigned n = 0; n < outputLayer.size() -1; n++){
 		outputLayer[n].calculateOutputGradients(targetValues[n]);
  	}

    //------- Calculate hidden layer gradients
  	for(unsigned layerNumber = layers.size() -2; layerNumber >0 ; layerNumber--){
		Layer &hiddenLayer = layers[layerNumber];
		Layer &nextLayer = layers[layerNumber+1];

		for(unsigned n = 0; n < hiddenLayer.size(); n++){
			hiddenLayer[n].calculateHiddenGradients(nextLayer);
  		}
  	}

    //------- For all layers from outputs to first hidden layer,
    //------- update connection weights
  	for(unsigned layerNumber = layers.size() -1; layerNumber >0 ; layerNumber--){
		Layer &layer = layers[layerNumber];
		Layer &prevLayer = layers[layerNumber-1];

		for(unsigned n = 0; n < layer.size() -1; n++){
			layer[n].updateInputWeights(prevLayer,eta,alpha);
		}
  	}

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, -0x48(%rbp)
movq %rdx, -0x50(%rbp)
movq %rsi, %rbx
movq %rdi, %r13
movq 0x28(%rdi), %r12
fldz
fstpt (%rdi)
movq -0x18(%r12), %rax
movq -0x10(%r12), %rcx
subq %rax, %rcx
movabsq $-0x5555555555555555, %r14 # imm = 0xAAAAAAAAAAAAAAAB
cmpq $0x60, %rcx
je 0x29c7
movl $0x1, %r15d
xorl %ecx, %ecx
movq (%rbx), %rdx
fldl (%rdx,%rcx,8)
fstpt -0x38(%rbp)
leaq (%rcx,%rcx,2), %rdi
shlq $0x5, %rdi
addq %rax, %rdi
callq 0x4698
fldt -0x38(%rbp)
fsubp %st, %st(1)
fldt (%r13)
fxch %st(1)
fmul %st, %st(0)
faddp %st, %st(1)
fstpt (%r13)
movl %r15d, %ecx
movq -0x18(%r12), %rax
movq -0x10(%r12), %rdx
subq %rax, %rdx
sarq $0x5, %rdx
imulq %r14, %rdx
decq %rdx
incl %r15d
cmpq %rcx, %rdx
ja 0x2978
fldt (%r13)
fmuls 0x364f(%rip)            # 0x6020
fld %st(0)
fstpt (%r13)
fstpt 0x10(%r13)
movq -0x18(%r12), %rax
movq -0x10(%r12), %rcx
subq %rax, %rcx
cmpq $0x60, %rcx
je 0x2a32
movq %rbx, %r15
movl $0x1, %ebx
xorl %ecx, %ecx
leaq (%rcx,%rcx,2), %rdi
shlq $0x5, %rdi
addq %rax, %rdi
movq (%r15), %rax
fldl (%rax,%rcx,8)
fstpt (%rsp)
callq 0x4808
movl %ebx, %ecx
movq -0x18(%r12), %rax
movq -0x10(%r12), %rdx
subq %rax, %rdx
sarq $0x5, %rdx
imulq %r14, %rdx
decq %rdx
incl %ebx
cmpq %rcx, %rdx
ja 0x29f8
movq 0x28(%r13), %rax
subq 0x20(%r13), %rax
shrq $0x3, %rax
imull $0xaaaaaaab, %eax, %eax # imm = 0xAAAAAAAB
addl $-0x2, %eax
movq %r13, -0x40(%rbp)
je 0x2ac1
movl %eax, %r15d
movq 0x20(%r13), %rcx
leaq (%r15,%r15,2), %rdx
movq (%rcx,%rdx,8), %rax
cmpq %rax, 0x8(%rcx,%rdx,8)
movabsq $-0x5555555555555555, %r13 # imm = 0xAAAAAAAAAAAAAAAB
je 0x2ab5
leaq (%rcx,%rdx,8), %rbx
leal 0x1(%r15), %edx
leaq (%rdx,%rdx,2), %rdx
leaq (%rcx,%rdx,8), %r12
movl $0x1, %r14d
xorl %ecx, %ecx
leaq (%rcx,%rcx,2), %rdi
shlq $0x5, %rdi
addq %rax, %rdi
movq %r12, %rsi
callq 0x4902
movl %r14d, %ecx
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
subq %rax, %rdx
sarq $0x5, %rdx
imulq %r13, %rdx
incl %r14d
cmpq %rcx, %rdx
ja 0x2a85
decq %r15
testl %r15d, %r15d
movq -0x40(%rbp), %r13
jne 0x2a50
movq 0x28(%r13), %rdx
subq 0x20(%r13), %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rax # imm = 0xAAAAAAAAAAAAAAAB
imulq %rax, %rdx
movl %edx, %eax
decl %eax
movq -0x48(%rbp), %rcx
je 0x2b84
movl %eax, %eax
movq -0x50(%rbp), %r12
movabsq $-0x5555555555555555, %r14 # imm = 0xAAAAAAAAAAAAAAAB
movq 0x20(%r13), %r9
movq %rax, -0x58(%rbp)
leaq (%rax,%rax,2), %rsi
movq (%r9,%rsi,8), %rax
movq 0x8(%r9,%rsi,8), %rdi
subq %rax, %rdi
cmpq $0x60, %rdi
je 0x2b70
movq %rdx, %r8
leaq (%r9,%rsi,8), %r15
addl $-0x2, %r8d
leaq (%r8,%r8,2), %rsi
leaq (%r9,%rsi,8), %rsi
movq %rsi, -0x38(%rbp)
movl $0x1, %ebx
xorl %esi, %esi
leaq (%rsi,%rsi,2), %rdi
shlq $0x5, %rdi
addq %rax, %rdi
movq -0x38(%rbp), %rsi
movq %r12, %rdx
movq %rcx, %r13
callq 0x4994
movq %r13, %rcx
movl %ebx, %esi
movq (%r15), %rax
movq 0x8(%r15), %r8
subq %rax, %r8
sarq $0x5, %r8
imulq %r14, %r8
decq %r8
incl %ebx
cmpq %rsi, %r8
ja 0x2b35
movq -0x58(%rbp), %rdx
leaq -0x1(%rdx), %rax
testl %eax, %eax
movq -0x40(%rbp), %r13
jne 0x2af9
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop