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;
11
13{
22 {
28 : base(new ScriptNode[] { z, dr, R, c, Palette, DimX, DimY },
29 new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Scalar, ArgumentType.Scalar,
30 ArgumentType.Normal, ArgumentType.Vector, ArgumentType.Scalar, ArgumentType.Scalar},
32 {
33 }
34
40 : base(new ScriptNode[] { z, dr, R, c, Palette, DimX },
41 new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Scalar, ArgumentType.Scalar,
42 ArgumentType.Normal, ArgumentType.Vector, ArgumentType.Scalar},
44 {
45 }
46
52 : base(new ScriptNode[] { z, dr, R, c, Palette },
53 new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Scalar, ArgumentType.Scalar,
54 ArgumentType.Normal, ArgumentType.Vector },
56 {
57 }
58
64 : base(new ScriptNode[] { z, dr, R, c },
65 new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Scalar, ArgumentType.Scalar,
66 ArgumentType.Normal },
68 {
69 }
70
76 : base(new ScriptNode[] { z, dr, R },
77 new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Scalar, ArgumentType.Scalar },
79 {
80 }
81
85 public override string[] DefaultArgumentNames
86 {
87 get
88 {
89 return new string[] { "z", "dr", "R", "c", "Palette", "DimX", "DimY" };
90 }
91 }
92
97 {
98 string ColorExpression = null;
99 SKColor[] Palette;
100 double[] Coefficients = null;
101 Complex[] CoefficientsZ = null;
102 double rc, ic;
103 double dr;
104 Complex R;
105 int dimx, dimy;
106 int c = Arguments.Length;
107 int i = 0;
108 object Obj;
109
110 Obj = Arguments[i++].AssociatedObjectValue;
111 if (Obj is Complex z)
112 {
113 rc = z.Real;
114 ic = z.Imaginary;
115 }
116 else
117 {
118 rc = Expression.ToDouble(Obj);
119 ic = Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
120 }
121
122 if (i >= c)
123 throw new ScriptRuntimeException("Insufficient parameters in call to NewtonBuilderFractal().", this);
124
125 dr = Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
126
127 if (i < c && ((Obj = Arguments[i].AssociatedObjectValue) is double || Obj is Complex))
128 {
129 R = Expression.ToComplex(Obj);
130 i++;
131 }
132 else
133 {
134 R = Complex.One;
135
136 if (i < c && this.Arguments[i] is null)
137 i++;
138 }
139
140 if (i < c && Arguments[i] is DoubleVector)
141 {
142 Coefficients = (double[])Arguments[i++].AssociatedObjectValue;
143
144 int j, d = Coefficients.Length;
145
146 CoefficientsZ = new Complex[d];
147 for (j = 0; j < d; j++)
148 CoefficientsZ[j] = new Complex(Coefficients[j], 0);
149 }
150 else if (i < c && Arguments[i] is ComplexVector)
151 CoefficientsZ = (Complex[])Arguments[i++].AssociatedObjectValue;
152 /*else if (i < c && Parameters[i] is RealPolynomial)
153 Coefficients = ((RealPolynomial)Arguments[i++].AssociatedObjectValue).Coefficients;
154 else if (i < c && Parameters[i] is ComplexPolynomial)
155 CoefficientsZ = ((ComplexPolynomial)Arguments[i++].AssociatedObjectValue).Coefficients;*/
156 else if (i < c && Arguments[i] is IVector)
157 {
158 IVector Vector = (IVector)Arguments[i++];
159 int j, d = Vector.Dimension;
160
161 CoefficientsZ = new Complex[d];
162 for (j = 0; j < d; j++)
163 CoefficientsZ[j] = Expression.ToComplex(Vector.GetElement(j).AssociatedObjectValue);
164 }
165 else
166 CoefficientsZ = Array.Empty<Complex>();
167
168 if (i < c && !(this.Arguments[i] is null) && Arguments[i] is ObjectVector)
169 {
170 ColorExpression = this.Arguments[i].SubExpression;
172 }
173 else
174 {
175 Palette = ColorModels.RandomLinearAnalogousHSL.CreatePalette(128, 4, out int Seed, this, Variables);
176 ColorExpression = "RandomLinearAnalogousHSL(128,4," + Seed.ToString() + ")";
177
178 if (i < c && this.Arguments[i] is null)
179 i++;
180 }
181
182 if (i < c)
183 dimx = (int)Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
184 else
185 dimx = 320;
186
187 if (i < c)
188 dimy = (int)Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
189 else
190 dimy = 200;
191
192 if (i < c)
193 {
194 throw new ScriptRuntimeException("Parameter mismatch in call to NewtonBuilderFractal(r,c,dr,Coefficients[,Palette][,dimx[,dimy]]).",
195 this);
196 }
197
198 if (dimx <= 0 || dimx > 5000 || dimy <= 0 || dimy > 5000)
199 throw new FractalImageSizeScriptException(this);
200
201 return NewtonFractal.CalcNewton(Variables, rc, ic, dr, R, CoefficientsZ, Palette, dimx, dimy, this, this.FractalZoomScript,
202 new object[] { Palette, dimx, dimy, R, CoefficientsZ, ColorExpression, rc, ic });
203 }
204
205 private string FractalZoomScript(double r, double i, double Size, object State)
206 {
207 object[] Parameters = (object[])State;
208 int DimX = (int)Parameters[1];
209 int DimY = (int)Parameters[2];
210 Complex R = (Complex)Parameters[3];
211 Complex[] CoefficientsZ = (Complex[])Parameters[4];
212 string ColorExpression = (string)Parameters[5];
213 double rc = (double)Parameters[6];
214 double ic = (double)Parameters[7];
215 Complex z0 = new Complex(r, i);
216 int j;
217
218 int c = CoefficientsZ.Length;
219 while (c > 1 && CoefficientsZ[c - 1].Equals(0))
220 c--;
221
222 Complex[] C2;
223
224 if (c < 1)
225 {
226 C2 = new Complex[2];
227 C2[0] = -z0;
228 C2[1] = Complex.One;
229 }
230 else
231 {
232 C2 = new Complex[c + 1];
233 C2[0] = Complex.Zero;
234 for (j = 0; j < c; j++)
235 C2[j + 1] = CoefficientsZ[j];
236
237 for (j = 0; j < c; j++)
238 C2[j] -= z0 * CoefficientsZ[j];
239 }
240
241 StringBuilder sb = new StringBuilder();
242
243 sb.Append("NewtonBuilderFractal((");
244 sb.Append(Expression.ToString(rc));
245 sb.Append(',');
246 sb.Append(Expression.ToString(ic));
247 sb.Append("),");
248 sb.Append(Expression.ToString(Size));
249 sb.Append(',');
250 sb.Append(Expression.ToString(R));
251 sb.Append(',');
252 sb.Append(Expression.ToString(C2));
253
254 if (!string.IsNullOrEmpty(ColorExpression))
255 {
256 sb.Append(',');
257 sb.Append(ColorExpression);
258 }
259
260 sb.Append(',');
261 sb.Append(DimX.ToString());
262 sb.Append(',');
263 sb.Append(DimY.ToString());
264 sb.Append(')');
265
266 return sb.ToString();
267 }
268
272 public override string FunctionName => nameof(NewtonBuilderFractal);
273 }
274}
Class managing a script expression.
Definition: Expression.cs:41
static Complex ToComplex(object Object)
Converts an object to a complex value.
Definition: Expression.cs:5228
static double ToDouble(object Object)
Converts an object to a double value.
Definition: Expression.cs:5110
static string ToString(double Value)
Converts a value to a string, that can be parsed as part of an expression.
Definition: Expression.cs:4760
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
Exception thrown if an image with invalid size is requested.
Defines a clickable fractal graph in the complex plane.
Definition: FractalGraph.cs:23
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:21
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:34
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