Пример #1
0
 InitDistributions()
 {
     _uniformDistribution = new ContinuousUniformDistribution(RandomSource);
     _uniformDistribution.SetDistributionParameters(-Constants.Pi_2, Constants.Pi_2);
     _exponentialDistribution = new ExponentialDistribution(RandomSource);
     _exponentialDistribution.SetDistributionParameters(1.0);
 }
        public void TestOptimalVariance()
        {
            ContinuousUniformDistribution dist = new ContinuousUniformDistribution(0, 10);
            Histogram h = Histogram.OptimalVariance(10, new List<double>(dist.EnumerateDoubles(1000)));

            Assert.That(h.Count, Is.EqualTo(10));

            Console.WriteLine(h.ToString());
        }
        public void TestOptimalSquaredFreedom()
        {
            ContinuousUniformDistribution dist = new ContinuousUniformDistribution(0, 10);
            Histogram h = Histogram.OptimalSquaredFreedom(10, new List<double>(dist.EnumerateDoubles(1000)));

            h.JoinBuckets();
            Assert.That(h.GetContainerOf(8).LowerBound, Is.LessThanOrEqualTo(8.0));
            Assert.That(h.GetContainerOf(8).UpperBound, Is.GreaterThanOrEqualTo(8.0));
            Assert.That(h.GetContainerIndexOf(11), Is.LessThan(0.0));
            Assert.That(h.GetContainerIndexOf(-1), Is.LessThan(0.0));
            Assert.That(delegate { h.GetContainerOf(11); }, Throws.TypeOf(typeof(ArgumentException)));
            Assert.That(delegate { h.GetContainerOf(-1); }, Throws.TypeOf(typeof(ArgumentException)));
            Assert.That(h.Count, Is.EqualTo(10));

            Console.WriteLine(h.ToString());
        }
Пример #4
0
 void InitDistributions()
 {
     _uniformDistribution = new ContinuousUniformDistribution(RandomSource);
     _uniformDistribution.SetDistributionParameters(-Constants.Pi_2, Constants.Pi_2);
     _exponentialDistribution = new ExponentialDistribution(RandomSource);
     _exponentialDistribution.SetDistributionParameters(1.0);
 }
        public void MatrixArrayArithmetics()
        {
            IContinuousGenerator uniform = new ContinuousUniformDistribution(-1, 2);

            Matrix r = Matrix.Random(3, 4, uniform);
            Matrix r2 = Matrix.Random(3, 4, uniform);

            // add/subtraction with non-matching sizes should fail
            Assert.That(delegate { Matrix m = r - Matrix.Zeros(4); }, Throws.TypeOf<ArgumentException>());
            Assert.That(delegate { Matrix m = r + Matrix.Zeros(4); }, Throws.TypeOf<ArgumentException>());
            Assert.That(delegate { r.SubtractInplace(Matrix.Zeros(4)); }, Throws.TypeOf<ArgumentException>());
            Assert.That(delegate { r.AddInplace(Matrix.Zeros(4)); }, Throws.TypeOf<ArgumentException>());

            // subtraction & addition
            Matrix a = r - r2;
            Assert.That(a + r2, NumericIs.AlmostEqualTo(r));
            a.AddInplace(r2);
            Assert.That(a, NumericIs.AlmostEqualTo(r));

            // subtraction with itself should be zero
            Assert.That((r - r).Norm1(), Is.EqualTo(0.0), "Subtract I: difference of identical Matrices is nonzero,\nSubsequent use of Subtract should be suspect");
            Assert.That((r.Clone() - r).Norm1(), Is.EqualTo(0.0), "Subtract II: difference of identical Matrices is nonzero,\nSubsequent use of Subtract should be suspect");
            Matrix b = r.Clone();
            b.SubtractInplace(r);
            Assert.That(b.Norm1(), Is.EqualTo(0.0), "Subtract III: difference of identical Matrices is nonzero,\nSubsequent use of Subtract should be suspect");
            Assert.That(b, Is.EqualTo(new Matrix(r.RowCount, r.ColumnCount)));

            // addition with negative of itself should be zero
            Matrix c = r.Clone();
            c.NegateInplace();
            Assert.That(c, Is.EqualTo(-r));
            Assert.That((c + r).Norm1(), Is.EqualTo(0.0));

            // array-division
            Assert.That(delegate { Matrix.ArrayDivide(Matrix.Ones(3), Matrix.Ones(4)); }, Throws.TypeOf<ArgumentException>());
            Assert.That(delegate { r.ArrayDivideInplace(Matrix.Ones(4)); }, Throws.TypeOf<ArgumentException>());
            Assert.That(Matrix.ArrayDivide(Matrix.Zeros(4), Matrix.Ones(4)), Is.EqualTo(Matrix.Zeros(4)));
            Matrix d = r.Clone();
            d.ArrayDivideInplace(r);
            Assert.That(d, NumericIs.AlmostEqualTo(new Matrix(r.RowCount, r.ColumnCount, 1.0)));

            // array-multiplication
            Assert.That(delegate { Matrix.ArrayMultiply(Matrix.Ones(3), Matrix.Ones(4)); }, Throws.TypeOf<ArgumentException>());
            Assert.That(delegate { r.ArrayMultiplyInplace(Matrix.Ones(4)); }, Throws.TypeOf<ArgumentException>());
            Assert.That(Matrix.ArrayMultiply(r, new Matrix(r.RowCount, r.ColumnCount, 1.0)), Is.EqualTo(r));
            Matrix e = r.Clone();
            e.ArrayMultiplyInplace(new Matrix(r.RowCount, r.ColumnCount));
            Assert.That(e, Is.EqualTo(new Matrix(r.RowCount, r.ColumnCount)));

            // mixed array multiplication & division
            Matrix f = r.Clone();
            f.ArrayMultiplyInplace(r2);
            f.ArrayDivideInplace(r2);
            Assert.That(f, NumericIs.AlmostEqualTo(r));
        }
        static void BuildLinearCase(int start, int stop, out double[] x, out double[] y, out double[] xtest, out double[] ytest)
        {
            const double yOffset = 2.0;
            int samples = stop - start + 1;
            ContinuousUniformDistribution uniform = new ContinuousUniformDistribution();

            // build linear samples
            x = new double[samples];
            y = new double[samples];
            for(int i = 0; i < x.Length; i++)
            {
                int z = start + i;
                x[i] = z;
                y[i] = z + yOffset; // arbitrary small y-axis offset
            }

            // build linear test vectors randomly between the sample points
            xtest = new double[samples+1];
            ytest = new double[samples+1];
            if(samples == 1)
            {
                xtest[0] = start - uniform.NextDouble();
                xtest[1] = start + uniform.NextDouble();
                ytest[0] = ytest[1] = start + yOffset;
            }
            else
            {
                for(int i = 0; i < xtest.Length; i++)
                {
                    double z = (i - 1) + uniform.NextDouble();
                    xtest[i] = z;
                    ytest[i] = z + yOffset;
                }
            }
        }