+
+function invert(table) {
+ // Here we solve
+ // net = m * gross + b
+ // for gross:
+ // net - b = m * gross
+ // (net - b) / m = gross
+
+ const ms = table.map(([start, end, rate]) => 1 - rate);
+ const full_brackets = [0].concat(table.map(([start, end, rate]) => (end - start) * rate)).slice(0, table.length);
+ function sum_lower_brackets(remaining_brackets, acc = 0) {
+ if (remaining_brackets.length == 0) return [];
+ return [acc + remaining_brackets[0]].concat(sum_lower_brackets(remaining_brackets.slice(1), acc + remaining_brackets[0]));
+ }
+ const bs = sum_lower_brackets(full_brackets).map((lower_brackets, i) => {
+ const [start, end, rate] = table[i];
+ // Finding b:
+ // net = gross - lower_brackets - rate * (gross - start)
+ // net = gross - lower_brackets - rate * gross + rate * start
+ // net = gross - rate * gross - lower_brackets + rate * start
+ // net = (1 - rate) * gross - lower_brackets + rate * start
+ // net = m * gross - lower_brackets + rate * start
+ // \_____________________________/ - here is b
+ return rate * start - lower_brackets;
+ });
+ const inverse_table = table.map(([start, end, rate], i) => {
+ const m = ms[i];
+ const b = bs[i];
+ return [m * start + b, m * end + b, m, b];
+ });
+ return function(net) {
+ for (const [start, end, m, b] of inverse_table) {
+ if (less_than_or_near(start, net) && less_than_or_near(net, end)) {
+ return (net - b) / m;
+ }
+ }
+ return net;
+ };
+}