Class: CArray
- Inherits:
-
Object
- Object
- CArray
- Defined in:
- lib/carray-calculus/core.rb
Overview
carray/base/calculus.rb
This file is part of Ruby/CArray extension library.
You can redistribute it and/or modify it under the terms of
the Ruby Licence.
Copyright (C) 2005 Hiroki Motoyoshi
Instance Method Summary collapse
- #differentiate ⇒ Object
- #integrate ⇒ Object
- #interp_nd_linear ⇒ Object
- #interpolate ⇒ Object
- #interpolate2(x, y, x0, y0) ⇒ Object
- #normalize(scale = nil) ⇒ Object
- #normalize!(scale = nil) ⇒ Object
- #solve(sc, val, type: "cubic", eps: 100 * Float::EPSILON) ⇒ Object
- #solve2(sc, eps: 100 * Float::EPSILON) ⇒ Object
Instance Method Details
#differentiate ⇒ Object
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 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 846 847 848 849 850 851 |
# File 'ext/carray_calculus.c', line 782
static VALUE
rb_ca_differentiate (volatile VALUE self,
volatile VALUE vsc, volatile VALUE vx)
{
volatile VALUE rval = self;
volatile VALUE out0, out;
CArray *ca, *cv, *sc, *cx, *co0, *co;
double *px, *po;
ca_size_t i;
Data_Get_Struct(self, CArray, ca);
sc = ca_wrap_readonly(vsc, CA_DOUBLE);
if ( ca_is_any_masked(ca) || ca_is_any_masked(sc) ) {
rb_raise(rb_eRuntimeError,
"can't calculate differentiation when masked elements exist");
}
if ( ca->elements != sc->elements ) {
rb_raise(rb_eRuntimeError, "data num mismatch with scale");
}
cv = ca_wrap_readonly(rval, CA_DOUBLE);
cx = ca_wrap_readonly(vx, CA_DOUBLE);
co0 = carray_new(ca->data_type, cx->ndim, cx->dim, 0, NULL);
out = out0 = ca_wrap_struct(co0);
co = ca_wrap_writable(out, CA_DOUBLE);
ca_attach_n(4, cv, sc, cx, co);
px = (double*) cx->ptr;
po = (double*) co->ptr;
ca_update_mask(cx);
if ( cx->mask ) {
boolean8_t *mx, *mo;
ca_create_mask(co);
mx = (boolean8_t *) cx->mask->ptr;
mo = (boolean8_t *) co->mask->ptr;
for (i=0; i<cx->elements; i++) {
if ( ! *mx ) {
*po = differentiate((double*)sc->ptr, (double*)cv->ptr,
cv->elements, *px);
}
else {
*mo = 1;
}
mx++; mo++; px++, po++;
}
}
else {
for (i=0; i<cx->elements; i++) {
*po = differentiate((double*)sc->ptr, (double*)cv->ptr,
cv->elements, *px);
px++, po++;
}
}
ca_sync(co);
ca_detach_n(4, cv, sc, cx, co);
if ( rb_ca_is_scalar(vx) ) {
return rb_funcall(out0, rb_intern("[]"), 1, INT2NUM(0));
}
else {
return out0;
}
}
|
#integrate ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'ext/carray_calculus.c', line 73
static VALUE
rb_ca_integrate (volatile VALUE self, volatile VALUE vsc)
{
CArray *sc, *ca;
double ans;
ca = ca_wrap_readonly(self, CA_DOUBLE);
sc = ca_wrap_readonly(vsc, CA_DOUBLE);
if ( ca->elements != sc->elements ) {
rb_raise(rb_eRuntimeError, "data num mismatch");
}
if ( ca_is_any_masked(ca) || ca_is_any_masked(sc) ) {
rb_raise(rb_eRuntimeError,
"can't calculate integrattion when masked elements exist");
}
ca_attach_n(2, ca, sc);
ans = simpson((double*)sc->ptr, (double*)ca->ptr, ca->elements);
ca_detach_n(2, ca, sc);
return rb_float_new(ans);
}
|
#interp_nd_linear ⇒ Object
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'ext/carray_interp.c', line 235
static VALUE
rb_ca_interpolate_bilinear (int argc, VALUE *argv, volatile VALUE self)
{
volatile VALUE vscales, vvalues, vs, out;
CArray *ca, *co, *cs;
double *scales[CA_RANK_MAX];
CArray *values[CA_RANK_MAX];
CArray *scales_ca[CA_RANK_MAX];
int8_t out_ndim;
ca_size_t out_dim[CA_RANK_MAX];
int i;
rb_scan_args(argc, argv, "2", &vscales, &vvalues);
Check_Type(vscales, T_ARRAY);
Check_Type(vvalues, T_ARRAY);
if ( RARRAY_LEN(vscales) != RARRAY_LEN(vvalues) ) {
rb_raise(rb_eArgError, "invalid number of values or scales");
}
ca = ca_wrap_readonly(self, CA_DOUBLE);
if ( ca->ndim != RARRAY_LEN(vvalues) ) {
rb_raise(rb_eArgError, "invalid number of values");
}
for (i=0; i<ca->ndim; i++) {
vs = rb_ary_entry(vscales, i);
if ( NIL_P(vs) ) {
scales[i] = NULL;
}
else {
cs = ca_wrap_readonly(vs, CA_DOUBLE);
scales_ca[i] = cs;
ca_attach(cs);
scales[i] = (double *) cs->ptr;
rb_ary_store(vscales, i, vs);
}
}
out_ndim = 0;
for (i=0; i<ca->ndim; i++) {
vs = rb_ary_entry(vvalues, i);
if ( NIL_P(vs) ) {
out_dim[out_ndim++] = ca->dim[i];
values[i] = NULL;
}
else {
values[i] = ca_wrap_readonly(vs, CA_DOUBLE);
if ( values[i]->obj_type != CA_OBJ_SCALAR ) {
out_dim[out_ndim++] = values[i]->elements;
}
rb_ary_store(vvalues, i, vs);
}
}
if ( out_ndim == 0 ) {
out = rb_cscalar_new(CA_DOUBLE, 0, NULL);
}
else {
out = rb_carray_new(CA_DOUBLE, out_ndim, out_dim, 0, NULL);
}
Data_Get_Struct(out, CArray, co);
for (i=0; i<ca->ndim; i++) {
if ( values[i] ) {
ca_attach(values[i]);
}
}
ca_attach(ca);
ca_interpolate(ca, scales, values, (double*) co->ptr);
ca_detach(ca);
for (i=0; i<ca->ndim; i++) {
if ( values[i] ) {
ca_detach(values[i]);
}
if ( scales[i] ) {
ca_detach(scales_ca[i]);
}
}
if ( out_ndim == 0 ) {
return rb_ca_fetch_addr(out, 0);
}
else {
return out;
}
}
|
#interpolate ⇒ Object
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 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 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 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 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 |
# File 'ext/carray_calculus.c', line 593
static VALUE
rb_ca_interpolate (int argc, VALUE *argv, VALUE self)
{
volatile VALUE rval = self;
volatile VALUE vsc, vx, ropt, rtype = Qnil, out0, out;
CArray *ca, *sc, *cv, *cx, *co0, *co;
char *typename = NULL;
int type = 0;
double *px, *po;
ca_size_t i;
Data_Get_Struct(self, CArray, ca);
rb_scan_args(argc, argv, "21", &vsc, &vx, &ropt);
rb_scan_options(ropt, "type", &rtype);
if ( ! NIL_P(rtype) ) {
Check_Type(rtype, T_STRING);
typename = StringValuePtr(rtype);
}
if ( typename == NULL || ! strncmp("cubic", typename, 5) ) {
type = 3;
}
else if ( ! strncmp("linear", typename, 6) ) {
type = 1;
}
else {
volatile VALUE inspect = rb_inspect(rtype);
rb_raise(rb_eRuntimeError,
"invalid interpolation type <%s>", StringValuePtr(inspect));
}
if ( ! NIL_P(vsc) ) {
cv = ca_wrap_readonly(rval, CA_DOUBLE);
sc = ca_wrap_readonly(vsc, CA_DOUBLE);
if ( ca_is_any_masked(cv) || ca_is_any_masked(sc) ) {
rb_raise(rb_eRuntimeError,
"can't calculate interpolation when masked elements exist");
}
if ( cv->elements != sc->elements ) {
rb_raise(rb_eRuntimeError, "data num mismatch with scale");
}
cx = ca_wrap_readonly(vx, CA_DOUBLE);
co0 = carray_new(ca->data_type, cx->ndim, cx->dim, 0, NULL);
out = out0 = ca_wrap_struct(co0);
co = ca_wrap_writable(out, CA_DOUBLE);
ca_attach_n(4, cv, sc, cx, co);
px = (double*) cx->ptr;
po = (double*) co->ptr;
ca_update_mask(cx);
if ( cx->mask ) {
boolean8_t *mx, *mo;
ca_create_mask(co);
mx = (boolean8_t *) cx->mask->ptr;
mo = (boolean8_t *) co->mask->ptr;
if ( type == 3 ) {
for (i=0; i<cx->elements; i++) {
if ( ! *mx ) {
*po = interpolate_cubic((double*)sc->ptr, (double*)cv->ptr,
cv->elements, *px);
}
else {
*mo = 1;
}
mx++; mo++; po++; px++;
}
}
else {
for (i=0; i<cx->elements; i++) {
if ( ! *mx ) {
*po = interpolate_linear((double*)sc->ptr, (double*)cv->ptr,
cv->elements, *px);
}
else {
*mo = 1;
}
mx++; mo++; po++; px++;
}
}
}
else {
if ( type == 3 ) {
for (i=0; i<cx->elements; i++) {
*po++ = interpolate_cubic((double*)sc->ptr, (double*)cv->ptr,
cv->elements, *px++);
}
}
else {
for (i=0; i<cx->elements; i++) {
*po++ = interpolate_linear((double*)sc->ptr, (double*)cv->ptr,
cv->elements, *px++);
}
}
}
ca_sync(co);
ca_detach_n(4, cv, sc, cx, co);
}
else {
cv = ca_wrap_readonly(rval, CA_DOUBLE);
if ( ca_is_any_masked(cv) ) {
rb_raise(rb_eRuntimeError,
"can't calculate interpolation when masked elements exist");
}
cx = ca_wrap_readonly(vx, CA_DOUBLE);
co0 = carray_new(ca->data_type, cx->ndim, cx->dim, 0, NULL);
out = out0 = ca_wrap_struct(co0);
co = ca_wrap_writable(out, CA_DOUBLE);
ca_attach_n(3, cv, cx, co);
px = (double*) cx->ptr;
po = (double*) co->ptr;
ca_update_mask(cx);
if ( cx->mask ) {
boolean8_t *mx, *mo;
ca_create_mask(co);
mx = (boolean8_t *) cx->mask->ptr;
mo = (boolean8_t *) co->mask->ptr;
if ( type == 3 ) {
for (i=0; i<cx->elements; i++) {
if ( ! *mx ) {
*po = interpolate_cubic(NULL, (double*)cv->ptr,
cv->elements, *px);
}
else {
*mo = 1;
}
mx++; mo++; po++; px++;
}
}
else {
for (i=0; i<cx->elements; i++) {
if ( ! *mx ) {
*po = interpolate_linear(NULL, (double*)cv->ptr,
cv->elements, *px);
}
else {
*mo = 1;
}
mx++; mo++; po++; px++;
}
}
}
else {
if ( type == 3 ) {
for (i=0; i<cx->elements; i++) {
*po++ = interpolate_cubic(NULL, (double*)cv->ptr,
cv->elements, *px++);
}
}
else {
for (i=0; i<cx->elements; i++) {
*po++ = interpolate_linear(NULL, (double*)cv->ptr,
cv->elements, *px++);
}
}
}
ca_sync(co);
ca_detach_n(3, cv, cx, co);
}
if ( rb_ca_is_scalar(vx) ) {
return rb_funcall(out0, rb_intern("[]"), 1, INT2NUM(0));
}
else {
return out0;
}
}
|
#interpolate2(x, y, x0, y0) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/carray-calculus/core.rb', line 103 def interpolate2 (x, y, x0, y0) if x0.is_a?(Numeric) and y0.is_a?(Numeric) return _interpolate2(x, y, x0, y0) else x0 = CArray.wrap_readonly(x0) y0 = CArray.wrap_readonly(y0) out = CArray.double(x0.size, y0.size) x0.each_with_index do |xi, i| y0.each_with_index do |yj, j| out[i,j] = _interpolate2(x, y, xi, yj) end end return out.compact end end |
#normalize(scale = nil) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/carray-calculus/core.rb', line 15 def normalize (scale = nil) if scale return self / self.integrate(scale) else return self / self.sum end end |
#normalize!(scale = nil) ⇒ Object
23 24 25 26 |
# File 'lib/carray-calculus/core.rb', line 23 def normalize! (scale = nil) self[] = normalize(scale) return self end |
#solve(sc, val, type: "cubic", eps: 100 * Float::EPSILON) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/carray-calculus/core.rb', line 28 def solve (sc, val, type: "cubic", eps: 100 * Float::EPSILON) func = self - val list, output = [], [] (0...dim0-1).each do |i| if func[i] == UNDEF elsif func[i].abs < eps and not list.include?(i-1) output.push(sc[i]) elsif func[i+1] == UNDEF elsif i < dim0 - 1 and func[i]*func[i+1] < 0 list.push(i) end end list.each do |i| sx = CArray.double(4) sy = CArray.double(4) sx[0], sx[3] = sc[i], sc[i+1] sy[0], sy[3] = func[i], func[i+1] sx[1], sx[2] = (2.0*sx[0]+sx[3])/3.0, (sx[0]+2.0*sx[3])/3.0 sy[1], sy[2] = func.interpolate(sc, sx[1], type: type), func.interpolate(sc, sx[2], type: type) output.push(sx.interpolate(sy, 0, type: type)) end return output.uniq end |
#solve2(sc, eps: 100 * Float::EPSILON) ⇒ Object
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 79 |
# File 'lib/carray-calculus/core.rb', line 52 def solve2 (sc, eps: 100 * Float::EPSILON) retvals = [] self.dim1.times do |j| func = self[nil,j].to_ca list, output = [], [] (0...dim0-1).each do |i| if func[i] == UNDEF elsif func[i].abs < eps and not list.include?(i-1) output.push(sc[i]) elsif func[i+1] == UNDEF elsif i < dim0 - 1 and func[i]*func[i+1] < 0 list.push(i) end end list.each do |i| sx = CArray.double(4) sy = CArray.double(4) sx[0], sx[3] = sc[i], sc[i+1] sy[0], sy[3] = func[i], func[i+1] sx[1], sx[2] = (2*sx[0]+sx[3])/3, (sx[0]+2*sx[3])/3 sy[1], sy[2] = func.interpolate(sc, sx[1], :type=>"linear"), func.interpolate(sc, sx[2], :type=>"linear") output.push(sx.interpolate(sy, 0)) end retvals << output.uniq end retvals = retvals.map{|s| s.empty? ? [nil] : s} return retvals end |