Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
NewtonBuilderFractal.cs
1using System;
2using System.Numerics;
3using System.Text;
4using SkiaSharp;
10
12{
21 {
27 : base(new ScriptNode[] { z, dr, R, c, Palette, DimX, DimY },
28 new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Scalar, ArgumentType.Scalar,
29 ArgumentType.Normal, ArgumentType.Vector, ArgumentType.Scalar, ArgumentType.Scalar},
31 {
32 }
33
39 : base(new ScriptNode[] { z, dr, R, c, Palette, DimX },
40 new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Scalar, ArgumentType.Scalar,
41 ArgumentType.Normal, ArgumentType.Vector, ArgumentType.Scalar},
43 {
44 }
45
51 : base(new ScriptNode[] { z, dr, R, c, Palette },
52 new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Scalar, ArgumentType.Scalar,
53 ArgumentType.Normal, ArgumentType.Vector },
55 {
56 }
57
63 : base(new ScriptNode[] { z, dr, R, c },
64 new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Scalar, ArgumentType.Scalar,
65 ArgumentType.Normal },
67 {
68 }
69
75 : base(new ScriptNode[] { z, dr, R },
76 new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Scalar, ArgumentType.Scalar },
78 {
79 }
80
84 public override string[] DefaultArgumentNames
85 {
86 get
87 {
88 return new string[] { "z", "dr", "R", "c", "Palette", "DimX", "DimY" };
89 }
90 }
91
96 {
97 string ColorExpression = null;
98 SKColor[] Palette;
99 double[] Coefficients = null;
100 Complex[] CoefficientsZ = null;
101 double rc, ic;
102 double dr;
103 Complex R;
104 int dimx, dimy;
105 int c = Arguments.Length;
106 int i = 0;
107 object Obj;
108
109 Obj = Arguments[i++].AssociatedObjectValue;
110 if (Obj is Complex z)
111 {
112 rc = z.Real;
113 ic = z.Imaginary;
114 }
115 else
116 {
117 rc = Expression.ToDouble(Obj);
118 ic = Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
119 }
120
121 if (i >= c)
122 throw new ScriptRuntimeException("Insufficient parameters in call to NewtonBuilderFractal().", this);
123
124 dr = Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
125
126 if (i < c && ((Obj = Arguments[i].AssociatedObjectValue) is double || Obj is Complex))
127 {
128 R = Expression.ToComplex(Obj);
129 i++;
130 }
131 else
132 {
133 R = Complex.One;
134
135 if (i < c && this.Arguments[i] is null)
136 i++;
137 }
138
139 if (i < c && Arguments[i] is DoubleVector)
140 {
141 Coefficients = (double[])Arguments[i++].AssociatedObjectValue;
142
143 int j, d = Coefficients.Length;
144
145 CoefficientsZ = new Complex[d];
146 for (j = 0; j < d; j++)
147 CoefficientsZ[j] = new Complex(Coefficients[j], 0);
148 }
149 else if (i < c && Arguments[i] is ComplexVector)
150 CoefficientsZ = (Complex[])Arguments[i++].AssociatedObjectValue;
151 /*else if (i < c && Parameters[i] is RealPolynomial)
152 Coefficients = ((RealPolynomial)Arguments[i++].AssociatedObjectValue).Coefficients;
153 else if (i < c && Parameters[i] is ComplexPolynomial)
154 CoefficientsZ = ((ComplexPolynomial)Arguments[i++].AssociatedObjectValue).Coefficients;*/
155 else if (i < c && Arguments[i] is IVector)
156 {
157 IVector Vector = (IVector)Arguments[i++];
158 int j, d = Vector.Dimension;
159
160 CoefficientsZ = new Complex[d];
161 for (j = 0; j < d; j++)
162 CoefficientsZ[j] = Expression.ToComplex(Vector.GetElement(j).AssociatedObjectValue);
163 }
164 else
165 CoefficientsZ = new Complex[0];
166
167 if (i < c && !(this.Arguments[i] is null) && Arguments[i] is ObjectVector)
168 {
169 ColorExpression = this.Arguments[i].SubExpression;
171 }
172 else
173 {
174 Palette = ColorModels.RandomLinearAnalogousHSL.CreatePalette(128, 4, out int Seed, this, Variables);
175 ColorExpression = "RandomLinearAnalogousHSL(128,4," + Seed.ToString() + ")";
176
177 if (i < c && this.Arguments[i] is null)
178 i++;
179 }
180
181 if (i < c)
182 dimx = (int)Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
183 else
184 dimx = 320;
185
186 if (i < c)
187 dimy = (int)Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
188 else
189 dimy = 200;
190
191 if (i < c)
192 {
193 throw new ScriptRuntimeException("Parameter mismatch in call to NewtonBuilderFractal(r,c,dr,Coefficients[,Palette][,dimx[,dimy]]).",
194 this);
195 }
196
197 if (dimx <= 0 || dimx > 5000 || dimy <= 0 || dimy > 5000)
198 throw new ScriptRuntimeException("Image size must be within 1x1 to 5000x5000", this);
199
200 return NewtonFractal.CalcNewton(Variables, rc, ic, dr, R, CoefficientsZ, Palette, dimx, dimy, this, this.FractalZoomScript,
201 new object[] { Palette, dimx, dimy, R, CoefficientsZ, ColorExpression, rc, ic });
202 }
203
204 private string FractalZoomScript(double r, double i, double Size, object State)
205 {
206 object[] Parameters = (object[])State;
207 int DimX = (int)Parameters[1];
208 int DimY = (int)Parameters[2];
209 Complex R = (Complex)Parameters[3];
210 Complex[] CoefficientsZ = (Complex[])Parameters[4];
211 string ColorExpression = (string)Parameters[5];
212 double rc = (double)Parameters[6];
213 double ic = (double)Parameters[7];
214 Complex z0 = new Complex(r, i);
215 int j;
216
217 int c = CoefficientsZ.Length;
218 while (c > 1 && CoefficientsZ[c - 1].Equals(0))
219 c--;
220
221 Complex[] C2;
222
223 if (c < 1)
224 {
225 C2 = new Complex[2];
226 C2[0] = -z0;
227 C2[1] = Complex.One;
228 }
229 else
230 {
231 C2 = new Complex[c + 1];
232 C2[0] = Complex.Zero;
233 for (j = 0; j < c; j++)
234 C2[j + 1] = CoefficientsZ[j];
235
236 for (j = 0; j < c; j++)
237 C2[j] -= z0 * CoefficientsZ[j];
238 }
239
240 StringBuilder sb = new StringBuilder();
241
242 sb.Append("NewtonBuilderFractal((");
243 sb.Append(Expression.ToString(rc));
244 sb.Append(',');
245 sb.Append(Expression.ToString(ic));
246 sb.Append("),");
247 sb.Append(Expression.ToString(Size));
248 sb.Append(',');
249 sb.Append(Expression.ToString(R));
250 sb.Append(',');
251 sb.Append(Expression.ToString(C2));
252
253 if (!string.IsNullOrEmpty(ColorExpression))
254 {
255 sb.Append(',');
256 sb.Append(ColorExpression);
257 }
258
259 sb.Append(',');
260 sb.Append(DimX.ToString());
261 sb.Append(',');
262 sb.Append(DimY.ToString());
263 sb.Append(')');
264
265 return sb.ToString();
266 }
267
271 public override string FunctionName => nameof(NewtonBuilderFractal);
272 }
273}
Class managing a script expression.
Definition: Expression.cs:39
static Complex ToComplex(object Object)
Converts an object to a complex value.
Definition: Expression.cs:4942
static double ToDouble(object Object)
Converts an object to a double value.
Definition: Expression.cs:4824
static string ToString(double Value)
Converts a value to a string, that can be parsed as part of an expression.
Definition: Expression.cs:4496
Calculates a Newton Fractal Image, but when clicked, adds a root to the underying polynomial,...
NewtonBuilderFractal(ScriptNode z, ScriptNode dr, ScriptNode R, int Start, int Length, Expression Expression)
TODO
NewtonBuilderFractal(ScriptNode z, ScriptNode dr, ScriptNode R, ScriptNode c, ScriptNode Palette, int Start, int Length, Expression Expression)
TODO
override IElement Evaluate(IElement[] Arguments, Variables Variables)
TODO
NewtonBuilderFractal(ScriptNode z, ScriptNode dr, ScriptNode R, ScriptNode c, ScriptNode Palette, ScriptNode DimX, int Start, int Length, Expression Expression)
TODO
NewtonBuilderFractal(ScriptNode z, ScriptNode dr, ScriptNode R, ScriptNode c, ScriptNode Palette, ScriptNode DimX, ScriptNode DimY, int Start, int Length, Expression Expression)
TODO
NewtonBuilderFractal(ScriptNode z, ScriptNode dr, ScriptNode R, ScriptNode c, int Start, int Length, Expression Expression)
TODO
static FractalGraph CalcNewton(Variables Variables, double rCenter, double iCenter, double rDelta, Complex R, double[] Coefficients, SKColor[] Palette, int Width, int Height, ScriptNode Node, FractalZoomScript FractalZoomScript, object State)
TODO
Defines a clickable fractal graph in the complex plane.
Definition: FractalGraph.cs:22
static SKColor[] ToPalette(ObjectVector Vector)
TODO
Base class for multivariate funcions.
ScriptNode[] Arguments
Function arguments.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
Expression Expression
Expression of which the node is a part.
Definition: ScriptNode.cs:177
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:33
Basic interface for vectors.
Definition: IVector.cs:9
int Dimension
Dimension of vector.
Definition: IVector.cs:14
IElement GetElement(int Index)
Gets an element of the vector.
ArgumentType
Type of parameter used in a function definition or a lambda definition.
Definition: IFunction.cs:9