public void Test_LDA_Performance2000_var(int rep) { int errorCode = 0; try { LDA lda = new LDA(); ILArray <double> X = ILMath.horzcat(ILMath.randn(2, 2000) * 2.0, ILMath.randn(2, 2000) * -2.0); ILLogicalArray labels = ILMath.tological(ILMath.horzcat(ILMath.ones(1, 2000), ILMath.zeros(1, 2000))); labels = labels.Concat(ILMath.tological(ILMath.zeros(1, 2000).Concat(ILMath.ones(1, 2000), 1)), 0); ILPerformer timer = new ILPerformer(); LDA.Hyperplane C; int oldRefMin = ILNumerics.Settings.ILSettings.MinimumRefDimensions; ILNumerics.Settings.ILSettings.MinimumRefDimensions = 2; timer.Tic(); for (int i = 0; i < rep; i++) { C = lda.TrainLDA(X, labels, 0.4); } timer.Toc(); Info("Test_LDA_Performance: with reference - data: 2x2000 run " + rep.ToString() + " times in: " + timer.Duration + "ms"); ILNumerics.Settings.ILSettings.MinimumRefDimensions = 3; timer.Tic(); for (int i = 0; i < rep; i++) { C = lda.TrainLDA(X, labels, 0.4); } timer.Toc(); ILNumerics.Settings.ILSettings.MinimumRefDimensions = oldRefMin; Info("Test_LDA_Performance: without reference - data: 2x2000 run " + rep.ToString() + " times in: " + timer.Duration + "ms"); Success(); }catch (Exception e) { Error(errorCode, e.Message); } }
//public void Test_SimpleAsyncAlgorithmSample() { // int errorCode = 0; // try { // SimpleAsyncSample sampleAlg = new SimpleAsyncSample(null, 2.0,50,ILMath.randn(3,2)); // sampleAlg.RunAsync(); // while (sampleAlg.State == ILAlgorithmRunningState.Running) { // Info(sampleAlg.Progress * 100 + "\r"); // System.Threading.Thread.Sleep(300); // } // Info(sampleAlg.Result.result.ToString()); // //sampleAlg.Result; // Success(); // } catch(Exception e) { // Error(errorCode,e.Message); // } //} public void Test_LDA() { int errorCode = 0; try { LDA lda = new LDA(); lda.StateChanged += new ILAlgorithmStateChangedEventHandler(lda_StateChanged); lda.ProgressChanged += new ILAlgorithmStateChangedEventHandler(lda_ProgressChanged); ILArray <double> X = new ILArray <double>(new double[] { -2, -2, -3, -3, 2, 2, 3, 3 }, 2, 4); ILLogicalArray labels = new ILLogicalArray(new byte[8] { 0, 1, 0, 1, 1, 0, 1, 0 }, 2, 4); LDA.Hyperplane C = lda.TrainLDA(X, labels, 0.4); if (Object.ReferenceEquals(C, null)) { throw new Exception("LDA: result is null!"); } if (!(C.w is ILArray <double>) || C.w.Dimensions[0] != 2) { throw new Exception("LDA: Results C[0] should be ILArray<double> 2x1"); } if (!(C.b is ILArray <double>) || !C.b.IsScalar) { throw new Exception("LDA: Results C[1] should be ILArray<double> 2x1"); } if (ILMath.abs(C.w[0] - -9.3750) > 1e-8) { throw new Exception("LDA: invalid result: C.w(1) should be : -9.3750"); } if (ILMath.abs(C.w[1] - -9.3750) > 1e-8) { throw new Exception("LDA: invalid result: C.w(2) should be : -9.3750"); } if (ILMath.abs(C.b.GetValue(0)) > 1e-8) { throw new Exception("LDA: invalid result: C.b should be : 0.0!"); } Success(); } catch (Exception e) { Error(errorCode, e.Message); } }
public void Test_LDA_Performance200k_10() { int errorCode = 0; try { LDA lda = new LDA(); ILArray <double> X = ILMath.horzcat(ILMath.randn(2, 200000) * 2.0, ILMath.randn(2, 200000) * -2.0); ILLogicalArray labels = ILMath.tological(ILMath.horzcat(ILMath.ones(1, 100000), ILMath.zeros(1, 100000))); labels = labels.Concat(ILMath.tological(ILMath.zeros(1, 100000).Concat(ILMath.ones(1, 100000), 1)), 0); ILPerformer timer = new ILPerformer(); timer.Tic(); LDA.Hyperplane C; for (int i = 0; i < 10; i++) { C = lda.TrainLDA(X, labels, 0.4); } timer.Toc(); Info("Test_LDA_Performance2: data: 2x200000 run 10 times in: " + timer.Duration + "ms"); Success(); }catch (Exception e) { Error(errorCode, e.Message); } }
public void Test_Invert() { int errorCode = 1; try{ ILArray <double> A = new double[] { 1, 2, 3, 4, 5, 6 }; ILLogicalArray d = A > 1; ILLogicalArray Res = !d; if (Res.NumberTrues != 1) { throw new Exception("invalid number of non zeros detected"); } ILLogicalArray result = new byte[] { 1, 0, 0, 0, 0, 0 }; if (!Res.Equals(result)) { throw new Exception("invalid values detected"); } Success(); } catch (Exception e) { Error(errorCode, e.Message); } }
/// <summary>Finds finite value elements</summary> /// <param name="A">input array</param> /// <returns>Logical array with 1 if the corresponding elements of input array is finite, 0 else.</returns> /// <remarks><para>If the input array is empty, an empty array will be returned.</para> /// <para>The array returned will be a dense array.</para></remarks> public static ILLogicalArray isfinite(ILArray <complex> A) { if (A.IsEmpty) { return(ILLogicalArray.empty(A.Dimensions)); } ILDimension inDim = A.Dimensions; byte [] retDblArr; // build ILDimension int newLength = inDim.NumberOfElements; //retDblArr = new byte [newLength]; retDblArr = ILMemoryPool.Pool.New <byte> (newLength); int leadDimLen = inDim [0]; // physical -> pointer arithmetic unsafe { fixed(byte *pOutArr = retDblArr) fixed(complex * pInArr = A.m_data) { byte * lastElement = pOutArr + retDblArr.Length; byte * tmpOut = pOutArr; complex *tmpIn = pInArr; while (tmpOut < lastElement) // HC02 { *tmpOut++ = complex.IsFinite(*tmpIn++) ?(byte)1:(byte)0; } } } return(new ILLogicalArray(retDblArr, inDim)); }
/// <summary>Locate infinite value elements</summary> /// <param name="A">input array</param> /// <returns>Logical array with 1 if the corresponding elements of input array is infinite, 0 else.</returns> /// <remarks><para>If the input array is empty, an empty array will be returned.</para> /// <para>The array returned will be a dense array.</para></remarks> public static ILLogicalArray isinf(ILArray <complex> A) { if (A.IsEmpty) { return(ILLogicalArray.empty(A.Dimensions)); } ILDimension inDim = A.Dimensions; byte [] retDblArr; // build ILDimension int newLength = inDim.NumberOfElements; //retDblArr = new byte [newLength]; retDblArr = ILMemoryPool.Pool.New <byte> (newLength); int leadDim = 0; int leadDimLen = inDim [0]; if (A.IsReference) { #region Reference storage // walk along the longest dimension (for performance reasons) for (int i = 1; i < inDim.NumberOfDimensions; i++) { if (leadDimLen < inDim [i]) { leadDimLen = inDim [i]; leadDim = i; } } ILIndexOffset idxOffset = A.m_indexOffset; int incOut = inDim.SequentialIndexDistance(leadDim); System.Diagnostics.Debug.Assert(!A.IsVector, "Reference arrays of vector size should not exist!"); if (A.IsMatrix) { #region Matrix //////////////////////////// MATRIX //////////////////// int secDim = (leadDim + 1) % 2; unsafe { fixed(int *leadDimStart = idxOffset [leadDim], secDimStart = idxOffset [secDim]) { fixed(byte *pOutArr = retDblArr) fixed(complex * pInArr = A.m_data) { byte * tmpOut = pOutArr; complex *tmpIn = pInArr; byte * tmpOutEnd = pOutArr + inDim.NumberOfElements - 1; int * secDimEnd = secDimStart + idxOffset [secDim].Length; int * secDimIdx = secDimStart; int * leadDimIdx = leadDimStart; int * leadDimEnd = leadDimStart + leadDimLen; while (secDimIdx < secDimEnd) { if (tmpOut > tmpOutEnd) { tmpOut = pOutArr + (tmpOut - tmpOutEnd); } tmpIn = pInArr + *secDimIdx++; leadDimIdx = leadDimStart; while (leadDimIdx < leadDimEnd) // HC00 { *tmpOut = complex.IsInfinity(*(tmpIn + *leadDimIdx++)) ?(byte)1:(byte)0; tmpOut += incOut; } } } } } #endregion } else { #region arbitrary size unsafe { int [] curPosition = new int [A.Dimensions.NumberOfDimensions]; fixed(int *leadDimStart = idxOffset [leadDim]) { fixed(byte *pOutArr = retDblArr) fixed(complex * pInArr = A.m_data) { byte * tmpOut = pOutArr; byte * tmpOutEnd = tmpOut + retDblArr.Length - 1; complex *tmpIn = pInArr + A.getBaseIndex(0, 0); tmpIn -= idxOffset [leadDim, 0]; // if the first index of leaddim is not 0, it will be added later anyway. so we subtract it here int *leadDimIdx = leadDimStart; int *leadDimEnd = leadDimStart + leadDimLen; int dimLen = curPosition.Length; int d, curD, count = retDblArr.Length / leadDimLen; // start at first element while (count-- > 0) { leadDimIdx = leadDimStart; while (leadDimIdx < leadDimEnd) //HC01 { *tmpOut = complex.IsInfinity(*(tmpIn + *leadDimIdx++)) ?(byte)1:(byte)0; tmpOut += incOut; } if (tmpOut > tmpOutEnd) { tmpOut -= retDblArr.Length - 1; } // increment higher dimensions d = 1; while (d < dimLen) { curD = (d + leadDim) % dimLen; tmpIn -= idxOffset [curD, curPosition [curD]]; curPosition [curD]++; if (curPosition [curD] < idxOffset [curD].Length) { tmpIn += idxOffset [curD, curPosition [curD]]; break; } curPosition [curD] = 0; tmpIn += idxOffset [curD, 0]; d++; } } } } } #endregion } #endregion } else { // physical -> pointer arithmetic #region physical storage unsafe { fixed(byte *pOutArr = retDblArr) fixed(complex * pInArr = A.m_data) { byte * lastElement = pOutArr + retDblArr.Length; byte * tmpOut = pOutArr; complex *tmpIn = pInArr; while (tmpOut < lastElement) // HC02 { *tmpOut++ = complex.IsInfinity(*tmpIn++) ?(byte)1:(byte)0; } } } #endregion } return(new ILLogicalArray(retDblArr, inDim)); }
/// <summary> /// Elementwise logical 'not equal' operator /// </summary> /// <param name="A">input array 1</param> /// <param name="B">input array 2</param> /// <returns>Logical array having '1' for elements in A not equal elements in B, '0' else</returns> /// <remarks><para>On empty input - empty array will be returned.</para> /// <para>A and / or B may be scalar. The scalar value will operate on all elements of the other arrays in this case.</para> /// <para>If neither of A or B is scalar or empty, the dimensions of both arrays must match.</para></remarks> /// <exception cref="ILNumerics.Exceptions.ILDimensionMismatchException">if neither of A or B is scalar and the size of both arrays does not match</exception> public static ILLogicalArray neq (ILArray<String> A, ILArray<String> B) { if (object.Equals(A,null)) throw new ILArgumentException("neq: input argurment A must not be null!"); if (object.Equals(B,null)) throw new ILArgumentException("neq: input argurment B must not be null!"); if (!A.Dimensions.IsSameShape(B.Dimensions)) throw new ILArgumentException("input arrays must have the same size"); if (A.IsEmpty || B.IsEmpty) return ILLogicalArray.empty(A.Dimensions); ILLogicalArray ret = null; string scalarValue; ILDimension retDim = null; byte[] retArr = null; if (A.IsScalar) { if (B.IsScalar) { retDim = new ILDimension(1,1); ret = new ILLogicalArray(new byte[1]{(A.GetValue(0) != B.GetValue(0))?(byte)1:(byte)0},1,1); } else { retDim = B.Dimensions; int len = B.Dimensions.NumberOfElements; retArr = new byte[len]; scalarValue = A.GetValue(0); for (int i = 0; i < len; i++) { if (scalarValue != B.GetValue(i)) { retArr[i] = 1; } } } } else { retDim = A.Dimensions; if (B.IsScalar) { int len = A.Dimensions.NumberOfElements; retArr = new byte[len]; scalarValue = B.GetValue(0); for (int i = 0; i < len; i++) { if (scalarValue != A.GetValue(i)) retArr[i] = 1; } } else { if (!A.Dimensions.IsSameSize(B.Dimensions)) throw new ILDimensionMismatchException("neq: size of arrays must match!"); int len = A.Dimensions.NumberOfElements; retArr = new byte[len]; for (int i = 0; i < len; i++) { if (A.GetValue(i) != B.GetValue(i)) retArr[i] = 1; } } } return new ILLogicalArray(retArr,retDim); }
public void Test_any() { int errorCode = 0; try { ILArray<int> I = ILMath.toint32(ILMath.counter(4,3,2)); I[":;1"] = 0; ILLogicalArray res = new ILLogicalArray (new byte[]{1,0,1,1,1,1},1,3,2); if (!ILMath.any(I).Equals(res)) throw new Exception("invalid result"); res["0;:;0"] = 0; ILLogicalArray res2 = new ILLogicalArray (new byte[]{0,1},1,1,2); if (!ILMath.any(res).Equals(res2)) throw new Exception("invalid result"); if (!ILMath.any(res2).Equals((byte)1)) throw new Exception("invalid result"); Success(); } catch(Exception e) { Error(errorCode,e.Message); } }
/// <summary> /// train the LDA /// </summary> /// <param name="X">data</param> /// <param name="Labels">labels</param> /// <returns>linear hyperplane wich best discriminates both classes</returns> /// <remarks> gamma will be set to 0.0</remarks> public Hyperplane TrainLDA (ILArray<double> X, ILLogicalArray Labels) { return TrainLDA(X,Labels,0.0); }
public void Test_NumberNonZero() { int errorCode = 1; try{ ILArray <double> A = new double[] { 1, 2, 3, 4, 5, 6 }; ILLogicalArray d = A > 1; if (d.NumberTrues != 5) { throw new Exception("invalid number of non zero elements"); } // check after setvalue : current '1' d[1] = true; if (d.NumberTrues != 5) { throw new Exception("invalid number of non zero elements"); } d[1] = false; if (d.NumberTrues != 4) { throw new Exception("invalid number of non zero elements"); } d[0] = false; if (d.NumberTrues != 4) { throw new Exception("invalid number of non zero elements"); } d[0] = true; if (d.NumberTrues != 5) { throw new Exception("invalid number of non zero elements"); } d[2] = null; if (d.NumberTrues != 4) { throw new Exception("invalid number of non zero elements"); } d[0] = null; if (d.NumberTrues != 3) { throw new Exception("invalid number of non zero elements"); } d[8, 1] = true; if (d.NumberTrues != 4) { throw new Exception("invalid number of non zero elements"); } // test string index access errorCode = 2; d = A > 1; if (d.NumberTrues != 5) { throw new Exception("invalid number of non zero elements"); } // check after setvalue : current '1' d["1"] = true; if (d.NumberTrues != 5) { throw new Exception("invalid number of non zero elements"); } d["1"] = false; if (d.NumberTrues != 4) { throw new Exception("invalid number of non zero elements"); } d["0"] = false; if (d.NumberTrues != 4) { throw new Exception("invalid number of non zero elements"); } d["0"] = true; if (d.NumberTrues != 5) { throw new Exception("invalid number of non zero elements"); } d["2"] = null; if (d.NumberTrues != 4) { throw new Exception("invalid number of non zero elements"); } d["0"] = null; if (d.NumberTrues != 3) { throw new Exception("invalid number of non zero elements"); } d["8;1"] = true; if (d.NumberTrues != 4) { throw new Exception("invalid number of non zero elements"); } // array index access errorCode = 3; ILArray <double> ind = new double[] { 1 }; d = A > 1; d[ind] = true; if (d.NumberTrues != 5) { throw new Exception("invalid number of non zero elements"); } // check after setvalue : current '1' ind = 0.0; d[ind] = true; if (d.NumberTrues != 6) { throw new Exception("invalid number of non zero elements"); } ind = 1; d[ind] = false; if (d.NumberTrues != 5) { throw new Exception("invalid number of non zero elements"); } ind = 0; d[ind] = false; if (d.NumberTrues != 4) { throw new Exception("invalid number of non zero elements"); } ind = 0; d[ind] = true; if (d.NumberTrues != 5) { throw new Exception("invalid number of non zero elements"); } ind = 2; d[ind] = null; if (d.NumberTrues != 4) { throw new Exception("invalid number of non zero elements"); } ind = 0; d[ind] = null; if (d.NumberTrues != 3) { throw new Exception("invalid number of non zero elements"); } ind[1] = 8; d[ind, 1] = true; if (d.NumberTrues != 4) { throw new Exception("invalid number of non zero elements"); } Success(); } catch (Exception e) { Error(errorCode, e.Message); } }
//public void Test_SimpleAsyncAlgorithmSample() { // int errorCode = 0; // try { // SimpleAsyncSample sampleAlg = new SimpleAsyncSample(null, 2.0,50,ILMath.randn(3,2)); // sampleAlg.RunAsync(); // while (sampleAlg.State == ILAlgorithmRunningState.Running) { // Info(sampleAlg.Progress * 100 + "\r"); // System.Threading.Thread.Sleep(300); // } // Info(sampleAlg.Result.result.ToString()); // //sampleAlg.Result; // Success(); // } catch(Exception e) { // Error(errorCode,e.Message); // } //} public void Test_LDA() { int errorCode = 0; try { LDA lda = new LDA(); lda.StateChanged += new ILAlgorithmStateChangedEventHandler(lda_StateChanged); lda.ProgressChanged += new ILAlgorithmStateChangedEventHandler(lda_ProgressChanged); ILArray<double> X = new ILArray<double>(new double[]{-2,-2,-3,-3,2,2,3,3},2,4); ILLogicalArray labels = new ILLogicalArray (new byte[8]{0,1,0,1,1,0,1,0},2,4); LDA.Hyperplane C = lda.TrainLDA(X,labels,0.4); if (Object.ReferenceEquals(C,null)) throw new Exception ("LDA: result is null!"); if (!(C.w is ILArray<double>) || C.w.Dimensions[0] != 2) throw new Exception("LDA: Results C[0] should be ILArray<double> 2x1"); if (!(C.b is ILArray<double>) || !C.b.IsScalar) throw new Exception("LDA: Results C[1] should be ILArray<double> 2x1"); if (ILMath.abs(C.w[0] - -9.3750) > 1e-8) throw new Exception("LDA: invalid result: C.w(1) should be : -9.3750"); if (ILMath.abs(C.w[1] - -9.3750) > 1e-8) throw new Exception("LDA: invalid result: C.w(2) should be : -9.3750"); if (ILMath.abs(C.b.GetValue(0)) > 1e-8) throw new Exception("LDA: invalid result: C.b should be : 0.0!"); Success(); } catch(Exception e) { Error(errorCode,e.Message); } }
/// <summary> /// linear discriminant analysis /// </summary> /// <param name="X">data matrix. Must be of size d x n, having samples arranged in columns </param> /// <param name="Labels">class labels for <paramref name="X"/>. </param> /// <param name="gamma">RLDA regularization parameter, with values between 0 and 1. /// GAMMA=0 gives normal LDA, GAMMA=1 uses a multiple of the identity matrix instead /// of the pooled covariance matrix.</param> /// <returns>cell array with discriminant hyperplane description</returns> /// <remarks><para><c>Labels</c> can be a vector of length n, having positive/negative values at indices /// corresponding to data X. Alternatively it is a 2 row matrix of length n with 1's in the first row at positions /// of data in the dirst class and 1's in the second row labeling the data in the second class.</para> /// <para>References: <list><item>J.H. Friedman, Regularized Discriminant Analysis, Journal /// of the Americal Statistical Association, vol.84(405), 1989. The method implemented here /// is Friedman's method with LAMDBA==1. </item> /// <item>The algorithm is base on implementation of Fraunhofer FIRST.IDA (2004)</item></list></para></remarks> public Hyperplane TrainLDA(ILArray <double> X, ILLogicalArray Labels, double gamma) { base.Run(); // <- will set state to 'Running' if (gamma > 1.0 || gamma < 0) { throw new ILArgumentException("Regularization parameter gamma must be in range 0...1!"); } // if size(yTr,1) == 1 yTr = [yTr<0; yTr>0]; end if (Labels.IsVector) { Labels = new ILLogicalArray(vertcat(Labels <0.0, Labels> 0.0)); } //ind = find(sum(abs(xTr),1)==inf); //ILArray<double> ind = Find(sum(abs(X),1) == double.PositiveInfinity); // <- he? what if negative infinity ? Hm. //// xTr(:,ind) = []; //X[null,ind] = null; //// yTr(:,ind) = []; //Labels[null,ind] = null; // nClasses= size(yTr,1); int nClasses = Labels.Dimensions[0]; // clInd= cell(nClasses,1); ILCell clInd = new ILCell(nClasses, 1); //N= zeros(nClasses, 1); int[] N = new int [nClasses]; int sumN = 0; //for ci= 1:nClasses, for (int ci = 0; ci < nClasses; ci++) { // clInd{ci} = find(yTr(ci,:)); clInd[ci] = find(Labels[ci, null]); // N(ci)= length(clInd{ci}); N[ci] = clInd[ci].Dimensions.Longest; sumN += N[ci]; } //priorP = ones(nClasses,1)/nClasses; ILArray <double> priorP = ones(nClasses, 1) / nClasses; //d= size(xTr,1); int d = X.Dimensions[0]; //m= zeros(d, nClasses); ILArray <double> m = zeros(d, nClasses); //Sq= zeros(d, d); ILArray <double> Sq = zeros(d, d); //for ci= 1:nClasses, for (int ci = 0; ci < nClasses; ci++) { // cli= clInd{ci}; ILArray <double> cli = (ILArray <double>)clInd[ci]; // m(:,ci)= mean(xTr(:,cli),2); m[null, ci] = mean(X[null, cli], 1); // % gleichanteil löschen: // yc= xTr(:,cli) - m(:,ci) * ones(1,N(ci)); ILArray <double> yc = X[null, cli] - repmat(m[null, ci], 1, N[ci]); // % yc sind xTr Daten des ersten merkmals mit mean = 0 // Sq = Sq + yc*yc'; Sq = Sq + multiply(yc, yc.T); } //Sq= Sq/(sum(N)-1); Sq = Sq / (sumN - 1); //Sq = (1-gamma)*Sq + gamma/d*trace(Sq)*eye(d); Sq = (1 - gamma) * Sq + gamma / (double)d * sum(diag(Sq)) * eye(d, d); //Sq = pinv(Sq); Sq = pinv(Sq); //C.w = Sq*m; ILArray <double> Cw = multiply(Sq, m); ILArray <double> Cb = -0.5 * sum(m * Cw, 0).T + log(priorP); //if nClasses==2 if (nClasses == 2) { // C.w = C.w(:,2) - C.w(:,1); Cw = Cw[null, 1] - Cw[null, 0]; // C.b = C.b(2)-C.b(1); Cb = Cb[1] - Cb[0]; } Hyperplane C; C.w = Cw; //C.b = -0.5*sum(m.*C.w,1)' + log(priorP); C.b = Cb; SetState(ILAlgorithmState.Finished); return(C); }
/// <summary> /// train the LDA /// </summary> /// <param name="X">data</param> /// <param name="Labels">labels</param> /// <returns>linear hyperplane wich best discriminates both classes</returns> /// <remarks> gamma will be set to 0.0</remarks> public Hyperplane TrainLDA(ILArray <double> X, ILLogicalArray Labels) { return(TrainLDA(X, Labels, 0.0)); }
/// <summary>determine, if any elements are nonzero</summary> /// <param name="A">N-dimensional array</param> /// <param name="leadDim">index of dimension to operate along</param> /// <returns><para>array of same size as A, having the 'leadDim's dimension reduced to 1, if any elements along that dimension are non-zero, '0' else. </para></returns> public static ILLogicalArray any(ILArray <complex> A, int leadDim) { if (A.IsEmpty) { return(ILLogicalArray.empty(A.Dimensions)); } if (A.IsScalar) { return(new ILLogicalArray(new byte [1] { (A.GetValue(0).iszero())?(byte)0:(byte)1 }, 1, 1)); } if (leadDim >= A.Dimensions.NumberOfDimensions) { throw new ILArgumentException("dimension parameter out of range!"); } ILDimension inDim = A.Dimensions; int[] newDims = inDim.ToIntArray(); int tmpCount = 0; int newLength; byte [] retDblArr; // build ILDimension newLength = inDim.NumberOfElements / newDims[leadDim]; newDims[leadDim] = 1; retDblArr = ILMemoryPool.Pool.New <byte>(newLength); ILDimension newDimension = new ILDimension(newDims); int incOut = newDimension.SequentialIndexDistance(leadDim); int leadDimLen = inDim[leadDim]; int nrHigherDims = inDim.NumberOfElements / leadDimLen; // physical -> pointer arithmetic if (leadDim == 0) { #region physical along 1st leading dimension unsafe { fixed(byte *pOutArr = retDblArr) fixed(complex * pInArr = A.m_data) { complex *lastElement; byte * tmpOut = pOutArr; complex *tmpIn = pInArr; for (int h = nrHigherDims; h-- > 0;) { lastElement = tmpIn + leadDimLen; while (tmpIn < lastElement) { tmpCount += ((*tmpIn++).iszero())?0:1; } *tmpOut = (tmpCount == 0)? (byte)0:(byte)1; tmpCount = 0; tmpOut++; } } } #endregion } else { #region physical along abitrary dimension // sum along abitrary dimension unsafe { fixed(byte *pOutArr = retDblArr) fixed(complex * pInArr = A.m_data) { byte * lastElementOut = newLength + pOutArr - 1; int inLength = inDim.NumberOfElements - 1; complex *lastElementIn = pInArr + inLength; int inc = inDim.SequentialIndexDistance(leadDim); byte * tmpOut = pOutArr; int outLength = newLength - 1; complex *leadEnd; complex *tmpIn = pInArr; for (int h = nrHigherDims; h-- > 0;) { leadEnd = tmpIn + leadDimLen * inc; while (tmpIn < leadEnd) { tmpCount += ((*tmpIn).iszero())?0:1; tmpIn += inc; } *tmpOut = (tmpCount == 0)? (byte)0:(byte)1; tmpCount = 0; tmpOut += inc; if (tmpOut > lastElementOut) { tmpOut = pOutArr + ((tmpOut - pOutArr) - outLength); } if (tmpIn > lastElementIn) { tmpIn = pInArr + ((tmpIn - pInArr) - inLength); } } } } #endregion } return(new ILLogicalArray(retDblArr, newDims));; }
bucketSort_variableLength <ElementType, SubelementType, IndexType>( IEnumerable <ElementType> input, ILKeyMapper <ElementType, SubelementType> mapper) { ILQueueList <ElementType, IndexType> ret; int m = mapper.NumberOfKeys; ILQueueList <ElementType, IndexType>[] buckets = new ILQueueList <ElementType, IndexType> [m]; ILQueueList <ElementType, IndexType> Q = new ILQueueList <ElementType, IndexType>(); IEnumerable <ElementType> inp = input; #region compute lmax int maxLen = 0, tmp = 0; foreach (ElementType elem in inp) { tmp = mapper.SubelementsCount(elem); if (tmp > maxLen) { maxLen = tmp; } } #endregion #region create Lengths array ILQueueList <ElementType, IndexType>[] Lengths = new ILQueueList <ElementType, IndexType> [maxLen]; foreach (ElementType elem in inp) { int len = mapper.SubelementsCount(elem) - 1; if (Lengths[len] == null) { Lengths[len] = new ILQueueList <ElementType, IndexType>(); } Lengths[len].Enqueue(elem); } #endregion #region create bucket indices for each position: Noempty array ILQueueList <int, byte>[] Noempty = new ILQueueList <int, byte> [maxLen]; ILLogicalArray alreadyFound = new ILLogicalArray(maxLen, m); byte tr = (byte)1; foreach (ElementType s in inp) { for (int l = mapper.SubelementsCount(s); l-- > 0;) { int hpos = mapper.Map(s, l, 0); if (alreadyFound.GetValue(l, hpos) == 0) { alreadyFound.SetValue(tr, l, hpos); if (Noempty[l] == null) { // create list and init with new value Noempty[l] = new ILQueueList <int, byte>(); } Noempty[l].Enqueue(hpos); } } } alreadyFound.Dispose(); for (int i = 0; i < maxLen; i++) { // sort lists for each length Noempty[i] = bucketSort_constantLength <int, int, byte>(Noempty[i], new ILIntLimitedKeyMapper(m)); } #endregion #region sort ILListItem <ElementType, IndexType> curElement; for (int l = maxLen; l-- > 0;) { // sort Length[l] list if (Lengths[l] != null) { Q.AddToStart(Lengths[l]); } // sort from last sorting loop (l+1) while (Q.Count > 0) { curElement = Q.Dequeue(); int hpos = mapper.Map(curElement.Data, l, 0); if (buckets[hpos] == null) { buckets[hpos] = new ILQueueList <ElementType, IndexType>(); } buckets[hpos].Enqueue(curElement); } // collect queues foreach (int c in Noempty[l]) { Q.Enqueue(buckets[c]); buckets[c].Clear(); } } #endregion return(Q); }
/// <summary> /// linear discriminant analysis /// </summary> /// <param name="X">data matrix. Must be of size d x n, having samples arranged in columns </param> /// <param name="Labels">class labels for <paramref name="X"/>. </param> /// <param name="gamma">RLDA regularization parameter, with values between 0 and 1. /// GAMMA=0 gives normal LDA, GAMMA=1 uses a multiple of the identity matrix instead /// of the pooled covariance matrix.</param> /// <returns>cell array with discriminant hyperplane description</returns> /// <remarks><para><c>Labels</c> can be a vector of length n, having positive/negative values at indices /// corresponding to data X. Alternatively it is a 2 row matrix of length n with 1's in the first row at positions /// of data in the dirst class and 1's in the second row labeling the data in the second class.</para> /// <para>References: <list><item>J.H. Friedman, Regularized Discriminant Analysis, Journal /// of the Americal Statistical Association, vol.84(405), 1989. The method implemented here /// is Friedman's method with LAMDBA==1. </item> /// <item>The algorithm is base on implementation of Fraunhofer FIRST.IDA (2004)</item></list></para></remarks> public Hyperplane TrainLDA(ILArray<double> X, ILLogicalArray Labels, double gamma) { base.Run(); // <- will set state to 'Running' if (gamma > 1.0 || gamma < 0) throw new ILArgumentException("Regularization parameter gamma must be in range 0...1!"); // if size(yTr,1) == 1 yTr = [yTr<0; yTr>0]; end if (Labels.IsVector) Labels = new ILLogicalArray( vertcat(Labels < 0.0, Labels > 0.0)); //ind = find(sum(abs(xTr),1)==inf); //ILArray<double> ind = Find(sum(abs(X),1) == double.PositiveInfinity); // <- he? what if negative infinity ? Hm. //// xTr(:,ind) = []; //X[null,ind] = null; //// yTr(:,ind) = []; //Labels[null,ind] = null; // nClasses= size(yTr,1); int nClasses = Labels.Dimensions[0]; // clInd= cell(nClasses,1); ILCell clInd = new ILCell(nClasses,1); //N= zeros(nClasses, 1); int[] N = new int [nClasses]; int sumN = 0; //for ci= 1:nClasses, for (int ci = 0; ci < nClasses; ci++) { // clInd{ci} = find(yTr(ci,:)); clInd[ci] = find(Labels[ci,null]); // N(ci)= length(clInd{ci}); N[ci] = clInd[ci].Dimensions.Longest; sumN += N[ci]; } //priorP = ones(nClasses,1)/nClasses; ILArray<double> priorP = ones(nClasses,1) / nClasses; //d= size(xTr,1); int d = X.Dimensions[0]; //m= zeros(d, nClasses); ILArray<double> m = zeros(d,nClasses); //Sq= zeros(d, d); ILArray<double> Sq = zeros(d,d); //for ci= 1:nClasses, for (int ci = 0; ci < nClasses; ci++) { // cli= clInd{ci}; ILArray<double> cli = (ILArray<double>)clInd[ci]; // m(:,ci)= mean(xTr(:,cli),2); m[null,ci] = mean(X[null,cli],1); // % gleichanteil löschen: // yc= xTr(:,cli) - m(:,ci) * ones(1,N(ci)); ILArray<double> yc = X[null,cli] - repmat(m[null,ci],1,N[ci]); // % yc sind xTr Daten des ersten merkmals mit mean = 0 // Sq = Sq + yc*yc'; Sq = Sq + multiply(yc,yc.T); } //Sq= Sq/(sum(N)-1); Sq = Sq / (sumN-1); //Sq = (1-gamma)*Sq + gamma/d*trace(Sq)*eye(d); Sq = (1-gamma) * Sq + gamma/(double)d * sum(diag(Sq))*eye(d,d); //Sq = pinv(Sq); Sq = pinv(Sq); //C.w = Sq*m; ILArray<double> Cw = multiply(Sq,m); ILArray<double> Cb = -0.5 * sum(m*Cw,0).T + log(priorP); //if nClasses==2 if (nClasses == 2) { // C.w = C.w(:,2) - C.w(:,1); Cw = Cw[null,1] - Cw[null,0]; // C.b = C.b(2)-C.b(1); Cb = Cb[1] - Cb[0]; } Hyperplane C; C.w = Cw; //C.b = -0.5*sum(m.*C.w,1)' + log(priorP); C.b = Cb; SetState(ILAlgorithmState.Finished); return C; }
/// <summary> /// read ONE array (arbitrary dimensions/type) from MAT file /// </summary> /// <param name="br">binary reader initialized and pointing to the beginning of the subarray element.</param> /// <returns>ILBaseArray of size and type originally stored into the mat file. Null if not loading this variable.</returns> private ILBaseArray read_miMATRIX (BinaryReader br, string[] vars2load) { long entryPositionInStream = br.BaseStream.Position; bool complex = false; bool logical = false; int mxClass = 0; int[] dimensions = new int [0]; MatFileType storageType = MatFileType.miUNKNOWN; int nrElements = 1; string name; ILBaseArray ret; // read array flags Int32 readInt = br.ReadInt32(); if (readInt != 6) throw new Exception ("found invalid datatype in array flag! currently only 'mxArray' types are supported!"); readInt = br.ReadInt32(); if (readInt != 8) throw new Exception ("unexpected array flag length. expected: 8 /found: " + readInt); readInt = br.ReadInt32(); complex = (readInt & mtFLAG_COMPLEX) != 0; logical = (readInt & mtFLAG_LOGICAL) != 0; mxClass = readInt & 0x00ff; // unknown br.ReadInt32(); // Read dimensions array readInt = br.ReadInt32(); if (readInt != 5) throw new Exception ("found invalid datatype in dimension flag!"); readInt = br.ReadInt32(); if (readInt < 2) throw new Exception ("Invalid number of dimensions found: " + readInt); dimensions = new int[(int)readInt / 4]; for (int i = 0; i < dimensions.Length; i++) { dimensions[i] = br.ReadInt32(); nrElements *= dimensions[i]; } // padding if needed if ((dimensions.Length % 2) != 0) br.ReadInt32(); // read Name - check for small data element format readInt = br.ReadInt32(); int nrSmallBytes = (int)((readInt & 0xffff0000) >> 16); if (nrSmallBytes != 0) { // process small element format if ((readInt & 0xffff) != 1) throw new Exception ("Invalid datype for (compressed) name element found: " + (readInt & 0x00ff) ); StringBuilder nameBuild = new StringBuilder(); nameBuild.Append ( br.ReadChars(nrSmallBytes)); // padding if needed while (nrSmallBytes < 4) { br.ReadByte(); nrSmallBytes ++; } name = nameBuild.ToString(); } else { // process 'long' format if (readInt != 1) throw new Exception ("Invalid datype for name element found: " + readInt); readInt = br.ReadInt32(); StringBuilder nameBuild = new StringBuilder(); nameBuild.Append ( br.ReadChars(readInt)); while (readInt % 8 != 0) { readInt ++; br.ReadByte(); } name = nameBuild.ToString(); } if (vars2load.Length != 0) { int varNameIdx; for (varNameIdx = 0; varNameIdx < vars2load.Length; varNameIdx++) if (name == vars2load[varNameIdx]) break; if (varNameIdx == vars2load.Length) return null; } // read data flags + check if small format readInt = br.ReadInt32(); nrSmallBytes = (Int16)((readInt & 0xffff0000) >> 16); System.Array realData = null; System.Array imagData = null; int len; if (nrSmallBytes != 0 && nrElements <= 4) { // small data element format for scalars only! // process small format -> real part storageType = (MatFileType)(readInt & 0xffff); len = nrSmallBytes; readElementGeneric(br, storageType, out realData, ref len,4); // padding //while (nrSmallBytes < 4 && br.BaseStream.Position < br.BaseStream.Length) { // br.ReadByte(); // nrSmallBytes++; //} } else { // read regular data : real part storageType = (MatFileType)Enum.Parse(typeof(MatFileType), readInt.ToString()); len = br.ReadInt32(); nrSmallBytes = len; readElementGeneric(br, storageType, out realData, ref len); // (padding is done in readElementGeneric) } // read imag part + check if small format if (complex) { readInt = br.ReadInt32(); nrSmallBytes = (Int16)((readInt & 0xffff0000) >> 16); if (nrSmallBytes != 0 && nrElements <= 4) { // process small format -> imag part storageType = (MatFileType)(readInt & 0xffff); len = nrSmallBytes; readElementGeneric(br, storageType, out imagData, ref len,4); // padding //while (nrSmallBytes < 4 && br.BaseStream.Position < br.BaseStream.Length) { // br.ReadByte(); // nrSmallBytes++; //} } else { // read regular data : image part storageType = (MatFileType)Enum.Parse(typeof(MatFileType), readInt.ToString());; len = br.ReadInt32(); nrSmallBytes = len; readElementGeneric(br, storageType, out imagData, ref len); // (padding's done in readElementGeneric) } } // convert to original data type if (complex) { complex[] retArr = new complex[nrElements]; double[] realPart = Convert2DoubleArray(realData); double[] imagPart = Convert2DoubleArray(imagData); for (int i = 0; i < nrElements; i ++) { retArr[i] = new complex(realPart[i] , imagPart[i]); } ret = new ILArray<complex>(retArr, dimensions); } else if (logical) { int numNonzero = 0; byte[] retArr = Convert2Logical(realData, out numNonzero); ret = new ILLogicalArray(retArr,new ILDimension(dimensions),numNonzero); } else { if (false) { } #region HYCALPER LOOPSTART /*!HC:TYPELIST: <hycalper> <type> <source locate="after"> pattern1 </source> <destination>mxINT8_CLASS</destination> <destination>mxUINT8_CLASS</destination> </type> <type> <source locate="after"> pattern3 </source> <destination>byte</destination> <destination>byte</destination> </type> <type> <source locate="after"> pattern4 </source> <destination>Convert2ByteArray</destination> <destination>Convert2ByteArray</destination> </type> <type> <source locate="after"> pattern5 </source> <destination><![CDATA[ILArray<byte>]]></destination> <destination><![CDATA[ILArray<byte>]]></destination> </type> </hycalper> */ else if (mxClass ==/*!HC:pattern1*/ mxDOUBLE_CLASS ) { /*!HC:pattern3*/ double [] dataArr; if (realData is /*!HC:pattern3*/ double []) { dataArr = (/*!HC:pattern3*/ double [] ) realData; } else { dataArr = /*!HC:pattern4*/ Convert2DoubleArray (realData); } ret = new /*!HC:pattern5*/ ILArray<double> (dataArr, dimensions); } #endregion HYCALPER LOOPEND #region HYCALPER AUTO GENERATED CODE // DO NOT EDIT INSIDE THIS REGION !! CHANGES WILL BE LOST !! else if (mxClass == mxUINT8_CLASS ) { byte [] dataArr; if (realData is byte []) { dataArr = ( byte [] ) realData; } else { dataArr = Convert2ByteArray (realData); } ret = new ILArray<byte> (dataArr, dimensions); } else if (mxClass == mxINT8_CLASS ) { byte [] dataArr; if (realData is byte []) { dataArr = ( byte [] ) realData; } else { dataArr = Convert2ByteArray (realData); } ret = new ILArray<byte> (dataArr, dimensions); } #endregion HYCALPER AUTO GENERATED CODE else throw new Exception("Unsupported data element type found! Cancelling..."); } // set name ret.Name = name; return ret; }
public void Test_Relop() { int errorCode = 0; try { byte[] data = new byte[120]; for (int i = 0; i < data.Length; i++) data[i] = (i % 2 == 1) ? (byte)1 : (byte)0; ILLogicalArray A = new ILLogicalArray(data, 6, 5, 4); double[] data2 = new double[120]; for (int i = 0; i < data.Length; i++) data2[i] = (i % 2) ; ILArray<double> B = new ILArray<double>(data2,6,5,4); ILArray<double> C = ILMath.ones(6,5,4); ILLogicalArray Res = ILMath.eq(B,C); if (!A.Equals(Res)) throw new Exception("Invalid result values!"); errorCode = 1; B = ILMath.ones(1000, 1000, 10); C = ILMath.zeros(1000, 1000, 10); ILPerformer pEq = new ILPerformer(); ILPerformer pAll = new ILPerformer(); long durationGC = 0, durationEq = 0; int cycles = 20; pAll.Tic(); for (int i = 0; i < cycles; i++) { pEq.Tic(); Res = B == C; pEq.Toc(); durationEq += pEq.Duration; pEq.Tic(); //System.GC.Collect(); pEq.Toc(); durationGC += pEq.Duration; } pAll.Toc(); durationEq /= cycles; durationGC /= cycles; Info("Test Eq[ual] phys. Arrays: [1000 x 1000 x 10] needed :" + durationEq + " /GC: " + durationGC + " /All: " + pAll.Duration); errorCode = 3; B = (ILArray<double>)B[1,"0:end;0:end;0:end"]; C = (ILArray<double>)C.T; durationGC = 0; durationEq = 0; cycles = 3; pAll.Tic(); for (int i = 0; i < cycles; i++) { pEq.Tic(); Res = B == C; pEq.Toc(); durationEq += pEq.Duration; pEq.Tic(); //System.GC.Collect(); pEq.Toc(); durationGC += pEq.Duration; } pAll.Toc(); durationEq /= cycles; durationGC /= cycles; Info("Test Eq[ual] Ref. Arrays: [1000 x 1000 x 10] needed :" + durationEq + " /GC: " + durationGC + " /All: " + pAll.Duration); /*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:end;1:end;2", 1]; 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; 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[24]{65,215,365,515,70,220,370,520,75,225 ,375,525,80,230,380,530,85,235,385,535,90,240,390,540}; Res = new ILArray<double>(data,1,4,6); 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 = new ILArray<double>(data, 20000000); Res = new ILArray<double>(new double[1] { 4.0e+07 }, 1); 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; ILArray<double>.MinimumRefDimensions = 1; A = (ILArray<double>)A.T; p.Tic(); C = ILMath.Sum(A); p.Toc(); Info("Sum(1,20000000) [ref vector] needed: " + p.ToString()); if (!Res.Equals(C)) throw new Exception("Wrong values of Sum result! "); 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; */ System.GC.Collect(); Success("Test_Relop successfull"); } catch (SerializationException e) { Error("Test_Relop failed at errorCode: " + errorCode + " Reason: " + e.Message); } catch (Exception e) { Error("Test_Relop failed at errorCode: " + errorCode + " Reason: " + e.Message); } }