Published
Edited
Jan 13, 2020
1 fork
10 stars
Insert cell
Insert cell
Algebra(()=>{
var f = x=>x*x;
var df = this.D(f);
return {
"f(2)" : f(1)+'',
"df(2)" : df(1)+''
}
})
Insert cell
Insert cell
Algebra(0,1,()=>{
var f = x=>x*~x;
var df = this.D(f);
return {
"f(2+i)" : f(2+1e1)+'',
"df(2+i)" : df(2+1e1)[0]+'',
}
})
Insert cell
Insert cell
Algebra(0,1,()=>{
var f = x=>x*x;
var df = this.D(f);
return {
"f(2+i)" : f(2+1e1)+'',
"df(2+i)" : df(2+1e1)+'',
}
})
Insert cell
Insert cell
Algebra(0,2,()=>{
var f = x=>Math.E**(x*(1e1));
var df = this.D(f);
return {
"f(1)" : f(1)+'',
"df(1)" : df(1)[0]+'',
"same with finite differences : ": 1000*(f(1.001)-f(1))+''
}
})
Insert cell
Insert cell
Algebra(0,2,()=>{
var f = x=>x*~x;
var df = this.Dt(f);
var v = 2 + 1e1 + 1e12;
return {
"f(2+i+k)" : f(v)+'',
"df(2+i+k) (transpose!)" : df(v)[0]+'',
"df(2+i+k) (same with forward differences)" :
(f(v+0.001)-f(v))*1000+
(f(v+0.001e1)-f(v))*1000e1+
(f(v+0.001e2)-f(v))*1000e2+
(f(v+0.001e12)-f(v))*1000e12+
'',
}
})
Insert cell
Insert cell
Insert cell
Algebra(2,()=>{

// Our cost function, we exponentiate the guess, rotate the 'from', subtract the 'to' and return the length.
var cost = (guess,from,to)=>{
var motor = Math.E**guess; // exponentiate guess
var r = (motor*from*~motor - to); // apply motor, subtract 'to'
return r*~r; // return length
};
// The derivative of the cost function w.r.t the guess. (always w.r.t the first parameter!)
var dcost = this.Dt(cost);
// Starting guess, from and to vectors.
var guess = 0, from = 1e1+1e2, to = 1e1-1e2;
// 30 gradient descent steps.
for (var i=0, rate=0.05; i<30; i++) {
var c = dcost(guess,from,to);
var guess = guess + rate*c[0]; // we only use c[0] (the others are zero as our cost function returns a scalar!)
}
// Output result
return {cost:cost(guess,from,to).s,guess:guess+'',"pi/4":Math.PI/4}

})
Insert cell
Insert cell
Algebra(4,1,()=>{
// Conformal no, ni and up function.
var [no,ni,up] = [.5e5-.5e4,1e4+1e5,x=>no+x+x*x*.5*ni];
// Shorthand for random between -1 and 1
var r = ()=>Math.random()*2-1;
// A bunch of random points.
var points = [...Array(10)].map(x=>up(r*1e1+r*1e2+r*1e3));
// The rotor we'll try to estimate
var rotor = Math.E ** (r*1e12 + r*1e13);
// The transformed points.
var points2 = rotor >>> points;
// The cost function for a given guess and single sample.
var cost = (guess,from,to)=>{
var est = guess >>> from; // transform 'from'
var dif = est-to; // difference with expected value.
return dif*~dif; // norm of difference.
}
// The derivative of the cost function : for free.
var dcost = this.Dt(cost);
// Our initial guess and GD rate
var guess = 1, rate = 0.1/points.length;
// max 50 steps of gradient descent
for (var i=0; i<50; i++) {
// Accumulate cost over all samples.
var c = 0; for (var j=0; j<points.length; j++) c = c + dcost(guess,points[j],points2[j]);
// Update guess
guess = guess + rate * c[0];
// Force a pure rotation result.
guess.e14 = guess.e24 = guess.e34 = guess.e15 = guess.e25 = guess.e35 = guess.e45 = 0;
// Calculate the error
var err = 0; for (var j=0; j<points.length; j++) err = err + cost(guess,points[j],points2[j]);
if (Math.abs(err.s)/points.length<0.0005) break;
}
// graph helpers.
var trail = (g,p,n=32)=>[...Array(n)].map((x,i)=>((1-i/(n-1))+i/(n-1)*g)>>>p).map((x,i,a)=>[x,a[i-1]||x]);
// Now graph it.
return this.graph([
no,"steps : "+i,"error : "+(err.s/points.length).toFixed(5),
!1e12,!1e13,!1e23,
0x0000ff,...points,
0xff00ff,...points2,
0x00ff00,...points.map(x=>trail(guess,x)).flat()
],{conformal:1,animate:1,spin:0.1,gl:1,width:'800px',grid:1,height:'600px'})

})
Insert cell
Insert cell
Algebra=require('ganja.js@1.0.164');
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more