Class: MPFI::Matrix
- Inherits:
-
Object
- Object
- MPFI::Matrix
- Includes:
- Enumerable
- Defined in:
- lib/mpfi/matrix.rb,
ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c
Direct Known Subclasses
Class Method Summary collapse
- .create(a) ⇒ Object
- .interval(ary) ⇒ Object
-
.suitable_matrix_from_ary(ary) ⇒ Object
ary is two-dimensional Array.
Instance Method Summary collapse
-
#* ⇒ Object
Return self * other.
-
#+ ⇒ Object
Return self + other.
-
#- ⇒ Object
Return self - other.
-
#== ⇒ Object
Return true if all elements of self equal to corresponding elements of other.
-
#add ⇒ Object
Return self + other which is MPFI::Matrix.
-
#at ⇒ Object
Return element at arg.
- #bounded? ⇒ Boolean
- #column ⇒ Object
-
#column_size ⇒ Object
Return column size of matrix.
- #div_scalar ⇒ Object
-
#each_element ⇒ Object
(also: #each)
Evaluate block with each element of matrix.
-
#each_element_with_index ⇒ Object
Evaluate block with each element and its index.
- #each_subdivision(nums) ⇒ Object
- #each_subdivision_by_size(size, &block) ⇒ Object
-
#element ⇒ Object
(also: #[])
Return element with row and column.
- #include? ⇒ Boolean
- #inspect ⇒ Object
- #intersect ⇒ Object
- #marshal_dump ⇒ Object
- #marshal_load ⇒ Object
- #max_diam_abs ⇒ Object
- #mid ⇒ Object
- #mid_interval ⇒ Object
-
#mul ⇒ Object
Return self * other which is MPFI::Matrix.
-
#mul_scalar ⇒ Object
Return scalar * self, where scalar is scalar and self is matrix.
-
#neg ⇒ Object
Return transposed matrix which is new object.
- #pretty_print(pp) ⇒ Object
- #row ⇒ Object
-
#row_size ⇒ Object
Return row size of matrix.
-
#set_element ⇒ Object
(also: #[]=)
Set robj to element of which row is row and column is column.
-
#size ⇒ Object
Return size of data array which equals to column * row.
-
#str_ary_for_inspect ⇒ Object
Return one dimensinal array which has strings converted elements to.
-
#str_ary_for_inspect2 ⇒ Object
Return two dimensinal array which has strings converted elements to.
- #strictly_include? ⇒ Boolean
-
#sub ⇒ Object
Return self - other which is MPFI::Matrix.
- #subdivision(nums) ⇒ Object
- #subdivision_by_size(size) ⇒ Object
- #to_a ⇒ Object
- #to_a2 ⇒ Object
- #to_s(delimiter = ' ') ⇒ Object
- #to_str_ary ⇒ Object
- #to_strf_ary ⇒ Object
-
#transpose ⇒ Object
Return transposed matrix which is new object.
-
#transpose! ⇒ Object
Transpose self.
- #union ⇒ Object
Class Method Details
.create(a) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/mpfi/matrix.rb', line 105 def self.create(a) case a when MPFI::Vector if self === a a.dup else self.new(a.to_a) end when MPFI::Matrix if a.column_size == a.row_size if MPFI::SquareMatrix === a a.dup else MPFI::SquareMatrix.new(a.to_a) end else a.dup end when Array if Array == a[0] && a.size == a[0].size MPFI::SquareMatrix.new(a) else self.new(a) end else self.new(a) end end |
.interval(ary) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/mpfi/matrix.rb', line 147 def self.interval(ary) if Array === ary && ary.all?{ |a| Array === a } row = ary.size column = ary[0].size if ary.all?{ |a| a.size == column } ret = self.new(row, column) (0...row).each do |i| (0...column).each do |j| case ary[i][j] when Array ret[i, j] = MPFI.interval(*ary[i][j]) when String ret[i, j] = MPFI.interval(*(ary[i][j].split)) when MPFI ret[i, j] = ary[i][j] else raise ArgumentError, "Invalid class for argument" end end end else ret = nil end end ret end |
.suitable_matrix_from_ary(ary) ⇒ Object
ary is two-dimensional Array.
135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/mpfi/matrix.rb', line 135 def self.suitable_matrix_from_ary(ary) if ary.size == 1 RowVector.new(ary[0]) elsif ary[0].size == 1 ColumnVector.new(ary.inject([]){ |res, val| res << val[0] }) elsif ary[0].size == ary.size SquareMatrix.new(ary) else Matrix.new(ary) end end |
Instance Method Details
#* ⇒ Object
Return self * other.
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 |
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 743
static VALUE r_mpfi_matrix_mul_matrix (VALUE self, VALUE other) {
MPFIMatrix *ptr_self, *ptr_other, *ptr_ret;
VALUE ret;
r_mpfi_get_matrix_struct(ptr_self, self);
r_mpfi_get_matrix_struct(ptr_other, other);
r_mpfi_make_matrix_struct(ret, ptr_ret);
if (ptr_self->column == ptr_other->row) {
if ((ptr_other->column == 1) && (ptr_self->row == 1)) {
ret = r_mpfi_matrix_vector_inner_product(self, other);
} else {
r_mpfi_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_other->column);
mpfi_matrix_mul(ptr_ret, ptr_self, ptr_other);
}
} else {
rb_raise(rb_eArgError, "Row size of first matrixes must be same as colmun size of second matrixes.");
}
return ret;
}
|
#+ ⇒ Object
Return self + other.
635 636 637 638 639 640 641 642 643 644 645 646 647 |
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 635
static VALUE r_mpfi_matrix_add (VALUE self, VALUE other) {
MPFIMatrix *ptr_self, *ptr_other, *ptr_ret;
VALUE ret;
r_mpfi_get_matrix_struct(ptr_self, self);
r_mpfi_get_matrix_struct(ptr_other, other);
if (ptr_self->column == ptr_other->column && ptr_self->row == ptr_other->row) {
r_mpfi_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
mpfi_matrix_add(ptr_ret, ptr_self, ptr_other);
} else {
rb_raise(rb_eArgError, "Matrixes must have same size.");
}
return ret;
}
|
#- ⇒ Object
Return self - other.
681 682 683 684 685 686 687 688 689 690 691 692 693 694 |
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 681
static VALUE r_mpfi_matrix_sub (VALUE self, VALUE other) {
MPFIMatrix *ptr_self, *ptr_other, *ptr_ret;
VALUE ret;
r_mpfi_get_matrix_struct(ptr_self, self);
r_mpfi_get_matrix_struct(ptr_other, other);
r_mpfi_make_matrix_struct(ret, ptr_ret);
if (ptr_self->column == ptr_other->column && ptr_self->row == ptr_other->row) {
r_mpfi_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
mpfi_matrix_sub(ptr_ret, ptr_self, ptr_other);
} else {
rb_raise(rb_eArgError, "Matrixes must have same size.");
}
return ret;
}
|
#== ⇒ Object
Return true if all elements of self equal to corresponding elements of other.
624 625 626 627 628 629 630 631 632 |
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 624
static VALUE r_mpfi_matrix_equal_p (VALUE self, VALUE other) {
MPFIMatrix *ptr_self, *ptr_other;
r_mpfi_get_matrix_struct(ptr_self, self);
r_mpfi_get_matrix_struct(ptr_other, other);
if (mpfi_matrix_equal_p(ptr_self, ptr_other) != 0) {
return Qnil;
}
return Qtrue;
}
|
#add ⇒ Object
Return self + other which is MPFI::Matrix.
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 |
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 650
static VALUE r_mpfi_matrix_add2 (VALUE self, VALUE other) {
MPFIMatrix *ptr_self, *ptr_ret;
VALUE ret;
r_mpfi_get_matrix_struct(ptr_self, self);
r_mpfi_make_matrix_struct(ret, ptr_ret);
if (RTEST(rb_funcall(__mpfi_matrix_class__, eqq, 1, other))) {
MPFIMatrix *ptr_other;
r_mpfi_get_matrix_struct(ptr_other, other);
if (ptr_self->column == ptr_other->column && ptr_self->row == ptr_other->row) {
r_mpfi_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
mpfi_matrix_add(ptr_ret, ptr_self, ptr_other);
} else {
rb_raise(rb_eArgError, "Matrixes must have same size.");
}
} else if (RTEST(rb_funcall(__mpfr_matrix_class__, eqq, 1, other))) {
MPFRMatrix *ptr_other;
r_mpfr_get_matrix_struct(ptr_other, other);
if (ptr_self->column == ptr_other->column && ptr_self->row == ptr_other->row) {
r_mpfi_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
mpfi_matrix_add_fr(ptr_ret, ptr_self, ptr_other);
} else {
rb_raise(rb_eArgError, "Matrixes must have same size.");
}
} else {
rb_raise(rb_eArgError, "Argument matrix must be an instance of MPFI::Matrix or MPFR::Matrix.");
}
return ret;
}
|
#at ⇒ Object
Return element at arg.
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 340
static VALUE r_mpfi_matrix_at (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;
}
}
|
#bounded? ⇒ Boolean
871 872 873 874 875 876 877 878 879 |
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 871
static VALUE r_mpfi_matrix_bounded_p (VALUE self) {
MPFIMatrix *ptr_self;
r_mpfi_get_matrix_struct(ptr_self, self);
if (mpfi_matrix_bounded_p(ptr_self) == 0) {
return Qtrue;
} else {
return Qnil;
}
}
|
#column ⇒ Object
570 571 572 573 574 575 576 577 578 579 580 581 582 583 |
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 570
static VALUE r_mpfi_matrix_column (VALUE self, VALUE arg) {
MPFIMatrix *ptr_self, *ptr_ret;
int num;
VALUE ret;
r_mpfi_get_matrix_struct(ptr_self, self);
num = NUM2INT(arg);
if (num < ptr_self->column) {
r_mpfi_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, 1);
mpfi_matrix_column(ptr_ret, ptr_self, num);
return ret;
} else {
return Qnil;
}
}
|
#column_size ⇒ Object
Return column size of matrix.
326 327 328 329 330 |
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 326
static VALUE r_mpfi_matrix_column_size (VALUE self) {
MPFIMatrix *ptr_self;
r_mpfi_get_matrix_struct(ptr_self, self);
return INT2FIX(ptr_self->column);
}
|
#div_scalar ⇒ Object
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 |
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 812
static VALUE r_mpfi_matrix_div_scalar (VALUE self, VALUE scalar) {
MPFIMatrix *ptr_self, *ptr_ret;
MPFI *ptr_scalar;
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 (RTEST(rb_funcall(r_mpfi_class, eqq, 1, scalar))) {
r_mpfi_get_struct(ptr_scalar, scalar);
mpfi_matrix_div_scalar(ptr_ret, ptr_self, ptr_scalar);
} else {
r_mpfi_temp_alloc_init(ptr_scalar);
r_mpfi_set_robj(ptr_scalar, scalar);
mpfi_matrix_div_scalar(ptr_ret, ptr_self, ptr_scalar);
r_mpfi_temp_free(ptr_scalar);
}
return ret;
}
|
#each_element ⇒ Object Also known as: each
Evaluate block with each element of matrix.
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 |
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 388
static VALUE r_mpfi_matrix_each_element (VALUE self) {
MPFIMatrix *ptr_self;
VALUE ret;
int i, j;
RETURN_ENUMERATOR(self, 0, NULL);
r_mpfi_get_matrix_struct(ptr_self, self);
ret = Qnil;
for (i = 0; i < ptr_self->row; i++) {
for (j = 0; j < ptr_self->column; j++) {
volatile VALUE el = r_mpfi_make_new_fi_obj(ptr_self->data + i + j * ptr_self->row);
ret = rb_yield(el);
}
}
return ret;
}
|
#each_element_with_index ⇒ Object
Evaluate block with each element and its index.
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 405
static VALUE r_mpfi_matrix_each_element_with_index (VALUE self) {
MPFIMatrix *ptr_self;
VALUE ret, tmp_i;
int i, j;
RETURN_ENUMERATOR(self, 0, NULL);
r_mpfi_get_matrix_struct(ptr_self, self);
ret = Qnil;
for (i = 0; i < ptr_self->row; i++) {
tmp_i = INT2NUM(i);
for (j = 0; j < ptr_self->column; j++) {
volatile VALUE el = r_mpfi_make_new_fi_obj(ptr_self->data + i + j * ptr_self->row);
ret = rb_yield(rb_ary_new3(3, el, tmp_i, INT2NUM(j)));
}
}
return ret;
}
|
#each_subdivision(nums) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/mpfi/matrix.rb', line 41 def each_subdivision(nums) if block_given? row_size = self.row_size col_size = self.column_size if (row_size != nums.size) || !(nums.all? { |col_nums| col_nums.size == col_size }) raise ArgumentError, "Invalid numbers to specify split" end dim = row_size * col_size num_current = 0 elements = [] row_size.times do |r| col_size.times do |c| elements << self[r, c].subdivision(nums[r][c]) end end indices = Array.new(dim, 0) args = Array.new(row_size) { Array.new(col_size) } while num_current >= 0 while num_current < dim row_num, col_num = num_current.divmod(col_size) if args[row_num][col_num] = 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
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/mpfi/matrix.rb', line 80 def each_subdivision_by_size(size, &block) if block_given? row_size = self.row_size col_size = self.column_size row_num, col_num = num.divmod(col_size) nums = Array.new(row_size) { Array.new(col_size) } row_size.times do |r| col_size.times do |c| nums[r][c] = (self[r, c].diam_abs / size).ceil end end each_subdivision(nums, &block) else to_enum(:each_subdivision_by_size, size) end end |
#element ⇒ Object Also known as: []
Return element with row and column.
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 357
static VALUE r_mpfi_matrix_element (VALUE self, VALUE row, VALUE column) {
MPFIMatrix *ptr_self;
int i, j;
r_mpfi_get_matrix_struct(ptr_self, self);
i = NUM2INT(row);
j = NUM2INT(column);
if (i < ptr_self->row && j < ptr_self->column) {
VALUE ret;
MPFI*ptr_ret;
r_mpfi_make_struct_init(ret, ptr_ret);
mpfi_set(ptr_ret, ptr_self->data + i + j * ptr_self->row);
return ret;
} else {
return Qnil;
}
}
|
#include? ⇒ Boolean
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 |
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 831
static VALUE r_mpfi_matrix_include_p (VALUE self, VALUE other) {
MPFIMatrix *ptr_self;
VALUE ret;
r_mpfi_get_matrix_struct(ptr_self, self);
ret = Qnil;
if (RTEST(rb_funcall(r_mpfi_matrix, eqq, 1, other))) {
MPFIMatrix *ptr_other;
r_mpfi_get_matrix_struct(ptr_other, other);
if (mpfi_matrix_include_p(ptr_self, ptr_other) == 0) {
ret = Qtrue;
}
} else if (RTEST(rb_funcall(r_mpfr_matrix, eqq, 1, other))) {
MPFRMatrix *ptr_other;
r_mpfr_get_matrix_struct(ptr_other, other);
if (mpfi_matrix_include_fr_p(ptr_self, ptr_other) == 0) {
ret = Qtrue;
}
} else {
rb_raise(rb_eArgError, "Argument must be MPFI::Matrix or MPFR::Matrix instance.");
}
return ret;
}
|
#inspect ⇒ Object
8 9 10 11 12 |
# File 'lib/mpfi/matrix.rb', line 8 def inspect tmp = str_ary_for_inspect2 tmp.map!{ |a| "['" + a.join("', '") + "']"} sprintf("#<%s:%x, [%s]>", self.class, __id__, tmp.join(', ')) end |
#intersect ⇒ Object
900 901 902 903 904 905 906 907 908 909 910 911 |
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 900
static VALUE r_mpfi_matrix_intersect (VALUE self, VALUE other) {
MPFIMatrix *ptr_self, *ptr_other, *ptr_ret;
VALUE val_ret;
r_mpfi_get_matrix_struct(ptr_self, self);
r_mpfi_get_matrix_struct(ptr_other, other);
r_mpfi_matrix_suitable_matrix_init(&val_ret, &ptr_ret, ptr_self->row, ptr_self->column);
if (mpfi_matrix_intersect(ptr_ret, ptr_self, ptr_other) == 0) {
return val_ret;
} else {
return Qnil;
}
}
|
#marshal_dump ⇒ Object
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 276
static VALUE r_mpfi_matrix_marshal_dump (VALUE self)
{
MPFIMatrix *ptr;
int i;
char *tmp_str;
VALUE ret_ary;
r_mpfi_get_matrix_struct(ptr, self);
ret_ary = rb_ary_new();
rb_ary_push(ret_ary, INT2FIX(ptr->row));
rb_ary_push(ret_ary, INT2FIX(ptr->column));
for (i = 0; i < ptr->size; i++) {
tmp_str = r_mpfi_dump_to_string(ptr->data + i);
rb_ary_push(ret_ary, rb_str_new2(tmp_str));
mpfr_free_str(tmp_str);
}
return ret_ary;
}
|
#marshal_load ⇒ Object
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 296
static VALUE r_mpfi_matrix_marshal_load (VALUE self, VALUE dump_ary)
{
MPFIMatrix *ptr;
int i;
char *dump;
VALUE dump_element;
r_mpfi_get_matrix_struct(ptr, self);
ptr->row = NUM2INT(rb_ary_entry(dump_ary, 0));
ptr->column = NUM2INT(rb_ary_entry(dump_ary, 1));
ptr->size = ptr->row * ptr->column;
ptr->data = ALLOC_N(MPFI, ptr->size);
for (i = 0; i < ptr->size; i++) {
dump_element = rb_ary_entry(dump_ary, i + 2);
Check_Type(dump_element, T_STRING);
dump = RSTRING_PTR(dump_element);
r_mpfi_load_string(ptr->data + i, dump);
}
return self;
}
|
#max_diam_abs ⇒ Object
927 928 929 930 931 932 933 934 935 |
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 927 static VALUE r_mpfi_matrix_max_diam_abs (VALUE self) { MPFIMatrix *ptr_self; MPFR *ptr_ret; VALUE ret_val; r_mpfi_get_matrix_struct(ptr_self, self); r_mpfr_make_struct_init(ret_val, ptr_ret); mpfi_matrix_max_diam_abs(ptr_ret, ptr_self); return ret_val; } |
#mid ⇒ Object
881 882 883 884 885 886 887 888 889 |
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 881
static VALUE r_mpfi_matrix_mid (VALUE self) {
MPFIMatrix *ptr_self;
VALUE val_ret;
MPFRMatrix *ptr_ret;
r_mpfi_get_matrix_struct(ptr_self, self);
r_mpfr_matrix_suitable_matrix_init(&val_ret, &ptr_ret, ptr_self->row, ptr_self->column);
mpfi_matrix_mid(ptr_ret, ptr_self);
return val_ret;
}
|
#mid_interval ⇒ Object
891 892 893 894 895 896 897 898 |
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 891
static VALUE r_mpfi_matrix_mid_interval (VALUE self) {
MPFIMatrix *ptr_self, *ptr_ret;
VALUE val_ret;
r_mpfi_get_matrix_struct(ptr_self, self);
r_mpfi_matrix_suitable_matrix_init(&val_ret, &ptr_ret, ptr_self->row, ptr_self->column);
mpfi_matrix_mid_interval(ptr_ret, ptr_self);
return val_ret;
}
|
#mul ⇒ Object
Return self * other which is MPFI::Matrix.
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 |
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 763
static VALUE r_mpfi_matrix_mul_matrix2 (VALUE self, VALUE other) {
MPFIMatrix *ptr_self, *ptr_ret;
VALUE ret;
r_mpfi_get_matrix_struct(ptr_self, self);
r_mpfi_make_matrix_struct(ret, ptr_ret);
if (RTEST(rb_funcall(__mpfi_matrix_class__, eqq, 1, other))) {
MPFIMatrix *ptr_other;
r_mpfi_get_matrix_struct(ptr_other, other);
if (ptr_self->column == ptr_other->row) {
r_mpfi_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_other->column);
mpfi_matrix_mul(ptr_ret, ptr_self, ptr_other);
} else {
rb_raise(rb_eArgError, "Row size of first matrixes must be same as colmun size of second matrixes.");
}
} else if (RTEST(rb_funcall(__mpfr_matrix_class__, eqq, 1, other))) {
MPFRMatrix *ptr_other;
r_mpfr_get_matrix_struct(ptr_other, other);
if (ptr_self->column == ptr_other->row) {
r_mpfi_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_other->column);
mpfi_matrix_mul_fr(ptr_ret, ptr_self, ptr_other);
} else {
rb_raise(rb_eArgError, "Row size of first matrixes must be same as colmun size of second matrixes.");
}
} else {
rb_raise(rb_eArgError, "Argument matrix must be an instance of MPFI::Matrix or MPFR::Matrix.");
}
return ret;
}
|
#mul_scalar ⇒ Object
Return scalar * self, where scalar is scalar and self is matrix.
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 |
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 794
static VALUE r_mpfi_matrix_mul_scalar (VALUE self, VALUE scalar) {
MPFIMatrix *ptr_self, *ptr_ret;
MPFI *ptr_scalar;
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 (RTEST(rb_funcall(r_mpfi_class, eqq, 1, scalar))) {
r_mpfi_get_struct(ptr_scalar, scalar);
mpfi_matrix_mul_scalar(ptr_ret, ptr_self, ptr_scalar);
} else {
r_mpfi_temp_alloc_init(ptr_scalar);
r_mpfi_set_robj(ptr_scalar, scalar);
mpfi_matrix_mul_scalar(ptr_ret, ptr_self, ptr_scalar);
r_mpfi_temp_free(ptr_scalar);
}
return ret;
}
|
#neg ⇒ Object
Return transposed matrix which is new object.
613 614 615 616 617 618 619 620 |
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 613
static VALUE r_mpfi_matrix_neg (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);
mpfi_matrix_neg(ptr_ret, ptr_self);
return ret;
}
|
#pretty_print(pp) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/mpfi/matrix.rb', line 14 def pretty_print(pp) ary = str_ary_for_inspect2 pp.object_group(self) do pp.text(sprintf(':%x, ', __id__)) pp.breakable pp.text("[") pp.nest(1) do for j in 0...row_size pp.breakable if j > 0 pp.text(%|["#{ary[j][0]}"|) pp.nest(1) do for i in 1...column_size pp.comma_breakable pp.text(%|"#{ary[j][i]}"|) end end pp.text("]") end pp.text("]") end end end |
#row ⇒ Object
555 556 557 558 559 560 561 562 563 564 565 566 567 568 |
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 555
static VALUE r_mpfi_matrix_row (VALUE self, VALUE arg) {
MPFIMatrix *ptr_self, *ptr_ret;
int num;
VALUE ret;
r_mpfi_get_matrix_struct(ptr_self, self);
num = NUM2INT(arg);
if (num < ptr_self->row) {
r_mpfi_matrix_suitable_matrix_init (&ret, &ptr_ret, 1, ptr_self->column);
mpfi_matrix_row(ptr_ret, ptr_self, num);
return ret;
} else {
return Qnil;
}
}
|
#row_size ⇒ Object
Return row size of matrix.
333 334 335 336 337 |
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 333
static VALUE r_mpfi_matrix_row_size (VALUE self) {
MPFIMatrix *ptr_self;
r_mpfi_get_matrix_struct(ptr_self, self);
return INT2FIX(ptr_self->row);
}
|
#set_element ⇒ Object Also known as: []=
Set robj to element of which row is row and column is column.
375 376 377 378 379 380 381 382 383 384 385 |
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 375
static VALUE r_mpfi_matrix_set_element (VALUE self, VALUE row, VALUE column, VALUE robj) {
MPFIMatrix *ptr_self;
int i, j;
r_mpfi_get_matrix_struct(ptr_self, self);
i = NUM2INT(row);
j = NUM2INT(column);
if (i < ptr_self->row && j < ptr_self->column) {
r_mpfi_set_robj(ptr_self->data + i + j * ptr_self->row, robj);
}
return Qnil;
}
|
#size ⇒ Object
Return size of data array which equals to column * row.
319 320 321 322 323 |
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 319
static VALUE r_mpfi_matrix_size (VALUE self) {
MPFIMatrix *ptr_self;
r_mpfi_get_matrix_struct(ptr_self, self);
return INT2FIX(ptr_self->size);
}
|
#str_ary_for_inspect ⇒ Object
Return one dimensinal array which has strings converted elements to.
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 |
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 423
static VALUE r_mpfi_matrix_str_ary_for_inspect (VALUE self) {
MPFIMatrix *ptr_self;
char *tmp_str;
VALUE ret_ary;
int i;
r_mpfi_get_matrix_struct(ptr_self, self);
ret_ary = rb_ary_new2(ptr_self->size);
for (i = 0; i < ptr_self->size; i++) {
if (!mpfr_asprintf(&tmp_str, "%.Re %.Re",
r_mpfi_left_ptr((ptr_self->data + i)), r_mpfi_right_ptr(ptr_self->data + i))) {
rb_raise(rb_eFatal, "Can not allocate a string by mpfr_asprintf.");
}
rb_ary_store(ret_ary, i, rb_str_new2(tmp_str));
mpfr_free_str(tmp_str);
}
return ret_ary;
}
|
#str_ary_for_inspect2 ⇒ Object
Return two dimensinal array which has strings converted elements to.
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 |
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 442
static VALUE r_mpfi_matrix_str_ary_for_inspect2 (VALUE self) {
MPFIMatrix *ptr_self;
char *tmp_str;
int i, j;
VALUE *ary, ret_ary;
r_mpfi_get_matrix_struct(ptr_self, self);
ary = ALLOC_N(VALUE, ptr_self->row);
for (i = 0; i < ptr_self->row; i++) {
ary[i] = rb_ary_new();
}
for (i = 0; i < ptr_self->size; i += ptr_self->row) {
for (j = 0; j < ptr_self->row; j++) {
if (!mpfr_asprintf(&tmp_str, "%.Re %.Re",
r_mpfi_left_ptr((ptr_self->data + i + j)), r_mpfi_right_ptr(ptr_self->data + i + j))) {
rb_raise(rb_eFatal, "Can not allocate a string by mpfr_asprintf.");
}
rb_ary_push(ary[j], rb_str_new2(tmp_str));
mpfr_free_str(tmp_str);
}
}
ret_ary = rb_ary_new4(ptr_self->row, ary);
free(ary);
return ret_ary;
}
|
#strictly_include? ⇒ Boolean
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 |
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 854
static VALUE r_mpfi_matrix_strictly_include_p (VALUE self, VALUE other) {
MPFIMatrix *ptr_self;
VALUE ret;
r_mpfi_get_matrix_struct(ptr_self, self);
ret = Qnil;
if (RTEST(rb_funcall(r_mpfi_matrix, eqq, 1, other))) {
MPFIMatrix *ptr_other;
r_mpfi_get_matrix_struct(ptr_other, other);
if (mpfi_matrix_strictly_include_p(ptr_self, ptr_other) == 0) {
ret = Qtrue;
}
} else {
rb_raise(rb_eArgError, "Argument must be MPFI::Matrix instance.");
}
return ret;
}
|
#sub ⇒ Object
Return self - other which is MPFI::Matrix.
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 |
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 697
static VALUE r_mpfi_matrix_sub2 (VALUE self, VALUE other) {
MPFIMatrix *ptr_self, *ptr_ret;
VALUE ret;
r_mpfi_get_matrix_struct(ptr_self, self);
r_mpfi_make_matrix_struct(ret, ptr_ret);
if (RTEST(rb_funcall(__mpfi_matrix_class__, eqq, 1, other))) {
MPFIMatrix *ptr_other;
r_mpfi_get_matrix_struct(ptr_other, other);
if (ptr_self->column == ptr_other->column && ptr_self->row == ptr_other->row) {
r_mpfi_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
mpfi_matrix_sub(ptr_ret, ptr_self, ptr_other);
} else {
rb_raise(rb_eArgError, "Matrixes must have same size.");
}
} else if (RTEST(rb_funcall(__mpfr_matrix_class__, eqq, 1, other))) {
MPFRMatrix *ptr_other;
r_mpfr_get_matrix_struct(ptr_other, other);
if (ptr_self->column == ptr_other->column && ptr_self->row == ptr_other->row) {
r_mpfi_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
mpfi_matrix_sub_fr(ptr_ret, ptr_self, ptr_other);
} else {
rb_raise(rb_eArgError, "Matrixes must have same size.");
}
} else {
rb_raise(rb_eArgError, "Argument matrix must be an instance of MPFI::Matrix or MPFR::Matrix.");
}
return ret;
}
|
#subdivision(nums) ⇒ Object
97 98 99 |
# File 'lib/mpfi/matrix.rb', line 97 def subdivision(nums) each_subdivision(nums).to_a end |
#subdivision_by_size(size) ⇒ Object
101 102 103 |
# File 'lib/mpfi/matrix.rb', line 101 def subdivision_by_size(size) each_subdivision_by_size(size).to_a end |
#to_a ⇒ Object
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 |
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 536
static VALUE r_mpfi_matrix_to_array2 (VALUE self) {
MPFIMatrix *ptr_self;
int i, j;
VALUE *ary, ret_ary;
r_mpfi_get_matrix_struct(ptr_self, self);
ary = ALLOC_N(VALUE, ptr_self->row);
for (i = 0; i < ptr_self->row; i++) {
ary[i] = rb_ary_new();
}
for (i = 0; i < ptr_self->size; i += ptr_self->row) {
for (j = 0; j < ptr_self->row; j++) {
rb_ary_push(ary[j], r_mpfi_make_new_fi_obj(ptr_self->data + i + j));
}
}
ret_ary = rb_ary_new4(ptr_self->row, ary);
free(ary);
return ret_ary;
}
|
#to_a2 ⇒ Object
524 525 526 527 528 529 530 531 532 533 534 |
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 524
static VALUE r_mpfi_matrix_to_array (VALUE self) {
MPFIMatrix *ptr_self;
int i;
VALUE ret_ary;
r_mpfi_get_matrix_struct(ptr_self, self);
ret_ary = rb_ary_new2(ptr_self->size);
for (i = 0; i < ptr_self->size; i++) {
rb_ary_store(ret_ary, i, r_mpfi_make_new_fi_obj(ptr_self->data + i));
}
return ret_ary;
}
|
#to_s(delimiter = ' ') ⇒ Object
37 38 39 |
# File 'lib/mpfi/matrix.rb', line 37 def to_s(delimiter = ' ') str_ary_for_inspect.join(delimiter) end |
#to_str_ary ⇒ Object
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 |
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 467
static VALUE r_mpfi_matrix_to_str_ary (VALUE self) {
MPFIMatrix *ptr_self;
char *tmp_str1, *tmp_str2;
int i, j;
VALUE *ary, ret_ary;
r_mpfi_get_matrix_struct(ptr_self, self);
ary = ALLOC_N(VALUE, ptr_self->row);
for (i = 0; i < ptr_self->row; i++) {
ary[i] = rb_ary_new();
}
for (i = 0; i < ptr_self->size; i += ptr_self->row) {
for (j = 0; j < ptr_self->row; j++) {
if (!mpfr_asprintf(&tmp_str1, "%.Re", r_mpfi_left_ptr((ptr_self->data + i + j)))) {
rb_raise(rb_eFatal, "Can not allocate a string by mpfr_asprintf.");
}
if (!mpfr_asprintf(&tmp_str2, "%.Re", r_mpfi_right_ptr(ptr_self->data + i + j))) {
rb_raise(rb_eFatal, "Can not allocate a string by mpfr_asprintf.");
}
rb_ary_push(ary[j], rb_ary_new3(2, rb_str_new2(tmp_str1), rb_str_new2(tmp_str2)));
mpfr_free_str(tmp_str1);
mpfr_free_str(tmp_str2);
}
}
ret_ary = rb_ary_new4(ptr_self->row, ary);
free(ary);
return ret_ary;
}
|
#to_strf_ary ⇒ Object
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 |
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 495
static VALUE r_mpfi_matrix_to_strf_ary (VALUE self, VALUE format_str) {
MPFIMatrix *ptr_self;
char *tmp_str1, *tmp_str2, *format;
int i, j;
VALUE *ary, ret_ary;
r_mpfi_get_matrix_struct(ptr_self, self);
format = StringValuePtr(format_str);
ary = ALLOC_N(VALUE, ptr_self->row);
for (i = 0; i < ptr_self->row; i++) {
ary[i] = rb_ary_new();
}
for (i = 0; i < ptr_self->size; i += ptr_self->row) {
for (j = 0; j < ptr_self->row; j++) {
if (!mpfr_asprintf(&tmp_str1, format, r_mpfi_left_ptr((ptr_self->data + i + j)))) {
rb_raise(rb_eFatal, "Can not allocate a string by mpfr_asprintf.");
}
if (!mpfr_asprintf(&tmp_str2, format, r_mpfi_right_ptr(ptr_self->data + i + j))) {
rb_raise(rb_eFatal, "Can not allocate a string by mpfr_asprintf.");
}
rb_ary_push(ary[j], rb_ary_new3(2, rb_str_new2(tmp_str1), rb_str_new2(tmp_str2)));
mpfr_free_str(tmp_str1);
mpfr_free_str(tmp_str2);
}
}
ret_ary = rb_ary_new4(ptr_self->row, ary);
free(ary);
return ret_ary;
}
|
#transpose ⇒ Object
Return transposed matrix which is new object.
586 587 588 589 590 591 592 593 |
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 586
static VALUE r_mpfi_matrix_transpose (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->column, ptr_self->row);
mpfi_matrix_transpose(ptr_ret, ptr_self);
return ret;
}
|
#transpose! ⇒ Object
Transpose self. This method is destroying method.
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 |
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 596
static VALUE r_mpfi_matrix_transpose2 (VALUE self) {
MPFIMatrix *ptr_self, tmp;
int i;
r_mpfi_get_matrix_struct(ptr_self, self);
if (ptr_self->column > 1 && ptr_self->row > 1) {
mpfi_matrix_init(&tmp, ptr_self->column, ptr_self->row);
mpfi_matrix_transpose(&tmp, ptr_self);
mpfi_matrix_set(ptr_self, &tmp);
mpfi_matrix_clear(&tmp);
}
i = ptr_self->column;
ptr_self->column = ptr_self->row;
ptr_self->row = i;
return self;
}
|
#union ⇒ Object
913 914 915 916 917 918 919 920 921 922 923 924 |
# File 'ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c', line 913
static VALUE r_mpfi_matrix_union (VALUE self, VALUE other) {
MPFIMatrix *ptr_self, *ptr_other, *ptr_ret;
VALUE val_ret;
r_mpfi_get_matrix_struct(ptr_self, self);
r_mpfi_get_matrix_struct(ptr_other, other);
r_mpfi_matrix_suitable_matrix_init(&val_ret, &ptr_ret, ptr_self->row, ptr_self->column);
if (mpfi_matrix_union(ptr_ret, ptr_self, ptr_other) == 0) {
return val_ret;
} else {
return Qnil;
}
}
|