private float getOptimalTau(PointInfo goalPointInfo, PointInfo startPointInfo, /*params for Newton*/ float t0 = 20, int max_n = 10, float r = 2.0f) { float p_x0 = startPointInfo.pos.x; float p_y0 = startPointInfo.pos.z; float v_x0 = startPointInfo.vel.x; float v_y0 = startPointInfo.vel.z; float p_x1 = goalPointInfo.pos.x; float p_y1 = goalPointInfo.pos.z; float v_x1 = goalPointInfo.vel.x; float v_y1 = goalPointInfo.vel.z; float a = ((24 * r * v_x1 * v_x0) + (12 * r * v_x1 * (v_x1 - v_x0)) - (4 * r * Mathf.Pow(2 * (v_x1 - v_x0) - 3 * v_x0, 2)) - (4 * r * Mathf.Pow(2 * (v_y1 - v_y0) - 3 * v_y0, 2))); float b = (-24 * r * v_x1 * (p_x1 - p_x0) + 24 * r * (2 * (v_x1 - v_x0) - 3 * v_x0) * (p_x1 - p_x0) + 24 * r * (2 * (v_y1 - v_y0) - 3 * v_y0) * (p_y1 - p_y0)); float c = -36 * r * ((p_x1 - p_x0) * (p_x1 - p_x0) + (p_y1 - p_y0) * (p_y1 - p_y0)); float[] coefficients = new float[5]; coefficients [0] = c; coefficients [1] = b; coefficients [2] = a; coefficients [3] = 0; coefficients [4] = 1; return(NewtonSolver.Newton(coefficients, t0, max_n)); }
public double GetTangentModulus(double ratio) { double stress = ratio * SoilModel.GetFailureStress(); var solver = new NewtonSolver(x => SoilModel.GetDeviatoricStress(x) - stress, 0); double eps = solver.Solve(); var derivative = new Derivative(x => SoilModel.GetDeviatoricStress(x), DerivativeAccuracy); return(derivative.GetFirstDerivative(eps)); }
public static double CalculateImpliedVolCall(double C, double S, double k, double r, double T, double x0 = 0.5, double maxErr = 1e-6, int N = 10000) { Func <double, double> F = (x) => { return(C - BlackScholesFormula.CalculateCallOptionPrice(x, S, k, r, T)); }; NewtonSolver s = new NewtonSolver(maxErr, N); return(s.Solve(F, null, x0)); }
public double GetSecantModulus(double ratio) { double stress = ratio * SoilModel.GetFailureStress(); var solver = new NewtonSolver(x => SoilModel.GetDeviatoricStress(x) - stress, 0); double eps = solver.Solve(); if (eps == 0) { throw new Exception(); } return(stress / eps); }
private static List <ISolver> DeafaultSolvers() { var opts = new NewtonOptions() { MaxIterations = 20, DerivativeStep = new double[] { 0.01 } }; var solver = new NewtonSolver(opts); return(new List <ISolver>() { solver }); }
public static double CalculateImpliedVolCall(double C, double S, double K, double r, double T, double x0 = 0.5, double maxErr = 1e-6, int N = 10000) { if (C <= 0 || T <= 0 || K <= 0 || S <= 0 || maxErr <= 0 || N <= 0) { throw new System.ArgumentException("Need C > 0, T > 0, K > 0, S > 0, maxErr > 0, N > 0."); } Func <double, double> F = (x) => { return(C - BlackScholesFormula.CalculateCallOptionPrice(x, S, K, r, T)); }; NewtonSolver s = new NewtonSolver(maxErr, N); return(s.Solve(F, null, x0)); }
/// <summary> /// Provides the revesed model, according to the specified variables /// </summary> public WorkflowReversedModel Reverse(List <Data> inputs, List <Data> outputs) { var opts = new NewtonOptions() { MaxIterations = 20, DerivativeStep = new double[] { 0.01 } }; var solver = new NewtonSolver(opts); string reversedName = GetReversedName(inputs, outputs); var reversedModel = new WorkflowReversedModel(reversedName, Description, inputs, outputs, this, new List <ISolver>() { solver }); reversedModel.Rename(FullName + new string(reversedName.SkipWhile(c => c != ':').ToArray())); return(reversedModel); }
private static List <ISolver> DeafaultSolversSCC() { var solver = new FixedPointSolver() { MaxEvals = 20 }; var opts = new NewtonOptions() { MaxIterations = 20, DerivativeStep = new double[] { 0.01 } }; var solver2 = new NewtonSolver(opts); return(new List <ISolver>() { solver, solver2 }); }
/// <summary> /// This function identifies the Modified models (from imMatrixi and imMatrif) in the modelObjects, then creates a 'modified model subprocess' for each modified model, /// and therafter combines it with other models in the modelObjects and return it in a object array /// </summary> /// <param name="IMi"></param> /// <param name="IMf"></param> /// <param name="components"></param> /// <param name="data"></param> /// <param name="clusters"></param> /// <param name="name"></param> /// <returns></returns> private static WorkflowComponent[] CreateReversedModels(IncidenceMatrix IMi, IncidenceMatrix IMf, List <WorkflowComponent> components, List <Data> data, List <Cluster> clusters, string name) { var processedComponents = new WorkflowComponent[IMf.RowCount]; for (int r = 0; r < IMi.RowCount; r++) { WorkflowComponent component = components[r]; Vector <double> row = IMi.Row(r); if (!IMf.Row(r).CompareAssigned(row)) { List <int> inputIndices = row.FindLocations(IN); List <int> outputIndices = row.FindLocations(OUT); var inputs = inputIndices.Select(i => data[i]).ToList(); var outputs = outputIndices.Select(i => data[i]).ToList(); if (component is Model model) { processedComponents[r] = model.Reverse(inputs, outputs); } else if (component is Workflow workflow) { var opts = new NewtonOptions() { MaxIterations = 20, DerivativeStep = new double[] { 0.01 } }; var solver = new NewtonSolver(opts); processedComponents[r] = new WorkflowGlobal($"{name}#GlobalWorkflow#{workflow.Name}", "", inputs, outputs, workflow.Components, workflow.Components, new List <ISolver>() { solver }); } } else { processedComponents[r] = components[r]; } } return(processedComponents); }
public void Circlify(List <TreemapData> data) { //IL faut commencer par les 2 plus gros cercles, le milieu du cercle final étant le centre du segment reliant les extrémités de ces cercles. double a1 = GetInnerCircleArea() * data[0].Size / Size; double a2 = GetInnerCircleArea() * data[1].Size / Size; double r1 = Math.Sqrt(a1 / Math.PI); double r2 = Math.Sqrt(a2 / Math.PI); var items = new Dictionary <TreemapItem, List <TreemapItem> >(); Geometric.Point center = GetCenter(); TreemapItem item1 = AddItem(data[0], center.AddX(-r2), r1); TreemapItem item2 = AddItem(data[1], center.AddX(r1), r2); items.Add(item1, new List <TreemapItem> { item2 }); items.Add(item2, new List <TreemapItem> { item1 }); for (int d = 2; d < data.Count; d++) { double a = GetInnerCircleArea() * data[d].Size / Size; double r = Math.Sqrt(a / Math.PI); var results = new List <Tuple <TreemapItem, TreemapItem, Geometric.Point, double> >(); foreach (var kvp in items) { TreemapItem i1 = kvp.Key; foreach (TreemapItem i2 in kvp.Value) { Triangle t = Triangle.FromTwoPointsAndTwoLengths(i1.GetCenter(), i2.GetCenter(), i1.GetRadius() + r, i2.GetRadius() + r); double distance = center.Distance(t.Point3); if (Items.All(item => item.GetCenter().Distance(t.Point3) - (item.GetRadius() + r) >= -0.0001)) { results.Add(Tuple.Create(i1, i2, t.Point3, distance)); } } } var result = results.OrderBy(t => t.Item4).First(); TreemapItem newItem = AddItem(data[d], result.Item3, r); items[result.Item1].Add(newItem); items[result.Item2].Add(newItem); items.Add(newItem, new List <TreemapItem> { result.Item1, result.Item2 }); } //Identify the item farther from the center and expand the chart so that this item becomes adjacent to external circle var max = Items .Select(i => new { Item = i, Distance = i.GetCenter().Distance(center) + i.GetRadius() }) .OrderByDescending(o => o.Distance) .First(); Homothety homothety = new Homothety(center, GetRadius() / max.Distance); foreach (var item in Items) { item.Rectangle = homothety.Transform(item.Rectangle); item.InnerRectangle = homothety.Transform(item.InnerRectangle); item.Empty = homothety.Transform(item.Empty); } //Homothety from contact point Vector vector = new Segment(center, max.Item.GetCenter()).ToVector(); Geometric.Point contact = max.Item.GetCenter() .AddX(max.Item.GetRadius() / vector.Length * vector.X) .AddY(max.Item.GetRadius() / vector.Length * vector.Y); NewtonSolver solver = new NewtonSolver((c) => { Homothety h = new Homothety(contact, c); double min = Items .Except(new List <TreemapItem> { max.Item }) .Select(i => h.Transform(i.InnerRectangle)) .Select(r => new { Center = new Geometric.Point(r.Left + r.Width / 2, r.Top + r.Height / 2), Radius = r.Width / 2 }) .Min(r => GetRadius() - (r.Center.Distance(center) + r.Radius)); return(min); }) .Solve(1); homothety = new Homothety(contact, solver.Solution); foreach (var item in Items) { item.Rectangle = homothety.Transform(item.Rectangle); item.InnerRectangle = homothety.Transform(item.InnerRectangle); item.Empty = homothety.Transform(item.Empty); } }
public ViscoElasticKnot(Knot _knot, GetBetaFunction getElasticBeta) : base(_knot.core_node, _knot.current_time) { int L = nodes.GetLength(0); lumen_sq_old = new double[L]; chrt_func = new MDFunction[L]; energy_conservation_func = new MDFunction[L - 1]; funcs = new MDFunction[2 * L]; wall_thickhess = new double[L]; lumen_sq_0 = new double[L]; beta_1 = new double[L]; chrt_b = new double[L]; chrt_f = new double[L]; c_dst = new double[L]; dep_matrix = new bool[2 * L, 2 * L]; prev_velocity = new double[L]; #if FAST nl_system = new NewtonSolver(2 * L); #endif for (int i = 0; i < L; i++) { for (int j = 0; j < L; j++) { dep_matrix[i, j] = false; } } for (int i = 0; i < L; i++) { double R0 = Math.Sqrt(nodes[i].lumen_sq_0 / Math.PI); beta_1[i] = getElasticBeta(R0, nodes[i].elasticity) / nodes[i].lumen_sq_0; wall_thickhess[i] = GlobalDefs.getBoileauWallThickness(R0, nodes[i].elasticity); lumen_sq_0[i] = nodes[i].lumen_sq_0; prev_velocity[i] = nodes[i].velocity; chrt_b[i] = 0; chrt_f[i] = 0; c_dst[i] = Math.Pow(nodes[i].lumen_sq_0, 0.25f) * Math.Sqrt(beta_1[i] / 2.0f / GlobalDefs.BLOOD_DENSITY); lumen_sq_old[i] = lumen_sq_0[i]; } for (int i = 0; i < L; i++) { pressure[i] = GlobalDefs.DIASTOLIC_PRESSURE; } int count = 0; unsafe { for (int i = 0; i < L; i++) { int I = i; #if FAST MDFunction_del f1_del = delegate(double *args) { double v = args[0 + I * 2]; double l = args[1 + I * 2]; if (v > 0) { return(Math.Abs(v) + 4 * (Math.Sqrt(Math.Sqrt(l)) * Math.Sqrt(beta_1[I] / 2.0f / GlobalDefs.BLOOD_DENSITY) - c_dst[I]) - chrt_f[I]); } else { return(Math.Abs(v) - 4 * (Math.Sqrt(Math.Sqrt(l)) * Math.Sqrt(beta_1[I] / 2.0f / GlobalDefs.BLOOD_DENSITY) - c_dst[I]) - chrt_b[I]); } }; baseMDFunction f1 = new delegateMDFunc(f1_del); nl_system.addFunc(f1); nl_system.setDetMatrixEl(count, 2 * I, true); nl_system.setDetMatrixEl(count, 2 * I + 1, true); #endif chrt_func[i] = delegate(double[] args) //v1,l1; v2,l2 ... { double v = args[0 + I * 2]; double l = args[1 + I * 2]; if (v > 0) { return(Math.Abs(v) + 4 * (Math.Pow(l, 0.25f) * Math.Sqrt(beta_1[I] / 2.0f / GlobalDefs.BLOOD_DENSITY) - c_dst[I]) - chrt_f[I]); } else { return(Math.Abs(v) - 4 * (Math.Pow(l, 0.25f) * Math.Sqrt(beta_1[I] / 2.0f / GlobalDefs.BLOOD_DENSITY) - c_dst[I]) - chrt_b[I]); } }; funcs[count] = chrt_func[i]; dep_matrix[count, 2 * I] = true; dep_matrix[count, 2 * I + 1] = true; count++; } } unsafe { #if FAST MDFunction_del f1_del = delegate(double *args) { double summ_flux = 0; for (int i = 0; i < L; i++) { summ_flux += args[0 + i * 2] * args[1 + i * 2]; } return(summ_flux); }; baseMDFunction f1 = new delegateMDFunc(f1_del); nl_system.addFunc(f1); for (int i = 0; i < 2 * L; i++) { nl_system.setDetMatrixEl(count, i, true); } #endif mass_conservation_func = delegate(double[] args) { double summ_flux = 0; for (int i = 0; i < L; i++) { double v = args[0 + i * 2]; double l = args[1 + i * 2]; summ_flux += v * l; } return(summ_flux); }; funcs[count] = mass_conservation_func; for (int i = 0; i < 2 * L; i++) { dep_matrix[count, i] = true; } }; count++; unsafe { for (int i = 1; i < L; i++) { int I = i; #if FAST MDFunction_del f1_del = delegate(double *args) { double v0 = args[0]; double p0 = calcPressureV(0, args[1]); double v = args[0 + I * 2]; double p = calcPressureV(I, args[1 + 2 * I]); return(GlobalDefs.BLOOD_DENSITY * (v0 * v0 - v * v) / 2 + p0 - p); }; baseMDFunction f1 = new delegateMDFunc(f1_del); nl_system.addFunc(f1); nl_system.setDetMatrixEl(count, 0, true); nl_system.setDetMatrixEl(count, 1, true); nl_system.setDetMatrixEl(count, 2 * I, true); nl_system.setDetMatrixEl(count, 2 * I + 1, true); #endif energy_conservation_func[i - 1] = delegate(double[] args) { double v0 = args[0]; double p0 = calcPressureV(0, args[1]); double v = args[0 + I * 2]; double p = calcPressureV(I, args[1 + 2 * I]); return(GlobalDefs.BLOOD_DENSITY * (v0 * v0 - v * v) / 2 + p0 - p); }; funcs[count] = energy_conservation_func[I - 1]; dep_matrix[count, 0] = true; dep_matrix[count, 1] = true; dep_matrix[count, 2 * I] = true; dep_matrix[count, 2 * I + 1] = true; count++; } #if FAST us_init_X = (double *)Marshal.AllocHGlobal(2 * L * sizeof(double)); us_solution = (double *)Marshal.AllocHGlobal(2 * L * sizeof(double)); for (int i = 0; i < 2 * L; i += 2) { us_init_X[i] = nodes[i / 2].velocity * v_sign[i / 2]; us_init_X[i + 1] = nodes[i / 2].lumen_sq; nl_system.setDxVectorEl(i, 1e-12f); nl_system.setDxVectorEl(i + 1, 1e-12f); } #endif } dX = new double[2 * L]; for (int i = 0; i < 2 * L; i += 2) { dX[i] = 1e-12f; dX[i + 1] = 1e-12f; } }