Quantizer

A Quantizer, once configured with index values in multiple dimensions, allows you to transform one multi-dimensional index point into a single integer representation.

This representation also searches for the closest index in each dimension. such a representation can be useful in conjunction with the GaussianProfile where the single integer representation points a gaussian of the GaussianProfile.

Each index dimension can either be sparse or dense (or uniform):

use util;

fn main() {

  var q = Quantizer::new();

  // 2 dimensions:
  q.configure([ [1, 3, 25, 50], DenseDim {min:0, max: 100, step: 10} ]);

  var pt = [10, 50];

  var qpt = q.quantize(pt);

  println("in ${q.dimensions()}-dimension space sized ${q.size()}, point ${pt} is found at ${qpt}"); 

}

Displays: in 2-dimension space sized 44, point [10,50] is found at 27

Another example illustrates a typical usage:

use util;

enum Region { North (0); South (1); East (2); West (3); }

fn main() {
  var q = Quantizer::new();

  // 2 dimensions:
  q.configure([
  	DenseDim {min:1, max:12, step: 1}, // months
	[2012, 2019, 2022, 2023], // years, sparse
	DenseDim {min:0, max:3, step: 3},
  ]);

  var pt = [3, 2020, Region::West as int];

  var qpt = q.quantize(pt);

  println("in ${q.dimensions()}-dimension space sized ${q.size()}, point ${pt} is found at ${qpt}"); 
}

Displays: in 3-dimension space sized 96, point [3,2020,3] is found at 21

Reference

native type Quantizer {
  static native fn new(): Quantizer;
  /// per dimension, configure either a sparse dimension (Array<int>), or uniform dimension (DenseDim)
  /// overwrites previous configuration.
  native fn configure(dim: Array<any>);
  /// the total size of the configured space.
  native fn size(): int;
  /// the number of dimensions configured for the quantizer.
  native fn dimensions(): int;
  /// an index to the closest element in the configured space.
  native fn quantize(keys: Array<int>): int;
}

/// Defines a dense (uniform) dimension.
type DenseDim {
  min: int;
  max: int;
  step: int;
}