public double Evaluate(double[] arg, double[] parameters)
        {
            var combinedArg = new double[arg.Length + parameters.Length];

            arg.CopyTo(combinedArg, 0);
            parameters.CopyTo(combinedArg, arg.Length);
            return(compiledTerm.Evaluate(combinedArg));
        }
        public double Evaluate(IReadOnlyList <double> arg, IReadOnlyList <double> parameters)
        {
            Guard.NotNull(arg, nameof(arg));
            Guard.MustHold(arg.Count == Variables.Count, ErrorMessages.ArgLength);
            Guard.NotNull(parameters, nameof(parameters));
            Guard.MustHold(parameters.Count == Parameters.Count, ErrorMessages.GradLength);

            var combinedArg = arg.Concat(parameters).ToArray();

            return(compiledTerm.Evaluate(combinedArg));
        }
        public double Evaluate(double[] arg, double[] parameters)
        {
            var combinedArg = arg.Concat(parameters).ToArray();

            return(compiledTerm.Evaluate(combinedArg));
        }
Esempio n. 4
0
        static void RunBenchmark(TextWriter resultWriter, TextWriter logWriter)
        {
            var fac = new CsvHelper.CsvFactory();

            using (var csvWriter = fac.CreateWriter(resultWriter))
            {
                csvWriter.WriteHeader <BenchmarkResult>();

                int[] sizes = { 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000 };
                foreach (int sz in sizes)
                {
                    var row = new BenchmarkResult();

                    var termsCount = sz; row.NumberOfTerms = termsCount;
                    var varsCount  = sz; row.NumberOfVars = varsCount;
                    var grad       = new double[sz];


                    logWriter.WriteLine("Benchmark for {0} terms and {1} variables", termsCount, varsCount);

                    logWriter.Write("\tConstructing coefficients ...");
                    var coefficients = GenerateCoefficients(termsCount, varsCount);
                    logWriter.WriteLine(" done");

                    // generate variables
                    var vars = new Variable[varsCount];
                    for (var j = 0; j < sz; ++j)
                    {
                        vars[j] = new Variable();
                    }


                    logWriter.Write("\tGenerating input data ...");
                    var inputData = new double[1000][];
                    for (var j = 0; j < inputData.Length; ++j)
                    {
                        inputData[j] = RandomDoubles(varsCount);
                    }
                    logWriter.WriteLine(" done");
                    GC.Collect(GC.MaxGeneration, GCCollectionMode.Default, true);


                    ICompiledTerm compiledTerm = null;
                    row.CompileMilliseconds = MeasureMsec("Constructing compiled term", logWriter,
                                                          () => compiledTerm = ConstructTerm(coefficients, vars));

                    row.MillisecondsPerManualEval = MeasureMsecPerOp("manual evaluation",
                                                                     logWriter, inputData.Length, () => inputData.Sum(array => NativeEvaluate(coefficients, array)));

                    row.MillisecondsPerCompiledEval = MeasureMsecPerOp("AutoDiff compiled evaluation",
                                                                       logWriter, inputData.Length, () => inputData.Sum(array => compiledTerm.Evaluate(array)));

                    row.MillisecondsPerCompiledDiff = MeasureMsecPerOp("compiled differentiation",
                                                                       logWriter, inputData.Length, () =>
                    {
                        var sum = 0.0;
                        foreach (var array in inputData)
                        {
                            var val = compiledTerm.Differentiate(array, grad);
                            sum    += val + grad.Sum();
                        }
                        return(sum);
                    });

                    csvWriter.WriteRecord(row);
                }
            }
        }