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(sfloat, numo_cSFloat)
146
152
  #endif
147
153
  DEF_NARRAY_FLT_MOD_METHOD_FUNC(sfloat, numo_cSFloat)
148
154
  DEF_NARRAY_FLT_DIVMOD_METHOD_FUNC(sfloat, numo_cSFloat)
155
+ DEF_NARRAY_POW_METHOD_FUNC(sfloat, numo_cSFloat)
156
+ DEF_NARRAY_MINUS_METHOD_FUNC(sfloat, numo_cSFloat)
157
+ DEF_NARRAY_RECIPROCAL_METHOD_FUNC(sfloat, numo_cSFloat)
158
+ DEF_NARRAY_SIGN_METHOD_FUNC(sfloat, numo_cSFloat)
159
+ DEF_NARRAY_SQUARE_METHOD_FUNC(sfloat, numo_cSFloat)
149
160
  DEF_NARRAY_FLT_FLOOR_METHOD_FUNC(sfloat, numo_cSFloat)
150
161
  DEF_NARRAY_FLT_ROUND_METHOD_FUNC(sfloat, numo_cSFloat)
151
162
  DEF_NARRAY_FLT_CEIL_METHOD_FUNC(sfloat, numo_cSFloat)
@@ -187,6 +198,7 @@ DEF_NARRAY_FLT_SEQ_METHOD_FUNC(sfloat)
187
198
  DEF_NARRAY_FLT_LOGSEQ_METHOD_FUNC(sfloat)
188
199
  DEF_NARRAY_EYE_METHOD_FUNC(sfloat)
189
200
  DEF_NARRAY_FLT_RAND_METHOD_FUNC(sfloat)
201
+ DEF_NARRAY_FLT_RAND_NORM_METHOD_FUNC(sfloat)
190
202
  #ifdef __SSE2__
191
203
  DEF_NARRAY_FLT_SQRT_SSE2_SGL_METHOD_FUNC(sfloat, numo_cSFloat)
192
204
  #else
@@ -1622,348 +1634,6 @@ static VALUE sfloat_abs(VALUE self) {
1622
1634
  return na_ndloop(&ndf, 1, self);
1623
1635
  }
1624
1636
 
1625
- static void iter_sfloat_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_sfloat_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 sfloat_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_sfloat_pow, STRIDE_LOOP, 2, 1, ain, aout };
1665
- ndfunc_t ndf_i = { iter_sfloat_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 sfloat_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 sfloat_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_sfloat_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 sfloat_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_sfloat_minus, FULL_LOOP, 1, 1, ain, aout };
1753
-
1754
- return na_ndloop(&ndf, 1, self);
1755
- }
1756
-
1757
- static void iter_sfloat_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 sfloat_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_sfloat_reciprocal, FULL_LOOP, 1, 1, ain, aout };
1823
-
1824
- return na_ndloop(&ndf, 1, self);
1825
- }
1826
-
1827
- static void iter_sfloat_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 sfloat_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_sfloat_sign, FULL_LOOP, 1, 1, ain, aout };
1893
-
1894
- return na_ndloop(&ndf, 1, self);
1895
- }
1896
-
1897
- static void iter_sfloat_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 sfloat_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_sfloat_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
 
@@ -2138,79 +1808,6 @@ static VALUE sfloat_modf(VALUE self) {
2138
1808
  return na_ndloop(&ndf, 1, self);
2139
1809
  }
2140
1810
 
2141
- typedef struct {
2142
- dtype mu;
2143
- rtype sigma;
2144
- } randn_opt_t;
2145
-
2146
- static void iter_sfloat_rand_norm(na_loop_t* const lp) {
2147
- size_t i;
2148
- char* p1;
2149
- ssize_t s1;
2150
- size_t* idx1;
2151
-
2152
- dtype *a0, *a1;
2153
-
2154
- dtype mu;
2155
- rtype sigma;
2156
- randn_opt_t* g;
2157
-
2158
- INIT_COUNTER(lp, i);
2159
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
2160
- g = (randn_opt_t*)(lp->opt_ptr);
2161
- mu = g->mu;
2162
- sigma = g->sigma;
2163
-
2164
- if (idx1) {
2165
-
2166
- for (; i > 1; i -= 2) {
2167
- a0 = (dtype*)(p1 + *idx1);
2168
- a1 = (dtype*)(p1 + *(idx1 + 1));
2169
- m_rand_norm(mu, sigma, a0, a1);
2170
- idx1 += 2;
2171
- }
2172
- if (i > 0) {
2173
- a0 = (dtype*)(p1 + *idx1);
2174
- m_rand_norm(mu, sigma, a0, 0);
2175
- }
2176
-
2177
- } else {
2178
-
2179
- for (; i > 1; i -= 2) {
2180
- a0 = (dtype*)(p1);
2181
- a1 = (dtype*)(p1 + s1);
2182
- m_rand_norm(mu, sigma, a0, a1);
2183
- p1 += s1 * 2;
2184
- }
2185
- if (i > 0) {
2186
- a0 = (dtype*)(p1);
2187
- m_rand_norm(mu, sigma, a0, 0);
2188
- }
2189
- }
2190
- }
2191
-
2192
- static VALUE sfloat_rand_norm(int argc, VALUE* args, VALUE self) {
2193
- int n;
2194
- randn_opt_t g;
2195
- VALUE v1 = Qnil, v2 = Qnil;
2196
- ndfunc_arg_in_t ain[1] = { { OVERWRITE, 0 } };
2197
- ndfunc_t ndf = { iter_sfloat_rand_norm, FULL_LOOP, 1, 0, ain, 0 };
2198
-
2199
- n = rb_scan_args(argc, args, "02", &v1, &v2);
2200
- if (n == 0) {
2201
- g.mu = m_zero;
2202
- } else {
2203
- g.mu = m_num_to_data(v1);
2204
- }
2205
- if (n == 2) {
2206
- g.sigma = NUM2DBL(v2);
2207
- } else {
2208
- g.sigma = 1;
2209
- }
2210
- na_ndloop3(&ndf, &g, 1, self);
2211
- return self;
2212
- }
2213
-
2214
1811
  static void iter_sfloat_poly(na_loop_t* const lp) {
2215
1812
  size_t i;
2216
1813
  dtype x, y, a;
@@ -3870,7 +3467,7 @@ void Init_numo_sfloat(void) {
3870
3467
  rb_define_method(cT, "rand", sfloat_rand, -1);
3871
3468
  /**
3872
3469
  * Generates random numbers from the normal distribution on self narray
3873
- * using Box-Muller Transformation.
3470
+ * using Box-Muller Transformation.
3874
3471
  * @overload rand_norm([mu,[sigma]])
3875
3472
  * @param [Numeric] mu mean of normal distribution. (default=0)
3876
3473
  * @param [Numeric] sigma standard deviation of normal distribution. (default=1)