// File: /workspace/github_cpp_makefile_star2_O2/ekg[P]sgd2/src/sgd2.hpp
// Function: sgd2::sgd(double*, std::vector<sgd2::term, std::allocator<sgd2::term>>&, std::vector<double, std::allocator<double>> const&, double)
void sgd(double* X, std::vector<term> &terms, const std::vector<double> &etas, double delta)
{
    std::random_device rd;
    std::mt19937 rg(rd());

    // iterate through step sizes
    for (double eta : etas)
    {
        // shuffle terms
        std::shuffle(terms.begin(), terms.end(), rg);

        double Delta_max = 0;
        for (const term &t : terms)
        {
            // cap step size
            double w_ij = t.w;
            double mu = eta * w_ij;
            if (mu > 1)
                mu = 1;

            double d_ij = t.d;
            uint64_t i = t.i, j = t.j;

            double dx = X[i*2]-X[j*2], dy = X[i*2+1]-X[j*2+1];
            double mag = sqrt(dx*dx + dy*dy);

            // check distances for early stopping
            double Delta = mu * (mag-d_ij) / 2;
            if (Delta > Delta_max)
                Delta_max = Delta;

            double r = Delta / mag;
            double r_x = r * dx;
            double r_y = r * dy;
            
            X[i*2] -= r_x;
            X[i*2+1] -= r_y;
            X[j*2] += r_x;
            X[j*2+1] += r_y;
        }
        //std::cerr << ++iteration << ", eta: " << eta << ", Delta: " << Delta_max << std::endl;
        if (Delta_max < delta)
            return;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2728, %rsp           # imm = 0x2728
movsd %xmm0, -0x30(%rbp)
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, %r14
leaq -0x13c0(%rbp), %r12
movq %r12, %rdi
callq 0x5b0e
movq %r12, %rdi
callq 0x2180
movl %eax, %esi
leaq -0x2748(%rbp), %rdi
callq 0x6ccc
movq (%r15), %r12
movq 0x8(%r15), %r13
leaq -0x2748(%rbp), %r15
cmpq %r13, %r12
je 0x24f1
movsd (%r12), %xmm0
movsd %xmm0, -0x38(%rbp)
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
movq %r15, %rdx
callq 0x5b64
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
xorpd %xmm0, %xmm0
movsd 0x8c00(%rip), %xmm8     # 0xb008
movsd 0x8bff(%rip), %xmm9     # 0xb010
movsd -0x38(%rbp), %xmm10
cmpq %rcx, %rax
je 0x24de
movsd 0x18(%rax), %xmm1
mulsd %xmm10, %xmm1
movapd %xmm8, %xmm2
minsd %xmm1, %xmm2
movq (%rax), %rsi
movq 0x8(%rax), %rdx
shlq $0x4, %rsi
shlq $0x4, %rdx
movsd (%r14,%rsi), %xmm3
movsd 0x8(%r14,%rsi), %xmm4
movapd %xmm3, %xmm1
subsd (%r14,%rdx), %xmm1
movapd %xmm4, %xmm5
subsd 0x8(%r14,%rdx), %xmm5
movapd %xmm5, %xmm6
mulsd %xmm5, %xmm6
movapd %xmm1, %xmm7
mulsd %xmm1, %xmm7
addsd %xmm6, %xmm7
sqrtsd %xmm7, %xmm6
movapd %xmm6, %xmm7
subsd 0x10(%rax), %xmm7
mulsd %xmm2, %xmm7
mulsd %xmm9, %xmm7
movapd %xmm7, %xmm2
divsd %xmm6, %xmm7
mulsd %xmm7, %xmm1
mulsd %xmm5, %xmm7
subsd %xmm1, %xmm3
movsd %xmm3, (%r14,%rsi)
subsd %xmm7, %xmm4
movsd %xmm4, 0x8(%r14,%rsi)
addsd (%r14,%rdx), %xmm1
movsd %xmm1, (%r14,%rdx)
addsd 0x8(%r14,%rdx), %xmm7
maxsd %xmm0, %xmm2
movsd %xmm7, 0x8(%r14,%rdx)
addq $0x20, %rax
movapd %xmm2, %xmm0
jmp 0x2417
addq $0x8, %r12
movsd -0x30(%rbp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x23d1
leaq -0x13c0(%rbp), %rdi
callq 0x5d56
addq $0x2728, %rsp           # imm = 0x2728
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2511
movq %rax, %rbx
leaq -0x13c0(%rbp), %rdi
callq 0x5d56
movq %rbx, %rdi
callq 0x2210