Esempio n. 1
0
        public void SetTest()
        {
            var s = new Sparse <double>();

            s[0]  = 1;
            s[99] = 99;
            s[10] = 42;

            double[] d = s.ToDense();

            for (int i = 0; i < 100; i++)
            {
                if (i == 0)
                {
                    Assert.AreEqual(1, s[i]);
                    Assert.AreEqual(1, d[i]);
                }
                else if (i == 10)
                {
                    Assert.AreEqual(42, s[i]);
                    Assert.AreEqual(42, d[i]);
                }
                else if (i == 99)
                {
                    Assert.AreEqual(99, s[i]);
                    Assert.AreEqual(99, d[i]);
                }
                else
                {
                    Assert.AreEqual(0, s[i]);
                    Assert.AreEqual(0, d[i]);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Elementwise multiplication of scalar a and vector b, storing in result.
        /// </summary>
        /// <param name="a">The scalar to be multiplied.</param>
        /// <param name="b">The vector to be multiplied.</param>
        /// <param name="result">An array to store the result.</param>
        public void Product(double a, Sparse <double> b, Sparse <double> result)
        {
            int  n   = result.Indices.Length;
            bool seq = true;

            for (int i = 0; i < result.Indices.Length; i++)
            {
                if (result.Indices[i] != i)
                {
                    seq = false;
                    break;
                }
            }

            int m   = b.Indices.Length;
            int max = Math.Max(result.Indices[n - 1], b.Indices[m - 1]);

            if (!seq || result.Indices[n - 1] < b.Indices[m - 1])
            {
                result.Values  = result.ToDense(max + 1);
                result.Indices = Vector.Range(max + 1);
            }

            for (int j = 0; j < b.Indices.Length; j++)
            {
                int i = b.Indices[j];
                result.Values[i] += a * b.Values[j];
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Elementwise multiplication of scalar a and vector b, storing in result.
        /// </summary>
        /// <param name="a">The scalar to be multiplied.</param>
        /// <param name="b">The vector to be multiplied.</param>
        /// <param name="accumulate">An array to store the result.</param>
        public void Product(double a, Sparse <double> b, Sparse <double> accumulate)
        {
            // TODO: Move those implementations to extension methods in the Sparse class.

            int  n   = accumulate.Indices.Length;
            bool seq = true;

            for (int i = 0; i < accumulate.Indices.Length; i++)
            {
                if (accumulate.Indices[i] != i)
                {
                    seq = false;
                    break;
                }
            }

            int m   = b.Indices.Length;
            int max = Math.Max(accumulate.Indices[n - 1], b.Indices[m - 1]);

            if (!seq || accumulate.Indices[n - 1] < b.Indices[m - 1])
            {
                accumulate.Values  = accumulate.ToDense(max + 1);
                accumulate.Indices = Vector.Range(max + 1);
            }

            for (int j = 0; j < b.Indices.Length; j++)
            {
                int i = b.Indices[j];
                accumulate.Values[i] += a * b.Values[j];
            }
        }