Module: CArray::DataTypeExtension
- Included in:
- CArray
- Defined in:
- lib/carray/construct.rb,
lib/carray/construct.rb,
lib/carray/data_type_extension.rb
Overview
DataTypeExtension is the carrier module for Numo / NumPy-style factory methods that are extended onto CArray and every typed CArray::Int32 / Float64 / ... class below. Its body is defined in lib/carray/data_type_extension.rb (required after this file), keeping the soft-compatibility surface separate from carray's native constructor API.
Instance Method Summary collapse
- #arange(*args) ⇒ Object
-
#empty(*shape) ⇒ CArray
Returns a new CArray of the given shape whose contents are uninitialised.
-
#eye(n, m = n, k = 0) ⇒ CArray
Returns a 2-D CArray with ones on the
k-th diagonal and zeros elsewhere. -
#full(shape, fill_value) ⇒ CArray
Returns a new CArray of the given shape filled with
fill_value. -
#identity(n) ⇒ CArray
Returns the
nbynidentity matrix. -
#linspace(x1, x2, n = 100) ⇒ CArray
Returns a 1-D CArray of
nvalues evenly spaced fromx1tox2inclusive, matching NumPy'slinspace. -
#ones(*shape) ⇒ CArray
Returns a new CArray of the given shape filled with ones.
-
#zeros(*shape) ⇒ CArray
Returns a new CArray of the given shape filled with zeros.
Instance Method Details
#arange(stop) ⇒ CArray #arange(start, stop) ⇒ CArray #arange(start, stop, step) ⇒ CArray
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/carray/data_type_extension.rb', line 186 def arange (*args) case args.size when 3 start, stop, step = *args when 2 start, stop = *args step = 1 when 1 start = 0 stop, = *args step = 1 else raise ArgumentError, "wrong number of arguments (given #{args.size}, expected 1..3)" end raise ArgumentError, "step must not be 0" if step == 0 data_type = self::DataType data_type ||= guess_data_type_from_values(start, stop, step) # Element count comes from the arguments as given, not from the # target data type: CArray::Int32.arange(0, 1, 0.25) counts four # elements from the float step and truncates them on store. # Integer arguments count exactly (divmod) so that a step dividing # the span evenly does not gain a spurious element through float # rounding. span = stop - start if span.is_a?(Integer) && step.is_a?(Integer) q, r = span.divmod(step) n = r.zero? ? q : q + 1 else n = (span.to_f / step).ceil end n = 0 if n < 0 CArray.new(data_type, [n]).seq(start, step) end |
#empty(*shape) ⇒ CArray
242 243 244 245 |
# File 'lib/carray/data_type_extension.rb', line 242 def empty (*args) CArray.__alloc_uninit__(self::DataType || CA_FLOAT64, normalize_shape(args)) end |
#eye(n, m = n, k = 0) ⇒ CArray
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/carray/data_type_extension.rb', line 108 def eye (n, m = nil, k = 0) m ||= n mat = CArray.new(self::DataType || CA_FLOAT64, [n, m]) if k >= 0 count = [n, m - k].min start = k else count = [n + k, m].min start = (-k) * m end if count > 0 mat[[start, count, m+1]] = 1 end mat end |
#full(shape, fill_value) ⇒ CArray
228 229 230 231 232 233 |
# File 'lib/carray/data_type_extension.rb', line 228 def full (shape, fill_value) data_type = self::DataType data_type ||= guess_data_type_from_values(fill_value) shape = [shape] unless shape.is_a?(Array) CArray.new(data_type, shape).fill(fill_value) end |
#identity(n) ⇒ CArray
128 129 130 131 132 |
# File 'lib/carray/data_type_extension.rb', line 128 def identity (n) mat = CArray.new(self::DataType || CA_FLOAT64, [n, n]) mat[[nil,n+1]] = 1 mat end |
#linspace(x1, x2, n = 100) ⇒ CArray
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/carray/data_type_extension.rb', line 153 def linspace (x1, x2, n = 100) data_type = self::DataType unless data_type guess = guess_data_type_from_values(x1, x2) guess = CA_FLOAT64 if guess == CA_INT64 data_type = guess end # span is float-only ("N evenly-spaced integers" is ambiguous — # see basics.rb). Route non-float targets through a float64 # span, then floor + cast; this matches np.linspace(dtype=int) # bit for bit (floor rounds toward -infinity so negative # midpoints go the same way as numpy's integer linspace). float_out = CArray.new(CA_FLOAT64, [n]).span(x1.to_f..x2.to_f) return float_out if data_type == CA_FLOAT64 float_out.floor.to_type(data_type) end |
#ones(*shape) ⇒ CArray
96 97 98 |
# File 'lib/carray/data_type_extension.rb', line 96 def ones (*args) CArray.new(self::DataType || CA_FLOAT64, normalize_shape(args)).one end |
#zeros(*shape) ⇒ CArray
87 88 89 |
# File 'lib/carray/data_type_extension.rb', line 87 def zeros (*args) CArray.new(self::DataType || CA_FLOAT64, normalize_shape(args)).zero end |