Module: MPFI::Vector

Included in:
ColumnVector, RowVector
Defined in:
lib/mpfi/matrix.rb,
ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.distance(a, b) ⇒ Object



252
253
254
# File 'lib/mpfi/matrix.rb', line 252

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

.inner_product(a, b) ⇒ Object



248
249
250
# File 'lib/mpfi/matrix.rb', line 248

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

Instance Method Details

#[]Object

Return element at arg.



1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 1029

static VALUE r_mpfi_vector_element (VALUE self, VALUE arg) {
  MPFIMatrix *ptr_self;
  int i;
  r_mpfi_get_matrix_struct(ptr_self, self);
  i = NUM2INT(arg);
  if (i < ptr_self->size) {
    VALUE ret;
    MPFI *ptr_ret;
    r_mpfi_make_struct_init(ret, ptr_ret);
    mpfi_set(ptr_ret, ptr_self->data + i);
    return ret;
  } else {
    return Qnil;
  }
}

#[]=Object

Set robj to arg th element.



1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 1046

static VALUE r_mpfi_vector_set_element (VALUE self, VALUE arg, VALUE robj) {
  MPFIMatrix *ptr_self;
  int i;
  r_mpfi_get_matrix_struct(ptr_self, self);
  i = NUM2INT(arg);
  if (i < ptr_self->size) {
    r_mpfi_set_robj(ptr_self->data + i, robj);
  }
  return Qnil;
}

#absObject

Take matrix for vector and return length of the vector.



1018
1019
1020
1021
1022
1023
1024
1025
1026
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 1018

static VALUE r_mpfi_vector_abs (VALUE self) {
  MPFIMatrix *ptr_self;
  VALUE ret;
  MPFI *ptr_ret;
  r_mpfi_get_matrix_struct(ptr_self, self);
  r_mpfi_make_struct_init(ret, ptr_ret);
  mpfi_matrix_vector_norm(ptr_ret, ptr_self);
  return ret;
}

#dimObject

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



1011
1012
1013
1014
1015
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 1011

static VALUE r_mpfi_vector_dim (VALUE self) {
  MPFIMatrix *ptr_self;
  r_mpfi_get_matrix_struct(ptr_self, self);
  return INT2FIX(ptr_self->size);
}

#distance_center_ptsObject



1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 1117

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

#distance_fromObject



1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 1102

static VALUE r_mpfi_vector_distance_from (VALUE self, VALUE arg) {
  MPFIMatrix *ptr_self, *ptr_arg;
  VALUE ret;
  MPFI *ptr_ret;
  r_mpfi_get_matrix_struct(ptr_self, self);
  r_mpfi_get_matrix_struct(ptr_arg, arg);
  r_mpfi_make_struct_init(ret, ptr_ret);
  if (ptr_self->size == ptr_arg->size) {
    mpfi_matrix_vector_distance(ptr_ret, ptr_self, ptr_arg);
  } else {
    rb_raise(rb_eArgError, "Vectors must have same size.");
  }
  return ret;
}

#each_elementObject Also known as: each

Evaluate block with each element of vector.



1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 1058

static VALUE r_mpfi_vector_each_element (VALUE self) {
  MPFIMatrix *ptr_self;
  VALUE ret;
  int i;
  RETURN_ENUMERATOR(self, 0, NULL);
  ret = Qnil;
  r_mpfi_get_matrix_struct(ptr_self, self);
  for (i = 0; i < ptr_self->size; i++) {
    volatile VALUE el = r_mpfi_make_new_fi_obj(ptr_self->data + i);
    ret = rb_yield(el);
  }
  return ret;
}

#each_element_with_indexObject Also known as: each_with_index

Evaluate block with each element and its index.



1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 1073

static VALUE r_mpfi_vector_each_element_with_index (VALUE self) {
  MPFIMatrix *ptr_self;
  VALUE ret;
  int i;
  RETURN_ENUMERATOR(self, 0, NULL);
  r_mpfi_get_matrix_struct(ptr_self, self);
  ret = Qnil;
  for (i = 0; i < ptr_self->size; i++) {
    volatile VALUE el = r_mpfi_make_new_fi_obj(ptr_self->data + i);
    ret = rb_yield(rb_ary_new3(2, el, INT2NUM(i)));
  }
  return ret;
}

#each_subdivision(*nums) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/mpfi/matrix.rb', line 200

def each_subdivision(*nums)
  if block_given?
    dim = self.size
    unless dim == nums.size
      raise ArgumentError, "Invalid numbers to specify split"
    end
    num_current = 0
    elements = nums.each_with_index.map { |num, i| self[i].subdivision(num) }
    indices = Array.new(dim, 0)
    args = []
    while num_current >= 0
      while num_current < dim
        if args[num_current] = elements[num_current][indices[num_current]]
          indices[num_current] += 1
        else
          indices[num_current] = 0
          num_current -= 1
          break
        end
        num_current += 1
      end
      if num_current == dim
        yield(self.class.new(args))
        num_current -= 1
      end
    end
  else
    to_enum(:each_subdivision, *nums)
  end
end

#each_subdivision_by_size(size, &block) ⇒ Object



231
232
233
234
235
236
237
238
# File 'lib/mpfi/matrix.rb', line 231

def each_subdivision_by_size(size, &block)
  if block_given?
    nums = self.size.times.map { |i| (self[i].diam_abs / size).ceil }
    each_subdivision(*nums, &block)
  else
    to_enum(:each_subdivision_by_size, size)
  end
end

#inner_productObject



1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 1087

static VALUE r_mpfi_vector_inner_product (VALUE self, VALUE arg) {
  MPFIMatrix *ptr_self, *ptr_arg;
  VALUE ret;
  MPFI *ptr_ret;
  r_mpfi_get_matrix_struct(ptr_self, self);
  r_mpfi_get_matrix_struct(ptr_arg, arg);
  r_mpfi_make_struct_init(ret, ptr_ret);
  if (ptr_self->size == ptr_arg->size) {
    mpfi_matrix_inner_product(ptr_ret, ptr_self, ptr_arg);
  } else {
    rb_raise(rb_eArgError, "Vectors must have same size.");
  }
  return ret;
}

#inspectObject



176
177
178
# File 'lib/mpfi/matrix.rb', line 176

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

#midpointObject



1132
1133
1134
1135
1136
1137
1138
1139
1140
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 1132

static VALUE r_mpfi_vector_midpoint (VALUE self, VALUE arg) {
  MPFIMatrix *ptr_self, *ptr_arg, *ptr_ret;
  VALUE ret;
  r_mpfi_get_matrix_struct(ptr_self, self);
  r_mpfi_get_matrix_struct(ptr_arg, arg);
  r_mpfi_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
  mpfi_vector_midpoint(ptr_ret, ptr_self, ptr_arg);
  return ret;
}

#normalizeObject



1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 1142

static VALUE r_mpfi_vector_normalize (VALUE self) {
  MPFIMatrix *ptr_self, *ptr_ret;
  VALUE ret;
  r_mpfi_get_matrix_struct(ptr_self, self);
  r_mpfi_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
  if (mpfi_vector_normalize(ptr_ret, ptr_self) == 0) {
    return ret;
  } else {
    return Qnil;
  }
}

#normalize!Object

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



1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 1155

static VALUE r_mpfi_vector_normalize2 (VALUE self) {
  MPFIMatrix *ptr_self, tmp;
  VALUE ret;
  r_mpfi_get_matrix_struct(ptr_self, self);
  ret = self;
  mpfi_matrix_init(&tmp, ptr_self->column, ptr_self->row);
  if (mpfi_vector_normalize(&tmp, ptr_self) == 0) {
    mpfi_matrix_set(ptr_self, &tmp);
  } else {
    ret = Qnil;
  }
  mpfi_matrix_clear(&tmp);
  return ret;
}

#pretty_print(pp) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/mpfi/matrix.rb', line 180

def pretty_print(pp)
  ary = str_ary_for_inspect
  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



1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 1170

static VALUE r_mpfi_vector_set_length (VALUE self, VALUE arg) {
  MPFIMatrix *ptr_self, *ptr_ret;
  VALUE ret, returned_value;
  MPFR *ptr_l;
  returned_value = Qnil;
  r_mpfi_get_matrix_struct(ptr_self, self);
  r_mpfi_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 (mpfi_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 (mpfi_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



1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 1193

static VALUE r_mpfi_vector_set_length2 (VALUE self, VALUE arg) {
  MPFIMatrix *ptr_self, *ptr_ret;
  VALUE ret, returned_value;
  MPFR *ptr_l;
  returned_value = Qnil;
  r_mpfi_get_matrix_struct(ptr_self, self);
  r_mpfi_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
  if (RTEST(rb_funcall(r_mpfi_class, eqq, 1, arg))) {
    r_mpfr_get_struct(ptr_l, arg);
    if (mpfi_vector_set_length(ptr_ret, ptr_self, ptr_l) == 0) {
      mpfi_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 (mpfi_vector_set_length(ptr_ret, ptr_self, ptr_l) == 0) {
      mpfi_matrix_set(ptr_self, ptr_ret);
      returned_value = self;
    }
    r_mpfr_temp_free(ptr_l);
  }
  return returned_value;
}

#subdivision(*nums) ⇒ Object



240
241
242
# File 'lib/mpfi/matrix.rb', line 240

def subdivision(*nums)
  each_subdivision(*nums).to_a
end

#subdivision_by_size(size) ⇒ Object



244
245
246
# File 'lib/mpfi/matrix.rb', line 244

def subdivision_by_size(size)
  each_subdivision_by_size(size).to_a
end

#to_s(delimiter = ' ') ⇒ Object



196
197
198
# File 'lib/mpfi/matrix.rb', line 196

def to_s(delimiter = ' ')
  str_ary_for_inspect.join(delimiter)
end