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