Beispiel #1
0
 public DiffFollow(DiffMethod method)
 {
     this.Follow = method;
     this.Add = Flag.Check((int)method, (int)DiffMethod.Add);
     this.Modify = Flag.Check((int)method, (int)DiffMethod.Modify);
     this.Remove = Flag.Check((int)method, (int)DiffMethod.Remove);
 }
Beispiel #2
0
        public async Task DiffCreateAndApply(byte[] src, byte[] dest, DiffMethod method)
        {
            await using var ms = new MemoryStream();
            switch (method)
            {
            case DiffMethod.Default:
                await Utils.CreatePatch(src, dest, ms);

                break;

            case DiffMethod.BSDiff:
                BSDiff.Create(src, dest, ms);
                break;

            case DiffMethod.OctoDiff:
                OctoDiff.Create(src, dest, ms);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(method), method, null);
            }

            ms.Position = 0;
            var patch = ms.ToArray();

            await using var resultStream = new MemoryStream();
            Utils.ApplyPatch(new MemoryStream(src), () => new MemoryStream(patch), resultStream);
            Assert.Equal(dest, resultStream.ToArray());
        }
Beispiel #3
0
 public DiffFollow(DiffMethod method)
 {
     this.Follow = method;
     this.Add    = Flag.Check((int)method, (int)DiffMethod.Add);
     this.Modify = Flag.Check((int)method, (int)DiffMethod.Modify);
     this.Remove = Flag.Check((int)method, (int)DiffMethod.Remove);
 }
Beispiel #4
0
 public Column(string name, Type type, Getter getter, DiffMethod diffMethod, double colWidth)
 {
     this.name       = name;
     this.getter     = getter;
     this.colWidth   = colWidth;
     this.type       = type;
     this.diffMethod = diffMethod;
 }
 /// <summary>
 /// Constructs a new Hybrid Monte Carlo sampler.
 /// </summary>
 /// <param name="x0">The initial sample.</param>
 /// <param name="pdfLnP">The log density of the distribution we want to sample from.</param>
 /// <param name="frogLeapSteps">Number frogleap simulation steps.</param>
 /// <param name="stepSize">Size of the frogleap simulation steps.</param>
 /// <param name="burnInterval">The number of iterations in between returning samples.</param>
 /// <param name="diff">The method used for differentiation.</param>
 /// <exception cref="ArgumentOutOfRangeException">When the number of burnInterval iteration is negative.</exception>
 /// <exception cref="ArgumentNullException">When either x0, pdfLnP or diff is null.</exception>
 public HybridMCGeneric(T x0, DensityLn <T> pdfLnP, int frogLeapSteps, double stepSize, int burnInterval, DiffMethod diff)
 {
     Energy        = new DensityLn <T>(x => - pdfLnP(x));
     FrogLeapSteps = frogLeapSteps;
     StepSize      = stepSize;
     BurnInterval  = burnInterval;
     mCurrent      = x0;
     Diff          = diff;
 }
Beispiel #6
0
 /// <summary>
 /// Constructs a new Hybrid Monte Carlo sampler.
 /// </summary>
 /// <param name="x0">The initial sample.</param>
 /// <param name="pdfLnP">The log density of the distribution we want to sample from.</param>
 /// <param name="frogLeapSteps">Number frogleap simulation steps.</param>
 /// <param name="stepSize">Size of the frogleap simulation steps.</param>
 /// <param name="burnInterval">The number of iterations in between returning samples.</param>
 /// <param name="randomSource">Random number generator used for sampling the momentum.</param>
 /// <param name="diff">The method used for differentiation.</param>
 /// <exception cref="ArgumentOutOfRangeException">When the number of burnInterval iteration is negative.</exception>
 /// <exception cref="ArgumentNullException">When either x0, pdfLnP or diff is null.</exception>
 protected HybridMCGeneric(T x0, DensityLn <T> pdfLnP, int frogLeapSteps, double stepSize, int burnInterval, Random randomSource, DiffMethod diff)
 {
     _energy       = x => - pdfLnP(x);
     FrogLeapSteps = frogLeapSteps;
     StepSize      = stepSize;
     BurnInterval  = burnInterval;
     Current       = x0;
     _diff         = diff;
     RandomSource  = randomSource;
 }
Beispiel #7
0
    public TaskSolution Solve(DiffMethod method, double a, double b, int n)
    {
        TablesManager manager = new TablesManager();

        t0Table    = manager.T0Table;
        mTable     = manager.MTable;
        nTable     = manager.NTable;
        sigmaTable = manager.SigmaTable;

        DiffEquationSys deSys = null;

        switch (method)
        {
        case DiffMethod.RungeKutta:
            deSys = new RungeKuttaDiffEquationSys(new DiffEquationSys.SeveralArgFun[]
            {
                (x, y) => ((y[1] - (Rk + Rp(y[0])) * y[0]) / Lk),
                (x, y) => (-y[0] / Ck)
            });
            break;

        case DiffMethod.ImplTrap:
            deSys = new ImplTrapDiffEquationSys(new DiffEquationSys.SeveralArgFun[]
            {
                (x, y) => ((y[1] - (Rk + Rp(y[0])) * y[0]) / Lk),
                (x, y) => (-y[0] / Ck)
            });
            break;
        }

        DiffEquationSolution[] sysSolution = deSys.FindSolution(a, b, new double[] { I0, Uc0 }, n);
        DiffEquationSolution   rpSolution  = new DiffEquationSolution(a, b, n);

        for (int i = 0; i <= n; i++)
        {
            rpSolution.Y[i] = Rp(sysSolution[0].Y[i]);
        }
        DiffEquationSolution ucpSolution = new DiffEquationSolution(a, b, n);

        for (int i = 0; i <= n; i++)
        {
            ucpSolution.Y[i] = rpSolution.Y[i] * sysSolution[0].Y[i];
        }

        TaskSolution taskSolution;

        taskSolution.I   = sysSolution[0];
        taskSolution.Uc  = sysSolution[1];
        taskSolution.Rp  = rpSolution;
        taskSolution.Ucp = ucpSolution;

        return(taskSolution);
    }
Beispiel #8
0
        /// <summary>
        /// Constructs a new Hybrid Monte Carlo sampler for a multivariate probability distribution.
        /// The components of the momentum will be sampled from a normal distribution with standard deviations
        /// given by pSdv. This constructor will set the burn interval, the method used for
        /// numerical differentiation and the random number generator.
        /// </summary>
        /// <param name="x0">The initial sample.</param>
        /// <param name="pdfLnP">The log density of the distribution we want to sample from.</param>
        /// <param name="frogLeapSteps">Number frogleap simulation steps.</param>
        /// <param name="stepSize">Size of the frogleap simulation steps.</param>
        /// <param name="burnInterval">The number of iterations in between returning samples.</param>
        /// <param name="pSdv">The standard deviations of the normal distributions that are used to sample
        /// the components of the momentum.</param>
        /// <param name="randomSource">Random number generator used for sampling the momentum.</param>
        /// <param name="diff">The method used for numerical differentiation.</param>
        /// <exception cref="ArgumentOutOfRangeException">When the number of burnInterval iteration is negative.</exception>
        /// <exception cref="ArgumentOutOfRangeException">When the length of pSdv is not the same as x0.</exception>
        public HybridMC(double[] x0, DensityLn <double[]> pdfLnP, int frogLeapSteps, double stepSize, int burnInterval, double[] pSdv, System.Random randomSource, DiffMethod diff)
            : base(x0, pdfLnP, frogLeapSteps, stepSize, burnInterval, randomSource, diff)
        {
            _length        = x0.Length;
            MomentumStdDev = pSdv;

            Initialize(x0);

            Burn(BurnInterval);
        }
Beispiel #9
0
        /// <summary>
        /// Constructs a new Hybrid Monte Carlo sampler for a multivariate probability distribution.
        /// The components of the momentum will be sampled from a normal distribution with standard deviations
        /// given by pSdv using the default <see cref="System.Random"/> random
        /// number generator.  This constructor will set both the burn interval and the method used for
        /// numerical differentiation.
        /// </summary>
        /// <param name="x0">The initial sample.</param>
        /// <param name="pdfLnP">The log density of the distribution we want to sample from.</param>
        /// <param name="frogLeapSteps">Number frogleap simulation steps.</param>
        /// <param name="stepSize">Size of the frogleap simulation steps.</param>
        /// <param name="burnInterval">The number of iterations in between returning samples.</param>
        /// <param name="pSdv">The standard deviations of the normal distributions that are used to sample
        /// the components of the momentum.</param>
        /// <param name="diff">The method used for numerical differentiation.</param>
        /// <exception cref="ArgumentOutOfRangeException">When the number of burnInterval iteration is negative.</exception>
        /// <exception cref="ArgumentOutOfRangeException">When the length of pSdv is not the same as x0.</exception>
        public HybridMC(double[] x0, DensityLn <double[]> pdfLnP, int frogLeapSteps, double stepSize, int burnInterval, double[] pSdv, DiffMethod diff)
            : base(x0, pdfLnP, frogLeapSteps, stepSize, burnInterval, diff)
        {
            Length         = x0.Count();
            MomentumStdDev = pSdv;

            Initialize(x0);

            Burn(BurnInterval);
        }
 /// <summary>
 /// Constructs a new Hybrid Monte Carlo sampler for a multivariate probability distribution.
 /// The momentum will be sampled from a normal distribution with standard deviation
 /// given by pSdv using a random
 /// number generator provided by the user.  This constructor will set both the burn interval and the method used for
 /// numerical differentiation.
 /// </summary>
 /// <param name="x0">The initial sample.</param>
 /// <param name="pdfLnP">The log density of the distribution we want to sample from.</param>
 /// <param name="frogLeapSteps">Number frogleap simulation steps.</param>
 /// <param name="stepSize">Size of the frogleap simulation steps.</param>
 /// <param name="burnInterval">The number of iterations in between returning samples.</param>
 /// <param name="pSdv">The standard deviation of the normal distribution that is used to sample
 /// the momentum.</param>
 /// <param name="diff">The method used for numerical differentiation.</param>
 /// <param name="randomSource">Random number generator used for sampling the momentum.</param>
 /// <exception cref="ArgumentOutOfRangeException">When the number of burnInterval iteration is negative.</exception>
 public UnivariateHybridMC(double x0, DensityLn <double> pdfLnP, int frogLeapSteps, double stepSize, int burnInterval, double pSdv, System.Random randomSource, DiffMethod diff)
     : base(x0, pdfLnP, frogLeapSteps, stepSize, burnInterval, randomSource, diff)
 {
     MomentumStdDev = pSdv;
     _distribution  = new Normal(0.0, MomentumStdDev, RandomSource);
     Burn(BurnInterval);
 }
Beispiel #11
0
 /// <summary>
 /// Parses the command-line options
 /// </summary>
 /// <param name="options">
 /// The program options array
 /// </param>
 /// <returns>
 /// True if the arguments are valid
 /// False otherwise
 /// </returns>
 static Boolean ParseOptions(String[] options)
 {
     // initialize options
      password = "";
      diffMethod = DiffMethod.Timestamp;
      sourcePaths = new List<IO.Path>();
      includeFilter = new List<Regex>();
      excludeFilter = new List<Regex>();
      deleteArchive = false;
      maxRetries = 5;
      maxFailures = 5;
      checkpointLength = 1024;
      rateLimit = Int32.MaxValue / 1024;
      compress = false;
      // parse options
      try
      {
     new Options.OptionSet()
     {
        { "c|connect=", v => connectionString = v },
        { "a|archive=", v => archiveName = v },
        { "p|password="******"r|max-retry=", (Int32 v) => maxRetries = v },
        { "f|max-fail=", (Int32 v) => maxFailures = v },
        { "s|source=", v => sourcePaths.Add((IO.Path)v) },
        { "n|include=", v => includeFilter.Add(new Regex(v, RegexOptions.IgnoreCase)) },
        { "x|exclude=", v => excludeFilter.Add(new Regex(v, RegexOptions.IgnoreCase)) },
        { "k|delete", v => deleteArchive = (v != null) },
        { "d|diff=", (DiffMethod v) => diffMethod = v },
        { "t|checkpoint=", (Int32 v) => checkpointLength = v },
        { "l|rate=", (Int32 v) => rateLimit = v },
        { "z|compress", v => compress = (v != null) },
     }.Parse(options);
      }
      catch { return false; }
      // validate options
      if (String.IsNullOrWhiteSpace(connectionString))
     return false;
      if (String.IsNullOrWhiteSpace(archiveName))
     return false;
      if (maxRetries < 0)
     return false;
      if (maxFailures < 0)
     return false;
      if (!sourcePaths.Any())
     sourcePaths.Add(IO.Path.Current);
      if (sourcePaths.Any(p => String.IsNullOrWhiteSpace(p)))
     return false;
      if (diffMethod == 0)
     return false;
      if (checkpointLength <= 0)
     return false;
      if (rateLimit <= 0)
     return false;
      return true;
 }
Beispiel #12
0
        /// <summary>
        /// Diff the given columns of two given files using a given tolerance and method reporting any information back to the StringBuilder passed by ref
        /// </summary>
        /// <param name="origfile">the original file</param>
        /// <param name="newfile">the new file to diff against the original file</param>
        /// <param name="colnames">the colnames to focus on</param>
        /// <param name="tolerance">the toleracne to use (absolute value/dp/%age depedning on 'method')</param>
        /// <param name="method">the method to use when diffing (absolute value/dp/%age)</param>
        /// <param name="output">a StringBuilder for reporting</param>
        /// <returns>true if files are approximately the same (within the given tolerance)</returns>
        internal static bool Diff(InputFile origfile, InputFile newfile, List <string> colnames, decimal tolerance, DiffMethod method, ref StringBuilder output)
        {
            Dictionary <string, int>
            orig_lookup    = new Dictionary <string, int>(),
                new_lookup = new Dictionary <string, int>();

            List <string>
            orig_cols    = new List <string>(origfile.ColNames),
                new_cols = new List <string>(newfile.ColNames);

            //basic little factory for setting up our comparer object
            CompareMethod comparer =
                method == DiffMethod.pct ? (CompareMethod) new PctCompare(tolerance) :
                method == DiffMethod.abs ? (CompareMethod) new AbsCompare(tolerance) :
                (CompareMethod) new DPCompare(tolerance);

            //set up a lookup dictionary to translate between column name and index
            foreach (string col in colnames)
            {
                orig_lookup.Add(col, orig_cols.IndexOf(col));
                new_lookup.Add(col, new_cols.IndexOf(col));
            }

            //grab the raw data to diff
            object[][]
            origdata = origfile.GetData(true),
            newdata = newfile.GetData(true);

            string result = "";

            if (origdata.Length != newdata.Length)
            {
                result += "The 2 files had different lengths, diff will end at bottom of shortest file" + Environment.NewLine;
            }

            object diff;

            int
                badhits  = 0,
                ibadline = -1,
                numlines = (int)Math.Min(origdata.Length, newdata.Length);

            //for each line in our files
            for (int i = 0; i < numlines; i++)
            {
                //for each column of interest
                foreach (string col in colnames)
                {
                    //if we don't have a 'match' (comparer.match takes care of the tolerances for us)
                    if (!comparer.match(origdata[i][orig_lookup[col]], newdata[i][new_lookup[col]], out diff))
                    {
                        badhits++;
                        //if we haven't picked up any fails on this line yet
                        if (ibadline != i)
                        {
                            //if this isn't the first line of our report then make sure we add a newline
                            if (ibadline >= 0)
                            {
                                output.AppendLine();
                            }
                            //if it is the first line then make sure we make it clear the file failed
                            else
                            {
                                output.AppendLine("FAIL");
                            }

                            //print the line number (remember this is the first fail for this line)
                            output.AppendFormat("\t{0,-4}:", i);
                            ibadline = i;
                        }

                        //print out the column name of the value that failed, along with the difference
                        output.AppendFormat("\t{0}:{1}", col, diff);
                    }
                }
            }

            if (badhits == 0)
            {
                output.AppendLine("PASS");
            }

            return(badhits == 0);
        }