// File: /workspace/github_cpp_makefile_star2_O3/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, %r12
movq %rdi, %rbp
movq (%rsi), %rax
cmpq 0x8(%rsi), %rax
jne 0x2a705
leaq 0x50(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x19326
leaq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x20ee2
leaq 0x50(%rsp), %rdi
callq 0x18494
movq (%r12), %rsi
leaq 0x50(%rsp), %rdi
callq 0x1e246
leaq 0x8(%rsp), %rdi
leaq 0x50(%rsp), %rsi
movl $0x1, %edx
callq 0x1a8b4
leaq 0x50(%rsp), %rdi
callq 0x19a36
movq 0x8(%rsp), %r14
movq 0x10(%rsp), %rbx
testq %r14, %r14
je 0x2a690
movq %r12, 0x48(%rsp)
xorl %r15d, %r15d
leaq 0x8(%rsp), %r12
movq %r14, 0x30(%rsp)
movq 0x10(%rsp), %rax
leaq 0x1(%rax), %r14
imulq %r15, %r14
imulq 0x8(%rsp), %rax
cmpq %rax, %r14
jae 0x2a2d5
movq 0x18(%rsp), %rax
movss (%rax,%r14,4), %xmm0
ucomiss 0x12264(%rip), %xmm0    # 0x3c490
jne 0x2a2c2
jp 0x2a2c2
xorl %r13d, %r13d
movq 0x10(%rsp), %rcx
movq %rcx, %r14
imulq %r13, %r14
addq %r15, %r14
imulq 0x8(%rsp), %rcx
cmpq %rcx, %r14
jae 0x2a272
movss (%rax,%r14,4), %xmm0
ucomiss 0x1222e(%rip), %xmm0    # 0x3c490
jne 0x2a2b4
jp 0x2a2b4
incq %r13
cmpq %r13, 0x30(%rsp)
jne 0x2a23b
jmp 0x2a2c2
leaq 0x50(%rsp), %rdi
leaq 0x121cf(%rip), %rsi     # 0x3c44d
movl $0x44, %edx
movl $0x3, %ecx
callq 0x387b6
movl $0x1f, %edx
leaq 0x50(%rsp), %rdi
leaq 0x12282(%rip), %rsi     # 0x3c520
callq 0x93b0
leaq 0x50(%rsp), %rdi
callq 0x389de
movq 0x18(%rsp), %rax
jmp 0x2a255
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x1a238
incq %r15
movq 0x30(%rsp), %r14
cmpq %r14, %r15
jne 0x2a1fe
jmp 0x2a314
leaq 0x50(%rsp), %r13
movq %r13, %rdi
leaq 0x12169(%rip), %rsi     # 0x3c44d
movl $0x44, %edx
movl $0x3, %ecx
callq 0x387b6
movl $0x1f, %edx
movq %r13, %rdi
leaq 0x1221e(%rip), %rsi     # 0x3c520
callq 0x93b0
movq %r13, %rdi
callq 0x389de
jmp 0x2a21a
xorl %r15d, %r15d
movq %rbp, 0x40(%rsp)
movq 0x10(%rsp), %rax
leaq 0x1(%rax), %r14
imulq %r15, %r14
imulq 0x8(%rsp), %rax
cmpq %rax, %r14
jae 0x2a651
movq 0x18(%rsp), %rax
testq %rbx, %rbx
je 0x2a43a
movss (%rax,%r14,4), %xmm0
movss %xmm0, 0x4(%rsp)
xorl %r12d, %r12d
movq 0x10(%rsp), %rcx
movq %rcx, %r13
imulq %r15, %r13
addq %r12, %r13
imulq 0x8(%rsp), %rcx
movq %r13, %r14
cmpq %rcx, %r13
jae 0x2a396
movss (%rax,%r13,4), %xmm0
cmpq %rcx, %r14
jae 0x2a3ec
divss 0x4(%rsp), %xmm0
movss %xmm0, (%rax,%r14,4)
incq %r12
cmpq %r12, %rbx
jne 0x2a355
jmp 0x2a43a
leaq 0x50(%rsp), %r14
movq %r14, %rdi
leaq 0x120a8(%rip), %rsi     # 0x3c44d
movl $0x44, %edx
movl $0x3, %ecx
callq 0x387b6
movl $0x1f, %edx
movq %r14, %rdi
leaq 0x1215d(%rip), %rsi     # 0x3c520
callq 0x93b0
movq %r14, %rdi
callq 0x389de
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %rax
movq %r15, %r14
imulq %rcx, %r14
addq %r12, %r14
imulq 0x8(%rsp), %rcx
jmp 0x2a372
movss %xmm0, (%rsp)
leaq 0x50(%rsp), %r13
movq %r13, %rdi
leaq 0x1204d(%rip), %rsi     # 0x3c44d
movl $0x50, %edx
movl $0x3, %ecx
callq 0x387b6
movl $0x1f, %edx
movq %r13, %rdi
leaq 0x12102(%rip), %rsi     # 0x3c520
callq 0x93b0
movq %r13, %rdi
callq 0x389de
movq 0x18(%rsp), %rax
movss (%rsp), %xmm0
jmp 0x2a37d
xorl %ebp, %ebp
cmpq %r15, %rbp
je 0x2a5e2
movq 0x10(%rsp), %rcx
movq %rcx, %r14
imulq %rbp, %r14
addq %r15, %r14
imulq 0x8(%rsp), %rcx
cmpq %rcx, %r14
jae 0x2a5f5
testq %rbx, %rbx
je 0x2a5e2
movss (%rax,%r14,4), %xmm0
movss %xmm0, (%rsp)
xorl %r12d, %r12d
movq 0x10(%rsp), %r14
movq %rbp, %r13
imulq %r14, %r13
addq %r12, %r13
movq 0x8(%rsp), %rcx
imulq %r14, %rcx
cmpq %rcx, %r13
jae 0x2a4ee
movss (%rax,%r13,4), %xmm0
movss %xmm0, 0x4(%rsp)
movq %r15, %r13
imulq %r14, %r13
addq %r12, %r13
cmpq %rcx, %r13
jae 0x2a540
movss (%rax,%r13,4), %xmm1
mulss (%rsp), %xmm1
imulq %rbp, %r14
addq %r12, %r14
cmpq %rcx, %r14
jae 0x2a592
movss 0x4(%rsp), %xmm0
subss %xmm1, %xmm0
movss %xmm0, (%rax,%r14,4)
incq %r12
cmpq %r12, %rbx
jne 0x2a47a
jmp 0x2a5e2
leaq 0x50(%rsp), %r14
movq %r14, %rdi
leaq 0x11f50(%rip), %rsi     # 0x3c44d
movl $0x44, %edx
movl $0x3, %ecx
callq 0x387b6
movl $0x1f, %edx
movq %r14, %rdi
leaq 0x12005(%rip), %rsi     # 0x3c520
callq 0x93b0
movq %r14, %rdi
callq 0x389de
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %rax
movq 0x8(%rsp), %rcx
imulq %r14, %rcx
jmp 0x2a497
leaq 0x50(%rsp), %r14
movq %r14, %rdi
leaq 0x11efe(%rip), %rsi     # 0x3c44d
movl $0x44, %edx
movl $0x3, %ecx
callq 0x387b6
movl $0x1f, %edx
movq %r14, %rdi
leaq 0x11fb3(%rip), %rsi     # 0x3c520
callq 0x93b0
movq %r14, %rdi
callq 0x389de
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %rax
movq 0x8(%rsp), %rcx
imulq %r14, %rcx
jmp 0x2a4b6
movss %xmm1, 0x3c(%rsp)
leaq 0x50(%rsp), %r13
movq %r13, %rdi
leaq 0x11ea6(%rip), %rsi     # 0x3c44d
movl $0x50, %edx
movl $0x3, %ecx
callq 0x387b6
movl $0x1f, %edx
movq %r13, %rdi
leaq 0x11f5b(%rip), %rsi     # 0x3c520
callq 0x93b0
movq %r13, %rdi
callq 0x389de
movq 0x18(%rsp), %rax
movss 0x3c(%rsp), %xmm1
jmp 0x2a4d1
incq %rbp
movq 0x30(%rsp), %r14
cmpq %r14, %rbp
jne 0x2a43c
jmp 0x2a639
leaq 0x50(%rsp), %r12
movq %r12, %rdi
leaq 0x11e49(%rip), %rsi     # 0x3c44d
movl $0x44, %edx
movl $0x3, %ecx
callq 0x387b6
movl $0x1f, %edx
movq %r12, %rdi
leaq 0x11efe(%rip), %rsi     # 0x3c520
callq 0x93b0
movq %r12, %rdi
callq 0x389de
movq 0x18(%rsp), %rax
jmp 0x2a463
incq %r15
cmpq %r14, %r15
movq 0x40(%rsp), %rbp
movq 0x48(%rsp), %r12
jne 0x2a31c
jmp 0x2a690
leaq 0x50(%rsp), %r12
movq %r12, %rdi
leaq 0x11ded(%rip), %rsi     # 0x3c44d
movl $0x44, %edx
movl $0x3, %ecx
callq 0x387b6
movl $0x1f, %edx
movq %r12, %rdi
leaq 0x11ea2(%rip), %rsi     # 0x3c520
callq 0x93b0
movq %r12, %rdi
callq 0x389de
jmp 0x2a338
movq (%r12), %rsi
movq 0x8(%r12), %rdx
movq %rbp, %rdi
callq 0x197fe
testq %r14, %r14
je 0x2a6e6
movq %r14, %r13
shlq $0x2, %rbx
xorl %r14d, %r14d
xorl %r15d, %r15d
movq 0x8(%r12), %rdx
movq %rdx, %rdi
imulq %r15, %rdi
shlq $0x2, %rdi
addq 0x10(%rbp), %rdi
shlq $0x2, %rdx
movq 0x18(%rsp), %rsi
addq %rdx, %rsi
addq %r14, %rsi
callq 0x92b0
incq %r15
addq %rbx, %r14
cmpq %r15, %r13
jne 0x2a6b3
leaq 0x8(%rsp), %rdi
callq 0x19a36
movq %rbp, %rax
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1308c(%rip), %rsi     # 0x3d798
leaq 0x50(%rsp), %r15
movq %r15, %rdi
movl $0x122, %edx            # imm = 0x122
movl $0x3, %ecx
callq 0x387b6
leaq 0x130be(%rip), %rsi     # 0x3d7e8
movl $0x1c, %edx
movq %r15, %rdi
callq 0x93b0
leaq 0x50(%rsp), %rdi
callq 0x389de
jmp 0x2a186
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x389de
jmp 0x2a7af
jmp 0x2a791
jmp 0x2a7a2
jmp 0x2a791
jmp 0x2a7a2
jmp 0x2a791
jmp 0x2a7a2
jmp 0x2a791
jmp 0x2a7a2
jmp 0x2a791
jmp 0x2a791
jmp 0x2a7a2
jmp 0x2a7a2
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x19a36
jmp 0x2a7a5
jmp 0x2a7a2
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x18494
jmp 0x2a7af
jmp 0x2a791
jmp 0x2a791
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x389de
jmp 0x2a7a5
jmp 0x2a7a2
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x19a36
movq %rbx, %rdi
callq 0x95d0
nop