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.
@@ -54,6 +54,11 @@ extern VALUE cRT;
54
54
  #include "mh/op/div.h"
55
55
  #include "mh/op/mod.h"
56
56
  #include "mh/divmod.h"
57
+ #include "mh/pow.h"
58
+ #include "mh/minus.h"
59
+ #include "mh/reciprocal.h"
60
+ #include "mh/sign.h"
61
+ #include "mh/square.h"
57
62
  #include "mh/round/floor.h"
58
63
  #include "mh/round/round.h"
59
64
  #include "mh/round/ceil.h"
@@ -95,6 +100,7 @@ extern VALUE cRT;
95
100
  #include "mh/logseq.h"
96
101
  #include "mh/eye.h"
97
102
  #include "mh/rand.h"
103
+ #include "mh/rand_norm.h"
98
104
  #include "mh/math/sqrt.h"
99
105
  #include "mh/math/cbrt.h"
100
106
  #include "mh/math/log.h"
@@ -146,6 +152,11 @@ DEF_NARRAY_FLT_DIV_METHOD_FUNC(dfloat, numo_cDFloat)
146
152
  #endif
147
153
  DEF_NARRAY_FLT_MOD_METHOD_FUNC(dfloat, numo_cDFloat)
148
154
  DEF_NARRAY_FLT_DIVMOD_METHOD_FUNC(dfloat, numo_cDFloat)
155
+ DEF_NARRAY_POW_METHOD_FUNC(dfloat, numo_cDFloat)
156
+ DEF_NARRAY_MINUS_METHOD_FUNC(dfloat, numo_cDFloat)
157
+ DEF_NARRAY_RECIPROCAL_METHOD_FUNC(dfloat, numo_cDFloat)
158
+ DEF_NARRAY_SIGN_METHOD_FUNC(dfloat, numo_cDFloat)
159
+ DEF_NARRAY_SQUARE_METHOD_FUNC(dfloat, numo_cDFloat)
149
160
  DEF_NARRAY_FLT_FLOOR_METHOD_FUNC(dfloat, numo_cDFloat)
150
161
  DEF_NARRAY_FLT_ROUND_METHOD_FUNC(dfloat, numo_cDFloat)
151
162
  DEF_NARRAY_FLT_CEIL_METHOD_FUNC(dfloat, numo_cDFloat)
@@ -187,6 +198,7 @@ DEF_NARRAY_FLT_SEQ_METHOD_FUNC(dfloat)
187
198
  DEF_NARRAY_FLT_LOGSEQ_METHOD_FUNC(dfloat)
188
199
  DEF_NARRAY_EYE_METHOD_FUNC(dfloat)
189
200
  DEF_NARRAY_FLT_RAND_METHOD_FUNC(dfloat)
201
+ DEF_NARRAY_FLT_RAND_NORM_METHOD_FUNC(dfloat)
190
202
  #ifdef __SSE2__
191
203
  DEF_NARRAY_FLT_SQRT_SSE2_DBL_METHOD_FUNC(dfloat, numo_cDFloat)
192
204
  #else
@@ -1622,348 +1634,6 @@ static VALUE dfloat_abs(VALUE self) {
1622
1634
  return na_ndloop(&ndf, 1, self);
1623
1635
  }
1624
1636
 
1625
- static void iter_dfloat_pow(na_loop_t* const lp) {
1626
- size_t i;
1627
- char *p1, *p2, *p3;
1628
- ssize_t s1, s2, s3;
1629
- dtype x, y;
1630
- INIT_COUNTER(lp, i);
1631
- INIT_PTR(lp, 0, p1, s1);
1632
- INIT_PTR(lp, 1, p2, s2);
1633
- INIT_PTR(lp, 2, p3, s3);
1634
- for (; i--;) {
1635
- GET_DATA_STRIDE(p1, s1, dtype, x);
1636
- GET_DATA_STRIDE(p2, s2, dtype, y);
1637
- x = m_pow(x, y);
1638
- SET_DATA_STRIDE(p3, s3, dtype, x);
1639
- }
1640
- }
1641
-
1642
- static void iter_dfloat_pow_int32(na_loop_t* const lp) {
1643
- size_t i;
1644
- char *p1, *p2, *p3;
1645
- ssize_t s1, s2, s3;
1646
- dtype x;
1647
- int32_t y;
1648
- INIT_COUNTER(lp, i);
1649
- INIT_PTR(lp, 0, p1, s1);
1650
- INIT_PTR(lp, 1, p2, s2);
1651
- INIT_PTR(lp, 2, p3, s3);
1652
- for (; i--;) {
1653
- GET_DATA_STRIDE(p1, s1, dtype, x);
1654
- GET_DATA_STRIDE(p2, s2, int32_t, y);
1655
- x = m_pow_int(x, y);
1656
- SET_DATA_STRIDE(p3, s3, dtype, x);
1657
- }
1658
- }
1659
-
1660
- static VALUE dfloat_pow_self(VALUE self, VALUE other) {
1661
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
1662
- ndfunc_arg_in_t ain_i[2] = { { cT, 0 }, { numo_cInt32, 0 } };
1663
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
1664
- ndfunc_t ndf = { iter_dfloat_pow, STRIDE_LOOP, 2, 1, ain, aout };
1665
- ndfunc_t ndf_i = { iter_dfloat_pow_int32, STRIDE_LOOP, 2, 1, ain_i, aout };
1666
-
1667
- // fixme : use na.integer?
1668
- if (FIXNUM_P(other) || rb_obj_is_kind_of(other, numo_cInt32)) {
1669
- return na_ndloop(&ndf_i, 2, self, other);
1670
- } else {
1671
- return na_ndloop(&ndf, 2, self, other);
1672
- }
1673
- }
1674
-
1675
- static VALUE dfloat_pow(VALUE self, VALUE other) {
1676
-
1677
- VALUE klass, v;
1678
- klass = na_upcast(rb_obj_class(self), rb_obj_class(other));
1679
- if (klass == cT) {
1680
- return dfloat_pow_self(self, other);
1681
- } else {
1682
- v = rb_funcall(klass, id_cast, 1, self);
1683
- return rb_funcall(v, id_pow, 1, other);
1684
- }
1685
- }
1686
-
1687
- static void iter_dfloat_minus(na_loop_t* const lp) {
1688
- size_t i, n;
1689
- char *p1, *p2;
1690
- ssize_t s1, s2;
1691
- size_t *idx1, *idx2;
1692
- dtype x;
1693
-
1694
- INIT_COUNTER(lp, n);
1695
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
1696
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
1697
-
1698
- if (idx1) {
1699
- if (idx2) {
1700
- for (i = 0; i < n; i++) {
1701
- GET_DATA_INDEX(p1, idx1, dtype, x);
1702
- x = m_minus(x);
1703
- SET_DATA_INDEX(p2, idx2, dtype, x);
1704
- }
1705
- } else {
1706
- for (i = 0; i < n; i++) {
1707
- GET_DATA_INDEX(p1, idx1, dtype, x);
1708
- x = m_minus(x);
1709
- SET_DATA_STRIDE(p2, s2, dtype, x);
1710
- }
1711
- }
1712
- } else {
1713
- if (idx2) {
1714
- for (i = 0; i < n; i++) {
1715
- GET_DATA_STRIDE(p1, s1, dtype, x);
1716
- x = m_minus(x);
1717
- SET_DATA_INDEX(p2, idx2, dtype, x);
1718
- }
1719
- } else {
1720
- //
1721
- if (is_aligned(p1, sizeof(dtype)) && is_aligned(p2, sizeof(dtype))) {
1722
- if (s1 == sizeof(dtype) && s2 == sizeof(dtype)) {
1723
- for (i = 0; i < n; i++) {
1724
- ((dtype*)p2)[i] = m_minus(((dtype*)p1)[i]);
1725
- }
1726
- return;
1727
- }
1728
- if (is_aligned_step(s1, sizeof(dtype)) && is_aligned_step(s2, sizeof(dtype))) {
1729
- //
1730
- for (i = 0; i < n; i++) {
1731
- *(dtype*)p2 = m_minus(*(dtype*)p1);
1732
- p1 += s1;
1733
- p2 += s2;
1734
- }
1735
- return;
1736
- //
1737
- }
1738
- }
1739
- for (i = 0; i < n; i++) {
1740
- GET_DATA_STRIDE(p1, s1, dtype, x);
1741
- x = m_minus(x);
1742
- SET_DATA_STRIDE(p2, s2, dtype, x);
1743
- }
1744
- //
1745
- }
1746
- }
1747
- }
1748
-
1749
- static VALUE dfloat_minus(VALUE self) {
1750
- ndfunc_arg_in_t ain[1] = { { cT, 0 } };
1751
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
1752
- ndfunc_t ndf = { iter_dfloat_minus, FULL_LOOP, 1, 1, ain, aout };
1753
-
1754
- return na_ndloop(&ndf, 1, self);
1755
- }
1756
-
1757
- static void iter_dfloat_reciprocal(na_loop_t* const lp) {
1758
- size_t i, n;
1759
- char *p1, *p2;
1760
- ssize_t s1, s2;
1761
- size_t *idx1, *idx2;
1762
- dtype x;
1763
-
1764
- INIT_COUNTER(lp, n);
1765
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
1766
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
1767
-
1768
- if (idx1) {
1769
- if (idx2) {
1770
- for (i = 0; i < n; i++) {
1771
- GET_DATA_INDEX(p1, idx1, dtype, x);
1772
- x = m_reciprocal(x);
1773
- SET_DATA_INDEX(p2, idx2, dtype, x);
1774
- }
1775
- } else {
1776
- for (i = 0; i < n; i++) {
1777
- GET_DATA_INDEX(p1, idx1, dtype, x);
1778
- x = m_reciprocal(x);
1779
- SET_DATA_STRIDE(p2, s2, dtype, x);
1780
- }
1781
- }
1782
- } else {
1783
- if (idx2) {
1784
- for (i = 0; i < n; i++) {
1785
- GET_DATA_STRIDE(p1, s1, dtype, x);
1786
- x = m_reciprocal(x);
1787
- SET_DATA_INDEX(p2, idx2, dtype, x);
1788
- }
1789
- } else {
1790
- //
1791
- if (is_aligned(p1, sizeof(dtype)) && is_aligned(p2, sizeof(dtype))) {
1792
- if (s1 == sizeof(dtype) && s2 == sizeof(dtype)) {
1793
- for (i = 0; i < n; i++) {
1794
- ((dtype*)p2)[i] = m_reciprocal(((dtype*)p1)[i]);
1795
- }
1796
- return;
1797
- }
1798
- if (is_aligned_step(s1, sizeof(dtype)) && is_aligned_step(s2, sizeof(dtype))) {
1799
- //
1800
- for (i = 0; i < n; i++) {
1801
- *(dtype*)p2 = m_reciprocal(*(dtype*)p1);
1802
- p1 += s1;
1803
- p2 += s2;
1804
- }
1805
- return;
1806
- //
1807
- }
1808
- }
1809
- for (i = 0; i < n; i++) {
1810
- GET_DATA_STRIDE(p1, s1, dtype, x);
1811
- x = m_reciprocal(x);
1812
- SET_DATA_STRIDE(p2, s2, dtype, x);
1813
- }
1814
- //
1815
- }
1816
- }
1817
- }
1818
-
1819
- static VALUE dfloat_reciprocal(VALUE self) {
1820
- ndfunc_arg_in_t ain[1] = { { cT, 0 } };
1821
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
1822
- ndfunc_t ndf = { iter_dfloat_reciprocal, FULL_LOOP, 1, 1, ain, aout };
1823
-
1824
- return na_ndloop(&ndf, 1, self);
1825
- }
1826
-
1827
- static void iter_dfloat_sign(na_loop_t* const lp) {
1828
- size_t i, n;
1829
- char *p1, *p2;
1830
- ssize_t s1, s2;
1831
- size_t *idx1, *idx2;
1832
- dtype x;
1833
-
1834
- INIT_COUNTER(lp, n);
1835
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
1836
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
1837
-
1838
- if (idx1) {
1839
- if (idx2) {
1840
- for (i = 0; i < n; i++) {
1841
- GET_DATA_INDEX(p1, idx1, dtype, x);
1842
- x = m_sign(x);
1843
- SET_DATA_INDEX(p2, idx2, dtype, x);
1844
- }
1845
- } else {
1846
- for (i = 0; i < n; i++) {
1847
- GET_DATA_INDEX(p1, idx1, dtype, x);
1848
- x = m_sign(x);
1849
- SET_DATA_STRIDE(p2, s2, dtype, x);
1850
- }
1851
- }
1852
- } else {
1853
- if (idx2) {
1854
- for (i = 0; i < n; i++) {
1855
- GET_DATA_STRIDE(p1, s1, dtype, x);
1856
- x = m_sign(x);
1857
- SET_DATA_INDEX(p2, idx2, dtype, x);
1858
- }
1859
- } else {
1860
- //
1861
- if (is_aligned(p1, sizeof(dtype)) && is_aligned(p2, sizeof(dtype))) {
1862
- if (s1 == sizeof(dtype) && s2 == sizeof(dtype)) {
1863
- for (i = 0; i < n; i++) {
1864
- ((dtype*)p2)[i] = m_sign(((dtype*)p1)[i]);
1865
- }
1866
- return;
1867
- }
1868
- if (is_aligned_step(s1, sizeof(dtype)) && is_aligned_step(s2, sizeof(dtype))) {
1869
- //
1870
- for (i = 0; i < n; i++) {
1871
- *(dtype*)p2 = m_sign(*(dtype*)p1);
1872
- p1 += s1;
1873
- p2 += s2;
1874
- }
1875
- return;
1876
- //
1877
- }
1878
- }
1879
- for (i = 0; i < n; i++) {
1880
- GET_DATA_STRIDE(p1, s1, dtype, x);
1881
- x = m_sign(x);
1882
- SET_DATA_STRIDE(p2, s2, dtype, x);
1883
- }
1884
- //
1885
- }
1886
- }
1887
- }
1888
-
1889
- static VALUE dfloat_sign(VALUE self) {
1890
- ndfunc_arg_in_t ain[1] = { { cT, 0 } };
1891
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
1892
- ndfunc_t ndf = { iter_dfloat_sign, FULL_LOOP, 1, 1, ain, aout };
1893
-
1894
- return na_ndloop(&ndf, 1, self);
1895
- }
1896
-
1897
- static void iter_dfloat_square(na_loop_t* const lp) {
1898
- size_t i, n;
1899
- char *p1, *p2;
1900
- ssize_t s1, s2;
1901
- size_t *idx1, *idx2;
1902
- dtype x;
1903
-
1904
- INIT_COUNTER(lp, n);
1905
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
1906
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
1907
-
1908
- if (idx1) {
1909
- if (idx2) {
1910
- for (i = 0; i < n; i++) {
1911
- GET_DATA_INDEX(p1, idx1, dtype, x);
1912
- x = m_square(x);
1913
- SET_DATA_INDEX(p2, idx2, dtype, x);
1914
- }
1915
- } else {
1916
- for (i = 0; i < n; i++) {
1917
- GET_DATA_INDEX(p1, idx1, dtype, x);
1918
- x = m_square(x);
1919
- SET_DATA_STRIDE(p2, s2, dtype, x);
1920
- }
1921
- }
1922
- } else {
1923
- if (idx2) {
1924
- for (i = 0; i < n; i++) {
1925
- GET_DATA_STRIDE(p1, s1, dtype, x);
1926
- x = m_square(x);
1927
- SET_DATA_INDEX(p2, idx2, dtype, x);
1928
- }
1929
- } else {
1930
- //
1931
- if (is_aligned(p1, sizeof(dtype)) && is_aligned(p2, sizeof(dtype))) {
1932
- if (s1 == sizeof(dtype) && s2 == sizeof(dtype)) {
1933
- for (i = 0; i < n; i++) {
1934
- ((dtype*)p2)[i] = m_square(((dtype*)p1)[i]);
1935
- }
1936
- return;
1937
- }
1938
- if (is_aligned_step(s1, sizeof(dtype)) && is_aligned_step(s2, sizeof(dtype))) {
1939
- //
1940
- for (i = 0; i < n; i++) {
1941
- *(dtype*)p2 = m_square(*(dtype*)p1);
1942
- p1 += s1;
1943
- p2 += s2;
1944
- }
1945
- return;
1946
- //
1947
- }
1948
- }
1949
- for (i = 0; i < n; i++) {
1950
- GET_DATA_STRIDE(p1, s1, dtype, x);
1951
- x = m_square(x);
1952
- SET_DATA_STRIDE(p2, s2, dtype, x);
1953
- }
1954
- //
1955
- }
1956
- }
1957
- }
1958
-
1959
- static VALUE dfloat_square(VALUE self) {
1960
- ndfunc_arg_in_t ain[1] = { { cT, 0 } };
1961
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
1962
- ndfunc_t ndf = { iter_dfloat_square, FULL_LOOP, 1, 1, ain, aout };
1963
-
1964
- return na_ndloop(&ndf, 1, self);
1965
- }
1966
-
1967
1637
  #define check_intdivzero(y) \
1968
1638
  {}
1969
1639
 
@@ -2174,79 +1844,6 @@ static VALUE dfloat_kahan_sum(int argc, VALUE* argv, VALUE self) {
2174
1844
  return dfloat_extract(v);
2175
1845
  }
2176
1846
 
2177
- typedef struct {
2178
- dtype mu;
2179
- rtype sigma;
2180
- } randn_opt_t;
2181
-
2182
- static void iter_dfloat_rand_norm(na_loop_t* const lp) {
2183
- size_t i;
2184
- char* p1;
2185
- ssize_t s1;
2186
- size_t* idx1;
2187
-
2188
- dtype *a0, *a1;
2189
-
2190
- dtype mu;
2191
- rtype sigma;
2192
- randn_opt_t* g;
2193
-
2194
- INIT_COUNTER(lp, i);
2195
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
2196
- g = (randn_opt_t*)(lp->opt_ptr);
2197
- mu = g->mu;
2198
- sigma = g->sigma;
2199
-
2200
- if (idx1) {
2201
-
2202
- for (; i > 1; i -= 2) {
2203
- a0 = (dtype*)(p1 + *idx1);
2204
- a1 = (dtype*)(p1 + *(idx1 + 1));
2205
- m_rand_norm(mu, sigma, a0, a1);
2206
- idx1 += 2;
2207
- }
2208
- if (i > 0) {
2209
- a0 = (dtype*)(p1 + *idx1);
2210
- m_rand_norm(mu, sigma, a0, 0);
2211
- }
2212
-
2213
- } else {
2214
-
2215
- for (; i > 1; i -= 2) {
2216
- a0 = (dtype*)(p1);
2217
- a1 = (dtype*)(p1 + s1);
2218
- m_rand_norm(mu, sigma, a0, a1);
2219
- p1 += s1 * 2;
2220
- }
2221
- if (i > 0) {
2222
- a0 = (dtype*)(p1);
2223
- m_rand_norm(mu, sigma, a0, 0);
2224
- }
2225
- }
2226
- }
2227
-
2228
- static VALUE dfloat_rand_norm(int argc, VALUE* args, VALUE self) {
2229
- int n;
2230
- randn_opt_t g;
2231
- VALUE v1 = Qnil, v2 = Qnil;
2232
- ndfunc_arg_in_t ain[1] = { { OVERWRITE, 0 } };
2233
- ndfunc_t ndf = { iter_dfloat_rand_norm, FULL_LOOP, 1, 0, ain, 0 };
2234
-
2235
- n = rb_scan_args(argc, args, "02", &v1, &v2);
2236
- if (n == 0) {
2237
- g.mu = m_zero;
2238
- } else {
2239
- g.mu = m_num_to_data(v1);
2240
- }
2241
- if (n == 2) {
2242
- g.sigma = NUM2DBL(v2);
2243
- } else {
2244
- g.sigma = 1;
2245
- }
2246
- na_ndloop3(&ndf, &g, 1, self);
2247
- return self;
2248
- }
2249
-
2250
1847
  static void iter_dfloat_poly(na_loop_t* const lp) {
2251
1848
  size_t i;
2252
1849
  dtype x, y, a;