private static void fdjac(double[] x, double[] fvec, double[,] df, EquationSet objEquationSet) { const double EPS = 1E-08; double h, temp; int n = x.Length; double[] f = new double[n]; for (int j = 0; j < n; j++) { temp = x[j]; h = EPS * Abs(temp); if (h == 0) { h = EPS; } x[j] = temp + h; h = x[j] - temp; objEquationSet(x, f); x[j] = temp; for (int i = 0; i < n; i++) { df[i, j] = (f[i] - fvec[i]) / h; } } }
public static bool[] FindRoot(double[] x, EquationSet objEquationSet) { bool Check = false; bool timesUP = newt(x, objEquationSet, Check); bool[] states = new bool[] { timesUP, Check }; return(states); }
private static double fmin(double[] x, double[] fvec, EquationSet objEquationSet) { objEquationSet(x, fvec); int n = x.Length; double sum = 0.0; for (int i = 0; i < n; i++) { sum = sum + fvec[i] * fvec[i]; } return(0.5 * sum); }
private void Update() { if (queue.Count != 0) { if (isRunning == false) { EquationSet es = queue.Dequeue(); var go = new GameObject("Mesh Visualizer" + meshVisuals.Count); go.transform.SetParent(transform); go.transform.localPosition = Vector3.zero; go.transform.localRotation = Quaternion.identity; var mrenderer = go.AddComponent <MeshRenderer>(); mrenderer.material = new Material(Shader.Find("Wireframe")); MeshFilter meshVisual = go.AddComponent <MeshFilter>(); meshVisuals.Add(meshVisual); uMin = es.uMin; uMax = es.uMax; vMin = es.vMin; vMax = es.vMax; solver.SetGlobalVariable("u", uMin * Mathf.PI); solver.SetGlobalVariable("v", vMin * Mathf.PI); expX = solver.SymbolicateExpression(es.exprX); expY = solver.SymbolicateExpression(es.exprY); expZ = solver.SymbolicateExpression(es.exprZ); varU = solver.GetGlobalVariable("u"); varV = solver.GetGlobalVariable("v"); meshVisual.transform.localScale = Vector3.one * es.scale; positions.Clear(); normals.Clear(); faces.Clear(); uvs.Clear(); mappingCache = new Dictionary <Vector2, Vector3>(); Vector3 v0 = MapPoint01(0, 0); Vector3 n0 = GetNormal01(0, 0); positions.Add(v0); normals.Add(n0); uvs.Add(new Vector2(0, 0)); Vector3 v1 = MapPoint01(0, 1); Vector3 n1 = GetNormal01(0, 1); positions.Add(v1); normals.Add(n1); uvs.Add(new Vector2(0, 1)); Vector3 v2 = MapPoint01(1, 1); Vector3 n2 = GetNormal01(1, 1); positions.Add(v2); normals.Add(n2); uvs.Add(new Vector2(1, 1)); Vector3 v3 = MapPoint01(1, 0); Vector3 n3 = GetNormal01(1, 0); positions.Add(v3); normals.Add(n3); uvs.Add(new Vector2(1, 0)); Tessellate(ref meshVisual, 0, 1, 2, 3); //tessel = StartCoroutine(Tessellate(0, 1, 2, 3)); } } }
private void Update() { if (queue.Count != 0) { if (isRunning == false) { EquationSet es = queue.Dequeue(); uMin = es.uMin; uMax = es.uMax; vMin = es.vMin; vMax = es.vMax; solver.SetGlobalVariable("u", uMin * Mathf.PI); solver.SetGlobalVariable("v", vMin * Mathf.PI); expX = solver.SymbolicateExpression(es.exprX); expY = solver.SymbolicateExpression(es.exprY); expZ = solver.SymbolicateExpression(es.exprZ); varU = solver.GetGlobalVariable("u"); varV = solver.GetGlobalVariable("v"); positions.Clear(); normals.Clear(); faces.Clear(); uvs.Clear(); mappingCache = new Dictionary <Vector2, Vector3>(); Vector3 v0 = MapPoint01(0, 0); Vector3 n0 = GetNormal01(0, 0); positions.Add(v0); normals.Add(n0); uvs.Add(new Vector2(0, 0)); Vector3 v1 = MapPoint01(0, 1); Vector3 n1 = GetNormal01(0, 1); positions.Add(v1); normals.Add(n1); uvs.Add(new Vector2(0, 1)); Vector3 v2 = MapPoint01(1, 1); Vector3 n2 = GetNormal01(1, 1); positions.Add(v2); normals.Add(n2); uvs.Add(new Vector2(1, 1)); Vector3 v3 = MapPoint01(1, 0); Vector3 n3 = GetNormal01(1, 0); positions.Add(v3); normals.Add(n3); uvs.Add(new Vector2(1, 0)); Tessellate(0, 1, 2, 3); //tessel = StartCoroutine(Tessellate(0, 1, 2, 3)); } } }
private static bool newt(double[] x, EquationSet objEquationSet, bool Check) { const int MAXITS = 200; const double TOLF = 1E-08; const double TOLMIN = 1E-10; const int STPMX = 100; const double TOLX = 1E-11; bool timsUP = false; double den, f, fold, stpmax, sum, temp, test; double d = 0; int n = x.Length; int[] indx = new int[n]; double[] g = new double[n]; double[] p = new double[n]; double[] xold = new double[n]; double[,] fjac = new double[n, n]; double[] fvec = new double[n]; f = fmin(x, fvec, objEquationSet); test = 0.0; for (int i = 0; i < n; i++) { if ((Abs(fvec[i]) > test)) { test = Abs(fvec[i]); } } if (test < 0.01 * TOLF) { Check = false; return(timsUP); } sum = 0; for (int i = 0; i < n; i++) { sum = sum + x[i] * x[i]; } stpmax = STPMX * Max(Sqrt(sum), Convert.ToDouble(n)); for (int its = 0; its < MAXITS; its++) { fdjac(x, fvec, fjac, objEquationSet); for (int i = 0; i < n; i++) { sum = 0; for (int j = 0; j < n; j++) { sum = sum + fjac[j, i] * fvec[j]; } g[i] = sum; } for (int i = 0; i < n; i++) { xold[i] = x[i]; } fold = f; for (int i = 0; i < n; i++) { p[i] = -fvec[i]; } ludcmp(fjac, indx, d); lubksb(fjac, indx, p); timsUP = lnsrch(xold, fold, g, p, x, f, stpmax, Check, fvec, objEquationSet); if (timsUP) { x[0] = 0; return(timsUP); } test = 0.0; for (int i = 0; i < n; i++) { if (Abs(fvec[i]) > test) { test = Abs(fvec[i]); } } if (test < TOLF) { Check = false; return(timsUP); } if (Check) { test = 0.0; den = Max(f, 0.5 * n); for (int i = 0; i < n; i++) { temp = Abs(g[i]) * Max(Abs(x[i]), 1.0) / den; if (temp > test) { test = temp; } } Check = (test < TOLMIN); return(timsUP); } test = 0.0; for (int i = 0; i < n; i++) { temp = (Abs(x[i] - xold[i])) / Max(Abs(x[i]), 1.0); if (temp > test) { test = temp; } } if (test < TOLX) { return(timsUP); } } return(timsUP); }
private static bool lnsrch(double[] xold, double fold, double[] g, double[] p, double[] x, double f, double stpmax, bool Check, double[] fvec, EquationSet objEquationSet) { const double ALF = 1E-08; const double TOLX = 1E-11; double a, alam, alam2 = 0.0, alamin, b, disc, f2 = 0.0; double rhs1, rhs2, slope, sum, temp, test, tmplam; int n = xold.Length; Check = false; sum = 0.0; for (int i = 0; i < n; i++) { sum = sum + p[i] * p[i]; } sum = Sqrt(sum); if (sum > stpmax) { for (int i = 0; i < n; i++) { p[i] = p[i] * stpmax / sum; } } slope = 0.0; for (int i = 0; i < n; i++) { slope = slope + g[i] * p[i]; } if (slope >= 0.0) { nError = "Roundoff problem in lnsrch."; } test = 0.0; for (int i = 0; i < n; i++) { temp = Abs(p[i]) / Max(Abs(xold[i]), 1); if (temp > test) { test = temp; } } alamin = TOLX / test; alam = 1.0; DateTime startT = DateTime.Now; int i_times = 0; while (true) { if (i_times == 100) { i_times = 0; if (DateTime.Now - startT > TimeSpanLimit) { Debug.WriteLine("NonLinear Find Root Overtime({0}s)", TimeSpanLimit.Seconds); return(true); } } for (int i = 0; i < n; i++) { x[i] = xold[i] + alam * p[i]; } f = fmin(x, fvec, objEquationSet); if (alam < alamin) { for (int i = 0; i < n; i++) { x[i] = xold[i]; } Check = true; return(false); } else if (f <= fold + ALF * alam * slope) { return(false); } else { if (alam == 1.0) { tmplam = -slope / (2.0 * (f - fold - slope)); } else { rhs1 = f - fold - alam * slope; rhs2 = f2 - fold - alam2 * slope; a = (rhs1 / (alam * alam) - rhs2 / (alam2 * alam2)) / (alam - alam2); b = (-alam2 * rhs1 / (alam * alam) + alam * rhs2 / (alam2 * alam2)) / (alam - alam2); if (a == 0.0) { tmplam = -slope / (2.0 * b); } else { disc = b * b - 3.0 * a * slope; if (disc < 0.0) { tmplam = 0.5 * alam; } else if (b <= 0.0) { tmplam = (-b + Sqrt(disc)) / (3.0 * a); } else { tmplam = -slope / (b + Sqrt(disc)); } } if (tmplam > 0.5 * alam) { tmplam = 0.5 * alam; } } } alam2 = alam; f2 = f; alam = Max(tmplam, 0.1 * alam); i_times++; } }