numo-linalg-alt 0.2.0

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.
Files changed (86) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +5 -0
  3. data/CODE_OF_CONDUCT.md +84 -0
  4. data/LICENSE.txt +27 -0
  5. data/README.md +106 -0
  6. data/ext/numo/linalg/blas/dot.c +72 -0
  7. data/ext/numo/linalg/blas/dot.h +13 -0
  8. data/ext/numo/linalg/blas/dot_sub.c +71 -0
  9. data/ext/numo/linalg/blas/dot_sub.h +13 -0
  10. data/ext/numo/linalg/blas/gemm.c +184 -0
  11. data/ext/numo/linalg/blas/gemm.h +16 -0
  12. data/ext/numo/linalg/blas/gemv.c +161 -0
  13. data/ext/numo/linalg/blas/gemv.h +16 -0
  14. data/ext/numo/linalg/blas/nrm2.c +67 -0
  15. data/ext/numo/linalg/blas/nrm2.h +13 -0
  16. data/ext/numo/linalg/converter.c +67 -0
  17. data/ext/numo/linalg/converter.h +23 -0
  18. data/ext/numo/linalg/extconf.rb +99 -0
  19. data/ext/numo/linalg/lapack/geev.c +152 -0
  20. data/ext/numo/linalg/lapack/geev.h +15 -0
  21. data/ext/numo/linalg/lapack/gelsd.c +92 -0
  22. data/ext/numo/linalg/lapack/gelsd.h +15 -0
  23. data/ext/numo/linalg/lapack/geqrf.c +72 -0
  24. data/ext/numo/linalg/lapack/geqrf.h +15 -0
  25. data/ext/numo/linalg/lapack/gesdd.c +108 -0
  26. data/ext/numo/linalg/lapack/gesdd.h +15 -0
  27. data/ext/numo/linalg/lapack/gesv.c +99 -0
  28. data/ext/numo/linalg/lapack/gesv.h +15 -0
  29. data/ext/numo/linalg/lapack/gesvd.c +152 -0
  30. data/ext/numo/linalg/lapack/gesvd.h +15 -0
  31. data/ext/numo/linalg/lapack/getrf.c +71 -0
  32. data/ext/numo/linalg/lapack/getrf.h +15 -0
  33. data/ext/numo/linalg/lapack/getri.c +82 -0
  34. data/ext/numo/linalg/lapack/getri.h +15 -0
  35. data/ext/numo/linalg/lapack/getrs.c +110 -0
  36. data/ext/numo/linalg/lapack/getrs.h +15 -0
  37. data/ext/numo/linalg/lapack/heev.c +71 -0
  38. data/ext/numo/linalg/lapack/heev.h +15 -0
  39. data/ext/numo/linalg/lapack/heevd.c +71 -0
  40. data/ext/numo/linalg/lapack/heevd.h +15 -0
  41. data/ext/numo/linalg/lapack/heevr.c +111 -0
  42. data/ext/numo/linalg/lapack/heevr.h +15 -0
  43. data/ext/numo/linalg/lapack/hegv.c +94 -0
  44. data/ext/numo/linalg/lapack/hegv.h +15 -0
  45. data/ext/numo/linalg/lapack/hegvd.c +94 -0
  46. data/ext/numo/linalg/lapack/hegvd.h +15 -0
  47. data/ext/numo/linalg/lapack/hegvx.c +133 -0
  48. data/ext/numo/linalg/lapack/hegvx.h +15 -0
  49. data/ext/numo/linalg/lapack/hetrf.c +68 -0
  50. data/ext/numo/linalg/lapack/hetrf.h +15 -0
  51. data/ext/numo/linalg/lapack/lange.c +66 -0
  52. data/ext/numo/linalg/lapack/lange.h +15 -0
  53. data/ext/numo/linalg/lapack/orgqr.c +79 -0
  54. data/ext/numo/linalg/lapack/orgqr.h +15 -0
  55. data/ext/numo/linalg/lapack/potrf.c +70 -0
  56. data/ext/numo/linalg/lapack/potrf.h +15 -0
  57. data/ext/numo/linalg/lapack/potri.c +70 -0
  58. data/ext/numo/linalg/lapack/potri.h +15 -0
  59. data/ext/numo/linalg/lapack/potrs.c +94 -0
  60. data/ext/numo/linalg/lapack/potrs.h +15 -0
  61. data/ext/numo/linalg/lapack/syev.c +71 -0
  62. data/ext/numo/linalg/lapack/syev.h +15 -0
  63. data/ext/numo/linalg/lapack/syevd.c +71 -0
  64. data/ext/numo/linalg/lapack/syevd.h +15 -0
  65. data/ext/numo/linalg/lapack/syevr.c +111 -0
  66. data/ext/numo/linalg/lapack/syevr.h +15 -0
  67. data/ext/numo/linalg/lapack/sygv.c +93 -0
  68. data/ext/numo/linalg/lapack/sygv.h +15 -0
  69. data/ext/numo/linalg/lapack/sygvd.c +93 -0
  70. data/ext/numo/linalg/lapack/sygvd.h +15 -0
  71. data/ext/numo/linalg/lapack/sygvx.c +133 -0
  72. data/ext/numo/linalg/lapack/sygvx.h +15 -0
  73. data/ext/numo/linalg/lapack/sytrf.c +72 -0
  74. data/ext/numo/linalg/lapack/sytrf.h +15 -0
  75. data/ext/numo/linalg/lapack/trtrs.c +99 -0
  76. data/ext/numo/linalg/lapack/trtrs.h +15 -0
  77. data/ext/numo/linalg/lapack/ungqr.c +79 -0
  78. data/ext/numo/linalg/lapack/ungqr.h +15 -0
  79. data/ext/numo/linalg/linalg.c +290 -0
  80. data/ext/numo/linalg/linalg.h +85 -0
  81. data/ext/numo/linalg/util.c +95 -0
  82. data/ext/numo/linalg/util.h +17 -0
  83. data/lib/numo/linalg/version.rb +10 -0
  84. data/lib/numo/linalg.rb +1309 -0
  85. data/vendor/tmp/.gitkeep +0 -0
  86. metadata +146 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0a27ac7b0136649ad3cb4f65ecaa88eb56327aee77ce92ec189ae51209937dfd
4
+ data.tar.gz: d646eb637c82f3602741c6e31a9d18a6b69298c260a5837bfe5cf7c3089fd064
5
+ SHA512:
6
+ metadata.gz: c89a302f9855730a25ce18fc1b7e81346aecdc6bb22ec4036a1b43dc8981a2e3cb1cfb70249aaee2f2ed3cb641b27058210c5eb6c0e3aef0fc706183d154e7e8
7
+ data.tar.gz: 475964589987be96c4a258f4fe01ee63101b5d936e2d6f0e4a198357ccd54361c2d162f87d41c0fe8faaba0badb741f620cb44f9d260c84d33663dd5e27a13f2
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [[0.2.0](https://github.com/yoshoku/numo-linalg-alt/compare/d010476...ea50089)]
2
+
3
+ - fork from [Numo::TinyLinalg main branch](https://github.com/yoshoku/numo-tiny_linalg/tree/d0104765c560e9664a868b7a3e2f3144bd32c428)
4
+ - rewrite native extensions with C programming language.
5
+ - implement module functions such as matmul, matrix_power, svdvals, orth, null_space, lu, lu_fact, lu_inv, lu_solve, ldl, cho_fact, cho_inv, eig, eigvals, eigvalsh, cond, slogdet, matrix_rank, lstsq, and expm that are not implemented in Numo::TinyLinalg.
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at yoshoku@outlook.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE.txt ADDED
@@ -0,0 +1,27 @@
1
+ Copyright (c) 2025 Atsushi Tatsuma
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ * Redistributions of source code must retain the above copyright notice, this
8
+ list of conditions and the following disclaimer.
9
+
10
+ * Redistributions in binary form must reproduce the above copyright notice,
11
+ this list of conditions and the following disclaimer in the documentation
12
+ and/or other materials provided with the distribution.
13
+
14
+ * Neither the name of the copyright holder nor the names of its
15
+ contributors may be used to endorse or promote products derived from
16
+ this software without specific prior written permission.
17
+
18
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,106 @@
1
+ # Numo::Linalg Alternative
2
+
3
+ [![Build Status](https://github.com/yoshoku/numo-linalg-alt/actions/workflows/main.yml/badge.svg)](https://github.com/yoshoku/numo-linalg-alt/actions/workflows/main.yml)
4
+ [![BSD 3-Clause License](https://img.shields.io/badge/License-BSD%203--Clause-orange.svg)](https://github.com/yoshoku/numo-linalg-alt/blob/main/LICENSE.txt)
5
+
6
+ Numo::Linalg Alternative (numo-linalg-alt) is an alternative to [Numo::Linalg](https://github.com/ruby-numo/numo-linalg).
7
+ Unlike Numo::Linalg, numo-linalg-alt depends on [Numo::NArray Alterntive](https://github.com/yoshoku/numo-narray-alt).
8
+
9
+ Please note that this gem was forked from [Numo::TinyLinalg](https://github.com/yoshoku/numo-tiny_linalg),
10
+ not Numo::Linalg, and therefore it does not support changing backend libraries for BLAS and LAPACK.
11
+ In addition, the version numbering rule is not compatible with that of Numo::Linalg.
12
+
13
+ The project owner has the utmost respect for Numo::Linalg and its creator, Prof. [Masahiro Tanaka](https://github.com/masa16).
14
+ This project is in no way intended to adversely affect the development of the original Numo::Linalg.
15
+
16
+ ## Installation
17
+
18
+ Unlike Numo::Linalg, numo-linalg-alt only supports [OpenBLAS](https://github.com/OpenMathLib/OpenBLAS)
19
+ as a backend library.
20
+
21
+ Install the OpenBLAS.
22
+
23
+ macOS:
24
+
25
+ ```sh
26
+ $ brew install openblas
27
+ ```
28
+
29
+ Ubuntu:
30
+
31
+ ```sh
32
+ $ sudo apt-get install libopenblas-dev liblapacke-dev
33
+ ```
34
+
35
+ Install the gem and add to the application's Gemfile by executing.
36
+
37
+ macOS:
38
+
39
+ ```sh
40
+ $ bundle config build.numo-linalg-alt "--with-opt-dir=/opt/homebrew/opt/openblas"
41
+ $ bundle add numo-linalg-alt
42
+ ```
43
+
44
+ Ubuntu:
45
+
46
+ ```sh
47
+ $ bundle add numo-linalg-alt
48
+ ```
49
+
50
+ If bundler is not being used to manage dependencies, install the gem by executing.
51
+
52
+ macOS:
53
+
54
+ ```sh
55
+ $ gem install numo-linalg-alt -- --with-opt-dir=/opt/homebrew/opt/openblas
56
+ ```
57
+
58
+ Ubuntu:
59
+
60
+ ```sh
61
+ $ gem install numo-linalg-alt
62
+ ```
63
+
64
+ ## Usage
65
+
66
+ An example of singular value decomposition.
67
+
68
+ ```ruby
69
+ require 'numo/linalg'
70
+
71
+ x = Numo::DFloat.new(5, 2).rand.dot(Numo::DFloat.new(2, 3).rand)
72
+ # =>
73
+ # Numo::DFloat#shape=[5,3]
74
+ # [[0.104945, 0.0284236, 0.117406],
75
+ # [0.862634, 0.210945, 0.922135],
76
+ # [0.324507, 0.0752655, 0.339158],
77
+ # [0.67085, 0.102594, 0.600882],
78
+ # [0.404631, 0.116868, 0.46644]]
79
+
80
+ s, u, vt = Numo::Linalg.svd(x, job: 'S')
81
+
82
+ z = u.dot(s.diag).dot(vt)
83
+ # =>
84
+ # Numo::DFloat#shape=[5,3]
85
+ # [[0.104945, 0.0284236, 0.117406],
86
+ # [0.862634, 0.210945, 0.922135],
87
+ # [0.324507, 0.0752655, 0.339158],
88
+ # [0.67085, 0.102594, 0.600882],
89
+ # [0.404631, 0.116868, 0.46644]]
90
+
91
+ puts (x - z).abs.max
92
+ # => 4.440892098500626e-16
93
+ ```
94
+
95
+ ## Contributing
96
+
97
+ Bug reports and pull requests are welcome on GitHub at https://github.com/yoshoku/numo-linalg-alt.
98
+ This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/yoshoku/numo-linalg-alt/blob/main/CODE_OF_CONDUCT.md).
99
+
100
+ ## Code of Conduct
101
+
102
+ Everyone interacting in the Numo::TinyLinalg project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/yoshoku/numo-linalg-alt/blob/main/CODE_OF_CONDUCT.md).
103
+
104
+ ## License
105
+
106
+ The gem is available as open source under the terms of the [BSD-3-Clause License](https://opensource.org/licenses/BSD-3-Clause).
@@ -0,0 +1,72 @@
1
+ #include "dot.h"
2
+
3
+ #define DEF_LINALG_FUNC(tDType, tNAryClass, fBlasFunc) \
4
+ static void _iter_##fBlasFunc(na_loop_t* const lp) { \
5
+ tDType* x = (tDType*)NDL_PTR(lp, 0); \
6
+ tDType* y = (tDType*)NDL_PTR(lp, 1); \
7
+ tDType* d = (tDType*)NDL_PTR(lp, 2); \
8
+ const blasint n = (blasint)NDL_SHAPE(lp, 0)[0]; \
9
+ tDType ret = cblas_##fBlasFunc(n, x, 1, y, 1); \
10
+ *d = ret; \
11
+ } \
12
+ \
13
+ static VALUE _linalg_blas_##fBlasFunc(VALUE self, VALUE x, VALUE y) { \
14
+ if (CLASS_OF(x) != tNAryClass) { \
15
+ x = rb_funcall(tNAryClass, rb_intern("cast"), 1, x); \
16
+ } \
17
+ if (!RTEST(nary_check_contiguous(x))) { \
18
+ x = nary_dup(x); \
19
+ } \
20
+ if (CLASS_OF(y) != tNAryClass) { \
21
+ y = rb_funcall(tNAryClass, rb_intern("cast"), 1, y); \
22
+ } \
23
+ if (!RTEST(nary_check_contiguous(y))) { \
24
+ y = nary_dup(y); \
25
+ } \
26
+ \
27
+ narray_t* x_nary = NULL; \
28
+ GetNArray(x, x_nary); \
29
+ narray_t* y_nary = NULL; \
30
+ GetNArray(y, y_nary); \
31
+ \
32
+ if (NA_NDIM(x_nary) != 1) { \
33
+ rb_raise(rb_eArgError, "x must be 1-dimensional"); \
34
+ return Qnil; \
35
+ } \
36
+ if (NA_NDIM(y_nary) != 1) { \
37
+ rb_raise(rb_eArgError, "y must be 1-dimensional"); \
38
+ return Qnil; \
39
+ } \
40
+ if (NA_SIZE(x_nary) == 0) { \
41
+ rb_raise(rb_eArgError, "x must not be empty"); \
42
+ return Qnil; \
43
+ } \
44
+ if (NA_SIZE(y_nary) == 0) { \
45
+ rb_raise(rb_eArgError, "x must not be empty"); \
46
+ return Qnil; \
47
+ } \
48
+ if (NA_SIZE(x_nary) != NA_SIZE(y_nary)) { \
49
+ rb_raise(rb_eArgError, "x and y must have same size"); \
50
+ return Qnil; \
51
+ } \
52
+ \
53
+ ndfunc_arg_in_t ain[2] = { { tNAryClass, 1 }, { tNAryClass, 1 } }; \
54
+ size_t shape_out[1] = { 1 }; \
55
+ ndfunc_arg_out_t aout[1] = { { tNAryClass, 0, shape_out } }; \
56
+ ndfunc_t ndf = { _iter_##fBlasFunc, NO_LOOP | NDF_EXTRACT, 2, 1, ain, aout }; \
57
+ VALUE ret = na_ndloop(&ndf, 2, x, y); \
58
+ \
59
+ RB_GC_GUARD(x); \
60
+ RB_GC_GUARD(y); \
61
+ return ret; \
62
+ }
63
+
64
+ DEF_LINALG_FUNC(double, numo_cDFloat, ddot)
65
+ DEF_LINALG_FUNC(float, numo_cSFloat, sdot)
66
+
67
+ #undef DEF_LINALG_FUNC
68
+
69
+ void define_linalg_blas_dot(VALUE mBlas) {
70
+ rb_define_module_function(mBlas, "ddot", RUBY_METHOD_FUNC(_linalg_blas_ddot), 2);
71
+ rb_define_module_function(mBlas, "sdot", RUBY_METHOD_FUNC(_linalg_blas_sdot), 2);
72
+ }
@@ -0,0 +1,13 @@
1
+ #ifndef NUMO_LINALG_ALT_BLAS_DOT_H
2
+ #define NUMO_LINALG_ALT_BLAS_DOT_H 1
3
+
4
+ #include <ruby.h>
5
+
6
+ #include <cblas.h>
7
+
8
+ #include <numo/narray.h>
9
+ #include <numo/template.h>
10
+
11
+ void define_linalg_blas_dot(VALUE mBlas);
12
+
13
+ #endif /* NUMO_LINALG_ALT_BLAS_DOT_H */
@@ -0,0 +1,71 @@
1
+ #include "dot_sub.h"
2
+
3
+ #define DEF_LINALG_FUNC(tDType, tNAryClass, fBlasFunc) \
4
+ static void _iter_##fBlasFunc(na_loop_t* const lp) { \
5
+ tDType* x = (tDType*)NDL_PTR(lp, 0); \
6
+ tDType* y = (tDType*)NDL_PTR(lp, 1); \
7
+ tDType* d = (tDType*)NDL_PTR(lp, 2); \
8
+ const blasint n = (blasint)NDL_SHAPE(lp, 0)[0]; \
9
+ cblas_##fBlasFunc(n, x, 1, y, 1, d); \
10
+ } \
11
+ \
12
+ static VALUE _linalg_blas_##fBlasFunc(VALUE self, VALUE x, VALUE y) { \
13
+ if (CLASS_OF(x) != tNAryClass) { \
14
+ x = rb_funcall(tNAryClass, rb_intern("cast"), 1, x); \
15
+ } \
16
+ if (!RTEST(nary_check_contiguous(x))) { \
17
+ x = nary_dup(x); \
18
+ } \
19
+ if (CLASS_OF(y) != tNAryClass) { \
20
+ y = rb_funcall(tNAryClass, rb_intern("cast"), 1, y); \
21
+ } \
22
+ if (!RTEST(nary_check_contiguous(y))) { \
23
+ y = nary_dup(y); \
24
+ } \
25
+ \
26
+ narray_t* x_nary = NULL; \
27
+ GetNArray(x, x_nary); \
28
+ narray_t* y_nary = NULL; \
29
+ GetNArray(y, y_nary); \
30
+ \
31
+ if (NA_NDIM(x_nary) != 1) { \
32
+ rb_raise(rb_eArgError, "x must be 1-dimensional"); \
33
+ return Qnil; \
34
+ } \
35
+ if (NA_NDIM(y_nary) != 1) { \
36
+ rb_raise(rb_eArgError, "y must be 1-dimensional"); \
37
+ return Qnil; \
38
+ } \
39
+ if (NA_SIZE(x_nary) == 0) { \
40
+ rb_raise(rb_eArgError, "x must not be empty"); \
41
+ return Qnil; \
42
+ } \
43
+ if (NA_SIZE(y_nary) == 0) { \
44
+ rb_raise(rb_eArgError, "x must not be empty"); \
45
+ return Qnil; \
46
+ } \
47
+ if (NA_SIZE(x_nary) != NA_SIZE(y_nary)) { \
48
+ rb_raise(rb_eArgError, "x and y must have same size"); \
49
+ return Qnil; \
50
+ } \
51
+ \
52
+ ndfunc_arg_in_t ain[2] = { { tNAryClass, 1 }, { tNAryClass, 1 } }; \
53
+ size_t shape_out[1] = { 1 }; \
54
+ ndfunc_arg_out_t aout[1] = { { tNAryClass, 0, shape_out } }; \
55
+ ndfunc_t ndf = { _iter_##fBlasFunc, NO_LOOP | NDF_EXTRACT, 2, 1, ain, aout }; \
56
+ VALUE ret = na_ndloop(&ndf, 2, x, y); \
57
+ \
58
+ RB_GC_GUARD(x); \
59
+ RB_GC_GUARD(y); \
60
+ return ret; \
61
+ }
62
+
63
+ DEF_LINALG_FUNC(double, numo_cDComplex, zdotu_sub)
64
+ DEF_LINALG_FUNC(float, numo_cSComplex, cdotu_sub)
65
+
66
+ #undef DEF_LINALG_FUNC
67
+
68
+ void define_linalg_blas_dot_sub(VALUE mBlas) {
69
+ rb_define_module_function(mBlas, "zdotu", RUBY_METHOD_FUNC(_linalg_blas_zdotu_sub), 2);
70
+ rb_define_module_function(mBlas, "cdotu", RUBY_METHOD_FUNC(_linalg_blas_cdotu_sub), 2);
71
+ }
@@ -0,0 +1,13 @@
1
+ #ifndef NUMO_LINALG_ALT_BLAS_DOT_SUB_H
2
+ #define NUMO_LINALG_ALT_BLAS_DOT_SUB_H 1
3
+
4
+ #include <ruby.h>
5
+
6
+ #include <cblas.h>
7
+
8
+ #include <numo/narray.h>
9
+ #include <numo/template.h>
10
+
11
+ void define_linalg_blas_dot_sub(VALUE mBlas);
12
+
13
+ #endif /* NUMO_LINALG_ALT_BLAS_DOT_SUB_H */
@@ -0,0 +1,184 @@
1
+ #include "gemm.h"
2
+
3
+ #define DEF_LINALG_OPTIONS(tDType) \
4
+ struct _gemm_options_##tDType { \
5
+ tDType alpha; \
6
+ tDType beta; \
7
+ enum CBLAS_ORDER order; \
8
+ enum CBLAS_TRANSPOSE transa; \
9
+ enum CBLAS_TRANSPOSE transb; \
10
+ blasint m; \
11
+ blasint n; \
12
+ blasint k; \
13
+ };
14
+
15
+ #define DEF_LINALG_ITER_FUNC(tDType, fBlasFunc) \
16
+ static void _iter_##fBlasFunc(na_loop_t* const lp) { \
17
+ const tDType* a = (tDType*)NDL_PTR(lp, 0); \
18
+ const tDType* b = (tDType*)NDL_PTR(lp, 1); \
19
+ tDType* c = (tDType*)NDL_PTR(lp, 2); \
20
+ const struct _gemm_options_##tDType* opt = (struct _gemm_options_##tDType*)(lp->opt_ptr); \
21
+ const blasint lda = opt->transa == CblasNoTrans ? opt->k : opt->m; \
22
+ const blasint ldb = opt->transb == CblasNoTrans ? opt->n : opt->k; \
23
+ const blasint ldc = opt->n; \
24
+ cblas_##fBlasFunc(opt->order, opt->transa, opt->transb, opt->m, opt->n, opt->k, \
25
+ opt->alpha, a, lda, b, ldb, opt->beta, c, ldc); \
26
+ }
27
+
28
+ #define DEF_LINALG_ITER_FUNC_COMPLEX(tDType, fBlasFunc) \
29
+ static void _iter_##fBlasFunc(na_loop_t* const lp) { \
30
+ const tDType* a = (tDType*)NDL_PTR(lp, 0); \
31
+ const tDType* b = (tDType*)NDL_PTR(lp, 1); \
32
+ tDType* c = (tDType*)NDL_PTR(lp, 2); \
33
+ const struct _gemm_options_##tDType* opt = (struct _gemm_options_##tDType*)(lp->opt_ptr); \
34
+ const blasint lda = opt->transa == CblasNoTrans ? opt->k : opt->m; \
35
+ const blasint ldb = opt->transb == CblasNoTrans ? opt->n : opt->k; \
36
+ const blasint ldc = opt->n; \
37
+ cblas_##fBlasFunc(opt->order, opt->transa, opt->transb, opt->m, opt->n, opt->k, \
38
+ &opt->alpha, a, lda, b, ldb, &opt->beta, c, ldc); \
39
+ }
40
+
41
+ #define DEF_LINALG_ITER_FUNC(tDType, fBlasFunc) \
42
+ static void _iter_##fBlasFunc(na_loop_t* const lp) { \
43
+ const tDType* a = (tDType*)NDL_PTR(lp, 0); \
44
+ const tDType* b = (tDType*)NDL_PTR(lp, 1); \
45
+ tDType* c = (tDType*)NDL_PTR(lp, 2); \
46
+ const struct _gemm_options_##tDType* opt = (struct _gemm_options_##tDType*)(lp->opt_ptr); \
47
+ const blasint lda = opt->transa == CblasNoTrans ? opt->k : opt->m; \
48
+ const blasint ldb = opt->transb == CblasNoTrans ? opt->n : opt->k; \
49
+ const blasint ldc = opt->n; \
50
+ cblas_##fBlasFunc(opt->order, opt->transa, opt->transb, opt->m, opt->n, opt->k, \
51
+ opt->alpha, a, lda, b, ldb, opt->beta, c, ldc); \
52
+ }
53
+
54
+ #define DEF_LINALG_FUNC(tDType, tNAryClass, fBlasFunc) \
55
+ static VALUE _linalg_blas_##fBlasFunc(int argc, VALUE* argv, VALUE self) { \
56
+ VALUE a = Qnil; \
57
+ VALUE b = Qnil; \
58
+ VALUE c = Qnil; \
59
+ VALUE kw_args = Qnil; \
60
+ rb_scan_args(argc, argv, "21:", &a, &b, &c, &kw_args); \
61
+ \
62
+ ID kw_table[5] = { rb_intern("alpha"), rb_intern("beta"), rb_intern("order"), \
63
+ rb_intern("transa"), rb_intern("transb") }; \
64
+ VALUE kw_values[5] = { Qundef, Qundef, Qundef, Qundef, Qundef }; \
65
+ rb_get_kwargs(kw_args, kw_table, 0, 5, kw_values); \
66
+ \
67
+ if (CLASS_OF(a) != tNAryClass) { \
68
+ a = rb_funcall(tNAryClass, rb_intern("cast"), 1, a); \
69
+ } \
70
+ if (!RTEST(nary_check_contiguous(a))) { \
71
+ a = nary_dup(a); \
72
+ } \
73
+ if (CLASS_OF(b) != tNAryClass) { \
74
+ b = rb_funcall(tNAryClass, rb_intern("cast"), 1, b); \
75
+ } \
76
+ if (!RTEST(nary_check_contiguous(b))) { \
77
+ b = nary_dup(b); \
78
+ } \
79
+ if (!NIL_P(c)) { \
80
+ if (CLASS_OF(c) != tNAryClass) { \
81
+ c = rb_funcall(tNAryClass, rb_intern("cast"), 1, c); \
82
+ } \
83
+ if (!RTEST(nary_check_contiguous(c))) { \
84
+ c = nary_dup(c); \
85
+ } \
86
+ } \
87
+ \
88
+ tDType alpha = kw_values[0] != Qundef ? conv_##tDType(kw_values[0]) : one_##tDType(); \
89
+ tDType beta = kw_values[1] != Qundef ? conv_##tDType(kw_values[1]) : zero_##tDType(); \
90
+ enum CBLAS_ORDER order = kw_values[2] != Qundef ? get_cblas_order(kw_values[2]) : CblasRowMajor; \
91
+ enum CBLAS_TRANSPOSE transa = kw_values[3] != Qundef ? get_cblas_trans(kw_values[3]) : CblasNoTrans; \
92
+ enum CBLAS_TRANSPOSE transb = kw_values[4] != Qundef ? get_cblas_trans(kw_values[4]) : CblasNoTrans; \
93
+ \
94
+ narray_t* a_nary = NULL; \
95
+ GetNArray(a, a_nary); \
96
+ narray_t* b_nary = NULL; \
97
+ GetNArray(b, b_nary); \
98
+ \
99
+ if (NA_NDIM(a_nary) != 2) { \
100
+ rb_raise(rb_eArgError, "a must be 2-dimensional"); \
101
+ return Qnil; \
102
+ } \
103
+ if (NA_NDIM(b_nary) != 2) { \
104
+ rb_raise(rb_eArgError, "b must be 2-dimensional"); \
105
+ return Qnil; \
106
+ } \
107
+ if (NA_SIZE(a_nary) == 0) { \
108
+ rb_raise(rb_eArgError, "a must not be empty"); \
109
+ return Qnil; \
110
+ } \
111
+ if (NA_SIZE(b_nary) == 0) { \
112
+ rb_raise(rb_eArgError, "b must not be empty"); \
113
+ return Qnil; \
114
+ } \
115
+ \
116
+ const blasint ma = (blasint)NA_SHAPE(a_nary)[0]; \
117
+ const blasint ka = (blasint)NA_SHAPE(a_nary)[1]; \
118
+ const blasint kb = (blasint)NA_SHAPE(b_nary)[0]; \
119
+ const blasint nb = (blasint)NA_SHAPE(b_nary)[1]; \
120
+ const blasint m = transa == CblasNoTrans ? ma : ka; \
121
+ const blasint n = transb == CblasNoTrans ? nb : kb; \
122
+ const blasint k = transa == CblasNoTrans ? ka : ma; \
123
+ const blasint l = transb == CblasNoTrans ? kb : nb; \
124
+ \
125
+ if (k != l) { \
126
+ rb_raise(nary_eShapeError, "shape1[1](=%d) != shape2[0](=%d)", k, l); \
127
+ return Qnil; \
128
+ } \
129
+ \
130
+ struct _gemm_options_##tDType opt = { alpha, beta, order, transa, transb, m, n, k }; \
131
+ size_t shape_out[2] = { (size_t)m, (size_t)n }; \
132
+ ndfunc_arg_out_t aout[1] = { { tNAryClass, 2, shape_out } }; \
133
+ VALUE ret = Qnil; \
134
+ \
135
+ if (!NIL_P(c)) { \
136
+ narray_t* c_nary = NULL; \
137
+ GetNArray(c, c_nary); \
138
+ blasint nc = (blasint)NA_SHAPE(c_nary)[0]; \
139
+ if (m > nc) { \
140
+ rb_raise(nary_eShapeError, "shape3[0](=%d) >= shape1[0]=%d", nc, m); \
141
+ return Qnil; \
142
+ } \
143
+ ndfunc_arg_in_t ain[3] = { { tNAryClass, 2 }, { tNAryClass, 2 }, { OVERWRITE, 2 } }; \
144
+ ndfunc_t ndf = { _iter_##fBlasFunc, NO_LOOP, 3, 0, ain, aout }; \
145
+ na_ndloop3(&ndf, &opt, 3, a, b, c); \
146
+ ret = c; \
147
+ } else { \
148
+ c = INT2NUM(0); \
149
+ ndfunc_arg_in_t ain[3] = { { tNAryClass, 2 }, { tNAryClass, 2 }, { sym_init, 0 } }; \
150
+ ndfunc_t ndf = { _iter_##fBlasFunc, NO_LOOP, 3, 1, ain, aout }; \
151
+ ret = na_ndloop3(&ndf, &opt, 3, a, b, c); \
152
+ } \
153
+ \
154
+ RB_GC_GUARD(a); \
155
+ RB_GC_GUARD(b); \
156
+ RB_GC_GUARD(c); \
157
+ \
158
+ return ret; \
159
+ }
160
+
161
+ DEF_LINALG_OPTIONS(double)
162
+ DEF_LINALG_OPTIONS(float)
163
+ DEF_LINALG_OPTIONS(dcomplex)
164
+ DEF_LINALG_OPTIONS(scomplex)
165
+ DEF_LINALG_ITER_FUNC(double, dgemm)
166
+ DEF_LINALG_ITER_FUNC(float, sgemm)
167
+ DEF_LINALG_ITER_FUNC_COMPLEX(dcomplex, zgemm)
168
+ DEF_LINALG_ITER_FUNC_COMPLEX(scomplex, cgemm)
169
+ DEF_LINALG_FUNC(double, numo_cDFloat, dgemm)
170
+ DEF_LINALG_FUNC(float, numo_cSFloat, sgemm)
171
+ DEF_LINALG_FUNC(dcomplex, numo_cDComplex, zgemm)
172
+ DEF_LINALG_FUNC(scomplex, numo_cSComplex, cgemm)
173
+
174
+ #undef DEF_LINALG_OPTIONS
175
+ #undef DEF_LINALG_ITER_FUNC
176
+ #undef DEF_LINALG_ITER_FUNC_COMPLEX
177
+ #undef DEF_LINALG_FUNC
178
+
179
+ void define_linalg_blas_gemm(VALUE mBlas) {
180
+ rb_define_module_function(mBlas, "dgemm", RUBY_METHOD_FUNC(_linalg_blas_dgemm), -1);
181
+ rb_define_module_function(mBlas, "sgemm", RUBY_METHOD_FUNC(_linalg_blas_sgemm), -1);
182
+ rb_define_module_function(mBlas, "zgemm", RUBY_METHOD_FUNC(_linalg_blas_zgemm), -1);
183
+ rb_define_module_function(mBlas, "cgemm", RUBY_METHOD_FUNC(_linalg_blas_cgemm), -1);
184
+ }
@@ -0,0 +1,16 @@
1
+ #ifndef NUMO_LINALG_ALT_BLAS_GEMM_H
2
+ #define NUMO_LINALG_ALT_BLAS_GEMM_H 1
3
+
4
+ #include <ruby.h>
5
+
6
+ #include <cblas.h>
7
+
8
+ #include <numo/narray.h>
9
+ #include <numo/template.h>
10
+
11
+ #include "../converter.h"
12
+ #include "../util.h"
13
+
14
+ void define_linalg_blas_gemm(VALUE mBlas);
15
+
16
+ #endif /* NUMO_LINALG_ALT_BLAS_GEMM_H */