Ejemplo n.º 1
0
        public long MeasurePoolParameter(int minArraySize, int maxPoolSize, int iter, ILArray <double> A, ILArray <double> B)
        {
            ILMemoryPool.Pool.Collect();
            ILMemoryPool.Pool.Reset(minArraySize, maxPoolSize);
            ILPerformer      mon = new ILPerformer();
            ILArray <double> C = ILMath.zeros(A.Dimensions);
            ILArray <double> t1, t2;

            mon.Tic();
            for (int i = 0; i < iter; i++)
            {
                t1 = A + B;
                t2 = C + t1;
                t1.Dispose();
                C.Dispose();
                C = t2;
            }
            mon.Toc();
            return(mon.Duration);
        }
Ejemplo n.º 2
0
        public void Test_Basic()
        {
            int errorCode = 0;

            try {
                ILMemoryPool.Pool.Reset(100, 200);
                ILArray <double> A    = ILMath.rand(30, 30);
                double[]         aArr = A.m_data;
                ILArray <double> B    = A.C;
                A.Dispose();
                // A's array should be in pool now - query
                double[]         retArr = ILMemoryPool.Pool.New <double>(B.Dimensions.NumberOfElements);
                ILArray <double> Res    = new ILArray <double>(retArr, B.Dimensions);
                if (!Res.Equals(B) || aArr != retArr)
                {
                    throw new Exception("the expected array was not returned from pool!");
                }
                // query again - should return new array
                aArr = ILMemoryPool.Pool.New <double>(B.Dimensions.NumberOfElements);
                if (aArr == retArr)
                {
                    throw new Exception("after reclaiming array should be removed from pool!");
                }
                // test if zero cleares everything;
                Res.Dispose();
                Res = ILMath.zeros(B.Dimensions);
                // Res should have the same array, but cleared ?
                if (Res.m_data != retArr)
                {
                    throw new Exception("unexpected array returned from pool");
                }
                B = new ILArray <double>(new double[30 * 30], 30, 30);
                if (!Res.Equals(B))
                {
                    throw new Exception("invalid values - ILMath.zeros should return zeros!");
                }
                Success();
            } catch (Exception e) {
                Error(errorCode, e.Message);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Determinant of square matrix
        /// </summary>
        /// <param name="A">square input matrix</param>
        /// <returns>determinant of A</returns>
        /// <remarks><para>The determinant is computed by decomposing A into upper and lower triangular part. Therefore LAPACK function ?getrf is used. <br />
        /// Due to the properties of determinants, det(a) is the same as det(L) * det(U),where det(L) can easily be extracted from the permutation indices returned from LU decomposition. det(U) - with U beeing an upper triangular matrix - equals the product of the diagonal elements.</para>
        /// <para>For scalar A, a plain copy of A is returned.</para></remarks>
        /// <example>Creating a nonsingular 4x4 (double) matrix and it's determinant
        /// <code>ILArray&lt;double&gt; A = ILMath.counter(1.0,1.0,4,4);
        ///A[1] = 0.0;  // make A nonsingular
        ///A[14] = 0.0; //(same as: A[2,3] = 0.0;)
        /// // A is now:
        /// //&lt;Double&gt; [4,4]
        /// //(:,:) 1e+001 *
        /// // 0,10000   0,50000   0,90000   1,30000
        /// // 0,00000   0,60000   1,00000   1,40000
        /// // 0,30000   0,70000   1,10000   0,00000
        /// // 0,40000   0,80000   1,20000   1,60000
        ///
        ///ILMath.det(A) gives:
        /// //&lt;Double&gt; -360
        ///</code></example>
        ///<exception cref="ILNumerics.Exceptions.ILArgumentException">if A is empty or not a square matrix</exception>
        public static /*!HC:inCls1*/ ILArray <double> det(/*!HC:inCls1*/ ILArray <double> A)
        {
            if (A.IsScalar)
            {
                return(A.C);
            }
            if (A.IsEmpty)
            {
                throw new ILArgumentException("det: A must be a matrix");
            }
            int m = A.Dimensions[0];

            if (m != A.Dimensions[1])
            {
                throw new ILArgumentException("det: matrix A must be square");
            }

            /*!HC:inCls1*/ ILArray <double> L = A.C;

            int [] pivInd = new int[m];
            int    info   = 0;

            /*!HC:lapack_*getrf*/ Lapack.dgetrf(m, m, L.m_data, m, pivInd, ref info);
            if (info < 0)
            {
                throw new ILArgumentException("det: illegal parameter error.");
            }
            // determine pivoting: number of exchanges
            /*!HC:inArr1*/ double retA = /*!HC:unityVal*/ 1.0;

            for (int i = 0; i < m;)
            {
                retA *= L.m_data[i * m + i];
                if (pivInd[i] != ++i)
                {
                    retA *= /*!HC:negUnityValNoCmplx*/ -1.0;
                }
            }
            L.Dispose();
            return(new /*!HC:inCls1*/ ILArray <double> (retA));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Determinant of square matrix
        /// </summary>
        /// <param name="A">square input matrix</param>
        /// <returns>determinant of A</returns>
        /// <remarks><para>The determinant is computed by decomposing A into upper and lower triangular part. Therefore LAPACK function ?getrf is used. <br />
        /// Due to the properties of determinants, det(a) is the same as det(L) * det(U),where det(L) can easily be extracted from the permutation indices returned from LU decomposition. det(U) - with U beeing an upper triangular matrix - equals the product of the diagonal elements.</para>
        /// <para>For scalar A, a plain copy of A is returned.</para></remarks>
        /// <example>Creating a nonsingular 4x4 (double) matrix and it's determinant
        /// <code>ILArray&lt;double&gt; A = ILMath.counter(1.0,1.0,4,4);
        ///A[1] = 0.0;  // make A nonsingular
        ///A[14] = 0.0; //(same as: A[2,3] = 0.0;)
        /// // A is now:
        /// //&lt;Double&gt; [4,4]
        /// //(:,:) 1e+001 *
        /// // 0,10000   0,50000   0,90000   1,30000
        /// // 0,00000   0,60000   1,00000   1,40000
        /// // 0,30000   0,70000   1,10000   0,00000
        /// // 0,40000   0,80000   1,20000   1,60000
        ///
        ///ILMath.det(A) gives:
        /// //&lt;Double&gt; -360
        ///</code></example>
        ///<exception cref="ILNumerics.Exceptions.ILArgumentException">if A is empty or not a square matrix</exception>
        public static ILArray <complex> det(ILArray <complex> A)
        {
            if (A.IsScalar)
            {
                return(A.C);
            }
            if (A.IsEmpty)
            {
                throw new ILArgumentException("det: A must be a matrix");
            }
            int m = A.Dimensions[0];

            if (m != A.Dimensions[1])
            {
                throw new ILArgumentException("det: matrix A must be square");
            }

            ILArray <complex> L = A.C;

            int [] pivInd = new int[m];
            int    info   = 0;

            Lapack.zgetrf(m, m, L.m_data, m, pivInd, ref info);
            if (info < 0)
            {
                throw new ILArgumentException("det: illegal parameter error.");
            }
            // determine pivoting: number of exchanges
            complex retA = new complex(1.0, 0.0);

            for (int i = 0; i < m;)
            {
                retA *= L.m_data[i * m + i];
                if (pivInd[i] != ++i)
                {
                    retA *= -1.0;
                }
            }
            L.Dispose();
            return(new  ILArray <complex> (retA));
        }
Ejemplo n.º 5
0
        public void Test_Sum() {
            int errorCode = 0;
            try {
                double[] data = new double[120];
				for (int i = 0; i < data.Length; i++)
					data[i] = i+1;
				ILArray<double> A = new ILArray<double>(data, 6, 5, 4);
				ILArray<double> Res = new ILArray<double>(new double[20] {
					21,57,93,129,165,201,237,273,309,345,381,417,453,489,525,561,597,633,669,705}, 1, 5, 4);
				ILArray<double> S = ILMath.sum(A);
                ILPerformer p = new ILPerformer();
                if (S.Dimensions.NonSingletonDimensions != 2)
					throw new Exception("Wrong size of Sum result! ");
				if (S.Dimensions.NumberOfDimensions != 3)
					throw new Exception("Wrong size of Sum result! ");
				if (S.Dimensions[0] != 1)
					throw new Exception("Wrong size of leading Dimension of Sum result! ");
				if (S.Dimensions[1] != 5)
					throw new Exception("Wrong size of 2. Dimension of Sum result! ");
				if (S.Dimensions[2] != 4)
					throw new Exception("Wrong size of 3. Dimension of Sum result! ");
				if (S.Dimensions.NumberOfElements != 20)
					throw new Exception("Wrong number of elements of Sum result! ");
				if (! Res.Equals(S))
					throw new Exception("Wrong values of Sum result! ");

				ILArray<double> B = (ILArray<double>)A[1,"1:end;1:end;2"];
				double [] data2 = new double[4] { 350,380,410,440 };
				Res = new ILArray<double>(data2, 4, 1); 
				if (!Res.Equals(ILMath.sum(B,1)))
					throw new Exception("Wrong values of Sum result! ");
				
				errorCode = 1;
                // test non-matrix, reference case 
                A = (ILArray<double>)ILMath.counter(4,3,2).CreateReference();
                if (!A.IsReference) 
                    throw new Exception("sum: test data creation failed: A should be reference array!"); 
                Res = new double[,]{{15,18,21,24},{51,54,57,60}};  
                Res = ILMath.reshape(Res ,4,1,2);
                B = ILMath.sum(A,1); 
                if (!Res.Equals(B)) 
                    throw new Exception("sum: invalid result (n-d, reference)"); 

				
                ILArray<double> C = new ILArray<double>(data, 1, 1, 4, 5, 6);
				Res = new ILArray<double>(new double[30] {
					10,26,42,58,74,90,106,122,138,154,170,186,202,218,234,250,266
					,282,298,314,330,346,362,378,394,410,426,442,458,474},1,1,1,5,6);
                p.Tic(); 
                S = ILMath.sum(C,2);
                p.Toc();
                if (!Res.Equals(S)) 
					throw new Exception("Wrong values of Sum result! ");
                Info("Sum(1x1x4x5x6)[phy] needed: " + p.ToString());
                p.Tic();
                ILMath.sum(C);
                p.Toc();
                Info("Sum(1x1x4x5x6)[phy ohne Zuweisung] needed: " + p.ToString());
				
                errorCode = 2;
                C = (ILArray<double>)A.T;
                p.Tic();
                B = ILMath.sum(C);
                p.Toc();
                Info("Sum(5x4x6)[Ref] needed: " + p.ToString());
                p.Tic();
                ILMath.sum(C);
                p.Toc();
                Info("Sum(5x4x6)[Ref ohne Zuweisung] needed: " + p.ToString());
				
				data = new double[8]{15,51,18,54,21,57,24,60};
				Res = new ILArray<double>(data,1,2,4);
				if (!Res.Equals(B))
					throw new Exception("Wrong values of Sum result! ");
				
				errorCode = 3; 
				// big array: 
				data = new double[1000 * 1000 * 10]; 
				A = new ILArray<double>(data,1000,1000,10);
                p.Tic();
                C = ILMath.sum(A);
                p.Toc();
                Info("Sum(1000x1000x10) [phy] needed: " + p.ToString());
                p.Tic();
                ILMath.sum(A);
                p.Toc();
                Info("Sum(1000x1000x10) [phy ohne Zuweisung] needed: " + p.ToString());

				errorCode = 4;
				data = new double[1000 * 1000 * 10];
				A = new ILArray<double>(data, 1000000, 10);
                p.Tic();
                C = ILMath.sum(A);
                p.Toc();
                Info("Sum(1000000x10) [phy] needed: " + p.ToString());
                p.Tic();
                ILMath.sum(A);
                p.Toc();
                Info("Sum(1000000x10) [phy ohne Zuweisung] needed: " + p.ToString());

				errorCode = 5;
				data = new double[10 * 1000000];
				A = new ILArray<double>(data, 10, 1000000);
                p.Tic();
                C = ILMath.sum(A);
                p.Toc();
                Info("Sum(10 x 1000000) [Phy] needed: " + p.ToString());
                p.Tic();
                ILMath.sum(A);
                p.Toc();
                Info("Sum(10 x 1000000) [Phy ohne Zuweisung] needed: " + p.ToString());

                errorCode = 6;
				data = new double[1000 * 1000 * 10];
				data[1] = 1.0;
                A = (ILArray<double>)A.T;
                p.Tic();
                C = ILMath.sum(A);
                p.Toc();
                Info("Sum(1000000 x 10) [Ref] needed: " + p.ToString());
                p.Tic();
                ILMath.sum(A);
                p.Toc();
                Info("Sum(1000000 x 10) [Ref ohne Zuweisung] needed: " + p.ToString());


                errorCode = 7;
                data = new double[120];
                for (int i = 0; i < data.Length; i++)
                    data[i] = i + 1;
                A = new ILArray<double>(data, 6, 5, 4); 
                data = new double[24]{65,70,75,80,85,90,215,220,225,230,235,240,365
                                        ,370,375,380,385,390,515,520,525,530,535,540};
                Res = new ILArray<double>(data, 6, 1, 4);
                p.Tic();
                C = ILMath.sum(A,1);
                p.Toc();
                Info("Sum(6, 5, 4) [phy, dim 2] needed: " + p.ToString());
                p.Tic();
                ILMath.sum(A,1);
                p.Toc();
                Info("Sum(6, 5, 4) [phy ohne Zuweisung] needed: " + p.ToString());
                if (!Res.Equals(C))
					throw new Exception("Wrong values of Sum result! ");

				errorCode = 8;
                //data = new double[20000000];
                //for (int i = 0; i < data.Length; i++)
                //    data[i] = 2;
				A = ILMath.zeros(1,20000000) + 2.0; 
				Res = 4.0e+07;
				p.Tic();
				C = ILMath.sum(A);
				p.Toc();
				Info("Sum(20000000,1) [phy] needed: " + p.ToString());
				if (!Res.Equals(C))
					throw new Exception("Wrong values of Sum result! ");
				p.Tic();
				ILMath.sum(A);
				p.Toc();
				Info("Sum(20000000,1) [phy ohne Zuw.] needed: " + p.ToString());

				errorCode = 9;
                A.MinimumRefDimensions = 2;
				A.Dispose();
				A = new ILArray<double>(ILMemoryPool.Pool.New<double>(20000000),1,20000000).R; 
				p.Tic();
				C = ILMath.sum(A);
				p.Toc();
				Info("Sum(2,10000000) [ref 'vector'] needed: " + p.ToString());
				p.Tic();
				ILMath.sum(A);
				p.Toc();
				Info("Sum(1,20000000) [ref vector ohne Zuw.] needed: " + p.ToString());
				A = null;
				B = null;
				C = null;
				Res = null; 
                errorCode = 10; 
				Success("Test_Sum successfull");
            } catch (SerializationException e) {
				Error("Test_Sum failed at errorCode: "+errorCode +" Reason: " + e.Message);
            } catch (Exception e) {
				Error("Test_Sum failed at errorCode: " + errorCode + " Reason: " + e.Message);
            }
        }