50 : base(new
ScriptNode[] { z, dr, R, Coefficients, Palette, DimX, DimY },
61 : base(new
ScriptNode[] { z, dr, R, Coefficients, Palette, DimX },
72 : base(new
ScriptNode[] { z, dr, R, Coefficients, Palette },
83 : base(new
ScriptNode[] { z, dr, R, Coefficients },
114 return new string[] {
"z",
"dr",
"R",
"Coefficients",
"Palette",
"DimX",
"DimY" };
123 string ColorExpression =
null;
125 double[] Coefficients =
null;
126 Complex[] CoefficientsZ =
null;
137 Obj =
Arguments[i++].AssociatedObjectValue;
138 if (Obj is Complex z)
154 if (i < c && ((Obj =
Arguments[i].AssociatedObjectValue) is
double || Obj is Complex))
163 if (i < c && this.Arguments[i] is
null)
170 Coefficients = (
double[])
Arguments[i++].AssociatedObjectValue;
172 CoefficientsZ = (Complex[])
Arguments[i++].AssociatedObjectValue;
182 CoefficientsZ =
new Complex[d];
183 for (j = 0; j < d; j++)
190 throw new ScriptRuntimeException(
"Lambda expression in calls to HalleyFractal() must be of one variable.",
this);
192 fDef = this.Arguments[i++];
197 " in call to HalleyFractal has to be a vector of numbers, containing coefficients " +
198 "of the polynomial to use. Now it was of type " +
Arguments[i].GetType().FullName,
207 ColorExpression = this.Arguments[i].SubExpression;
212 Palette = ColorModels.RandomLinearAnalogousHSL.CreatePalette(128, 4, out
int Seed,
this,
Variables);
213 ColorExpression =
"RandomLinearAnalogousHSL(128,4," + Seed.ToString() +
")";
215 if (i < c && this.Arguments[i] is
null)
231 throw new ScriptRuntimeException(
"Parameter mismatch in call to HalleyFractal(z,dr[,R][,Coefficients][,Palette][,dimx[,dimy]]).",
235 if (dimx <= 0 || dimx > 5000 || dimy <= 0 || dimy > 5000)
240 return CalcHalley(
Variables, rc, ic, dr, R, f, fDef, Palette, dimx, dimy,
241 this, this.FractalZoomScript,
242 new object[] { Palette, dimx, dimy, R, fDef, ColorExpression });
244 else if (!(CoefficientsZ is
null))
247 this, this.FractalZoomScript,
248 new object[] { Palette, dimx, dimy, R, CoefficientsZ, ColorExpression });
253 this, this.FractalZoomScript,
254 new object[] { Palette, dimx, dimy, R, Coefficients, ColorExpression });
258 private string FractalZoomScript(
double r,
double i,
double Size,
object State)
260 object[] Parameters = (
object[])State;
261 int DimX = (int)Parameters[1];
262 int DimY = (int)Parameters[2];
263 Complex R = (Complex)Parameters[3];
264 double[] Coefficients = Parameters[4] as
double[];
265 string ColorExpression = (string)Parameters[5];
267 StringBuilder sb =
new StringBuilder();
269 sb.Append(
"HalleyFractal((");
280 sb.Append(fDef.SubExpression);
281 else if (Parameters[4] is Complex[] CoefficientsZ)
286 if (!
string.IsNullOrEmpty(ColorExpression))
289 sb.Append(ColorExpression);
293 sb.Append(DimX.ToString());
295 sb.Append(DimY.ToString());
298 return sb.ToString();
317 double[] Coefficients, SKColor[] Palette,
int Width,
int Height,
ScriptNode Node,
318 FractalZoomScript FractalZoomScript,
object State)
324 double RIm = R.Imaginary;
325 double r0, i0, r1, i1;
328 double zr, zi, zr2, zi2, zr3, zi3, zr4, zi4, zr5, zi5, zr6, zi6;
334 int Degree = Coefficients.Length - 1;
339 greens =
new byte[N];
342 for (x = 0; x < N; x++)
346 greens[x] = cl.Green;
352 Array.Resize(ref Coefficients, 4);
354 Coefficients[++Degree] = 0;
357 double[] Prim =
new double[Degree];
358 for (x = 1; x <= Degree; x++)
359 Prim[x - 1] = x * Coefficients[x];
361 double[] Bis =
new double[Degree - 1];
362 for (x = 1; x < Degree; x++)
363 Bis[x - 1] = x * Prim[x];
367 Coefficients = (
double[])Coefficients.Clone();
368 Array.Reverse(Coefficients);
370 int size = Width * Height * 4;
373 byte[] rgb =
new byte[size];
376 r0 = rCenter - rDelta;
377 r1 = rCenter + rDelta;
379 aspect = ((double)Width) / Height;
381 i0 = iCenter - rDelta / aspect;
382 i1 = iCenter + rDelta / aspect;
384 dr = (r1 - r0) / Width;
385 di = (i1 - i0) / Height;
387 for (y = 0, i = i0, index = 0; y < Height; y++, i += di)
389 for (x = 0, r = r0; x < Width; x++, r += dr)
399 foreach (
double C
in Coefficients)
401 Temp = zr2 * zr - zi2 * zi + C;
402 zi2 = zr2 * zi + zi2 * zr;
408 foreach (
double C
in Prim)
410 Temp = zr3 * zr - zi3 * zi + C;
411 zi3 = zr3 * zi + zi3 * zr;
417 foreach (
double C
in Bis)
419 Temp = zr4 * zr - zi4 * zi + C;
420 zi4 = zr4 * zi + zi4 * zr;
426 zr5 = 2 * (zr2 * zr3 - zi2 * zi3);
427 zi5 = 2 * (zr2 * zi3 + zi2 * zr3);
431 zr6 = 2 * (zr3 * zr3 - zi3 * zi3) - (zr2 * zr4 - zi2 * zi4);
432 zi6 = 4 * zr3 * zi3 - (zr2 * zi4 + zr4 * zi2);
436 Temp = 1.0 / (zr6 * zr6 + zi6 * zi6);
437 zr4 = (zr5 * zr6 + zi5 * zi6) * Temp;
438 zi4 = (zi5 * zr6 - zr5 * zi6) * Temp;
441 Temp = zr4 * RRe - zi4 * RIm;
442 zi4 = zr4 * RIm + zi4 * RRe;
448 Temp = Math.Sqrt(zr4 * zr4 + zi4 * zi4);
450 while ((Temp > Conv) && (Temp < Div) && (n++ < N));
452 if (Temp < Conv && n < N)
454 rgb[index++] = blues[n];
455 rgb[index++] = greens[n];
456 rgb[index++] = reds[n];
470 return new FractalGraph(
Variables, Pixels, r0, i0, r1, i1, rDelta * 2,
true, Node,
FractalZoomScript, State);
477 Complex[] Coefficients, SKColor[] Palette,
int Width,
int Height,
ScriptNode Node,
478 FractalZoomScript FractalZoomScript,
object State)
484 double RIm = R.Imaginary;
485 double r0, i0, r1, i1;
488 double zr, zi, zr2, zi2, zr3, zi3, zr4, zi4, zr5, zi5, zr6, zi6;
494 int Degree = Coefficients.Length - 1;
499 greens =
new byte[N];
502 for (x = 0; x < N; x++)
506 greens[x] = cl.Green;
512 Array.Resize(ref Coefficients, 4);
514 Coefficients[++Degree] = Complex.Zero;
517 Complex[] Prim =
new Complex[Degree];
518 for (x = 1; x <= Degree; x++)
519 Prim[x - 1] = x * Coefficients[x];
521 Complex[] Bis =
new Complex[Degree - 1];
522 for (x = 1; x < Degree; x++)
523 Bis[x - 1] = x * Prim[x];
527 Coefficients = (Complex[])Coefficients.Clone();
528 Array.Reverse(Coefficients);
530 int j, c = Prim.Length;
532 double[] ReC =
new double[c + 1];
533 double[] ImC =
new double[c + 1];
534 double[] RePrim =
new double[c];
535 double[] ImPrim =
new double[c];
536 double[] ReBis =
new double[c2];
537 double[] ImBis =
new double[c2];
540 for (j = 0; j < c; j++)
544 ImC[j] = z.Imaginary;
548 ImPrim[j] = z.Imaginary;
554 ImBis[j] = z.Imaginary;
560 ImC[j] = z.Imaginary;
562 int size = Width * Height * 4;
565 byte[] rgb =
new byte[size];
568 r0 = rCenter - rDelta;
569 r1 = rCenter + rDelta;
571 aspect = ((double)Width) / Height;
573 i0 = iCenter - rDelta / aspect;
574 i1 = iCenter + rDelta / aspect;
576 dr = (r1 - r0) / Width;
577 di = (i1 - i0) / Height;
579 for (y = 0, i = i0, index = 0; y < Height; y++, i += di)
581 for (x = 0, r = r0; x < Width; x++, r += dr)
591 for (j = 0; j <= c; j++)
593 Temp = zr2 * zr - zi2 * zi + ReC[j];
594 zi2 = zr2 * zi + zi2 * zr + ImC[j];
600 for (j = 0; j < c; j++)
602 Temp = zr3 * zr - zi3 * zi + RePrim[j];
603 zi3 = zr3 * zi + zi3 * zr + ImPrim[j];
609 for (j = 0; j < c2; j++)
611 Temp = zr4 * zr - zi4 * zi + ReBis[j];
612 zi4 = zr4 * zi + zi4 * zr + ImBis[j];
618 zr5 = 2 * (zr2 * zr3 - zi2 * zi3);
619 zi5 = 2 * (zr2 * zi3 + zi2 * zr3);
623 zr6 = 2 * (zr3 * zr3 - zi3 * zi3) - (zr2 * zr4 - zi2 * zi4);
624 zi6 = 4 * zr3 * zi3 - (zr2 * zi4 + zr4 * zi2);
628 Temp = 1.0 / (zr6 * zr6 + zi6 * zi6);
629 zr4 = (zr5 * zr6 + zi5 * zi6) * Temp;
630 zi4 = (zi5 * zr6 - zr5 * zi6) * Temp;
633 Temp = zr4 * RRe - zi4 * RIm;
634 zi4 = zr4 * RIm + zi4 * RRe;
640 Temp = Math.Sqrt(zr4 * zr4 + zi4 * zi4);
642 while ((Temp > Conv) && (Temp < Div) && (n++ < N));
644 if (Temp < Conv && n < N)
646 rgb[index++] = blues[n];
647 rgb[index++] = greens[n];
648 rgb[index++] = reds[n];
662 return new FractalGraph(
Variables, Pixels, r0, i0, r1, i1, rDelta * 2,
true, Node,
FractalZoomScript, State);
670 FractalZoomScript FractalZoomScript,
object State)
676 double RIm = R.Imaginary;
677 double r0, i0, r1, i1;
687 greens =
new byte[N];
690 for (x = 0; x < N; x++)
694 greens[x] = cl.Green;
701 string ParameterName;
703 !(Differentiable.Differentiate(ParameterName = Differentiable.DefaultVariableName, v) is
ILambdaExpression fPrim))
714 int size = Width * Height * 4;
717 byte[] rgb =
new byte[size];
732 r0 = rCenter - rDelta;
733 r1 = rCenter + rDelta;
735 aspect = ((double)Width) / Height;
737 i0 = iCenter - rDelta / aspect;
738 i1 = iCenter + rDelta / aspect;
740 dr = (r1 - r0) / Width;
741 di = (i1 - i0) / Height;
743 for (y = 0, i = i0; y < Height; y++, i += di)
745 Row =
new Complex[Width];
746 Offset =
new int[Width];
749 for (x = 0, x2 = y * Width * 4, r = r0; x < Width; x++, r += dr, x2 += 4)
751 Row[x] =
new Complex(r, i);
756 while (n < N && c > 0)
761 Obj2 = fPrim.Evaluate(P, v);
762 Obj3 = fBis.Evaluate(P, v);
763 Row2 = Obj.AssociatedObjectValue as Complex[];
764 Row3 = Obj2.AssociatedObjectValue as Complex[];
765 Row4 = Obj3.AssociatedObjectValue as Complex[];
767 if (Row2 is
null || Row3 is
null || Row4 is
null)
769 throw new ScriptRuntimeException(
"Lambda expression (and its first and second derivative) must be able to accept complex vectors, " +
770 "and return complex vectors of equal length. Type returned: " +
771 Obj.GetType().FullName +
", " + Obj2.GetType().FullName +
" and " + Obj3.GetType().FullName,
774 else if (Row2.Length != c || Row3.Length != c)
776 throw new ScriptRuntimeException(
"Lambda expression (and its first and second derivative) must be able to accept complex vectors, " +
777 "and return complex vectors of equal length. Length returned: " +
778 Row2.Length.ToString() +
", " + Row3.Length.ToString() +
" and " + Row4.Length.ToString() +
779 ". Expected: " + c.ToString(), Node);
782 for (x = x2 = 0; x < c; x++)
789 z = R2 * z * z2 / (2 * z2 * z2 - z * z3);
794 if (Mod > Conv && Mod < Div)
812 rgb[j++] = greens[n];
822 Array.Resize(ref Row, x2);
823 Array.Resize(ref Offset, x2);
830 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
Exception thrown if an image with invalid size is requested.
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.