static VALUE rb_gsl_linalg_complex_LU_invert(int argc, VALUE *argv, VALUE obj) { gsl_matrix_complex *m = NULL, *mtmp = NULL, *inverse = NULL; gsl_permutation *p = NULL; int flagm = 0, signum, itmp; switch (TYPE(obj)) { case T_MODULE: case T_CLASS: case T_OBJECT: CHECK_MATRIX_COMPLEX(argv[0]); Data_Get_Struct(argv[0], gsl_matrix_complex, m); if (CLASS_OF(argv[0]) != cgsl_matrix_complex_LU) { mtmp = gsl_matrix_complex_alloc(m->size1, m->size2); gsl_matrix_complex_memcpy(mtmp, m); flagm = 1; } else { mtmp = m; } itmp = 1; break; default: Data_Get_Struct(obj, gsl_matrix_complex, m); if (CLASS_OF(obj) != cgsl_matrix_complex_LU) { mtmp = gsl_matrix_complex_alloc(m->size1, m->size2); gsl_matrix_complex_memcpy(mtmp, m); flagm = 1; } else { mtmp = m; } itmp = 0; } if (flagm == 1) { p = gsl_permutation_alloc(m->size1); gsl_linalg_complex_LU_decomp(mtmp, p, &signum); } else { Data_Get_Struct(argv[itmp], gsl_permutation, p); } inverse = gsl_matrix_complex_alloc(m->size1, m->size2); gsl_linalg_complex_LU_invert(mtmp, p, inverse); if (flagm == 1) { gsl_matrix_complex_free(mtmp); gsl_permutation_free(p); } return Data_Wrap_Struct(cgsl_matrix_complex, 0, gsl_matrix_complex_free, inverse); }
bool Matrix_Inverse(gsl_matrix_complex *A, gsl_matrix_complex *B){ int s; gsl_matrix_complex *T; gsl_permutation *p; T=gsl_matrix_complex_alloc(B->size1,B->size2); gsl_matrix_complex_memcpy(T, B); p=gsl_permutation_alloc (T->size1); gsl_linalg_complex_LU_decomp (T, p, &s); gsl_linalg_complex_LU_invert (T, p,A); gsl_permutation_free(p); gsl_matrix_complex_free(T); return true; }
static void *matrix_invert(void *m, bool complex) { int sign = 0; int size = ((gsl_matrix *)m)->size1; void *result; if (size != ((gsl_matrix *)m)->size2) return NULL; gsl_permutation *p = gsl_permutation_calloc(size); if (!complex) { gsl_matrix *tmp = gsl_matrix_alloc(size, size); result = gsl_matrix_alloc(size, size); gsl_matrix_memcpy(tmp, (gsl_matrix *)m); gsl_linalg_LU_decomp(tmp, p, &sign); if (gsl_linalg_LU_invert(tmp, p, (gsl_matrix *)result) != GSL_SUCCESS) { gsl_matrix_free(result); return NULL; } gsl_matrix_free(tmp); } else { gsl_matrix_complex *tmp = gsl_matrix_complex_alloc(size, size); result = gsl_matrix_complex_alloc(size, size); gsl_matrix_complex_memcpy(tmp, (gsl_matrix_complex *)m); gsl_linalg_complex_LU_decomp(tmp, p, &sign); if (gsl_linalg_complex_LU_invert(tmp, p, (gsl_matrix_complex *)result) != GSL_SUCCESS) { gsl_matrix_complex_free(result); return NULL; } gsl_matrix_complex_free(tmp); } gsl_permutation_free(p); return result; }
static int mc_inverse(lua_State *L) { mMatComplex *m = qlua_checkMatComplex(L, 1); mMatComplex *lu; mMatComplex *r; gsl_permutation *p; int signum; if (m->l_size != m->r_size) return luaL_error(L, "square matrix expected"); p = new_permutation(L, m->l_size); lu = qlua_newMatComplex(L, m->l_size, m->l_size); r = qlua_newMatComplex(L, m->l_size, m->l_size); gsl_matrix_complex_memcpy(lu->m, m->m); gsl_linalg_complex_LU_decomp(lu->m, p, &signum); if (gsl_linalg_complex_LU_invert(lu->m, p, r->m)) luaL_error(L, "matrix:inverse() failed"); gsl_permutation_free(p); return 1; }
/** * C++ version of gsl_linalg_complex_LU_invert(). * @param LU An LU decomposition matrix * @param p A permutation * @param inverse The inverse matrix * @return Error code on failure */ inline int complex_LU_invert( matrix_complex const& LU, permutation const& p, matrix_complex& inverse ){ return gsl_linalg_complex_LU_invert( LU.get(), p.get(), inverse.get() ); }