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

Instance Method Details

#arange(stop) ⇒ CArray #arange(start, stop) ⇒ CArray #arange(start, stop, step) ⇒ CArray

Overloads:

  • #arange(stop) ⇒ CArray

    Returns a 1-D CArray with values 0, 1, ..., stop - 1.

    Parameters:

    • stop (Numeric)

      exclusive upper bound.

    Returns:

  • #arange(start, stop) ⇒ CArray

    Returns start, start + 1, ..., stop - 1.

    Parameters:

    • start (Numeric)

      inclusive lower bound.

    • stop (Numeric)

      exclusive upper bound.

    Returns:

  • #arange(start, stop, step) ⇒ CArray

    Returns values from start to stop (exclusive) stepping by step.

    Parameters:

    • start (Numeric)

      inclusive lower bound.

    • stop (Numeric)

      exclusive upper bound.

    • step (Numeric)

      spacing.

    Returns:

Raises:

  • (ArgumentError)


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

Returns a new CArray of the given shape whose contents are uninitialised. The caller must overwrite the array before reading from it. CA_OBJECT silently falls back to a zero-VALUE init required for GC safety.

Parameters:

Returns:



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

Returns a 2-D CArray with ones on the k-th diagonal and zeros elsewhere.

Parameters:

  • n (Integer)

    number of rows.

  • m (Integer) (defaults to: n)

    number of columns; defaults to n.

  • k (Integer) (defaults to: 0)

    diagonal offset (positive above the main diagonal, negative below).

Returns:



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

Returns a new CArray of the given shape filled with fill_value. shape accepts an Integer or an Array of Integers. The data_type is inferred from fill_value when the receiver is not a typed class.

Parameters:

  • shape (Integer, Array<Integer>)
  • fill_value (Object)

    value used to fill the array.

Returns:



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

Returns the n by n identity matrix.

Parameters:

  • n (Integer)

    matrix size.

Returns:



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

Returns a 1-D CArray of n values evenly spaced from x1 to x2 inclusive, matching NumPy's linspace. For an integer target data_type, values are computed in float64 with the linspace step (x2 - x1) / (n - 1), then floored to the target type — the rule np.linspace(x1, x2, n, dtype=int) uses (round toward -infinity, not toward zero). Both endpoints are hit exactly for integer output when x1 and x2 are integer-valued.

Called on CArray itself (module method) integer inputs promote to CA_FLOAT64 and the result is a float array. Called via an integer subclass (CArray::Int32.linspace(...)), the result is an integer array with floor semantics.

Parameters:

  • x1 (Numeric)

    first value.

  • x2 (Numeric)

    last value.

  • n (Integer) (defaults to: 100)

    number of samples.

Returns:



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

Returns a new CArray of the given shape filled with ones. Same shape and data_type rules as #zeros.

Parameters:

Returns:



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

Returns a new CArray of the given shape filled with zeros. Shape accepts both splat Integers and a single Array. When called on a typed class the data_type is taken from the class; otherwise it defaults to CA_FLOAT64.

Parameters:

Returns:



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