Example #1
0
        private bool Chk_Device(ref string szRet)
        {
            ProcessLayer pl       = new ProcessLayer();
            bool         bSuccess = pl.SyncStart("nfc/nfc-list.exe", null);

            if (!bSuccess)
            {
                szRet = "错误!无法连接到设备";
                return(false);
            }
            string[] RegexRet = RegexStringLayer.GetSubString(pl.m_RetOutputMsg.ToString(), "NFC device:\\s*(.+)\\s*/");

            bool bRet;

            if (string.IsNullOrWhiteSpace(RegexRet[0]))
            {
                bRet  = false;
                szRet = "错误!无法连接到设备";
            }
            else
            {
                bRet  = true;
                szRet = RegexRet[0];
                szDevice.Append(RegexRet[0]);
            }

            return(bRet);
        }
Example #2
0
 public Main(string szDevice)
 {
     CheckForIllegalCrossThreadCalls = false;
     InitializeComponent();
     Device.Text = "Device Info: " + szDevice;
     pl          = new ProcessLayer(ProcStatus, txt_Log);
 }
 public ICalculator GenFractalCalc(List<ProcessLayer> LayerData, FractalType fractaltype, string code, ProcessLayer deflayer)
 {
     List<ICalculator> c = new List<ICalculator>();
       for (int i = 0; i < _processes; i++)
       {
     c.Add(_factory.GenFractalCalc(LayerData, fractaltype, code, deflayer));
       }
       return new ThreadCalculator(c.ToArray());
 }
 public ICalculator GenFractalCalc(List<ProcessLayer> LayerData, FractalType fractaltype, string code, ProcessLayer deflayer)
 {
     BinaryFormatter bin = new BinaryFormatter();
       foreach (var client in clients) {
     var s = client.GetStream();
     BinaryWriter bw = new BinaryWriter(s);
     bw.Write(1);
     bin.Serialize(s, LayerData);
     bin.Serialize(s, fractaltype);
     bin.Serialize(s, code);
     for (int i = 0; i < LayerData.Count; i++)
     {
       if (LayerData[i]==deflayer) bin.Serialize(s, i);
     }
       }
       return new DistributedCalculator(clients);
 }
 public SimpleCalculator(FractalType fractaltype, string code, ProcessLayer deflayer)
 {
     this.fractaltype = fractaltype;
     if (fractaltype == FractalType.FRACTAL_TYPE_CONVERGENT || fractaltype == FractalType.FRACTAL_TYPE_DIVERGENT)
     {
       Microsoft.CSharp.CSharpCodeProvider cp = new Microsoft.CSharp.CSharpCodeProvider();
       System.CodeDom.Compiler.CompilerParameters cpar = new System.CodeDom.Compiler.CompilerParameters();
       cpar.GenerateInMemory = true;
       cpar.GenerateExecutable = false;
       cpar.ReferencedAssemblies.Add("system.dll");
       cpar.ReferencedAssemblies.Add(System.Reflection.Assembly.GetExecutingAssembly().Location);
       cpar.ReferencedAssemblies.Add("system.core.dll");
       cpar.ReferencedAssemblies.Add("system.numerics.dll");
       string src = "using System;using System.Numerics;using RestFract;using RestFract.Generators;class evalclass:IComplexEval {" +
            "public evalclass(){} public Complex eval(Complex x, Complex c, double n, double p)" +
            "{ return " + code + "; } }";
       System.CodeDom.Compiler.CompilerResults cr = cp.CompileAssemblyFromSource(cpar, src);
       if (cr.Errors.Count == 0 && cr.CompiledAssembly != null)
       {
     Type ObjType = cr.CompiledAssembly.GetType("evalclass");
     try
     {
       if (ObjType != null)
       {
     this.code = (IComplexEval)Activator.CreateInstance(ObjType);
       }
     }
     catch (Exception e)
     {
       this.code = null;
       throw new NotImplementedException("Could not compile code",e);
     }
       }
       else
       {
     this.code = null;
     throw new NotImplementedException("Could not compile code: "+cr.Errors[0]);
       }
     }
     this.deflayer = deflayer;
 }
        public ICalculator GenFractalCalc(List<ProcessLayer> LayerData, FractalType fractaltype, string code, ProcessLayer deflayer)
        {
            bool hastriangle = false;
              bool fractdiv = true;
              SeqType modesused = 0;

              foreach (var it in LayerData)
              {
            if (it.c_checktype.HasFlag(SeqCheck.MPL_CHECK_TRIANGLE)) hastriangle = true;
            if (it.c_checktype.HasFlag(SeqCheck.MPL_CHECK_TRIANGLE_SMOOTH)) hastriangle = true;
            modesused |= it.c_seqtype;
              }

              if (modesused.HasFlag(SeqType.MPL_SEQ_STDDEV)) modesused |= SeqType.MPL_SEQ_VARIANCE;
              if (modesused.HasFlag(SeqType.MPL_SEQ_VARIANCE)) modesused |= SeqType.MPL_SEQ_VARSX;
              if (modesused.HasFlag(SeqType.MPL_SEQ_VARSX)) modesused |= SeqType.MPL_SEQ_MEAN;

              CodeNamespace ns = new CodeNamespace();
              CodeTypeDeclaration cl = new CodeTypeDeclaration("FractRunner");
              cl.IsClass = true;
              cl.BaseTypes.Add(typeof(IPreCompiledCalculator));
              cl.TypeAttributes = System.Reflection.TypeAttributes.Public;
              ns.Types.Add(cl);

              CodeMemberMethod calc = new CodeMemberMethod();
              calc.Attributes = MemberAttributes.Public;
              calc.ReturnType = new CodeTypeReference(typeof(void));
              calc.Name = "CalcFractal";
              calc.Parameters.Add(new CodeParameterDeclarationExpression(typeof(Complex), "x"));
              calc.Parameters.Add(new CodeParameterDeclarationExpression(typeof(Complex), "c"));
              calc.Parameters.Add(new CodeParameterDeclarationExpression(typeof(List<ProcessLayer>), "ld"));
              calc.Parameters.Add(new CodeParameterDeclarationExpression(typeof(double), "pr"));
              cl.Members.Add(calc);

              calc.Statements.Add(new CodeVariableDeclarationStatement(typeof(Complex), "sumx", new CodePrimitiveExpression(0)));
              calc.Statements.Add(new CodeVariableDeclarationStatement(typeof(Complex), "meanx", new CodePrimitiveExpression(0)));
              calc.Statements.Add(new CodeVariableDeclarationStatement(typeof(Complex), "varsx", new CodePrimitiveExpression(0)));
              calc.Statements.Add(new CodeVariableDeclarationStatement(typeof(Complex), "variacex", new CodePrimitiveExpression(0)));
              calc.Statements.Add(new CodeVariableDeclarationStatement(typeof(Complex), "sdx", new CodePrimitiveExpression(0)));
              calc.Statements.Add(new CodeVariableDeclarationStatement(typeof(Complex), "minx", new CodePrimitiveExpression(0)));
              calc.Statements.Add(new CodeVariableDeclarationStatement(typeof(Complex), "maxx", new CodePrimitiveExpression(0)));
              calc.Statements.Add(new CodeVariableDeclarationStatement(typeof(Complex), "deltax", new CodePrimitiveExpression(0)));

              calc.Statements.Add(new CodeVariableDeclarationStatement(typeof(Complex), "deltac", new CodePrimitiveExpression(0)));

              calc.Statements.Add(new CodeVariableDeclarationStatement(typeof(double), "delta", new CodePrimitiveExpression(0)));
              calc.Statements.Add(new CodeVariableDeclarationStatement(typeof(double), "newxnorm", new CodePrimitiveExpression(0)));
              calc.Statements.Add(new CodeVariableDeclarationStatement(typeof(double), "lowbound", new CodePrimitiveExpression(0)));
              calc.Statements.Add(new CodeVariableDeclarationStatement(typeof(double), "newd", new CodePrimitiveExpression(0)));

              if (hastriangle)
              {
            if (fractaltype == FractalType.FRACTAL_TYPE_MANDEL)
            {
              // trinorm = c.Magnitude;
              calc.Statements.Add(new CodeVariableDeclarationStatement(typeof(double), "trinorm",
              new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("c"), "Magnitude")
              ));
            }
            else
            {
              // trinorm = c.Norm;
              calc.Statements.Add(new CodeVariableDeclarationStatement(typeof(double), "trinorm",
            new CodeBinaryOperatorExpression(new CodeBinaryOperatorExpression(new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("c"), "Real"), CodeBinaryOperatorType.Multiply, new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("c"), "Real")), CodeBinaryOperatorType.Add, new CodeBinaryOperatorExpression(new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("c"), "Imaginary"), CodeBinaryOperatorType.Multiply, new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("c"), "Imaginary")))
              ));
            }
              }

              calc.Statements.Add(new CodeVariableDeclarationStatement(typeof(bool), "end", new CodePrimitiveExpression(false)));
              calc.Statements.Add(new CodeVariableDeclarationStatement(typeof(int), "n", new CodePrimitiveExpression(0)));
              calc.Statements.Add(new CodeVariableDeclarationStatement(typeof(Complex), "newx", new CodeArgumentReferenceExpression("x")));

              // while (!end)
              CodeIterationStatement itr = new CodeIterationStatement(new CodeSnippetStatement(), new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("end"), CodeBinaryOperatorType.ValueEquality, new CodePrimitiveExpression(false)), new CodeSnippetStatement());
              calc.Statements.Add(itr);

              // n++;
              itr.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("n"), new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("n"), CodeBinaryOperatorType.Add, new CodePrimitiveExpression(1))));

              switch (fractaltype)
              {
            case FractalType.FRACTAL_TYPE_MANDEL:
              itr.Statements.Add(
            new CodeAssignStatement(
              new CodeVariableReferenceExpression("newx"),
              new CodeObjectCreateExpression(typeof(Complex),
                new CodeBinaryOperatorExpression(
                  new CodeBinaryOperatorExpression(
                    new CodeBinaryOperatorExpression(new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("x"), "Real"), CodeBinaryOperatorType.Multiply, new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("x"), "Real")),
                    CodeBinaryOperatorType.Subtract,
                    new CodeBinaryOperatorExpression(new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("x"), "Imaginary"), CodeBinaryOperatorType.Multiply, new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("x"), "Imaginary"))),
                  CodeBinaryOperatorType.Add,
                  new CodePropertyReferenceExpression(new CodeArgumentReferenceExpression("c"), "Real")
                ),
                new CodeBinaryOperatorExpression(
                  new CodeBinaryOperatorExpression(
                    new CodeBinaryOperatorExpression(new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("x"), "Real"), CodeBinaryOperatorType.Multiply, new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("x"), "Imaginary")),
                    CodeBinaryOperatorType.Multiply,
                    new CodePrimitiveExpression(2)
                    ),
                  CodeBinaryOperatorType.Add,
                  new CodePropertyReferenceExpression(new CodeArgumentReferenceExpression("c"), "Imaginary")
                  )
              )));
              //double sx = x.Real;
              //double sy = x.Imaginary;
              //return new Complex(sx * sx - sy * sy + c.Real, 2 * sx * sy + c.Imaginary);
              break;
            case FractalType.FRACTAL_TYPE_MANDEL_N:
              itr.Statements.Add(new CodeAssignStatement(
              new CodeVariableReferenceExpression("newx"),
              new CodeBinaryOperatorExpression(
                new CodeMethodInvokeExpression(
                  new CodeTypeReferenceExpression(typeof(Complex)),
                  "Pow",
                  new CodeArgumentReferenceExpression("x"),
                  new CodeArgumentReferenceExpression("pr")
                ),
                CodeBinaryOperatorType.Add,
                new CodeArgumentReferenceExpression("c")
                )
              ));
              // return Complex.Pow(x, param) + c;
              break;
            case FractalType.FRACTAL_TYPE_BURNINGSHIP:
              itr.Statements.Add(
            new CodeAssignStatement(
              new CodeVariableReferenceExpression("newx"),
              new CodeObjectCreateExpression(typeof(Complex),
                new CodeBinaryOperatorExpression(
                  new CodeBinaryOperatorExpression(
                    new CodeBinaryOperatorExpression(new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("x"), "Real"), CodeBinaryOperatorType.Multiply, new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("x"), "Real")),
                    CodeBinaryOperatorType.Subtract,
                    new CodeBinaryOperatorExpression(new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("x"), "Imaginary"), CodeBinaryOperatorType.Multiply, new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("x"), "Imaginary"))),
                  CodeBinaryOperatorType.Add,
                  new CodePropertyReferenceExpression(new CodeArgumentReferenceExpression("c"), "Real")
                ),
                new CodeBinaryOperatorExpression(
                  new CodeBinaryOperatorExpression(
                    new CodeMethodInvokeExpression(
                      new CodeTypeReferenceExpression(typeof(Math)),
                      "Abs",
                      new CodeBinaryOperatorExpression(new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("x"), "Real"), CodeBinaryOperatorType.Multiply, new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("x"), "Imaginary"))
                    ),
                    CodeBinaryOperatorType.Multiply,
                    new CodePrimitiveExpression(2)
                    ),
                  CodeBinaryOperatorType.Add,
                  new CodePropertyReferenceExpression(new CodeArgumentReferenceExpression("c"), "Imaginary")
                  )
              )));
              //  double sx = x.Real;
              //  double sy = x.Imaginary;
              //  return new Complex(sx * sx - sy * sy + c.Real, 2 * absval(sx * sy) + c.Imaginary);
              break;
            case FractalType.FRACTAL_TYPE_BURNINGSHIP_N:
              itr.Statements.Add(new CodeAssignStatement(
              new CodeVariableReferenceExpression("newx"),
              new CodeBinaryOperatorExpression(
                new CodeMethodInvokeExpression(
                  new CodeTypeReferenceExpression(typeof(Complex)),
                  "Pow",
                  new CodeObjectCreateExpression(typeof(Complex),
                    new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Math)), "Abs", new CodePropertyReferenceExpression(new CodeArgumentReferenceExpression("x"), "Real")),
                    new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Math)), "Abs", new CodePropertyReferenceExpression(new CodeArgumentReferenceExpression("x"), "Imaginary"))
                  ),
                  new CodeArgumentReferenceExpression("pr")
                ),
                CodeBinaryOperatorType.Add,
                new CodeArgumentReferenceExpression("c")
                )
              ));
              // return Complex.Pow(new Complex(absval(x.Real), absval(x.Imaginary)), n) + c;
              break;
            case FractalType.FRACTAL_TYPE_DIVERGENT:
              itr.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("newx"), new CodeSnippetExpression(code)));
              //  newx = code.eval(x, c, n, param);
              break;
            case FractalType.FRACTAL_TYPE_CONVERGENT:
              fractdiv = false;
              itr.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("newx"), new CodeSnippetExpression(code)));
              break;
            default:
              throw new NotSupportedException("Unknown FractalType");
              }
              if (modesused.HasFlag(SeqType.MPL_SEQ_SUM))
              {
            itr.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("sumx"), new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("sumx"), CodeBinaryOperatorType.Add, new CodeVariableReferenceExpression("newx"))));
            //sumx+=newx;
              }
              if (modesused.HasFlag(SeqType.MPL_SEQ_MEAN))
              {
            itr.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("deltac"), new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("newx"), CodeBinaryOperatorType.Subtract, new CodeVariableReferenceExpression("meanx"))));
            itr.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("meanx"), new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("meanx"), CodeBinaryOperatorType.Add, new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("deltac"), CodeBinaryOperatorType.Divide, new CodeCastExpression(typeof(double), new CodeVariableReferenceExpression("n"))))));
            /*Complex delta = newx-meanx;
            meanx = meanx+delta/(double)n;*/
            if (modesused.HasFlag(SeqType.MPL_SEQ_VARSX))
            {
              itr.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("varsx"), new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("varsx"), CodeBinaryOperatorType.Add, new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("deltac"), CodeBinaryOperatorType.Multiply, new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("newx"), CodeBinaryOperatorType.Subtract, new CodeVariableReferenceExpression("meanx"))))));
              //varsx = varsx + delta*(newx-meanx);
              if (modesused.HasFlag(SeqType.MPL_SEQ_VARIANCE))
              {
            CodeConditionStatement ifst = new CodeConditionStatement(new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("n"), CodeBinaryOperatorType.ValueEquality, new CodePrimitiveExpression(1)));
            // if (n!=1) {
            ifst.FalseStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("variacex"), new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("varsx"), CodeBinaryOperatorType.Divide, new CodeBinaryOperatorExpression(new CodeCastExpression(typeof(double), new CodeVariableReferenceExpression("n")), CodeBinaryOperatorType.Subtract, new CodePrimitiveExpression(1.0)))));
            //variacex = varsx/((double)n-(double)1);
            if (modesused.HasFlag(SeqType.MPL_SEQ_STDDEV))
            {
              ifst.FalseStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("sdx"), new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Complex)), "Sqrt", new CodeVariableReferenceExpression("variacex"))));
              //sdx = Complex.Sqrt(variacex);
            }
            itr.Statements.Add(ifst);
              }
            }
              }
              if (modesused.HasFlag(SeqType.MPL_SEQ_MIN))
              {
            CodeConditionStatement ifst = new CodeConditionStatement(new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("n"), CodeBinaryOperatorType.ValueEquality, new CodePrimitiveExpression(1)));
            ifst.TrueStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("minx"), new CodeVariableReferenceExpression("newx")));
            ifst.FalseStatements.Add(new CodeConditionStatement(new CodeBinaryOperatorExpression(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Complex)), "Abs", new CodeVariableReferenceExpression("newx")), CodeBinaryOperatorType.LessThan, new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Complex)), "Abs", new CodeVariableReferenceExpression("minx"))), new CodeAssignStatement(new CodeVariableReferenceExpression("minx"), new CodeVariableReferenceExpression("newx"))));
            itr.Statements.Add(ifst);
            //if (n==1) minx=newx; else if (Complex.Abs(newx)<Complex.Abs(minx)) minx=newx;
              }
              if (modesused.HasFlag(SeqType.MPL_SEQ_MAX))
              {
            CodeConditionStatement ifst = new CodeConditionStatement(new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("n"), CodeBinaryOperatorType.ValueEquality, new CodePrimitiveExpression(1)));
            ifst.TrueStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("maxx"), new CodeVariableReferenceExpression("newx")));
            ifst.FalseStatements.Add(new CodeConditionStatement(new CodeBinaryOperatorExpression(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Complex)), "Abs", new CodeVariableReferenceExpression("newx")), CodeBinaryOperatorType.GreaterThan, new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Complex)), "Abs", new CodeVariableReferenceExpression("maxx"))), new CodeAssignStatement(new CodeVariableReferenceExpression("maxx"), new CodeVariableReferenceExpression("newx"))));
            itr.Statements.Add(ifst);
            //if (n==1) maxx=newx; else if (Complex.Abs(newx)>Complex.Abs(maxx)) maxx=newx;
              }
              if (modesused.HasFlag(SeqType.MPL_SEQ_DELTA))
              {
            itr.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("deltax"), new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("newx"), CodeBinaryOperatorType.Subtract, new CodeArgumentReferenceExpression("x"))));
            //deltax = newx-x;
              }
              itr.Statements.Add(new CodeVariableDeclarationStatement(typeof(IEnumerator<ProcessLayer>), "pc", new CodeMethodInvokeExpression(new CodeArgumentReferenceExpression("ld"), "GetEnumerator")));
              itr.Statements.Add(new CodeVariableDeclarationStatement(typeof(ProcessLayer), "p"));
              foreach (var p in LayerData)
              {
            itr.Statements.Add(new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("pc"), "MoveNext"));
            itr.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("p"), new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("pc"), "Current")));
            CodeConditionStatement ifst = new CodeConditionStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_active"));
            //if (p.c_active) {
            ifst.TrueStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_n"), new CodeVariableReferenceExpression("n")));
            //p.c_n = n;
            ifst.TrueStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_old2x"), new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_oldx")));
            ifst.TrueStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_oldx"), new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x")));
            //p.c_old2x = p.c_oldx;
            //p.c_oldx = p.c_x;
            switch (p.c_seqtype)
            {
              case SeqType.MPL_SEQ_NORMAL: ifst.TrueStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"), new CodeVariableReferenceExpression("newx"))); break; // p.c_x = newx; break;
              case SeqType.MPL_SEQ_SUM: ifst.TrueStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"), new CodeVariableReferenceExpression("sumx"))); break; //  p.c_x = sumx; break;
              case SeqType.MPL_SEQ_MEAN: ifst.TrueStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"), new CodeVariableReferenceExpression("meanx"))); break; // p.c_x = meanx; break;
              case SeqType.MPL_SEQ_VARSX: ifst.TrueStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"), new CodeVariableReferenceExpression("varsx"))); break; // p.c_x = variacex; break;
              case SeqType.MPL_SEQ_VARIANCE: ifst.TrueStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"), new CodeVariableReferenceExpression("variacex"))); break; // p.c_x = variacex; break;
              case SeqType.MPL_SEQ_STDDEV: ifst.TrueStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"), new CodeVariableReferenceExpression("sdx"))); break; // p.c_x = sdx; break;
              case SeqType.MPL_SEQ_MIN: ifst.TrueStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"), new CodeVariableReferenceExpression("minx"))); break; //  p.c_x = minx; break;
              case SeqType.MPL_SEQ_MAX: ifst.TrueStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"), new CodeVariableReferenceExpression("maxx"))); break; //  p.c_x = maxx; break;
              case SeqType.MPL_SEQ_DELTA: ifst.TrueStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"), new CodeVariableReferenceExpression("deltax"))); break; //  p.c_x = deltax; break;
              default: ifst.TrueStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"), new CodeVariableReferenceExpression("newx"))); break; // p.c_x = newx; break;
            }
            ifst.TrueStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("newd"), new CodePrimitiveExpression(0)));
            //double newd = 0;

            switch (p.c_checktype)
            {
              case SeqCheck.MPL_CHECK_SMOOTH:
            if (fractdiv)
            {
              ifst.TrueStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("newd"), new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Math)), "Exp", new CodeBinaryOperatorExpression(new CodePrimitiveExpression(0), CodeBinaryOperatorType.Subtract, new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Complex)), "Abs", new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"))))));
              //newd = Math.Exp(-Complex.Abs(p.c_x));
            }
            else
            {
              ifst.TrueStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("newd"), new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Math)), "Exp", new CodeBinaryOperatorExpression(new CodePrimitiveExpression(0), CodeBinaryOperatorType.Subtract, new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Complex)), "Abs", new CodeBinaryOperatorExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"), CodeBinaryOperatorType.Subtract, new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_oldx")))))));
              //newd = Math.Exp(-Complex.Abs(p.c_x-p.c_oldx));
            }
            break;
              case SeqCheck.MPL_CHECK_REAL:
            ifst.TrueStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("newd"), new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"), "Real")));
            //newd = p.c_x.Real;
            break;
              case SeqCheck.MPL_CHECK_IMAG:
            ifst.TrueStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("newd"), new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"), "Imaginary")));
            //newd = p.c_x.Imaginary;
            break;
              case SeqCheck.MPL_CHECK_ARG:
            ifst.TrueStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("newd"), new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"), "Phase")));
            //newd = p.c_x.Phase;
            break;
              case SeqCheck.MPL_CHECK_ABS:
            ifst.TrueStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("newd"), new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"), "Magnitude")));
            //newd = p.c_x.Magnitude;
            break;
              case SeqCheck.MPL_CHECK_CURVATURE:
            ifst.TrueStatements.Add(new CodeConditionStatement(
              new CodeBinaryOperatorExpression(
                new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_oldx"),
                CodeBinaryOperatorType.ValueEquality,
                new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_old2x")),
                new CodeStatement[] { new CodeAssignStatement(new CodeVariableReferenceExpression("newd"), new CodePrimitiveExpression(0)) },
                new CodeStatement[] {
                          new CodeAssignStatement(new CodeVariableReferenceExpression("newd"),
                            new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Complex)),"Abs",
                              new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Complex)),"Atan",
                              new CodeBinaryOperatorExpression(
                                new CodeBinaryOperatorExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_x"),
                                  CodeBinaryOperatorType.Subtract,
                                  new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_oldx")),
                                  CodeBinaryOperatorType.Divide,
                                  new CodeBinaryOperatorExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_oldx"),
                                    CodeBinaryOperatorType.Subtract,
                                    new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_old2x"))
                              )))
                            )
                        }));
            //if ((p.c_oldx!=p.c_old2x)) newd=Complex.Abs(Complex.Atan((p.c_x-p.c_oldx) / (p.c_oldx-p.c_old2x))); else newd=0; }
            break;
              case SeqCheck.MPL_CHECK_TRIANGLE:
            if (fractaltype == FractalType.FRACTAL_TYPE_MANDEL)
            {
              ifst.TrueStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("newxnorm"), new CodeBinaryOperatorExpression(new CodeBinaryOperatorExpression(new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_oldx"), "Real"), CodeBinaryOperatorType.Multiply, new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_oldx"), "Real")), CodeBinaryOperatorType.Add, new CodeBinaryOperatorExpression(new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_oldx"), "Imaginary"), CodeBinaryOperatorType.Multiply, new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_oldx"), "Imaginary")))));
              //double newxnorm = p.c_oldx.Norm();
              ifst.TrueStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("lowbound"), new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Math)), "Abs", new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("newxnorm"), CodeBinaryOperatorType.Subtract, new CodeVariableReferenceExpression("trinorm")))));
              //double lowbound = absval(newxnorm-trinorm);
              ifst.TrueStatements.Add(
                new CodeConditionStatement(
                  new CodeBinaryOperatorExpression(new CodeBinaryOperatorExpression(new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("newxnorm"), CodeBinaryOperatorType.Add, new CodeVariableReferenceExpression("trinorm")), CodeBinaryOperatorType.Subtract, new CodeVariableReferenceExpression("lowbound")), CodeBinaryOperatorType.ValueEquality, new CodePrimitiveExpression(0)),
                  new CodeStatement[] { new CodeAssignStatement(new CodeVariableReferenceExpression("newd"), new CodePrimitiveExpression(0)) },
                  new CodeStatement[] { new CodeAssignStatement(new CodeVariableReferenceExpression("newd"), new CodeBinaryOperatorExpression(new CodeBinaryOperatorExpression(new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"), "Magnitude"), CodeBinaryOperatorType.Subtract, new CodeVariableReferenceExpression("lowbound")), CodeBinaryOperatorType.Divide, new CodeBinaryOperatorExpression(new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("newxnorm"), CodeBinaryOperatorType.Add, new CodeVariableReferenceExpression("trinorm")), CodeBinaryOperatorType.Subtract, new CodeVariableReferenceExpression("lowbound")))) }
                ));
              //if ((newxnorm+trinorm-lowbound)==0) newd=0; else
              //  newd = (p.c_x.Magnitude-lowbound)/(newxnorm+trinorm-lowbound);
            }
            else
            {
              ifst.TrueStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("newxnorm"), new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"), "Magnitude")));
              //double newxnorm = p.c_x.Magnitude;
              ifst.TrueStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("lowbound"), new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Math)), "Abs", new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("newxnorm"), CodeBinaryOperatorType.Subtract, new CodeVariableReferenceExpression("trinorm")))));
              //double lowbound = absval(newxnorm-trinorm);
              ifst.TrueStatements.Add(
                new CodeConditionStatement(
                  new CodeBinaryOperatorExpression(new CodeBinaryOperatorExpression(new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("newxnorm"), CodeBinaryOperatorType.Add, new CodeVariableReferenceExpression("trinorm")), CodeBinaryOperatorType.Subtract, new CodeVariableReferenceExpression("lowbound")), CodeBinaryOperatorType.ValueEquality, new CodePrimitiveExpression(0)),
                  new CodeStatement[] { new CodeAssignStatement(new CodeVariableReferenceExpression("newd"), new CodePrimitiveExpression(0)) },
                  new CodeStatement[] { new CodeAssignStatement(new CodeVariableReferenceExpression("newd"), new CodeBinaryOperatorExpression(new CodeBinaryOperatorExpression(new CodePropertyReferenceExpression(new CodeBinaryOperatorExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"), CodeBinaryOperatorType.Subtract, new CodeVariableReferenceExpression("c")), "Magnitude"), CodeBinaryOperatorType.Subtract, new CodeVariableReferenceExpression("lowbound")), CodeBinaryOperatorType.Divide, new CodeBinaryOperatorExpression(new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("newxnorm"), CodeBinaryOperatorType.Add, new CodeVariableReferenceExpression("trinorm")), CodeBinaryOperatorType.Subtract, new CodeVariableReferenceExpression("lowbound")))) }
                ));
              //if ((newxnorm+trinorm-lowbound)==0) newd=0; else
              //  newd = ((Complex.Abs(p.c_x-c)-lowbound)/(newxnorm+trinorm-lowbound));
            }
            break;
              case SeqCheck.MPL_CHECK_TRIANGLE_SMOOTH:
            if (fractaltype == FractalType.FRACTAL_TYPE_MANDEL)
            {
              ifst.TrueStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("newxnorm"), new CodeBinaryOperatorExpression(new CodeBinaryOperatorExpression(new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_oldx"), "Real"), CodeBinaryOperatorType.Multiply, new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_oldx"), "Real")), CodeBinaryOperatorType.Add, new CodeBinaryOperatorExpression(new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_oldx"), "Imaginary"), CodeBinaryOperatorType.Multiply, new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_oldx"), "Imaginary")))));
              //double newxnorm = p.c_oldx.Norm();
              ifst.TrueStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("lowbound"), new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Math)), "Abs", new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("newxnorm"), CodeBinaryOperatorType.Subtract, new CodeVariableReferenceExpression("trinorm")))));
              //double lowbound = absval(newxnorm-trinorm);
              ifst.TrueStatements.Add(
                new CodeConditionStatement(
                  new CodeBinaryOperatorExpression(new CodeBinaryOperatorExpression(new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("newxnorm"), CodeBinaryOperatorType.Add, new CodeVariableReferenceExpression("trinorm")), CodeBinaryOperatorType.Subtract, new CodeVariableReferenceExpression("lowbound")), CodeBinaryOperatorType.ValueEquality, new CodePrimitiveExpression(0)),
                  new CodeStatement[] { new CodeAssignStatement(new CodeVariableReferenceExpression("newd"), new CodePrimitiveExpression(0)) },
                  new CodeStatement[] { new CodeAssignStatement(new CodeVariableReferenceExpression("newd"), new CodeBinaryOperatorExpression(new CodeBinaryOperatorExpression(new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"), "Magnitude"), CodeBinaryOperatorType.Subtract, new CodeVariableReferenceExpression("lowbound")), CodeBinaryOperatorType.Divide, new CodeBinaryOperatorExpression(new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("newxnorm"), CodeBinaryOperatorType.Add, new CodeVariableReferenceExpression("trinorm")), CodeBinaryOperatorType.Subtract, new CodeVariableReferenceExpression("lowbound")))) }
                ));
              //if ((newxnorm+trinorm-lowbound)==0) newd=0; else
              //  newd = (p.c_x.Magnitude-lowbound)/(newxnorm+trinorm-lowbound);
            }
            else
            {
              ifst.TrueStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("newxnorm"), new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"), "Magnitude")));
              //double newxnorm = p.c_x.Magnitude;
              ifst.TrueStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("lowbound"), new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Math)), "Abs", new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("newxnorm"), CodeBinaryOperatorType.Subtract, new CodeVariableReferenceExpression("trinorm")))));
              //double lowbound = absval(newxnorm-trinorm);
              ifst.TrueStatements.Add(
                new CodeConditionStatement(
                  new CodeBinaryOperatorExpression(new CodeBinaryOperatorExpression(new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("newxnorm"), CodeBinaryOperatorType.Add, new CodeVariableReferenceExpression("trinorm")), CodeBinaryOperatorType.Subtract, new CodeVariableReferenceExpression("lowbound")), CodeBinaryOperatorType.ValueEquality, new CodePrimitiveExpression(0)),
                  new CodeStatement[] { new CodeAssignStatement(new CodeVariableReferenceExpression("newd"), new CodePrimitiveExpression(0)) },
                  new CodeStatement[] { new CodeAssignStatement(new CodeVariableReferenceExpression("newd"), new CodeBinaryOperatorExpression(new CodeBinaryOperatorExpression(new CodePropertyReferenceExpression(new CodeBinaryOperatorExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"), CodeBinaryOperatorType.Subtract, new CodeVariableReferenceExpression("c")), "Magnitude"), CodeBinaryOperatorType.Subtract, new CodeVariableReferenceExpression("lowbound")), CodeBinaryOperatorType.Divide, new CodeBinaryOperatorExpression(new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("newxnorm"), CodeBinaryOperatorType.Add, new CodeVariableReferenceExpression("trinorm")), CodeBinaryOperatorType.Subtract, new CodeVariableReferenceExpression("lowbound")))) }
                ));
              //if ((newxnorm+trinorm-lowbound)==0) newd=0; else
              //  newd = ((Complex.Abs(p.c_x-c)-lowbound)/(newxnorm+trinorm-lowbound));
            }
            break;
              case SeqCheck.MPL_CHECK_ORBIT_TRAP:
            switch (p.c_orbittraptype)
            {
              case OrbitTrap.MPL_ORBIT_TRAP_POINT:
                ifst.TrueStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("newd"), new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Complex)), "Abs", new CodeBinaryOperatorExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"), CodeBinaryOperatorType.Subtract, new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_pointA")))));
                //newd = Complex.Abs(p.c_x - p.c_pointA);
                break;
              case OrbitTrap.MPL_ORBIT_TRAP_LINE:
                if ((p.c_pointA.Real) == 1)
                {
                  ifst.TrueStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("newd"), new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Math)), "Abs", new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"), "Real"))));
                  //newd = Math.Abs(p.c_x.Real);
                }
                else
                {
                  ifst.TrueStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("newd"), new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Math)), "Abs", new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"), "Imaginary"))));
                  //newd = Math.Abs(p.c_x.Imaginary);
                }
                break;
              case OrbitTrap.MPL_ORBIT_TRAP_GAUSS:
                {
                  ifst.TrueStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("newd"), new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Complex)),"Abs", new CodeBinaryOperatorExpression(new CodeObjectCreateExpression(typeof(Complex),new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Math)), "Round", new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"), "Real")),new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Math)), "Round", new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"), "Imaginary"))), CodeBinaryOperatorType.Subtract,new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x")))));
                  //Complex gauss = new Complex(Math.Round(p.c_x.Real),Math.Round(p.c_x.Imaginary));
                  //newd = Complex.Abs(gauss - p.c_x);
                }
                break;
            }
            break;
            }
            switch (p.c_checkseqtype) {
              case SeqType.MPL_SEQ_NORMAL: ifst.TrueStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_calc"),new CodeVariableReferenceExpression("newd"))); break;
              case SeqType.MPL_SEQ_SUM: ifst.TrueStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_calc"), new CodeBinaryOperatorExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_calc"),CodeBinaryOperatorType.Add, new CodeVariableReferenceExpression("newd")))); break; // p.c_calc += newd; break;
              case SeqType.MPL_SEQ_MEAN: ifst.TrueStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_calc"), new CodeBinaryOperatorExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_calc"), CodeBinaryOperatorType.Add, new CodeVariableReferenceExpression("newd")))); break; // p.c_calc += newd; break;
              case SeqType.MPL_SEQ_VARSX: {
              ifst.TrueStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("delta"),new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("newd"), CodeBinaryOperatorType.Subtract, new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_cmean"))));
              //double delta = newd - p.c_cmean;
              ifst.TrueStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_cmean"),new CodeBinaryOperatorExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_cmean"),CodeBinaryOperatorType.Add,new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("delta"),CodeBinaryOperatorType.Divide,new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_n")))));
              //p.c_cmean = p.c_cmean+delta/p.c_n;
              ifst.TrueStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_calc"),new CodeBinaryOperatorExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_calc"),CodeBinaryOperatorType.Add,new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("delta"),CodeBinaryOperatorType.Multiply,new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("newd"),CodeBinaryOperatorType.Subtract,new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_cmean"))))));
              //p.c_calc += delta*(newd-p.c_cmean);
            }
            break;
              case SeqType.MPL_SEQ_VARIANCE: {
              ifst.TrueStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("delta"),new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("newd"), CodeBinaryOperatorType.Subtract, new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_cmean"))));
              //double delta = newd - p.c_cmean;
              ifst.TrueStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_cmean"),new CodeBinaryOperatorExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_cmean"),CodeBinaryOperatorType.Add,new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("delta"),CodeBinaryOperatorType.Divide,new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_n")))));
              //p.c_cmean = p.c_cmean+delta/p.c_n;
              ifst.TrueStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_cvarsx"),new CodeBinaryOperatorExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_cvarsx"),CodeBinaryOperatorType.Add,new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("delta"),CodeBinaryOperatorType.Multiply,new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("newd"),CodeBinaryOperatorType.Subtract,new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_cmean"))))));
              //p.c_cvarsx = p.c_cvarsx + delta*(newd-p.c_cmean);
              ifst.TrueStatements.Add(new CodeConditionStatement(
                  new CodeBinaryOperatorExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_n"), CodeBinaryOperatorType.ValueEquality, new CodePrimitiveExpression(1)),
                  new CodeStatement[] { },
                  new CodeStatement[] { new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_calc"),new CodeBinaryOperatorExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_cvarsx"),CodeBinaryOperatorType.Divide,new CodeBinaryOperatorExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_n"),CodeBinaryOperatorType.Subtract,new CodePrimitiveExpression(1.0))))
                  }));
              /*if (p.c_n!=1) {
                p.c_calc = p.c_cvarsx/(p.c_n-1.0);
              }*/
            }
             break;
              case SeqType.MPL_SEQ_STDDEV: {
              ifst.TrueStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("delta"), new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("newd"), CodeBinaryOperatorType.Subtract, new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_cmean"))));
              //double delta = newd - p.c_cmean;
              ifst.TrueStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_cmean"), new CodeBinaryOperatorExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_cmean"), CodeBinaryOperatorType.Add, new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("delta"), CodeBinaryOperatorType.Divide, new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_n")))));
              //p.c_cmean = p.c_cmean+delta/p.c_n;
              ifst.TrueStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_cvarsx"), new CodeBinaryOperatorExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_cvarsx"), CodeBinaryOperatorType.Add, new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("delta"), CodeBinaryOperatorType.Multiply, new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("newd"), CodeBinaryOperatorType.Subtract, new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_cmean"))))));
              //p.c_cvarsx = p.c_cvarsx + delta*(newd-p.c_cmean);
              ifst.TrueStatements.Add(new CodeConditionStatement(
                  new CodeBinaryOperatorExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_n"), CodeBinaryOperatorType.ValueEquality, new CodePrimitiveExpression(1)),
                  new CodeStatement[] { },
                  new CodeStatement[] { new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_cvariance"),new CodeBinaryOperatorExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_cvarsx"),CodeBinaryOperatorType.Divide,new CodeBinaryOperatorExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_n"),CodeBinaryOperatorType.Subtract,new CodePrimitiveExpression(1.0))))
                  }));
              /*if (p.c_n!=1) {
                p.c_cvariance = p.c_cvarsx/(p.c_n-1.0);
              }*/
              ifst.TrueStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_calc"),new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Math)), "Sqrt",new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_cvariance"))));
              //p.c_calc = Math.Sqrt(p.c_cvariance);
            }
            break;
              case SeqType.MPL_SEQ_MIN:
            ifst.TrueStatements.Add(new CodeConditionStatement(new CodeBinaryOperatorExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_n"), CodeBinaryOperatorType.ValueEquality, new CodePrimitiveExpression(1)),
              new CodeStatement[] { new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_calc"), new CodeVariableReferenceExpression("newd")) },
              new CodeStatement[] { new CodeConditionStatement(new CodeBinaryOperatorExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_calc"),CodeBinaryOperatorType.GreaterThan,new CodeVariableReferenceExpression("newd")),
                new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_calc"),new CodeVariableReferenceExpression("newd")),
                new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_resx"),new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_x")),
                new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_resn"),new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_n")))
              }));
            //if (p.c_n==1) p.c_calc=newd; else if (p.c_calc>newd) { p.c_calc = newd; p.c_resx = p.c_x; p.c_resn = p.c_n; }
              break;
              case SeqType.MPL_SEQ_MAX:
            ifst.TrueStatements.Add(new CodeConditionStatement(new CodeBinaryOperatorExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_n"), CodeBinaryOperatorType.ValueEquality, new CodePrimitiveExpression(1)),
              new CodeStatement[] { new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_calc"), new CodeVariableReferenceExpression("newd")) },
              new CodeStatement[] { new CodeConditionStatement(new CodeBinaryOperatorExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_calc"),CodeBinaryOperatorType.LessThan,new CodeVariableReferenceExpression("newd")),
                new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_calc"),new CodeVariableReferenceExpression("newd")),
                new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_resx"),new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_x")),
                new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_resn"),new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_n")))
              }));
            // if (p.c_n==1) p.c_calc=newd; else if (p.c_calc<newd) { p.c_calc = newd; p.c_resx = p.c_x; p.c_resn = p.c_n; }
              break;
              case SeqType.MPL_SEQ_DELTA:
            ifst.TrueStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_calc"),new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("newd"), CodeBinaryOperatorType.Subtract,new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_calc"))));
            //p.c_calc = newd-p.c_calc;
              break;
              default:
            ifst.TrueStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_calc"), new CodeVariableReferenceExpression("newd")));
            //p.c_calc = newd;
              break;
            }

            if (p.c_convchktype==ConvCheck.MPL_CONVCHK_REAL) {
            ifst.TrueStatements.Add(new CodeConditionStatement(new CodeBinaryOperatorExpression(new CodeBinaryOperatorExpression(new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_x"),"Real"),CodeBinaryOperatorType.Multiply,new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_x"),"Real")),fractdiv ? CodeBinaryOperatorType.GreaterThan : CodeBinaryOperatorType.LessThan,new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_bailout")),new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_active"),new CodePrimitiveExpression(false))));
               /*double ddd = p.c_x.Real*p.c_x.Real;
               if ((fractdiv) && ( ddd>p.c_bailout)) p.c_active = false;
               if (!(fractdiv) && ( ddd<p.c_bailout)) p.c_active = false;*/
            } else if (p.c_convchktype==ConvCheck.MPL_CONVCHK_IMAG) {
               ifst.TrueStatements.Add(new CodeConditionStatement(new CodeBinaryOperatorExpression(new CodeBinaryOperatorExpression(new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"), "Imaginary"), CodeBinaryOperatorType.Multiply, new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"), "Imaginary")), fractdiv ? CodeBinaryOperatorType.GreaterThan : CodeBinaryOperatorType.LessThan, new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_bailout")), new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_active"), new CodePrimitiveExpression(false))));
               /*double ddd = p.c_x.Imaginary*p.c_x.Imaginary;
               if ((fractdiv) && ( ddd>p.c_bailout)) p.c_active = false;
               if (!(fractdiv) && ( ddd<p.c_bailout)) p.c_active = false;*/
            } else if (p.c_convchktype==ConvCheck.MPL_CONVCHK_OR) {
              ifst.TrueStatements.Add(new CodeConditionStatement(new CodeBinaryOperatorExpression(new CodeBinaryOperatorExpression(new CodeBinaryOperatorExpression(new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"), "Imaginary"), CodeBinaryOperatorType.Multiply,new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"), "Imaginary")),CodeBinaryOperatorType.BooleanOr,new CodeBinaryOperatorExpression(new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"), "Real"), CodeBinaryOperatorType.Multiply, new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"), "Real"))), fractdiv ? CodeBinaryOperatorType.GreaterThan : CodeBinaryOperatorType.LessThan, new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_bailout")), new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_active"), new CodePrimitiveExpression(false))));
              /*if ((fractdiv) && ((p.c_x.Real*p.c_x.Real>p.c_bailout) || (p.c_x.Imaginary*p.c_x.Imaginary>p.c_bailout))) p.c_active = false;
              if (!(fractdiv) && ((p.c_x.Real*p.c_x.Real<p.c_bailout) || (p.c_x.Imaginary*p.c_x.Imaginary<p.c_bailout))) p.c_active = false;*/
            } else if (p.c_convchktype==ConvCheck.MPL_CONVCHK_AND) {
              ifst.TrueStatements.Add(new CodeConditionStatement(new CodeBinaryOperatorExpression(new CodeBinaryOperatorExpression(new CodeBinaryOperatorExpression(new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"), "Imaginary"), CodeBinaryOperatorType.Multiply, new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"), "Imaginary")), CodeBinaryOperatorType.BooleanAnd, new CodeBinaryOperatorExpression(new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"), "Real"), CodeBinaryOperatorType.Multiply, new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"), "Real"))), fractdiv ? CodeBinaryOperatorType.GreaterThan : CodeBinaryOperatorType.LessThan, new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_bailout")), new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_active"), new CodePrimitiveExpression(false))));
              /*if ((fractdiv) && ((p.c_x.Real*p.c_x.Real>p.c_bailout) && (p.c_x.Imaginary*p.c_x.Imaginary>p.c_bailout))) p.c_active = false;
              if (!(fractdiv) && ((p.c_x.Real*p.c_x.Real<p.c_bailout) && (p.c_x.Imaginary*p.c_x.Imaginary<p.c_bailout))) p.c_active = false;*/
            } else if (p.c_convchktype==ConvCheck.MPL_CONVCHK_MANH) {
              ifst.TrueStatements.Add(new CodeConditionStatement(new CodeBinaryOperatorExpression(new CodeBinaryOperatorExpression(new CodeBinaryOperatorExpression(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Math)),"Abs",new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_x"),"Imaginary")),CodeBinaryOperatorType.Add,new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Math)),"Abs",new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_x"),"Real"))),CodeBinaryOperatorType.Multiply,new CodeBinaryOperatorExpression(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Math)),"Abs",new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_x"),"Imaginary")),CodeBinaryOperatorType.Add,new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Math)),"Abs",new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_x"),"Real")))),fractdiv ? CodeBinaryOperatorType.GreaterThan : CodeBinaryOperatorType.LessThan, new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_bailout")), new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_active"), new CodePrimitiveExpression(false))));
              /*double ddd = (absval(p.c_x.Imaginary)+absval(p.c_x.Real))*(absval(p.c_x.Imaginary)+absval(p.c_x.Real));
               if ((fractdiv) && ( ddd>p.c_bailout)) p.c_active = false;
              if (!(fractdiv) && ( ddd<p.c_bailout)) p.c_active = false;*/
            } else if (p.c_convchktype==ConvCheck.MPL_CONVCHK_MANR) {
              ifst.TrueStatements.Add(new CodeConditionStatement(new CodeBinaryOperatorExpression(new CodeBinaryOperatorExpression(new CodeBinaryOperatorExpression(new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_x"),"Real"),CodeBinaryOperatorType.Add,new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_x"),"Imaginary")),CodeBinaryOperatorType.Multiply,new CodeBinaryOperatorExpression(new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_x"),"Real"),CodeBinaryOperatorType.Add,new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_x"),"Imaginary"))),fractdiv ? CodeBinaryOperatorType.GreaterThan : CodeBinaryOperatorType.LessThan, new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_bailout")), new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_active"), new CodePrimitiveExpression(false))));
              /*double ddd = (p.c_x.Real+p.c_x.Imaginary)*(p.c_x.Real+p.c_x.Imaginary);
               if ((fractdiv) && ( ddd>p.c_bailout)) p.c_active = false;
              if (!(fractdiv) && ( ddd<p.c_bailout)) p.c_active = false; */
            } else {
              ifst.TrueStatements.Add(new CodeConditionStatement(new CodeBinaryOperatorExpression(new CodeBinaryOperatorExpression(new CodeBinaryOperatorExpression(new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"), "Real"), CodeBinaryOperatorType.Multiply, new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"), "Real")), CodeBinaryOperatorType.Add, new CodeBinaryOperatorExpression(new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"), "Imaginary"), CodeBinaryOperatorType.Multiply, new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_x"), "Imaginary"))), fractdiv ? CodeBinaryOperatorType.GreaterThan : CodeBinaryOperatorType.LessThan, new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_bailout")), new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_active"), new CodePrimitiveExpression(false))));
              /*double ddd = p.c_x.Norm();
               if ((fractdiv) && ( ddd>p.c_bailout)) p.c_active = false;
              if (!(fractdiv) && ( ddd<p.c_bailout)) p.c_active = false;*/
            }
            ifst.TrueStatements.Add(new CodeConditionStatement(new CodeBinaryOperatorExpression(
              new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_n"),CodeBinaryOperatorType.GreaterThan,new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_nlimit")),
              new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_active"),new CodePrimitiveExpression(false)),
              new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_isin"),new CodePrimitiveExpression(true))
            ));
            //if (p.c_n>p.c_nlimit) { p.c_active = false; p.c_isin = true; }
            if (p.c_checktype == SeqCheck.MPL_CHECK_TRIANGLE_SMOOTH) {
              throw new NotImplementedException("Smooth triangle algorithm is unavailable in this CalculatorFactory");
              /*if (p.c_active == false)
            if (!p.c_isin) {
              p.c_oldx = p.c_x;
              p.c_x = Fractal_Mandel(p.c_x,c);
              p.c_n++;
              double newxnorm = p.c_oldx.Norm();
              double lowbound = absval(newxnorm-trinorm);
              if ((newxnorm+trinorm-lowbound)==0) newd=0; else
                newd = (p.c_x.Magnitude-lowbound)/(newxnorm+trinorm-lowbound);
              p.c_calc += newd;
              double oldsum = p.c_calc/(p.c_n+1);
              double il2=1/Math.Log(2);
              double lp=Math.Log(Math.Log(p.c_bailout));
              double f=il2*lp-il2*Math.Log(Math.Log(Complex.Abs(p.c_x)))+2;
              double az2 = p.c_x.Norm();
              p.c_oldx = p.c_x;
              p.c_x = Fractal_Mandel(p.c_oldx,c);
              lowbound = absval(az2-trinorm);
              if ((az2+trinorm-lowbound)!=0) p.c_calc+=(Complex.Abs(p.c_x)-lowbound)/(az2+trinorm-lowbound);
              p.c_n++;
              p.c_calc = p.c_calc/(p.c_n+1);
              p.c_calc = oldsum+(p.c_calc-oldsum)*(f-1);
            } else {
              p.c_calc /= p.c_n+1;
            }*/
            }
            else if (p.c_checkseqtype == SeqType.MPL_SEQ_MEAN)
            {
              ifst.TrueStatements.Add(new CodeConditionStatement(new CodeBinaryOperatorExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_active"),CodeBinaryOperatorType.ValueEquality,new CodePrimitiveExpression(false)),new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_calc"),new CodeBinaryOperatorExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_calc"),CodeBinaryOperatorType.Divide,new CodeBinaryOperatorExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"),"c_n"),CodeBinaryOperatorType.Add,new CodePrimitiveExpression(1))))));
              //if (p.c_active == false) p.c_calc /= p.c_n+1;
            }
            if (p == deflayer)
            {
              ifst.TrueStatements.Add(new CodeConditionStatement(new CodeBinaryOperatorExpression(new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("p"), "c_active"), CodeBinaryOperatorType.ValueEquality, new CodePrimitiveExpression(false)),new CodeAssignStatement(new CodeVariableReferenceExpression("end"), new CodePrimitiveExpression(true))));
              /*if (!deflayer.c_active) end = true; */
            }
            itr.Statements.Add(ifst);
              }
              itr.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("x"), new CodeVariableReferenceExpression("newx")));
              // x = newx;

              CodeCompileUnit compileUnit = new CodeCompileUnit();
              compileUnit.Namespaces.Add(ns);
              compileUnit.ReferencedAssemblies.Add(typeof(ICalculator).Assembly.Location);
              compileUnit.ReferencedAssemblies.Add(typeof(Complex).Assembly.Location);

            #if USE_CPP
              Microsoft.VisualC.CppCodeProvider10 cs = new Microsoft.VisualC.CppCodeProvider10();
              //CSharpCodeProvider cs = new CSharpCodeProvider(new Dictionary<string, string>(){ {"CompilerVersion", "v4.0"} });
              CompilerParameters opts = new CompilerParameters();
              opts.GenerateExecutable = false;
              opts.GenerateInMemory = true;
              opts.IncludeDebugInformation = false;
              using (Stream s = File.Open("test.cpp",FileMode.Create))
            using (StreamWriter sw = new StreamWriter(s))
              cs.GenerateCodeFromCompileUnit(compileUnit,sw,new CodeGeneratorOptions());

              var pr = System.Diagnostics.Process.Start(@"cmd", @"/c """"c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\vcvars32.bat"" & ""c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\cl.exe"""" test.cpp /CLR:PURE /O2 /LD /GS-");
              pr.WaitForExit();
              Assembly assembly = Assembly.Load(File.ReadAllBytes("test.dll"), null);
            #else
              CSharpCodeProvider cs = new CSharpCodeProvider(new Dictionary<string, string>(){ {"CompilerVersion", "v4.0"} });
              CompilerParameters opts = new CompilerParameters();
              opts.GenerateExecutable = false;
              opts.GenerateInMemory = true;
              opts.IncludeDebugInformation = false;
              CompilerResults res =  cs.CompileAssemblyFromDom(opts, compileUnit);
              /*using (Stream s = File.Open("test.cs", FileMode.Create))
            using (StreamWriter sw = new StreamWriter(s))
              cs.GenerateCodeFromCompileUnit(compileUnit, sw, new CodeGeneratorOptions());*/
              foreach (CompilerError error in res.Errors)
              {
            if (!error.IsWarning) {
              throw new NotImplementedException(error.ErrorText + " " + error.Line.ToString());
            }
              }
              Assembly assembly = res.CompiledAssembly;
            #endif

              return new PreCompiledCalculator((IPreCompiledCalculator)Activator.CreateInstance(assembly.GetType("FractRunner")));
        }
        public ICalculator GenFractalCalc(List<ProcessLayer> LayerData, FractalType fractaltype, string code, ProcessLayer deflayer)
        {
            string macros = @"
            #pragma OPENCL EXTENSION cl_amd_printf : enable

            inline float ABS(float a) {
              return a>0?a:-a;
            }

            inline float ARGC(float2 a) {
              return atan2(a.y,a.x);
            }

            inline float NORM(float2 a) {
              return a.x*a.x+a.y*a.y;
            }

            inline float ABSC(float2 a) {
              return sqrt(NORM(a));
            }

            inline float2 MULC(float2 a, float2 b) {
              return (float2)( a.x*b.x-a.y*b.y, a.y*b.x+a.x*b.y  );
            }

            inline float2 DIVC(float2 a, float2 b) {
              return (float2)( (a.x*b.x+a.y*b.y)/(b.x*b.x+b.y*b.y), (a.y*b.x-a.x*b.y)/(b.x*b.x+b.y*b.y)  );
            }

            inline float2 lnc(float2 c) {
              float r = ABSC(c);
              float a = ARGC(c);
              return (float2)(log(r),a);
            }

            inline float2 arctanc(float2 c) {
              float2 io = (float2)(0.0f,1.0f);
              float2 two = (float2)(2.0f,0.0f);
              float2 one = (float2)(1.0f,0.0f);

              return (float2)(MULC(DIVC(io,two),lnc(one - MULC(io,c))-lnc(one + MULC(io,c))));
            }

            inline float2 powc(float2 c, float p) {
              if (NORM(c)==0) {
            return (float2)(0.0f,0.0f);
              } else {
            float r = pow(ABSC(c),p);
            float a = ARGC(c)*p;
            return (float2)(r*cos(a),r*sin(a));
              }
            }

            struct ProcessLayer {
             float2 c_old2x;
             float2 c_oldx;
             float2 c_x;
             float2 c_resx;
             float c_calc;
             float c_cmean;
             float c_cvarsx;
             float c_cvariance;
             int c_active;
             int c_isin;
             int c_n;
             int c_resn;
            };

            kernel void FractalCalc (
            global  read_only float2* in_x,
            global  read_only float2* in_c,
            ";

              StringBuilder kernel = new StringBuilder(macros);

              for (int i=0; i< LayerData.Count; i++) {
            kernel.Append("    global write_only struct ProcessLayer* out_p" + i);
            kernel.Append(i+1==LayerData.Count ? "\n){" : ",\n");
              }

              bool hastriangle = false;
              bool fractdiv = true;
              SeqType modesused = 0;

              foreach (var it in LayerData)
              {
            if (it.c_checktype.HasFlag(SeqCheck.MPL_CHECK_TRIANGLE)) hastriangle = true;
            if (it.c_checktype.HasFlag(SeqCheck.MPL_CHECK_TRIANGLE_SMOOTH)) hastriangle = true;
            modesused |= it.c_seqtype;
              }

              if (modesused.HasFlag(SeqType.MPL_SEQ_STDDEV)) modesused |= SeqType.MPL_SEQ_VARIANCE;
              if (modesused.HasFlag(SeqType.MPL_SEQ_VARIANCE)) modesused |= SeqType.MPL_SEQ_VARSX;
              if (modesused.HasFlag(SeqType.MPL_SEQ_VARSX)) modesused |= SeqType.MPL_SEQ_MEAN;

              kernel.Append("float2 sumx = (float2)(0.0f,0.0f);");
              kernel.Append("float2 meanx = (float2)(0.0f,0.0f);");
              kernel.Append("float2 varsx = (float2)(0.0f,0.0f);");
              kernel.Append("float2 variacex = (float2)(0.0f,0.0f);");
              kernel.Append("float2 sdx = (float2)(0.0f,0.0f);");
              kernel.Append("float2 minx = (float2)(0.0f,0.0f);");
              kernel.Append("float2 maxx = (float2)(0.0f,0.0f);");
              kernel.Append("float2 deltax = (float2)(0.0f,0.0f);");
              kernel.Append("float2 deltac = (float2)(0.0f,0.0f);");

              kernel.Append("float delta = 0.0f;");
              kernel.Append("float newxnorm = 0.0f;");
              kernel.Append("float lowbound = 0.0f;");
              kernel.Append("float newd = 0.0f;");

              kernel.Append("int end = 0;");
              kernel.Append("int n = 0;");
              kernel.Append("float2 newx = (float2)(0.0f,0.0f);");

              kernel.Append("int index = get_global_id(0);");
              kernel.Append("float2 x = in_x[index];");
              kernel.Append("float2 c = in_c[index];");

              for (int i = 0; i < LayerData.Count; i++)
              {
            kernel.Append("struct ProcessLayer p"+i+";");
            kernel.Append("p"+i+".c_active = 1;");
            kernel.Append("p"+i+".c_isin = 0;");
            kernel.Append("p"+i+".c_x = x;");
            kernel.Append("p"+i+".c_oldx = x;");
            kernel.Append("p"+i+".c_old2x = x;");
            kernel.Append("p"+i+".c_calc = 0;");
            kernel.Append("p"+i+".c_cmean = 0;");
            kernel.Append("p"+i+".c_cvarsx = 0;");
            kernel.Append("p"+i+".c_cvariance = 0;");
              }

              kernel.Append("struct ProcessLayer* p = 0;");

              if (hastriangle)
              {
            if (fractaltype == FractalType.FRACTAL_TYPE_MANDEL)
            {
              kernel.Append("float trinorm = ABSC(c);");
              // trinorm = c.Magnitude;
            }
            else
            {
              kernel.Append("float trinorm = NORM(c);");
              // trinorm = c.Norm;
            }
              }

              kernel.Append("while (!end) {");
              // while (!end)
              kernel.Append("n++;");
              // n++;

              switch (fractaltype)
              {
            case FractalType.FRACTAL_TYPE_MANDEL:
              kernel.Append("newx = (float2)(x.x*x.x - x.y*x.y,2*x.x*x.y) + c;");
              //kernel.Append(@"printf(""%f %f - "",newx.x,newx.y);");
              //double sx = x.Real;
              //double sy = x.Imaginary;
              //return new Complex(sx * sx - sy * sy + c.Real, 2 * sx * sy + c.Imaginary);
              break;
            case FractalType.FRACTAL_TYPE_MANDEL_N:
              kernel.Append("newx = powc(x,pr) + c;");
              // return Complex.Pow(x, param) + c;
              break;
            case FractalType.FRACTAL_TYPE_BURNINGSHIP:
              kernel.Append("newx = (float2)(x.x*x.x-x.y*x.y,2*ABS(x.x*x.y)) + c;");
              //  double sx = x.Real;
              //  double sy = x.Imaginary;
              //  return new Complex(sx * sx - sy * sy + c.Real, 2 * absval(sx * sy) + c.Imaginary);
              break;
            case FractalType.FRACTAL_TYPE_BURNINGSHIP_N:
              kernel.Append("newx = powc((ABS(x.x),ABS(x.y)),pr) + c;");
              // return Complex.Pow(new Complex(absval(x.Real), absval(x.Imaginary)), n) + c;
              break;
            case FractalType.FRACTAL_TYPE_DIVERGENT:
              kernel.Append("newx = " + code + ";");
              //  newx = code.eval(x, c, n, param);
              break;
            case FractalType.FRACTAL_TYPE_CONVERGENT:
              kernel.Append("newx = " + code + ";");
              fractdiv = false;
              break;
            default:
              throw new NotSupportedException("Unknown FractalType");
              }
              if (modesused.HasFlag(SeqType.MPL_SEQ_SUM))
              {
            kernel.Append("sumx += newx;");
            //sumx+=newx;
              }
              if (modesused.HasFlag(SeqType.MPL_SEQ_MEAN))
              {
            kernel.Append("deltax = newx-meanx;");
            kernel.Append("meanx += deltax/(float)n;");
            /*Complex delta = newx-meanx;
            meanx = meanx+delta/(double)n;*/
            if (modesused.HasFlag(SeqType.MPL_SEQ_VARSX))
            {
              kernel.Append("varsx += MULC(deltax,(newx-meanx));");
              //varsx = varsx + delta*(newx-meanx);
              if (modesused.HasFlag(SeqType.MPL_SEQ_VARIANCE))
              {
            kernel.Append("if (n!=1) {");
            // if (n!=1) {
            kernel.Append("variacex = varsx / (float)((float)n-(float)1.0f);");
            //variacex = varsx/((double)n-(double)1);
            if (modesused.HasFlag(SeqType.MPL_SEQ_STDDEV))
            {
              kernel.Append("sdx = powc(variacex,0.5f);");
              //sdx = Complex.Sqrt(variacex);
            }
            kernel.Append("}");
              }
            }
              }
              if (modesused.HasFlag(SeqType.MPL_SEQ_MIN))
              {
            kernel.Append("if (n==1) minx = newx; else {");
            kernel.Append("if (NORM(newx)<NORM(minx)) { minx = newx; } }");
            //if (n==1) minx=newx; else if (Complex.Abs(newx)<Complex.Abs(minx)) minx=newx;
              }
              if (modesused.HasFlag(SeqType.MPL_SEQ_MAX))
              {
            kernel.Append("if (n==1) maxx = newx; else {");
            kernel.Append("if (NORM(newx)>NORM(maxx)) { maxx = newx; } }");
            //if (n==1) maxx=newx; else if (Complex.Abs(newx)>Complex.Abs(maxx)) maxx=newx;
              }
              if (modesused.HasFlag(SeqType.MPL_SEQ_DELTA))
              {
            kernel.Append("deltax = newx - x");
            //deltax = newx-x;
              }

              for (int i=0; i< LayerData.Count; i++)
              {
            var p = LayerData[i];
            kernel.Append("p = &p"+i+";");
            kernel.Append("if (p->c_active) {");
            //if (p.c_active) {
            kernel.Append("p->c_n = n;");
            //p.c_n = n;
            kernel.Append("p->c_old2x = p->c_oldx;");
            kernel.Append("p->c_oldx = p->c_x;");
            //p.c_old2x = p.c_oldx;
            //p.c_oldx = p.c_x;
            switch (p.c_seqtype)
            {
              case SeqType.MPL_SEQ_NORMAL: kernel.Append("p->c_x = newx;"); break; // p.c_x = newx; break;
              case SeqType.MPL_SEQ_SUM: kernel.Append("p->c_x = sumx;"); break; //  p.c_x = sumx; break;
              case SeqType.MPL_SEQ_MEAN: kernel.Append("p->c_x = meanx;"); break;// p.c_x = meanx; break;
              case SeqType.MPL_SEQ_VARSX: kernel.Append("p->c_x = varsx;"); break;
              case SeqType.MPL_SEQ_VARIANCE: kernel.Append("p->c_x = variacex;"); break; // p.c_x = variacex; break;
              case SeqType.MPL_SEQ_STDDEV: kernel.Append("p->c_x = sdx;"); break; // p.c_x = sdx; break;
              case SeqType.MPL_SEQ_MIN: kernel.Append("p->c_x = minx;"); break; //  p.c_x = minx; break;
              case SeqType.MPL_SEQ_MAX: kernel.Append("p->c_x = maxx;"); break; //  p.c_x = maxx; break;
              case SeqType.MPL_SEQ_DELTA: kernel.Append("p->c_x = deltax;"); break; //  p.c_x = deltax; break;
              default: kernel.Append("p->c_x = newx;"); break; // p.c_x = newx; break;
            }
            kernel.Append("newd = 0;");
            //double newd = 0;

            switch (p.c_checktype)
            {
              case SeqCheck.MPL_CHECK_SMOOTH:
            if (fractdiv)
            {
              kernel.Append("newd = exp(-ABSC(p->c_x));");
              //newd = Math.Exp(-Complex.Abs(p.c_x));
            }
            else
            {
              kernel.Append("newd = exp(-ABSC(p->c_x-p->c_oldx));");
              //newd = Math.Exp(-Complex.Abs(p.c_x-p.c_oldx));
            }
            break;
              case SeqCheck.MPL_CHECK_REAL:
            kernel.Append("newd = p->c_x.x;");
            //newd = p.c_x.Real;
            break;
              case SeqCheck.MPL_CHECK_IMAG:
            kernel.Append("newd = p->c_x.y;");
            //newd = p.c_x.Imaginary;
            break;
              case SeqCheck.MPL_CHECK_ARG:
            kernel.Append("newd = atan2(p->c_x.y,p->c_x.x);");
            //newd = p.c_x.Phase;
            break;
              case SeqCheck.MPL_CHECK_ABS:
            kernel.Append("newd = ABSC(p->c_x);");
            //newd = p.c_x.Magnitude;
            break;
              case SeqCheck.MPL_CHECK_CURVATURE:
            kernel.Append("if (isnotequal(p.c_oldx,p.c_old2x)) { newd = ABSC(atanc(DIVC(p->c_x-p->c_oldx,p->c_oldx-p->c_old2x))); } else newd = 0;");
            //if ((p.c_oldx!=p.c_old2x)) newd=Complex.Abs(Complex.Atan((p.c_x-p.c_oldx) / (p.c_oldx-p.c_old2x))); else newd=0; }
            break;
              case SeqCheck.MPL_CHECK_TRIANGLE:
            if (fractaltype == FractalType.FRACTAL_TYPE_MANDEL)
            {
              kernel.Append("newxnorm = NORM(p->c_oldx);");
              //double newxnorm = p.c_oldx.Norm();
              kernel.Append("lowbound = ABS(newxnorm-trinorm);");
              //double lowbound = absval(newxnorm-trinorm);
              kernel.Append("if ((newxnorm+trinorm-lowbound)==0) newd = 0; else newd = (ABSC(p->c_x)-lowbound)/(newxnorm+trinorm-lowbound);");
              //if ((newxnorm+trinorm-lowbound)==0) newd=0; else
              //  newd = (p.c_x.Magnitude-lowbound)/(newxnorm+trinorm-lowbound);
            }
            else
            {
              kernel.Append("newxnorm = ABSC(p->c_x);");
              //double newxnorm = p.c_x.Magnitude;
              kernel.Append("lowbound = ABS(newxnorm-trinorm);");
              //double lowbound = absval(newxnorm-trinorm);
              kernel.Append("if ((newxnorm+trinorm-lowbound)==0) newd = 0; else newd = (ABSC(p->c_x-c)-lowbound)/(newxnorm+trinorm-lowbound);");
              //if ((newxnorm+trinorm-lowbound)==0) newd=0; else
              //  newd = ((Complex.Abs(p.c_x-c)-lowbound)/(newxnorm+trinorm-lowbound));
            }
            break;
              case SeqCheck.MPL_CHECK_TRIANGLE_SMOOTH:
            if (fractaltype == FractalType.FRACTAL_TYPE_MANDEL)
            {
              kernel.Append("newxnorm = NORM(p->c_oldx);");
              //double newxnorm = p.c_oldx.Norm();
              kernel.Append("lowbound = ABS(newxnorm-trinorm);");
              //double lowbound = absval(newxnorm-trinorm);
              kernel.Append("if ((newxnorm+trinorm-lowbound)==0) newd = 0; else newd = (ABSC(p->c_x)-lowbound)/(newxnorm+trinorm-lowbound);");
              //if ((newxnorm+trinorm-lowbound)==0) newd=0; else
              //  newd = (p.c_x.Magnitude-lowbound)/(newxnorm+trinorm-lowbound);
            }
            else
            {
              kernel.Append("newxnorm = ABSC(p->c_x);");
              //double newxnorm = p.c_x.Magnitude;
              kernel.Append("lowbound = ABS(newxnorm-trinorm);");
              //double lowbound = absval(newxnorm-trinorm);
              kernel.Append("if ((newxnorm+trinorm-lowbound)==0) newd = 0; else newd = (ABSC(p->c_x-c)-lowbound)/(newxnorm+trinorm-lowbound);");
              //if ((newxnorm+trinorm-lowbound)==0) newd=0; else
              //  newd = ((Complex.Abs(p.c_x-c)-lowbound)/(newxnorm+trinorm-lowbound));
            }
            break;
              case SeqCheck.MPL_CHECK_ORBIT_TRAP:
            switch (p.c_orbittraptype)
            {
              case OrbitTrap.MPL_ORBIT_TRAP_POINT:
                kernel.Append("newd = ABSC(p->c_x - p->c_pointA);");
                //newd = Complex.Abs(p.c_x - p.c_pointA);
                break;
              case OrbitTrap.MPL_ORBIT_TRAP_LINE:
                if ((p.c_pointA.Real) == 1)
                {
                  kernel.Append("newd = ABS(p->c_x.x);");
                  //newd = Math.Abs(p.c_x.Real);
                }
                else
                {
                  kernel.Append("newd = ABS(p->c_x.y);");
                  //newd = Math.Abs(p.c_x.Imaginary);
                }
                break;
              case OrbitTrap.MPL_ORBIT_TRAP_GAUSS:
                {
                  kernel.Append("newd = ABSC((round(p->c_x.x),round(p->c_x.y)) - p->c_x);");
                  //Complex gauss = new Complex(Math.Round(p.c_x.Real),Math.Round(p.c_x.Imaginary));
                  //newd = Complex.Abs(gauss - p.c_x);
                }
                break;
            }
            break;
            }
            switch (p.c_checkseqtype)
            {
              case SeqType.MPL_SEQ_NORMAL: kernel.Append("p->c_calc = newd;"); break;
              case SeqType.MPL_SEQ_SUM: kernel.Append("p->c_calc += newd;"); break; // p.c_calc += newd; break;
              case SeqType.MPL_SEQ_MEAN: kernel.Append("p->c_calc += newd;"); break; // p.c_calc += newd; break;
              case SeqType.MPL_SEQ_VARSX:
            {
              kernel.Append("delta = newd - p->c_cmean;");
              //double delta = newd - p.c_cmean;
              kernel.Append("p->c_cmean = p->c_cmean + delta / p->c_n;");
              //p.c_cmean = p.c_cmean+delta/p.c_n;
              kernel.Append("p->c_calc += delta * (newd - p->c_cmean);");
              //p.c_calc += delta*(newd-p.c_cmean);
            }
            break;
              case SeqType.MPL_SEQ_VARIANCE:
            {
              kernel.Append("delta = newd - p->c_cmean;");
              //double delta = newd - p.c_cmean;
              kernel.Append("p->c_cmean = p->c_cmean + delta / p->c_n;");
              //p.c_cmean = p.c_cmean+delta/p.c_n;
              kernel.Append("p->c_cvarsx += delta * (newd - p->c_cmean);");
              //p.c_cvarsx = p.c_cvarsx + delta*(newd-p.c_cmean);
              kernel.Append("if (p->c_n!=1) { p->c_calc = p->c_cvarsx/(p->c_n-1.0f); }");
              /*if (p.c_n!=1) {
                p.c_calc = p.c_cvarsx/(p.c_n-1.0);
              }*/
            }
            break;
              case SeqType.MPL_SEQ_STDDEV:
            {
              kernel.Append("delta = newd - p->c_cmean;");
              //double delta = newd - p.c_cmean;
              kernel.Append("p->c_cmean = p->c_cmean + delta / p->c_n;");
              //p.c_cmean = p.c_cmean+delta/p.c_n;
              kernel.Append("p->c_cvarsx += delta * (newd - p->c_cmean);");
              //p.c_cvarsx = p.c_cvarsx + delta*(newd-p.c_cmean);
              kernel.Append("if (p->c_n!=1) { p->c_cvariance = p->c_cvarsx/((float)p->c_n-1.0f);");
              /*if (p.c_n!=1) {
                p.c_cvariance = p.c_cvarsx/(p.c_n-1.0);
              }*/
              kernel.Append("p->c_calc = sqrt(p->c_cvariance);");
              //p.c_calc = Math.Sqrt(p.c_cvariance);
              kernel.Append("}");
            }
            break;
              case SeqType.MPL_SEQ_MIN:
            kernel.Append("if (p->c_n==1) p->c_calc = newd; else if (p->c_calc>newd) { p->c_calc = newd; p->c_resx = p->c_x; p->c_resn = p->c_n; };");
            //if (p.c_n==1) p.c_calc=newd; else if (p.c_calc>newd) { p.c_calc = newd; p.c_resx = p.c_x; p.c_resn = p.c_n; }
            break;
              case SeqType.MPL_SEQ_MAX:
            kernel.Append("if (p->c_n==1) p->c_calc = newd; else if (p->c_calc<newd) { p->c_calc = newd; p->c_resx = p->c_x; p->c_resn = p->c_n; };");
            // if (p.c_n==1) p.c_calc=newd; else if (p.c_calc<newd) { p.c_calc = newd; p.c_resx = p.c_x; p.c_resn = p.c_n; }
            break;
              case SeqType.MPL_SEQ_DELTA:
            kernel.Append("p->c_calc = newd-p->c_calc;");
            //p.c_calc = newd-p.c_calc;
            break;
              default:
            kernel.Append("p->c_calc = newd;");
            //p.c_calc = newd;
            break;
            }

            if (p.c_convchktype == ConvCheck.MPL_CONVCHK_REAL)
            {
              kernel.AppendFormat(CultureInfo.InvariantCulture,"if (p->c_x.x*p->c_x.x " + (fractdiv ? ">" : "<") + " {0:E}f) p->c_active = 0;", p.c_bailout);
              /*double ddd = p.c_x.Real*p.c_x.Real;
              if ((fractdiv) && ( ddd>p.c_bailout)) p.c_active = false;
              if (!(fractdiv) && ( ddd<p.c_bailout)) p.c_active = false;*/
            }
            else if (p.c_convchktype == ConvCheck.MPL_CONVCHK_IMAG)
            {
              kernel.AppendFormat(CultureInfo.InvariantCulture, "if (p->c_x.y*p->c_x.y " + (fractdiv ? ">" : "<") + " {0:E}f) p->c_active = 0;", p.c_bailout);
              /*double ddd = p.c_x.Imaginary*p.c_x.Imaginary;
              if ((fractdiv) && ( ddd>p.c_bailout)) p.c_active = false;
              if (!(fractdiv) && ( ddd<p.c_bailout)) p.c_active = false;*/
            }
            else if (p.c_convchktype == ConvCheck.MPL_CONVCHK_OR)
            {
              kernel.AppendFormat(CultureInfo.InvariantCulture, "if ((p->c_x.y*p->c_x.y " + (fractdiv ? ">" : "<") + " {0:E}f) || (p->c_x.x*p->c_x.x " + (fractdiv ? ">" : "<") + " {0:E}f)) p->c_active = 0;", p.c_bailout);
              /*if ((fractdiv) && ((p.c_x.Real*p.c_x.Real>p.c_bailout) || (p.c_x.Imaginary*p.c_x.Imaginary>p.c_bailout))) p.c_active = false;
              if (!(fractdiv) && ((p.c_x.Real*p.c_x.Real<p.c_bailout) || (p.c_x.Imaginary*p.c_x.Imaginary<p.c_bailout))) p.c_active = false;*/
            }
            else if (p.c_convchktype == ConvCheck.MPL_CONVCHK_AND)
            {
              kernel.AppendFormat(CultureInfo.InvariantCulture, "if ((p->c_x.y*p->c_x.y " + (fractdiv ? ">" : "<") + " {0:E}f) && (p->c_x.x*p->c_x.x " + (fractdiv ? ">" : "<") + " {0:E}f)) p->c_active = 0;", p.c_bailout);
              /*if ((fractdiv) && ((p.c_x.Real*p.c_x.Real>p.c_bailout) && (p.c_x.Imaginary*p.c_x.Imaginary>p.c_bailout))) p.c_active = false;
              if (!(fractdiv) && ((p.c_x.Real*p.c_x.Real<p.c_bailout) && (p.c_x.Imaginary*p.c_x.Imaginary<p.c_bailout))) p.c_active = false;*/
            }
            else if (p.c_convchktype == ConvCheck.MPL_CONVCHK_MANH)
            {
              kernel.AppendFormat(CultureInfo.InvariantCulture, "if ( ((ABS(p->c_x.y)+ABS(p->c_x.x))*((ABS(p->c_x.y)+ABS(p->c_x.x))) " + (fractdiv ? ">" : "<") + " {0:G}f)) p->c_active = 0;", p.c_bailout);
              /*double ddd = (absval(p.c_x.Imaginary)+absval(p.c_x.Real))*(absval(p.c_x.Imaginary)+absval(p.c_x.Real));
               if ((fractdiv) && ( ddd>p.c_bailout)) p.c_active = false;
              if (!(fractdiv) && ( ddd<p.c_bailout)) p.c_active = false;*/
            }
            else if (p.c_convchktype == ConvCheck.MPL_CONVCHK_MANR)
            {
              kernel.AppendFormat(CultureInfo.InvariantCulture, "if ( ((p->c_x.y+p->c_x.x)*(p->c_x.y+p->c_x.x)) " + (fractdiv ? ">" : "<") + " {0:E}f)) p->c_active = 0;", p.c_bailout);
              /*double ddd = (p.c_x.Real+p.c_x.Imaginary)*(p.c_x.Real+p.c_x.Imaginary);
               if ((fractdiv) && ( ddd>p.c_bailout)) p.c_active = false;
              if (!(fractdiv) && ( ddd<p.c_bailout)) p.c_active = false; */
            }
            else
            {
              kernel.AppendFormat(CultureInfo.InvariantCulture, "if (NORM(p->c_x) " + (fractdiv ? ">" : "<") + " {0:E}f) p->c_active = 0;", p.c_bailout);
              /*double ddd = p.c_x.Norm();
               if ((fractdiv) && ( ddd>p.c_bailout)) p.c_active = false;
              if (!(fractdiv) && ( ddd<p.c_bailout)) p.c_active = false;*/
            }
            kernel.AppendFormat(CultureInfo.InvariantCulture, "if (p->c_n>{0}) {{ p->c_active = 0; p->c_isin = 1; }}", p.c_nlimit);
            //if (p.c_n>p.c_nlimit) { p.c_active = false; p.c_isin = true; }
            if (p.c_checktype == SeqCheck.MPL_CHECK_TRIANGLE_SMOOTH)
            {
              throw new NotImplementedException("Smooth triangle algorithm is unavailable in this CalculatorFactory");
              /*if (p.c_active == false)
            if (!p.c_isin) {
              p.c_oldx = p.c_x;
              p.c_x = Fractal_Mandel(p.c_x,c);
              p.c_n++;
              double newxnorm = p.c_oldx.Norm();
              double lowbound = absval(newxnorm-trinorm);
              if ((newxnorm+trinorm-lowbound)==0) newd=0; else
                newd = (p.c_x.Magnitude-lowbound)/(newxnorm+trinorm-lowbound);
              p.c_calc += newd;
              double oldsum = p.c_calc/(p.c_n+1);
              double il2=1/Math.Log(2);
              double lp=Math.Log(Math.Log(p.c_bailout));
              double f=il2*lp-il2*Math.Log(Math.Log(Complex.Abs(p.c_x)))+2;
              double az2 = p.c_x.Norm();
              p.c_oldx = p.c_x;
              p.c_x = Fractal_Mandel(p.c_oldx,c);
              lowbound = absval(az2-trinorm);
              if ((az2+trinorm-lowbound)!=0) p.c_calc+=(Complex.Abs(p.c_x)-lowbound)/(az2+trinorm-lowbound);
              p.c_n++;
              p.c_calc = p.c_calc/(p.c_n+1);
              p.c_calc = oldsum+(p.c_calc-oldsum)*(f-1);
            } else {
              p.c_calc /= p.c_n+1;
            }*/
            }
            else if (p.c_checkseqtype == SeqType.MPL_SEQ_MEAN)
            {
              kernel.Append("if (p->c_active == 0) p->c_calc /= (float)p->c_n+1.0f;");
              //if (p.c_active == false) p.c_calc /= p.c_n+1;
            }
            if (p == deflayer)
            {
              kernel.Append("if (p->c_active == 0) end = 1;");
              /*if (!deflayer.c_active) end = true; */
            }
            kernel.Append("}");

              }
              kernel.Append("x = newx; }");
              for (int i = 0; i < LayerData.Count; i++)
              {
            kernel.Append("out_p"+i+"[index] = p"+i+";");
            //kernel.Append("out_p" + i + "[index].c_calc = 52.0f;");
              }
              kernel.Append("}");

              //System.Console.WriteLine(kernel.Replace(";", ";\n").Replace("}","}\n"));
              //kernel.Clear();
              //kernel.Append(@"kernel void VectorAdd(global  read_only float* a,global  read_only float* b,global write_only float* c ){int index = get_global_id(0);c[index] = a[index] + b[index];}");

              ComputeProgram prg = new ComputeProgram(_context, kernel.Replace(";", ";\n").Replace("}","}\n").ToString());
              try
              {
            prg.Build(null, null, null, IntPtr.Zero);
              }
              catch (ComputeException e)
              {
            throw new Exception("Error while building: " + prg.GetBuildLog(_context.Devices[0]), e);
              }
              ComputeKernel krnl = prg.CreateKernel("FractalCalc");

              return new OpenCLCalculator(_context,prg,krnl);
        }
 public ICalculator GenFractalCalc(List<ProcessLayer> LayerData, FractalType fractaltype, string code, ProcessLayer deflayer)
 {
     return new SimpleCalculator(fractaltype, code, deflayer);
 }
            private void CalcFractal(Complex x, Complex c, List<ProcessLayer> LayerData, double param)
            {
                bool fractdiv = true;
                bool hastriangle = false;
                double trinorm = 0;

                Complex sumx = 0, meanx = 0, varsx = 0, variacex = 0, sdx = 0, minx = 0, maxx = 0, deltax = 0;
                SeqType modesused = 0;

                foreach (var it in LayerData)
                {
                  it.c_active = true;
                  it.c_isin = false;
                  if (it.c_default) deflayer = it;
                  if (it.c_checktype.HasFlag(SeqCheck.MPL_CHECK_TRIANGLE)) hastriangle = true;
                  if (it.c_checktype.HasFlag(SeqCheck.MPL_CHECK_TRIANGLE_SMOOTH)) hastriangle = true;
                  modesused |= it.c_seqtype;
                  it.c_x = x;
                  it.c_oldx = x;
                  it.c_old2x = x;
                  it.c_calc = 0;
                  it.c_cmean = 0;
                  it.c_cvarsx = 0;
                  it.c_cvariance = 0;
                }

                if (modesused.HasFlag(SeqType.MPL_SEQ_STDDEV)) modesused |= SeqType.MPL_SEQ_VARIANCE;
                if (modesused.HasFlag(SeqType.MPL_SEQ_VARIANCE)) modesused |= SeqType.MPL_SEQ_VARSX;
                if (modesused.HasFlag(SeqType.MPL_SEQ_VARSX)) modesused |= SeqType.MPL_SEQ_MEAN;

                if (hastriangle)
                {
                  if (fractaltype == FractalType.FRACTAL_TYPE_MANDEL)
                  {
                trinorm = Complex.Abs(c);
                  }
                  else
                  {
                trinorm = norm(c);
                  }
                }

                if (deflayer != null)
                {
                  int n = 0;
                  bool end = false;
                  Complex newx = x;
                  while (!end)
                  {
                n++;
                switch (fractaltype)
                {
                  case FractalType.FRACTAL_TYPE_MANDEL:
                newx = Fractal_Mandel(x, c);
                break;
                  case FractalType.FRACTAL_TYPE_MANDEL_N:
                newx = Fractal_Mandel_n(x, c, param);
                break;
                  case FractalType.FRACTAL_TYPE_BURNINGSHIP:
                newx = Fractal_BurningShip(x, c);
                break;
                  case FractalType.FRACTAL_TYPE_BURNINGSHIP_N:
                newx = Fractal_BurningShip_n(x, c, param);
                break;
                  case FractalType.FRACTAL_TYPE_DIVERGENT:
                if (code != null)
                {
                  newx = code.eval(x, c, n, param);
                }
                break;
                  case FractalType.FRACTAL_TYPE_CONVERGENT:
                fractdiv = false;
                if (code != null)
                {
                  newx = code.eval(x, c, n, param);
                }
                break;
                  default:
                newx = x;
                break;
                }
                if (modesused.HasFlag(SeqType.MPL_SEQ_SUM)) sumx += newx;
                if (modesused.HasFlag(SeqType.MPL_SEQ_MEAN))
                {
                  Complex delta = newx - meanx;
                  meanx = meanx + delta / (double)n;
                  if (modesused.HasFlag(SeqType.MPL_SEQ_VARSX))
                  {
                varsx = varsx + delta * (newx - meanx);
                if (modesused.HasFlag(SeqType.MPL_SEQ_VARIANCE))
                {
                  if (n != 1)
                  {
                    variacex = varsx / ((double)n - (double)1);
                    if (modesused.HasFlag(SeqType.MPL_SEQ_STDDEV))
                    {
                      sdx = Complex.Sqrt(variacex);
                    }
                  }
                }
                  }
                }
                if (modesused.HasFlag(SeqType.MPL_SEQ_MIN)) if (n == 1) minx = newx; else if (Complex.Abs(newx) < Complex.Abs(minx)) minx = newx;
                if (modesused.HasFlag(SeqType.MPL_SEQ_MAX)) if (n == 1) maxx = newx; else if (Complex.Abs(newx) > Complex.Abs(maxx)) maxx = newx;
                if (modesused.HasFlag(SeqType.MPL_SEQ_DELTA)) deltax = newx - x;
                foreach (var p in LayerData)
                {
                  if (p.c_active)
                  {
                p.c_n = n;
                p.c_old2x = p.c_oldx;
                p.c_oldx = p.c_x;
                switch (p.c_seqtype)
                {
                  case SeqType.MPL_SEQ_NORMAL: p.c_x = newx; break;
                  case SeqType.MPL_SEQ_SUM: p.c_x = sumx; break;
                  case SeqType.MPL_SEQ_MEAN: p.c_x = meanx; break;
                  case SeqType.MPL_SEQ_VARSX: p.c_x = varsx; break;
                  case SeqType.MPL_SEQ_VARIANCE: p.c_x = variacex; break;
                  case SeqType.MPL_SEQ_STDDEV: p.c_x = sdx; break;
                  case SeqType.MPL_SEQ_MIN: p.c_x = minx; break;
                  case SeqType.MPL_SEQ_MAX: p.c_x = maxx; break;
                  case SeqType.MPL_SEQ_DELTA: p.c_x = deltax; break;
                  default: p.c_x = newx; break;
                }
                double newd = 0;
                switch (p.c_checktype)
                {
                  case SeqCheck.MPL_CHECK_SMOOTH:
                    if (fractdiv)
                    {
                      newd = Math.Exp(-Complex.Abs(p.c_x));
                    }
                    else
                    {
                      newd = Math.Exp(-Complex.Abs(p.c_x - p.c_oldx));
                    }
                    break;
                  case SeqCheck.MPL_CHECK_REAL:
                    newd = p.c_x.Real;
                    break;
                  case SeqCheck.MPL_CHECK_IMAG:
                    newd = p.c_x.Imaginary;
                    break;
                  case SeqCheck.MPL_CHECK_ARG:
                    newd = p.c_x.Phase;
                    break;
                  case SeqCheck.MPL_CHECK_ABS:
                    newd = p.c_x.Magnitude;
                    break;
                  case SeqCheck.MPL_CHECK_CURVATURE:
                    {
                      if ((p.c_oldx != p.c_old2x))
                        newd = Complex.Abs(Complex.Atan((p.c_x - p.c_oldx) / (p.c_oldx - p.c_old2x)));
                      else newd = 0;
                    }
                    break;
                  case SeqCheck.MPL_CHECK_TRIANGLE:
                    if (fractaltype == FractalType.FRACTAL_TYPE_MANDEL)
                    {
                      double newxnorm = norm(p.c_oldx);
                      double lowbound = absval(newxnorm - trinorm);
                      if ((newxnorm + trinorm - lowbound) == 0) newd = 0;
                      else
                        newd = (p.c_x.Magnitude - lowbound) / (newxnorm + trinorm - lowbound);
                    }
                    else
                    {
                      double newxnorm = p.c_x.Magnitude;
                      double lowbound = absval(newxnorm - trinorm);
                      if ((newxnorm + trinorm - lowbound) == 0) newd = 0;
                      else
                        newd = ((Complex.Abs(p.c_x - c) - lowbound) / (newxnorm + trinorm - lowbound));
                    }
                    break;
                  case SeqCheck.MPL_CHECK_TRIANGLE_SMOOTH:
                    if (fractaltype == FractalType.FRACTAL_TYPE_MANDEL)
                    {
                      double newxnorm = norm(p.c_oldx);
                      double lowbound = absval(newxnorm - trinorm);
                      if ((newxnorm + trinorm - lowbound) == 0) newd = 0;
                      else
                        newd = (Complex.Abs(p.c_x) - lowbound) / (newxnorm + trinorm - lowbound);
                    }
                    else
                    {
                      double newxnorm = p.c_x.Magnitude;
                      double lowbound = absval(newxnorm - trinorm);
                      if ((newxnorm + trinorm - lowbound) == 0) newd = 0;
                      else
                        newd = ((Complex.Abs(p.c_x - c) - lowbound) / (newxnorm + trinorm - lowbound));
                    }
                    break;
                  case SeqCheck.MPL_CHECK_ORBIT_TRAP:
                    switch (p.c_orbittraptype)
                    {
                      case OrbitTrap.MPL_ORBIT_TRAP_POINT:
                        newd = Complex.Abs(p.c_x - p.c_pointA);
                        break;
                      case OrbitTrap.MPL_ORBIT_TRAP_LINE:
                        if ((p.c_pointA.Real) == 1)
                        {
                          newd = absval(p.c_x.Real);
                        }
                        else
                        {
                          newd = absval(p.c_x.Imaginary);
                        }
                        break;
                      case OrbitTrap.MPL_ORBIT_TRAP_GAUSS:
                        {
                          Complex gauss = new Complex(Math.Round(p.c_x.Real), Math.Round(p.c_x.Imaginary));
                          newd = Complex.Abs(gauss - p.c_x);
                        }
                        break;
                    }
                    break;
                }
                switch (p.c_checkseqtype)
                {
                  case SeqType.MPL_SEQ_NORMAL: p.c_calc = newd; break;
                  case SeqType.MPL_SEQ_SUM: p.c_calc += newd; break;
                  case SeqType.MPL_SEQ_MEAN: p.c_calc += newd; break;
                  case SeqType.MPL_SEQ_VARSX:
                    {
                      double delta = newd - p.c_cmean;
                      p.c_cmean = p.c_cmean + delta / p.c_n;
                      p.c_calc += delta * (newd - p.c_cmean);
                    }
                    break;
                  case SeqType.MPL_SEQ_VARIANCE:
                    {
                      double delta = newd - p.c_cmean;
                      p.c_cmean = p.c_cmean + delta / p.c_n;
                      p.c_cvarsx = p.c_cvarsx + delta * (newd - p.c_cmean);
                      if (p.c_n != 1)
                      {
                        p.c_calc = p.c_cvarsx / (p.c_n - 1.0);
                      }
                    }
                    break;
                  case SeqType.MPL_SEQ_STDDEV:
                    {
                      double delta = newd - p.c_cmean;
                      p.c_cmean = p.c_cmean + delta / p.c_n;
                      p.c_cvarsx = p.c_cvarsx + delta * (newd - p.c_cmean);
                      if (p.c_n != 1)
                      {
                        p.c_cvariance = p.c_cvarsx / (p.c_n - 1.0);
                      }
                      p.c_calc = Math.Sqrt(p.c_cvariance);
                    }
                    break;
                  case SeqType.MPL_SEQ_MIN: if (p.c_n == 1) p.c_calc = newd; else if (p.c_calc > newd) { p.c_calc = newd; p.c_resx = p.c_x; p.c_resn = p.c_n; } break;
                  case SeqType.MPL_SEQ_MAX: if (p.c_n == 1) p.c_calc = newd; else if (p.c_calc < newd) { p.c_calc = newd; p.c_resx = p.c_x; p.c_resn = p.c_n; } break;
                  case SeqType.MPL_SEQ_DELTA: p.c_calc = newd - p.c_calc; break;
                  default: p.c_calc = newd; break;
                }
                if (p.c_convchktype == ConvCheck.MPL_CONVCHK_REAL)
                {
                  double ddd = p.c_x.Real * p.c_x.Real;
                  if ((fractdiv) && (ddd > p.c_bailout)) p.c_active = false;
                  if (!(fractdiv) && (ddd < p.c_bailout)) p.c_active = false;
                }
                else if (p.c_convchktype == ConvCheck.MPL_CONVCHK_IMAG)
                {
                  double ddd = p.c_x.Imaginary * p.c_x.Imaginary;
                  if ((fractdiv) && (ddd > p.c_bailout)) p.c_active = false;
                  if (!(fractdiv) && (ddd < p.c_bailout)) p.c_active = false;
                }
                else if (p.c_convchktype == ConvCheck.MPL_CONVCHK_OR)
                {
                  if ((fractdiv) && ((p.c_x.Real * p.c_x.Real > p.c_bailout) || (p.c_x.Imaginary * p.c_x.Imaginary > p.c_bailout))) p.c_active = false;
                  if (!(fractdiv) && ((p.c_x.Real * p.c_x.Real < p.c_bailout) || (p.c_x.Imaginary * p.c_x.Imaginary < p.c_bailout))) p.c_active = false;
                }
                else if (p.c_convchktype == ConvCheck.MPL_CONVCHK_AND)
                {
                  if ((fractdiv) && ((p.c_x.Real * p.c_x.Real > p.c_bailout) && (p.c_x.Imaginary * p.c_x.Imaginary > p.c_bailout))) p.c_active = false;
                  if (!(fractdiv) && ((p.c_x.Real * p.c_x.Real < p.c_bailout) && (p.c_x.Imaginary * p.c_x.Imaginary < p.c_bailout))) p.c_active = false;
                }
                else if (p.c_convchktype == ConvCheck.MPL_CONVCHK_MANH)
                {
                  double ddd = (absval(p.c_x.Imaginary) + absval(p.c_x.Real)) * (absval(p.c_x.Imaginary) + absval(p.c_x.Real));
                  if ((fractdiv) && (ddd > p.c_bailout)) p.c_active = false;
                  if (!(fractdiv) && (ddd < p.c_bailout)) p.c_active = false;
                }
                else if (p.c_convchktype == ConvCheck.MPL_CONVCHK_MANR)
                {
                  double ddd = (p.c_x.Real + p.c_x.Imaginary) * (p.c_x.Real + p.c_x.Imaginary);
                  if ((fractdiv) && (ddd > p.c_bailout)) p.c_active = false;
                  if (!(fractdiv) && (ddd < p.c_bailout)) p.c_active = false;
                }
                else
                {
                  double ddd = norm(p.c_x);
                  if ((fractdiv) && (ddd > p.c_bailout)) p.c_active = false;
                  if (!(fractdiv) && (ddd < p.c_bailout)) p.c_active = false;
                }
                if (p.c_n > p.c_nlimit) { p.c_active = false; p.c_isin = true; }
                if (p.c_active == false)
                {
                  if (p.c_checktype == SeqCheck.MPL_CHECK_TRIANGLE_SMOOTH)
                  {
                    if (!p.c_isin)
                    {
                      p.c_oldx = p.c_x;
                      p.c_x = Fractal_Mandel(p.c_x, c);
                      p.c_n++;
                      double newxnorm = norm(p.c_oldx);
                      double lowbound = absval(newxnorm - trinorm);
                      if ((newxnorm + trinorm - lowbound) == 0) newd = 0;
                      else
                        newd = (p.c_x.Magnitude - lowbound) / (newxnorm + trinorm - lowbound);
                      p.c_calc += newd;
                      double oldsum = p.c_calc / (p.c_n + 1);
                      double il2 = 1 / Math.Log(2);
                      double lp = Math.Log(Math.Log(p.c_bailout));
                      double f = il2 * lp - il2 * Math.Log(Math.Log(Complex.Abs(p.c_x))) + 2;
                      double az2 = norm(p.c_x);
                      p.c_oldx = p.c_x;
                      p.c_x = Fractal_Mandel(p.c_oldx, c);
                      lowbound = absval(az2 - trinorm);
                      if ((az2 + trinorm - lowbound) != 0) p.c_calc += (Complex.Abs(p.c_x) - lowbound) / (az2 + trinorm - lowbound);
                      p.c_n++;
                      p.c_calc = p.c_calc / (p.c_n + 1);
                      p.c_calc = oldsum + (p.c_calc - oldsum) * (f - 1);
                    }
                    else
                    {
                      p.c_calc /= p.c_n + 1;
                    }
                  }
                  else if (p.c_checkseqtype == SeqType.MPL_SEQ_MEAN)
                  {
                    p.c_calc /= p.c_n + 1;
                  }
                }
                  }
                }
                x = newx;
                if (!deflayer.c_active)
                {
                  end = true;
                }
                  }
                }
                else
                {
                  throw new NotImplementedException("No default layer found");
                }
            }