numo-narray-alt 0.9.12 → 0.9.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -48,6 +48,11 @@ extern VALUE cRT;
48
48
  #include "mh/op/sub.h"
49
49
  #include "mh/op/mul.h"
50
50
  #include "mh/op/div.h"
51
+ #include "mh/pow.h"
52
+ #include "mh/minus.h"
53
+ #include "mh/reciprocal.h"
54
+ #include "mh/sign.h"
55
+ #include "mh/square.h"
51
56
  #include "mh/comp/eq.h"
52
57
  #include "mh/comp/ne.h"
53
58
  #include "mh/comp/nearly_eq.h"
@@ -74,6 +79,7 @@ extern VALUE cRT;
74
79
  #include "mh/logseq.h"
75
80
  #include "mh/eye.h"
76
81
  #include "mh/rand.h"
82
+ #include "mh/rand_norm.h"
77
83
  #include "mh/math/sqrt.h"
78
84
  #include "mh/math/cbrt.h"
79
85
  #include "mh/math/log.h"
@@ -106,6 +112,11 @@ DEF_NARRAY_ADD_METHOD_FUNC(scomplex, numo_cSComplex)
106
112
  DEF_NARRAY_SUB_METHOD_FUNC(scomplex, numo_cSComplex)
107
113
  DEF_NARRAY_MUL_METHOD_FUNC(scomplex, numo_cSComplex)
108
114
  DEF_NARRAY_FLT_DIV_METHOD_FUNC(scomplex, numo_cSComplex)
115
+ DEF_NARRAY_POW_METHOD_FUNC(scomplex, numo_cSComplex)
116
+ DEF_NARRAY_MINUS_METHOD_FUNC(scomplex, numo_cSComplex)
117
+ DEF_NARRAY_RECIPROCAL_METHOD_FUNC(scomplex, numo_cSComplex)
118
+ DEF_NARRAY_SIGN_METHOD_FUNC(scomplex, numo_cSComplex)
119
+ DEF_NARRAY_SQUARE_METHOD_FUNC(scomplex, numo_cSComplex)
109
120
  DEF_NARRAY_EQ_METHOD_FUNC(scomplex, numo_cSComplex)
110
121
  DEF_NARRAY_NE_METHOD_FUNC(scomplex, numo_cSComplex)
111
122
  DEF_NARRAY_NEARLY_EQ_METHOD_FUNC(scomplex, numo_cSComplex)
@@ -132,6 +143,7 @@ DEF_NARRAY_FLT_SEQ_METHOD_FUNC(scomplex)
132
143
  DEF_NARRAY_FLT_LOGSEQ_METHOD_FUNC(scomplex)
133
144
  DEF_NARRAY_EYE_METHOD_FUNC(scomplex)
134
145
  DEF_NARRAY_CMP_RAND_METHOD_FUNC(scomplex)
146
+ DEF_NARRAY_CMP_RAND_NORM_METHOD_FUNC(scomplex, float)
135
147
  DEF_NARRAY_FLT_SQRT_METHOD_FUNC(scomplex, numo_cSComplex)
136
148
  DEF_NARRAY_FLT_CBRT_METHOD_FUNC(scomplex, numo_cSComplex)
137
149
  DEF_NARRAY_FLT_LOG_METHOD_FUNC(scomplex, numo_cSComplex)
@@ -1763,374 +1775,6 @@ static VALUE scomplex_abs(VALUE self) {
1763
1775
  return na_ndloop(&ndf, 1, self);
1764
1776
  }
1765
1777
 
1766
- static void iter_scomplex_pow(na_loop_t* const lp) {
1767
- size_t i;
1768
- char *p1, *p2, *p3;
1769
- ssize_t s1, s2, s3;
1770
- dtype x, y;
1771
- INIT_COUNTER(lp, i);
1772
- INIT_PTR(lp, 0, p1, s1);
1773
- INIT_PTR(lp, 1, p2, s2);
1774
- INIT_PTR(lp, 2, p3, s3);
1775
- for (; i--;) {
1776
- GET_DATA_STRIDE(p1, s1, dtype, x);
1777
- GET_DATA_STRIDE(p2, s2, dtype, y);
1778
- x = m_pow(x, y);
1779
- SET_DATA_STRIDE(p3, s3, dtype, x);
1780
- }
1781
- }
1782
-
1783
- static void iter_scomplex_pow_int32(na_loop_t* const lp) {
1784
- size_t i;
1785
- char *p1, *p2, *p3;
1786
- ssize_t s1, s2, s3;
1787
- dtype x;
1788
- int32_t y;
1789
- INIT_COUNTER(lp, i);
1790
- INIT_PTR(lp, 0, p1, s1);
1791
- INIT_PTR(lp, 1, p2, s2);
1792
- INIT_PTR(lp, 2, p3, s3);
1793
- for (; i--;) {
1794
- GET_DATA_STRIDE(p1, s1, dtype, x);
1795
- GET_DATA_STRIDE(p2, s2, int32_t, y);
1796
- x = m_pow_int(x, y);
1797
- SET_DATA_STRIDE(p3, s3, dtype, x);
1798
- }
1799
- }
1800
-
1801
- static VALUE scomplex_pow_self(VALUE self, VALUE other) {
1802
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
1803
- ndfunc_arg_in_t ain_i[2] = { { cT, 0 }, { numo_cInt32, 0 } };
1804
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
1805
- ndfunc_t ndf = { iter_scomplex_pow, STRIDE_LOOP, 2, 1, ain, aout };
1806
- ndfunc_t ndf_i = { iter_scomplex_pow_int32, STRIDE_LOOP, 2, 1, ain_i, aout };
1807
-
1808
- // fixme : use na.integer?
1809
- if (FIXNUM_P(other) || rb_obj_is_kind_of(other, numo_cInt32)) {
1810
- return na_ndloop(&ndf_i, 2, self, other);
1811
- } else {
1812
- return na_ndloop(&ndf, 2, self, other);
1813
- }
1814
- }
1815
-
1816
- /*
1817
- Binary power.
1818
- @overload ** other
1819
- @param [Numo::NArray,Numeric] other
1820
- @return [Numo::NArray] self to the other-th power.
1821
- */
1822
- static VALUE scomplex_pow(VALUE self, VALUE other) {
1823
-
1824
- VALUE klass, v;
1825
- klass = na_upcast(rb_obj_class(self), rb_obj_class(other));
1826
- if (klass == cT) {
1827
- return scomplex_pow_self(self, other);
1828
- } else {
1829
- v = rb_funcall(klass, id_cast, 1, self);
1830
- return rb_funcall(v, id_pow, 1, other);
1831
- }
1832
- }
1833
-
1834
- static void iter_scomplex_minus(na_loop_t* const lp) {
1835
- size_t i, n;
1836
- char *p1, *p2;
1837
- ssize_t s1, s2;
1838
- size_t *idx1, *idx2;
1839
- dtype x;
1840
-
1841
- INIT_COUNTER(lp, n);
1842
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
1843
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
1844
-
1845
- if (idx1) {
1846
- if (idx2) {
1847
- for (i = 0; i < n; i++) {
1848
- GET_DATA_INDEX(p1, idx1, dtype, x);
1849
- x = m_minus(x);
1850
- SET_DATA_INDEX(p2, idx2, dtype, x);
1851
- }
1852
- } else {
1853
- for (i = 0; i < n; i++) {
1854
- GET_DATA_INDEX(p1, idx1, dtype, x);
1855
- x = m_minus(x);
1856
- SET_DATA_STRIDE(p2, s2, dtype, x);
1857
- }
1858
- }
1859
- } else {
1860
- if (idx2) {
1861
- for (i = 0; i < n; i++) {
1862
- GET_DATA_STRIDE(p1, s1, dtype, x);
1863
- x = m_minus(x);
1864
- SET_DATA_INDEX(p2, idx2, dtype, x);
1865
- }
1866
- } else {
1867
- //
1868
- if (is_aligned(p1, sizeof(dtype)) && is_aligned(p2, sizeof(dtype))) {
1869
- if (s1 == sizeof(dtype) && s2 == sizeof(dtype)) {
1870
- for (i = 0; i < n; i++) {
1871
- ((dtype*)p2)[i] = m_minus(((dtype*)p1)[i]);
1872
- }
1873
- return;
1874
- }
1875
- if (is_aligned_step(s1, sizeof(dtype)) && is_aligned_step(s2, sizeof(dtype))) {
1876
- //
1877
- for (i = 0; i < n; i++) {
1878
- *(dtype*)p2 = m_minus(*(dtype*)p1);
1879
- p1 += s1;
1880
- p2 += s2;
1881
- }
1882
- return;
1883
- //
1884
- }
1885
- }
1886
- for (i = 0; i < n; i++) {
1887
- GET_DATA_STRIDE(p1, s1, dtype, x);
1888
- x = m_minus(x);
1889
- SET_DATA_STRIDE(p2, s2, dtype, x);
1890
- }
1891
- //
1892
- }
1893
- }
1894
- }
1895
-
1896
- /*
1897
- Unary minus.
1898
- @overload -@
1899
- @return [Numo::SComplex] minus of self.
1900
- */
1901
- static VALUE scomplex_minus(VALUE self) {
1902
- ndfunc_arg_in_t ain[1] = { { cT, 0 } };
1903
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
1904
- ndfunc_t ndf = { iter_scomplex_minus, FULL_LOOP, 1, 1, ain, aout };
1905
-
1906
- return na_ndloop(&ndf, 1, self);
1907
- }
1908
-
1909
- static void iter_scomplex_reciprocal(na_loop_t* const lp) {
1910
- size_t i, n;
1911
- char *p1, *p2;
1912
- ssize_t s1, s2;
1913
- size_t *idx1, *idx2;
1914
- dtype x;
1915
-
1916
- INIT_COUNTER(lp, n);
1917
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
1918
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
1919
-
1920
- if (idx1) {
1921
- if (idx2) {
1922
- for (i = 0; i < n; i++) {
1923
- GET_DATA_INDEX(p1, idx1, dtype, x);
1924
- x = m_reciprocal(x);
1925
- SET_DATA_INDEX(p2, idx2, dtype, x);
1926
- }
1927
- } else {
1928
- for (i = 0; i < n; i++) {
1929
- GET_DATA_INDEX(p1, idx1, dtype, x);
1930
- x = m_reciprocal(x);
1931
- SET_DATA_STRIDE(p2, s2, dtype, x);
1932
- }
1933
- }
1934
- } else {
1935
- if (idx2) {
1936
- for (i = 0; i < n; i++) {
1937
- GET_DATA_STRIDE(p1, s1, dtype, x);
1938
- x = m_reciprocal(x);
1939
- SET_DATA_INDEX(p2, idx2, dtype, x);
1940
- }
1941
- } else {
1942
- //
1943
- if (is_aligned(p1, sizeof(dtype)) && is_aligned(p2, sizeof(dtype))) {
1944
- if (s1 == sizeof(dtype) && s2 == sizeof(dtype)) {
1945
- for (i = 0; i < n; i++) {
1946
- ((dtype*)p2)[i] = m_reciprocal(((dtype*)p1)[i]);
1947
- }
1948
- return;
1949
- }
1950
- if (is_aligned_step(s1, sizeof(dtype)) && is_aligned_step(s2, sizeof(dtype))) {
1951
- //
1952
- for (i = 0; i < n; i++) {
1953
- *(dtype*)p2 = m_reciprocal(*(dtype*)p1);
1954
- p1 += s1;
1955
- p2 += s2;
1956
- }
1957
- return;
1958
- //
1959
- }
1960
- }
1961
- for (i = 0; i < n; i++) {
1962
- GET_DATA_STRIDE(p1, s1, dtype, x);
1963
- x = m_reciprocal(x);
1964
- SET_DATA_STRIDE(p2, s2, dtype, x);
1965
- }
1966
- //
1967
- }
1968
- }
1969
- }
1970
-
1971
- /*
1972
- Unary reciprocal.
1973
- @overload reciprocal
1974
- @return [Numo::SComplex] reciprocal of self.
1975
- */
1976
- static VALUE scomplex_reciprocal(VALUE self) {
1977
- ndfunc_arg_in_t ain[1] = { { cT, 0 } };
1978
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
1979
- ndfunc_t ndf = { iter_scomplex_reciprocal, FULL_LOOP, 1, 1, ain, aout };
1980
-
1981
- return na_ndloop(&ndf, 1, self);
1982
- }
1983
-
1984
- static void iter_scomplex_sign(na_loop_t* const lp) {
1985
- size_t i, n;
1986
- char *p1, *p2;
1987
- ssize_t s1, s2;
1988
- size_t *idx1, *idx2;
1989
- dtype x;
1990
-
1991
- INIT_COUNTER(lp, n);
1992
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
1993
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
1994
-
1995
- if (idx1) {
1996
- if (idx2) {
1997
- for (i = 0; i < n; i++) {
1998
- GET_DATA_INDEX(p1, idx1, dtype, x);
1999
- x = m_sign(x);
2000
- SET_DATA_INDEX(p2, idx2, dtype, x);
2001
- }
2002
- } else {
2003
- for (i = 0; i < n; i++) {
2004
- GET_DATA_INDEX(p1, idx1, dtype, x);
2005
- x = m_sign(x);
2006
- SET_DATA_STRIDE(p2, s2, dtype, x);
2007
- }
2008
- }
2009
- } else {
2010
- if (idx2) {
2011
- for (i = 0; i < n; i++) {
2012
- GET_DATA_STRIDE(p1, s1, dtype, x);
2013
- x = m_sign(x);
2014
- SET_DATA_INDEX(p2, idx2, dtype, x);
2015
- }
2016
- } else {
2017
- //
2018
- if (is_aligned(p1, sizeof(dtype)) && is_aligned(p2, sizeof(dtype))) {
2019
- if (s1 == sizeof(dtype) && s2 == sizeof(dtype)) {
2020
- for (i = 0; i < n; i++) {
2021
- ((dtype*)p2)[i] = m_sign(((dtype*)p1)[i]);
2022
- }
2023
- return;
2024
- }
2025
- if (is_aligned_step(s1, sizeof(dtype)) && is_aligned_step(s2, sizeof(dtype))) {
2026
- //
2027
- for (i = 0; i < n; i++) {
2028
- *(dtype*)p2 = m_sign(*(dtype*)p1);
2029
- p1 += s1;
2030
- p2 += s2;
2031
- }
2032
- return;
2033
- //
2034
- }
2035
- }
2036
- for (i = 0; i < n; i++) {
2037
- GET_DATA_STRIDE(p1, s1, dtype, x);
2038
- x = m_sign(x);
2039
- SET_DATA_STRIDE(p2, s2, dtype, x);
2040
- }
2041
- //
2042
- }
2043
- }
2044
- }
2045
-
2046
- /*
2047
- Unary sign.
2048
- @overload sign
2049
- @return [Numo::SComplex] sign of self.
2050
- */
2051
- static VALUE scomplex_sign(VALUE self) {
2052
- ndfunc_arg_in_t ain[1] = { { cT, 0 } };
2053
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
2054
- ndfunc_t ndf = { iter_scomplex_sign, FULL_LOOP, 1, 1, ain, aout };
2055
-
2056
- return na_ndloop(&ndf, 1, self);
2057
- }
2058
-
2059
- static void iter_scomplex_square(na_loop_t* const lp) {
2060
- size_t i, n;
2061
- char *p1, *p2;
2062
- ssize_t s1, s2;
2063
- size_t *idx1, *idx2;
2064
- dtype x;
2065
-
2066
- INIT_COUNTER(lp, n);
2067
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
2068
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
2069
-
2070
- if (idx1) {
2071
- if (idx2) {
2072
- for (i = 0; i < n; i++) {
2073
- GET_DATA_INDEX(p1, idx1, dtype, x);
2074
- x = m_square(x);
2075
- SET_DATA_INDEX(p2, idx2, dtype, x);
2076
- }
2077
- } else {
2078
- for (i = 0; i < n; i++) {
2079
- GET_DATA_INDEX(p1, idx1, dtype, x);
2080
- x = m_square(x);
2081
- SET_DATA_STRIDE(p2, s2, dtype, x);
2082
- }
2083
- }
2084
- } else {
2085
- if (idx2) {
2086
- for (i = 0; i < n; i++) {
2087
- GET_DATA_STRIDE(p1, s1, dtype, x);
2088
- x = m_square(x);
2089
- SET_DATA_INDEX(p2, idx2, dtype, x);
2090
- }
2091
- } else {
2092
- //
2093
- if (is_aligned(p1, sizeof(dtype)) && is_aligned(p2, sizeof(dtype))) {
2094
- if (s1 == sizeof(dtype) && s2 == sizeof(dtype)) {
2095
- for (i = 0; i < n; i++) {
2096
- ((dtype*)p2)[i] = m_square(((dtype*)p1)[i]);
2097
- }
2098
- return;
2099
- }
2100
- if (is_aligned_step(s1, sizeof(dtype)) && is_aligned_step(s2, sizeof(dtype))) {
2101
- //
2102
- for (i = 0; i < n; i++) {
2103
- *(dtype*)p2 = m_square(*(dtype*)p1);
2104
- p1 += s1;
2105
- p2 += s2;
2106
- }
2107
- return;
2108
- //
2109
- }
2110
- }
2111
- for (i = 0; i < n; i++) {
2112
- GET_DATA_STRIDE(p1, s1, dtype, x);
2113
- x = m_square(x);
2114
- SET_DATA_STRIDE(p2, s2, dtype, x);
2115
- }
2116
- //
2117
- }
2118
- }
2119
- }
2120
-
2121
- /*
2122
- Unary square.
2123
- @overload square
2124
- @return [Numo::SComplex] square of self.
2125
- */
2126
- static VALUE scomplex_square(VALUE self) {
2127
- ndfunc_arg_in_t ain[1] = { { cT, 0 } };
2128
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
2129
- ndfunc_t ndf = { iter_scomplex_square, FULL_LOOP, 1, 1, ain, aout };
2130
-
2131
- return na_ndloop(&ndf, 1, self);
2132
- }
2133
-
2134
1778
  static void iter_scomplex_conj(na_loop_t* const lp) {
2135
1779
  size_t i, n;
2136
1780
  char *p1, *p2;
@@ -2667,78 +2311,6 @@ static VALUE scomplex_copysign(VALUE self, VALUE other) {
2667
2311
  }
2668
2312
  }
2669
2313
 
2670
- typedef struct {
2671
- dtype mu;
2672
- rtype sigma;
2673
- } randn_opt_t;
2674
-
2675
- static void iter_scomplex_rand_norm(na_loop_t* const lp) {
2676
- size_t i;
2677
- char* p1;
2678
- ssize_t s1;
2679
- size_t* idx1;
2680
-
2681
- dtype* a0;
2682
-
2683
- dtype mu;
2684
- rtype sigma;
2685
- randn_opt_t* g;
2686
-
2687
- INIT_COUNTER(lp, i);
2688
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
2689
- g = (randn_opt_t*)(lp->opt_ptr);
2690
- mu = g->mu;
2691
- sigma = g->sigma;
2692
-
2693
- if (idx1) {
2694
-
2695
- for (; i--;) {
2696
- a0 = (dtype*)(p1 + *idx1);
2697
- m_rand_norm(mu, sigma, a0);
2698
- idx1 += 1;
2699
- }
2700
-
2701
- } else {
2702
-
2703
- for (; i--;) {
2704
- a0 = (dtype*)(p1);
2705
- m_rand_norm(mu, sigma, a0);
2706
- p1 += s1;
2707
- }
2708
- }
2709
- }
2710
-
2711
- /*
2712
- Generates random numbers from the normal distribution on self narray
2713
- using Box-Muller Transformation.
2714
-
2715
- @overload rand_norm([mu,[sigma]])
2716
- @param [Numeric] mu mean of normal distribution. (default=0)
2717
- @param [Numeric] sigma standard deviation of normal distribution. (default=1)
2718
- @return [Numo::SComplex] self.
2719
- */
2720
- static VALUE scomplex_rand_norm(int argc, VALUE* argv, VALUE self) {
2721
- int n;
2722
- randn_opt_t g;
2723
- VALUE v1 = Qnil, v2 = Qnil;
2724
- ndfunc_arg_in_t ain[1] = { { OVERWRITE, 0 } };
2725
- ndfunc_t ndf = { iter_scomplex_rand_norm, FULL_LOOP, 1, 0, ain, 0 };
2726
-
2727
- n = rb_scan_args(argc, argv, "02", &v1, &v2);
2728
- if (n == 0) {
2729
- g.mu = m_zero;
2730
- } else {
2731
- g.mu = m_num_to_data(v1);
2732
- }
2733
- if (n == 2) {
2734
- g.sigma = NUM2DBL(v2);
2735
- } else {
2736
- g.sigma = 1;
2737
- }
2738
- na_ndloop3(&ndf, &g, 1, self);
2739
- return self;
2740
- }
2741
-
2742
2314
  static void iter_scomplex_poly(na_loop_t* const lp) {
2743
2315
  size_t i;
2744
2316
  dtype x, y, a;
@@ -2934,11 +2506,37 @@ void Init_numo_scomplex(void) {
2934
2506
  * @return [Numo::NArray] self / other
2935
2507
  */
2936
2508
  rb_define_method(cT, "/", scomplex_div, 1);
2509
+ /**
2510
+ * Binary power.
2511
+ * @overload ** other
2512
+ * @param [Numo::NArray,Numeric] other
2513
+ * @return [Numo::NArray] self to the other-th power.
2514
+ */
2937
2515
  rb_define_method(cT, "**", scomplex_pow, 1);
2938
2516
  rb_define_alias(cT, "pow", "**");
2517
+ /**
2518
+ * Unary minus.
2519
+ * @overload -@
2520
+ * @return [Numo::SComplex] minus of self.
2521
+ */
2939
2522
  rb_define_method(cT, "-@", scomplex_minus, 0);
2523
+ /**
2524
+ * Unary reciprocal.
2525
+ * @overload reciprocal
2526
+ * @return [Numo::SComplex] reciprocal of self.
2527
+ */
2940
2528
  rb_define_method(cT, "reciprocal", scomplex_reciprocal, 0);
2529
+ /**
2530
+ * Unary sign.
2531
+ * @overload sign
2532
+ * @return [Numo::SComplex] sign of self.
2533
+ */
2941
2534
  rb_define_method(cT, "sign", scomplex_sign, 0);
2535
+ /**
2536
+ * Unary square.
2537
+ * @overload square
2538
+ * @return [Numo::SComplex] square of self.
2539
+ */
2942
2540
  rb_define_method(cT, "square", scomplex_square, 0);
2943
2541
  rb_define_method(cT, "conj", scomplex_conj, 0);
2944
2542
  rb_define_method(cT, "im", scomplex_im, 0);
@@ -3201,6 +2799,15 @@ void Init_numo_scomplex(void) {
3201
2799
  * # [4, 3, 3, 2, 4, 2]
3202
2800
  */
3203
2801
  rb_define_method(cT, "rand", scomplex_rand, -1);
2802
+ /**
2803
+ * Generates random numbers from the normal distribution on self narray
2804
+ * using Box-Muller Transformation.
2805
+ *
2806
+ * @overload rand_norm([mu,[sigma]])
2807
+ * @param [Numeric] mu mean of normal distribution. (default=0)
2808
+ * @param [Numeric] sigma standard deviation of normal distribution. (default=1)
2809
+ * @return [Numo::SComplex] self.
2810
+ */
3204
2811
  rb_define_method(cT, "rand_norm", scomplex_rand_norm, -1);
3205
2812
  rb_define_method(cT, "poly", scomplex_poly, -2);
3206
2813
  rb_define_singleton_method(cT, "[]", scomplex_s_cast, -2);