numo-random 0.1.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.
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Numo
4
+ module Random
5
+ # The version of Numo::Random you install.
6
+ VERSION = '0.1.0'
7
+ end
8
+ end
@@ -0,0 +1,184 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'numo/narray'
4
+
5
+ require_relative 'random/version'
6
+ require_relative 'random/randomext'
7
+
8
+ # Ruby/Numo (NUmerical MOdules)
9
+ module Numo
10
+ # Numo::Random provides random number generation with several distributions for Numo::NArray.
11
+ module Random
12
+ # Generator is a class that generates random number with several distributions.
13
+ #
14
+ # @example
15
+ # require 'numo/random'
16
+ #
17
+ # x = Numo::DFloat.new(100)
18
+ #
19
+ # rng = Numo::Random::Generator.new
20
+ # rng.uniform(x, low: -1, high: 2)
21
+ class Generator
22
+ # Returns random number generation algorithm.
23
+ # @return [String]
24
+ attr_accessor :algorithm
25
+
26
+ # Creates a new random number generator.
27
+ #
28
+ # @param seed [Integer] random seed used to initialize the random number generator.
29
+ # @param algorithm [String] random number generation algorithm.
30
+ def initialize(seed: nil, algorithm: 'pcg64') # rubocop:disable Lint/UnusedMethodArgument
31
+ @algorithm = 'pcg64'
32
+ @rng = PCG64.new(seed: seed)
33
+ end
34
+
35
+ # Returns the seed of random number generator.
36
+ #
37
+ # @return [Integer]
38
+ def seed
39
+ rng.seed
40
+ end
41
+
42
+ # Sets the seed of random number generator.
43
+ #
44
+ # @param val [Integer] random seed.
45
+ def seed=(val)
46
+ rng.seed = val
47
+ end
48
+
49
+ # Returns random number with uniform distribution in the half-open interval [0, 1).
50
+ #
51
+ # @example
52
+ # require 'numo/random'
53
+ #
54
+ # rng = Numo::Random::Generator.new
55
+ # v = rng.random
56
+ #
57
+ # @return [Float]
58
+ def random
59
+ rng.random
60
+ end
61
+
62
+ # Fills given array with uniformly distributed random values in the interval [low, high).
63
+ #
64
+ # @example
65
+ # require 'numo/random'
66
+ #
67
+ # x = Numo::DFloat.new(100)
68
+ #
69
+ # rng = Numo::Random::Generator.new
70
+ # rng.uniform(x, low: -1.5, high: 1.5)
71
+ #
72
+ # @param x [Numo::DFloat | Numo::SFloat] array filled with random values.
73
+ # @param low [Float] lower boundary.
74
+ # @param high [Float] upper boundary.
75
+ def uniform(x, low: 0.0, high: 1.0)
76
+ rng.uniform(x, low: low, high: high)
77
+ end
78
+
79
+ # Fills given array with random values according to the Cauchy (Lorentz) distribution.
80
+ #
81
+ # @example
82
+ # require 'numo/random'
83
+ #
84
+ # x = Numo::DFloat.new(100)
85
+ #
86
+ # rng = Numo::Random::Generator.new
87
+ # rng.cauchy(x, loc: 0.0, scale: 1.0)
88
+ #
89
+ # @param x [Numo::DFloat | Numo::SFloat] array filled with random values.
90
+ # @param loc [Float] location parameter.
91
+ # @param scale [Float] scale parameter.
92
+ def cauchy(x, loc: 0.0, scale: 1.0)
93
+ rng.cauchy(x, loc: loc, scale: scale)
94
+ end
95
+
96
+ # Fills given array with random values according to the Chi-squared distribution.
97
+ #
98
+ # @example
99
+ # require 'numo/random'
100
+ #
101
+ # x = Numo::DFloat.new(100)
102
+ #
103
+ # rng = Numo::Random::Generator.new
104
+ # rng.chisquare(x, df: 2.0)
105
+ #
106
+ # @param x [Numo::DFloat | Numo::SFloat] array filled with random values.
107
+ # @param df [Float] degrees of freedom, must be > 0.
108
+ def chisquare(x, df:)
109
+ rng.chisquare(x, df: df)
110
+ end
111
+
112
+ # Fills given array with random values according to the F-distribution.
113
+ #
114
+ # @example
115
+ # require 'numo/random'
116
+ #
117
+ # x = Numo::DFloat.new(100)
118
+ #
119
+ # rng = Numo::Random::Generator.new
120
+ # rng.f(x, dfnum: 2.0, dfden: 4.0)
121
+ #
122
+ # @param x [Numo::DFloat | Numo::SFloat] array filled with random values.
123
+ # @param dfnum [Float] degrees of freedom in numerator, must be > 0.
124
+ # @param dfden [Float] degrees of freedom in denominator, must be > 0.
125
+ def f(x, dfnum:, dfden:)
126
+ rng.f(x, dfnum: dfnum, dfden: dfden)
127
+ end
128
+
129
+ # Fills given array with random values according to a normal (Gaussian) distribution.
130
+ #
131
+ # @example
132
+ # require 'numo/random'
133
+ #
134
+ # x = Numo::DFloat.new(100)
135
+ #
136
+ # rng = Numo::Random::Generator.new
137
+ # rng.normal(x, loc: 0.0, scale: 1.0)
138
+ #
139
+ # @param x [Numo::DFloat | Numo::SFloat] array filled with random values.
140
+ # @param loc [Float] location parameter.
141
+ # @param scale [Float] scale parameter.
142
+ def normal(x, loc: 0.0, scale: 1.0)
143
+ rng.normal(x, loc: loc, scale: scale)
144
+ end
145
+
146
+ # Fills given array with random values according to a log-normal distribution.
147
+ #
148
+ # @example
149
+ # require 'numo/random'
150
+ #
151
+ # x = Numo::DFloat.new(100)
152
+ #
153
+ # rng = Numo::Random::Generator.new
154
+ # rng.lognormal(x, mean: 0.0, sigma: 1.0)
155
+ #
156
+ # @param x [Numo::DFloat | Numo::SFloat] array filled with random values.
157
+ # @param mean [Float] mean of normal distribution.
158
+ # @param sigma [Float] standard deviation of normal distribution.
159
+ def lognormal(x, mean: 0.0, sigma: 1.0)
160
+ rng.lognormal(x, mean: mean, sigma: sigma)
161
+ end
162
+
163
+ # Fills given array with random values according to the Student's t-distribution.
164
+ #
165
+ # @example
166
+ # require 'numo/random'
167
+ #
168
+ # x = Numo::DFloat.new(100)
169
+ #
170
+ # rng = Numo::Random::Generator.new
171
+ # rng.standard_t(x, df: 8.0)
172
+ #
173
+ # @param x [Numo::DFloat | Numo::SFloat] array filled with random values.
174
+ # @param df [Float] degrees of freedom, must be > 0.
175
+ def standard_t(x, df:)
176
+ rng.standard_t(x, df: df)
177
+ end
178
+
179
+ private
180
+
181
+ attr_reader :rng
182
+ end
183
+ end
184
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: numo-random
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - yoshoku
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-10-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: numo-narray
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.9.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.9.1
27
+ description: Numo::Random provides random number generation with several distributions
28
+ for Numo::NArray.
29
+ email:
30
+ - yoshoku@outlook.com
31
+ executables: []
32
+ extensions:
33
+ - ext/numo/random/extconf.rb
34
+ extra_rdoc_files: []
35
+ files:
36
+ - CHANGELOG.md
37
+ - CODE_OF_CONDUCT.md
38
+ - LICENSE.txt
39
+ - README.md
40
+ - ext/numo/random/extconf.rb
41
+ - ext/numo/random/randomext.cpp
42
+ - ext/numo/random/randomext.hpp
43
+ - ext/numo/random/src/LICENSE.txt
44
+ - ext/numo/random/src/pcg_extras.hpp
45
+ - ext/numo/random/src/pcg_random.hpp
46
+ - ext/numo/random/src/pcg_uint128.hpp
47
+ - lib/numo/random.rb
48
+ - lib/numo/random/version.rb
49
+ homepage: https://github.com/yoshoku/numo-random
50
+ licenses:
51
+ - Apache-2.0
52
+ metadata:
53
+ homepage_uri: https://github.com/yoshoku/numo-random
54
+ source_code_uri: https://github.com/yoshoku/numo-random
55
+ changelog_uri: https://github.com/yoshoku/numo-random/blob/main/CHANGELOG.md
56
+ documentation_uri: https://www.rubydoc.info/gems/numo-random
57
+ rubygems_mfa_required: 'true'
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubygems_version: 3.3.7
74
+ signing_key:
75
+ specification_version: 4
76
+ summary: Numo::Random provides random number generation with several distributions
77
+ for Numo::NArray.
78
+ test_files: []