public void Test_MatrixMultiply() {
			int errorCode = 0;
			// success? 
			try {
				ILPerformer p = new ILPerformer();
				double[] data1 = new double[120]; 
				double[] data2 = new double[120];
                double[] results = new double[36] { 177840,180120,182400,184680,186960,189240,180120,182440,
                    184760,187080,189400,191720,182400,184760,187120,189480,191840,194200,184680,187080,
                    189480,191880,194280,196680,186960,189400,191840,194280,196720,199160,189240,191720,
                    194200,196680,199160,201640};
                for (int i = 0; i < 120; i++) {
                    data1[i] = i;
                    data2[i] = i * 2;
                }
                ILDA A = new ILDA(data1, 6, 20);
                ILDA BR = new ILDA(data2, 6, 20);
                ILDA ResultExpect = new ILDA(results, 6,6);
				ILDA Result = null;
				ILDA AR = A.T.T;
                ILDA B = BR.T;
                B.Detach(); 
				BR = B.T.T;
				errorCode = 0;
                p.Tic(); 
                Result = ILMath.multiply(A , B);
                p.Toc();
				errorCode = 1;
                if (Result.Dimensions.NumberOfDimensions != 2)
					throw new Exception("Wrong number of results dimensions!");
				errorCode = 2;
                if (Result.Dimensions[0] != 6 ||Result.Dimensions[1] != 6)
					throw new Exception("Wrong result's size!");
				errorCode = 3;
                if (!Result.Equals(ResultExpect))
                    throw new Exception("Wrong results values!");
                Info("Test_MatrixMultiplyDouble succeeded: phy/phy " + p.ToString() + "ms needed");
                errorCode = 4;
				p.Tic(); 
				if (!ResultExpect.Equals(ILMath.multiply(AR, BR)))
                    throw new Exception("Wrong data value on ref/ref");
				p.Toc();
				Info("Test_MatrixMultiplyDouble succeeded: ref/ref " + p.ToString() + "ms needed");
                // test if wrong dimensions throw exceptions 
				try {
					ILMath.multiply (A, AR);
					throw new InvalidOperationException();
				} catch (Exception e) {
					if (e is InvalidOperationException)
						throw new Exception("Dimensions should be forced to match!");
				}
                data1 = new double[1000 * 2000];
                A = new ILDA(data1, 1000, 2000);
                B = new ILDA(data1, 2000, 1000);
                p.Tic();
                Result = ILMath.multiply(A, B);
                p.Toc();
                Info("Test_MatrixMultiply 1000x2000: phy/phy " + p.ToString() + "ms needed");
                Info("TODO: Implement test MatrixMultiply with reference storages!!"); 

                errorCode = 5; 
                ILArray<float> fA = new ILArray<float>(new float[12] {1f,2f,3f,4f,5f,6f,7f,8f,9f,10f,11f,12f},4,3); 
                ILArray<float> fB = new ILArray<float>(new float[12] {17f,18f,19f,20f,21f,22f,23f,24f,25f,26f,27f,28f},3,4); 
                ILArray<float> fRes = new ILArray<float>(new float[16] {278f,332f,386f,440f,323f,386f,449f,512f,368f,440,512f,584f,413f,494f,575f,656f},4,4); 
                if (ILMath.norm(fRes - ILMath.multiply(fA, fB)) > 1.0e-10) 
                    throw new Exception ("Test_MatrixMultiply: invalid values detected!");
                
                errorCode = 6; 
                ILArray<complex> zA = ILMath.real2complex(ILMath.vector(1,12),ILMath.vector(-1,-1,-12)); 
                ILArray<complex> zB = ILMath.real2complex(ILMath.vector(17,28),ILMath.vector(-17,-1,-28)); 
                //{new complex (0 , 0.5560), new complex (0, 0.6640), new complex (0, 0.7720), new complex (0, 0.8800), new complex (0,  0.6460), new complex (0,  0.7720), new complex (0,  0.8980), new complex (0,  1.0240), new complex (0,  0.7360), new complex (0,  0.8800),new complex(0,1.0240),new complex(0,1.1680),new complex(0,0.8260),new complex(0,0.9880),new complex(0,1.1500),new complex(0,1.3120)}
                ILArray<complex> zRes = ILMath.real2complex(ILMath.zeros(1,16), new ILArray<double>(new double[]{556,664,772,880,646,772,898,1024,736,880,1024,1168,826,988,1150,1312}) * (-1) ); 
                zA = ILMath.reshape(zA,4,3); 
                zB = ILMath.reshape(zB,3,4); 
                zRes = ILMath.reshape(zRes,4,4); 

                if (ILMath.norm(zRes - ILMath.multiply(zA, zB)) > 1.0e-10) 
                    throw new Exception ("Test_MatrixMultiply: invalid values detected!");
                
                errorCode = 7; 
                ILArray<fcomplex> cA = ILMath.real2fcomplex(ILMath.vector(1,12),ILMath.vector(-1,-1,-12)); 
                ILArray<fcomplex> cB = ILMath.real2fcomplex(ILMath.vector(17,28),ILMath.vector(-17,-1,-28)); 
                ILArray<fcomplex> cRes = ILMath.real2fcomplex(ILMath.zeros(1,16), new ILArray<double>(new double[]{556,664,772,880,646,772,898,1024,736,880,1024,1168,826,988,1150,1312}) * (-1) ); 
                cA = ILMath.reshape(cA,4,3); 
                cB = ILMath.reshape(cB,3,4); 
                cRes = ILMath.reshape(cRes,4,4); 

                if (ILMath.norm(cRes - ILMath.multiply(cA, cB)) > 1.0e-10)
                    throw new Exception ("Test_MatrixMultiply: invalid values detected!");
                
                Success("Test_MatrixMultiply successful.");
			} catch (Exception e) {
                Error("Test_MatrixMultiply failed at step: " + errorCode + " Msg: " + e.Message);
			}
		}
Exemple #2
0
        public void Test_Find() {
			int errorCode = 0;
			try {
				double[] data = new double[24];
				for (int i = 0; i < data.Length; i++)
					data[i] = i;
				ILArray<double> A = new ILArray<double>(data, 2, 3, 4);
                ILArray<double> B = null; 
				ILArray<double> Res = A[ILMath.find(A > 10)];
                // test emtpy 
                Res = A[ILMath.find(A > 50)];
                if (!Res.IsEmpty) 
					Error("Test_Find: empty index array should throw an exception! "); 
                errorCode = 1; 
                ILArray<double> ResI = new ILArray<double>(new double[12] {0,2,4,6,8,10,12,14,16,18,20,22},1,12);
                ILArray<double> ResR = ILMath.zeros(1,12); 
                ILArray<double> ResC = new ILArray<double>(new double[12] {0,1,2,3,4,5,6,7,8,9,10,11},1,12);
                ILArray<byte> ResV = new ILArray<byte>(new byte[12] {1,1,1,1,1,1,1,1,1,1,1,1},1,12);

                // Test physical storage forward, no limit
                ILArray<double> Rows = ILMath.empty(); 
                ILArray<double> Cols = ILMath.empty(); 
                ILArray<byte> Vals = ILArray<byte>.empty(); 
                Rows = ILMath.find(A/2 == ILMath.round(A/2)); 
                if (!Rows.Equals(ResI)) 
                    throw new Exception ("Invalid values: Find(A/2 == ILMath.round(A/2)); A - physical");
                Rows = ILMath.find(A/2 == ILMath.round(A/2),2);
                if (!Rows.Equals(ResI["0;0,1"]))
                    throw new Exception ("Invalid values: Find(A/2 == ILMath.round(A/2),2); A - physical");
                Rows = ILMath.find(A/2 == ILMath.round(A/2),-2);
                if (!Rows.Equals(ResI["0;10:end"]))
                    throw new Exception ("Invalid values: Find(A/2 == ILMath.round(A/2),-2); A - physical");
                // Test physical, backward, no limit
                Rows = ILMath.find(A/2 == ILMath.round(A/2),-200);
                if (!Rows.Equals(ResI))
                    throw new Exception ("Invalid values: Find(A/2 == ILMath.round(A/2),-200); A - physical");
                Rows = ILMath.find(A/2 == ILMath.round(A/2),0,ref Cols, ref Vals); 
                if (!Rows.Equals(ResR) || !Cols.Equals(ResC) || ILMath.find(Vals != ResV.T).Length > 0)
                    throw new Exception ("Invalid values: [r,c,v] = Find(A/2 == ILMath.round(A/2),-200); A - physical");
                Rows = ILMath.find(A/2 == ILMath.round(A/2),-3,ref Cols, ref Vals); 
                if (!Rows.Equals(ResR["0;9,10,11"]) || !Cols.Equals(ResC["0;9,10,11"]) || !Vals.Equals(ResV["0;9,10,11"]))
                    throw new Exception ("Invalid values: [r,c,v] = Find(A/2 == ILMath.round(A/2),-3); A - physical");
                // Test reference storage, forward, no limit
                errorCode = 2; 

                ILLogicalArray A1 = (A/2 == ILMath.round(A/2));
                A1 = A1.GetShifted(3);
                Rows = ILMath.find(A1); 
                if (!Rows.Equals(ResI)) 
                    throw new Exception ("Invalid values: Find(A/2 == ILMath.round(A/2)); A - reference");
                Rows = ILMath.find(A1,2);
                if (!Rows.Equals(ResI["0;0,1"]))
                    throw new Exception ("Invalid values: Find(A/2 == ILMath.round(A/2),2); A - reference");
                Rows = ILMath.find(A1,-2);
                if (!Rows.Equals(ResI["0;10:end"]))
                    throw new Exception ("Invalid values: Find(A/2 == ILMath.round(A/2),-2); A - reference");
                // Test physical, backward, no limit
                Rows = ILMath.find(A1,-200);
                if (!Rows.Equals(ResI))
                    throw new Exception ("Invalid values: Find(A/2 == ILMath.round(A/2),-200); A - reference");
                Rows = ILMath.find(A1,0,ref Cols, ref Vals); 
                if (!Rows.Equals(ResR) || !Cols.Equals(ResC) || ILMath.find(Vals != ResV.T).Length > 0)
                    throw new Exception ("Invalid values: [r,c,v] = Find(A/2 == ILMath.round(A/2),-200); A - reference");
                Rows = ILMath.find(A1,-3,ref Cols, ref Vals); 
                if (!Rows.Equals(ResR["0;9,10,11"]) || !Cols.Equals(ResC["0;9,10,11"]) || !Vals.Equals(ResV["0;9,10,11"]))
                    throw new Exception ("Invalid values: [r,c,v] = Find(A/2 == ILMath.round(A/2),-3); A - reference");

                errorCode = 3;
                float[] data2 = new float[24];
                for (int i = 0; i < data2.Length; i++)
					data2[i] = (float)(i%2.0f);
                ResI = new ILArray<double>(new double[4]{18,20,22,24},4,1) - 1.0; 
                ILArray<float> A2 = new ILArray<float>(data2,2,3,4);
                Rows = ILMath.find(A2,-4);
                if (!Rows.Equals(ResI))
                    throw new Exception ("Invalid values: Find(A2,-4); A - phys., float");

                errorCode = 4; 
                // test large 
                A = ILMath.ones(500, 500, 10);
                A["200:300;200:300;0:end"] = (ILArray<double>)0.0;
                A = A["0:end;0:end;0:end"];
                ILLogicalArray L = A != 0; 
                ILPerformer p = new ILPerformer();
                // Matlab Code: 
                // a = ones(1000,1000,10); a(200:300,200:300,:) = 0; tic; b = a(find(a~=0)); toc; 
                Info("Test_Find (testing performance please wait...) ");
                int cycles = 20;
                long duration = 0;
                for (int i = 0; i < cycles; i++) {
                    p.Tic();
                    B = ILMath.find(L); 
                    p.Toc();
                    duration += p.Duration;
                    //System.GC.Collect();
                }
                duration /= cycles;
                Info("Test_Find [500,500,10] " + (L.IsReference ? "Ref: " : "Phy: ") + "'B = Find(A != 0)' avg. needed: " + p.ToString() + " ms");

                errorCode = 5; 
                cycles = 4;
                duration = 0;
                L = L["0:end;0:end;0:end"]; 
                for (int i = 0; i < cycles; i++) {
                    p.Tic();
                    B = ILMath.find(L);
                    p.Toc();
                    duration += p.Duration;
                    System.GC.Collect(); 
                }
                duration /= cycles;
                Info("Test_Find [500,500,10] " + (L.IsReference ? "Ref: " : "Phy: ") + "'B = Find(L)' avg. needed: " + p.ToString() + " ms");

                errorCode = 6; 
                // value ok? 
                if (ILMath.sum(ILMath.sum(B)) != 2997460747500)
                    throw new Exception("TEst_Find: wrong value of result!"); 

				Success();
			} catch (Exception e) {
				Error(errorCode, e.Message);
			}
		}
Exemple #3
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);
            }
        }
Exemple #4
0
        public void Test_MMult() {
            int errorCode = 0;
            try {
                double[] data = new double[20];
				for (int i = 0; i < data.Length; i++)
					data[i] = i+1;
                ILArray<double> origA = new ILArray<double>(data,5,4); 
				ILArray<double> A = new ILArray<double>(origA);
                ILArray<double> B = new ILArray<double>(origA.T).Detach();

                ILArray<double> Res = new ILArray<double>(new double[] {414,448,482,516,550,448,486,524,562,600,482,524,566,608,650,516,562,608,654,700,550,600,650,700,750} ,5,5); 
                // test phy * phy
                if (!ILMath.Equals (ILMath.multiply(A,B),Res) )
                    throw new Exception ("Test MathBasic: MMult failed at A*B (phys * phys) due: invalid values"); 
                B = new ILArray<double> (origA.T);
                // test phy * ref 
                if (!ILMath.Equals (ILMath.multiply(A,B),Res) )
                    throw new Exception ("Test MathBasic: MMult failed at A*B (phys * ref) due: invalid values"); 
                // test ref * ref 
                A = origA.T.Detach().T; 
                if (!ILMath.Equals (ILMath.multiply(A,B),Res) )
                    throw new Exception ("Test MathBasic: MMult failed at A*B (ref * phys) due: invalid values"); 
                // test ref * phy 
                A = A.Detach(); 
                B = B.Detach(); 
                if (!ILMath.Equals (ILMath.multiply(A,B),Res) )
                    throw new Exception ("Test MathBasic: MMult failed at A*B (ref * ref) due: invalid values"); 
                ILPerformer p = new ILPerformer(); 
                data = new double[1000000];
                A = ILMath.ones(1000,1000); 
                p.Tic(); 
                    B = ILMath.multiply (A,A.T); 
                p.Toc(); 
                Info("MMult 1000x1000 procceeded in " + p.ToString() + "msec.");
                Success("MMult finished successfully"); 
            } catch (Exception e) {
				Error("Test_MMult failed at errorCode: " + errorCode + " Reason: " + e.Message);
            }
        }
Exemple #5
0
 public void Test_EnumeratorPerf(ILArray<double> A) {
     int errorCode = 1; 
     try  {
         ILPerformer p = new ILPerformer();
         Info("Enumerator test A => " + A.Dimensions); 
         p.Tic();
         foreach (ILArray<double> a in A) { }
         p.Toc();
         Info(" foreach (ILArray<double> a in A){ ... => " + p.ToString() + " ms");
         errorCode = 2; 
         p.Tic(); 
         foreach (double a in A) { }
         p.Toc(); 
         Info(" foreach (double a in A){ }  => " + p.ToString() + " ms");
         errorCode = 3; 
         p.Tic(); 
         foreach (double a in A.Values) { }
         p.Toc();
         Info(" foreach (double a in A.Values){ } [solid] => " + p.ToString() + " ms");
         A = A.R;
         p.Tic(); 
         foreach (double a in A.Values) { }
         p.Toc();
         Info(" foreach (double a in A.Values){ } [Ref] => " + p.ToString() + " ms");
         int l = A.Dimensions.NumberOfElements; 
         double b;
         p.Tic(); 
         for (int i = 0; i < l; i++) { b = A.GetValue(i); }
         p.Toc();
         Info(" for (int i = 0; i < l; i++) { a = A.GetValue(i); } => " + p.ToString() + " ms");
         if (A.IsReference) 
             A.Detach(); 
         p.Tic(); 
         for (int i = 0; i < l; i++) { b = A.m_data[i]; }
         p.Toc();
         Info(" for (int i = 0; i < l; i++) { a = A.m_data[i]; } => " + p.ToString() + " ms");
         Success(); 
     } catch (Exception e) {
         Error(errorCode,e.Message); 
     }
 }