protected override void Merge(ILambdaExpression accumulatorLambda, ILambdaExpression lambda)
    {
      var declaredElement = lambda.ParameterDeclarations[0].DeclaredElement;

      var replacement = accumulatorLambda.BodyExpression;

      var toReplace = new LocalList<IReferenceExpression>();

      foreach (var referenceExpression in lambda.BodyExpression.Descendants<IReferenceExpression>())
      {
        var currentElement = referenceExpression.Reference.Resolve().DeclaredElement;

        if (declaredElement.Equals(currentElement) ||
            referenceExpression.QualifierExpression == null && 
            referenceExpression.NameIdentifier.Name == declaredElement.ShortName)
        {
          toReplace.Add(referenceExpression);
        }
      }

      for (int i = 0; i < toReplace.Count; i++)
      {
        toReplace[i].ReplaceBy(replacement);
      }

      accumulatorLambda.SetBodyExpression(lambda.BodyExpression);
    }
Esempio n. 2
0
        public static FractalGraph CalcJulia(double rCenter, double iCenter, ILambdaExpression f,
                                             ScriptNode fDef, double rDelta, Variables Variables,
                                             SKColor[] Palette, int Width, int Height, ScriptNode Node,
                                             FractalZoomScript FractalZoomScript, object State)
        {
            double r0, i0, r1, i1;
            double dr, di;
            double r, i;
            double aspect;
            int    x, y;
            int    n, N;

            N = Palette.Length;

            rDelta *= 0.5;
            r0      = rCenter - rDelta;
            r1      = rCenter + rDelta;

            aspect = ((double)Width) / Height;

            i0 = iCenter - rDelta / aspect;
            i1 = iCenter + rDelta / aspect;

            dr = (r1 - r0) / Width;
            di = (i1 - i0) / Height;

            int Size = Width * Height;

            int[]   ColorIndex = new int[Size];
            int     c, j, x2;
            Complex z;

            IElement[] P = new IElement[1];
            IElement   Obj;
            double     Mod;

            for (y = 0, i = i0; y < Height; y++, i += di)
            {
                Complex[] Row    = new Complex[Width];
                int[]     Offset = new int[Width];

                c = Width;
                for (x = 0, x2 = y * Width, r = r0; x < Width; x++, r += dr, x2++)
                {
                    Row[x]    = new Complex(r, i);
                    Offset[x] = x2;
                }

                Variables v = new Variables();
                Variables.CopyTo(v);

                n = 0;
                while (n < N && c > 0)
                {
                    n++;
                    P[0] = Expression.Encapsulate(Row);
                    Obj  = f.Evaluate(P, v);
                    Row  = Obj.AssociatedObjectValue as Complex[];

                    if (Row == null)
                    {
                        throw new ScriptRuntimeException("Lambda expression must be able to accept complex vectors, " +
                                                         "and return complex vectors of equal length. Type returned: " +
                                                         Obj.GetType().FullName, Node);
                    }
                    else if (Row.Length != c)
                    {
                        throw new ScriptRuntimeException("Lambda expression must be able to accept complex vectors, " +
                                                         "and return complex vectors of equal length. Length returned: " +
                                                         Row.Length.ToString() + ". Expected: " + c.ToString(), Node);
                    }

                    for (x = x2 = 0; x < c; x++)
                    {
                        z = Row[x];
                        j = Offset[x];

                        Mod = z.Magnitude;

                        if (Mod < 3)
                        {
                            if (x != x2)
                            {
                                Row[x2]    = z;
                                Offset[x2] = j;
                            }

                            x2++;
                        }
                        else
                        {
                            ColorIndex[j++] = n;
                        }
                    }

                    if (x2 < x)
                    {
                        Array.Resize <Complex>(ref Row, x2);
                        Array.Resize <int>(ref Offset, x2);
                        c = x2;
                    }
                }

                if (c > 0)
                {
                    for (x = 0; x < c; x++)
                    {
                        j             = Offset[x];
                        ColorIndex[j] = N;
                    }
                }
            }

            int[] Boundary = FractalGraph.FindBoundaries(ColorIndex, Width, Height);

            return(new FractalGraph(FractalGraph.ToBitmap(Boundary, Width, Height, Palette),
                                    r0, i0, r1, i1, rDelta * 2, true, Node, FractalZoomScript, State));
        }
        public static FractalGraph CalcMandelbrot(double rCenter, double iCenter, double rDelta,
                                                  ILambdaExpression f, Variables Variables, SKColor[] Palette, int Width, int Height,
                                                  ScriptNode Node, FractalZoomScript FractalZoomScript, object State)
        {
            byte[]  reds;
            byte[]  greens;
            byte[]  blues;
            double  r0, i0, r1, i1;
            double  dr, di;
            double  r, i, Mod;
            double  aspect;
            int     x, x2, y;
            int     n, N;
            SKColor cl;

            N      = Palette.Length;
            reds   = new byte[N];
            greens = new byte[N];
            blues  = new byte[N];

            for (x = 0; x < N; x++)
            {
                cl        = Palette[x];
                reds[x]   = cl.Red;
                greens[x] = cl.Green;
                blues[x]  = cl.Blue;
            }

            int size = Width * Height * 4;

            byte[]  rgb = new byte[size];
            Complex z;

            IElement[] P = new IElement[2];
            int        j, c;
            IElement   Obj;

            rDelta *= 0.5;
            r0      = rCenter - rDelta;
            r1      = rCenter + rDelta;

            aspect = ((double)Width) / Height;

            i0 = iCenter - rDelta / aspect;
            i1 = iCenter + rDelta / aspect;

            dr = (r1 - r0) / Width;
            di = (i1 - i0) / Height;

            for (y = 0, i = i0; y < Height; y++, i += di)
            {
                Complex[] Row    = new Complex[Width];
                Complex[] Row0   = new Complex[Width];
                int[]     Offset = new int[Width];

                c = Width;
                for (x = 0, x2 = y * Width * 4, r = r0; x < Width; x++, r += dr, x2 += 4)
                {
                    Row[x]    = Row0[x] = new Complex(r, i);
                    Offset[x] = x2;
                }

                Variables v = new Variables();
                Variables.CopyTo(v);

                n = 0;
                while (n < N && c > 0)
                {
                    n++;
                    P[0] = Expression.Encapsulate(Row);
                    P[1] = Expression.Encapsulate(Row0);
                    Obj  = f.Evaluate(P, v);
                    Row  = Obj.AssociatedObjectValue as Complex[];

                    if (Row is null)
                    {
                        throw new ScriptRuntimeException("Lambda expression must be able to accept complex vectors, " +
                                                         "and return complex vectors of equal length. Type returned: " +
                                                         Obj.GetType().FullName, Node);
                    }
                    else if (Row.Length != c)
                    {
                        throw new ScriptRuntimeException("Lambda expression must be able to accept complex vectors, " +
                                                         "and return complex vectors of equal length. Length returned: " +
                                                         Row.Length.ToString() + ". Expected: " + c.ToString(), Node);
                    }

                    for (x = x2 = 0; x < c; x++)
                    {
                        z = Row[x];
                        j = Offset[x];

                        Mod = z.Magnitude;

                        if (Mod < 3)
                        {
                            if (x != x2)
                            {
                                Row[x2]    = z;
                                Row0[x2]   = Row0[x];
                                Offset[x2] = j;
                            }

                            x2++;
                        }
                        else
                        {
                            if (n >= N)
                            {
                                rgb[j++] = 0;
                                rgb[j++] = 0;
                                rgb[j++] = 0;
                            }
                            else
                            {
                                rgb[j++] = blues[n];
                                rgb[j++] = greens[n];
                                rgb[j++] = reds[n];
                            }

                            rgb[j++] = 255;
                        }
                    }

                    if (x2 < x)
                    {
                        Array.Resize <Complex>(ref Row, x2);
                        Array.Resize <Complex>(ref Row0, x2);
                        Array.Resize <int>(ref Offset, x2);
                        c = x2;
                    }
                }

                if (c > 0)
                {
                    for (x = 0; x < c; x++)
                    {
                        j = Offset[x];

                        rgb[j++] = 0;
                        rgb[j++] = 0;
                        rgb[j++] = 0;
                        rgb[j++] = 255;
                    }
                }
            }

            using (SKData Data = SKData.Create(new MemoryStream(rgb)))
            {
                SKImage Bitmap = SKImage.FromPixelData(new SKImageInfo(Width, Height, SKColorType.Bgra8888), Data, Width * 4);
                return(new FractalGraph(Bitmap, r0, i0, r1, i1, rDelta * 2, true, Node, FractalZoomScript, State));
            }
        }
 public override void VisitLambdaExpression([NotNull] ILambdaExpression operation)
 {
 }
 public override void Visit(ILambdaExpression expr, IKaVESet <IName> context)
 {
     context.Add(expr.Name);
     base.Visit(expr, context);
 }
 internal AbsExpression(string argName, ILambdaExpression body)
 {
     this.argName = argName;
     this.body    = body;
 }
 public virtual void VisitLambdaExpression(ILambdaExpression value)
 {
     this.VisitVariableDeclarationCollection(value.Parameters);
     this.VisitExpression(value.Body);
 }
Esempio n. 8
0
 public override void Visit(ILambdaExpression inv, ITypeShape context)
 {
     // stop here for now!
 }
 public override void Visit(ILambdaExpression inv, IList <Event> events)
 {
     // stop here for now!
 }
Esempio n. 10
0
        public static FractalGraph CalcNewton(double rCenter, double iCenter, double rDelta, Complex R,
                                              ILambdaExpression f, ScriptNode fDef, Variables Variables, int N, int Width, int Height,
                                              ScriptNode Node, FractalZoomScript FractalZoomScript, object State)
        {
            double        RRe             = R.Real;
            double        RIm             = R.Imaginary;
            List <double> AttractorsR     = new List <double>();
            List <double> AttractorsI     = new List <double>();
            List <int>    AttractorColors = new List <int>();

            double[] AttractorsR2 = new double[0];
            double[] AttractorsI2 = new double[0];
            int[]    AttractorsColors2 = new int[0];
            double   r0, i0, r1, i1;
            double   dr, di;
            double   r, i;
            double   zr, zi;
            double   aspect;
            int      x, y, b, c, d;
            int      NrAttractors = 0;
            int      n;
            int      index;

            Variables v = new Variables();

            Variables.CopyTo(v);

            if (!(f is IDifferentiable Differentiable) ||
                !(Differentiable.Differentiate(Differentiable.DefaultVariableName, v) is ILambdaExpression fPrim))
            {
                throw new ScriptRuntimeException("Lambda expression not differentiable.", Node);
            }

            int    size  = Width * Height * 4;
            double Conv  = 1e-10;
            double Div   = 1e10;
            double Conv2 = Conv * 2;

            byte[] rgb = new byte[size];

            Complex[]  Row;
            Complex[]  Row2;
            Complex[]  Row3;
            int[]      Offset;
            IElement[] P = new IElement[1];
            int        j, x2;
            IElement   Obj, Obj2;
            double     Mod;
            Complex    z;

            rDelta *= 0.5;
            r0      = rCenter - rDelta;
            r1      = rCenter + rDelta;

            aspect = ((double)Width) / Height;

            i0 = iCenter - rDelta / aspect;
            i1 = iCenter + rDelta / aspect;

            dr = (r1 - r0) / Width;
            di = (i1 - i0) / Height;

            for (y = 0, i = i0, index = 0; y < Height; y++, i += di)
            {
                Row    = new Complex[Width];
                Offset = new int[Width];

                c = Width;
                for (x = 0, x2 = y * Width * 4, r = r0; x < Width; x++, r += dr, x2 += 4)
                {
                    Row[x]    = new Complex(r, i);
                    Offset[x] = x2;
                }

                n = 0;
                while (n < N && c > 0)
                {
                    n++;
                    P[0] = Expression.Encapsulate(Row);
                    Obj  = f.Evaluate(P, v);
                    Obj2 = fPrim.Evaluate(P, v);
                    Row2 = Obj.AssociatedObjectValue as Complex[];
                    Row3 = Obj2.AssociatedObjectValue as Complex[];

                    if (Row2 == null || Row3 == null)
                    {
                        throw new ScriptRuntimeException("Lambda expression (and its first derivative) must be able to accept complex vectors, " +
                                                         "and return complex vectors of equal length. Type returned: " +
                                                         Obj.GetType().FullName + " and " + Obj2.GetType().FullName, Node);
                    }
                    else if (Row2.Length != c || Row3.Length != c)
                    {
                        throw new ScriptRuntimeException("Lambda expression (and its first derivative) must be able to accept complex vectors, " +
                                                         "and return complex vectors of equal length. Length returned: " +
                                                         Row2.Length.ToString() + " and " + Row3.Length.ToString() +
                                                         ". Expected: " + c.ToString(), Node);
                    }

                    for (x = x2 = 0; x < c; x++)
                    {
                        j       = Offset[x];
                        z       = R * Row2[x] / Row3[x];
                        Row[x] -= z;

                        Mod = z.Magnitude;

                        if (Mod > Conv && Mod < Div)
                        {
                            if (x != x2)
                            {
                                Offset[x2] = j;
                            }

                            x2++;
                        }
                        else
                        {
                            if (n >= N)
                            {
                                rgb[j++] = 0;
                                rgb[j++] = 0;
                                rgb[j++] = 0;
                            }
                            else
                            {
                                zr = z.Real;
                                zi = z.Imaginary;

                                for (b = 0; b < NrAttractors; b++)
                                {
                                    if (Math.Abs(AttractorsR2[b] - zr) < Conv2 &&
                                        Math.Abs(AttractorsI2[b] - zi) < Conv2)
                                    {
                                        break;
                                    }
                                }

                                if (b == NrAttractors)
                                {
                                    AttractorsR.Add(zr);
                                    AttractorsI.Add(zi);

                                    int p1 = ~((b % 6) + 1);
                                    int p2 = ((b / 6) % 7);

                                    int Red   = (p1 & 1) != 0 ? 255 : 0;
                                    int Green = (p1 & 2) != 0 ? 255 : 0;
                                    int Blue  = (p1 & 4) != 0 ? 255 : 0;

                                    if ((p2 & 1) != 0)
                                    {
                                        Red >>= 1;
                                    }

                                    if ((p2 & 2) != 0)
                                    {
                                        Green >>= 1;
                                    }

                                    if ((p2 & 4) != 0)
                                    {
                                        Blue >>= 1;
                                    }

                                    Blue <<= 8;
                                    Blue  |= Green;
                                    Blue <<= 8;
                                    Blue  |= Red;

                                    AttractorColors.Add(Blue);

                                    AttractorsR2      = AttractorsR.ToArray();
                                    AttractorsI2      = AttractorsI.ToArray();
                                    AttractorsColors2 = AttractorColors.ToArray();

                                    NrAttractors++;
                                }

                                b = AttractorColors[b];

                                d            = (byte)b;
                                rgb[index++] = (byte)((d * (N - n + 1)) / N);
                                b          >>= 8;
                                d            = (byte)b;
                                rgb[index++] = (byte)((d * (N - n + 1)) / N);
                                b          >>= 8;
                                d            = (byte)b;
                                rgb[index++] = (byte)((d * (N - n + 1)) / N);
                            }

                            rgb[j++] = 255;
                        }
                    }

                    if (x2 < x)
                    {
                        Array.Resize <Complex>(ref Row, x2);
                        Array.Resize <int>(ref Offset, x2);
                        c = x2;
                    }
                }
            }

            using (SKData Data = SKData.Create(new MemoryStream(rgb)))
            {
                SKImage Bitmap = SKImage.FromPixelData(new SKImageInfo(Width, Height, SKColorType.Bgra8888), Data, Width * 4);
                return(new FractalGraph(Bitmap, r0, i0, r1, i1, rDelta * 2, true, Node, FractalZoomScript, State));
            }
        }
 public void Visit(ILambdaExpression expr, SSTPrintingContext c)
 {
     c.ParameterList(expr.Name.Parameters).Space().Text("=>");
     c.StatementBlock(expr.Body, this);
 }
            private void WriteLambdaExpression(ILambdaExpression value, IFormatter formatter)
            {
                formatter.WriteKeyword("function");
                formatter.Write("(");

                for (int i = 0; i < value.Parameters.Count; i++)
                {
                    if (i != 0)
                    {
                        formatter.Write(", ");
                    }

                    // this.WriteVariableIdentifier(value.Parameters[i].Variable.Identifier, formatter);
                    this.WriteDeclaration(value.Parameters[i].Name, formatter);
                }

                formatter.Write(")");

                formatter.Write(" { ");

                formatter.WriteKeyword("return");

                formatter.Write(" ");

                this.WriteExpression(value.Body, formatter);

                formatter.Write("; }");
            }
Esempio n. 13
0
        public override IElement Evaluate(IElement[] Arguments, Variables Variables)
        {
            double[]          Coefficients = null;
            Complex[]         CoefficientsZ = null;
            ILambdaExpression f = null;
            ScriptNode        fDef = null;
            double            rc, ic;
            double            dr;
            Complex           R;
            int     N;
            int     dimx, dimy;
            int     c = Arguments.Length;
            int     i = 0;
            object  Obj;
            Complex z;

            Obj = Arguments[i++].AssociatedObjectValue;
            if (Obj is Complex)
            {
                z  = (Complex)Obj;
                rc = z.Real;
                ic = z.Imaginary;
            }
            else
            {
                rc = Expression.ToDouble(Obj);
                ic = Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
            }

            if (i >= c)
            {
                throw new ScriptRuntimeException("Insufficient parameters in call to NewtonBasinFractal().", this);
            }

            dr = Expression.ToDouble(Arguments[i++].AssociatedObjectValue);

            if (i < c && (Arguments[i] is DoubleNumber || Arguments[i] is ComplexNumber))
            {
                R = Expression.ToComplex(Arguments[i++].AssociatedObjectValue);
            }
            else
            {
                R = Complex.One;

                if (i < c && this.Arguments[i] is null)
                {
                    i++;
                }
            }

            if (i < c)
            {
                if (Arguments[i] is DoubleVector)
                {
                    Coefficients = (double[])Arguments[i++].AssociatedObjectValue;
                }
                else if (Arguments[i] is ComplexVector)
                {
                    CoefficientsZ = (Complex[])Arguments[i++].AssociatedObjectValue;
                }

                /*else if (Parameters[i] is RealPolynomial)
                 *      Coefficients = ((RealPolynomial)Arguments[i++].AssociatedObjectValue).Coefficients;
                 * else if (Parameters[i] is ComplexPolynomial)
                 *      CoefficientsZ = ((ComplexPolynomial)Arguments[i++].AssociatedObjectValue).Coefficients;*/
                else if (Arguments[i] is IVector)
                {
                    IVector Vector = (IVector)Arguments[i++];
                    int     j, d = Vector.Dimension;

                    CoefficientsZ = new Complex[d];
                    for (j = 0; j < d; j++)
                    {
                        CoefficientsZ[j] = Expression.ToComplex(Vector.GetElement(j).AssociatedObjectValue);
                    }
                }
                else if (Arguments[i].AssociatedObjectValue is ILambdaExpression)
                {
                    f = (ILambdaExpression)Arguments[i];
                    if (f.NrArguments != 1)
                    {
                        throw new ScriptRuntimeException("Lambda expression in calls to NewtonBasinFractal() must be of one variable.", this);
                    }

                    fDef = this.Arguments[i++];
                }
                else
                {
                    throw new ScriptRuntimeException("Parameter " + (i + 1).ToString() +
                                                     " in call to NewtonBasinFractal has to be a vector of numbers, containing coefficients " +
                                                     "of the polynomial to use. Now it was of type " + Arguments[i].GetType().FullName,
                                                     this);
                }
            }
            else
            {
                throw new ScriptRuntimeException("Missing coefficients or lambda expression.", this);
            }

            if (i < c)
            {
                N = (int)Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
            }
            else
            {
                N = 32;
            }

            if (i < c)
            {
                dimx = (int)Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
            }
            else
            {
                dimx = 320;
            }

            if (i < c)
            {
                dimy = (int)Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
            }
            else
            {
                dimy = 200;
            }

            if (i < c)
            {
                throw new ScriptRuntimeException("Parameter mismatch in call to NewtonBasinFractal(r,c,dr,Coefficients[,Palette][,dimx[,dimy]]).",
                                                 this);
            }

            if (dimx <= 0 || dimx > 5000 || dimy <= 0 || dimy > 5000)
            {
                throw new ScriptRuntimeException("Image size must be within 1x1 to 5000x5000", this);
            }

            if (f != null)
            {
                return(CalcNewton(rc, ic, dr, R, f, fDef, Variables, N, dimx, dimy,
                                  this, this.FractalZoomScript,
                                  new object[] { dimx, dimy, N, R, fDef }));
            }
            else if (CoefficientsZ != null)
            {
                return(CalcNewton(rc, ic, dr, R, CoefficientsZ, N, dimx, dimy,
                                  this, this.FractalZoomScript,
                                  new object[] { dimx, dimy, N, R, CoefficientsZ }));
            }
            else
            {
                return(CalcNewton(rc, ic, dr, R, Coefficients, N, dimx, dimy,
                                  this, this.FractalZoomScript,
                                  new object[] { dimx, dimy, N, R, Coefficients }));
            }
        }
Esempio n. 14
0
        public static FractalGraph CalcHalley(double rCenter, double iCenter, double rDelta, Complex R,
                                              ILambdaExpression f, ScriptNode fDef, Variables Variables, SKColor[] Palette, int Width, int Height,
                                              ScriptNode Node, FractalZoomScript FractalZoomScript, object State)
        {
            double RRe = R.Real;
            double RIm = R.Imaginary;
            double r0, i0, r1, i1;
            double dr, di;
            double r, i;
            double aspect;
            int    x, y;
            int    n, N;

            N = Palette.Length;

            Variables v = new Variables();

            Variables.CopyTo(v);

            string ParameterName;

            if (!(f is IDifferentiable Differentiable) ||
                !(Differentiable.Differentiate(ParameterName = Differentiable.DefaultVariableName, v) is ILambdaExpression fPrim))
            {
                throw new ScriptRuntimeException("Lambda expression not differentiable.", Node);
            }

            if (!(fPrim is IDifferentiable Differentiable2) ||
                !(Differentiable2.Differentiate(ParameterName, v) is ILambdaExpression fBis))
            {
                throw new ScriptRuntimeException("Lambda expression not twice differentiable.", Node);
            }

            int size = Width * Height;

            double[] ColorIndex = new double[size];
            double   Conv       = 1e-10;
            double   Div        = 1e10;

            Complex[]  Row;
            Complex[]  Row2;
            Complex[]  Row3;
            Complex[]  Row4;
            int[]      Offset;
            IElement[] P = new IElement[1];
            int        j, c, x2;
            IElement   Obj, Obj2, Obj3;
            double     Mod;
            Complex    z, z2, z3;
            Complex    R2 = R * 2;

            rDelta *= 0.5;
            r0      = rCenter - rDelta;
            r1      = rCenter + rDelta;

            aspect = ((double)Width) / Height;

            i0 = iCenter - rDelta / aspect;
            i1 = iCenter + rDelta / aspect;

            dr = (r1 - r0) / Width;
            di = (i1 - i0) / Height;

            for (y = 0, i = i0; y < Height; y++, i += di)
            {
                Row    = new Complex[Width];
                Offset = new int[Width];

                c = Width;
                for (x = 0, x2 = y * Width, r = r0; x < Width; x++, r += dr, x2++)
                {
                    Row[x]    = new Complex(r, i);
                    Offset[x] = x2;
                }

                n = 0;
                while (n < N && c > 0)
                {
                    n++;
                    P[0] = Expression.Encapsulate(Row);
                    Obj  = f.Evaluate(P, v);
                    Obj2 = fPrim.Evaluate(P, v);
                    Obj3 = fBis.Evaluate(P, v);
                    Row2 = Obj.AssociatedObjectValue as Complex[];
                    Row3 = Obj2.AssociatedObjectValue as Complex[];
                    Row4 = Obj3.AssociatedObjectValue as Complex[];

                    if (Row2 == null || Row3 == null || Row4 == null)
                    {
                        throw new ScriptRuntimeException("Lambda expression (and its first and second derivative) must be able to accept complex vectors, " +
                                                         "and return complex vectors of equal length. Type returned: " +
                                                         Obj.GetType().FullName + ", " + Obj2.GetType().FullName + " and " + Obj3.GetType().FullName,
                                                         Node);
                    }
                    else if (Row2.Length != c || Row3.Length != c)
                    {
                        throw new ScriptRuntimeException("Lambda expression (and its first and second derivative) must be able to accept complex vectors, " +
                                                         "and return complex vectors of equal length. Length returned: " +
                                                         Row2.Length.ToString() + ", " + Row3.Length.ToString() + " and " + Row4.Length.ToString() +
                                                         ". Expected: " + c.ToString(), Node);
                    }

                    for (x = x2 = 0; x < c; x++)
                    {
                        j  = Offset[x];
                        z  = Row2[x];
                        z2 = Row3[x];
                        z3 = Row4[x];

                        z       = R2 * z * z2 / (2 * z2 * z2 - z * z3);
                        Row[x] -= z;

                        Mod = z.Magnitude;

                        if (Mod > Conv && Mod < Div)
                        {
                            if (x != x2)
                            {
                                Offset[x2] = j;
                            }

                            x2++;
                        }
                        else
                        {
                            if (n >= N)
                            {
                                ColorIndex[j++] = N;
                            }
                            else
                            {
                                ColorIndex[j++] = n;
                            }
                        }
                    }

                    if (x2 < x)
                    {
                        Array.Resize <Complex>(ref Row, x2);
                        Array.Resize <int>(ref Offset, x2);
                        c = x2;
                    }
                }
            }

            Node.Expression.Preview(new GraphBitmap(FractalGraph.ToBitmap(ColorIndex, Width, Height, Palette)));

            double[] Boundary = FractalGraph.FindBoundaries(ColorIndex, Width, Height);
            FractalGraph.Smooth(ColorIndex, Boundary, Width, Height, N, Palette, Node, Variables);

            return(new FractalGraph(FractalGraph.ToBitmap(ColorIndex, Width, Height, Palette),
                                    r0, i0, r1, i1, rDelta * 2, true, Node, FractalZoomScript, State));
        }
Esempio n. 15
0
 /// <summary>
 /// Orders elements based logic defined in a lambda expression.
 /// </summary>
 /// <param name="Lambda">Lambda expression-</param>
 /// <param name="Variables">Variables to use during evaluation.</param>
 public LambdaOrder(ILambdaExpression Lambda, Variables Variables)
 {
     this.lambda    = Lambda;
     this.arguments = new IElement[2];
     this.variables = Variables;
 }
    private void MergeInvocations([NotNull] IInvocationExpression outerInvocation)
    {
      var outerInvocationArgument = outerInvocation.Arguments[0];

      var outerLambda = outerInvocationArgument.Value as ILambdaExpression;
      if (outerLambda == null)
      {
        var methodGroup = (IReferenceExpression)outerInvocationArgument.Value;

        outerLambda = CreateLambdaFromMethodGroup(methodGroup, outerInvocation.InvokedExpression);
      }

      var lambdas = new ILambdaExpression[myCallsCount];
      lambdas[myCallsCount - 1] = outerLambda;

      var parameter = outerLambda.ParameterDeclarations[0];

      var currentInvocation = outerInvocation;

      for (int i = myCallsCount - 2; i >= 0; i--)
      {
        currentInvocation = currentInvocation.GetInnerInvocation().NotNull();

        lambdas[i] = ExtractLambda(currentInvocation, parameter.NameIdentifier);
      }

      var innerMostInvokedExpression = currentInvocation.InvokedExpression;

      var accumulatorLambda = lambdas[0];

      for (int i = 1; i < lambdas.Length; i++)
      {
        Merge(accumulatorLambda, lambdas[i]);
      }

      outerInvocationArgument.SetValue(accumulatorLambda);
      outerInvocation.SetInvokedExpression(innerMostInvokedExpression);
    }
Esempio n. 17
0
 public SampleFix(ILambdaExpression lambdaExpression)
 {
     _lambdaExpression = lambdaExpression;
 }
 public override void VisitLambdaExpression(ILambdaExpression operation)
 {
     // for now there doesn't seem to be any way to annotate lambdas with attributes
 }
Esempio n. 19
0
        public override void VisitLambdaExpression(ILambdaExpression operation)
        {
            var signature = operation.Signature;

            base.VisitLambdaExpression(operation);
        }
 public static ILambdaValue Interpret(ILambdaExpression e)
 {
     return(e.Eval(new Dictionary <string, ILambdaValue>()));
 }
Esempio n. 21
0
        public static FractalGraph CalcHalley(double rCenter, double iCenter, double rDelta, Complex R,
                                              ILambdaExpression f, ScriptNode fDef, Variables Variables, SKColor[] Palette, int Width, int Height,
                                              ScriptNode Node, FractalZoomScript FractalZoomScript, object State)
        {
            double RRe = R.Real;
            double RIm = R.Imaginary;
            double r0, i0, r1, i1;
            double dr, di;
            double r, i;
            double aspect;
            int    x, y;
            int    n, N;

            N = Palette.Length;

            Variables v = new Variables();

            Variables.CopyTo(v);

            string ParameterName;

            if (!(f is IDifferentiable Differentiable) ||
                !(Differentiable.Differentiate(ParameterName = Differentiable.DefaultVariableName, v) is ILambdaExpression fPrim))
            {
                throw new ScriptRuntimeException("Lambda expression not differentiable.", Node);
            }

            if (!(fPrim is IDifferentiable Differentiable2) ||
                !(Differentiable2.Differentiate(ParameterName, v) is ILambdaExpression fBis))
            {
                throw new ScriptRuntimeException("Lambda expression not twice differentiable.", Node);
            }

            int size = Width * Height;

            int[]  ColorIndex = new int[size];
            double Conv       = 1e-10;
            double Div        = 1e10;

            Complex[]  Row;
            Complex[]  Row2;
            Complex[]  Row3;
            Complex[]  Row4;
            int[]      Offset;
            IElement[] P = new IElement[1];
            int        j, c, x2;
            IElement   Obj, Obj2, Obj3;
            double     Mod;
            Complex    z, z2, z3;
            Complex    R2 = R * 2;

            rDelta *= 0.5;
            r0      = rCenter - rDelta;
            r1      = rCenter + rDelta;

            aspect = ((double)Width) / Height;

            i0 = iCenter - rDelta / aspect;
            i1 = iCenter + rDelta / aspect;

            dr = (r1 - r0) / Width;
            di = (i1 - i0) / Height;

            for (y = 0, i = i0; y < Height; y++, i += di)
            {
                Row    = new Complex[Width];
                Offset = new int[Width];

                c = Width;
                for (x = 0, x2 = y * Width, r = r0; x < Width; x++, r += dr, x2++)
                {
                    Row[x]    = new Complex(r, i);
                    Offset[x] = x2;
                }

                n = 0;
                while (n < N && c > 0)
                {
                    n++;
                    P[0] = Expression.Encapsulate(Row);
                    Obj  = f.Evaluate(P, v);
                    Obj2 = fPrim.Evaluate(P, v);
                    Obj3 = fBis.Evaluate(P, v);
                    Row2 = Obj.AssociatedObjectValue as Complex[];
                    Row3 = Obj2.AssociatedObjectValue as Complex[];
                    Row4 = Obj3.AssociatedObjectValue as Complex[];

                    if (Row2 is null || Row3 is null || Row4 is null)
                    {
                        throw new ScriptRuntimeException("Lambda expression (and its first and second derivative) must be able to accept complex vectors, " +
                                                         "and return complex vectors of equal length. Type returned: " +
                                                         Obj.GetType().FullName + ", " + Obj2.GetType().FullName + " and " + Obj3.GetType().FullName,
                                                         Node);
                    }
 internal ClosureValue(string argName, ILambdaExpression body, Dictionary <string, ILambdaValue> env)
 {
     this.argName = argName;
     this.body    = body;
     this.env     = env;
 }
Esempio n. 23
0
        public static FractalGraph CalcHalley(double rCenter, double iCenter, double rDelta, Complex R,
                                              ILambdaExpression f, ScriptNode fDef, Variables Variables, SKColor[] Palette, int Width, int Height,
                                              ScriptNode Node, FractalZoomScript FractalZoomScript, object State)
        {
            byte[]  reds;
            byte[]  greens;
            byte[]  blues;
            double  RRe = R.Real;
            double  RIm = R.Imaginary;
            double  r0, i0, r1, i1;
            double  dr, di;
            double  r, i;
            double  aspect;
            int     x, y;
            int     n, N;
            SKColor cl;

            N      = Palette.Length;
            reds   = new byte[N];
            greens = new byte[N];
            blues  = new byte[N];

            for (x = 0; x < N; x++)
            {
                cl        = Palette[x];
                reds[x]   = cl.Red;
                greens[x] = cl.Green;
                blues[x]  = cl.Blue;
            }

            Variables v = new Variables();

            Variables.CopyTo(v);

            string ParameterName;

            if (!(f is IDifferentiable Differentiable) ||
                !(Differentiable.Differentiate(ParameterName = Differentiable.DefaultVariableName, v) is ILambdaExpression fPrim))
            {
                throw new ScriptRuntimeException("Lambda expression not differentiable.", Node);
            }

            if (!(fPrim is IDifferentiable Differentiable2) ||
                !(Differentiable2.Differentiate(ParameterName, v) is ILambdaExpression fBis))
            {
                throw new ScriptRuntimeException("Lambda expression not twice differentiable.", Node);
            }

            int    size = Width * Height * 4;
            double Conv = 1e-10;
            double Div  = 1e10;

            byte[] rgb = new byte[size];

            Complex[]  Row;
            Complex[]  Row2;
            Complex[]  Row3;
            Complex[]  Row4;
            int[]      Offset;
            IElement[] P = new IElement[1];
            int        j, c, x2;
            IElement   Obj, Obj2, Obj3;
            double     Mod;
            Complex    z, z2, z3;
            Complex    R2 = R * 2;

            rDelta *= 0.5;
            r0      = rCenter - rDelta;
            r1      = rCenter + rDelta;

            aspect = ((double)Width) / Height;

            i0 = iCenter - rDelta / aspect;
            i1 = iCenter + rDelta / aspect;

            dr = (r1 - r0) / Width;
            di = (i1 - i0) / Height;

            for (y = 0, i = i0; y < Height; y++, i += di)
            {
                Row    = new Complex[Width];
                Offset = new int[Width];

                c = Width;
                for (x = 0, x2 = y * Width * 4, r = r0; x < Width; x++, r += dr, x2 += 4)
                {
                    Row[x]    = new Complex(r, i);
                    Offset[x] = x2;
                }

                n = 0;
                while (n < N && c > 0)
                {
                    n++;
                    P[0] = Expression.Encapsulate(Row);
                    Obj  = f.Evaluate(P, v);
                    Obj2 = fPrim.Evaluate(P, v);
                    Obj3 = fBis.Evaluate(P, v);
                    Row2 = Obj.AssociatedObjectValue as Complex[];
                    Row3 = Obj2.AssociatedObjectValue as Complex[];
                    Row4 = Obj3.AssociatedObjectValue as Complex[];

                    if (Row2 == null || Row3 == null || Row4 == null)
                    {
                        throw new ScriptRuntimeException("Lambda expression (and its first and second derivative) must be able to accept complex vectors, " +
                                                         "and return complex vectors of equal length. Type returned: " +
                                                         Obj.GetType().FullName + ", " + Obj2.GetType().FullName + " and " + Obj3.GetType().FullName,
                                                         Node);
                    }
                    else if (Row2.Length != c || Row3.Length != c)
                    {
                        throw new ScriptRuntimeException("Lambda expression (and its first and second derivative) must be able to accept complex vectors, " +
                                                         "and return complex vectors of equal length. Length returned: " +
                                                         Row2.Length.ToString() + ", " + Row3.Length.ToString() + " and " + Row4.Length.ToString() +
                                                         ". Expected: " + c.ToString(), Node);
                    }

                    for (x = x2 = 0; x < c; x++)
                    {
                        j  = Offset[x];
                        z  = Row2[x];
                        z2 = Row3[x];
                        z3 = Row4[x];

                        z       = R2 * z * z2 / (2 * z2 * z2 - z * z3);
                        Row[x] -= z;

                        Mod = z.Magnitude;

                        if (Mod > Conv && Mod < Div)
                        {
                            if (x != x2)
                            {
                                Offset[x2] = j;
                            }

                            x2++;
                        }
                        else
                        {
                            if (n >= N)
                            {
                                rgb[j++] = 0;
                                rgb[j++] = 0;
                                rgb[j++] = 0;
                            }
                            else
                            {
                                rgb[j++] = blues[n];
                                rgb[j++] = greens[n];
                                rgb[j++] = reds[n];
                            }

                            rgb[j++] = 255;
                        }
                    }

                    if (x2 < x)
                    {
                        Array.Resize <Complex>(ref Row, x2);
                        Array.Resize <int>(ref Offset, x2);
                        c = x2;
                    }
                }
            }

            using (SKData Data = SKData.Create(new MemoryStream(rgb)))
            {
                SKImage Bitmap = SKImage.FromPixelData(new SKImageInfo(Width, Height, SKColorType.Bgra8888), Data, Width * 4);
                return(new FractalGraph(Bitmap, r0, i0, r1, i1, rDelta * 2, true, Node, FractalZoomScript, State));
            }
        }
Esempio n. 24
0
 public virtual void VisitLambdaExpression(ILambdaExpression operation)
 {
     DefaultVisit(operation);
 }
 public ApplyExpression(ILambdaExpression f, ILambdaExpression arg)
 {
     this.f   = f;
     this.arg = arg;
 }
Esempio n. 26
0
 public override void VisitLambdaExpression(ILambdaExpression operation)
 {
     // for now there doesn't seem to be any way to annotate lambdas with attributes
 }
 internal AddExpression(ILambdaExpression left, ILambdaExpression right)
 {
     this.left  = left;
     this.right = right;
 }
 public SampleHighlighting(ILambdaExpression lambdaExpression)
 {
     LambdaExpression = lambdaExpression;
 }
 public IfExpression(ILambdaExpression cond, ILambdaExpression t, ILambdaExpression f)
 {
     condition  = cond;
     trueValue  = t;
     falseValue = f;
 }
Esempio n. 30
0
 public override void VisitLambdaExpression(ILambdaExpression operation)
 {
     Visit(operation.Body);
 }
 public static ILambdaExpression AbsExp(string argName, ILambdaExpression body)
 {
     return(new AbsExpression(argName, body));
 }
Esempio n. 32
0
        public override IElement Evaluate(IElement[] Arguments, Variables Variables)
        {
            string ColorExpression = null;

            SKColor[]         Palette;
            double[]          Coefficients = null;
            Complex[]         CoefficientsZ = null;
            ILambdaExpression f = null;
            ScriptNode        fDef = null;
            double            rc, ic;
            double            dr;
            Complex           R;
            int     dimx, dimy;
            int     c = Arguments.Length;
            int     i = 0;
            object  Obj;
            Complex z;

            Obj = Arguments[i++].AssociatedObjectValue;
            if (Obj is Complex)
            {
                z  = (Complex)Obj;
                rc = z.Real;
                ic = z.Imaginary;
            }
            else
            {
                rc = Expression.ToDouble(Obj);
                ic = Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
            }

            if (i >= c)
            {
                throw new ScriptRuntimeException("Insufficient parameters in call to HalleySmoothFractal().", this);
            }

            dr = Expression.ToDouble(Arguments[i++].AssociatedObjectValue);

            if (i < c && (Arguments[i] is DoubleNumber || Arguments[i] is ComplexNumber))
            {
                R = Expression.ToComplex(Arguments[i++].AssociatedObjectValue);
            }
            else
            {
                R = Complex.One;

                if (i < c && this.Arguments[i] == null)
                {
                    i++;
                }
            }

            if (i < c)
            {
                if (Arguments[i] is DoubleVector)
                {
                    Coefficients = (double[])Arguments[i++].AssociatedObjectValue;
                }
                else if (Arguments[i] is ComplexVector)
                {
                    CoefficientsZ = (Complex[])Arguments[i++].AssociatedObjectValue;
                }

                /*else if (Parameters[i] is RealPolynomial)
                 *      Coefficients = ((RealPolynomial)Arguments[i++].AssociatedObjectValue).Coefficients;
                 * else if (Parameters[i] is ComplexPolynomial)
                 *      CoefficientsZ = ((ComplexPolynomial)Arguments[i++].AssociatedObjectValue).Coefficients;*/
                else if (Arguments[i] is IVector)
                {
                    IVector Vector = (IVector)Arguments[i++];
                    int     j, d = Vector.Dimension;

                    CoefficientsZ = new Complex[d];
                    for (j = 0; j < d; j++)
                    {
                        CoefficientsZ[j] = Expression.ToComplex(Vector.GetElement(j).AssociatedObjectValue);
                    }
                }
                else if (Arguments[i].AssociatedObjectValue is ILambdaExpression)
                {
                    f = (ILambdaExpression)Arguments[i];
                    if (f.NrArguments != 1)
                    {
                        throw new ScriptRuntimeException("Lambda expression in calls to HalleySmoothFractal() must be of one variable.", this);
                    }

                    fDef = this.Arguments[i++];
                }
                else
                {
                    throw new ScriptRuntimeException("Parameter " + (i + 1).ToString() +
                                                     " in call to HalleySmoothFractal has to be a vector of numbers, containing coefficients " +
                                                     "of the polynomial to use. Now it was of type " + Arguments[i].GetType().FullName,
                                                     this);
                }
            }
            else
            {
                throw new ScriptRuntimeException("Missing coefficients or lambda expression.", this);
            }

            if (i < c && this.Arguments[i] != null && Arguments[i] is ObjectVector)
            {
                ColorExpression = this.Arguments[i].SubExpression;
                Palette         = FractalGraph.ToPalette((ObjectVector)Arguments[i++]);
            }
            else
            {
                Palette         = ColorModels.RandomLinearAnalogousHSL.CreatePalette(128, 4, out int Seed, this, Variables);
                ColorExpression = "RandomLinearAnalogousHSL(128,4," + Seed.ToString() + ")";

                if (i < c && this.Arguments[i] == null)
                {
                    i++;
                }
            }

            if (i < c)
            {
                dimx = (int)Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
            }
            else
            {
                dimx = 320;
            }

            if (i < c)
            {
                dimy = (int)Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
            }
            else
            {
                dimy = 200;
            }

            if (i < c)
            {
                throw new ScriptRuntimeException("Parameter mismatch in call to HalleySmoothFractal(z,dr[,R][,Coefficients][,Palette][,dimx[,dimy]]).",
                                                 this);
            }

            if (dimx <= 0 || dimx > 5000 || dimy <= 0 || dimy > 5000)
            {
                throw new ScriptRuntimeException("Image size must be within 1x1 to 5000x5000", this);
            }

            if (f != null)
            {
                return(CalcHalley(rc, ic, dr, R, f, fDef, Variables, Palette, dimx, dimy,
                                  this, this.FractalZoomScript,
                                  new object[] { Palette, dimx, dimy, R, fDef, ColorExpression }));
            }
            else if (CoefficientsZ != null)
            {
                return(CalcHalley(rc, ic, dr, R, CoefficientsZ, Palette, dimx, dimy,
                                  this, Variables, this.FractalZoomScript,
                                  new object[] { Palette, dimx, dimy, R, CoefficientsZ, ColorExpression }));
            }
            else
            {
                return(CalcHalley(rc, ic, dr, R, Coefficients, Palette, dimx, dimy,
                                  this, Variables, this.FractalZoomScript,
                                  new object[] { Palette, dimx, dimy, R, Coefficients, ColorExpression }));
            }
        }
 public static ILambdaExpression ApplyExp(ILambdaExpression f, ILambdaExpression arg)
 {
     return(new ApplyExpression(f, arg));
 }
Esempio n. 34
0
        public override void VisitLambdaExpression(ILambdaExpression operation)
        {
            var signature = operation.Signature;

            base.VisitLambdaExpression(operation);
        }
 public static ILambdaExpression AddExp(ILambdaExpression left, ILambdaExpression right)
 {
     return(new AddExpression(left, right));
 }
 protected override void Merge(ILambdaExpression accumulatorLambda, ILambdaExpression lambda)
 {
   var newBody = Factory.CreateExpression("$0 && $1", accumulatorLambda.BodyExpression, lambda.BodyExpression);
   accumulatorLambda.SetBodyExpression(newBody);
 }
 public static ILambdaExpression IfExp(ILambdaExpression cond, ILambdaExpression t, ILambdaExpression f)
 {
     return(new IfExpression(cond, t, f));
 }
Esempio n. 38
0
 public virtual void VisitLambdaExpression(ILambdaExpression operation)
 {
     DefaultVisit(operation);
 }
 public virtual IExpression TransformLambdaExpression(ILambdaExpression value)
 {
     this.InisituTransformVariableDeclarationCollection(value.Parameters);
     value.Body = this.TransformExpression(value.Body);
     return value;
 }