/*
  matrix.c
  Ruby/GSL: Ruby extension library for GSL (GNU Scientific Library)
    (C) Copyright 2001-2004 by Yoshiki Tsunesada
  
  Ruby/GSL is free software: you can redistribute it and/or modify it
  under the terms of the GNU General Public License.
  This library is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY.
*/

#include "rb_gsl_array.h"
#include "rb_gsl_complex.h"
#ifdef HAVE_NARRAY_H
#include "rb_gsl_with_narray.h"
#endif

static gsl_matrix* gsl_matrix_alloc_from_array_sizes(VALUE ary, 
						     VALUE nn1, VALUE nn2);
static gsl_matrix* gsl_matrix_alloc_from_arrays(int argc, VALUE *argv);
void gsl_matrix_view_free(gsl_matrix_view * mv);
static gsl_matrix* gsl_matrix_alloc_from_vector_sizes(VALUE ary,
						      VALUE nn1, VALUE nn2);
static gsl_matrix* gsl_matrix_alloc_from_vectors(int argc, VALUE *argv);
static VALUE rb_gsl_matrix_new(int argc, VALUE *argv, VALUE klass)
{
  gsl_matrix *m = NULL;
  size_t n1, n2;
#ifdef HAVE_NARRAY_H
  size_t n;
  VALUE ary;
  struct NARRAY *na;
#endif

#ifdef HAVE_NARRAY_H
  if (argc < 1) 
    rb_raise(rb_eArgError, "wrong number of arguments (%d for >= 1)", argc);
  if (NA_IsNArray(argv[0])) {
    GetNArray(argv[0], na);
    n = na->shape[0]*na->shape[1];
    m = gsl_matrix_alloc(na->shape[1], na->shape[0]);
    if (m == NULL) rb_raise(rb_eNoMemError, "gsl_matrix_alloc failed");
    ary = na_change_type(argv[0], NA_DFLOAT);
    memcpy(m->data, NA_PTR_TYPE(ary,double*), n*sizeof(double));
    return Data_Wrap_Struct(cgsl_matrix, 0, gsl_matrix_free, m);
  }
#endif
  
  if (argc < 1) 
    rb_raise(rb_eArgError, "wrong number of arguments (%d for >= 1)", argc);
  
  if (CLASS_OF(argv[0]) == rb_cRange) argv[0] = rb_gsl_range2ary(argv[0]);
  switch (TYPE(argv[0])) {
  case T_FIXNUM:
  case T_BIGNUM:
    if (argc != 2) rb_raise(rb_eArgError, "wrong number of arguments");
    CHECK_FIXNUM(argv[1]);
    n1 = FIX2INT(argv[0]);
    n2 = FIX2INT(argv[1]);
    m = gsl_matrix_calloc(n1, n2);
    if (m == NULL) rb_raise(rb_eNoMemError, "gsl_matrix_calloc failed");
    break;
  case T_ARRAY:
    if (argc == 1) {
      m = gsl_matrix_alloc_from_arrays(argc, argv);
      break;
    }
    if (CLASS_OF(argv[1]) == rb_cRange) argv[1] = rb_gsl_range2ary(argv[1]);
    switch (TYPE(argv[1])) {
    case T_ARRAY:
      m = gsl_matrix_alloc_from_arrays(argc, argv);
      break;
    case T_FIXNUM:
    case T_BIGNUM:
      if (argc != 3) rb_raise(rb_eArgError, "wrong number of arguments (%d for 3)",
			      argc);
      CHECK_FIXNUM(argv[2]);
      m = gsl_matrix_alloc_from_array_sizes(argv[0], argv[1], argv[2]);
      break;
    default:
      rb_raise(rb_eTypeError, 
	       "wrong argument type %s\nUsage: new(n1, n2), new([], [], [], ...), new([], n1, n2)", rb_class2name(CLASS_OF(argv[1])));
      break;
    }
    break;
  default:
    if (VECTOR_P(argv[0])) {
      if (argc == 3 && FIXNUM_P(argv[1]) && FIXNUM_P(argv[2])) {
	m = gsl_matrix_alloc_from_vector_sizes(argv[0], argv[1], argv[2]);
      } else {
	m = gsl_matrix_alloc_from_vectors(argc, argv);
      }
    } else {
      rb_raise(rb_eTypeError, "wrong argument type %s\nUsage: new(n1, n2), new([], [], [], ...), new([], n1, n2)", rb_class2name(CLASS_OF(argv[0])));
    }
    break;
  }
  return Data_Wrap_Struct(klass, 0, gsl_matrix_free, m);
}

static VALUE rb_gsl_matrix_calloc(VALUE klass, VALUE nn1, VALUE nn2)
{
  gsl_matrix *m = NULL;
  CHECK_FIXNUM(nn1); CHECK_FIXNUM(nn2);
  m = gsl_matrix_calloc(FIX2INT(nn1), FIX2INT(nn2));
  if (m == NULL) rb_raise(rb_eNoMemError, "gsl_matrix_calloc failed");
  return Data_Wrap_Struct(klass, 0, gsl_matrix_free, m);
}

static VALUE rb_gsl_matrix_eye(int argc, VALUE *argv, VALUE klass)
{
  gsl_matrix *m = NULL;
  double val;
  size_t n, i;
  switch (argc) {
  case 1:
    CHECK_FIXNUM(argv[0]);
    n = FIX2INT(argv[0]);
    val = 1.0;
    break;
  case 2:
    CHECK_FIXNUM(argv[0]);
    Need_Float(argv[1]);
    n = FIX2INT(argv[0]);
    val = NUM2DBL(argv[1]);
    break;
  default:
    rb_raise(rb_eArgError, "wrong number of arguments (%d for 1 or 2)", argc);
    break;
  }
  m = gsl_matrix_calloc(n, n);
  if (m == NULL) rb_raise(rb_eNoMemError, "gsl_matrix_calloc failed");
  for (i = 0; i < n; i++) gsl_matrix_set(m, i, i, val);
  return Data_Wrap_Struct(klass, 0, gsl_matrix_free, m);
}

static VALUE rb_gsl_matrix_identity(VALUE klass, VALUE nn)
{
  gsl_matrix *m = NULL;
  size_t n, i;
  CHECK_FIXNUM(nn);
  n = FIX2INT(nn);
  m = gsl_matrix_calloc(n, n);
  if (m == NULL) rb_raise(rb_eNoMemError, "gsl_matrix_calloc failed");
  for (i = 0; i < n; i++) gsl_matrix_set(m, i, i, 1.0);
  return Data_Wrap_Struct(klass, 0, gsl_matrix_free, m);
}

/* create a matrix from arrays */
static gsl_matrix* gsl_matrix_alloc_from_arrays(int argc, VALUE *argv)
{
  int n, i, j;
  gsl_matrix *m = NULL;
  if (CLASS_OF(argv[0]) == rb_cRange) argv[0] = rb_gsl_range2ary(argv[0]);
  else Check_Type(argv[0], T_ARRAY);
  n = RARRAY(argv[0])->len;
  m = gsl_matrix_alloc(argc, n);
  if (m == NULL) rb_raise(rb_eNoMemError, "gsl_matrix_alloc failed");
  for (i = 0; i < argc; i++) {
    if (CLASS_OF(argv[i]) == rb_cRange) argv[i] = rb_gsl_range2ary(argv[i]);
    else Check_Type(argv[i], T_ARRAY);
    for (j = 0; j < n; j++) {
      if (j >= RARRAY(argv[i])->len)
	gsl_matrix_set(m, i, j, 0.0);
      else
	gsl_matrix_set(m, i, j, NUM2DBL(rb_ary_entry(argv[i], j)));
    }
  }
  return m;
}

static gsl_matrix* gsl_matrix_alloc_from_vectors(int argc, VALUE *argv)
{
  int n, i, j;
  gsl_matrix *m = NULL;
  gsl_vector *v = NULL;
  if (argc < 1) rb_raise(rb_eArgError, "too few arguments");
  CHECK_VECTOR(argv[0]);
  Data_Get_Struct(argv[0], gsl_vector, v);
  n = v->size;
  m = gsl_matrix_alloc(argc, n);
  if (m == NULL) rb_raise(rb_eNoMemError, "gsl_matrix_alloc failed");
  for (i = 0; i < argc; i++) {
    CHECK_VECTOR(argv[i]);
    Data_Get_Struct(argv[i], gsl_vector, v);
    for (j = 0; j < n; j++) {
      if (j >= v->size) gsl_matrix_set(m, i, j, 0.0);
      else gsl_matrix_set(m, i, j, gsl_vector_get(v, j));
    }
  }
  return m;
}

/* create a matrix from two sizes and an array */
static gsl_matrix* gsl_matrix_alloc_from_array_sizes(VALUE ary,
						     VALUE nn1, VALUE nn2)
{
  size_t n1, n2;
  gsl_matrix *m = NULL;
  int i, j, k;
  CHECK_FIXNUM(nn1); CHECK_FIXNUM(nn2);
  Check_Type(ary, T_ARRAY);
  n1 = FIX2INT(nn1);
  n2 = FIX2INT(nn2);
  m = gsl_matrix_alloc(n1, n2);
  if (m == NULL) rb_raise(rb_eNoMemError, "gsl_matrix_alloc failed");
  k = 0;
  for (i = 0; i < n1; i++) {
    for (j = 0; j < n2; j++, k++)
      gsl_matrix_set(m, i, j, NUM2DBL(rb_ary_entry(ary, k)));
  }
  return m;
}

static gsl_matrix* gsl_matrix_alloc_from_vector_sizes(VALUE ary,
						      VALUE nn1, VALUE nn2)
{
  size_t n1, n2;
  gsl_matrix *m = NULL;
  gsl_vector *v = NULL;
  int i, j, k;
  CHECK_VECTOR(ary);
  CHECK_FIXNUM(nn1); CHECK_FIXNUM(nn2);
  Data_Get_Struct(ary, gsl_vector, v);
  n1 = FIX2INT(nn1);
  n2 = FIX2INT(nn2);
  m = gsl_matrix_alloc(n1, n2);
  if (m == NULL) rb_raise(rb_eNoMemError, "gsl_matrix_alloc failed");
  k = 0;
  for (i = 0; i < n1; i++) {
    for (j = 0; j < n2; j++, k++) gsl_matrix_set(m, i, j, gsl_vector_get(v, k));
  }
  return m;
}

static VALUE rb_gsl_matrix_size1(VALUE obj)
{
  gsl_matrix *m = NULL;
  Data_Get_Struct(obj, gsl_matrix, m);
  return INT2FIX(m->size1);
}

static VALUE rb_gsl_matrix_size2(VALUE obj)
{
  gsl_matrix *m = NULL;
  Data_Get_Struct(obj, gsl_matrix, m);
  return INT2FIX(m->size2);
}

static VALUE rb_gsl_matrix_get(VALUE obj, VALUE i, VALUE j)
{
  gsl_matrix *m = NULL;
  Data_Get_Struct(obj, gsl_matrix, m);
  return rb_float_new(gsl_matrix_get(m, FIX2INT(i), FIX2INT(j)));
}

static VALUE rb_gsl_matrix_set(int argc, VALUE *argv, VALUE obj)
{
  gsl_matrix *m = NULL;
  size_t size1, size2;
  int i, j, k;
  VALUE ary;
  if (argc < 2) rb_raise(rb_eArgError, "too few arguments");
  Data_Get_Struct(obj, gsl_matrix, m);
  if (CLASS_OF(argv[0]) == rb_cRange) argv[0] = rb_gsl_range2ary(argv[0]);
  switch (TYPE(argv[0])) {
  case T_ARRAY:
    if (CLASS_OF(argv[1]) == rb_cRange) argv[1] = rb_gsl_range2ary(argv[1]);
    switch (TYPE(argv[1])) {
    case T_ARRAY:
      for (i = 0; i < m->size1; i++) {
	if (CLASS_OF(argv[i]) == rb_cRange) ary = rb_gsl_range2ary(argv[i]);
	else ary = argv[i];
	for (j = 0; j < m->size2; j++) {
	  gsl_matrix_set(m, i, j, NUM2DBL(rb_ary_entry(ary, j)));
	}
      }
      break;
    case T_FIXNUM:
    case T_BIGNUM:
      if (argc != 3) rb_raise(rb_eArgError, "wrong number of arguments");
      CHECK_FIXNUM(argv[1]); CHECK_FIXNUM(argv[2]);
      ary = argv[0];
      size1 = FIX2INT(argv[1]);
      size2 = FIX2INT(argv[2]);
      k = 0;
      for (i = 0; i < size1; i++) {
	for (j = 0; j < size2; j++, k++) {
	  gsl_matrix_set(m, i, j, NUM2DBL(rb_ary_entry(ary, k)));
	}
      }
      break;
    default:
      rb_raise(rb_eTypeError, "wrong argument type %s", rb_class2name(CLASS_OF(argv[1])));
      break;
    }
    break;
  case T_FIXNUM:
  case T_BIGNUM:
    if (argc != 3) {
      rb_raise(rb_eArgError, "wrong number of args. (usage: row, col, and val)");
    }
    CHECK_FIXNUM(argv[1]);
    Need_Float(argv[2]);
    gsl_matrix_set(m, FIX2INT(argv[0]), FIX2INT(argv[1]), NUM2DBL(argv[2]));
    break;
  default:
    rb_raise(rb_eTypeError, 
	     "wrong argument type %s", rb_class2name(CLASS_OF(argv[0])));
    break;
  }
  return obj;
}

static VALUE rb_gsl_matrix_set_all(VALUE obj, VALUE x)
{
  gsl_matrix *m = NULL;
  Need_Float(x);
  Data_Get_Struct(obj, gsl_matrix, m);
  gsl_matrix_set_all(m, NUM2DBL(x));
  return obj;
}

static VALUE rb_gsl_matrix_set_zero(VALUE obj)
{
  gsl_matrix *m = NULL;
  Data_Get_Struct(obj, gsl_matrix, m);
  gsl_matrix_set_zero(m);
  return obj;
}

static VALUE rb_gsl_matrix_set_identity(VALUE obj)
{
  gsl_matrix *m = NULL;
  Data_Get_Struct(obj, gsl_matrix, m);
  gsl_matrix_set_identity(m);
  return obj;
}

static VALUE rb_gsl_matrix_submatrix(int argc, VALUE *argv, VALUE obj)
{
  gsl_matrix *m = NULL;
  gsl_matrix_view *mv = NULL;
  size_t i, j, n1, n2;
  Data_Get_Struct(obj, gsl_matrix, m);
  switch (argc) {
  case 0:
    i = 0; j = 0;
    n1 = m->size1; n2 = m->size2;
    break;
  case 4:
    CHECK_FIXNUM(argv[0]); CHECK_FIXNUM(argv[1]);
    CHECK_FIXNUM(argv[2]); CHECK_FIXNUM(argv[3]);
    i = FIX2INT(argv[0]);  j = FIX2INT(argv[1]);
    n1 = FIX2INT(argv[2]);  n2 = FIX2INT(argv[3]);
    break;
  default:
    rb_raise(rb_eArgError, "wrong number of arguments (%d for 0 or 4)", argc);
    break;
  }
  mv = gsl_matrix_view_alloc();
  *mv = gsl_matrix_submatrix(m, i, j, n1, n2);
  return Data_Wrap_Struct(cgsl_matrix_view, 0, gsl_matrix_view_free, mv);
}

static VALUE rb_gsl_matrix_print(VALUE obj)
{
  gsl_matrix *m = NULL;
  int i, j;
  Data_Get_Struct(obj, gsl_matrix, m);
  printf("[ ");
  for (i = 0; i < m->size1; i++) {
    if (i != 0) printf("  ");
    for (j = 0; j < m->size2; j++) {
      printf("%4.3e ", gsl_matrix_get(m, i, j));
    }
    if (i == m->size1 - 1) printf("]\n");
    else printf("\n");
  }
  return obj;
}

static VALUE rb_gsl_matrix_row(VALUE obj, VALUE i)
{
  gsl_matrix *m = NULL;
  gsl_vector_view *vv = NULL;
  CHECK_FIXNUM(i);
  Data_Get_Struct(obj, gsl_matrix, m);
  vv = gsl_vector_view_alloc();
  *vv = gsl_matrix_row(m, FIX2INT(i));
  return Data_Wrap_Struct(cgsl_vector_view, 0, gsl_vector_view_free, vv);
}

static VALUE rb_gsl_matrix_column(VALUE obj, VALUE j)
{
  gsl_matrix *m = NULL;
  gsl_vector_view *vv = NULL;
  CHECK_FIXNUM(j);
  Data_Get_Struct(obj, gsl_matrix, m);
  vv = gsl_vector_view_alloc();
  *vv = gsl_matrix_column(m, FIX2INT(j));
  return Data_Wrap_Struct(cgsl_vector_col_view, 0, gsl_vector_view_free, vv);
}

static VALUE rb_gsl_matrix_diagonal(VALUE obj)
{
  gsl_matrix *m = NULL;
  gsl_vector_view *vv = NULL;
  Data_Get_Struct(obj, gsl_matrix, m);
  vv = gsl_vector_view_alloc();
  *vv = gsl_matrix_diagonal(m);
  return Data_Wrap_Struct(cgsl_vector_view, 0, gsl_vector_view_free, vv);
}

static VALUE rb_gsl_matrix_set_diagonal(VALUE obj, VALUE diag)
{
  gsl_matrix *m = NULL;
  gsl_vector *v;
  int i;
  Data_Get_Struct(obj, gsl_matrix, m);
  if (VECTOR_P(diag)) {
    Data_Get_Struct(diag, gsl_vector, v);
    for (i = 0; i < m->size1; i++) gsl_matrix_set(m, i, i, gsl_vector_get(v, i));
  } else if (TYPE(diag) == T_ARRAY) {
    for (i = 0; i < m->size1; i++) {
      gsl_matrix_set(m, i, i, NUM2DBL(rb_ary_entry(diag, i)));
    }
  } else {
    rb_raise(rb_eTypeError, "wrong argument type %s (GSL::Vector or Array expected)",
	     rb_class2name(CLASS_OF(diag)));
  }
  return obj;
}

static VALUE rb_gsl_matrix_subdiagonal(VALUE obj, VALUE k)
{
  gsl_matrix *m = NULL;
  gsl_vector_view *vv = NULL;
  CHECK_FIXNUM(k);
  Data_Get_Struct(obj, gsl_matrix, m);
  vv = gsl_vector_view_alloc();
  *vv = gsl_matrix_subdiagonal(m, FIX2INT(k));
  return Data_Wrap_Struct(cgsl_vector_view, 0, gsl_vector_view_free, vv);
}

static VALUE rb_gsl_matrix_superdiagonal(VALUE obj, VALUE k)
{
  gsl_matrix *m = NULL;
  gsl_vector_view *vv = NULL;
  CHECK_FIXNUM(k);
  Data_Get_Struct(obj, gsl_matrix, m);
  vv = gsl_vector_view_alloc();
  *vv = gsl_matrix_superdiagonal(m, FIX2INT(k));
  return Data_Wrap_Struct(cgsl_vector_view, 0, gsl_vector_view_free, vv);
}

static VALUE rb_gsl_matrix_clone(VALUE obj)
{
  gsl_matrix *m = NULL, *mnew = NULL;
  Data_Get_Struct(obj, gsl_matrix, m);
  mnew = gsl_matrix_alloc(m->size1, m->size2);
  gsl_matrix_memcpy(mnew, m);
  return Data_Wrap_Struct(cgsl_matrix, 0, gsl_matrix_free, mnew);
}

/* singleton */
static VALUE rb_gsl_matrix_swap(VALUE obj, VALUE mm1, VALUE mm2)
{
  gsl_matrix *m1 = NULL, *m2 = NULL;
  CHECK_MATRIX(mm1);  CHECK_MATRIX(mm2);
  Data_Get_Struct(mm1, gsl_matrix, m1);
  Data_Get_Struct(mm2, gsl_matrix, m2);
  gsl_matrix_swap(m1, m2);
  return mm1;
}

/* singleton */
static VALUE rb_gsl_matrix_memcpy(VALUE obj, VALUE mm1, VALUE mm2)
{
  gsl_matrix *m1 = NULL, *m2 = NULL;
  CHECK_MATRIX(mm1);  CHECK_MATRIX(mm2);
  Data_Get_Struct(mm1, gsl_matrix, m1);
  Data_Get_Struct(mm2, gsl_matrix, m2);
  gsl_matrix_memcpy(m1, m2);
  return mm1;
}

static VALUE rb_gsl_matrix_get_row(VALUE obj, VALUE i)
{
  gsl_matrix *m = NULL;
  gsl_vector *v = NULL;
  CHECK_FIXNUM(i);
  Data_Get_Struct(obj, gsl_matrix, m);
  v = gsl_vector_alloc(m->size1);
  if (v == NULL) rb_raise(rb_eNoMemError, "gsl_vector_alloc failed");
  gsl_matrix_get_row(v, m, FIX2INT(i));
  return Data_Wrap_Struct(cgsl_vector, 0, gsl_vector_free, v);
}

static VALUE rb_gsl_matrix_get_col(VALUE obj, VALUE i)
{
  gsl_matrix *m = NULL;
  gsl_vector *v = NULL;
  CHECK_FIXNUM(i);
  Data_Get_Struct(obj, gsl_matrix, m);
  v = gsl_vector_alloc(m->size2);
  if (v == NULL) rb_raise(rb_eNoMemError, "gsl_vector_alloc failed");
  gsl_matrix_get_col(v, m, FIX2INT(i));
  return Data_Wrap_Struct(cgsl_vector_col, 0, gsl_vector_free, v);
}

static VALUE rb_gsl_matrix_set_row(VALUE obj, VALUE i, VALUE vv)
{
  gsl_matrix *m = NULL;
  gsl_vector *v = NULL;
  int flag = 0;
  CHECK_FIXNUM(i);
  if (CLASS_OF(vv) == rb_cRange) vv = rb_gsl_range2ary(vv);
  if (TYPE(vv) == T_ARRAY) {
    v = get_vector(vv);
    flag = 1;
  } else {
    CHECK_VECTOR(vv);
    Data_Get_Struct(vv, gsl_vector, v);
  }
  Data_Get_Struct(obj, gsl_matrix, m);
  gsl_matrix_set_row(m, FIX2INT(i), v);
  if (flag == 1) gsl_vector_free(v);
  return obj;
}

static VALUE rb_gsl_matrix_set_col(VALUE obj, VALUE j, VALUE vv)
{
  gsl_matrix *m = NULL;
  gsl_vector *v = NULL;
  int flag = 0;
  CHECK_FIXNUM(j);
  if (CLASS_OF(vv) == rb_cRange) vv = rb_gsl_range2ary(vv);
  if (TYPE(vv) == T_ARRAY) {
    v = get_vector(vv);
    flag = 1;
  } else {
    CHECK_VECTOR(vv);
    Data_Get_Struct(vv, gsl_vector, v);
  }
  Data_Get_Struct(obj, gsl_matrix, m);
  gsl_matrix_set_col(m, FIX2INT(j), v);
  if (flag == 1) gsl_vector_free(v);
  return obj;
}

static VALUE rb_gsl_matrix_swap_rows(VALUE obj, VALUE i, VALUE j)
{
  gsl_matrix *m = NULL;
  CHECK_FIXNUM(i);   CHECK_FIXNUM(j);
  Data_Get_Struct(obj, gsl_matrix, m);
  gsl_matrix_swap_rows(m, FIX2INT(i), FIX2INT(j));
  return obj;
}

static VALUE rb_gsl_matrix_swap_columns(VALUE obj, VALUE i, VALUE j)
{
  gsl_matrix *m = NULL;
  CHECK_FIXNUM(i);   CHECK_FIXNUM(j);
  Data_Get_Struct(obj, gsl_matrix, m);
  gsl_matrix_swap_columns(m, FIX2INT(i), FIX2INT(j));
  return obj;
}

static VALUE rb_gsl_matrix_swap_rowcol(VALUE obj, VALUE i, VALUE j)
{
  gsl_matrix *m = NULL;
  CHECK_FIXNUM(i);   CHECK_FIXNUM(j);
  Data_Get_Struct(obj, gsl_matrix, m);
  gsl_matrix_swap_rowcol(m, FIX2INT(i), FIX2INT(j));
  return obj;
}

static VALUE rb_gsl_matrix_transpose_memcpy(VALUE obj)
{
  gsl_matrix *m = NULL, *mnew = NULL;
  Data_Get_Struct(obj, gsl_matrix, m);
  mnew = make_matrix_clone(m);
  gsl_matrix_transpose_memcpy(mnew, m);
  return Data_Wrap_Struct(cgsl_matrix, 0, gsl_matrix_free, mnew);
}

static VALUE rb_gsl_matrix_transpose(VALUE obj)
{
  gsl_matrix *m = NULL;
  Data_Get_Struct(obj, gsl_matrix, m);
  gsl_matrix_transpose(m);
  return obj;
}

static VALUE rb_gsl_matrix_isnull(VALUE obj)
{
  gsl_matrix *m = NULL;
  Data_Get_Struct(obj, gsl_matrix, m);
  return INT2FIX(gsl_matrix_isnull(m));
}

static VALUE rb_gsl_matrix_isnull2(VALUE obj)
{
  gsl_matrix *m = NULL;
  Data_Get_Struct(obj, gsl_matrix, m);
  if (gsl_matrix_isnull(m)) return Qtrue;
  else return Qfalse;
}

enum {
  GSL_MATRIX_ADD,
  GSL_MATRIX_ADD2,
  GSL_MATRIX_SUB,
  GSL_MATRIX_SUB2,
  GSL_MATRIX_MUL,
  GSL_MATRIX_MUL2,
  GSL_MATRIX_DIV,
  GSL_MATRIX_DIV2,
};

static VALUE rb_gsl_matrix_arithmetics(int flag, VALUE obj, VALUE bb);

static VALUE rb_gsl_matrix_arithmetics(int flag, VALUE obj, VALUE bb)
{
  gsl_matrix *m = NULL, *mb = NULL, *mnew = NULL;
  gsl_matrix_complex *cm = NULL, *cmb = NULL, *cmnew = NULL;
  gsl_complex *c = NULL;
  gsl_vector *v = NULL, *vnew = NULL;
  gsl_vector_complex *cv = NULL, *cvnew = NULL;
  Data_Get_Struct(obj, gsl_matrix, m);
  switch (TYPE(bb)) {
  case T_FLOAT:
  case T_FIXNUM:
  case T_BIGNUM:
    switch (flag) {
    case GSL_MATRIX_ADD:
      mnew = make_matrix_clone(m);
      gsl_matrix_add_constant(mnew, NUM2DBL(bb));
      return Data_Wrap_Struct(cgsl_matrix, 0, gsl_matrix_free, mnew);
      break;
    case GSL_MATRIX_ADD2:
      gsl_matrix_add_constant(m, NUM2DBL(bb));
      return obj;
      break;
    case GSL_MATRIX_SUB:
      mnew = make_matrix_clone(m);
      gsl_matrix_add_constant(mnew, -NUM2DBL(bb));
      return Data_Wrap_Struct(cgsl_matrix, 0, gsl_matrix_free, mnew);
      break;
    case GSL_MATRIX_SUB2:
      gsl_matrix_add_constant(m, -NUM2DBL(bb));
      return obj;
      break;
    case GSL_MATRIX_MUL:
      mnew = make_matrix_clone(m);
      gsl_matrix_scale(mnew, NUM2DBL(bb));
      return Data_Wrap_Struct(cgsl_matrix, 0, gsl_matrix_free, mnew);
      break;
    case GSL_MATRIX_MUL2:
      gsl_matrix_scale(m, NUM2DBL(bb));
      return obj;
      break;
    case GSL_MATRIX_DIV:
      mnew = make_matrix_clone(m);
      gsl_matrix_scale(mnew, 1.0/NUM2DBL(bb));
      return Data_Wrap_Struct(cgsl_matrix, 0, gsl_matrix_free, mnew);
      break;
    case GSL_MATRIX_DIV2:
      gsl_matrix_scale(m, 1.0/NUM2DBL(bb));
      return obj;
      break;
    default:
      rb_raise(rb_eRuntimeError, "operation not defined");
      break;
    }
    break;
  default:
    if (rb_obj_is_kind_of(bb, cgsl_matrix)) {
      Data_Get_Struct(bb, gsl_matrix, mb);
      switch (flag) {
      case GSL_MATRIX_ADD:
	mnew = make_matrix_clone(m);
	gsl_matrix_add(mnew, mb);
	return Data_Wrap_Struct(cgsl_matrix, 0, gsl_matrix_free, mnew);
	break;
      case GSL_MATRIX_ADD2:
	gsl_matrix_add(m, mb);
	return obj;
	break;
      case GSL_MATRIX_SUB:
	mnew = make_matrix_clone(m);
	gsl_matrix_sub(mnew,mb);
	return Data_Wrap_Struct(cgsl_matrix, 0, gsl_matrix_free, mnew);
	break;
      case GSL_MATRIX_SUB2:
	gsl_matrix_sub(m, mb);
	return obj;
	break;
      case GSL_MATRIX_MUL:
	mnew = make_matrix_clone(m);
	gsl_matrix_mul_elements(mnew, mb);
	return Data_Wrap_Struct(cgsl_matrix, 0, gsl_matrix_free, mnew);
	break;
      case GSL_MATRIX_MUL2:
	gsl_matrix_mul_elements(m, mb);
	return obj;
	break;
      case GSL_MATRIX_DIV:
	mnew = make_matrix_clone(m);
	gsl_matrix_div_elements(mnew, mb);
	return Data_Wrap_Struct(cgsl_matrix, 0, gsl_matrix_free, mnew);
	break;
      case GSL_MATRIX_DIV2:
	gsl_matrix_div_elements(m, mb);
	return obj;
	break;
      default:
	rb_raise(rb_eRuntimeError, "operation not defined");
	break;
      }
    } else if (rb_obj_is_kind_of(bb, cgsl_matrix_complex)) {
      Data_Get_Struct(bb, gsl_matrix_complex, cmb);
      cmnew = matrix_to_complex(m);
      switch (flag) {
      case GSL_MATRIX_ADD:
      case GSL_MATRIX_ADD2:
	gsl_matrix_complex_add(cmnew, cmb);
	break;
      case GSL_MATRIX_SUB:
      case GSL_MATRIX_SUB2:
	gsl_matrix_complex_sub(cmnew,cmb);
	break;
      case GSL_MATRIX_MUL:
      case GSL_MATRIX_MUL2:
	gsl_matrix_complex_mul_elements(cmnew, cmb);
	break;
      case GSL_MATRIX_DIV:
      case GSL_MATRIX_DIV2:
	gsl_matrix_complex_div_elements(cmnew, cmb);
	break;
      default:
	rb_raise(rb_eRuntimeError, "operation not defined");
	break;
      }
      return Data_Wrap_Struct(cgsl_matrix_complex, 0, gsl_matrix_complex_free, cmnew);
    } else if (rb_obj_is_kind_of(bb, cgsl_complex)) {
      Data_Get_Struct(bb, gsl_complex, c);
      cmb = gsl_matrix_complex_alloc(m->size1, m->size2);
      if (cmb == NULL) rb_raise(rb_eNoMemError, "gsl_matrix_complex_alloc failed");
      gsl_matrix_complex_set_all(cmb, *c);
      cmnew = matrix_to_complex(m);
      switch (flag) {
      case GSL_MATRIX_ADD:
      case GSL_MATRIX_ADD2:
	gsl_matrix_complex_add(cmnew, cmb);
	break;
      case GSL_MATRIX_SUB:
      case GSL_MATRIX_SUB2:
	gsl_matrix_complex_sub(cmnew,cmb);
	break;
      case GSL_MATRIX_MUL:
      case GSL_MATRIX_MUL2:
	gsl_matrix_complex_mul_elements(cmnew, cmb);
	break;
      case GSL_MATRIX_DIV:
      case GSL_MATRIX_DIV2:
	gsl_matrix_complex_div_elements(cmnew, cmb);
	break;
      default:
	rb_raise(rb_eRuntimeError, "operation not defined");
	break;
      }
      gsl_matrix_complex_free(cmb);
      return Data_Wrap_Struct(cgsl_matrix_complex, 0, gsl_matrix_complex_free, cmnew);
    } else if (rb_obj_is_kind_of(bb, cgsl_vector)) {
      if (!VECTOR_COL_P(bb))
	rb_raise(rb_eTypeError, 
		 "Operation with %s is not defined (GSL::Vector::Col expected)", 
		 rb_class2name(CLASS_OF(bb)));
      Data_Get_Struct(bb, gsl_vector, v);
      switch (flag) {
      case GSL_MATRIX_MUL:
      case GSL_MATRIX_MUL2:
	vnew = gsl_vector_alloc(v->size);
	if (vnew == NULL) rb_raise(rb_eNoMemError, "gsl_vector_alloc failed");
	gsl_matrix_mul_vector(vnew, m, v);
	return Data_Wrap_Struct(cgsl_vector_col, 0, gsl_vector_free, vnew);
	break;
      default:
	rb_raise(rb_eRuntimeError, "operation is not defined %s and Matrix",
		 rb_class2name(CLASS_OF(bb)));
	break;
      }
    } else if (rb_obj_is_kind_of(bb, cgsl_vector_complex)) {
      Data_Get_Struct(bb, gsl_vector_complex, cv);
      switch (flag) {
      case GSL_MATRIX_MUL:
      case GSL_MATRIX_MUL2:
	cm = matrix_to_complex(m);
	cvnew = gsl_vector_complex_alloc(cv->size);
	if (cvnew == NULL) rb_raise(rb_eNoMemError, "gsl_vector_complex_alloc failed");
	gsl_matrix_complex_mul_vector(cvnew, cm, cv);
	gsl_matrix_complex_free(cm);
	return Data_Wrap_Struct(cgsl_vector_complex, 0, gsl_vector_complex_free, cvnew);
	break;
      default:
	rb_raise(rb_eRuntimeError, "operation is not defined %s and Matrix",
		 rb_class2name(CLASS_OF(bb)));
	break;
      }
    } else {
      rb_raise(rb_eTypeError, "wrong argument type %s", rb_class2name(CLASS_OF(bb)));
    }
  }
  /* never reach here */
  return Qnil;
}

static VALUE rb_gsl_matrix_add(VALUE obj, VALUE bb)
{
  return rb_gsl_matrix_arithmetics(GSL_MATRIX_ADD, obj, bb);
}

static VALUE rb_gsl_matrix_add2(VALUE obj, VALUE bb)
{
  return rb_gsl_matrix_arithmetics(GSL_MATRIX_ADD2, obj, bb);
}

static VALUE rb_gsl_matrix_sub(VALUE obj, VALUE bb)
{
  return rb_gsl_matrix_arithmetics(GSL_MATRIX_SUB, obj, bb);
}

static VALUE rb_gsl_matrix_sub2(VALUE obj, VALUE bb)
{
  return rb_gsl_matrix_arithmetics(GSL_MATRIX_SUB2, obj, bb);
}

static VALUE rb_gsl_matrix_mul_elements(VALUE obj, VALUE bb)
{
  return rb_gsl_matrix_arithmetics(GSL_MATRIX_MUL, obj, bb);
}

static VALUE rb_gsl_matrix_mul_elements2(VALUE obj, VALUE bb)
{
  return rb_gsl_matrix_arithmetics(GSL_MATRIX_MUL2, obj, bb);
}

static VALUE rb_gsl_matrix_mul(VALUE obj, VALUE bb)
{
  gsl_matrix *m = NULL, *b = NULL, *mnew = NULL;
  gsl_vector *v = NULL, *vnew = NULL;
  Data_Get_Struct(obj, gsl_matrix, m);
  if (rb_obj_is_kind_of(bb, cgsl_matrix)) {
    Data_Get_Struct(bb, gsl_matrix, b);
    mnew = gsl_matrix_alloc(m->size1, b->size2);
    /*    gsl_matrix_mul(mnew, m, b);*/
    gsl_linalg_matmult(m, b, mnew);
    return Data_Wrap_Struct(cgsl_matrix, 0, gsl_matrix_free, mnew);
  } else if (rb_obj_is_kind_of(bb, cgsl_vector)) {
    Data_Get_Struct(bb, gsl_vector, v);
    vnew = gsl_vector_alloc(v->size);
    if (vnew == NULL) rb_raise(rb_eNoMemError, "gsl_vector_alloc failed");
    gsl_matrix_mul_vector(vnew, m, v);
    gsl_matrix_free(mnew);
    return Data_Wrap_Struct(cgsl_vector, 0, gsl_vector_free, vnew);
  } else {
    switch (TYPE(bb)) {
    case T_FLOAT:
    case T_BIGNUM:
    case T_FIXNUM:
      mnew = make_matrix_clone(m);
      gsl_matrix_scale(mnew, NUM2DBL(bb));
      return Data_Wrap_Struct(cgsl_matrix, 0, gsl_matrix_free, mnew);
      break;
    default:
      rb_raise(rb_eTypeError, 
	       "wrong argument type %s", rb_class2name(CLASS_OF(bb)));
      break;
    }
  }
}

static VALUE rb_gsl_matrix_mul2(VALUE obj, VALUE bb)
{
  gsl_matrix *m = NULL, *b = NULL, *mnew = NULL;
  gsl_vector *v = NULL, *vnew = NULL;
  Data_Get_Struct(obj, gsl_matrix, m);
  if (rb_obj_is_kind_of(bb, cgsl_matrix)) {
    Data_Get_Struct(bb, gsl_matrix, b);
    mnew = gsl_matrix_alloc(m->size1, b->size2);
    /*    gsl_matrix_mul(mnew, m, b);*/
    gsl_linalg_matmult(m, b, mnew);
    gsl_matrix_memcpy(m, mnew);
    gsl_matrix_free(mnew);
    return obj;
  } else if (rb_obj_is_kind_of(bb, cgsl_vector)) {
    Data_Get_Struct(bb, gsl_vector, v);
    vnew = gsl_vector_alloc(v->size);
    if (vnew == NULL) rb_raise(rb_eNoMemError, "gsl_vector_alloc failed");
    gsl_matrix_mul_vector(vnew, m, v);
    return Data_Wrap_Struct(cgsl_vector, 0, gsl_vector_free, vnew);
  } else {
    switch (TYPE(bb)) {
    case T_FLOAT:
    case T_BIGNUM:
    case T_FIXNUM:
      gsl_matrix_scale(m, NUM2DBL(bb));
      return obj;
      break;
    default:
      rb_raise(rb_eTypeError, 
	       "wrong argument type %s", rb_class2name(CLASS_OF(bb)));
      break;
    }
  }
}

static VALUE rb_gsl_matrix_div_elements(VALUE obj, VALUE bb)
{
  return rb_gsl_matrix_arithmetics(GSL_MATRIX_DIV, obj, bb);
}

static VALUE rb_gsl_matrix_div_elements2(VALUE obj, VALUE bb)
{
  return rb_gsl_matrix_arithmetics(GSL_MATRIX_DIV2, obj, bb);
}

static VALUE rb_gsl_matrix_scale(VALUE obj, VALUE x)
{
  return rb_gsl_matrix_arithmetics(GSL_MATRIX_MUL, obj, x);
}

static VALUE rb_gsl_matrix_scale2(VALUE obj, VALUE x)
{
  return rb_gsl_matrix_arithmetics(GSL_MATRIX_MUL2, obj, x);
}

static VALUE rb_gsl_matrix_add_constant(VALUE obj, VALUE x)
{
  return rb_gsl_matrix_arithmetics(GSL_MATRIX_ADD, obj, x);
}

static VALUE rb_gsl_matrix_add_constant2(VALUE obj, VALUE x)
{
  return rb_gsl_matrix_arithmetics(GSL_MATRIX_ADD2, obj, x);
}

static VALUE rb_gsl_matrix_max(VALUE obj)
{
  gsl_matrix *m = NULL;
  Data_Get_Struct(obj, gsl_matrix, m);
  return rb_float_new(gsl_matrix_max(m));
}

static VALUE rb_gsl_matrix_min(VALUE obj)
{
  gsl_matrix *m = NULL;
  Data_Get_Struct(obj, gsl_matrix, m);
  return rb_float_new(gsl_matrix_min(m));
}

static VALUE rb_gsl_matrix_minmax(VALUE obj)
{
  gsl_matrix *m = NULL;
  double min, max;
  Data_Get_Struct(obj, gsl_matrix, m);
  gsl_matrix_minmax(m, &min, &max);
  return rb_ary_new3(2, rb_float_new(min), rb_float_new(max));
}

static VALUE rb_gsl_matrix_max_index(VALUE obj)
{
  gsl_matrix *m = NULL;
  size_t imax, jmax;
  Data_Get_Struct(obj, gsl_matrix, m);
  gsl_matrix_max_index(m, &imax, &jmax);
  return rb_ary_new3(2, INT2FIX(imax), INT2FIX(jmax));
}

static VALUE rb_gsl_matrix_min_index(VALUE obj)
{
  gsl_matrix *m = NULL;
  size_t imin, jmin;
  Data_Get_Struct(obj, gsl_matrix, m);
  gsl_matrix_min_index(m, &imin, &jmin);
  return rb_ary_new3(2, INT2FIX(imin), INT2FIX(jmin));
}

static VALUE rb_gsl_matrix_minmax_index(VALUE obj)
{
  gsl_matrix *m = NULL;
  size_t imin, jmin, imax, jmax;
  Data_Get_Struct(obj, gsl_matrix, m);
  gsl_matrix_minmax_index(m, &imin, &jmin, &imax, &jmax);
  return rb_ary_new3(2, rb_ary_new3(2, INT2FIX(imin), INT2FIX(jmin)),
		     rb_ary_new3(2, INT2FIX(imax), INT2FIX(jmax)));
}

static VALUE rb_gsl_matrix_fwrite(VALUE obj, VALUE io)
{
  gsl_matrix *h = NULL;
  FILE *f = NULL;
  int status, flag = 0;
  Data_Get_Struct(obj, gsl_matrix, h);
  f = rb_gsl_open_writefile(io, &flag);
  status = gsl_matrix_fwrite(f, h);
  if (flag == 1) fclose(f);
  return INT2FIX(status);
}

static VALUE rb_gsl_matrix_fread(VALUE obj, VALUE io)
{
  gsl_matrix *h = NULL;
  FILE *f = NULL;
  int status, flag = 0;
  Data_Get_Struct(obj, gsl_matrix, h);
  f = rb_gsl_open_readfile(io, &flag);
  status = gsl_matrix_fread(f, h);
  if (flag == 1) fclose(f);
  return INT2FIX(status);
}

static VALUE rb_gsl_matrix_fprintf(int argc, VALUE *argv, VALUE obj)
{
  gsl_matrix *h = NULL;
  FILE *fp = NULL;
  int status, flag = 0;
  if (argc != 1 && argc != 2) {
    rb_raise(rb_eArgError, "wrong number of arguments (%d for 1 or 2)", argc);
  }
  Data_Get_Struct(obj, gsl_matrix, h);
  fp = rb_gsl_open_writefile(argv[0], &flag);
  if (argc == 2) {
    Check_Type(argv[1], T_STRING);
    status = gsl_matrix_fprintf(fp, h, STR2CSTR(argv[1]));
  } else {
    status = gsl_matrix_fprintf(fp, h, "%g");
  }
  if (flag == 1) fclose(fp);
  return INT2FIX(status);
}

static VALUE rb_gsl_matrix_printf(int argc, VALUE *argv, VALUE obj)
{
  gsl_matrix *h = NULL;
  int status;
  Data_Get_Struct(obj, gsl_matrix, h);
  if (argc == 1) {
    Check_Type(argv[0], T_STRING);
    status = gsl_matrix_fprintf(stdout, h, STR2CSTR(argv[0]));
  } else {
    status = gsl_matrix_fprintf(stdout, h, "%g");
  }
  return INT2FIX(status);
}

static VALUE rb_gsl_matrix_fscanf(VALUE obj, VALUE io)
{
  gsl_matrix *h = NULL;
  FILE *fp = NULL;
  int status, flag = 0;
  Data_Get_Struct(obj, gsl_matrix, h);
  fp = rb_gsl_open_readfile(io, &flag);
  status = gsl_matrix_fscanf(fp, h);
  if (flag == 1) fclose(fp);
  return INT2FIX(status);
}

static VALUE rb_gsl_matrix_to_complex(VALUE obj)
{
  gsl_matrix *m = NULL;
  gsl_matrix_complex *cm = NULL;
  Data_Get_Struct(obj, gsl_matrix, m);
  cm = matrix_to_complex(m);
  return Data_Wrap_Struct(cgsl_matrix_complex, 0, gsl_matrix_complex_free, cm);
}

gsl_matrix_view* gsl_matrix_view_alloc()
{
  gsl_matrix_view *mv = NULL;
  mv = ALLOC(gsl_matrix_view);
  if (mv == NULL) rb_raise(rb_eRuntimeError, "malloc failed");
  return mv;
}

void gsl_matrix_view_free(gsl_matrix_view * mv)
{
  free((gsl_matrix_view *) mv);
}

static VALUE rb_gsl_matrix_coerce(VALUE obj, VALUE other)
{
  gsl_matrix *m = NULL, *mnew = NULL;
  gsl_matrix_complex *cm = NULL;
  double x;
  gsl_complex *z = NULL;
  VALUE vcm;
  Data_Get_Struct(obj, gsl_matrix, m);
  switch (TYPE(other)) {
  case T_FLOAT:
  case T_FIXNUM:
  case T_BIGNUM:
    mnew = gsl_matrix_alloc(m->size1, m->size2);
    if (mnew == NULL) rb_raise(rb_eNoMemError, "gsl_matrix_alloc failed");
    x = NUM2DBL(other);
    gsl_matrix_set_all(mnew, x);
    return rb_ary_new3(2, Data_Wrap_Struct(cgsl_matrix, 0, gsl_matrix_free, mnew), obj);
    break;
  default:
    if (rb_obj_is_kind_of(other, cgsl_complex)) {
      Data_Get_Struct(other, gsl_complex, z);
      cm = gsl_matrix_complex_alloc(m->size1, m->size2);
      if (cm == NULL) rb_raise(rb_eNoMemError, "gsl_matrix_complex_alloc failed");
      gsl_matrix_complex_set_all(cm, *z);
      vcm =  Data_Wrap_Struct(cgsl_matrix_complex, 0, gsl_matrix_complex_free, cm);
      return rb_ary_new3(2, vcm, rb_gsl_matrix_to_complex(obj));
    } else if (rb_obj_is_kind_of(other, cgsl_matrix_complex)) {
      cm = matrix_to_complex(m);
      vcm = Data_Wrap_Struct(cgsl_matrix_complex, 0, gsl_matrix_complex_free, cm);
      return rb_ary_new3(2, other, vcm);
    } else {
      rb_raise(rb_eTypeError, "cannot coerce %s to Matrix", 
	       rb_class2name(CLASS_OF(other)));
    }
    break;
  }
}

static VALUE rb_gsl_matrix_trace(VALUE obj)
{
  gsl_matrix *m = NULL;
  int i;
  double trace = 0.0;
  Data_Get_Struct(obj, gsl_matrix, m);
  for (i = 0; i < m->size1; i++) trace += gsl_matrix_get(m, i, i);
  return rb_float_new(trace);
}

static VALUE rb_gsl_matrix_each_row(VALUE obj)
{
  gsl_matrix *m = NULL;
  gsl_vector_view vv, *pv = &vv;
  int i;
  Data_Get_Struct(obj, gsl_matrix, m);
  for (i = 0; i < m->size1; i++) {
    vv = gsl_matrix_row(m, i);
    rb_yield(Data_Wrap_Struct(cgsl_vector_view_ro, 0, NULL, pv));
  }
  return Qtrue;
}

static VALUE rb_gsl_matrix_each_col(VALUE obj)
{
  gsl_matrix *m = NULL;
  gsl_vector_view vv, *pv = &vv;
  int i;
  Data_Get_Struct(obj, gsl_matrix, m);
  for (i = 0; i < m->size2; i++) {
    vv = gsl_matrix_column(m, i);
    rb_yield(Data_Wrap_Struct(cgsl_vector_view_ro, 0, NULL, pv));
  }
  return Qtrue;
}

void Init_gsl_matrix()
{
  rb_define_singleton_method(cgsl_matrix, "new", rb_gsl_matrix_new, -1);
  rb_define_singleton_method(cgsl_matrix, "[]", rb_gsl_matrix_new, -1);
  rb_define_singleton_method(cgsl_matrix, "alloc", rb_gsl_matrix_new, -1);
  rb_define_singleton_method(cgsl_matrix, "calloc", rb_gsl_matrix_calloc, 2);
  rb_define_singleton_method(cgsl_matrix, "eye", rb_gsl_matrix_eye, -1);
  rb_define_singleton_method(cgsl_matrix, "scalar", rb_gsl_matrix_eye, -1);
  rb_define_singleton_method(cgsl_matrix, "diagonal", rb_gsl_matrix_eye, -1);
  rb_define_singleton_method(cgsl_matrix, "identity", rb_gsl_matrix_identity, 1);
  rb_define_singleton_method(cgsl_matrix, "unit", rb_gsl_matrix_identity, 1);
  rb_define_singleton_method(cgsl_matrix, "I", rb_gsl_matrix_identity, 1);

  rb_define_method(cgsl_matrix, "size1", rb_gsl_matrix_size1, 0);
  rb_define_method(cgsl_matrix, "size2", rb_gsl_matrix_size2, 0);
  rb_define_alias(cgsl_matrix, "size", "size1");
  rb_define_method(cgsl_matrix, "get", rb_gsl_matrix_get, 2);
  rb_define_method(cgsl_matrix, "set", rb_gsl_matrix_set, -1);
  rb_define_method(cgsl_matrix, "set_all", rb_gsl_matrix_set_all, 1);
  rb_define_method(cgsl_matrix, "set_zero", rb_gsl_matrix_set_zero, 0);
  rb_define_method(cgsl_matrix, "set_identity", rb_gsl_matrix_set_identity, 0);
  rb_define_method(cgsl_matrix, "print", rb_gsl_matrix_print, 0);
  rb_define_method(cgsl_matrix, "inspect", rb_gsl_matrix_print, 0);
  rb_define_method(cgsl_matrix, "submatrix", rb_gsl_matrix_submatrix, -1);
  rb_define_alias(cgsl_matrix, "view", "submatrix");
  rb_define_method(cgsl_matrix, "row", rb_gsl_matrix_row, 1);
  rb_define_method(cgsl_matrix, "get_row", rb_gsl_matrix_get_row, 1);
  /*  rb_define_alias(cgsl_matrix, "get_row", "row");*/
  rb_define_alias(cgsl_matrix, "[]", "row");
  rb_define_method(cgsl_matrix, "column", rb_gsl_matrix_column, 1);
  rb_define_alias(cgsl_matrix, "col", "column");
  rb_define_method(cgsl_matrix, "get_col", rb_gsl_matrix_get_col, 1);
  /*  rb_define_alias(cgsl_matrix, "get_column", "column");
      rb_define_alias(cgsl_matrix, "get_col", "column");*/
  rb_define_method(cgsl_matrix, "diagonal", rb_gsl_matrix_diagonal, 0);
  rb_define_method(cgsl_matrix, "set_diagonal", rb_gsl_matrix_set_diagonal, 1);
  rb_define_method(cgsl_matrix, "subdiagonal", rb_gsl_matrix_subdiagonal, 1);
  rb_define_method(cgsl_matrix, "superdiagonal", rb_gsl_matrix_superdiagonal, 1);

  rb_define_method(cgsl_matrix, "clone", rb_gsl_matrix_clone, 0);
  rb_define_method(cgsl_matrix, "isnull", rb_gsl_matrix_isnull, 0);
  rb_define_method(cgsl_matrix, "isnull?", rb_gsl_matrix_isnull2, 0);
  rb_define_method(cgsl_matrix, "set_col", rb_gsl_matrix_set_col, 2);
  rb_define_method(cgsl_matrix, "set_row", rb_gsl_matrix_set_row, 2);
  rb_define_method(cgsl_matrix, "swap_rows", rb_gsl_matrix_swap_rows, 2);
  rb_define_method(cgsl_matrix, "swap_columns", rb_gsl_matrix_swap_columns, 2);
  rb_define_alias(cgsl_matrix, "swap_cols", "swap_columns");
  rb_define_method(cgsl_matrix, "swap_rowcol", rb_gsl_matrix_swap_rowcol, 2);
  rb_define_method(cgsl_matrix, "transpose_memcpy", rb_gsl_matrix_transpose_memcpy, 0);
  rb_define_alias(cgsl_matrix, "transpose", "transpose_memcpy");
  rb_define_alias(cgsl_matrix, "trans", "transpose_memcpy");
  rb_define_method(cgsl_matrix, "transpose!", rb_gsl_matrix_transpose, 0);
  rb_define_alias(cgsl_matrix, "trans!", "transpose!");

  rb_define_method(cgsl_matrix, "add", rb_gsl_matrix_add, 1);
  rb_define_alias(cgsl_matrix, "+", "add");
  rb_define_method(cgsl_matrix, "sub", rb_gsl_matrix_sub, 1);
  rb_define_alias(cgsl_matrix, "-", "sub");
  rb_define_method(cgsl_matrix, "mul_elements", rb_gsl_matrix_mul_elements, 1);
  rb_define_method(cgsl_matrix, "div_elements", rb_gsl_matrix_div_elements, 1);
  rb_define_alias(cgsl_matrix, "*", "mul_elements");
  rb_define_alias(cgsl_matrix, "/", "div_elements");
  rb_define_method(cgsl_matrix, "scale", rb_gsl_matrix_scale, 1);
  rb_define_method(cgsl_matrix, "add_constant", rb_gsl_matrix_add_constant, 1);

  rb_define_method(cgsl_matrix, "add!", rb_gsl_matrix_add2, 1);
  rb_define_alias(cgsl_matrix, "+=", "add!");
  rb_define_method(cgsl_matrix, "sub!", rb_gsl_matrix_sub2, 1);
  rb_define_alias(cgsl_matrix, "-=", "sub!");
  rb_define_method(cgsl_matrix, "mul_elements!", rb_gsl_matrix_mul_elements2, 1);
  rb_define_alias(cgsl_matrix, "*=", "mul_elements!");
  rb_define_method(cgsl_matrix, "div_elements!", rb_gsl_matrix_div_elements2, 1);
  rb_define_alias(cgsl_matrix, "/=", "div_elements!");
  rb_define_method(cgsl_matrix, "scale!", rb_gsl_matrix_scale2, 1);
  rb_define_method(cgsl_matrix, "add_constant!", rb_gsl_matrix_add_constant2, 1);

  /***/
  rb_define_method(cgsl_matrix, "mul", rb_gsl_matrix_mul, 1);
  rb_define_alias(cgsl_matrix, "**", "mul");
  rb_define_method(cgsl_matrix, "mul!", rb_gsl_matrix_mul2, 1);
  /***/

  rb_define_method(cgsl_matrix, "max", rb_gsl_matrix_max, 0);
  rb_define_method(cgsl_matrix, "min", rb_gsl_matrix_min, 0);
  rb_define_method(cgsl_matrix, "minmax", rb_gsl_matrix_minmax, 0);
  rb_define_method(cgsl_matrix, "max_index", rb_gsl_matrix_max_index, 0);
  rb_define_method(cgsl_matrix, "min_index", rb_gsl_matrix_min_index, 0);
  rb_define_method(cgsl_matrix, "minmax_index", rb_gsl_matrix_minmax_index, 0);

  rb_define_method(cgsl_matrix, "fwrite", rb_gsl_matrix_fwrite, 1);
  rb_define_method(cgsl_matrix, "fread", rb_gsl_matrix_fread, 1);
  rb_define_method(cgsl_matrix, "fprintf", rb_gsl_matrix_fprintf, -1);
  rb_define_method(cgsl_matrix, "printf", rb_gsl_matrix_printf, -1);
  rb_define_method(cgsl_matrix, "fscanf", rb_gsl_matrix_fscanf, 1);

  rb_define_method(cgsl_matrix, "to_complex", rb_gsl_matrix_to_complex, 0);

  rb_define_method(cgsl_matrix, "coerce", rb_gsl_matrix_coerce, 1);

  rb_undef_method(cgsl_matrix_view_ro, "set");

  rb_define_singleton_method(cgsl_matrix, "memcpy", rb_gsl_matrix_memcpy, 2);
  rb_define_singleton_method(cgsl_matrix, "swap", rb_gsl_matrix_swap, 2);

  rb_define_method(cgsl_matrix, "trace", rb_gsl_matrix_trace, 0);

  rb_define_method(cgsl_matrix, "each_row", rb_gsl_matrix_each_row, 0);
  rb_define_method(cgsl_matrix, "each_col", rb_gsl_matrix_each_col, 0);
  rb_define_alias(cgsl_matrix, "each_column", "each_col");
}
