53 : base(new
ScriptNode[] { z, dr, R, c, Palette, DimX, DimY },
65 : base(new
ScriptNode[] { z, dr, R, c, Palette, DimX },
77 : base(new
ScriptNode[] { z, dr, R, c, Palette },
91 ArgumentType.Normal },
103 return new string[] {
"z",
"dr",
"R",
"c",
"Palette",
"DimX",
"DimY" };
112 string ColorExpression =
null;
114 double[] Coefficients =
null;
115 Complex[] CoefficientsZ =
null;
126 Obj =
Arguments[i++].AssociatedObjectValue;
127 if (Obj is Complex z)
143 if (i < c && ((Obj =
Arguments[i].AssociatedObjectValue) is
double || Obj is Complex))
152 if (i < c && this.Arguments[i] is
null)
159 Coefficients = (
double[])
Arguments[i++].AssociatedObjectValue;
161 CoefficientsZ = (Complex[])
Arguments[i++].AssociatedObjectValue;
171 CoefficientsZ =
new Complex[d];
172 for (j = 0; j < d; j++)
179 throw new ScriptRuntimeException(
"Lambda expression in calls to NewtonFractal() must be of one variable.",
this);
181 fDef = this.Arguments[i++];
186 " in call to NewtonFractal has to be a vector of numbers, containing coefficients " +
187 "of the polynomial to use. Now it was of type " +
Arguments[i].GetType().FullName,
196 ColorExpression = this.Arguments[i].SubExpression;
201 Palette = ColorModels.RandomLinearAnalogousHSL.CreatePalette(128, 4, out
int Seed,
this,
Variables);
202 ColorExpression =
"RandomLinearAnalogousHSL(128,4," + Seed.ToString() +
")";
204 if (i < c && this.Arguments[i] is
null)
220 throw new ScriptRuntimeException(
"Parameter mismatch in call to NewtonFractal(r,c,dr,Coefficients[,Palette][,dimx[,dimy]]).",
224 if (dimx <= 0 || dimx > 5000 || dimy <= 0 || dimy > 5000)
229 return CalcNewton(rc, ic, dr, R, f, fDef,
Variables, Palette, dimx, dimy,
230 this, this.FractalZoomScript,
new object[] { Palette, dimx, dimy, R, fDef, ColorExpression });
232 else if (!(CoefficientsZ is
null))
235 this, this.FractalZoomScript,
new object[] { Palette, dimx, dimy, R, CoefficientsZ, ColorExpression });
240 this, this.FractalZoomScript,
new object[] { Palette, dimx, dimy, R, Coefficients, ColorExpression });
244 private string FractalZoomScript(
double r,
double i,
double Size,
object State)
246 object[] Parameters = (
object[])State;
247 int DimX = (int)Parameters[1];
248 int DimY = (int)Parameters[2];
249 Complex R = (Complex)Parameters[3];
250 double[] Coefficients = Parameters[4] as
double[];
251 string ColorExpression = (string)Parameters[5];
253 StringBuilder sb =
new StringBuilder();
255 sb.Append(
"NewtonFractal((");
266 sb.Append(fDef.SubExpression);
267 else if (Parameters[4] is Complex[] CoefficientsZ)
272 if (!
string.IsNullOrEmpty(ColorExpression))
275 sb.Append(ColorExpression);
279 sb.Append(DimX.ToString());
281 sb.Append(DimY.ToString());
284 return sb.ToString();
291 double[] Coefficients, SKColor[] Palette,
int Width,
int Height,
ScriptNode Node, FractalZoomScript FractalZoomScript,
297 double r0, i0, r1, i1;
300 double zr, zi, zr2, zi2, zr3, zi3, zr4, zi4;
302 double RIm = R.Imaginary;
308 int Degree = Coefficients.Length - 1;
313 greens =
new byte[N];
316 for (x = 0; x < N; x++)
320 greens[x] = cl.Green;
326 Array.Resize(ref Coefficients, 3);
328 Coefficients[++Degree] = 0;
331 double[] Prim =
new double[Degree];
332 for (x = 1; x <= Degree; x++)
333 Prim[x - 1] = x * Coefficients[x];
336 Coefficients = (
double[])Coefficients.Clone();
337 Array.Reverse(Coefficients);
339 int size = Width * Height * 4;
342 byte[] rgb =
new byte[size];
345 r0 = rCenter - rDelta;
346 r1 = rCenter + rDelta;
348 aspect = ((double)Width) / Height;
350 i0 = iCenter - rDelta / aspect;
351 i1 = iCenter + rDelta / aspect;
353 dr = (r1 - r0) / Width;
354 di = (i1 - i0) / Height;
356 for (y = 0, i = i0, index = 0; y < Height; y++, i += di)
358 for (x = 0, r = r0; x < Width; x++, r += dr)
368 foreach (
double C
in Coefficients)
370 Temp = zr2 * zr - zi2 * zi + C;
371 zi2 = zr2 * zi + zi2 * zr;
377 foreach (
double C
in Prim)
379 Temp = zr3 * zr - zi3 * zi + C;
380 zi3 = zr3 * zi + zi3 * zr;
386 Temp = 1.0 / (zr3 * zr3 + zi3 * zi3);
387 zr4 = (zr2 * zr3 + zi2 * zi3) * Temp;
388 zi4 = (zi2 * zr3 - zr2 * zi3) * Temp;
391 Temp = zr4 * RRe - zi4 * RIm;
392 zi4 = zr4 * RIm + zi4 * RRe;
398 Temp = Math.Sqrt(zr4 * zr4 + zi4 * zi4);
400 while ((Temp > Conv) && (Temp < Div) && (n++ < N));
402 if (Temp < Conv && n < N)
404 rgb[index++] = blues[n];
405 rgb[index++] = greens[n];
406 rgb[index++] = reds[n];
420 return new FractalGraph(
Variables, Pixels, r0, i0, r1, i1, rDelta * 2,
true, Node,
FractalZoomScript, State);
427 Complex[] Coefficients, SKColor[] Palette,
int Width,
int Height,
ScriptNode Node, FractalZoomScript FractalZoomScript,
434 double RIm = R.Imaginary;
435 double r0, i0, r1, i1;
438 double zr, zi, zr2, zi2, zr3, zi3, zr4, zi4;
444 int Degree = Coefficients.Length - 1;
449 greens =
new byte[N];
452 for (x = 0; x < N; x++)
456 greens[x] = cl.Green;
462 Array.Resize(ref Coefficients, 3);
464 Coefficients[++Degree] = Complex.Zero;
467 Complex[] Prim =
new Complex[Degree];
468 for (x = 1; x <= Degree; x++)
469 Prim[x - 1] = x * Coefficients[x];
472 Coefficients = (Complex[])Coefficients.Clone();
473 Array.Reverse(Coefficients);
475 int j, c = Prim.Length;
476 double[] ReC =
new double[c + 1];
477 double[] ImC =
new double[c + 1];
478 double[] RePrim =
new double[c];
479 double[] ImPrim =
new double[c];
482 for (j = 0; j < c; j++)
486 ImC[j] = z.Imaginary;
490 ImPrim[j] = z.Imaginary;
495 ImC[j] = z.Imaginary;
497 int size = Width * Height * 4;
500 byte[] rgb =
new byte[size];
503 r0 = rCenter - rDelta;
504 r1 = rCenter + rDelta;
506 aspect = ((double)Width) / Height;
508 i0 = iCenter - rDelta / aspect;
509 i1 = iCenter + rDelta / aspect;
511 dr = (r1 - r0) / Width;
512 di = (i1 - i0) / Height;
514 for (y = 0, i = i0, index = 0; y < Height; y++, i += di)
516 for (x = 0, r = r0; x < Width; x++, r += dr)
526 for (j = 0; j <= c; j++)
528 Temp = zr2 * zr - zi2 * zi + ReC[j];
529 zi2 = zr2 * zi + zi2 * zr + ImC[j];
535 for (j = 0; j < c; j++)
537 Temp = zr3 * zr - zi3 * zi + RePrim[j];
538 zi3 = zr3 * zi + zi3 * zr + ImPrim[j];
544 Temp = 1.0 / (zr3 * zr3 + zi3 * zi3);
545 zr4 = (zr2 * zr3 + zi2 * zi3) * Temp;
546 zi4 = (zi2 * zr3 - zr2 * zi3) * Temp;
549 Temp = zr4 * RRe - zi4 * RIm;
550 zi4 = zr4 * RIm + zi4 * RRe;
556 Temp = Math.Sqrt(zr4 * zr4 + zi4 * zi4);
558 while ((Temp > Conv) && (Temp < Div) && (n++ < N));
560 if (Temp < Conv && n < N)
562 rgb[index++] = blues[n];
563 rgb[index++] = greens[n];
564 rgb[index++] = reds[n];
578 return new FractalGraph(
Variables, Pixels, r0, i0, r1, i1, rDelta * 2,
true, Node,
FractalZoomScript, State);
586 ScriptNode Node, FractalZoomScript FractalZoomScript,
object State)
592 double RIm = R.Imaginary;
593 double r0, i0, r1, i1;
604 greens =
new byte[N];
607 for (x = 0; x < N; x++)
611 greens[x] = cl.Green;
619 !(Differentiable.Differentiate(Differentiable.DefaultVariableName, v) is
ILambdaExpression fPrim))
624 int size = Width * Height * 4;
627 byte[] rgb =
new byte[size];
639 r0 = rCenter - rDelta;
640 r1 = rCenter + rDelta;
642 aspect = ((double)Width) / Height;
644 i0 = iCenter - rDelta / aspect;
645 i1 = iCenter + rDelta / aspect;
647 dr = (r1 - r0) / Width;
648 di = (i1 - i0) / Height;
650 for (y = 0, i = i0; y < Height; y++, i += di)
652 Row =
new Complex[Width];
653 Offset =
new int[Width];
656 for (x = 0, x2 = y * Width * 4, r = r0; x < Width; x++, r += dr, x2 += 4)
658 Row[x] =
new Complex(r, i);
663 while (n < N && c > 0)
668 Obj2 = fPrim.Evaluate(P, v);
669 Row2 = Obj.AssociatedObjectValue as Complex[];
670 Row3 = Obj2.AssociatedObjectValue as Complex[];
672 if (Row2 is
null || Row3 is
null)
674 throw new ScriptRuntimeException(
"Lambda expression (and its first derivative) must be able to accept complex vectors, " +
675 "and return complex vectors of equal length. Type returned: " +
676 Obj.GetType().FullName +
" and " + Obj2.GetType().FullName, Node);
678 else if (Row2.Length != c || Row3.Length != c)
680 throw new ScriptRuntimeException(
"Lambda expression (and its first derivative) must be able to accept complex vectors, " +
681 "and return complex vectors of equal length. Length returned: " +
682 Row2.Length.ToString() +
" and " + Row3.Length.ToString() +
683 ". Expected: " + c.ToString(), Node);
686 for (x = x2 = 0; x < c; x++)
689 z = R * Row2[x] / Row3[x];
694 if (Mod > Conv && Mod < Div)
712 rgb[j++] = greens[n];
722 Array.Resize(ref Row, x2);
723 Array.Resize(ref Offset, x2);
730 return new FractalGraph(
Variables, Pixels, r0, i0, r1, i1, rDelta * 2,
true, Node,
FractalZoomScript, State);
Script runtime exception.
Class managing a script expression.
static Complex ToComplex(object Object)
Converts an object to a complex value.
static double ToDouble(object Object)
Converts an object to a double value.
static string ToString(double Value)
Converts a value to a string, that can be parsed as part of an expression.
Calculates a Newton Fractal Image
static FractalGraph CalcNewton(Variables Variables, double rCenter, double iCenter, double rDelta, Complex R, Complex[] Coefficients, SKColor[] Palette, int Width, int Height, ScriptNode Node, FractalZoomScript FractalZoomScript, object State)
TODO
override IElement Evaluate(IElement[] Arguments, Variables Variables)
TODO
NewtonFractal(ScriptNode z, ScriptNode dr, ScriptNode R, ScriptNode c, ScriptNode Palette, ScriptNode DimX, int Start, int Length, Expression Expression)
TODO
static FractalGraph CalcNewton(double rCenter, double iCenter, double rDelta, Complex R, ILambdaExpression f, ScriptNode _, Variables Variables, SKColor[] Palette, int Width, int Height, ScriptNode Node, FractalZoomScript FractalZoomScript, object State)
TODO
override string FunctionName
TODO
NewtonFractal(ScriptNode z, ScriptNode dr, ScriptNode R, ScriptNode c, ScriptNode Palette, int Start, int Length, Expression Expression)
TODO
NewtonFractal(ScriptNode z, ScriptNode dr, ScriptNode R, ScriptNode c, int Start, int Length, Expression Expression)
TODO
NewtonFractal(ScriptNode z, ScriptNode dr, ScriptNode R, ScriptNode c, ScriptNode Palette, ScriptNode DimX, ScriptNode DimY, 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
override string[] DefaultArgumentNames
TODO
Defines a clickable fractal graph in the complex plane.
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.
int Length
Length of expression covered by node.
override string ToString()
Expression Expression
Expression of which the node is a part.
int Start
Start position in script expression.
void CopyTo(Variables Variables)
Copies available variables to another variable collection.
Basic interface for all types of elements.
object AssociatedObjectValue
Associated object value.
Basic interface for vectors.
int Dimension
Dimension of vector.
IElement GetElement(int Index)
Gets an element of the vector.
Base interface for lambda expressions.
Base interface for lambda expressions.
IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the lambda expression.
int NrArguments
Number of arguments.
delegate string FractalZoomScript(double r, double i, double Size, object State)
Generates new script when zoomed.
ArgumentType
Type of parameter used in a function definition or a lambda definition.