distribution.rb 9.15 KB
Newer Older
Brice Videau's avatar
Brice Videau committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
module CCS

  DistributionType = enum FFI::Type::INT32, :ccs_distribution_type_t, [
    :CCS_UNIFORM,
    :CCS_NORMAL,
    :CCS_ROULETTE
  ]
  class MemoryPointer
    def read_ccs_distribution_type_t
      DistributionType.from_native(read_int32, nil)
    end
  end

  ScaleType = enum FFI::Type::INT32, :ccs_scale_type_t, [
    :CCS_LINEAR,
    :CCS_LOGARITHMIC
  ]
  class MemoryPointer
    def read_ccs_scale_type_t
      ScaleType.from_native(read_int32, nil)
    end
  end

  attach_function :ccs_distribution_get_type, [:ccs_distribution_t, :pointer], :ccs_result_t
  attach_function :ccs_distribution_get_data_type, [:ccs_distribution_t, :pointer], :ccs_result_t
  attach_function :ccs_distribution_get_dimension, [:ccs_distribution_t, :pointer], :ccs_result_t
  attach_function :ccs_distribution_get_scale_type, [:ccs_distribution_t, :pointer], :ccs_result_t
  attach_function :ccs_distribution_get_quantization, [:ccs_distribution_t, :pointer], :ccs_result_t
  attach_function :ccs_distribution_get_bounds, [:ccs_distribution_t, :pointer], :ccs_result_t
Brice Videau's avatar
Brice Videau committed
30
  attach_function :ccs_distribution_check_oversampling, [:ccs_distribution_t, Interval.by_ref, :pointer], :ccs_result_t
Brice Videau's avatar
Brice Videau committed
31 32 33 34 35 36 37
  attach_function :ccs_distribution_sample, [:ccs_distribution_t, :ccs_rng_t, :pointer], :ccs_result_t
  attach_function :ccs_distribution_samples, [:ccs_distribution_t, :ccs_rng_t, :size_t, :pointer], :ccs_result_t

  class Distribution < Object
    add_property :type, :ccs_distribution_type_t, :ccs_distribution_get_type, memoize: true
    add_property :data_type, :ccs_numeric_type_t, :ccs_distribution_get_data_type, memoize: true
    add_property :dimension, :size_t, :ccs_distribution_get_dimension, memoize: true
Brice Videau's avatar
Brice Videau committed
38
    add_property :scale, :ccs_scale_type_t, :ccs_distribution_get_scale_type, memoize: true
Brice Videau's avatar
Brice Videau committed
39

Brice Videau's avatar
Brice Videau committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    def self.from_handle(handle)
      ptr = MemoryPointer::new(:ccs_distribution_type_t)
      res = CCS.ccs_distribution_get_type(handle, ptr)
      CCS.error_check(res)
      case ptr.read_ccs_distribution_type_t
      when :CCS_UNIFORM
        UniformDistribution::new(handle, retain: true)
      when :CCS_NORMAL
        NormalDistribution::new(handle, retain: true)
      when :CCS_ROULETTE
        RouletteDistribution::new(handle, retain: true)
      else
        raise StandardError, :CCS_INVALID_DISTRIBUTION
      end
    end

Brice Videau's avatar
Brice Videau committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    def quantization
      @quantization ||= begin
        ptr = MemoryPointer::new(:ccs_numeric_t)
        res = CCS.ccs_distribution_get_quantization(@handle, ptr)
        CCS.error_check(res)
        if data_type == :CCS_NUM_FLOAT
          ptr.read_ccs_float_t
        else
          ptr.read_ccs_int_t
        end
      end
    end

    def bounds
      @bounds ||= begin
        interval = Interval::new(type: :CCS_NUM_FLOAT)
        res = CCS.ccs_distribution_get_bounds(@handle, interval)
        CCS.error_check(res)
        interval
      end
    end

    def oversampling?(interval)
      ptr = MemoryPointer::new(:ccs_bool_t)
      res = CCS.ccs_distribution_check_oversampling(@handle, interval, ptr)
      CCS.error_check(res)
      ptr.read_ccs_bool_t == CCS::FALSE ? false : true
    end

    def sample(rng)
      ptr = MemoryPointer::new(:ccs_numeric_t)
      res = CCS.ccs_distribution_sample(@handle, rng, ptr)
      CCS.error_check(res)
      if data_type == :CCS_NUM_FLOAT
        ptr.read_ccs_float_t
      else
        ptr.read_ccs_int_t
      end
    end

    def samples(rng, count)
      ptr = MemoryPointer::new(:ccs_numeric_t, count)
      res = CCS.ccs_distribution_samples(@handle, rng, count, ptr)
      CCS.error_check(res)
      if data_type == :CCS_NUM_FLOAT
        ptr.read_array_of_ccs_float_t(count)
      else
        ptr.read_array_of_ccs_int_t(count)
      end
    end

  end

  attach_function :ccs_create_uniform_distribution, [:ccs_numeric_type_t, :ccs_numeric_t, :ccs_numeric_t, :ccs_scale_type_t, :ccs_numeric_t, :pointer], :ccs_result_t
  attach_function :ccs_create_uniform_int_distribution, [:ccs_int_t, :ccs_int_t, :ccs_scale_type_t, :ccs_int_t, :pointer], :ccs_result_t
  attach_function :ccs_create_uniform_float_distribution, [:ccs_float_t, :ccs_float_t, :ccs_scale_type_t, :ccs_float_t, :pointer], :ccs_result_t
Brice Videau's avatar
Brice Videau committed
112
  attach_function :ccs_uniform_distribution_get_parameters, [:ccs_distribution_t, :pointer, :pointer], :ccs_result_t
Brice Videau's avatar
Brice Videau committed
113 114

  class UniformDistribution < Distribution
Brice Videau's avatar
Brice Videau committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
    def initialize(handle = nil, retain: false, data_type: :CCS_NUM_FLOAT, lower: 0.0, upper: 1.0, scale: :CCS_LINEAR, quantization: 0.0)
      if handle
        super(handle, retain: retain)
      else
        ptr = MemoryPointer::new(:ccs_distribution_t)
        res = if data_type == :CCS_NUM_FLOAT
            CCS.ccs_create_uniform_float_distribution(lower, upper, scale, quantization, ptr)
          else
            CCS.ccs_create_uniform_int_distribution(lower, upper, scale, quantization, ptr)
          end
        CCS.error_check(res)
        super(ptr.read_pointer, retain: false)
      end
    end

    def self.int(lower:, upper:, scale: :CCS_LINEAR, quantization: 0)
      self.new(nil, data_type: :CCS_NUM_INTEGER, lower: lower, upper: upper, scale: scale, quantization: quantization)
    end

    def self.float(lower:, upper:, scale: :CCS_LINEAR, quantization: 0.0)
      self.new(nil, data_type: :CCS_NUM_FLOAT, lower: lower, upper: upper, scale: scale, quantization: quantization)
    end

    def lower
      @lower ||= begin
        ptr = MemoryPointer::new(:ccs_numeric_t)
        res = CCS.ccs_uniform_distribution_get_parameters(@handle, ptr, nil)
        CCS.error_check(res)
        if data_type == :CCS_NUM_FLOAT
          ptr.read_ccs_float_t
        else
          ptr.read_ccs_int_t
        end
      end
    end

    def upper
      @upper ||= begin
        ptr = MemoryPointer::new(:ccs_numeric_t)
        res = CCS.ccs_uniform_distribution_get_parameters(@handle, nil, ptr)
        CCS.error_check(res)
        if data_type == :CCS_NUM_FLOAT
          ptr.read_ccs_float_t
        else
          ptr.read_ccs_int_t
        end
      end
    end
  end

  attach_function :ccs_create_normal_distribution, [:ccs_numeric_type_t, :ccs_float_t, :ccs_float_t, :ccs_scale_type_t, :ccs_numeric_t, :pointer], :ccs_result_t
  attach_function :ccs_create_normal_int_distribution, [:ccs_float_t, :ccs_float_t, :ccs_scale_type_t, :ccs_int_t, :pointer], :ccs_result_t
  attach_function :ccs_create_normal_float_distribution, [:ccs_float_t, :ccs_float_t, :ccs_scale_type_t, :ccs_float_t, :pointer], :ccs_result_t
  attach_function :ccs_normal_distribution_get_parameters, [:ccs_distribution_t, :pointer, :pointer], :ccs_result_t
  class NormalDistribution < Distribution
    def initialize(handle = nil, retain: false, data_type: :CCS_NUM_FLOAT, mu: 0.0, sigma: 1.0, scale: :CCS_LINEAR, quantization: 0.0)
Brice Videau's avatar
Brice Videau committed
171
      if handle
Brice Videau's avatar
Brice Videau committed
172
        super(handle, retain: retain)
Brice Videau's avatar
Brice Videau committed
173 174 175
      else
        ptr = MemoryPointer::new(:ccs_distribution_t)
        res = if data_type == :CCS_NUM_FLOAT
Brice Videau's avatar
Brice Videau committed
176
            CCS.ccs_create_normal_float_distribution(mu, sigma, scale, quantization, ptr)
Brice Videau's avatar
Brice Videau committed
177
          else
Brice Videau's avatar
Brice Videau committed
178
            CCS.ccs_create_normal_int_distribution(mu, sigma, scale, quantization, ptr)
Brice Videau's avatar
Brice Videau committed
179 180 181 182 183
          end
        CCS.error_check(res)
        super(ptr.read_pointer, retain: false)
      end
    end
Brice Videau's avatar
Brice Videau committed
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209

    def self.int(mu:, sigma:, scale: :CCS_LINEAR, quantization: 0)
      self::new(nil, retain: false, data_type: :CCS_NUM_INTEGER, mu: mu, sigma: sigma, scale: scale, quantization: quantization) 
    end

    def self.float(mu:, sigma:, scale: :CCS_LINEAR, quantization: 0)
      self::new(nil, retain: false, data_type: :CCS_NUM_FLOAT, mu: mu, sigma: sigma, scale: scale, quantization: quantization) 
    end

    def mu
      @mu ||= begin
        ptr = MemoryPointer::new(:ccs_numeric_t)
        res = CCS.ccs_normal_distribution_get_parameters(@handle, ptr, nil)
        CCS.error_check(res)
        ptr.read_ccs_float_t
      end
    end

    def sigma
      @sigma ||= begin
        ptr = MemoryPointer::new(:ccs_numeric_t)
        res = CCS.ccs_normal_distribution_get_parameters(@handle, nil, ptr)
        CCS.error_check(res)
        ptr.read_ccs_float_t
      end
    end
Brice Videau's avatar
Brice Videau committed
210 211
  end

Brice Videau's avatar
Brice Videau committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
  attach_function :ccs_create_roulette_distribution, [:size_t, :pointer, :pointer], :ccs_result_t
  attach_function :ccs_roulette_distribution_get_num_areas, [:ccs_distribution_t, :pointer], :ccs_result_t
  attach_function :ccs_roulette_distribution_get_areas, [:ccs_distribution_t, :size_t, :pointer, :pointer], :ccs_result_t
  class RouletteDistribution < Distribution
    add_property :num_areas, :size_t, :ccs_roulette_distribution_get_num_areas, memoize: true
    def initialize(handle = nil, retain: false, areas: [])
      if handle
        super(handle, retain: retain)
      else
        ptr = MemoryPointer::new(:ccs_distribution_t)
        p_areas = MemoryPointer::new(:ccs_float_t, areas.size)
        p_areas.write_array_of_ccs_float_t(areas)
        res = CCS.ccs_create_roulette_distribution(areas.size, p_areas, ptr)
        CCS.error_check(res)
        super(ptr.read_pointer, retain: false)
      end
    end

    def areas
      @areas ||= begin
        count = num_areas
        ptr = MemoryPointer::new(:ccs_float_t, count)
        res = CCS.ccs_roulette_distribution_get_areas(@handle, count, ptr, nil)
        CCS.error_check(res)
        ptr.read_array_of_ccs_float_t(count)
      end
    end
  end
Brice Videau's avatar
Brice Videau committed
240
end