Class: MPFR::Matrix
- Inherits:
-
Object
- Object
- MPFR::Matrix
- Includes:
- Enumerable
- Defined in:
- lib/mpfr/rspec.rb,
lib/mpfr/matrix.rb,
ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c
Direct Known Subclasses
Class Method Summary collapse
- .create(a) ⇒ Object
-
.suitable_matrix_from_ary(ary) ⇒ Object
ary is two-dimensional Array.
Instance Method Summary collapse
-
#== ⇒ Object
Return true if all elements of self equal to corresponding elements of other.
-
#add ⇒ Object
(also: #+)
Return self + other.
-
#add2 ⇒ Object
Return self + other which is MPFR::Matrix.
-
#at ⇒ Object
Return element at p1.
- #check_error(other, error = nil) ⇒ Object
-
#column ⇒ Object
Return p1-th column vector.
-
#column_size ⇒ Object
Return column size of matrix.
-
#div_scalar ⇒ Object
Return scalar * self, where scalar is scalar and self is matrix.
-
#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.
-
#element ⇒ Object
(also: #[])
Return element with row and column.
- #inspect ⇒ Object
- #marshal_dump ⇒ Object
- #marshal_load(dump_ary) ⇒ Object
-
#max_norm ⇒ Object
Return maximum value of absolute value of element.
-
#mul_matrix ⇒ Object
(also: #*)
Return self * other.
-
#mul_matrix2 ⇒ Object
Return self * other which is MPFR::Matrix.
-
#mul_scalar ⇒ Object
Return scalar * self, where scalar is scalar and self is matrix.
-
#neg ⇒ Object
Multiply all elements by -1.
- #pretty_print(pp) ⇒ Object
-
#row ⇒ Object
Return p1-th row vector.
-
#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(format_str) ⇒ Object
Return one dimensinal array including strings which elements of matrix is converted to by MPFR#to_strf.
-
#str_ary2(format_str) ⇒ Object
Return two dimensinal array including strings which elements of matrix is converted to by MPFR#to_strf.
- #str_ary2_2(format) ⇒ Object
- #str_ary3(format) ⇒ Object
-
#sub ⇒ Object
(also: #-)
Return self - other.
-
#sub2 ⇒ Object
Return self - other which is MPFR::Matrix.
-
#to_a ⇒ Object
Retrn one dimensinal array including MPFR elements of matrix.
-
#to_a2 ⇒ Object
Retrn two dimensinal array including MPFR elements of matrix.
- #to_s(f = "%.Ff", delimiter = ' ') ⇒ Object
-
#transpose ⇒ Object
Return transposed matrix which is new object.
-
#transpose! ⇒ Object
Transpose self.
-
#vector_norm ⇒ Object
Regard matrix as vector and return length of the vector.
Class Method Details
.create(a) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/mpfr/matrix.rb', line 65 def self.create(a) case a when MPFR::Vector if self === a a.dup else self.new(a.to_a) end when MPFR::Matrix if a.column_size == a.row_size if MPFR::SquareMatrix === a a.dup else MPFR::SquareMatrix.new(a.to_a) end else a.dup end when Array if Array == a[0] && a.size == a[0].size MPFR::SquareMatrix.new(a) else self.new(a) end else self.new(a) end end |
.suitable_matrix_from_ary(ary) ⇒ Object
ary is two-dimensional Array.
96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/mpfr/matrix.rb', line 96 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 true if all elements of self equal to corresponding elements of other.
340 341 342 343 344 345 346 347 348 349 350 |
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 340
static VALUE r_mpfr_matrix_equal_p (VALUE self, VALUE other)
{
MPFRMatrix *ptr_self, *ptr_other;
r_mpfr_get_matrix_struct(ptr_self, self);
r_mpfr_get_matrix_struct(ptr_other, other);
if (mpfr_matrix_equal_p(ptr_self, ptr_other) != 0) {
return Qnil;
} else {
return Qtrue;
}
}
|
#add ⇒ Object Also known as: +
Return self + other.
617 618 619 620 621 622 623 624 625 626 627 628 629 630 |
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 617
static VALUE r_mpfr_matrix_add (VALUE self, VALUE other)
{
MPFRMatrix *ptr_self, *ptr_other, *ptr_ret;
VALUE ret;
r_mpfr_get_matrix_struct(ptr_self, self);
r_mpfr_get_matrix_struct(ptr_other, other);
if (ptr_self->column == ptr_other->column && ptr_self->row == ptr_other->row) {
r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
mpfr_matrix_add(ptr_ret, ptr_self, ptr_other);
}else{
rb_raise(rb_eArgError, "Matrixes must have same size.");
}
return ret;
}
|
#add2 ⇒ Object
Return self + other which is MPFR::Matrix.
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 |
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 633
static VALUE r_mpfr_matrix_add2 (VALUE self, VALUE other)
{
MPFRMatrix *ptr_self, *ptr_other, *ptr_ret;
VALUE ret;
r_mpfr_get_matrix_struct(ptr_self, self);
r_mpfr_get_matrix_struct(ptr_other, other);
r_mpfr_make_matrix_struct(ret, ptr_ret);
if (ptr_self->column == ptr_other->column && ptr_self->row == ptr_other->row) {
mpfr_matrix_init(ptr_ret, ptr_self->row, ptr_self->column);
mpfr_matrix_add(ptr_ret, ptr_self, ptr_other);
}else{
rb_raise(rb_eArgError, "Matrixes must have same size.");
}
return ret;
}
|
#at ⇒ Object
Return element at p1.
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 |
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 395
static VALUE r_mpfr_matrix_at (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;
}
}
|
#check_error(other, error = nil) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/mpfr/rspec.rb', line 17 def check_error(other, error = nil) err = error || @error column_size.times.all? do |i| row_siize.times.all? do |j| self[i][j].check_error(other[i][j], err) end end end |
#column ⇒ Object
Return p1-th column vector.
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 |
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 560
static VALUE r_mpfr_matrix_column (VALUE self, VALUE arg)
{
MPFRMatrix *ptr_self, *ptr_ret;
int num;
VALUE ret;
r_mpfr_get_matrix_struct(ptr_self, self);
num = NUM2INT(arg);
if(num < ptr_self->column){
r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, 1);
mpfr_matrix_column(ptr_ret, ptr_self, num);
return ret;
}else{
return Qnil;
}
}
|
#column_size ⇒ Object
Return column size of matrix.
361 362 363 364 365 366 |
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 361
static VALUE r_mpfr_matrix_column_size (VALUE self)
{
MPFRMatrix *ptr_self;
r_mpfr_get_matrix_struct(ptr_self, self);
return INT2FIX(ptr_self->column);
}
|
#div_scalar ⇒ Object
Return scalar * self, where scalar is scalar and self is matrix.
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 |
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 759
static VALUE r_mpfr_matrix_div_scalar (VALUE self, VALUE scalar)
{
MPFRMatrix *ptr_self, *ptr_ret;
MPFR *ptr_scalar;
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(RTEST(rb_funcall(r_mpfr_class, eqq, 1, scalar))){
r_mpfr_get_struct(ptr_scalar, scalar);
mpfr_matrix_div_scalar(ptr_ret, ptr_self, ptr_scalar);
}else{
r_mpfr_temp_alloc_init(ptr_scalar);
r_mpfr_set_robj(ptr_scalar, scalar, GMP_RNDN);
mpfr_matrix_div_scalar(ptr_ret, ptr_self, ptr_scalar);
r_mpfr_temp_free(ptr_scalar);
}
return ret;
}
|
#each_element ⇒ Object Also known as: each
Evaluate block with each element of matrix.
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 |
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 426
static VALUE r_mpfr_matrix_each_element (VALUE self)
{
MPFRMatrix *ptr_self;
int i, j;
VALUE ret;
r_mpfr_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 arg = r_mpfr_make_new_fr_obj(mpfr_matrix_get_element(ptr_self, i, j));
ret = rb_yield(arg);
}
}
return ret;
}
|
#each_element_with_index ⇒ Object
Evaluate block with each element and its index.
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 |
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 443
static VALUE r_mpfr_matrix_each_element_with_index (VALUE self)
{
MPFRMatrix *ptr_self;
VALUE ret, tmp_i;
int i, j;
r_mpfr_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 arg = r_mpfr_make_new_fr_obj(mpfr_matrix_get_element(ptr_self, i, j));
ret = rb_yield(rb_ary_new3(3, arg, tmp_i, INT2NUM(j)));
}
}
return ret;
}
|
#element ⇒ Object Also known as: []
Return element with row and column.
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 377
static VALUE r_mpfr_matrix_element (VALUE self, VALUE row, VALUE column)
{
MPFRMatrix *ptr_self;
int i, j;
r_mpfr_get_matrix_struct(ptr_self, self);
i = NUM2INT(row), j = NUM2INT(column);
if (i < ptr_self->row && j < ptr_self->column) {
VALUE ret;
MPFR *ptr_ret;
r_mpfr_make_struct_init(ret, ptr_ret);
mpfr_set(ptr_ret, ptr_self->data + i + j * ptr_self->row, GMP_RNDN);
return ret;
}else{
return Qnil;
}
}
|
#inspect ⇒ Object
31 32 33 34 35 |
# File 'lib/mpfr/matrix.rb', line 31 def inspect tmp = str_ary2("%.Re") tmp.map!{ |a| "['" + a.join("', '") + "']" } sprintf("#<%s:%x, [%s]>", self.class, __id__, tmp.join(', ')) end |
#marshal_dump ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 195
static VALUE r_mpfr_matrix_marshal_dump(VALUE self)
{
MPFRMatrix *ptr;
int i;
char *tmp_str;
VALUE ret_ary;
r_mpfr_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_mpfr_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(dump_ary) ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 215
static VALUE r_mpfr_matrix_marshal_load(VALUE self, VALUE dump_ary)
{
MPFRMatrix *ptr;
int i;
char *dump;
VALUE dump_element;
r_mpfr_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(MPFR, 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_mpfr_load_string(ptr->data + i, dump);
}
return self;
}
|
#max_norm ⇒ Object
Return maximum value of absolute value of element.
791 792 793 794 795 796 797 798 799 800 |
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 791
static VALUE r_mpfr_matrix_max_norm (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_max_norm(ptr_ret, ptr_self);
return ret;
}
|
#mul_matrix ⇒ Object Also known as: *
Return self * other.
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 |
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 701
static VALUE r_mpfr_matrix_mul_matrix (VALUE self, VALUE other)
{
MPFRMatrix *ptr_self, *ptr_other, *ptr_ret;
VALUE ret;
r_mpfr_get_matrix_struct(ptr_self, self);
r_mpfr_get_matrix_struct(ptr_other, other);
r_mpfr_make_matrix_struct(ret, ptr_ret);
if (ptr_self->column == ptr_other->row) {
if((ptr_other->column == 1) && (ptr_self->row == 1)){
ret = r_mpfr_vector_inner_product(self, other);
}else{
r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_other->column);
mpfr_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;
}
|
#mul_matrix2 ⇒ Object
Return self * other which is MPFR::Matrix.
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 |
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 722
static VALUE r_mpfr_matrix_mul_matrix2 (VALUE self, VALUE other)
{
MPFRMatrix *ptr_self, *ptr_other, *ptr_ret;
VALUE ret;
r_mpfr_get_matrix_struct(ptr_self, self);
r_mpfr_get_matrix_struct(ptr_other, other);
r_mpfr_make_matrix_struct(ret, ptr_ret);
if (ptr_self->column == ptr_other->row) {
mpfr_matrix_init(ptr_ret, ptr_self->row, ptr_other->column);
mpfr_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;
}
|
#mul_scalar ⇒ Object
Return scalar * self, where scalar is scalar and self is matrix.
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 |
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 739
static VALUE r_mpfr_matrix_mul_scalar (VALUE self, VALUE scalar)
{
MPFRMatrix *ptr_self, *ptr_ret;
MPFR *ptr_scalar;
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(RTEST(rb_funcall(r_mpfr_class, eqq, 1, scalar))){
r_mpfr_get_struct(ptr_scalar, scalar);
mpfr_matrix_mul_scalar(ptr_ret, ptr_self, ptr_scalar);
}else{
r_mpfr_temp_alloc_init(ptr_scalar);
r_mpfr_set_robj(ptr_scalar, scalar, GMP_RNDN);
mpfr_matrix_mul_scalar(ptr_ret, ptr_self, ptr_scalar);
r_mpfr_temp_free(ptr_scalar);
}
return ret;
}
|
#neg ⇒ Object
Multiply all elements by -1. This method is non-destructive.
606 607 608 609 610 611 612 613 614 |
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 606
static VALUE r_mpfr_matrix_neg (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);
mpfr_matrix_neg(ptr_ret, ptr_self);
return ret;
}
|
#pretty_print(pp) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/mpfr/matrix.rb', line 37 def pretty_print(pp) ary = str_ary2("%.Re") 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("]") pp.comma_breakable if j < row_size - 1 end pp.text("]") end end end |
#row ⇒ Object
Return p1-th row vector.
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 |
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 543
static VALUE r_mpfr_matrix_row (VALUE self, VALUE arg)
{
MPFRMatrix *ptr_self, *ptr_ret;
int num;
VALUE ret;
r_mpfr_get_matrix_struct(ptr_self, self);
num = NUM2INT(arg);
if(num < ptr_self->row){
r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, 1, ptr_self->column);
mpfr_matrix_row(ptr_ret, ptr_self, num);
return ret;
}else{
return Qnil;
}
}
|
#row_size ⇒ Object
Return row size of matrix.
369 370 371 372 373 374 |
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 369
static VALUE r_mpfr_matrix_row_size (VALUE self)
{
MPFRMatrix *ptr_self;
r_mpfr_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.
413 414 415 416 417 418 419 420 421 422 423 |
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 413
static VALUE r_mpfr_matrix_set_element (VALUE self, VALUE row, VALUE column, VALUE robj)
{
MPFRMatrix *ptr_self;
int i, j;
r_mpfr_get_matrix_struct(ptr_self, self);
i = NUM2INT(row), j = NUM2INT(column);
if (i < ptr_self->row && j < ptr_self->column) {
r_mpfr_set_robj(mpfr_matrix_get_element(ptr_self, i, j), robj, GMP_RNDN);
}
return Qnil;
}
|
#size ⇒ Object
Return size of data array which equals to column * row.
353 354 355 356 357 358 |
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 353
static VALUE r_mpfr_matrix_size (VALUE self)
{
MPFRMatrix *ptr_self;
r_mpfr_get_matrix_struct(ptr_self, self);
return INT2FIX(ptr_self->size);
}
|
#str_ary(format_str) ⇒ Object
Return one dimensinal array including strings which elements of matrix is converted to by MPFR#to_strf.
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 |
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 461
static VALUE r_mpfr_matrix_str_ary(VALUE self, VALUE format_str)
{
MPFRMatrix *ptr_self;
char *format, *tmp_str;
int i;
VALUE ret_ary;
r_mpfr_get_matrix_struct(ptr_self, self);
ret_ary = rb_ary_new2(ptr_self->size);
format = StringValuePtr(format_str);
for (i = 0; i < ptr_self->size; i++) {
if (!mpfr_asprintf(&tmp_str, format, 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_ary2(format_str) ⇒ Object
Return two dimensinal array including strings which elements of matrix is converted to by MPFR#to_strf.
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 |
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 481
static VALUE r_mpfr_matrix_str_ary2(VALUE self, VALUE format_str)
{
MPFRMatrix *ptr_self;
char *format, *tmp_str;
int i, j;
VALUE ret, *ary;
r_mpfr_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_str, format, 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 = rb_ary_new4(ptr_self->row, ary);
free(ary);
return ret;
}
|
#str_ary2_2(format) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/mpfr/matrix.rb', line 6 def str_ary2_2(format) ary = str_ary(format) tmp = [] (0...row_size).each { tmp << [] } i = 0 while i < size for j in 0...row_size tmp[j] << ary[i + j] end i += row_size end tmp end |
#str_ary3(format) ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/mpfr/matrix.rb', line 20 def str_ary3(format) ary = str_ary(format) tmp = [] i = 0 while i < size tmp << ary[i...(i + row_size)] i += row_size end tmp end |
#sub ⇒ Object Also known as: -
Return self - other.
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 |
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 650
static VALUE r_mpfr_matrix_sub (VALUE self, VALUE other)
{
MPFRMatrix *ptr_self, *ptr_other, *ptr_ret;
VALUE ret;
r_mpfr_get_matrix_struct(ptr_self, self);
r_mpfr_get_matrix_struct(ptr_other, other);
r_mpfr_make_matrix_struct(ret, ptr_ret);
if (ptr_self->column == ptr_other->column && ptr_self->row == ptr_other->row) {
r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
mpfr_matrix_sub(ptr_ret, ptr_self, ptr_other);
}else{
rb_raise(rb_eArgError, "Matrixes must have same size.");
}
return ret;
}
|
#sub2 ⇒ Object
Return self - other which is MPFR::Matrix.
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 |
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 667
static VALUE r_mpfr_matrix_sub2 (VALUE self, VALUE other)
{
MPFRMatrix *ptr_self, *ptr_other, *ptr_ret;
VALUE ret;
r_mpfr_get_matrix_struct(ptr_self, self);
r_mpfr_get_matrix_struct(ptr_other, other);
r_mpfr_make_matrix_struct(ret, ptr_ret);
if (ptr_self->column == ptr_other->column && ptr_self->row == ptr_other->row) {
mpfr_matrix_init(ptr_ret, ptr_self->row, ptr_self->column);
mpfr_matrix_sub(ptr_ret, ptr_self, ptr_other);
}else{
rb_raise(rb_eArgError, "Matrixes must have same size.");
}
return ret;
}
|
#to_a ⇒ Object
Retrn one dimensinal array including MPFR elements of matrix.
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 |
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 522
static VALUE r_mpfr_matrix_to_array2(VALUE self)
{
MPFRMatrix *ptr_self;
int i, j;
VALUE ret_ary, *ary;
r_mpfr_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_mpfr_make_new_fr_obj(ptr_self->data + i + j));
}
}
ret_ary = rb_ary_new4(ptr_self->row, ary);
free(ary);
return ret_ary;
}
|
#to_a2 ⇒ Object
Retrn two dimensinal array including MPFR elements of matrix.
508 509 510 511 512 513 514 515 516 517 518 519 |
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 508
static VALUE r_mpfr_matrix_to_array(VALUE self)
{
MPFRMatrix *ptr_self;
int i;
VALUE ret_ary;
r_mpfr_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_mpfr_make_new_fr_obj(ptr_self->data + i));
}
return ret_ary;
}
|
#to_s(f = "%.Ff", delimiter = ' ') ⇒ Object
61 62 63 |
# File 'lib/mpfr/matrix.rb', line 61 def to_s(f = "%.Ff", delimiter = ' ') str_ary(f).join(delimiter) end |
#transpose ⇒ Object
Return transposed matrix which is new object.
577 578 579 580 581 582 583 584 585 |
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 577
static VALUE r_mpfr_matrix_transpose (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->column, ptr_self->row);
mpfr_matrix_transpose(ptr_ret, ptr_self);
return ret;
}
|
#transpose! ⇒ Object
Transpose self. This method is destructive method.
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 |
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 588
static VALUE r_mpfr_matrix_transpose2 (VALUE self)
{
MPFRMatrix *ptr_self, tmp;
int i;
r_mpfr_get_matrix_struct(ptr_self, self);
if (ptr_self->column > 1 && ptr_self->row > 1) {
mpfr_matrix_init(&tmp, ptr_self->column, ptr_self->row);
mpfr_matrix_transpose(&tmp, ptr_self);
mpfr_matrix_set(ptr_self, &tmp);
mpfr_matrix_clear(&tmp);
}
i = ptr_self->column;
ptr_self->column = ptr_self->row;
ptr_self->row = i;
return self;
}
|
#vector_norm ⇒ Object
Regard matrix as vector and return length of the vector.
779 780 781 782 783 784 785 786 787 788 |
# File 'ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c', line 779
static VALUE r_mpfr_matrix_vector_norm (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;
}
|