/*
  matrix_complex.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_config.h"
#include "rb_gsl_array.h"
#include "rb_gsl_complex.h"


enum {
  GSL_MATRIX_COMPLEX_ADD,
  GSL_MATRIX_COMPLEX_ADD2,
  GSL_MATRIX_COMPLEX_SUB,
  GSL_MATRIX_COMPLEX_SUB2,
  GSL_MATRIX_COMPLEX_MUL,
  GSL_MATRIX_COMPLEX_MUL2,
  GSL_MATRIX_COMPLEX_DIV,
  GSL_MATRIX_COMPLEX_DIV2,
};

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

static VALUE rb_gsl_matrix_complex_arithmetics(int flag, VALUE obj, VALUE bb)
{
  gsl_matrix *m = NULL;
  gsl_matrix_complex *cm = NULL, *cmb = NULL, *cmnew = NULL;
  gsl_complex *c = NULL, z;
  gsl_vector *v = NULL;
  gsl_vector_complex *cv = NULL, *cvnew = NULL, *cvb = NULL;
  Data_Get_Struct(obj, gsl_matrix_complex, cm);
  switch (TYPE(bb)) {
  case T_FLOAT:
  case T_FIXNUM:
  case T_BIGNUM:
    z = gsl_complex_rect(NUM2DBL(bb), 0.0);
    switch (flag) {
    case GSL_MATRIX_COMPLEX_ADD:
      cmnew = make_matrix_complex_clone(cm);
      gsl_matrix_complex_add_constant(cmnew, z);
      return Data_Wrap_Struct(cgsl_matrix_complex, 0, gsl_matrix_complex_free, cmnew);
      break;
    case GSL_MATRIX_COMPLEX_ADD2:
      gsl_matrix_complex_add_constant(cm, z);
      return obj;
      break;
    case GSL_MATRIX_COMPLEX_SUB:
      cmnew = make_matrix_complex_clone(cm);
      gsl_matrix_complex_add_constant(cmnew, gsl_complex_negative(z));
      return Data_Wrap_Struct(cgsl_matrix_complex, 0, gsl_matrix_complex_free, cmnew);
      break;
    case GSL_MATRIX_COMPLEX_SUB2:
      gsl_matrix_complex_add_constant(cm, gsl_complex_negative(z));
      return obj;
      break;
    case GSL_MATRIX_COMPLEX_MUL:
      cmnew = make_matrix_complex_clone(cm);
      gsl_matrix_complex_scale(cmnew, z);
      return Data_Wrap_Struct(cgsl_matrix_complex, 0, gsl_matrix_complex_free, cmnew);
      break;
    case GSL_MATRIX_COMPLEX_MUL2:
      gsl_matrix_complex_scale(cm, z);
      return obj;
      break;
    case GSL_MATRIX_COMPLEX_DIV:
      cmnew = make_matrix_complex_clone(cm);
      gsl_matrix_complex_scale(cmnew, gsl_complex_inverse(z));
      return Data_Wrap_Struct(cgsl_matrix_complex, 0, gsl_matrix_complex_free, cmnew);
      break;
    case GSL_MATRIX_COMPLEX_DIV2:
      gsl_matrix_complex_scale(cm, gsl_complex_inverse(z));
      return obj;
      break;
    default:
      rb_raise(rb_eRuntimeError, "operation not defined");
      break;
    }
    break;
  default:
    if (rb_obj_is_kind_of(bb, cgsl_matrix_complex)) {
      Data_Get_Struct(bb, gsl_matrix_complex, cmb);
      switch (flag) {
      case GSL_MATRIX_COMPLEX_ADD:
	cmnew = make_matrix_complex_clone(cm);
	gsl_matrix_complex_add(cmnew, cmb);
	return Data_Wrap_Struct(cgsl_matrix_complex, 0, gsl_matrix_complex_free, cmnew);
	break;
      case GSL_MATRIX_COMPLEX_ADD2:
	gsl_matrix_complex_add(cm, cmb);
	return obj;
	break;
      case GSL_MATRIX_COMPLEX_SUB:
	cmnew = make_matrix_complex_clone(cm);
	gsl_matrix_complex_sub(cmnew,cmb);
	return Data_Wrap_Struct(cgsl_matrix_complex, 0, gsl_matrix_complex_free, cmnew);
	break;
      case GSL_MATRIX_COMPLEX_SUB2:
	gsl_matrix_complex_sub(cm, cmb);
	return obj;
	break;
      case GSL_MATRIX_COMPLEX_MUL:
	cmnew = make_matrix_complex_clone(cm);
	gsl_matrix_complex_mul_elements(cmnew, cmb);
	return Data_Wrap_Struct(cgsl_matrix_complex, 0, gsl_matrix_complex_free, cmnew);
	break;
      case GSL_MATRIX_COMPLEX_MUL2:
	gsl_matrix_complex_mul_elements(cm, cmb);
	return obj;
	break;
      case GSL_MATRIX_COMPLEX_DIV:
	cmnew = make_matrix_complex_clone(cm);
	gsl_matrix_complex_div_elements(cmnew, cmb);
	return Data_Wrap_Struct(cgsl_matrix_complex, 0, gsl_matrix_complex_free, cmnew);
	break;
      case GSL_MATRIX_COMPLEX_DIV2:
	gsl_matrix_complex_div_elements(cm, cmb);
	return obj;
	break;
      default:
	rb_raise(rb_eRuntimeError, "operation not defined");
	break;
      }
    } else if (rb_obj_is_kind_of(bb, cgsl_matrix)) {
      Data_Get_Struct(bb, gsl_matrix, m);
      cmb = matrix_to_complex(m);
      cmnew = make_matrix_complex_clone(cm);
      switch (flag) {
      case GSL_MATRIX_COMPLEX_ADD:
      case GSL_MATRIX_COMPLEX_ADD2:
	gsl_matrix_complex_add(cmnew, cmb);
	break;
      case GSL_MATRIX_COMPLEX_SUB:
      case GSL_MATRIX_COMPLEX_SUB2:
	gsl_matrix_complex_sub(cmnew,cmb);
	break;
      case GSL_MATRIX_COMPLEX_MUL:
      case GSL_MATRIX_COMPLEX_MUL2:
	gsl_matrix_complex_mul_elements(cmnew, cmb);
	break;
      case GSL_MATRIX_COMPLEX_DIV:
      case GSL_MATRIX_COMPLEX_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);
      switch (flag) {
      case GSL_MATRIX_COMPLEX_ADD:
	cmnew = make_matrix_complex_clone(cm);
	gsl_matrix_complex_add_constant(cmnew, *c);
	break;
      case GSL_MATRIX_COMPLEX_ADD2:
	gsl_matrix_complex_add_constant(cm, *c);
	break;
      case GSL_MATRIX_COMPLEX_SUB:
	c->dat[0] *= -1.0;
	c->dat[1] *= -1.0;
	cmnew = make_matrix_complex_clone(cm);
	gsl_matrix_complex_add_constant(cmnew, *c);
	break;
      case GSL_MATRIX_COMPLEX_SUB2:
	c->dat[0] *= -1.0;
	c->dat[1] *= -1.0;
	gsl_matrix_complex_add_constant(cm, *c);
	break;
      case GSL_MATRIX_COMPLEX_MUL:
	cmnew = make_matrix_complex_clone(cm);
	gsl_matrix_complex_scale(cmnew, *c);
	break;
      case GSL_MATRIX_COMPLEX_MUL2:
	gsl_matrix_complex_scale(cm, *c);
	break;
      case GSL_MATRIX_COMPLEX_DIV:
	cmnew = make_matrix_complex_clone(cm);
	cmb = gsl_matrix_complex_alloc(cm->size1, cm->size2);
	if (cmb == NULL) rb_raise(rb_eNoMemError, "gsl_matrix_complex_alloc failed");
	gsl_matrix_complex_set_all(cmb, *c);
	gsl_matrix_complex_div_elements(cmnew, cmb);
	gsl_matrix_complex_free(cmb);
	break;
      case GSL_MATRIX_COMPLEX_DIV2:
	cmb = gsl_matrix_complex_alloc(cm->size1, cm->size2);
	if (cmb == NULL) rb_raise(rb_eNoMemError, "gsl_matrix_complex_alloc failed");
	gsl_matrix_complex_set_all(cmb, *c);
	gsl_matrix_complex_div_elements(cm, cmb);
	gsl_matrix_complex_free(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_vector)) {
      Data_Get_Struct(bb, gsl_vector, v);
      switch (flag) {
      case GSL_MATRIX_COMPLEX_MUL:
      case GSL_MATRIX_COMPLEX_MUL2:
	cvb = vector_to_complex(v);
	cvnew = gsl_vector_complex_alloc(v->size);
	if (cvnew == NULL) rb_raise(rb_eNoMemError, "gsl_vector_complex_alloc failed");
	gsl_matrix_complex_mul_vector(cvnew, cm, cvb);
	gsl_vector_complex_free(cvb);
	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_Complex",
		 rb_class2name(CLASS_OF(bb)));
	break;
      }
    } else if (rb_obj_is_kind_of(bb, cgsl_vector_complex)) {
      if (!VECTOR_COMPLEX_COL_P(bb))
	rb_raise(rb_eTypeError, 
		 "Operation is not defined with %s (Vector::Complex::Col expected)", 
		 rb_class2name(CLASS_OF(bb)));
      Data_Get_Struct(bb, gsl_vector_complex, cv);
      switch (flag) {
      case GSL_MATRIX_COMPLEX_MUL:
      case GSL_MATRIX_COMPLEX_MUL2:
	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);
	return Data_Wrap_Struct(cgsl_vector_complex_col, 0, gsl_vector_complex_free, cvnew);;
      default:
	rb_raise(rb_eRuntimeError, 
		 "operation is not defined %s and Matrix_Complex",
		 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_complex_new(VALUE klass, VALUE s1, VALUE s2)
{
  gsl_matrix_complex *m = NULL;
  CHECK_FIXNUM(s1); CHECK_FIXNUM(s2);
  m = gsl_matrix_complex_calloc(FIX2INT(s1),  FIX2INT(s2));
  if (m == NULL) rb_raise(rb_eNoMemError, "gsl_matrix_complex_alloc failed");
  return Data_Wrap_Struct(klass, 0, gsl_matrix_complex_free, m);
}

static VALUE rb_gsl_matrix_complex_eye(int argc, VALUE *argv, VALUE klass)
{
  size_t n, i;
  gsl_matrix_complex *m = NULL;
  gsl_complex z, *p = &z;
  switch (argc) {
  case 1:
    CHECK_FIXNUM(argv[0]);
    n = FIX2INT(argv[0]);
    z = gsl_complex_rect(1.0, 0.0);
    break;
  case 2:
    CHECK_FIXNUM(argv[0]);
    n = FIX2INT(argv[0]);
    switch (TYPE(argv[1])) {
    case T_FIXNUM:
    case T_BIGNUM:
    case T_FLOAT:
      z = gsl_complex_rect(NUM2DBL(argv[1]), 0.0);
      break;
    case T_ARRAY:
      if (RARRAY(argv[1])->len < 2) rb_raise(rb_eArgError, "wrong argument");
      z = gsl_complex_rect(NUM2DBL(rb_ary_entry(argv[1], 0)),
			   NUM2DBL(rb_ary_entry(argv[1], 1)));
      break;
    default:
      if (rb_obj_is_kind_of(argv[1], cgsl_complex)) {
	Data_Get_Struct(argv[1], gsl_complex, p);
      } else {
	rb_raise(rb_eTypeError, 
		 "wrong argument type %s", rb_class2name(CLASS_OF(argv[1])));
      }
      break;
    }
    break;
  case 3:
    CHECK_FIXNUM(argv[0]);
    n = FIX2INT(argv[0]);
    Need_Float(argv[1]); Need_Float(argv[2]);
    z = gsl_complex_rect(NUM2DBL(argv[1]), NUM2DBL(argv[2]));
    break;
  default:
    rb_raise(rb_eArgError, "wrong number of arguments (%d for 1, 2, or 3)", argc);
    break;
  }
  m = gsl_matrix_complex_calloc(n, n);
  if (m == NULL) rb_raise(rb_eNoMemError, "gsl_matrix_complex_alloc failed");
  for (i = 0; i < n; i++) gsl_matrix_complex_set(m, i, i, z);
  return  Data_Wrap_Struct(klass, 0, gsl_matrix_complex_free, m);
}

static VALUE rb_gsl_matrix_complex_identity(VALUE klass, VALUE nn)
{
  size_t n, i;
  gsl_matrix_complex *m = NULL;
  gsl_complex z;
  CHECK_FIXNUM(nn);
  n = FIX2INT(nn);
  m = gsl_matrix_complex_calloc(n, n);
  if (m == NULL) rb_raise(rb_eNoMemError, "gsl_matrix_complex_calloc failed");
  z = gsl_complex_rect(1.0, 0.0);
  for (i = 0; i < n; i++) gsl_matrix_complex_set(m, i, i, z);
  return  Data_Wrap_Struct(klass, 0, gsl_matrix_complex_free, m);
}

static VALUE rb_gsl_matrix_complex_set(int argc, VALUE *argv, VALUE obj)
{
  gsl_matrix_complex *m = NULL;
  gsl_complex *z = NULL, tmp;
  size_t i, j;
  if (argc < 3) rb_raise(rb_eArgError, "wrong number of arguments");
  CHECK_FIXNUM(argv[0]);
  CHECK_FIXNUM(argv[1]);
  Data_Get_Struct(obj, gsl_matrix_complex, m);
  i = FIX2INT(argv[0]);
  j = FIX2INT(argv[1]);
  switch (argc) {
  case 3:
    if (rb_obj_is_kind_of(argv[2], cgsl_complex)) {
      Data_Get_Struct(argv[2], gsl_complex, z);
    } else {
      switch(TYPE(argv[2])) {
      case T_ARRAY:
	tmp.dat[0] = NUM2DBL(rb_ary_entry(argv[2], 0));
	tmp.dat[1] = NUM2DBL(rb_ary_entry(argv[2], 1));
	z = &tmp;
	break;
      case T_FLOAT:
      case T_BIGNUM:
      case T_FIXNUM:
	tmp.dat[0] = NUM2DBL(argv[2]);
	tmp.dat[1] = 0.0;
	z = &tmp;
	break;
      default:
	rb_raise(rb_eTypeError, 
		 "wrong argument type %s", rb_class2name(CLASS_OF(argv[2])));
	break;
      }
      break;
    }
    break;
  case 4:
    tmp.dat[0] = NUM2DBL(argv[2]);
    tmp.dat[1] = NUM2DBL(argv[3]);
    z = &tmp;
    break;
  default:
    rb_raise(rb_eArgError, "wrong number of arguments");
    break;
  }
  gsl_matrix_complex_set(m, i, j, *z);
  return obj;
}

static VALUE rb_gsl_matrix_complex_set_row(int argc, VALUE *argv, VALUE obj)
{
  gsl_matrix_complex *A = NULL;
  size_t i, k;
  gsl_complex z, *pz = &z;
  if (argc < 2) rb_raise(rb_eArgError, "wrong number of arguments (%d for >= 2)",
			 argc);
  CHECK_FIXNUM(argv[0]);
  Data_Get_Struct(obj, gsl_matrix_complex, A);
  i = FIX2INT(argv[0]);
  for (k = 1; k < argc; k++) {
    if (k-1 >= A->size1) break;
    switch (TYPE(argv[k])) {
    case T_ARRAY:
      z.dat[0] = NUM2DBL(rb_ary_entry(argv[k], 0));
      z.dat[1] = NUM2DBL(rb_ary_entry(argv[k], 1));
      break;
    default:
      CHECK_COMPLEX(argv[k]);
      Data_Get_Struct(argv[k], gsl_complex, pz);
      break;
    }
    gsl_matrix_complex_set(A, i, k-1, z);
  }
  return obj;
}

static VALUE rb_gsl_matrix_complex_set_col(int argc, VALUE *argv, VALUE obj)
{
  gsl_matrix_complex *A = NULL;
  int j, k;
  gsl_complex z, *pz = &z;
  if (argc < 2) rb_raise(rb_eArgError, "wrong number of arguments (%d for >= 2)",
			 argc);
  CHECK_FIXNUM(argv[0]);
  Data_Get_Struct(obj, gsl_matrix_complex, A);
  j = FIX2INT(argv[0]);
  for (k = 1; k < argc; k++) {
    if (k-1 >= A->size2) break;
    switch (TYPE(argv[k])) {
    case T_ARRAY:
      z.dat[0] = NUM2DBL(rb_ary_entry(argv[k], 0));
      z.dat[1] = NUM2DBL(rb_ary_entry(argv[k], 1));
      break;
    default:
      CHECK_COMPLEX(argv[k]);
      Data_Get_Struct(argv[k], gsl_complex, pz);
      break;
    }
    gsl_matrix_complex_set(A, k-1, j, z);
  }
  return obj;
}

static VALUE rb_gsl_matrix_complex_get(VALUE obj, VALUE i, VALUE j)
{
  gsl_matrix_complex *m = NULL;
  gsl_complex *c = NULL;
  CHECK_FIXNUM(i); CHECK_FIXNUM(j);
  Data_Get_Struct(obj, gsl_matrix_complex, m);
  c = gsl_matrix_complex_ptr(m, FIX2INT(i), FIX2INT(j));
  return Data_Wrap_Struct(cgsl_complex, 0, NULL, c);
}

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

static VALUE rb_gsl_matrix_complex_print(VALUE obj)
{
  gsl_matrix_complex *h = NULL;
  gsl_complex *z = NULL;
  size_t i, j;
  Data_Get_Struct(obj, gsl_matrix_complex, h);
  printf("[ ");
  for (i = 0; i < h->size1; i++) {
    if (i != 0) printf("  ");
    for (j = 0; j < h->size2; j++) {
      z = gsl_matrix_complex_ptr(h, i, j);
      printf("[%4.3e %4.3e] ", GSL_REAL(*z), GSL_IMAG(*z));
    }
    if (i != h->size1 -1) printf("\n");
    else printf("]\n");
  }
  return obj;
}

static VALUE rb_gsl_matrix_complex_fwrite(VALUE obj, VALUE io)
{
  gsl_matrix_complex *h = NULL;
  FILE *fp = NULL;
  int status, flag = 0;
  Data_Get_Struct(obj, gsl_matrix_complex, h);
  fp = rb_gsl_open_writefile(io, &flag);
  status = gsl_matrix_complex_fwrite(fp, h);
  if (flag == 1) fclose(fp);
  return INT2FIX(status);
}

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

static VALUE rb_gsl_matrix_complex_fprintf(int argc, VALUE *argv, VALUE obj)
{
  gsl_matrix_complex *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_complex, h);
  fp = rb_gsl_open_writefile(argv[0], &flag);
  if (argc == 2) {
    Check_Type(argv[1], T_STRING);
    status = gsl_matrix_complex_fprintf(fp, h, STR2CSTR(argv[1]));
  } else {
    status = gsl_matrix_complex_fprintf(fp, h, "%g");
  }
  if (flag == 1) fclose(fp);
  return INT2FIX(status);
}

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

gsl_matrix_complex_view* gsl_matrix_complex_view_alloc()
{
  gsl_matrix_complex_view *vv = NULL;
  vv = ALLOC(gsl_matrix_complex_view);
  if (vv == NULL) rb_raise(rb_eRuntimeError, "malloc failed");
  return vv;
}

void gsl_matrix_complex_view_free(gsl_matrix_view * vv)
{
  free((gsl_matrix_complex_view *) vv);
}

/* singleton */
static VALUE rb_gsl_matrix_complex_memcpy(VALUE obj, VALUE dst, VALUE src)
{
  gsl_matrix_complex *m, *dest;
  CHECK_MATRIX_COMPLEX(dst);
  CHECK_MATRIX_COMPLEX(src);
  Data_Get_Struct(dst, gsl_matrix_complex, dest);
  Data_Get_Struct(src, gsl_matrix_complex, m);
  gsl_matrix_complex_memcpy(dest, m);
  return dst;
}

static VALUE rb_gsl_matrix_complex_clone(VALUE obj)
{
  gsl_matrix_complex *m, *mnew = NULL;
  Data_Get_Struct(obj, gsl_matrix_complex, m);
  mnew = gsl_matrix_complex_alloc(m->size1, m->size2);
  if (mnew == NULL) rb_raise(rb_eNoMemError, "gsl_matrix_complex_alloc failed");
  gsl_matrix_complex_memcpy(mnew, m);
  return Data_Wrap_Struct(cgsl_matrix_complex, 0, gsl_matrix_complex_free, mnew);
}

static VALUE rb_gsl_matrix_complex_swap_rows(VALUE obj, VALUE i, VALUE j)
{
  gsl_matrix_complex *m = NULL;
  CHECK_FIXNUM(i); CHECK_FIXNUM(j);
  Data_Get_Struct(obj, gsl_matrix_complex, m);
  gsl_matrix_complex_swap_rows(m, FIX2INT(i), FIX2INT(j));
  return obj;
}

static VALUE rb_gsl_matrix_complex_swap_columns(VALUE obj, VALUE i, VALUE j)
{
  gsl_matrix_complex *m = NULL;
  CHECK_FIXNUM(i); CHECK_FIXNUM(j);
  Data_Get_Struct(obj, gsl_matrix_complex, m);
  gsl_matrix_complex_swap_columns(m, FIX2INT(i), FIX2INT(j));
  return obj;
}

static VALUE rb_gsl_matrix_complex_swap_rowcol(VALUE obj, VALUE i, VALUE j)
{
  gsl_matrix_complex *m = NULL;
  CHECK_FIXNUM(i); CHECK_FIXNUM(j);
  Data_Get_Struct(obj, gsl_matrix_complex, m);
  gsl_matrix_complex_swap_rowcol(m, FIX2INT(i), FIX2INT(j));
  return obj;
}

static VALUE rb_gsl_matrix_complex_transpose(VALUE obj)
{
  gsl_matrix_complex *m = NULL;
  Data_Get_Struct(obj, gsl_matrix_complex, m);
  gsl_matrix_complex_transpose(m);
  return obj;
}

static VALUE rb_gsl_matrix_complex_isnull(VALUE obj)
{
  gsl_matrix_complex *m = NULL;
  Data_Get_Struct(obj, gsl_matrix_complex, m);
  if (gsl_matrix_complex_isnull(m)) return Qtrue;
  else return Qfalse;
}

static VALUE rb_gsl_matrix_complex_add(VALUE obj, VALUE mmb)
{
  return rb_gsl_matrix_complex_arithmetics(GSL_MATRIX_COMPLEX_ADD, obj, mmb);
}

static VALUE rb_gsl_matrix_complex_sub(VALUE obj, VALUE mmb)
{
  return rb_gsl_matrix_complex_arithmetics(GSL_MATRIX_COMPLEX_SUB, obj, mmb);
}

static VALUE rb_gsl_matrix_complex_mul_elements(VALUE obj, VALUE mmb)
{
  return rb_gsl_matrix_complex_arithmetics(GSL_MATRIX_COMPLEX_MUL, obj, mmb);
}

static VALUE rb_gsl_matrix_complex_div_elements(VALUE obj, VALUE mmb)
{
  return rb_gsl_matrix_complex_arithmetics(GSL_MATRIX_COMPLEX_DIV, obj, mmb);
}

static VALUE rb_gsl_matrix_complex_scale(VALUE obj, VALUE s)
{
  return rb_gsl_matrix_complex_arithmetics(GSL_MATRIX_COMPLEX_MUL, obj, s);
}

static VALUE rb_gsl_matrix_complex_scale_bang(VALUE obj, VALUE s)
{
  gsl_matrix_complex *m;
  gsl_complex c, *z = &c;
  Data_Get_Struct(obj, gsl_matrix_complex, m);
  switch (TYPE(s)) {
  case T_FIXNUM:
  case T_FLOAT:
    GSL_SET_REAL(z, NUM2DBL(s));
    GSL_SET_IMAG(z, 0.0);
    break;
  default:
    CHECK_COMPLEX(s);
    Data_Get_Struct(s, gsl_complex, z);
    break;
  }
  gsl_matrix_complex_scale(m, *z);
  return obj;
}

static VALUE rb_gsl_matrix_complex_mul(VALUE obj, VALUE mb)
{
  gsl_matrix_complex *cm = NULL, *cmb = NULL, *cmnew = NULL;
  gsl_matrix *m = NULL;
  int flag = 0;
  Data_Get_Struct(obj, gsl_matrix_complex, cm);
  if (COMPLEX_P(mb) || TYPE(mb) == T_FIXNUM || TYPE(mb) == T_FLOAT)
    return rb_gsl_matrix_complex_mul_elements(obj, mb);
  if (MATRIX_P(mb)) {
    Data_Get_Struct(mb, gsl_matrix, m);
    cmb = matrix_to_complex(m);
    flag = 1;
  } else {
    CHECK_MATRIX_COMPLEX(mb);
    Data_Get_Struct(mb, gsl_matrix_complex, cmb);
  }
  cmnew = gsl_matrix_complex_alloc(cm->size1, cm->size2);
  if (cmnew == NULL) rb_raise(rb_eNoMemError, "gsl_matrix_complex_alloc failed");
  gsl_matrix_complex_mul(cmnew, cm, cmb);
  if (flag == 1) gsl_matrix_complex_free(cmb);
  return Data_Wrap_Struct(cgsl_matrix_complex, 0, gsl_matrix_complex_free, cmnew);
}

static VALUE rb_gsl_matrix_complex_mul2(VALUE obj, VALUE mb)
{
  gsl_matrix_complex *cm = NULL, *cmb = NULL, *cmnew = NULL;
  gsl_matrix *m = NULL;
  int flag = 0;
  Data_Get_Struct(obj, gsl_matrix_complex, cm);
  if (MATRIX_P(mb)) {
    Data_Get_Struct(mb, gsl_matrix, m);
    cmb = matrix_to_complex(m);
    flag = 1;
  } else {
    CHECK_MATRIX_COMPLEX(mb);
    Data_Get_Struct(mb, gsl_matrix_complex, cmb);
  }
  cmnew = gsl_matrix_complex_alloc(cm->size1, cm->size2);
  if (cmnew == NULL) rb_raise(rb_eNoMemError, "gsl_matrix_complex_alloc failed");
  gsl_matrix_complex_mul(cmnew, cm, cmb);
  gsl_matrix_complex_memcpy(cm, cmnew);
  if (flag == 1) gsl_matrix_complex_free(cmb);
  return obj;
}

static VALUE rb_gsl_matrix_complex_add_diagonal(VALUE obj, VALUE s)
{
  gsl_matrix_complex *m = NULL;
  gsl_complex c, *z = NULL;
  Data_Get_Struct(obj, gsl_matrix_complex, m);
  switch (TYPE(s)) {
  case T_FLOAT:
  case T_BIGNUM:
  case T_FIXNUM:
    c.dat[0] = NUM2DBL(s);
    c.dat[1] = 0.0;
    gsl_matrix_complex_add_diagonal(m, c);
    break;
  case T_ARRAY:
    c.dat[0] = NUM2DBL(rb_ary_entry(s, 0));
    c.dat[1] = NUM2DBL(rb_ary_entry(s, 1));
    gsl_matrix_complex_add_diagonal(m, c);
    break;
  default:
    if (rb_obj_is_kind_of(s, cgsl_complex)) {
      Data_Get_Struct(s, gsl_complex, z);
      gsl_matrix_complex_add_diagonal(m, *z);
    } else {
      rb_raise(rb_eTypeError, 
	       "wrong argument type %s", rb_class2name(CLASS_OF(s)));
    }
    break;
  }
  return obj;
}

static VALUE rb_gsl_matrix_complex_set_zero(VALUE obj)
{
  gsl_matrix_complex *m = NULL;
  Data_Get_Struct(obj, gsl_matrix_complex, m);
  gsl_matrix_complex_set_zero(m);
  return obj;
}

static VALUE rb_gsl_matrix_complex_set_identity(VALUE obj)
{
  gsl_matrix_complex *m = NULL;
  Data_Get_Struct(obj, gsl_matrix_complex, m);
  gsl_matrix_complex_set_identity(m);
  return obj;
}

static VALUE rb_gsl_matrix_complex_set_all(VALUE obj, VALUE s)
{
  gsl_matrix_complex *m = NULL;
  gsl_complex c, *z = NULL;
  Data_Get_Struct(obj, gsl_matrix_complex, m);
  switch (TYPE(s)) {
  case T_FLOAT:
  case T_BIGNUM:
  case T_FIXNUM:
    c.dat[0] = NUM2DBL(s);
    c.dat[1] = 0.0;
    gsl_matrix_complex_set_all(m, c);
    break;
  case T_ARRAY:
    c.dat[0] = NUM2DBL(rb_ary_entry(s, 0));
    c.dat[1] = NUM2DBL(rb_ary_entry(s, 1));
    gsl_matrix_complex_set_all(m, c);
    break;
  default:
    if (rb_obj_is_kind_of(s, cgsl_complex)) {
      Data_Get_Struct(s, gsl_complex, z);
      gsl_matrix_complex_set_all(m, *z);
    } else {
      rb_raise(rb_eTypeError, 
	       "wrong argument type %s", rb_class2name(CLASS_OF(s)));
    }
    break;
  }
  return obj;
}

static VALUE rb_gsl_matrix_complex_submatrix(int argc, VALUE *argv, VALUE obj)
{
  gsl_matrix_complex *m = NULL;
  gsl_matrix_complex_view *mv = NULL;
  size_t i, j, n1, n2;
  Data_Get_Struct(obj, gsl_matrix_complex, 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[4]); 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_complex_view_alloc();
  *mv = gsl_matrix_complex_submatrix(m, i, j, n1, n2);
  return Data_Wrap_Struct(cgsl_matrix_complex_view, 0, gsl_matrix_complex_view_free, mv);
}

static VALUE rb_gsl_matrix_complex_row(VALUE obj, VALUE i)
{
  gsl_matrix_complex *m = NULL;
  gsl_vector_complex_view *vv = NULL;
  CHECK_FIXNUM(i);
  Data_Get_Struct(obj, gsl_matrix_complex, m);
  vv = gsl_vector_complex_view_alloc();
  *vv = gsl_matrix_complex_row(m, FIX2INT(i));
  return Data_Wrap_Struct(cgsl_vector_complex_view, 0, gsl_vector_complex_view_free, vv);
}

static VALUE rb_gsl_matrix_complex_column(VALUE obj, VALUE i)
{
  gsl_matrix_complex *m = NULL;
  gsl_vector_complex_view *vv = NULL;
  CHECK_FIXNUM(i);
  Data_Get_Struct(obj, gsl_matrix_complex, m);
  vv = gsl_vector_complex_view_alloc();
  *vv = gsl_matrix_complex_column(m, FIX2INT(i));
  return Data_Wrap_Struct(cgsl_vector_complex_col_view, 0, gsl_vector_complex_view_free, vv);
}

static VALUE rb_gsl_matrix_complex_diagonal(VALUE obj)
{
  gsl_matrix_complex *m = NULL;
  gsl_vector_complex_view *vv = NULL;
  Data_Get_Struct(obj, gsl_matrix_complex, m);
  vv = gsl_vector_complex_view_alloc();
  *vv = gsl_matrix_complex_diagonal(m);
  return Data_Wrap_Struct(cgsl_vector_complex_view, 0, gsl_vector_complex_view_free, vv);
}

static VALUE rb_gsl_matrix_complex_set_diagonal(VALUE obj, VALUE diag)
{
  gsl_matrix_complex *m = NULL;
  gsl_vector_complex *v;
  size_t i;
  Data_Get_Struct(obj, gsl_matrix_complex, m);
  if (VECTOR_COMPLEX_P(diag)) {
    Data_Get_Struct(diag, gsl_vector_complex, v);
    for (i = 0; i < m->size1; i++) gsl_matrix_complex_set(m, i, i, gsl_vector_complex_get(v, i));
  } else {
    rb_raise(rb_eTypeError, "wrong argument type %s (GSL::Vector_Complex or Array expected)",
	     rb_class2name(CLASS_OF(diag)));
  }
  return obj;
}

static VALUE rb_gsl_matrix_complex_subdiagonal(VALUE obj, VALUE i)
{
  gsl_matrix_complex *m = NULL;
  gsl_vector_complex_view *vv = NULL;
  CHECK_FIXNUM(i);
  Data_Get_Struct(obj, gsl_matrix_complex, m);
  vv = gsl_vector_complex_view_alloc();
  *vv = gsl_matrix_complex_subdiagonal(m, FIX2INT(i));
  return Data_Wrap_Struct(cgsl_vector_complex_view, 0, gsl_vector_complex_view_free, vv);
}

static VALUE rb_gsl_matrix_complex_superdiagonal(VALUE obj, VALUE i)
{
  gsl_matrix_complex *m = NULL;
  gsl_vector_complex_view *vv = NULL;
  CHECK_FIXNUM(i);
  Data_Get_Struct(obj, gsl_matrix_complex, m);
  vv = gsl_vector_complex_view_alloc();
  *vv = gsl_matrix_complex_superdiagonal(m, FIX2INT(i));
  return Data_Wrap_Struct(cgsl_vector_complex_view, 0, gsl_vector_complex_view_free, vv);
}

static VALUE rb_gsl_matrix_complex_coerce(VALUE obj, VALUE other)
{
  gsl_matrix_complex *cm = NULL, *cmnew = NULL;
  gsl_matrix *m = NULL;
  gsl_complex z;
  VALUE vcm;
  Data_Get_Struct(obj, gsl_matrix_complex, cm);
  switch (TYPE(other)) {
  case T_FLOAT:
  case T_FIXNUM:
  case T_BIGNUM:
    cmnew = gsl_matrix_complex_alloc(cm->size1, cm->size2);
    if (cmnew == NULL) rb_raise(rb_eNoMemError, "gsl_matrix_complex_alloc failed");
    GSL_SET_REAL(&z, NUM2DBL(other));
    GSL_SET_IMAG(&z, 0.0);
    gsl_matrix_complex_set_all(cmnew, z);
    vcm = Data_Wrap_Struct(cgsl_matrix_complex, 0, gsl_matrix_complex_free, cmnew);
    return rb_ary_new3(2, vcm, obj);
    break;
  default:
    if (rb_obj_is_kind_of(other, cgsl_matrix)) {
      Data_Get_Struct(other, gsl_matrix, m);
      cmnew = matrix_to_complex(m);
      vcm = Data_Wrap_Struct(cgsl_matrix_complex, 0, gsl_matrix_complex_free, cmnew);
      return rb_ary_new3(2, vcm, obj);
    } else {
      rb_raise(rb_eTypeError, "cannot coerce %s to GSL::Matrix::Complex",
	       rb_class2name(CLASS_OF(other)));
    }
    break;
  }
}

static VALUE rb_gsl_matrix_complex_to_real(VALUE obj)
{
  gsl_matrix_complex *cm = NULL;
  gsl_matrix *m = NULL;
  gsl_complex z;
  size_t i, j;
  Data_Get_Struct(obj, gsl_matrix_complex, cm);
  m = gsl_matrix_alloc(cm->size1, cm->size2);
  if (m == NULL) rb_raise(rb_eNoMemError, "gsl_matrix_alloc failed");
  for (i = 0; i < cm->size1; i++) {
    for (j = 0; j < cm->size2; j++) {
      z = gsl_matrix_complex_get(cm, i, j);
      gsl_matrix_set(m, i, j, GSL_REAL(z));
    }
  }
  return Data_Wrap_Struct(cgsl_matrix, 0, gsl_matrix_free, m);
}

static void gsl_matrix_complex_conjugate(gsl_matrix_complex *cm)
{
  gsl_complex z;
  size_t i, j;
  for (i = 0; i < cm->size1; i++) {
    for (j = 0; j < cm->size2; j++) {
      z = gsl_matrix_complex_get(cm, i, j);
      gsl_matrix_complex_set(cm, i, j, gsl_complex_conjugate(z));
    }
  }
}

static void gsl_matrix_complex_conjugate2(gsl_matrix_complex *cmnew, gsl_matrix_complex *cm)
{
  gsl_complex z;
  size_t i, j;
  for (i = 0; i < cm->size1; i++) {
    for (j = 0; j < cm->size2; j++) {
      z = gsl_matrix_complex_get(cm, i, j);
      gsl_matrix_complex_set(cmnew, i, j, gsl_complex_conjugate(z));
    }
  }
}

static VALUE rb_gsl_matrix_complex_conjugate(VALUE obj)
{
  gsl_matrix_complex *cm = NULL;
  Data_Get_Struct(obj, gsl_matrix_complex, cm);
  gsl_matrix_complex_conjugate(cm);
  return obj;
}

static VALUE rb_gsl_matrix_complex_conjugate2(VALUE obj)
{
  gsl_matrix_complex *cm = NULL, *cmnew = NULL;
  Data_Get_Struct(obj, gsl_matrix_complex, cm);
  cmnew = gsl_matrix_complex_alloc(cm->size1, cm->size2);
  if (cmnew == NULL) rb_raise(rb_eNoMemError, "gsl_matrix_complex_alloc failed");
  gsl_matrix_complex_conjugate2(cmnew, cm);
  return Data_Wrap_Struct(cgsl_matrix_complex, 0, gsl_matrix_complex_free, cmnew);
}

static VALUE rb_gsl_matrix_complex_dagger(VALUE obj)
{
  gsl_matrix_complex *cm = NULL;
  Data_Get_Struct(obj, gsl_matrix_complex, cm);
  gsl_matrix_complex_conjugate(cm);
  gsl_matrix_complex_transpose(cm);
  return obj;
}

static VALUE rb_gsl_matrix_complex_dagger2(VALUE obj)
{
  gsl_matrix_complex *cm = NULL, *cmnew = NULL;
  Data_Get_Struct(obj, gsl_matrix_complex, cm);
  cmnew = gsl_matrix_complex_alloc(cm->size1, cm->size2);
  if (cmnew == NULL) rb_raise(rb_eNoMemError, "gsl_matrix_complex_alloc failed");
  gsl_matrix_complex_conjugate2(cmnew, cm);
  gsl_matrix_complex_transpose(cmnew);
  return Data_Wrap_Struct(cgsl_matrix_complex, 0, gsl_matrix_complex_free, cmnew);
}

static VALUE rb_gsl_matrix_complex_trace(VALUE obj)
{
  gsl_matrix_complex *m = NULL;
  gsl_complex *trace = NULL;
  VALUE vtrace;
  size_t i;
  Data_Get_Struct(obj, gsl_matrix_complex, m);
  vtrace = Data_Make_Struct(cgsl_complex, gsl_complex, 0, free, trace);
  trace->dat[0] = 0.0; trace->dat[1] = 0.0;
  for (i = 0; i < m->size1; i++) *trace = gsl_complex_add(*trace, gsl_matrix_complex_get(m, i, i));
  return vtrace;
}

static VALUE rb_gsl_matrix_complex_each_row(VALUE obj)
{
  gsl_matrix_complex *m = NULL;
  gsl_vector_complex_view vv, *pv = &vv;
  size_t i;
  Data_Get_Struct(obj, gsl_matrix_complex, m);
  for (i = 0; i < m->size1; i++) {
    vv = gsl_matrix_complex_row(m, i);
    rb_yield(Data_Wrap_Struct(cgsl_vector_complex_view_ro, 0, NULL, pv));
  }
  return Qtrue;
}

static VALUE rb_gsl_matrix_complex_each_col(VALUE obj)
{
  gsl_matrix_complex *m = NULL;
  gsl_vector_complex_view vv, *pv = &vv;
  size_t i;
  Data_Get_Struct(obj, gsl_matrix_complex, m);
  for (i = 0; i < m->size2; i++) {
    vv = gsl_matrix_complex_column(m, i);
    rb_yield(Data_Wrap_Struct(cgsl_vector_complex_view_ro, 0, NULL, pv));
  }
  return Qtrue;
}

static VALUE rb_gsl_matrix_complex_size1(VALUE obj)
{
  gsl_matrix_complex *m = NULL;
  Data_Get_Struct(obj, gsl_matrix_complex, m);
  return INT2FIX(m->size1);
}
static VALUE rb_gsl_matrix_complex_size2(VALUE obj)
{
  gsl_matrix_complex *m = NULL;
  Data_Get_Struct(obj, gsl_matrix_complex, m);
  return INT2FIX(m->size2);
}

static VALUE rb_gsl_matrix_complex_uplus(VALUE obj)
{
  return obj;
}

static VALUE rb_gsl_matrix_complex_uminus(VALUE obj)
{
  gsl_matrix_complex *m = NULL, *mnew;
  size_t i, j;
  Data_Get_Struct(obj, gsl_matrix_complex, m);
  mnew = gsl_matrix_complex_alloc(m->size1, m->size2);
  for (i = 0; i < m->size1; i++) {
    for (j = 0; j < m->size2; j++) {
      gsl_matrix_complex_set(mnew, i, j, gsl_complex_negative(gsl_matrix_complex_get(m, i, j)));
    }
  }
  return Data_Wrap_Struct(cgsl_matrix_complex, 0, gsl_matrix_complex_free, mnew);
}

void Init_gsl_matrix_complex(VALUE module)
{
  rb_define_singleton_method(cgsl_matrix_complex, "new", rb_gsl_matrix_complex_new, 2);
  rb_define_singleton_method(cgsl_matrix_complex, "alloc", rb_gsl_matrix_complex_new, 2);
  rb_define_singleton_method(cgsl_matrix_complex, "calloc", rb_gsl_matrix_complex_new, 2);
  rb_define_singleton_method(cgsl_matrix_complex, "eye", rb_gsl_matrix_complex_eye, -1);
  rb_define_singleton_method(cgsl_matrix_complex, "diagonal", rb_gsl_matrix_complex_eye, -1);
  rb_define_singleton_method(cgsl_matrix_complex, "identity", rb_gsl_matrix_complex_identity, 1);
  rb_define_singleton_method(cgsl_matrix_complex, "unit", rb_gsl_matrix_complex_identity, 1);
  rb_define_singleton_method(cgsl_matrix_complex, "I", rb_gsl_matrix_complex_identity, 1);

  rb_define_method(cgsl_matrix_complex, "set", rb_gsl_matrix_complex_set, -1);

  rb_define_method(cgsl_matrix_complex, "set_row", rb_gsl_matrix_complex_set_row, -1);
  rb_define_method(cgsl_matrix_complex, "set_col", rb_gsl_matrix_complex_set_col, -1);

  rb_define_method(cgsl_matrix_complex, "get", rb_gsl_matrix_complex_get, 2);
  
  
  rb_define_method(cgsl_matrix_complex, "fprintf", rb_gsl_matrix_complex_fprintf, -1);
  rb_define_method(cgsl_matrix_complex, "printf", rb_gsl_matrix_complex_printf, -1);
  rb_define_method(cgsl_matrix_complex, "print", rb_gsl_matrix_complex_print, 0);
  rb_define_method(cgsl_matrix_complex, "inspect", rb_gsl_matrix_complex_print, 0);
  rb_define_method(cgsl_matrix_complex, "fwrite", rb_gsl_matrix_complex_fwrite, 1);
  rb_define_method(cgsl_matrix_complex, "fread", rb_gsl_matrix_complex_fread, 1);
  rb_define_method(cgsl_matrix_complex, "fscanf", rb_gsl_matrix_complex_fscanf, 1);
  
  rb_define_singleton_method(cgsl_matrix_complex, "memcpy", rb_gsl_matrix_complex_memcpy, 2);
  rb_define_method(cgsl_matrix_complex, "clone", rb_gsl_matrix_complex_clone, 0);
  rb_define_alias(cgsl_matrix_complex, "duplicate", "clone");
  rb_define_method(cgsl_matrix_complex, "swap_rows", rb_gsl_matrix_complex_swap_rows, 2);
  rb_define_method(cgsl_matrix_complex, "swap_columns", rb_gsl_matrix_complex_swap_columns, 2);
  rb_define_method(cgsl_matrix_complex, "swap_rowcol", rb_gsl_matrix_complex_swap_rowcol, 2);
  
  rb_define_method(cgsl_matrix_complex, "transpose", rb_gsl_matrix_complex_transpose, 0);
  rb_define_method(cgsl_matrix_complex, "isnull", rb_gsl_matrix_complex_isnull, 0);
  
  rb_define_method(cgsl_matrix_complex, "add", rb_gsl_matrix_complex_add, 1);
  rb_define_alias(cgsl_matrix_complex, "add_constant", "add");
  rb_define_alias(cgsl_matrix_complex, "+", "add");
  rb_define_method(cgsl_matrix_complex, "sub", rb_gsl_matrix_complex_sub, 1);
  rb_define_alias(cgsl_matrix_complex, "-", "sub");
  rb_define_method(cgsl_matrix_complex, "mul_elements", rb_gsl_matrix_complex_mul_elements, 1);
  /*  rb_define_alias(cgsl_matrix_complex, "*", "mul_elements");*/
  rb_define_method(cgsl_matrix_complex, "div_elements", rb_gsl_matrix_complex_div_elements, 1);
  rb_define_alias(cgsl_matrix_complex, "/", "div_elements");
  rb_define_method(cgsl_matrix_complex, "scale", rb_gsl_matrix_complex_scale, 1);
  rb_define_method(cgsl_matrix_complex, "scale!", rb_gsl_matrix_complex_scale_bang, 1);
  
  rb_define_method(cgsl_matrix_complex, "add_diagonal", rb_gsl_matrix_complex_add_diagonal, 1);
  
  rb_define_method(cgsl_matrix_complex, "set_zero", rb_gsl_matrix_complex_set_zero, 0);
  rb_define_method(cgsl_matrix_complex, "set_identity", rb_gsl_matrix_complex_set_identity, 0);
  rb_define_method(cgsl_matrix_complex, "set_all", rb_gsl_matrix_complex_set_all, 1);
  
  rb_define_method(cgsl_matrix_complex, "submatrix", rb_gsl_matrix_complex_submatrix, -1);
  rb_define_alias(cgsl_matrix_complex, "view", "submatrix");
  rb_define_method(cgsl_matrix_complex, "row", rb_gsl_matrix_complex_row, 1);
  rb_define_alias(cgsl_matrix_complex, "[]", "row");
  rb_define_method(cgsl_matrix_complex, "column", rb_gsl_matrix_complex_column, 1);
  rb_define_alias(cgsl_matrix_complex, "col", "column");
  rb_define_method(cgsl_matrix_complex, "diagonal", rb_gsl_matrix_complex_diagonal, 0);
  rb_define_method(cgsl_matrix_complex, "set_diagonal", rb_gsl_matrix_complex_set_diagonal, 1);
  rb_define_method(cgsl_matrix_complex, "subdiagonal", rb_gsl_matrix_complex_subdiagonal, 1);
  rb_define_method(cgsl_matrix_complex, "superdiagonal", rb_gsl_matrix_complex_superdiagonal, 1);
  
  rb_define_method(cgsl_matrix_complex, "coerce", rb_gsl_matrix_complex_coerce, 1);
  
  rb_define_method(cgsl_matrix_complex, "mul", rb_gsl_matrix_complex_mul, 1);
  rb_define_alias(cgsl_matrix_complex, "*", "mul");
  rb_define_method(cgsl_matrix_complex, "mul!", rb_gsl_matrix_complex_mul2, 1);
  
  rb_define_method(cgsl_matrix_complex, "to_real", rb_gsl_matrix_complex_to_real, 0);
  
  /* 25.June.2004 */
  rb_define_method(cgsl_matrix_complex, "conjugate!", rb_gsl_matrix_complex_conjugate, 0);
  rb_define_method(cgsl_matrix_complex, "conjugate", rb_gsl_matrix_complex_conjugate2, 0);
  rb_define_method(cgsl_matrix_complex, "dagger!", rb_gsl_matrix_complex_dagger, 0);
  rb_define_method(cgsl_matrix_complex, "dagger", rb_gsl_matrix_complex_dagger2, 0);
  
  rb_define_method(cgsl_matrix_complex, "trace", rb_gsl_matrix_complex_trace, 0);
  rb_define_method(cgsl_matrix_complex, "each_row", rb_gsl_matrix_complex_each_row, 0);
  rb_define_method(cgsl_matrix_complex, "each_col", rb_gsl_matrix_complex_each_col, 0);
  rb_define_alias(cgsl_matrix_complex, "each_column", "each_col");
  
  rb_define_method(cgsl_matrix_complex, "size1", rb_gsl_matrix_complex_size1, 0);
  rb_define_method(cgsl_matrix_complex, "size2", rb_gsl_matrix_complex_size2, 0);
  
  /*****/
  rb_define_method(cgsl_matrix_complex, "-@", rb_gsl_matrix_complex_uminus, 0);
  rb_define_method(cgsl_matrix_complex, "+@", rb_gsl_matrix_complex_uplus, 0);
}

