private static Vector ComputeWeights(Matrix X, Matrix d, double lambda, double minDistance, int maxIterations) { // k+1, n int k = X.RowCount, n = X.ColumnCount; // Precompute the penalty term var penalty = Matrix.Identity(k, k); penalty.Multiply(lambda); penalty[k - 1, k - 1] = 0; Matrix p, g, H, pMinusD = new Matrix(n, 1), w, wNew = new Matrix(k, 1), Xt; double[] diag = new double[n]; int iters = 0; do { w = wNew; // p = f(wT * X) w.Transpose(); p = w.Multiply(X); w.Transpose(); for (int i = 0; i < n; i++) { p[0, i] = CalcLogistic(p[0, i]); pMinusD[i, 0] = p[0, i] - d[0, i]; diag[i] = p[0, i] * (1 - p[0, i]); } // g = X * (d - p) - penalty * w g = X.Multiply(pMinusD); g.Subtract(penalty.Multiply(w)); // H = X*diag(p.*(1-p))*XT + penalty Xt = X.Clone(); Xt.Transpose(); Xt.Multiply(diag); H = X.Multiply(Xt); H.Add(penalty); // w = w + H^-1 * g try { wNew = H.Inverse().Multiply(g); } catch (Exception) { break; } //wNew = H.Inverse().Multiply(g); wNew.Add(w); } while (++iters != maxIterations && GetDistance(w, wNew) >= minDistance); return(wNew.GetColumnVector(0)); }
public HarmonicOscillatorModelingResultViewModel(Tuple <List <double>, List <double>, List <double>, List <double> > numeric) { for (var i = 0; i < numeric.Item1.Count; ++i) { Xt.Add(new ObservablePoint(numeric.Item1[i], numeric.Item2[i])); Vt.Add(new ObservablePoint(numeric.Item1[i], numeric.Item3[i])); E.Add(new ObservablePoint(numeric.Item1[i], numeric.Item4[i])); Vx.Add(new ObservablePoint(numeric.Item2[i], numeric.Item3[i])); } Collection.Add(new LineSeries() { Values = Xt }); }
public HorizontalThrowModelingResultViewModel( Tuple <List <double>, List <Tuple <double, double> >, List <Tuple <double, double> > > numericValues) { List <double> ts = numericValues.Item1; List <Tuple <double, double> > xy = numericValues.Item2, vs = numericValues.Item3; for (int i = 0; i < ts.Count; ++i) { Yt.Add(new ObservablePoint(ts[i], xy[i].Item2)); Xt.Add(new ObservablePoint(ts[i], xy[i].Item1)); Yx.Add(new ObservablePoint(xy[i].Item1, xy[i].Item2)); Vxt.Add(new ObservablePoint(ts[i], vs[i].Item1)); Vyt.Add(new ObservablePoint(ts[i], vs[i].Item2)); } Collection.Add(new LineSeries() { Values = Yx }); }
/* Main function to solve the linear regression*/ static double[] solve_LR(Matrix <double> X, Matrix <double> Y) { int r = X.RowCount; int c = X.ColumnCount; Matrix <double> Xt, XtX, inv, invXt, mResult; Xt = X.Transpose(); XtX = Xt.Multiply(X); inv = XtX.Inverse(); invXt = inv.Multiply(Xt); mResult = invXt.Multiply(Y); double[] result = new double[mResult.RowCount * mResult.ColumnCount]; for (int i = 0; i < mResult.RowCount; i++) { for (int j = 0; j < mResult.ColumnCount; j++) { result[(i * mResult.ColumnCount) + j] = mResult[i, j]; } } return(result); }
public static extern void TNK_IMP_Xm_XmRemoveWMProtocolCallback( IntPtr w, [MarshalAs(UnmanagedType.LPStr)] string name, Xt.G.XtCallBack call );
public static void XmRemoveWMProtocolCallback( Native.WidgetHandle w, string name, Xt.G.XtCallBack call ) { NativeMethods.TNK_IMP_Xm_XmRemoveWMProtocolCallback(w.Widget, name, call ); }
public string[] Convert(string[] val, GeoUnits from, GeoUnits to, ISpatialParameters parameters) { if (val.Length != 2) { return(null); } double x = 0.0, y = 0.0, phi = 0.0; if (from == GeoUnits.DegreesMinutesSeconds) { // Umwandelt in double } else { if (!double.TryParse(val[0], out x)) { return(null); } if (!double.TryParse(val[1], out y)) { return(null); } } if (from == GeoUnits.DegreesMinutesSeconds || from == GeoUnits.DecimalDegrees) { if (parameters != null) { x += parameters.lon_0; y += parameters.lat_0; } phi = y; } else { if (parameters != null) { x -= Convert(parameters.x_0, parameters.Unit, from); y -= Convert(parameters.y_0, parameters.Unit, from); } } double Xm = ToMeters(x, from, phi, 1); double Ym = ToMeters(y, from, 0.0, 1); double Yt = FromMeters(Ym, to, 0.0, 1), Xt; if (to == GeoUnits.DegreesMinutesSeconds || to == GeoUnits.DecimalDegrees) { phi = Yt + ((parameters != null) ? parameters.lat_0 : 0.0); Xt = FromMeters(Xm, to, phi, 1) + ((parameters != null) ? parameters.lon_0 : 0.0); } else { Xt = FromMeters(Xm, to, 0.0, 1); } if (to == GeoUnits.DegreesMinutesSeconds) { return(new string[] { deg2GMS(Xt, 2), deg2GMS(Yt, 2) }); } else if (to == GeoUnits.DecimalDegrees) { Xt = Math.Round(Xt, 2); Yt = Math.Round(Yt, 2); return(new string[] { Xt.ToString(), Yt.ToString() }); } else { if (parameters != null) { Xt += Convert(parameters.x_0, parameters.Unit, to); Yt += Convert(parameters.y_0, parameters.Unit, to); } Xt = Math.Round(Xt, 2); Yt = Math.Round(Yt, 2); return(new string[] { String.Format("{0:+0.00;-0.00;}", Xt), String.Format("{0:+0.00;-0.00;}", Yt) }); } }