// File: /workspace/github_cpp_makefile_star2_O2/btbujiangjun[P]abcdl/src/algebra/MatrixAlgebra.cpp
// Function: abcdl::algebra::Matrix<int>::inverse() const
Matrix<real> Matrix<T>::inverse() const{
    CHECK(_rows == _cols);
    //copy src mat
    Matrix<real> extend_mat = clone();
    //extend eye matrix
    extend_mat.extend(EyeMatrix<real>(_rows), Axis_type::COL);

    //adjust row
    size_t extend_mat_rows = extend_mat.rows();
    size_t extend_mat_cols = extend_mat.cols();
    for(size_t i = 0; i != extend_mat_rows; i++){
        if(extend_mat.get_data(i, i) == 0){
            size_t j;
            for(j = 0; j != extend_mat_rows; j++){
                if(extend_mat.get_data(j, i) != 0){
                    extend_mat.swap_row(i, j);
                    break;
                }
            }
            CHECK(j <= extend_mat_rows);//every element is zero in col i, cannot be inverse.
        }
    }

    //calc extend matrix
    for(size_t i = 0; i != extend_mat_rows; i++){
        //all element div the first element,to make diagonal elemnt is 1
        real diagonal_element = extend_mat.get_data(i, i);
        for(size_t j = 0; j < extend_mat_cols; j++){
            extend_mat.set_data(extend_mat.get_data(i, j)/diagonal_element, i, j);
        }

        //to make the element of other rows in col i is 0
        for(size_t m = 0; m != extend_mat_rows; m++){
            if(m == i){//skip itself
                continue;
            }
            real element = extend_mat.get_data(m, i);
            for(size_t n = 0; n != extend_mat_cols; n++){
                extend_mat.set_data(extend_mat.get_data(m, n) - extend_mat.get_data(i, n) * element, m, n);
            }
        }
    }

    //calc inverse matrix
    Matrix<real> result_mat(_rows, _cols);
    for(size_t i = 0; i != extend_mat_rows; i++){
        memcpy(&result_mat.data()[i * _cols], &extend_mat.data()[i * extend_mat_cols + _cols], sizeof(real) * _cols);
    }
    return result_mat;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1d8, %rsp            # imm = 0x1D8
movq %rsi, %rbp
movq %rdi, 0x10(%rsp)
movq (%rsi), %rax
cmpq 0x8(%rsi), %rax
jne 0x23ac1
leaq 0x28(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x179e6
leaq 0x50(%rsp), %rdi
movq %rbx, %rsi
callq 0x1dbf0
leaq 0x28(%rsp), %rdi
callq 0x16d30
movq (%rbp), %rsi
leaq 0x28(%rsp), %rdi
callq 0x1be2e
leaq 0x50(%rsp), %rdi
leaq 0x28(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x18c04
leaq 0x28(%rsp), %rdi
callq 0x17fd6
leaq 0x50(%rsp), %r15
movq (%r15), %rax
movq %rax, 0x8(%rsp)
movq 0x8(%r15), %r14
xorl %r12d, %r12d
cmpq 0x8(%rsp), %r12
je 0x23945
movq %r15, %rdi
movq %r12, %rsi
movq %r12, %rdx
callq 0x18010
movss (%rax), %xmm0
ucomiss 0xeb8d(%rip), %xmm0     # 0x32490
jne 0x23940
jp 0x23940
xorl %ebx, %ebx
cmpq %rbx, 0x8(%rsp)
je 0x23940
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x18010
movss (%rax), %xmm0
ucomiss 0xeb67(%rip), %xmm0     # 0x32490
jne 0x23932
jp 0x23932
incq %rbx
jmp 0x23909
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x18638
incq %r12
jmp 0x238e3
xorl %r15d, %r15d
leaq 0x50(%rsp), %r12
leaq 0x28(%rsp), %r13
movq %rbp, 0x20(%rsp)
cmpq 0x8(%rsp), %r15
je 0x23a4a
movq %r12, %rdi
movq %r15, %rsi
movq %r15, %rdx
callq 0x18010
movss (%rax), %xmm0
movss %xmm0, 0x4(%rsp)
xorl %ebx, %ebx
cmpq %rbx, %r14
je 0x239b5
movq %r12, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x18010
movss (%rax), %xmm0
divss 0x4(%rsp), %xmm0
movss %xmm0, 0x28(%rsp)
movq %r12, %rdi
movq %r13, %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x1811e
incq %rbx
jmp 0x2397c
xorl %ebp, %ebp
cmpq 0x8(%rsp), %rbp
je 0x23a3d
cmpq %r15, %rbp
jne 0x239c8
incq %rbp
jmp 0x239b7
movq %r12, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x18010
movss (%rax), %xmm0
movss %xmm0, 0x1c(%rsp)
xorl %ebx, %ebx
cmpq %rbx, %r14
je 0x239c3
movq %r12, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x18010
movss (%rax), %xmm0
movss %xmm0, 0x4(%rsp)
movq %r12, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x18010
movss (%rax), %xmm0
mulss 0x1c(%rsp), %xmm0
movss 0x4(%rsp), %xmm1
subss %xmm0, %xmm1
movss %xmm1, 0x28(%rsp)
movq %r12, %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x1811e
incq %rbx
jmp 0x239e2
incq %r15
movq 0x20(%rsp), %rbp
jmp 0x23957
movq (%rbp), %rsi
movq 0x8(%rbp), %rdx
movq 0x10(%rsp), %rdi
callq 0x17dc6
shlq $0x2, %r14
xorl %ebx, %ebx
xorl %r15d, %r15d
cmpq %r15, 0x8(%rsp)
je 0x23aa0
movq 0x8(%rbp), %rdx
movq %rdx, %rdi
imulq %r15, %rdi
shlq $0x2, %rdi
movq 0x10(%rsp), %rax
addq 0x10(%rax), %rdi
shlq $0x2, %rdx
movq 0x60(%rsp), %rsi
addq %rdx, %rsi
addq %rbx, %rsi
callq 0x92c0
incq %r15
addq %r14, %rbx
jmp 0x23a65
leaq 0x50(%rsp), %rdi
callq 0x17fd6
movq 0x10(%rsp), %rax
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xfca0(%rip), %rsi      # 0x33768
leaq 0x50(%rsp), %rbx
pushq $0x3
popq %rcx
movq %rbx, %rdi
movl $0x122, %edx            # imm = 0x122
callq 0x2ee18
leaq 0xfcd4(%rip), %rsi      # 0x337b8
movq %rbx, %rdi
callq 0x9370
leaq 0x50(%rsp), %rdi
callq 0x2f054
jmp 0x2387e
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x2f054
jmp 0x23b45
jmp 0x23b38
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x17fd6
jmp 0x23b3b
jmp 0x23b38
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x16d30
jmp 0x23b45
jmp 0x23b38
jmp 0x23b38
jmp 0x23b38
jmp 0x23b38
jmp 0x23b38
jmp 0x23b38
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x17fd6
movq %rbx, %rdi
callq 0x95e0
nop