Module: CArray::DataTypeExtension
- Included in:
- CArray, Boolean, Complex128, Complex64, Fixlen, Float32, Float64, Int16, Int32, Int64, Int8, Object, UInt16, UInt32, UInt64, UInt8
- 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 |
# 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 end data_type = self::DataType data_type ||= guess_data_type_from_values(start, stop, step) CArray.send(:__cast__, data_type, start..stop-step, step) end |
#empty(*shape) ⇒ CArray
225 226 227 228 |
# File 'lib/carray/data_type_extension.rb', line 225 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
211 212 213 214 215 216 |
# File 'lib/carray/data_type_extension.rb', line 211 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 |