using System; using static System.Console; using static System.Math; public static partial class qnewton{ public static readonly double DELTA=Pow(2,-26); public static readonly double lambda_min=Pow(2,-12); public static vector gradient(Funcf,vector x){ vector g=new vector(x.size); double fx=f(x); for(int i=0;iphi,vector x){ matrix H=new matrix(x.size); Func f = z => gradient(phi,z); double DELTA=Pow(2,-13); vector dx=x.map(t => Abs(t)*DELTA); vector fx=f(x); for(int j=0;jf,ref vector x, double acc=1e-3, string method="sr1"){ double fx=f(x); vector gx=gradient(f,x); /* matrix H=hessian(f,x); var qrH=new givensQR(H); matrix B=qrH.inverse(); */ matrix B=matrix.id(x.size); int nsteps=0; while(nsteps<1000){ if(gx.norm()1e-6){ double gamma=u%y/sTy/2; vector a=(u-gamma*s)/sTy; B.update(a,s); B.update(s,a); } } else if(method=="sr1"){ double uTy=u%y; if(Abs(uTy)>1e-6){ B.update(u,u,1/uTy); } } else throw new ArgumentException("qnewton: unknown method"); x=z; gx=gz; fx=fz; }//while return nsteps; }//root }//class