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

Instance Method Summary collapse

Instance Method Details

#arange(*args) ⇒ Object



421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/carray/construct.rb', line 421

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.__new__(data_type, start..stop, step)
end

#eye(n, m = nil, k = 0) ⇒ Object



397
398
399
400
401
402
403
# File 'lib/carray/construct.rb', line 397

def eye (n, m = nil, k = 0)
  m ||= n
  mat = CArray.new(self::DataType || CA_FLOAT64, [n, m])
  start = k > 0 ? k : m - k - 1
  mat[[start..-1,m+1]] = 1
  mat
end

#full(shape, fill_value) ⇒ Object



438
439
440
441
442
443
# File 'lib/carray/construct.rb', line 438

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) ⇒ Object



405
406
407
408
409
# File 'lib/carray/construct.rb', line 405

def identity (n)
  mat = CArray.new(self::DataType || CA_FLOAT64, [n, n])
  mat[[nil,n+1]] = 1
  mat      
end

#linspace(x1, x2, n = 100) ⇒ Object



411
412
413
414
415
416
417
418
419
# File 'lib/carray/construct.rb', line 411

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
  CArray.new(data_type, [n]).span(x1..x2)
end

#ones(*shape) ⇒ Object



393
394
395
# File 'lib/carray/construct.rb', line 393

def ones (*shape)
  CArray.new(self::DataType || CA_FLOAT64 , shape).one
end

#zeros(*shape) ⇒ Object



389
390
391
# File 'lib/carray/construct.rb', line 389

def zeros (*shape)
  CArray.new(self::DataType || CA_FLOAT64, shape).zero
end