The OpenD Programming Language

scale

Parameters

slice Slice!(Iterator, N, kind)

slice

m T

value to subtract from slice

d U

value to divide slice by

Examples

Scale vector

import mir.algorithm.iteration: all;
import mir.math.common: approxEqual;
import mir.ndslice.slice: sliced;
import mir.stat.descriptive.univariate: mean, gmean, hmean, median, standardDeviation;

auto x = [1.0, 2, 3, 4, 5, 6].sliced;

assert(x.scale.all!approxEqual([-1.336306, -0.801784, -0.267261, 0.267261, 0.801784, 1.336306]));
assert(x.scale(3.5, 1.87083).all!approxEqual([-1.336306, -0.801784, -0.267261, 0.267261, 0.801784, 1.336306]));

// Can scale using different `centralTendency` functions
assert(x.scale!hmean.all!approxEqual([-0.774512, -0.23999, 0.294533, 0.829055, 1.363578, 1.898100]));
assert(x.scale!gmean.all!approxEqual([-1.065728, -0.531206, 0.003317, 0.537839, 1.072362, 1.606884]));
assert(x.scale!median.all!approxEqual([-1.336306, -0.801784, -0.267261, 0.267261, 0.801784, 1.336306]));

// Can scale using different `centralTendency` and `dispersion` functions
assert(x.scale!(mean, a => a.standardDeviation(true)).all!approxEqual([-1.46385, -0.87831, -0.29277, 0.29277, 0.87831, 1.46385]));
assert(x.scale!(hmean, a => a.standardDeviation(true)).all!approxEqual([-0.848436, -0.262896, 0.322645, 0.908185, 1.493725, 2.079265]));
assert(x.scale!(gmean, a => a.standardDeviation(true)).all!approxEqual([-1.167447, -0.581907, 0.003633, 0.589173, 1.174713, 1.760253]));
assert(x.scale!(median, a => a.standardDeviation(true)).all!approxEqual([-1.46385, -0.87831, -0.29277, 0.29277, 0.87831, 1.46385]));

Scale dynamic array

import mir.algorithm.iteration: all;
import mir.math.common: approxEqual;

auto x = [1.0, 2, 3, 4, 5, 6];
assert(x.scale.all!approxEqual([-1.336306, -0.801784, -0.267261, 0.267261, 0.801784, 1.336306]));
assert(x.scale(3.5, 1.87083).all!approxEqual([-1.336306, -0.801784, -0.267261, 0.267261, 0.801784, 1.336306]));

Scale matrix

import mir.algorithm.iteration: all;
import mir.math.common: approxEqual;
import mir.ndslice.fuse: fuse;

auto x = [
    [1.0, 2, 3], 
    [4.0, 5, 6]
].fuse;

assert(x.scale.all!approxEqual([[-1.336306, -0.801784, -0.267261], [0.267261, 0.801784, 1.336306]]));
assert(x.scale(3.5, 1.87083).all!approxEqual([[-1.336306, -0.801784, -0.267261], [0.267261, 0.801784, 1.336306]]));

Column scale matrix

import mir.algorithm.iteration: all, equal;
import mir.math.common: approxEqual;
import mir.ndslice.fuse: fuse;
import mir.ndslice.topology: alongDim, byDim, map;

auto x = [
    [20.0, 100.0, 2000.0],
    [10.0,   5.0,    2.0]
].fuse;

auto result = [
    [ 0.707107,  0.707107,  0.707107],
    [-0.707107, -0.707107, -0.707107]
].fuse;

// Use byDim with map to scale by row/column.
auto xScaleByDim = x.byDim!1.map!scale;
auto resultByDim = result.byDim!1;
assert(xScaleByDim.equal!(equal!approxEqual)(resultByDim));

auto xScaleAlongDim = x.alongDim!0.map!scale;
auto resultAlongDim = result.alongDim!0;
assert(xScaleAlongDim.equal!(equal!approxEqual)(resultAlongDim));

Can also pass arguments to mean and standardDeviation functions used by scale

import mir.algorithm.iteration: all;
import mir.math.common: approxEqual;
import mir.ndslice.slice: sliced;
import mir.stat.descriptive.univariate: mean, standardDeviation;

//Set sum algorithm
auto a = [1, 1e100, 1, -1e100];

auto x = a.sliced * 10_000;

auto result = [6.123724e-101, 1.224745, 6.123724e-101, -1.224745].sliced;

assert(x.scale!(mean!"kbn", standardDeviation!("online", "kbn")).all!approxEqual(result));
assert(x.scale!(mean!"kb2", standardDeviation!("online", "kb2")).all!approxEqual(result));
assert(x.scale!(mean!"precise", standardDeviation!("online", "precise")).all!approxEqual(result));

Meta