// File: /workspace/github_cpp_makefile_star2_O2/istellartech[P]OpenTsiolkovsky/src/fileio.cpp
// Function: interp_matrix_2d(double, double, Eigen::Matrix<double, -1, -1, 0, -1, -1>)
double interp_matrix_2d(double mach, double alpha, MatrixXd matrix){
    // 2変数関数として線形補間をする。範囲外のデータが入力された場合強制終了。
    // 先頭行先頭列に変数を並べる
    // @params mach  取得したいデータ縦方向
    // @params alpha 取得したいデータ横方向
    // @params matrix 2x? データ配列
    // @return y(double) 入力mach, alphaのデータ
    const int col_number = 15;
    double y;

    int index_mach, index_alpha;
    double Dmach = -1;
    double Dalpha = -1;

    // search index mach and calculate Dmach
    if(mach < matrix(1, 0) || mach > matrix(matrix.rows()-1, 0)){
        std::cout << "ERROR : interp_matrix_2d. First argument is out of the boundary of matrix.";
        exit(1);
    }
    for (int i = 2; i < matrix.rows(); i++) {
        if(mach < matrix(i, 0)){
            index_mach = i - 1;

            double mach_lower, mach_higher;
            mach_lower  = matrix(i - 1, 0);
            mach_higher = matrix(    i, 0);
            Dmach = (mach - mach_lower)/(mach_higher - mach_lower);

            break;
        }
    }
    

    // search index alpha and calculate Dalpha
    if(alpha < matrix(0, 1) || alpha > matrix(0, col_number -1)){
        std::cout << "ERROR : interp_matrix_2d. Second argument is out of the boundary of matrix.";
        exit(1);
    }
    for (int i = 2; i < col_number; i++) {
        if(alpha < matrix(0, i)){
            index_alpha = i - 1;

            double alpha_lower, alpha_higher;
            alpha_lower  = matrix(0, i - 1);
            alpha_higher = matrix(0,     i);
            Dalpha = (alpha - alpha_lower)/(alpha_higher - alpha_lower);

            break;
        }
    }

    // calculate y
    if(Dmach < 0.5){
        if(Dalpha < 0.5){
            y = matrix(index_mach, index_alpha)
              + ( matrix(index_mach + 1, index_alpha) - matrix(index_mach, index_alpha)) * Dmach
              + ( matrix(index_mach, index_alpha + 1) - matrix(index_mach, index_alpha)) * Dalpha;
        }else{
            y = matrix(index_mach, index_alpha + 1)
              + ( matrix(index_mach + 1, index_alpha + 1) - matrix(index_mach, index_alpha + 1)) * Dmach
              + ( matrix(index_mach, index_alpha + 1) - matrix(index_mach, index_alpha)) * (Dalpha - 1);
        }
    }else{
        if(Dalpha < 0.5){
            y = matrix(index_mach + 1, index_alpha)
              + ( matrix(index_mach + 1, index_alpha) - matrix(index_mach, index_alpha)) * (Dmach - 1)
              + ( matrix(index_mach + 1, index_alpha + 1) - matrix(index_mach + 1, index_alpha)) * Dalpha;
        }else{
            y = matrix(index_mach + 1, index_alpha + 1)
              + ( matrix(index_mach + 1, index_alpha + 1) - matrix(index_mach, index_alpha + 1)) * (Dmach - 1)
              + ( matrix(index_mach + 1, index_alpha + 1) - matrix(index_mach + 1, index_alpha)) * (Dalpha - 1);
        }
    }

    return y;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x20, %rsp
movq %rdi, %rbx
movsd %xmm1, 0x10(%rsp)
movsd %xmm0, (%rsp)
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0x6548
movsd (%rsp), %xmm1
movsd (%rax), %xmm0
ucomisd %xmm1, %xmm0
ja 0x164ca
movq 0x8(%rbx), %rsi
decq %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x6548
movsd (%rsp), %xmm0
ucomisd (%rax), %xmm0
ja 0x164ca
pushq $0x2
popq %r14
movsd 0x9e55(%rip), %xmm0     # 0x1ff00
movsd %xmm0, 0x8(%rsp)
cmpq %r14, 0x8(%rbx)
jle 0x160d8
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x6548
movsd (%rsp), %xmm1
movsd (%rax), %xmm0
ucomisd %xmm1, %xmm0
ja 0x160da
incq %r14
jmp 0x160b1
jmp 0x16123
leaq -0x1(%r14), %r13
movslq %r13d, %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x6548
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x6548
movsd (%rax), %xmm0
movsd (%rsp), %xmm1
movsd 0x8(%rsp), %xmm2
subsd %xmm2, %xmm1
subsd %xmm2, %xmm0
divsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
pushq $0x1
popq %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x6548
movsd (%rax), %xmm0
ucomisd 0x10(%rsp), %xmm0
ja 0x164da
pushq $0xe
popq %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x6548
movsd 0x10(%rsp), %xmm0
ucomisd (%rax), %xmm0
ja 0x164da
pushq $0x2
popq %r15
cmpq $0xf, %r15
je 0x16185
movq %rbx, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x6548
movsd (%rax), %xmm0
ucomisd 0x10(%rsp), %xmm0
ja 0x161a8
incq %r15
jmp 0x16161
movsd 0x9d73(%rip), %xmm0     # 0x1ff00
movsd %xmm0, 0x10(%rsp)
movsd 0x9c9d(%rip), %xmm0     # 0x1fe38
ucomisd 0x8(%rsp), %xmm0
ja 0x1620e
jmp 0x162b7
leaq -0x1(%r15), %r14
movq %rbx, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x6548
movsd (%rax), %xmm0
movsd %xmm0, (%rsp)
movq %rbx, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x6548
movsd 0x10(%rsp), %xmm1
movsd (%rax), %xmm0
movsd (%rsp), %xmm2
subsd %xmm2, %xmm1
subsd %xmm2, %xmm0
divsd %xmm0, %xmm1
movsd 0x9c46(%rip), %xmm0     # 0x1fe38
ucomisd 0x8(%rsp), %xmm0
movsd %xmm1, 0x10(%rsp)
jbe 0x162ad
ucomisd %xmm1, %xmm0
jbe 0x16362
movslq %r13d, %r15
movslq %r14d, %r14
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x6548
movsd (%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
incl %r13d
movslq %r13d, %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x6548
movsd (%rax), %xmm0
movsd %xmm0, (%rsp)
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x6548
movsd (%rsp), %xmm0
subsd (%rax), %xmm0
mulsd 0x8(%rsp), %xmm0
addsd 0x18(%rsp), %xmm0
movsd %xmm0, (%rsp)
leal 0x1(%r14), %eax
movslq %eax, %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x6548
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x6548
movsd 0x8(%rsp), %xmm0
subsd (%rax), %xmm0
mulsd 0x10(%rsp), %xmm0
jmp 0x16401
ucomisd %xmm1, %xmm0
jbe 0x1640b
leal 0x1(%r13), %eax
movslq %eax, %r15
movslq %r14d, %r14
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x6548
movsd (%rax), %xmm0
movsd %xmm0, (%rsp)
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x6548
movsd (%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
movslq %r13d, %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x6548
movsd 0x18(%rsp), %xmm1
subsd (%rax), %xmm1
movsd 0x8(%rsp), %xmm0
addsd 0x9bea(%rip), %xmm0     # 0x1ff00
mulsd %xmm1, %xmm0
addsd (%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
leal 0x1(%r14), %eax
movslq %eax, %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x6548
movsd (%rax), %xmm0
movsd %xmm0, (%rsp)
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x6548
movsd (%rsp), %xmm0
subsd (%rax), %xmm0
mulsd 0x10(%rsp), %xmm0
jmp 0x164b6
movslq %r13d, %r12
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x6548
movsd (%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
incl %r13d
movslq %r13d, %rsi
movq %rbx, %rdi
movq %r15, %rdx
callq 0x6548
movsd (%rax), %xmm0
movsd %xmm0, (%rsp)
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x6548
movsd (%rsp), %xmm0
subsd (%rax), %xmm0
mulsd 0x8(%rsp), %xmm0
addsd 0x18(%rsp), %xmm0
movsd %xmm0, (%rsp)
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x6548
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %rbx, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x6548
movsd 0x8(%rsp), %xmm1
subsd (%rax), %xmm1
movsd 0x10(%rsp), %xmm0
addsd 0x9b03(%rip), %xmm0     # 0x1ff00
mulsd %xmm1, %xmm0
addsd (%rsp), %xmm0
jmp 0x164bc
leal 0x1(%r13), %eax
movslq %eax, %r12
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x6548
movsd (%rax), %xmm0
movsd %xmm0, (%rsp)
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x6548
movsd (%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
movslq %r13d, %rsi
movq %rbx, %rdi
movq %r15, %rdx
callq 0x6548
movsd 0x18(%rsp), %xmm1
subsd (%rax), %xmm1
movsd 0x8(%rsp), %xmm0
addsd 0x9a99(%rip), %xmm0     # 0x1ff00
mulsd %xmm1, %xmm0
addsd (%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x6548
movsd (%rax), %xmm0
movsd %xmm0, (%rsp)
movq %rbx, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x6548
movsd (%rsp), %xmm1
subsd (%rax), %xmm1
movsd 0x10(%rsp), %xmm0
addsd 0x9a4e(%rip), %xmm0     # 0x1ff00
mulsd %xmm1, %xmm0
addsd 0x8(%rsp), %xmm0
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movq 0x18adf(%rip), %rdi     # 0x2efb0
leaq 0xc158(%rip), %rsi      # 0x22630
jmp 0x164e8
movq 0x18acf(%rip), %rdi     # 0x2efb0
leaq 0xc193(%rip), %rsi      # 0x2267b
callq 0x5370
pushq $0x1
popq %rdi
callq 0x5440