Class: MPFR::SquareMatrix

Inherits:
Matrix
  • Object
show all
Defined in:
ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Matrix

#==, #add, #add2, #at, #check_error, #column, #column_size, create, #div_scalar, #each_element, #each_element_with_index, #element, #inspect, #marshal_dump, #marshal_load, #max_norm, #mul_matrix, #mul_matrix2, #mul_scalar, #neg, #pretty_print, #row, #row_size, #set_element, #size, #str_ary, #str_ary2, #str_ary2_2, #str_ary3, #sub, #sub2, suitable_matrix_from_ary, #to_a, #to_a2, #to_s, #transpose, #transpose!, #vector_norm

Class Method Details

.identityObject

Return identity matrix.



872
873
874
875
876
877
878
879
880
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 872

static VALUE r_mpfr_square_matrix_identity (VALUE self, VALUE size)
{
  VALUE ret;
  MPFRMatrix *ptr_ret;
  int s = NUM2INT(size);
  r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, s, s);
  mpfr_square_matrix_identity(ptr_ret);
  return ret;
}

Instance Method Details

#determinantObject

Return determinant of matrix.



848
849
850
851
852
853
854
855
856
857
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 848

static VALUE r_mpfr_square_matrix_determinant (VALUE self)
{
  MPFRMatrix *ptr_self;
  MPFR *ptr_ret;
  VALUE ret;
  r_mpfr_get_matrix_struct(ptr_self, self);
  r_mpfr_make_struct_init(ret, ptr_ret);
  mpfr_square_matrix_determinant(ptr_ret, ptr_self);
  return ret;
}

#dimObject

Return row size of matrix.



803
804
805
806
807
808
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 803

static VALUE r_mpfr_square_matrix_dim (VALUE self)
{
  MPFRMatrix *ptr_self;
  r_mpfr_get_matrix_struct(ptr_self, self);
  return INT2FIX(ptr_self->row);
}

#lu_decompObject

Perform LU decomposition and return [matrix_l, matrix_r, indx].



811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 811

static VALUE r_mpfr_square_matrix_lu_decomp (VALUE self)
{
  MPFRMatrix *ptr_self, *ptr_ret_l, *ptr_ret_u;
  VALUE ret_l, ret_u, ret, ret_indx_ary;
  int i, j, *indx;
  r_mpfr_get_matrix_struct(ptr_self, self);
  r_mpfr_matrix_suitable_matrix_init (&ret_l, &ptr_ret_l, ptr_self->row, ptr_self->column);
  r_mpfr_matrix_suitable_matrix_init (&ret_u, &ptr_ret_u, ptr_self->row, ptr_self->column);
  indx = ALLOC_N(int, ptr_self->row);
  if(mpfr_square_matrix_lu_decomp (ptr_ret_u, indx, ptr_self) >= 0){
    ret_indx_ary = rb_ary_new2(ptr_self->row);
    for(i = 1; i < ptr_ret_u->row; i++){
      for(j = 0; j < i; j++){
	mpfr_set(mpfr_matrix_get_element(ptr_ret_l, i, j), mpfr_matrix_get_element(ptr_ret_u, i, j), GMP_RNDN);
	mpfr_set_si(mpfr_matrix_get_element(ptr_ret_u, i, j), 0, GMP_RNDN);
      }
    }
    for(i = 0; i < ptr_ret_u->row; i++){
      mpfr_set_si(mpfr_matrix_get_element(ptr_ret_l, i, i), 1, GMP_RNDN);
    }
    for(i = 0; i < ptr_ret_u->row; i++){
      for(j = i + 1; j < ptr_ret_u->column; j++){
	mpfr_set_si(mpfr_matrix_get_element(ptr_ret_l, i, j), 0, GMP_RNDN);
      }
    }
    for(i = 0; i < ptr_ret_u->row; i++){
      rb_ary_store(ret_indx_ary, i, INT2NUM(indx[i]));
    }
    ret = rb_ary_new3(3, ret_l, ret_u, ret_indx_ary);
  } else {
    ret = Qnil;
  }
  free(indx);
  return ret;
}

#qr_decompObject

Perform QR decomposition and return [matrix_q, matrix_r].



860
861
862
863
864
865
866
867
868
869
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 860

static VALUE r_mpfr_square_matrix_qr_decomp (VALUE self)
{
  MPFRMatrix *ptr_self, *ptr_q, *ptr_r;
  VALUE q, r;
  r_mpfr_get_matrix_struct(ptr_self, self);
  r_mpfr_matrix_suitable_matrix_init (&q, &ptr_q, ptr_self->row, ptr_self->column);
  r_mpfr_matrix_suitable_matrix_init (&r, &ptr_r, ptr_self->column, ptr_self->column);
  mpfr_square_matrix_qr_decomp(ptr_q, ptr_r, ptr_self);
  return rb_ary_new3(2, q, r);
}