private Differentiable getDevice(byte[] id) { if (id != null) { for (int i = 0; i < devices.Count; i++) { Differentiable device = devices[i]; byte[] ID = device.getId(); if (id.Length == ID.Length) { bool isOk = true; for (int index = 0; index < ID.Length; index++) { if (id[index] != ID[index]) { isOk = false; } } if (isOk) { if (this.device is CardDevice && device is CardDevice) { return(device); } else if (this.device is WarningDevice && device is WarningDevice) { return(device); } } } } } return(null); }
/// <summary> /// Implements the differentiation chain rule, by differentiating the argument and multiplying it to the differentiation of the main node. /// </summary> /// <param name="VariableName">Name of variable to differentiate on.</param> /// <param name="Variables">Collection of variables.</param> /// <param name="Argument">Inner argument</param> /// <param name="Differentiation">Differentiation of main node.</param> /// <returns><paramref name="Differentiation"/>*D(<paramref name="Argument"/>)</returns> protected ScriptNode DifferentiationChainRule(string VariableName, Variables Variables, ScriptNode Argument, ScriptNode Differentiation) { if (Argument is IDifferentiable Differentiable) { ScriptNode ChainFactor = Differentiable.Differentiate(VariableName, Variables); if (ChainFactor is ConstantElement ConstantElement && ConstantElement.Constant is DoubleNumber DoubleNumber) { if (DoubleNumber.Value == 0) { return(ConstantElement); } else if (DoubleNumber.Value == 1) { return(Differentiation); } } int Start = this.Start; int Len = this.Length; Expression Exp = this.Expression; if (Differentiation is Invert Invert) { if (Invert.Operand is Negate Negate) { return(new Negate(new Divide(ChainFactor, Negate.Operand, Start, Len, Expression), Start, Len, Expression)); } else { return(new Divide(ChainFactor, Invert.Operand, Start, Len, Expression)); } } else if (Differentiation is Negate Negate) { if (Negate.Operand is Invert Invert2) { return(new Negate(new Divide(ChainFactor, Invert2.Operand, Start, Len, Expression), Start, Len, Expression)); } else { return(new Negate(new Multiply(Negate.Operand, ChainFactor, Start, Len, Expression), Start, Len, Expression)); } } else { return(new Multiply(Differentiation, ChainFactor, Start, Len, Expression)); } } else { throw new ScriptRuntimeException("Argument not differentiable.", this); } }
/// <summary> /// Differentiates a script node, if possible. /// </summary> /// <param name="VariableName">Name of variable to differentiate on.</param> /// <param name="Variables">Collection of variables.</param> /// <returns>Differentiated node.</returns> public ScriptNode Differentiate(string VariableName, Variables Variables) { if (this.op is IDifferentiable Differentiable) { return(new Permil( Differentiable.Differentiate(VariableName, Variables), this.Start, this.Length, this.Expression)); } else { throw new ScriptRuntimeException("Operand not differentiable.", this); } }
private void showDevice(Differentiable device) { splitContainer1.Panel2.Controls.Clear(); setTextBoxValue(); if (device is CardDevice) { label3.Location = new Point(52, 120); label3.Text = "Range Time:"; label4.Location = new Point(52, 180); label4.Text = "Still Time:"; label5.Location = new Point(58, 240); label5.Text = "Acc Range:"; label7.Text = "Type:Card Device"; label9.Text = "(range:1~255,unit:s)"; label10.Text = "(range:0~65535)"; label11.Text = "(range:10~86)"; splitContainer1.Panel2.Controls.Add(label11); splitContainer1.Panel2.Controls.Add(textBox4); } else if (device is WarningDevice) { label3.Location = new Point(52, 120); label3.Text = "Link Time:"; label4.Location = new Point(34, 180); label4.Text = "Safe Distance:"; label5.Location = new Point(70, 240); label5.Text = "Volume:"; label6.Location = new Point(82, 300); label6.Text = "Card:"; label7.Text = "Type:Warning Device"; label9.Text = "(range:0~65535,unit:s)"; label10.Text = "(range:0~65535,unit:cm)"; label11.Text = "(range:0~10)"; splitContainer1.Panel2.Controls.Add(label6); splitContainer1.Panel2.Controls.Add(button15); splitContainer1.Panel2.Controls.Add(button14); splitContainer1.Panel2.Controls.Add(button13); //splitContainer1.Panel2.Controls.Add(button12);//setCard splitContainer1.Panel2.Controls.Add(listView3); splitContainer1.Panel2.Controls.Add(textBox5); splitContainer1.Panel2.Controls.Add(trackBar1); label13.Location = new Point(50, 30); trackBar1.Controls.Add(label13); //splitContainer1.Panel2.Controls.Add(label13); } addView(); }
private List <Differentiable> getDifferentiablesCom00() { if (buf.Length == 50 * 7 + 5 && checkBuf()) { List <Differentiable> devices = new List <Differentiable>(); byte devicesNum = buf[2]; for (int i = 0; i < devicesNum; i++) { Differentiable device = null; byte[] id = new byte[3]; for (int j = 0; j < 7; j++) { if (j < 3) { id[j] = buf[3 + i * 7 + j]; } if (j == 6) { switch (buf[3 + i * 7 + j]) { case 0x00: device = new CardDevice(); break; case 0x01: device = new WarningDevice(); break; case 0x02: break; case 0x03: break; default: break; } device.setId(id); } } devices.Add(device); } return(devices); } else if (buf.Length > 50 * 7 + 5) { buf = new byte[0]; } return(null); }
/// <summary> /// Differentiates a lambda expression, if possible. /// </summary> /// <param name="VariableName">Name of variable to differentiate on.</param> /// <param name="Variables">Collection of variables.</param> /// <returns>Differentiated lambda expression.</returns> public ScriptNode Differentiate(string VariableName, Variables Variables) { if (Array.IndexOf <string>(this.argumentNames, VariableName) < 0) { return(new ConstantElement(Objects.DoubleNumber.ZeroElement, this.Start, this.Length, this.Expression)); } else if (this.op is IDifferentiable Differentiable) { return(new LambdaDefinition(this.argumentNames, this.argumentTypes, Differentiable.Differentiate(VariableName, Variables), this.Start, this.Length, this.Expression)); } else { throw new ScriptRuntimeException("Lambda expression not differentiable.", this); } }
private void listClick(ListView listView) { foreach (ListViewItem item in listView.SelectedItems) { string itemText = item.Text; byte[] id = new byte[itemText.Length / 2]; for (int i = 0; i < id.Length; i++) { id[i] = Convert.ToByte(itemText.Substring(i * 2, 2), 16); } Differentiable device = getDevice(id); this.device = device; setDeviceCom(); showDevice(this.device); searchDeviceAllInfo(); } }
private void TabControl1_SelectedIndexChanged(object sender, EventArgs e) { listView1.Items.Clear(); listView2.Items.Clear(); if (serialPort.IsOpen) { clickEventNum = CLICKEVENTNUM.SEARCH; deviceCom.sendSearchDeviceNumCom(); } if ("Tag".Equals(tabControl1.SelectedTab.Text)) { device = new CardDevice(); showDevice(device); } else if ("Warn".Equals(tabControl1.SelectedTab.Text)) { device = new WarningDevice(); showDevice(device); } setDeviceCom(); }
public static FractalGraph CalcNewtonSmooth(double rCenter, double iCenter, double rDelta, Complex R, ILambdaExpression f, ScriptNode fDef, Variables Variables, SKColor[] Palette, int Width, int Height, ScriptNode Node, FractalZoomScript FractalZoomScript, object State) { double RRe = R.Real; double RIm = R.Imaginary; double r0, i0, r1, i1; double dr, di; double r, i; double aspect; int x, y; int n, N; N = Palette.Length; if (Width <= 2 || Height <= 2) { throw new ScriptRuntimeException("Width and Height has to be greater than 2.", Node); } Variables v = new Variables(); Variables.CopyTo(v); if (!(f is IDifferentiable Differentiable) || !(Differentiable.Differentiate(Differentiable.DefaultVariableName, v) is ILambdaExpression fPrim)) { throw new ScriptRuntimeException("Lambda expression not differentiable.", Node); } int Size = Width * Height; double Conv = 1e-10; double Div = 1e10; double[] ColorIndex = new double[Size]; int Index = 0; Complex[] Row; Complex[] Row2; Complex[] Row3; int[] Offset; IElement[] P = new IElement[1]; int j, c, x2; IElement Obj, Obj2; double Mod; Complex z; rDelta *= 0.5; r0 = rCenter - rDelta; r1 = rCenter + rDelta; aspect = ((double)Width) / Height; i0 = iCenter - rDelta / aspect; i1 = iCenter + rDelta / aspect; dr = (r1 - r0) / Width; di = (i1 - i0) / Height; for (y = 0, i = i0; y < Height; y++, i += di) { Row = new Complex[Width]; Offset = new int[Width]; c = Width; for (x = 0, x2 = y * Width, r = r0; x < Width; x++, r += dr, x2++) { Row[x] = new Complex(r, i); Offset[x] = x2; } n = 0; while (n < N && c > 0) { n++; P[0] = Expression.Encapsulate(Row); Obj = f.Evaluate(P, v); Obj2 = fPrim.Evaluate(P, v); Row2 = Obj.AssociatedObjectValue as Complex[]; Row3 = Obj2.AssociatedObjectValue as Complex[]; if (Row2 == null || Row3 == null) { throw new ScriptRuntimeException("Lambda expression (and its first derivative) must be able to accept complex vectors, " + "and return complex vectors of equal length. Type returned: " + Obj.GetType().FullName + " and " + Obj2.GetType().FullName, Node); } else if (Row2.Length != c || Row3.Length != c) { throw new ScriptRuntimeException("Lambda expression (and its first derivative) must be able to accept complex vectors, " + "and return complex vectors of equal length. Length returned: " + Row2.Length.ToString() + " and " + Row3.Length.ToString() + ". Expected: " + c.ToString(), Node); } for (x = x2 = 0; x < c; x++) { j = Offset[x]; z = R * Row2[x] / Row3[x]; Row[x] -= z; Mod = z.Magnitude; if (Mod > Conv && Mod < Div) { if (x != x2) { Offset[x2] = j; } x2++; } else { if (n >= N) { ColorIndex[Index++] = N; } else { ColorIndex[j++] = n; } } } if (x2 < x) { Array.Resize <Complex>(ref Row, x2); Array.Resize <int>(ref Offset, x2); c = x2; } } } Node.Expression.Preview(new GraphBitmap(FractalGraph.ToBitmap(ColorIndex, Width, Height, Palette))); double[] Boundary = FractalGraph.FindBoundaries(ColorIndex, Width, Height); FractalGraph.Smooth(ColorIndex, Boundary, Width, Height, N, Palette, Node, Variables); return(new FractalGraph(FractalGraph.ToBitmap(ColorIndex, Width, Height, Palette), r0, i0, r1, i1, rDelta * 2, true, Node, FractalZoomScript, State)); }
public static FractalGraph CalcNewton(double rCenter, double iCenter, double rDelta, Complex R, ILambdaExpression f, ScriptNode fDef, Variables Variables, SKColor[] Palette, int Width, int Height, ScriptNode Node, FractalZoomScript FractalZoomScript, object State) { byte[] reds; byte[] greens; byte[] blues; double RRe = R.Real; double RIm = R.Imaginary; double r0, i0, r1, i1; double dr, di; double r, i; double aspect; int x, y; int n, N; SKColor cl; Complex z; N = Palette.Length; reds = new byte[N]; greens = new byte[N]; blues = new byte[N]; for (x = 0; x < N; x++) { cl = Palette[x]; reds[x] = cl.Red; greens[x] = cl.Green; blues[x] = cl.Blue; } Variables v = new Variables(); Variables.CopyTo(v); if (!(f is IDifferentiable Differentiable) || !(Differentiable.Differentiate(Differentiable.DefaultVariableName, v) is ILambdaExpression fPrim)) { throw new ScriptRuntimeException("Lambda expression not differentiable.", Node); } int size = Width * Height * 4; double Conv = 1e-10; double Div = 1e10; byte[] rgb = new byte[size]; Complex[] Row; Complex[] Row2; Complex[] Row3; int[] Offset; IElement[] P = new IElement[1]; int j, c, x2; IElement Obj, Obj2; double Mod; rDelta *= 0.5; r0 = rCenter - rDelta; r1 = rCenter + rDelta; aspect = ((double)Width) / Height; i0 = iCenter - rDelta / aspect; i1 = iCenter + rDelta / aspect; dr = (r1 - r0) / Width; di = (i1 - i0) / Height; for (y = 0, i = i0; y < Height; y++, i += di) { Row = new Complex[Width]; Offset = new int[Width]; c = Width; for (x = 0, x2 = y * Width * 4, r = r0; x < Width; x++, r += dr, x2 += 4) { Row[x] = new Complex(r, i); Offset[x] = x2; } n = 0; while (n < N && c > 0) { n++; P[0] = Expression.Encapsulate(Row); Obj = f.Evaluate(P, v); Obj2 = fPrim.Evaluate(P, v); Row2 = Obj.AssociatedObjectValue as Complex[]; Row3 = Obj2.AssociatedObjectValue as Complex[]; if (Row2 == null || Row3 == null) { throw new ScriptRuntimeException("Lambda expression (and its first derivative) must be able to accept complex vectors, " + "and return complex vectors of equal length. Type returned: " + Obj.GetType().FullName + " and " + Obj2.GetType().FullName, Node); } else if (Row2.Length != c || Row3.Length != c) { throw new ScriptRuntimeException("Lambda expression (and its first derivative) must be able to accept complex vectors, " + "and return complex vectors of equal length. Length returned: " + Row2.Length.ToString() + " and " + Row3.Length.ToString() + ". Expected: " + c.ToString(), Node); } for (x = x2 = 0; x < c; x++) { j = Offset[x]; z = R * Row2[x] / Row3[x]; Row[x] -= z; Mod = z.Magnitude; if (Mod > Conv && Mod < Div) { if (x != x2) { Offset[x2] = j; } x2++; } else { if (n >= N) { rgb[j++] = 0; rgb[j++] = 0; rgb[j++] = 0; } else { rgb[j++] = blues[n]; rgb[j++] = greens[n]; rgb[j++] = reds[n]; } rgb[j++] = 255; } } if (x2 < x) { Array.Resize <Complex>(ref Row, x2); Array.Resize <int>(ref Offset, x2); c = x2; } } } using (SKData Data = SKData.Create(new MemoryStream(rgb))) { SKImage Bitmap = SKImage.FromPixelData(new SKImageInfo(Width, Height, SKColorType.Bgra8888), Data, Width * 4); return(new FractalGraph(Bitmap, r0, i0, r1, i1, rDelta * 2, true, Node, FractalZoomScript, State)); } }
public void setDevice(Differentiable device) { this.device = device; }
public DeviceCom(SerialPort serialPort, Differentiable device) { this.serialPort = serialPort; this.device = device; }
public static FractalGraph CalcNewton(double rCenter, double iCenter, double rDelta, Complex R, ILambdaExpression f, ScriptNode fDef, Variables Variables, int N, int Width, int Height, ScriptNode Node, FractalZoomScript FractalZoomScript, object State) { double RRe = R.Real; double RIm = R.Imaginary; List <double> AttractorsR = new List <double>(); List <double> AttractorsI = new List <double>(); List <int> AttractorColors = new List <int>(); double[] AttractorsR2 = new double[0]; double[] AttractorsI2 = new double[0]; int[] AttractorsColors2 = new int[0]; double r0, i0, r1, i1; double dr, di; double r, i; double zr, zi; double aspect; int x, y, b, c, d; int NrAttractors = 0; int n; int index; Variables v = new Variables(); Variables.CopyTo(v); if (!(f is IDifferentiable Differentiable) || !(Differentiable.Differentiate(Differentiable.DefaultVariableName, v) is ILambdaExpression fPrim)) { throw new ScriptRuntimeException("Lambda expression not differentiable.", Node); } int size = Width * Height * 4; double Conv = 1e-10; double Div = 1e10; double Conv2 = Conv * 2; byte[] rgb = new byte[size]; Complex[] Row; Complex[] Row2; Complex[] Row3; int[] Offset; IElement[] P = new IElement[1]; int j, x2; IElement Obj, Obj2; double Mod; Complex z; rDelta *= 0.5; r0 = rCenter - rDelta; r1 = rCenter + rDelta; aspect = ((double)Width) / Height; i0 = iCenter - rDelta / aspect; i1 = iCenter + rDelta / aspect; dr = (r1 - r0) / Width; di = (i1 - i0) / Height; for (y = 0, i = i0, index = 0; y < Height; y++, i += di) { Row = new Complex[Width]; Offset = new int[Width]; c = Width; for (x = 0, x2 = y * Width * 4, r = r0; x < Width; x++, r += dr, x2 += 4) { Row[x] = new Complex(r, i); Offset[x] = x2; } n = 0; while (n < N && c > 0) { n++; P[0] = Expression.Encapsulate(Row); Obj = f.Evaluate(P, v); Obj2 = fPrim.Evaluate(P, v); Row2 = Obj.AssociatedObjectValue as Complex[]; Row3 = Obj2.AssociatedObjectValue as Complex[]; if (Row2 == null || Row3 == null) { throw new ScriptRuntimeException("Lambda expression (and its first derivative) must be able to accept complex vectors, " + "and return complex vectors of equal length. Type returned: " + Obj.GetType().FullName + " and " + Obj2.GetType().FullName, Node); } else if (Row2.Length != c || Row3.Length != c) { throw new ScriptRuntimeException("Lambda expression (and its first derivative) must be able to accept complex vectors, " + "and return complex vectors of equal length. Length returned: " + Row2.Length.ToString() + " and " + Row3.Length.ToString() + ". Expected: " + c.ToString(), Node); } for (x = x2 = 0; x < c; x++) { j = Offset[x]; z = R * Row2[x] / Row3[x]; Row[x] -= z; Mod = z.Magnitude; if (Mod > Conv && Mod < Div) { if (x != x2) { Offset[x2] = j; } x2++; } else { if (n >= N) { rgb[j++] = 0; rgb[j++] = 0; rgb[j++] = 0; } else { zr = z.Real; zi = z.Imaginary; for (b = 0; b < NrAttractors; b++) { if (Math.Abs(AttractorsR2[b] - zr) < Conv2 && Math.Abs(AttractorsI2[b] - zi) < Conv2) { break; } } if (b == NrAttractors) { AttractorsR.Add(zr); AttractorsI.Add(zi); int p1 = ~((b % 6) + 1); int p2 = ((b / 6) % 7); int Red = (p1 & 1) != 0 ? 255 : 0; int Green = (p1 & 2) != 0 ? 255 : 0; int Blue = (p1 & 4) != 0 ? 255 : 0; if ((p2 & 1) != 0) { Red >>= 1; } if ((p2 & 2) != 0) { Green >>= 1; } if ((p2 & 4) != 0) { Blue >>= 1; } Blue <<= 8; Blue |= Green; Blue <<= 8; Blue |= Red; AttractorColors.Add(Blue); AttractorsR2 = AttractorsR.ToArray(); AttractorsI2 = AttractorsI.ToArray(); AttractorsColors2 = AttractorColors.ToArray(); NrAttractors++; } b = AttractorColors[b]; d = (byte)b; rgb[index++] = (byte)((d * (N - n + 1)) / N); b >>= 8; d = (byte)b; rgb[index++] = (byte)((d * (N - n + 1)) / N); b >>= 8; d = (byte)b; rgb[index++] = (byte)((d * (N - n + 1)) / N); } rgb[j++] = 255; } } if (x2 < x) { Array.Resize <Complex>(ref Row, x2); Array.Resize <int>(ref Offset, x2); c = x2; } } } using (SKData Data = SKData.Create(new MemoryStream(rgb))) { SKImage Bitmap = SKImage.FromPixelData(new SKImageInfo(Width, Height, SKColorType.Bgra8888), Data, Width * 4); return(new FractalGraph(Bitmap, r0, i0, r1, i1, rDelta * 2, true, Node, FractalZoomScript, State)); } }
public static FractalGraph CalcHalley(double rCenter, double iCenter, double rDelta, Complex R, ILambdaExpression f, ScriptNode fDef, Variables Variables, SKColor[] Palette, int Width, int Height, ScriptNode Node, FractalZoomScript FractalZoomScript, object State) { byte[] reds; byte[] greens; byte[] blues; double RRe = R.Real; double RIm = R.Imaginary; double r0, i0, r1, i1; double dr, di; double r, i; double aspect; int x, y; int n, N; SKColor cl; N = Palette.Length; reds = new byte[N]; greens = new byte[N]; blues = new byte[N]; for (x = 0; x < N; x++) { cl = Palette[x]; reds[x] = cl.Red; greens[x] = cl.Green; blues[x] = cl.Blue; } Variables v = new Variables(); Variables.CopyTo(v); string ParameterName; if (!(f is IDifferentiable Differentiable) || !(Differentiable.Differentiate(ParameterName = Differentiable.DefaultVariableName, v) is ILambdaExpression fPrim)) { throw new ScriptRuntimeException("Lambda expression not differentiable.", Node); } if (!(fPrim is IDifferentiable Differentiable2) || !(Differentiable2.Differentiate(ParameterName, v) is ILambdaExpression fBis)) { throw new ScriptRuntimeException("Lambda expression not twice differentiable.", Node); } int size = Width * Height * 4; double Conv = 1e-10; double Div = 1e10; byte[] rgb = new byte[size]; Complex[] Row; Complex[] Row2; Complex[] Row3; Complex[] Row4; int[] Offset; IElement[] P = new IElement[1]; int j, c, x2; IElement Obj, Obj2, Obj3; double Mod; Complex z, z2, z3; Complex R2 = R * 2; rDelta *= 0.5; r0 = rCenter - rDelta; r1 = rCenter + rDelta; aspect = ((double)Width) / Height; i0 = iCenter - rDelta / aspect; i1 = iCenter + rDelta / aspect; dr = (r1 - r0) / Width; di = (i1 - i0) / Height; for (y = 0, i = i0; y < Height; y++, i += di) { Row = new Complex[Width]; Offset = new int[Width]; c = Width; for (x = 0, x2 = y * Width * 4, r = r0; x < Width; x++, r += dr, x2 += 4) { Row[x] = new Complex(r, i); Offset[x] = x2; } n = 0; while (n < N && c > 0) { n++; P[0] = Expression.Encapsulate(Row); Obj = f.Evaluate(P, v); Obj2 = fPrim.Evaluate(P, v); Obj3 = fBis.Evaluate(P, v); Row2 = Obj.AssociatedObjectValue as Complex[]; Row3 = Obj2.AssociatedObjectValue as Complex[]; Row4 = Obj3.AssociatedObjectValue as Complex[]; if (Row2 is null || Row3 is null || Row4 is null) { throw new ScriptRuntimeException("Lambda expression (and its first and second derivative) must be able to accept complex vectors, " + "and return complex vectors of equal length. Type returned: " + Obj.GetType().FullName + ", " + Obj2.GetType().FullName + " and " + Obj3.GetType().FullName, Node); }
public static FractalGraph CalcNewtonSmooth(double rCenter, double iCenter, double rDelta, Complex R, ILambdaExpression f, ScriptNode fDef, Variables Variables, SKColor[] Palette, int Width, int Height, ScriptNode Node, FractalZoomScript FractalZoomScript, object State) { double RRe = R.Real; double RIm = R.Imaginary; double r0, i0, r1, i1; double dr, di; double r, i; double aspect; int x, y; int n, N; N = Palette.Length; if (Width <= 2 || Height <= 2) { throw new ScriptRuntimeException("Width and Height has to be greater than 2.", Node); } Variables v = new Variables(); Variables.CopyTo(v); if (!(f is IDifferentiable Differentiable) || !(Differentiable.Differentiate(Differentiable.DefaultVariableName, v) is ILambdaExpression fPrim)) { throw new ScriptRuntimeException("Lambda expression not differentiable.", Node); } int Size = Width * Height; double Conv = 1e-10; double Div = 1e10; double[] ColorIndex = new double[Size]; int Index = 0; Complex[] Row; Complex[] Row2; Complex[] Row3; int[] Offset; IElement[] P = new IElement[1]; int j, c, x2; IElement Obj, Obj2; double Mod; Complex z; rDelta *= 0.5; r0 = rCenter - rDelta; r1 = rCenter + rDelta; aspect = ((double)Width) / Height; i0 = iCenter - rDelta / aspect; i1 = iCenter + rDelta / aspect; dr = (r1 - r0) / Width; di = (i1 - i0) / Height; for (y = 0, i = i0; y < Height; y++, i += di) { Row = new Complex[Width]; Offset = new int[Width]; c = Width; for (x = 0, x2 = y * Width, r = r0; x < Width; x++, r += dr, x2++) { Row[x] = new Complex(r, i); Offset[x] = x2; } n = 0; while (n < N && c > 0) { n++; P[0] = Expression.Encapsulate(Row); Obj = f.Evaluate(P, v); Obj2 = fPrim.Evaluate(P, v); Row2 = Obj.AssociatedObjectValue as Complex[]; Row3 = Obj2.AssociatedObjectValue as Complex[]; if (Row2 is null || Row3 is null) { throw new ScriptRuntimeException("Lambda expression (and its first derivative) must be able to accept complex vectors, " + "and return complex vectors of equal length. Type returned: " + Obj.GetType().FullName + " and " + Obj2.GetType().FullName, Node); }
public static FractalGraph CalcNewton(double rCenter, double iCenter, double rDelta, Complex R, ILambdaExpression f, ScriptNode fDef, Variables Variables, int N, int Width, int Height, ScriptNode Node, FractalZoomScript FractalZoomScript, object State) { double RRe = R.Real; double RIm = R.Imaginary; List <double> AttractorsR = new List <double>(); List <double> AttractorsI = new List <double>(); List <int> AttractorColors = new List <int>(); double[] AttractorsR2 = new double[0]; double[] AttractorsI2 = new double[0]; int[] AttractorsColors2 = new int[0]; double r0, i0, r1, i1; double dr, di; double r, i; double zr, zi; double aspect; int x, y, b, c, d; int NrAttractors = 0; int n; int index; Variables v = new Variables(); Variables.CopyTo(v); if (!(f is IDifferentiable Differentiable) || !(Differentiable.Differentiate(Differentiable.DefaultVariableName, v) is ILambdaExpression fPrim)) { throw new ScriptRuntimeException("Lambda expression not differentiable.", Node); } int size = Width * Height * 4; double Conv = 1e-10; double Div = 1e10; double Conv2 = Conv * 2; byte[] rgb = new byte[size]; Complex[] Row; Complex[] Row2; Complex[] Row3; int[] Offset; IElement[] P = new IElement[1]; int j, x2; IElement Obj, Obj2; double Mod; Complex z; rDelta *= 0.5; r0 = rCenter - rDelta; r1 = rCenter + rDelta; aspect = ((double)Width) / Height; i0 = iCenter - rDelta / aspect; i1 = iCenter + rDelta / aspect; dr = (r1 - r0) / Width; di = (i1 - i0) / Height; for (y = 0, i = i0, index = 0; y < Height; y++, i += di) { Row = new Complex[Width]; Offset = new int[Width]; c = Width; for (x = 0, x2 = y * Width * 4, r = r0; x < Width; x++, r += dr, x2 += 4) { Row[x] = new Complex(r, i); Offset[x] = x2; } n = 0; while (n < N && c > 0) { n++; P[0] = Expression.Encapsulate(Row); Obj = f.Evaluate(P, v); Obj2 = fPrim.Evaluate(P, v); Row2 = Obj.AssociatedObjectValue as Complex[]; Row3 = Obj2.AssociatedObjectValue as Complex[]; if (Row2 is null || Row3 is null) { throw new ScriptRuntimeException("Lambda expression (and its first derivative) must be able to accept complex vectors, " + "and return complex vectors of equal length. Type returned: " + Obj.GetType().FullName + " and " + Obj2.GetType().FullName, Node); }