Example #1
0
        public void GetMatrixTest()
        {
            var matrix = Walsh.GetMatrix(0);

            Assert.AreEqual(1, matrix.GetLength(0));
            Assert.AreEqual(1, matrix.GetLength(1));
            Assert.AreEqual(1, matrix[0, 0]);

            matrix = Walsh.GetMatrix(1);
            Assert.AreEqual(2, matrix.GetLength(0));
            Assert.AreEqual(2, matrix.GetLength(1));
            Assert.AreEqual(new sbyte[, ] {
                { 1, 1 }, { 1, -1 }
            }, matrix);

            matrix = Walsh.GetMatrix(2);
            Assert.AreEqual(4, matrix.GetLength(0));
            Assert.AreEqual(4, matrix.GetLength(1));
            Assert.AreEqual(new sbyte[, ] {
                { 1, 1, 1, 1 }, { 1, 1, -1, -1 }, { 1, -1, 1, -1 }, { 1, -1, -1, 1 }
            }, matrix);

            matrix = Walsh.GetMatrix(3);
            Assert.AreEqual(8, matrix.GetLength(0));
            Assert.AreEqual(8, matrix.GetLength(1));
            Assert.AreEqual(-1, matrix[5, 4]);
        }
        /// <summary>
        /// coeff(n)=c_{1,n}(f), n > 0
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        private double coeff(Func <double, double> f, int n)
        {
            // we use formula c_{1,n}(f) = c_n(f')
            --n;
            if (n == 0)
            {
                return(f(1) - f(0));
            }

            var k     = (int)Log(n, 2) + 1;
            var w     = Walsh.GetMatrix(k);
            var pow2k = Pow(2, k);
            var s     = 0d;

            for (int j = 0; j <= pow2k - 1; j++)
            {
                s += w[n, j] * (f((j + 1) / pow2k) - f(j / pow2k));
            }

            return(s);
        }