← 返回数学手册

public/learn/models.mjs

与本次数学页面一起发布的源码快照。SHA-256: dbe94eb71cdfa68a0715bba809f53c6f12564b7fbe97a08e7fc72b04119e0457

下载原始文件 · 版本指纹清单

1// Pure arithmetic used by course controls and independent numerical checks.
2export const sigmoid=x=>1/(1+Math.exp(-x));
3export function kernel(x,scale,opacity=1){return opacity*Math.exp(-0.5*(x/scale)**2);}
4export function cameraSample(x,z,f,scale=0.2){
5 const u=64+f*x/z,j=[f/z,-f*x/(z*z)];
6 return {u,variance:scale**2*(j[0]**2+j[1]**2)+0.3,width:Math.sqrt(scale**2*(j[0]**2+j[1]**2)+0.3)};
7}
8export function composite(a,b,reverse=false){
9 const layers=reverse?[[b,[0,0,1],'B'],[a,[1,0,0],'A']]:[[a,[1,0,0],'A'],[b,[0,0,1],'B']];
10 let remaining=1;const color=[0,0,0],weights=[];
11 for(const [alpha,rgb,id] of layers){const weight=remaining*alpha;weights.push({id,weight});rgb.forEach((v,c)=>color[c]+=weight*v);remaining*=1-alpha;}
12 const over=[0,0,0];for(const [alpha,rgb] of [...layers].reverse())rgb.forEach((v,c)=>over[c]=alpha*v+(1-alpha)*over[c]);
13 return {color,over,weights,remaining};
14}
15export function scalarGradient(beta,rate=1){
16 const c=.8,target=.6,alpha=sigmoid(beta),prediction=c*alpha,loss=(prediction-target)**2;
17 const gradient=2*(prediction-target)*c*alpha*(1-alpha),next=beta-rate*gradient;
18 const lossAt=x=>(c*sigmoid(x)-target)**2,epsilon=1e-4;
19 return {alpha,prediction,loss,gradient,next,nextLoss:lossAt(next),finite:(lossAt(beta+epsilon)-lossAt(beta-epsilon))/(2*epsilon)};
20}
21export function duplication(alpha){return {unchanged:1-(1-alpha)**2,halved:1-(1-alpha/2)**2,child:1-Math.sqrt(1-alpha),preserved:alpha};}
22export function resizeSample(r,wrong=false){return {focal:wrong?600:600*r,principal:400*r,expected:460*r,actual:(wrong?600:600*r)*.1+400*r};}
23export function memoryBudget(count,visible,fps){return {records:count*128,indices:count*visible*4,perSecond:count*visible*4*fps};}
24