Module: MPFR::Vector

Included in:
ColumnVector, RowVector
Defined in:
lib/mpfr/rspec.rb,
lib/mpfr/matrix.rb,
ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.distance(a, b) ⇒ Object



139
140
141
# File 'lib/mpfr/matrix.rb', line 139

def self.distance(a, b)
  a.distance_from(b)
end

.inner_product(a, b) ⇒ Object



135
136
137
# File 'lib/mpfr/matrix.rb', line 135

def self.inner_product(a, b)
  a.inner_product(b)
end

Instance Method Details

#[]Object

Return element at p1.



903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 903

static VALUE r_mpfr_vector_element (VALUE self, VALUE arg)
{
  MPFRMatrix *ptr_self;
  int i;
  r_mpfr_get_matrix_struct(ptr_self, self);
  i = NUM2INT(arg);
  if (i < ptr_self->size) {
    VALUE ret;
    MPFR *ptr_ret;
    r_mpfr_make_struct_init(ret, ptr_ret);
    mpfr_set(ptr_ret, ptr_self->data + i, GMP_RNDN);
    return ret;
  }else{
    return Qnil;
  }
}

#[]=Object

Set p2 to p1 th element.



921
922
923
924
925
926
927
928
929
930
931
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 921

static VALUE r_mpfr_vector_set_element (VALUE self, VALUE arg, VALUE robj)
{
  MPFRMatrix *ptr_self;
  int i;
  r_mpfr_get_matrix_struct(ptr_self, self);
  i = NUM2INT(arg);
  if (i < ptr_self->size) {
    r_mpfr_set_robj(ptr_self->data + i, robj, GMP_RNDN);
  }
  return Qnil;
}

#absObject

Regard matrix as vector and return length of the vector.



891
892
893
894
895
896
897
898
899
900
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 891

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

#check_error(other, error = nil) ⇒ Object



28
29
30
31
32
33
# File 'lib/mpfr/rspec.rb', line 28

def check_error(other, error = nil)
  err = error || @error
  size.times.all? do |i|
    self[i].check_error(other[i], err)
  end
end

#dimObject

Return size of data array which equals to column * row.



883
884
885
886
887
888
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 883

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

#distance_fromObject

Return distance from p1, i.e. sqrt((x1 - x2)^2 + (y1 - y2)^2).



968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 968

static VALUE r_mpfr_vector_distance_from (VALUE self, VALUE arg)
{
  MPFRMatrix *ptr_self, *ptr_arg;
  VALUE ret;
  MPFR *ptr_ret;
  r_mpfr_get_matrix_struct(ptr_self, self);
  r_mpfr_get_matrix_struct(ptr_arg, arg);
  r_mpfr_make_struct_init(ret, ptr_ret);
  if(ptr_self->size == ptr_arg->size){
    mpfr_matrix_vector_distance(ptr_ret, ptr_self, ptr_arg);
  }else{
    rb_raise(rb_eArgError, "Vectors must have same size.");
  }
  return ret;
}

#dividing_pointObject

Return dividing point of self and p1 by p2.



997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 997

static VALUE r_mpfr_vector_dividing_point (VALUE self, VALUE arg, VALUE div)
{
  MPFRMatrix *ptr_self, *ptr_arg, *ptr_ret;
  MPFR *ptr_div;
  VALUE ret;
  r_mpfr_get_matrix_struct(ptr_self, self);
  r_mpfr_get_matrix_struct(ptr_arg, arg);
  r_mpfr_get_struct(ptr_div, div);
  r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
  mpfr_vector_dividing_point(ptr_ret, ptr_self, ptr_arg, ptr_div);
  return ret;
}

#each_elementObject Also known as: each

Evaluate block with each element of vector.



934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 934

static VALUE r_mpfr_vector_each_element (VALUE self)
{
  MPFRMatrix *ptr_self;
  VALUE ret, tmp;
  MPFR *tmp_ptr;
  int i;
  r_mpfr_get_matrix_struct(ptr_self, self);
  ret = Qnil;
  r_mpfr_make_struct_init(tmp, tmp_ptr);
  for (i = 0; i < ptr_self->size; i++) {
    mpfr_set(tmp_ptr, ptr_self->data + i, GMP_RNDN);
    ret = rb_yield(tmp);
  }
  return ret;
}

#each_element_with_indexObject Also known as: each_with_index

Evaluate block with each element and its index.



951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 951

static VALUE r_mpfr_vector_each_element_with_index (VALUE self)
{
  MPFRMatrix *ptr_self;
  VALUE ret, tmp;
  MPFR *tmp_ptr;
  int i;
  r_mpfr_get_matrix_struct(ptr_self, self);
  ret = Qnil;
  r_mpfr_make_struct_init(tmp, tmp_ptr);
  for (i = 0; i < ptr_self->size; i++) {
    mpfr_set(tmp_ptr, ptr_self->data + i, GMP_RNDN);
    ret = rb_yield(rb_ary_new3(2, tmp, INT2NUM(i)));
  }
  return ret;
}

#inner_productObject

Regard matrix as vector and return the value of inner product.



684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 684

static VALUE r_mpfr_vector_inner_product (VALUE self, VALUE arg)
{
  MPFRMatrix *ptr_self, *ptr_arg;
  VALUE ret;
  MPFR *ptr_ret;
  r_mpfr_get_matrix_struct(ptr_self, self);
  r_mpfr_get_matrix_struct(ptr_arg, arg);
  r_mpfr_make_struct_init(ret, ptr_ret);
  if(ptr_self->size == ptr_arg->size){
    mpfr_matrix_inner_product(ptr_ret, ptr_self, ptr_arg);
  }else{
    rb_raise(rb_eArgError, "Vectors must have same size.");
  }
  return ret;
}

#inspectObject



111
112
113
# File 'lib/mpfr/matrix.rb', line 111

def inspect
  sprintf("#<%s:%x, ['%s']>", self.class, __id__, str_ary("%.Re").join("', '"))
end

#midpointObject

Return midpoint between self and p1.



985
986
987
988
989
990
991
992
993
994
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 985

static VALUE r_mpfr_vector_midpoint (VALUE self, VALUE arg)
{
  MPFRMatrix *ptr_self, *ptr_arg, *ptr_ret;
  VALUE ret;
  r_mpfr_get_matrix_struct(ptr_self, self);
  r_mpfr_get_matrix_struct(ptr_arg, arg);
  r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
  mpfr_vector_midpoint(ptr_ret, ptr_self, ptr_arg);
  return ret;
}

#normalizeObject

Return normalized vector of self.



1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 1011

static VALUE r_mpfr_vector_normalize (VALUE self)
{
  MPFRMatrix *ptr_self, *ptr_ret;
  VALUE ret;
  r_mpfr_get_matrix_struct(ptr_self, self);
  r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
  if(mpfr_vector_normalize(ptr_ret, ptr_self) == 0){
    return ret;
  }else{
    return Qnil;
  }
}

#normalize!Object

Return normalized vector of self. This method is destructive method.



1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 1025

static VALUE r_mpfr_vector_normalize2 (VALUE self)
{
  MPFRMatrix *ptr_self, tmp;
  VALUE ret;
  r_mpfr_get_matrix_struct(ptr_self, self);
  ret = self;
  mpfr_matrix_init(&tmp, ptr_self->column, ptr_self->row);
  if(mpfr_vector_normalize(&tmp, ptr_self) == 0){
    mpfr_matrix_set(ptr_self, &tmp);
  }else{
    ret = Qnil;
  }
  mpfr_matrix_clear(&tmp);
  return ret;
}

#pretty_print(pp) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/mpfr/matrix.rb', line 115

def pretty_print(pp)
  ary = str_ary("%.Re")
  pp.object_group(self) do
    pp.text(sprintf(':%x, ', __id__))
    pp.breakable
    pp.text(%|["#{ary[0]}"|)
    pp.nest(1) do
      for i in 1...dim
        pp.comma_breakable
        pp.text(%|"#{ary[i]}"|)
      end
    end
    pp.text("]")
  end
end

#set_lengthObject

Return new vector whose length is p1.



1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 1042

static VALUE r_mpfr_vector_set_length (VALUE self, VALUE arg)
{
  MPFRMatrix *ptr_self, *ptr_ret;
  VALUE ret, returned_value;
  MPFR *ptr_l;
  r_mpfr_get_matrix_struct(ptr_self, self);
  returned_value = Qnil;
  r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
  if(RTEST(rb_funcall(r_mpfr_class, eqq, 1, arg))){
    r_mpfr_get_struct(ptr_l, arg);
    if(mpfr_vector_set_length(ptr_ret, ptr_self, ptr_l) == 0){
      returned_value = ret;
    }
  }else{
    r_mpfr_temp_alloc_init(ptr_l);
    r_mpfr_set_robj(ptr_l, arg, GMP_RNDN);
    if(mpfr_vector_set_length(ptr_ret, ptr_self, ptr_l) == 0){
      returned_value = ret;
    }
    r_mpfr_temp_free(ptr_l);
  }
  return returned_value;
}

#set_length!Object

Return new vector whose length is p1. This method is destructive.



1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 1067

static VALUE r_mpfr_vector_set_length2 (VALUE self, VALUE arg)
{
  MPFRMatrix *ptr_self, *ptr_ret;
  VALUE ret, returned_value;
  MPFR *ptr_l;
  r_mpfr_get_matrix_struct(ptr_self, self);
  returned_value = Qnil;
  r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
  if(RTEST(rb_funcall(r_mpfr_class, eqq, 1, arg))){
    r_mpfr_get_struct(ptr_l, arg);
    if(mpfr_vector_set_length(ptr_ret, ptr_self, ptr_l) == 0){
      mpfr_matrix_set(ptr_self, ptr_ret);
      returned_value = self;
    }
  }else{
    r_mpfr_temp_alloc_init(ptr_l);
    r_mpfr_set_robj(ptr_l, arg, GMP_RNDN);
    if(mpfr_vector_set_length(ptr_ret, ptr_self, ptr_l) == 0){
      mpfr_matrix_set(ptr_self, ptr_ret);
      returned_value = self;
    }
    r_mpfr_temp_free(ptr_l);
  }
  return returned_value;
}

#to_s(format = "%.Re", delimiter = ' ') ⇒ Object



131
132
133
# File 'lib/mpfr/matrix.rb', line 131

def to_s(format = "%.Re", delimiter = ' ')
  str_ary(format).join(delimiter)
end