git.delta.rocks / unique-network / refs/commits / 1aa9f363fc18

difftreelog

perf(calibrate) optimize fraction

Yaroslav Bolyukin2022-12-19parent: #e642a25.patch.diff
in: master

1 file changed

modifiedtests/src/calibrate.tsdiffbeforeafterboth
9 }9 }
1010
11 mul(other: Fract) {11 mul(other: Fract) {
12 return new Fract(this.a * other.a, this.b * other.b);12 return new Fract(this.a * other.a, this.b * other.b).optimize();
13 }13 }
1414
15 div(other: Fract) {15 div(other: Fract) {
16 return this.mul(other.inv());16 return this.mul(other.inv());
17 }17 }
1818
19 plus(other: Fract) {19 plus(other: Fract) {
20 if (this.b === other.b) {
21 return new Fract(this.a + other.a, this.b);
22 }
20 return new Fract(this.a * other.b + other.a * this.b, this.b * other.b);23 return new Fract(this.a * other.b + other.a * this.b, this.b * other.b).optimize();
21 }24 }
2225
23 minus(other: Fract) {26 minus(other: Fract) {
28 return new Fract(-this.a, this.b);31 return new Fract(-this.a, this.b);
29 }32 }
30 inv() {33 inv() {
34 if (this.a < 0) {
35 return new Fract(-this.b, -this.a);
36 } else {
31 return new Fract(this.b, this.a);37 return new Fract(this.b, this.a);
32 }38 }
39 }
40
41 optimize() {
42 function gcd(x: bigint, y: bigint) {
43 if (x < 0n)
44 x = -x;
45 if (y < 0n)
46 y = -y;
47 while(y) {
48 const t = y;
49 y = x % y;
50 x = t;
51 }
52 return x;
53 }
54 const v = gcd(this.a, this.b);
55 return new Fract(this.a / v, this.b / v);
56 }
3357
34 toBigInt() {58 toBigInt() {
35 return this.a / this.b;59 return this.a / this.b;
36 }60 }
61 toNumber() {
62 const v = this.optimize();
63 return Number(v.a) / Number(v.b);
64 }
65 toString() {
66 const v = this.optimize();
67 return `${v.a} / ${v.b}`;
68 }
3769
38 lt(other: Fract) {70 lt(other: Fract) {
39 return this.a * other.b < other.a * this.b;71 return this.a * other.b < other.a * this.b;
142174
143const hypothesisLinear = (a: Fract, b: Fract) => (x: Fract) => rpn(x, a, '*', b, '+');175const hypothesisLinear = (a: Fract, b: Fract) => (x: Fract) => rpn(x, a, '*', b, '+');
144176
145// JS has no builtin function to calculate sqrt of bigint
146// https://stackoverflow.com/a/53684036/6190169
147function error(points: { x: Fract, y: Fract }[], hypothesis: (a: Fract) => Fract) {177function error(points: { x: Fract, y: Fract }[], hypothesis: (a: Fract) => Fract) {
148 return points.map(p => {178 return points.map(p => {
149 const v = hypothesis(p.x);179 const v = hypothesis(p.x);
165 await token.transfer(alice, {Substrate: bob.address});195 await token.transfer(alice, {Substrate: bob.address});
166 const aliceBalanceAfter = await helper.balance.getSubstrate(alice.address);196 const aliceBalanceAfter = await helper.balance.getSubstrate(alice.address);
167197
168 console.log(`\t[NFT Transfer] Original price: ${Number(aliceBalanceBefore - aliceBalanceAfter) / Number(helper.balance.getOneTokenNominal())} UNQ`);198 console.log(`\t[NFT transfer] Original price: ${Number(aliceBalanceBefore - aliceBalanceAfter) / Number(helper.balance.getOneTokenNominal())} UNQ`);
169 }199 }
170200
171 const api = helper.getApi();201 const api = helper.getApi();
188 const {a, b} = linearRegression(dataPoints);218 const {a, b} = linearRegression(dataPoints);
189219
190 const hyp = hypothesisLinear(a, b);220 const hyp = hypothesisLinear(a, b);
191 console.log(`Error: ${error(dataPoints, hyp)}`);221 console.log(`\t[NFT transfer] Error: ${error(dataPoints, hyp).toNumber()}`);
192222
193 // 0.1 UNQ223 // 0.1 UNQ
194 const perfectValue = hyp(rpn(new Fract(helper.balance.getOneTokenNominal()), new Fract(1n, 10n), '*'));224 const perfectValue = hyp(rpn(new Fract(helper.balance.getOneTokenNominal()), new Fract(1n, 10n), '*'));
201 await token.transfer(alice, {Substrate: bob.address});231 await token.transfer(alice, {Substrate: bob.address});
202 const aliceBalanceAfter = await helper.balance.getSubstrate(alice.address);232 const aliceBalanceAfter = await helper.balance.getSubstrate(alice.address);
203233
204 console.log(`\t[NFT Transfer] Calibrated price: ${Number(aliceBalanceBefore - aliceBalanceAfter) / Number(helper.balance.getOneTokenNominal())} UNQ`);234 console.log(`\t[NFT transfer] Calibrated price: ${Number(aliceBalanceBefore - aliceBalanceAfter) / Number(helper.balance.getOneTokenNominal())} UNQ`);
205 }235 }
206}236}
207237
246 const {a, b} = linearRegression(dataPoints);276 const {a, b} = linearRegression(dataPoints);
247277
248 const hyp = hypothesisLinear(a, b);278 const hyp = hypothesisLinear(a, b);
249 console.log(`Error: ${error(dataPoints, hyp)}`);279 console.log(`\t[ETH NFT transfer] Error: ${error(dataPoints, hyp).toNumber()}`);
250280
251 // 0.15 UNQ281 // 0.15 UNQ
252 const perfectValue = hyp(rpn(new Fract(helper.balance.getOneTokenNominal()), new Fract(15n, 100n), '*'));282 const perfectValue = hyp(rpn(new Fract(helper.balance.getOneTokenNominal()), new Fract(15n, 100n), '*'));