module CArray::DataTypeExtension

Public Instance Methods

arange(*args) click to toggle source
# File lib/carray/construct.rb, line 419
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, step)
end
eye(n, m = nil, k = 0) click to toggle source
# File lib/carray/construct.rb, line 395
def eye (n, m = nil, k = 0)
  m ||= n
  mat = CArray.new(self::DataType || CA_FLOAT64, [n, m])
  start = ( k == 0 ) ? k : ( k > 0 ? k : m - k - 1 )
  mat[[start..-1,m+1]] = 1
  mat
end
full(shape, fill_value) click to toggle source
# File lib/carray/construct.rb, line 436
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) click to toggle source
# File lib/carray/construct.rb, line 403
def identity (n)
  mat = CArray.new(self::DataType || CA_FLOAT64, [n, n])
  mat[[nil,n+1]] = 1
  mat      
end
linspace(x1, x2, n = 100) click to toggle source
# File lib/carray/construct.rb, line 409
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) click to toggle source
# File lib/carray/construct.rb, line 391
def ones (*shape)
  CArray.new(self::DataType || CA_FLOAT64 , shape).one
end
zeros(*shape) click to toggle source
# File lib/carray/construct.rb, line 387
def zeros (*shape)
  CArray.new(self::DataType || CA_FLOAT64, shape).zero
end

Private Instance Methods

guess_data_type_from_values(*values) click to toggle source
# File lib/carray/construct.rb, line 371
def guess_data_type_from_values (*values)
  if values.all? {|v| v == true || v == false }
    CA_BOOLEAN
  elsif values.all? { |v| v.is_a?(Integer) }
    CA_INT64
  elsif values.all? { |v| v.is_a?(Float) }
    CA_FLOAT64
  elsif values.all? { |v| v.is_a?(Complex) }
    CA_CMPLX128
  else
    CA_OBJECT
  end
end