public override void SetQuick(int row, int column, double value)
        {
            int i = row;
            int j = column;

            int          k         = -1;
            IntArrayList indexList = indexes[i];

            if (indexList != null)
            {
                k = indexList.BinarySearch(j);
            }

            if (k >= 0)
            { // found
                if (value == 0)
                {
                    List <Double> valueList = values[i];
                    indexList.Remove(k);
                    valueList.Remove(k);
                    int s = indexList.Count;
                    if (s > 2 && s * 3 < indexList.ToArray().Length)
                    {
                        indexList.SetSize(s * 3 / 2);
                        indexList.TrimToSize();
                        indexList.SetSize(s);

                        valueList.SetSize(s * 3 / 2);
                        valueList.TrimExcess();
                        valueList.SetSize(s);
                    }
                }
                else
                {
                    values[i][k] = value;
                }
            }
            else
            { // not found
                if (value == 0)
                {
                    return;
                }

                k = -k - 1;

                if (indexList == null)
                {
                    indexes[i] = new IntArrayList(3);
                    values[i]  = new List <Double>(3);
                }
                indexes[i].Insert(k, j);
                values[i].Insert(k, value);
            }
        }
示例#2
0
        [Test] public void BinarySearchTests()
        {
            IntArrayList list = new IntArrayList();

            int[] sorted = new int[] { 0, 0, 1, 2, 3, 4, 6, 7, 8, 8, 8, 8, 8, 8, 9, 10 };
            list.AddRange(sorted);
            AssertEquals(Array.BinarySearch(sorted, 5), list.BinarySearch(5));
            AssertEquals(Array.BinarySearch(sorted, 11), list.BinarySearch(11));
            AssertEquals(Array.BinarySearch(sorted, 8), list.BinarySearch(8));
            AssertEquals(Array.BinarySearch(sorted, 0), list.BinarySearch(0));
            AssertEquals(Array.BinarySearch(sorted, 10), list.BinarySearch(10));
            AssertEquals(Array.BinarySearch(sorted, -1), list.BinarySearch(-1));
            AssertEquals(Array.BinarySearch(sorted, 100), list.BinarySearch(100));
            list.Clear();
            AssertEquals(Array.BinarySearch(new int[] {}, 5), list.BinarySearch(5));
        }
示例#3
0
        public void SequentialInsertDeleteGetAllKeys()
        {
            IntHashSet numbers = new IntHashSet();
            const int queueSize = 3000;
            const int cacheSize = 32;
            TestKey keyFactory = new TestKey();
            IBTree bTree = new /*BTree*/OmniaMeaBTree( _indexFileName, keyFactory );
            bTree.SetCacheSize( cacheSize );
            using( bTree )
            {
                bTree.Open();
                int time = System.Environment.TickCount;
                Queue queue = new Queue( queueSize );
                const int cycles = 20;

                for ( int i = 0; i < queueSize * cacheSize; i++ )
                {
                    TestKey testKey = new TestKey( GetUniqueRand( numbers ) );
                    bTree.InsertKey( testKey, (int)testKey.Key );
                }

                for( int count = 0; count < cycles; ++count )
                {
                    for ( int i = 0; i < queueSize; i++ )
                    {
                        TestKey testKey = new TestKey( GetUniqueRand( numbers ) );
                        queue.Enqueue( testKey );
                        bTree.InsertKey( testKey, (int)testKey.Key );
                    }
                    int count_ = bTree.Count;
                    bTree.Close();
                    bTree.Open();
                    ArrayList keys = new ArrayList( count_ );
                    IntArrayList ints = new IntArrayList( count_ );
                    bTree.GetAllKeys( keys );
                    if( keys.Count != count_ || bTree.Count != count_ )
                    {
                        throw new Exception(
                            "keys.Count = " +  keys.Count + ", bTree.Count = " + bTree.Count + ", count_ = " + count_ );
                    }
                    foreach( KeyPair pair in keys )
                    {
                        ints.Add( (int) pair._key.Key );
                    }
                    for ( int i = 0; i < queueSize - 1; i++ )
                    {
                        TestKey testKey = (TestKey)queue.Dequeue();
                        int key = (int)testKey.Key;
                        bTree.DeleteKey( testKey, key );
                        numbers.Remove( key );
                        if( ints.BinarySearch( key ) < 0 )
                        {
                            throw new Exception( "The ints array doesn't contain removed key" );
                        }
                    }
                    queue.Clear();
                }

                Assert.AreEqual( cycles + queueSize * cacheSize, bTree.Count );

                time = System.Environment.TickCount - time;
                Console.WriteLine( " work took " + time.ToString() );

                bTree.Close();
            }
        }
        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;
        }