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.
@@ -55,6 +55,11 @@ extern VALUE cRT;
55
55
  #include "mh/op/div.h"
56
56
  #include "mh/op/mod.h"
57
57
  #include "mh/divmod.h"
58
+ #include "mh/pow.h"
59
+ #include "mh/minus.h"
60
+ #include "mh/reciprocal.h"
61
+ #include "mh/sign.h"
62
+ #include "mh/square.h"
58
63
  #include "mh/comp/eq.h"
59
64
  #include "mh/comp/ne.h"
60
65
  #include "mh/comp/gt.h"
@@ -107,6 +112,11 @@ DEF_NARRAY_MUL_METHOD_FUNC(int32, numo_cInt32)
107
112
  DEF_NARRAY_INT_DIV_METHOD_FUNC(int32, numo_cInt32)
108
113
  DEF_NARRAY_INT_MOD_METHOD_FUNC(int32, numo_cInt32)
109
114
  DEF_NARRAY_INT_DIVMOD_METHOD_FUNC(int32, numo_cInt32)
115
+ DEF_NARRAY_POW_METHOD_FUNC(int32, numo_cInt32)
116
+ DEF_NARRAY_MINUS_METHOD_FUNC(int32, numo_cInt32)
117
+ DEF_NARRAY_RECIPROCAL_METHOD_FUNC(int32, numo_cInt32)
118
+ DEF_NARRAY_SIGN_METHOD_FUNC(int32, numo_cInt32)
119
+ DEF_NARRAY_SQUARE_METHOD_FUNC(int32, numo_cInt32)
110
120
  DEF_NARRAY_EQ_METHOD_FUNC(int32, numo_cInt32)
111
121
  DEF_NARRAY_NE_METHOD_FUNC(int32, numo_cInt32)
112
122
  DEF_NARRAY_GT_METHOD_FUNC(int32, numo_cInt32)
@@ -1629,374 +1639,6 @@ static VALUE int32_abs(VALUE self) {
1629
1639
  return na_ndloop(&ndf, 1, self);
1630
1640
  }
1631
1641
 
1632
- static void iter_int32_pow(na_loop_t* const lp) {
1633
- size_t i;
1634
- char *p1, *p2, *p3;
1635
- ssize_t s1, s2, s3;
1636
- dtype x, y;
1637
- INIT_COUNTER(lp, i);
1638
- INIT_PTR(lp, 0, p1, s1);
1639
- INIT_PTR(lp, 1, p2, s2);
1640
- INIT_PTR(lp, 2, p3, s3);
1641
- for (; i--;) {
1642
- GET_DATA_STRIDE(p1, s1, dtype, x);
1643
- GET_DATA_STRIDE(p2, s2, dtype, y);
1644
- x = m_pow(x, y);
1645
- SET_DATA_STRIDE(p3, s3, dtype, x);
1646
- }
1647
- }
1648
-
1649
- static void iter_int32_pow_int32(na_loop_t* const lp) {
1650
- size_t i;
1651
- char *p1, *p2, *p3;
1652
- ssize_t s1, s2, s3;
1653
- dtype x;
1654
- int32_t y;
1655
- INIT_COUNTER(lp, i);
1656
- INIT_PTR(lp, 0, p1, s1);
1657
- INIT_PTR(lp, 1, p2, s2);
1658
- INIT_PTR(lp, 2, p3, s3);
1659
- for (; i--;) {
1660
- GET_DATA_STRIDE(p1, s1, dtype, x);
1661
- GET_DATA_STRIDE(p2, s2, int32_t, y);
1662
- x = m_pow_int(x, y);
1663
- SET_DATA_STRIDE(p3, s3, dtype, x);
1664
- }
1665
- }
1666
-
1667
- static VALUE int32_pow_self(VALUE self, VALUE other) {
1668
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
1669
- ndfunc_arg_in_t ain_i[2] = { { cT, 0 }, { numo_cInt32, 0 } };
1670
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
1671
- ndfunc_t ndf = { iter_int32_pow, STRIDE_LOOP, 2, 1, ain, aout };
1672
- ndfunc_t ndf_i = { iter_int32_pow_int32, STRIDE_LOOP, 2, 1, ain_i, aout };
1673
-
1674
- // fixme : use na.integer?
1675
- if (FIXNUM_P(other) || rb_obj_is_kind_of(other, numo_cInt32)) {
1676
- return na_ndloop(&ndf_i, 2, self, other);
1677
- } else {
1678
- return na_ndloop(&ndf, 2, self, other);
1679
- }
1680
- }
1681
-
1682
- /*
1683
- Binary power.
1684
- @overload ** other
1685
- @param [Numo::NArray,Numeric] other
1686
- @return [Numo::NArray] self to the other-th power.
1687
- */
1688
- static VALUE int32_pow(VALUE self, VALUE other) {
1689
-
1690
- VALUE klass, v;
1691
- klass = na_upcast(rb_obj_class(self), rb_obj_class(other));
1692
- if (klass == cT) {
1693
- return int32_pow_self(self, other);
1694
- } else {
1695
- v = rb_funcall(klass, id_cast, 1, self);
1696
- return rb_funcall(v, id_pow, 1, other);
1697
- }
1698
- }
1699
-
1700
- static void iter_int32_minus(na_loop_t* const lp) {
1701
- size_t i, n;
1702
- char *p1, *p2;
1703
- ssize_t s1, s2;
1704
- size_t *idx1, *idx2;
1705
- dtype x;
1706
-
1707
- INIT_COUNTER(lp, n);
1708
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
1709
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
1710
-
1711
- if (idx1) {
1712
- if (idx2) {
1713
- for (i = 0; i < n; i++) {
1714
- GET_DATA_INDEX(p1, idx1, dtype, x);
1715
- x = m_minus(x);
1716
- SET_DATA_INDEX(p2, idx2, dtype, x);
1717
- }
1718
- } else {
1719
- for (i = 0; i < n; i++) {
1720
- GET_DATA_INDEX(p1, idx1, dtype, x);
1721
- x = m_minus(x);
1722
- SET_DATA_STRIDE(p2, s2, dtype, x);
1723
- }
1724
- }
1725
- } else {
1726
- if (idx2) {
1727
- for (i = 0; i < n; i++) {
1728
- GET_DATA_STRIDE(p1, s1, dtype, x);
1729
- x = m_minus(x);
1730
- SET_DATA_INDEX(p2, idx2, dtype, x);
1731
- }
1732
- } else {
1733
- //
1734
- if (is_aligned(p1, sizeof(dtype)) && is_aligned(p2, sizeof(dtype))) {
1735
- if (s1 == sizeof(dtype) && s2 == sizeof(dtype)) {
1736
- for (i = 0; i < n; i++) {
1737
- ((dtype*)p2)[i] = m_minus(((dtype*)p1)[i]);
1738
- }
1739
- return;
1740
- }
1741
- if (is_aligned_step(s1, sizeof(dtype)) && is_aligned_step(s2, sizeof(dtype))) {
1742
- //
1743
- for (i = 0; i < n; i++) {
1744
- *(dtype*)p2 = m_minus(*(dtype*)p1);
1745
- p1 += s1;
1746
- p2 += s2;
1747
- }
1748
- return;
1749
- //
1750
- }
1751
- }
1752
- for (i = 0; i < n; i++) {
1753
- GET_DATA_STRIDE(p1, s1, dtype, x);
1754
- x = m_minus(x);
1755
- SET_DATA_STRIDE(p2, s2, dtype, x);
1756
- }
1757
- //
1758
- }
1759
- }
1760
- }
1761
-
1762
- /*
1763
- Unary minus.
1764
- @overload -@
1765
- @return [Numo::Int32] minus of self.
1766
- */
1767
- static VALUE int32_minus(VALUE self) {
1768
- ndfunc_arg_in_t ain[1] = { { cT, 0 } };
1769
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
1770
- ndfunc_t ndf = { iter_int32_minus, FULL_LOOP, 1, 1, ain, aout };
1771
-
1772
- return na_ndloop(&ndf, 1, self);
1773
- }
1774
-
1775
- static void iter_int32_reciprocal(na_loop_t* const lp) {
1776
- size_t i, n;
1777
- char *p1, *p2;
1778
- ssize_t s1, s2;
1779
- size_t *idx1, *idx2;
1780
- dtype x;
1781
-
1782
- INIT_COUNTER(lp, n);
1783
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
1784
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
1785
-
1786
- if (idx1) {
1787
- if (idx2) {
1788
- for (i = 0; i < n; i++) {
1789
- GET_DATA_INDEX(p1, idx1, dtype, x);
1790
- x = m_reciprocal(x);
1791
- SET_DATA_INDEX(p2, idx2, dtype, x);
1792
- }
1793
- } else {
1794
- for (i = 0; i < n; i++) {
1795
- GET_DATA_INDEX(p1, idx1, dtype, x);
1796
- x = m_reciprocal(x);
1797
- SET_DATA_STRIDE(p2, s2, dtype, x);
1798
- }
1799
- }
1800
- } else {
1801
- if (idx2) {
1802
- for (i = 0; i < n; i++) {
1803
- GET_DATA_STRIDE(p1, s1, dtype, x);
1804
- x = m_reciprocal(x);
1805
- SET_DATA_INDEX(p2, idx2, dtype, x);
1806
- }
1807
- } else {
1808
- //
1809
- if (is_aligned(p1, sizeof(dtype)) && is_aligned(p2, sizeof(dtype))) {
1810
- if (s1 == sizeof(dtype) && s2 == sizeof(dtype)) {
1811
- for (i = 0; i < n; i++) {
1812
- ((dtype*)p2)[i] = m_reciprocal(((dtype*)p1)[i]);
1813
- }
1814
- return;
1815
- }
1816
- if (is_aligned_step(s1, sizeof(dtype)) && is_aligned_step(s2, sizeof(dtype))) {
1817
- //
1818
- for (i = 0; i < n; i++) {
1819
- *(dtype*)p2 = m_reciprocal(*(dtype*)p1);
1820
- p1 += s1;
1821
- p2 += s2;
1822
- }
1823
- return;
1824
- //
1825
- }
1826
- }
1827
- for (i = 0; i < n; i++) {
1828
- GET_DATA_STRIDE(p1, s1, dtype, x);
1829
- x = m_reciprocal(x);
1830
- SET_DATA_STRIDE(p2, s2, dtype, x);
1831
- }
1832
- //
1833
- }
1834
- }
1835
- }
1836
-
1837
- /*
1838
- Unary reciprocal.
1839
- @overload reciprocal
1840
- @return [Numo::Int32] reciprocal of self.
1841
- */
1842
- static VALUE int32_reciprocal(VALUE self) {
1843
- ndfunc_arg_in_t ain[1] = { { cT, 0 } };
1844
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
1845
- ndfunc_t ndf = { iter_int32_reciprocal, FULL_LOOP, 1, 1, ain, aout };
1846
-
1847
- return na_ndloop(&ndf, 1, self);
1848
- }
1849
-
1850
- static void iter_int32_sign(na_loop_t* const lp) {
1851
- size_t i, n;
1852
- char *p1, *p2;
1853
- ssize_t s1, s2;
1854
- size_t *idx1, *idx2;
1855
- dtype x;
1856
-
1857
- INIT_COUNTER(lp, n);
1858
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
1859
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
1860
-
1861
- if (idx1) {
1862
- if (idx2) {
1863
- for (i = 0; i < n; i++) {
1864
- GET_DATA_INDEX(p1, idx1, dtype, x);
1865
- x = m_sign(x);
1866
- SET_DATA_INDEX(p2, idx2, dtype, x);
1867
- }
1868
- } else {
1869
- for (i = 0; i < n; i++) {
1870
- GET_DATA_INDEX(p1, idx1, dtype, x);
1871
- x = m_sign(x);
1872
- SET_DATA_STRIDE(p2, s2, dtype, x);
1873
- }
1874
- }
1875
- } else {
1876
- if (idx2) {
1877
- for (i = 0; i < n; i++) {
1878
- GET_DATA_STRIDE(p1, s1, dtype, x);
1879
- x = m_sign(x);
1880
- SET_DATA_INDEX(p2, idx2, dtype, x);
1881
- }
1882
- } else {
1883
- //
1884
- if (is_aligned(p1, sizeof(dtype)) && is_aligned(p2, sizeof(dtype))) {
1885
- if (s1 == sizeof(dtype) && s2 == sizeof(dtype)) {
1886
- for (i = 0; i < n; i++) {
1887
- ((dtype*)p2)[i] = m_sign(((dtype*)p1)[i]);
1888
- }
1889
- return;
1890
- }
1891
- if (is_aligned_step(s1, sizeof(dtype)) && is_aligned_step(s2, sizeof(dtype))) {
1892
- //
1893
- for (i = 0; i < n; i++) {
1894
- *(dtype*)p2 = m_sign(*(dtype*)p1);
1895
- p1 += s1;
1896
- p2 += s2;
1897
- }
1898
- return;
1899
- //
1900
- }
1901
- }
1902
- for (i = 0; i < n; i++) {
1903
- GET_DATA_STRIDE(p1, s1, dtype, x);
1904
- x = m_sign(x);
1905
- SET_DATA_STRIDE(p2, s2, dtype, x);
1906
- }
1907
- //
1908
- }
1909
- }
1910
- }
1911
-
1912
- /*
1913
- Unary sign.
1914
- @overload sign
1915
- @return [Numo::Int32] sign of self.
1916
- */
1917
- static VALUE int32_sign(VALUE self) {
1918
- ndfunc_arg_in_t ain[1] = { { cT, 0 } };
1919
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
1920
- ndfunc_t ndf = { iter_int32_sign, FULL_LOOP, 1, 1, ain, aout };
1921
-
1922
- return na_ndloop(&ndf, 1, self);
1923
- }
1924
-
1925
- static void iter_int32_square(na_loop_t* const lp) {
1926
- size_t i, n;
1927
- char *p1, *p2;
1928
- ssize_t s1, s2;
1929
- size_t *idx1, *idx2;
1930
- dtype x;
1931
-
1932
- INIT_COUNTER(lp, n);
1933
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
1934
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
1935
-
1936
- if (idx1) {
1937
- if (idx2) {
1938
- for (i = 0; i < n; i++) {
1939
- GET_DATA_INDEX(p1, idx1, dtype, x);
1940
- x = m_square(x);
1941
- SET_DATA_INDEX(p2, idx2, dtype, x);
1942
- }
1943
- } else {
1944
- for (i = 0; i < n; i++) {
1945
- GET_DATA_INDEX(p1, idx1, dtype, x);
1946
- x = m_square(x);
1947
- SET_DATA_STRIDE(p2, s2, dtype, x);
1948
- }
1949
- }
1950
- } else {
1951
- if (idx2) {
1952
- for (i = 0; i < n; i++) {
1953
- GET_DATA_STRIDE(p1, s1, dtype, x);
1954
- x = m_square(x);
1955
- SET_DATA_INDEX(p2, idx2, dtype, x);
1956
- }
1957
- } else {
1958
- //
1959
- if (is_aligned(p1, sizeof(dtype)) && is_aligned(p2, sizeof(dtype))) {
1960
- if (s1 == sizeof(dtype) && s2 == sizeof(dtype)) {
1961
- for (i = 0; i < n; i++) {
1962
- ((dtype*)p2)[i] = m_square(((dtype*)p1)[i]);
1963
- }
1964
- return;
1965
- }
1966
- if (is_aligned_step(s1, sizeof(dtype)) && is_aligned_step(s2, sizeof(dtype))) {
1967
- //
1968
- for (i = 0; i < n; i++) {
1969
- *(dtype*)p2 = m_square(*(dtype*)p1);
1970
- p1 += s1;
1971
- p2 += s2;
1972
- }
1973
- return;
1974
- //
1975
- }
1976
- }
1977
- for (i = 0; i < n; i++) {
1978
- GET_DATA_STRIDE(p1, s1, dtype, x);
1979
- x = m_square(x);
1980
- SET_DATA_STRIDE(p2, s2, dtype, x);
1981
- }
1982
- //
1983
- }
1984
- }
1985
- }
1986
-
1987
- /*
1988
- Unary square.
1989
- @overload square
1990
- @return [Numo::Int32] square of self.
1991
- */
1992
- static VALUE int32_square(VALUE self) {
1993
- ndfunc_arg_in_t ain[1] = { { cT, 0 } };
1994
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
1995
- ndfunc_t ndf = { iter_int32_square, FULL_LOOP, 1, 1, ain, aout };
1996
-
1997
- return na_ndloop(&ndf, 1, self);
1998
- }
1999
-
2000
1642
  static void iter_int32_poly(na_loop_t* const lp) {
2001
1643
  size_t i;
2002
1644
  dtype x, y, a;
@@ -2706,11 +2348,37 @@ void Init_numo_int32(void) {
2706
2348
  * @return [Numo::NArray] divmod of self and other.
2707
2349
  */
2708
2350
  rb_define_method(cT, "divmod", int32_divmod, 1);
2351
+ /**
2352
+ * Binary power.
2353
+ * @overload ** other
2354
+ * @param [Numo::NArray,Numeric] other
2355
+ * @return [Numo::NArray] self to the other-th power.
2356
+ */
2709
2357
  rb_define_method(cT, "**", int32_pow, 1);
2710
2358
  rb_define_alias(cT, "pow", "**");
2359
+ /**
2360
+ * Unary minus.
2361
+ * @overload -@
2362
+ * @return [Numo::Int32] minus of self.
2363
+ */
2711
2364
  rb_define_method(cT, "-@", int32_minus, 0);
2365
+ /**
2366
+ * Unary reciprocal.
2367
+ * @overload reciprocal
2368
+ * @return [Numo::Int32] reciprocal of self.
2369
+ */
2712
2370
  rb_define_method(cT, "reciprocal", int32_reciprocal, 0);
2371
+ /**
2372
+ * Unary sign.
2373
+ * @overload sign
2374
+ * @return [Numo::Int32] sign of self.
2375
+ */
2713
2376
  rb_define_method(cT, "sign", int32_sign, 0);
2377
+ /**
2378
+ * Unary square.
2379
+ * @overload square
2380
+ * @return [Numo::Int32] square of self.
2381
+ */
2714
2382
  rb_define_method(cT, "square", int32_square, 0);
2715
2383
  rb_define_alias(cT, "conj", "view");
2716
2384
  rb_define_alias(cT, "im", "view");