49 : base(new
ScriptNode[] { z, dr, R, Coefficients, Palette, DimX, DimY },
60 : base(new
ScriptNode[] { z, dr, R, Coefficients, Palette, DimX },
71 : base(new
ScriptNode[] { z, dr, R, Coefficients, Palette },
82 : base(new
ScriptNode[] { z, dr, R, Coefficients },
113 return new string[] {
"z",
"dr",
"R",
"Coefficients",
"Palette",
"DimX",
"DimY" };
122 string ColorExpression =
null;
124 double[] Coefficients =
null;
125 Complex[] CoefficientsZ =
null;
136 Obj =
Arguments[i++].AssociatedObjectValue;
137 if (Obj is Complex z)
153 if (i < c && ((Obj =
Arguments[i].AssociatedObjectValue) is
double || Obj is Complex))
162 if (i < c && this.Arguments[i] is
null)
169 Coefficients = (
double[])
Arguments[i++].AssociatedObjectValue;
171 CoefficientsZ = (Complex[])
Arguments[i++].AssociatedObjectValue;
181 CoefficientsZ =
new Complex[d];
182 for (j = 0; j < d; j++)
189 throw new ScriptRuntimeException(
"Lambda expression in calls to HalleyFractal() must be of one variable.",
this);
191 fDef = this.Arguments[i++];
196 " in call to HalleyFractal has to be a vector of numbers, containing coefficients " +
197 "of the polynomial to use. Now it was of type " +
Arguments[i].GetType().FullName,
206 ColorExpression = this.Arguments[i].SubExpression;
211 Palette = ColorModels.RandomLinearAnalogousHSL.CreatePalette(128, 4, out
int Seed,
this,
Variables);
212 ColorExpression =
"RandomLinearAnalogousHSL(128,4," + Seed.ToString() +
")";
214 if (i < c && this.Arguments[i] is
null)
230 throw new ScriptRuntimeException(
"Parameter mismatch in call to HalleyFractal(z,dr[,R][,Coefficients][,Palette][,dimx[,dimy]]).",
234 if (dimx <= 0 || dimx > 5000 || dimy <= 0 || dimy > 5000)
239 return CalcHalley(
Variables, rc, ic, dr, R, f, fDef, Palette, dimx, dimy,
240 this, this.FractalZoomScript,
241 new object[] { Palette, dimx, dimy, R, fDef, ColorExpression });
243 else if (!(CoefficientsZ is
null))
246 this, this.FractalZoomScript,
247 new object[] { Palette, dimx, dimy, R, CoefficientsZ, ColorExpression });
252 this, this.FractalZoomScript,
253 new object[] { Palette, dimx, dimy, R, Coefficients, ColorExpression });
257 private string FractalZoomScript(
double r,
double i,
double Size,
object State)
259 object[] Parameters = (
object[])State;
260 int DimX = (int)Parameters[1];
261 int DimY = (int)Parameters[2];
262 Complex R = (Complex)Parameters[3];
263 double[] Coefficients = Parameters[4] as
double[];
264 string ColorExpression = (string)Parameters[5];
266 StringBuilder sb =
new StringBuilder();
268 sb.Append(
"HalleyFractal((");
279 sb.Append(fDef.SubExpression);
280 else if (Parameters[4] is Complex[] CoefficientsZ)
285 if (!
string.IsNullOrEmpty(ColorExpression))
288 sb.Append(ColorExpression);
292 sb.Append(DimX.ToString());
294 sb.Append(DimY.ToString());
297 return sb.ToString();
316 double[] Coefficients, SKColor[] Palette,
int Width,
int Height,
ScriptNode Node,
317 FractalZoomScript FractalZoomScript,
object State)
323 double RIm = R.Imaginary;
324 double r0, i0, r1, i1;
327 double zr, zi, zr2, zi2, zr3, zi3, zr4, zi4, zr5, zi5, zr6, zi6;
333 int Degree = Coefficients.Length - 1;
338 greens =
new byte[N];
341 for (x = 0; x < N; x++)
345 greens[x] = cl.Green;
351 Array.Resize(ref Coefficients, 4);
353 Coefficients[++Degree] = 0;
356 double[] Prim =
new double[Degree];
357 for (x = 1; x <= Degree; x++)
358 Prim[x - 1] = x * Coefficients[x];
360 double[] Bis =
new double[Degree - 1];
361 for (x = 1; x < Degree; x++)
362 Bis[x - 1] = x * Prim[x];
366 Coefficients = (
double[])Coefficients.Clone();
367 Array.Reverse(Coefficients);
369 int size = Width * Height * 4;
372 byte[] rgb =
new byte[size];
375 r0 = rCenter - rDelta;
376 r1 = rCenter + rDelta;
378 aspect = ((double)Width) / Height;
380 i0 = iCenter - rDelta / aspect;
381 i1 = iCenter + rDelta / aspect;
383 dr = (r1 - r0) / Width;
384 di = (i1 - i0) / Height;
386 for (y = 0, i = i0, index = 0; y < Height; y++, i += di)
388 for (x = 0, r = r0; x < Width; x++, r += dr)
398 foreach (
double C
in Coefficients)
400 Temp = zr2 * zr - zi2 * zi + C;
401 zi2 = zr2 * zi + zi2 * zr;
407 foreach (
double C
in Prim)
409 Temp = zr3 * zr - zi3 * zi + C;
410 zi3 = zr3 * zi + zi3 * zr;
416 foreach (
double C
in Bis)
418 Temp = zr4 * zr - zi4 * zi + C;
419 zi4 = zr4 * zi + zi4 * zr;
425 zr5 = 2 * (zr2 * zr3 - zi2 * zi3);
426 zi5 = 2 * (zr2 * zi3 + zi2 * zr3);
430 zr6 = 2 * (zr3 * zr3 - zi3 * zi3) - (zr2 * zr4 - zi2 * zi4);
431 zi6 = 4 * zr3 * zi3 - (zr2 * zi4 + zr4 * zi2);
435 Temp = 1.0 / (zr6 * zr6 + zi6 * zi6);
436 zr4 = (zr5 * zr6 + zi5 * zi6) * Temp;
437 zi4 = (zi5 * zr6 - zr5 * zi6) * Temp;
440 Temp = zr4 * RRe - zi4 * RIm;
441 zi4 = zr4 * RIm + zi4 * RRe;
447 Temp = Math.Sqrt(zr4 * zr4 + zi4 * zi4);
449 while ((Temp > Conv) && (Temp < Div) && (n++ < N));
451 if (Temp < Conv && n < N)
453 rgb[index++] = blues[n];
454 rgb[index++] = greens[n];
455 rgb[index++] = reds[n];
469 return new FractalGraph(
Variables, Pixels, r0, i0, r1, i1, rDelta * 2,
true, Node,
FractalZoomScript, State);
476 Complex[] Coefficients, SKColor[] Palette,
int Width,
int Height,
ScriptNode Node,
477 FractalZoomScript FractalZoomScript,
object State)
483 double RIm = R.Imaginary;
484 double r0, i0, r1, i1;
487 double zr, zi, zr2, zi2, zr3, zi3, zr4, zi4, zr5, zi5, zr6, zi6;
493 int Degree = Coefficients.Length - 1;
498 greens =
new byte[N];
501 for (x = 0; x < N; x++)
505 greens[x] = cl.Green;
511 Array.Resize(ref Coefficients, 4);
513 Coefficients[++Degree] = Complex.Zero;
516 Complex[] Prim =
new Complex[Degree];
517 for (x = 1; x <= Degree; x++)
518 Prim[x - 1] = x * Coefficients[x];
520 Complex[] Bis =
new Complex[Degree - 1];
521 for (x = 1; x < Degree; x++)
522 Bis[x - 1] = x * Prim[x];
526 Coefficients = (Complex[])Coefficients.Clone();
527 Array.Reverse(Coefficients);
529 int j, c = Prim.Length;
531 double[] ReC =
new double[c + 1];
532 double[] ImC =
new double[c + 1];
533 double[] RePrim =
new double[c];
534 double[] ImPrim =
new double[c];
535 double[] ReBis =
new double[c2];
536 double[] ImBis =
new double[c2];
539 for (j = 0; j < c; j++)
543 ImC[j] = z.Imaginary;
547 ImPrim[j] = z.Imaginary;
553 ImBis[j] = z.Imaginary;
559 ImC[j] = z.Imaginary;
561 int size = Width * Height * 4;
564 byte[] rgb =
new byte[size];
567 r0 = rCenter - rDelta;
568 r1 = rCenter + rDelta;
570 aspect = ((double)Width) / Height;
572 i0 = iCenter - rDelta / aspect;
573 i1 = iCenter + rDelta / aspect;
575 dr = (r1 - r0) / Width;
576 di = (i1 - i0) / Height;
578 for (y = 0, i = i0, index = 0; y < Height; y++, i += di)
580 for (x = 0, r = r0; x < Width; x++, r += dr)
590 for (j = 0; j <= c; j++)
592 Temp = zr2 * zr - zi2 * zi + ReC[j];
593 zi2 = zr2 * zi + zi2 * zr + ImC[j];
599 for (j = 0; j < c; j++)
601 Temp = zr3 * zr - zi3 * zi + RePrim[j];
602 zi3 = zr3 * zi + zi3 * zr + ImPrim[j];
608 for (j = 0; j < c2; j++)
610 Temp = zr4 * zr - zi4 * zi + ReBis[j];
611 zi4 = zr4 * zi + zi4 * zr + ImBis[j];
617 zr5 = 2 * (zr2 * zr3 - zi2 * zi3);
618 zi5 = 2 * (zr2 * zi3 + zi2 * zr3);
622 zr6 = 2 * (zr3 * zr3 - zi3 * zi3) - (zr2 * zr4 - zi2 * zi4);
623 zi6 = 4 * zr3 * zi3 - (zr2 * zi4 + zr4 * zi2);
627 Temp = 1.0 / (zr6 * zr6 + zi6 * zi6);
628 zr4 = (zr5 * zr6 + zi5 * zi6) * Temp;
629 zi4 = (zi5 * zr6 - zr5 * zi6) * Temp;
632 Temp = zr4 * RRe - zi4 * RIm;
633 zi4 = zr4 * RIm + zi4 * RRe;
639 Temp = Math.Sqrt(zr4 * zr4 + zi4 * zi4);
641 while ((Temp > Conv) && (Temp < Div) && (n++ < N));
643 if (Temp < Conv && n < N)
645 rgb[index++] = blues[n];
646 rgb[index++] = greens[n];
647 rgb[index++] = reds[n];
661 return new FractalGraph(
Variables, Pixels, r0, i0, r1, i1, rDelta * 2,
true, Node,
FractalZoomScript, State);
669 FractalZoomScript FractalZoomScript,
object State)
675 double RIm = R.Imaginary;
676 double r0, i0, r1, i1;
686 greens =
new byte[N];
689 for (x = 0; x < N; x++)
693 greens[x] = cl.Green;
700 string ParameterName;
702 !(Differentiable.Differentiate(ParameterName = Differentiable.DefaultVariableName, v) is
ILambdaExpression fPrim))
713 int size = Width * Height * 4;
716 byte[] rgb =
new byte[size];
731 r0 = rCenter - rDelta;
732 r1 = rCenter + rDelta;
734 aspect = ((double)Width) / Height;
736 i0 = iCenter - rDelta / aspect;
737 i1 = iCenter + rDelta / aspect;
739 dr = (r1 - r0) / Width;
740 di = (i1 - i0) / Height;
742 for (y = 0, i = i0; y < Height; y++, i += di)
744 Row =
new Complex[Width];
745 Offset =
new int[Width];
748 for (x = 0, x2 = y * Width * 4, r = r0; x < Width; x++, r += dr, x2 += 4)
750 Row[x] =
new Complex(r, i);
755 while (n < N && c > 0)
760 Obj2 = fPrim.Evaluate(P, v);
761 Obj3 = fBis.Evaluate(P, v);
762 Row2 = Obj.AssociatedObjectValue as Complex[];
763 Row3 = Obj2.AssociatedObjectValue as Complex[];
764 Row4 = Obj3.AssociatedObjectValue as Complex[];
766 if (Row2 is
null || Row3 is
null || Row4 is
null)
768 throw new ScriptRuntimeException(
"Lambda expression (and its first and second derivative) must be able to accept complex vectors, " +
769 "and return complex vectors of equal length. Type returned: " +
770 Obj.GetType().FullName +
", " + Obj2.GetType().FullName +
" and " + Obj3.GetType().FullName,
773 else if (Row2.Length != c || Row3.Length != c)
775 throw new ScriptRuntimeException(
"Lambda expression (and its first and second derivative) must be able to accept complex vectors, " +
776 "and return complex vectors of equal length. Length returned: " +
777 Row2.Length.ToString() +
", " + Row3.Length.ToString() +
" and " + Row4.Length.ToString() +
778 ". Expected: " + c.ToString(), Node);
781 for (x = x2 = 0; x < c; x++)
788 z = R2 * z * z2 / (2 * z2 * z2 - z * z3);
793 if (Mod > Conv && Mod < Div)
811 rgb[j++] = greens[n];
821 Array.Resize(ref Row, x2);
822 Array.Resize(ref Offset, x2);
829 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 Halley Fractal Image
HalleyFractal(ScriptNode z, ScriptNode dr, ScriptNode R, ScriptNode Coefficients, ScriptNode Palette, int Start, int Length, Expression Expression)
TODO
HalleyFractal(ScriptNode z, ScriptNode dr, int Start, int Length, Expression Expression)
TODO
static FractalGraph CalcHalley(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
HalleyFractal(ScriptNode z, ScriptNode dr, ScriptNode R, ScriptNode Coefficients, ScriptNode Palette, ScriptNode DimX, int Start, int Length, Expression Expression)
TODO
HalleyFractal(ScriptNode z, ScriptNode dr, ScriptNode R, int Start, int Length, Expression Expression)
TODO
HalleyFractal(ScriptNode z, ScriptNode dr, ScriptNode R, ScriptNode Coefficients, ScriptNode Palette, ScriptNode DimX, ScriptNode DimY, int Start, int Length, Expression Expression)
TODO
HalleyFractal(ScriptNode z, ScriptNode dr, ScriptNode R, ScriptNode Coefficients, int Start, int Length, Expression Expression)
TODO
override string[] DefaultArgumentNames
TODO
static FractalGraph CalcHalley(Variables Variables, double rCenter, double iCenter, double rDelta, Complex R, double[] Coefficients, SKColor[] Palette, int Width, int Height, ScriptNode Node, FractalZoomScript FractalZoomScript, object State)
Calculates a Halley Fractal
override IElement Evaluate(IElement[] Arguments, Variables Variables)
TODO
static FractalGraph CalcHalley(Variables Variables, double rCenter, double iCenter, double rDelta, Complex R, ILambdaExpression f, ScriptNode _, SKColor[] Palette, int Width, int Height, ScriptNode Node, FractalZoomScript FractalZoomScript, object State)
TODO
override string FunctionName
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.