Esempio n. 1
0
        [Test] public void Insert()
        {
            IntArrayList list = new IntArrayList();

            for (int i = 0; i < 256; i++)
            {
                list.Insert(0, i);
            }

            AssertEquals(256, list.Count);
            for (int i = 0; i < 256; i++)
            {
                AssertEquals(255 - i, list [i]);
                AssertEquals(i, list.IndexOf(255 - i));
            }
        }
Esempio n. 2
0
        public void TestIntArrayListInsertElementPastCapacity()
        {
            IntArrayList array = new IntArrayList();

            for (int i = 0; i < array.Capacity; i++)
            {
                array.Add(i);
            }

            try
            {
                array.Insert(array.Capacity + 3, 0);
            }
            catch (ArgumentOutOfRangeException exc_)
            {
                Console.WriteLine("Caught necessary exception: " + exc_.Message);
            }
        }
        public void Decompose(DoubleMatrix2D A)
        {
            int CUT_OFF = 10;

            // setup
            LU = A;
            int m = A.Rows;
            int n = A.Columns;

            // setup pivot vector
            if (this.piv == null || this.piv.Length != m)
            {
                this.piv = new int[m];
            }
            for (int i = m; --i >= 0;)
            {
                piv[i] = i;
            }
            pivsign = 1;

            if (m * n == 0)
            {
                LU = LU;
                return; // nothing to do
            }

            //precompute and cache some views to avoid regenerating them time and again
            DoubleMatrix1D[] LUrows = new DoubleMatrix1D[m];
            for (int i = 0; i < m; i++)
            {
                LUrows[i] = LU.ViewRow(i);
            }

            IntArrayList   nonZeroIndexes = new IntArrayList();      // sparsity
            DoubleMatrix1D LUcolj         = LU.ViewColumn(0).Like(); // blocked column j

            Cern.Jet.Math.Mult multFunction = Cern.Jet.Math.Mult.CreateInstance(0);

            // Outer loop.
            for (int j = 0; j < n; j++)
            {
                // blocking (make copy of j-th column to localize references)
                LUcolj.Assign(LU.ViewColumn(j));

                // sparsity detection
                int maxCardinality = m / CUT_OFF; // == heuristic depending on speedup
                LUcolj.GetNonZeros(nonZeroIndexes, null, maxCardinality);
                int     cardinality = nonZeroIndexes.Count;
                Boolean sparse      = (cardinality < maxCardinality);

                // Apply previous transformations.
                for (int i = 0; i < m; i++)
                {
                    int    kmax = System.Math.Min(i, j);
                    double s;
                    if (sparse)
                    {
                        s = LUrows[i].ZDotProduct(LUcolj, 0, kmax, nonZeroIndexes);
                    }
                    else
                    {
                        s = LUrows[i].ZDotProduct(LUcolj, 0, kmax);
                    }
                    double before = LUcolj[i];
                    double after  = before - s;
                    LUcolj[i] = after; // LUcolj is a copy
                    LU[i, j]  = after; // this is the original
                    if (sparse)
                    {
                        if (before == 0 && after != 0)
                        { // nasty bug fixed!
                            int pos = nonZeroIndexes.BinarySearch(i);
                            pos = -pos - 1;
                            nonZeroIndexes.Insert(pos, i);
                        }
                        if (before != 0 && after == 0)
                        {
                            nonZeroIndexes.Remove(nonZeroIndexes.BinarySearch(i));
                        }
                    }
                }

                // Find pivot and exchange if necessary.
                int p = j;
                if (p < m)
                {
                    double max = System.Math.Abs(LUcolj[p]);
                    for (int i = j + 1; i < m; i++)
                    {
                        double v = System.Math.Abs(LUcolj[i]);
                        if (v > max)
                        {
                            p   = i;
                            max = v;
                        }
                    }
                }
                if (p != j)
                {
                    LUrows[p].Swap(LUrows[j]);
                    int k = piv[p]; piv[p] = piv[j]; piv[j] = k;
                    pivsign = -pivsign;
                }

                // Compute multipliers.
                double jj;
                if (j < m && (jj = LU[j, j]) != 0.0)
                {
                    multFunction.Multiplicator = 1 / jj;
                    LU.ViewColumn(j).ViewPart(j + 1, m - (j + 1)).Assign(multFunction);
                }
            }
            LU = LU;
        }