/// <summary> /// Multidimensional scaling/PCoA: transform distances to points in a coordinate system. /// </summary> /// <param name="input">A matrix of pairwise distances. Zero indicates identical objects.</param> /// <returns>A matrix, the columns of which are coordinates in the nth dimension. /// The rows are in the same order as the input.</returns> public static ILArray <double> Scale(ILArray <double> input) { int n = input.Length; ILArray <double> p = ILMath.eye <double>(n, n) - ILMath.repmat(1.0 / n, n, n); ILArray <double> a = -.5 * ILMath.multiplyElem(input, input); ILArray <double> b = ILMath.multiply(p, a, p); ILArray <complex> V = ILMath.empty <complex>(); ILArray <complex> E = ILMath.eig((b + b.T) / 2, V); ILArray <int> i = ILMath.empty <int>(); ILArray <double> e = ILMath.sort(ILMath.diag(ILMath.real(E)), i); e = ILMath.flipud(e); i = ILMath.toint32(ILMath.flipud(ILMath.todouble(i))); ILArray <int> keep = ILMath.empty <int>(); for (int j = 0; j < e.Length; j++) { if (e[j] > 0.000000001) { keep.SetValue(j, keep.Length); } } ILArray <double> Y; if (ILMath.isempty(keep)) { Y = ILMath.zeros(n, 1); } else { Y = ILMath.zeros <double>(V.S[0], keep.Length); for (int j = 0; j < keep.Length; j++) { Y[ILMath.full, j] = ILMath.todouble(-V[ILMath.full, i[keep[j]]]); } Y = ILMath.multiply(Y, ILMath.diag(ILMath.sqrt(e[keep]))); } ILArray <int> maxind = ILMath.empty <int>(); ILMath.max(ILMath.abs(Y), maxind, 0); int d = Y.S[1]; ILArray <int> indices = maxind + ILMath.toint32(ILMath.array <int>(SteppedRange(0, n, (d - 1) * n))); ILArray <double> colsign = ILMath.sign(Y[indices]); for (int j = 0; j < Y.S[1]; j++) { Y[ILMath.full, j] = Y[ILMath.full, j] * colsign[j]; } return(Y); }
/// <summary> /// map all elements in A into final colors /// </summary> /// <param name="A">array with elements to map</param> /// <returns>colors as ILArray, the i-th row represents the color for the i-th element of A as RGB tripel.</returns> public ILArray <float> Map(ILArray <float> A) { ILArray <float> ret = new ILArray <float>(A.Dimensions.NumberOfElements, 3); float min, max; if (!A.GetLimits(out min, out max) || min == max) { // special case: all constant: eturn middle of colormap return(ILMath.repmat(m_map[m_map.Length / 2, null], ret.Dimensions[0], 1)); } float dist = (m_map.Dimensions[0] - 1) / (A.MaxValue - min); for (int i = 0; i < ret.Dimensions[0]; i++) { double index = (double)(A.GetValue(i) - min) * dist; if (index >= m_map.Dimensions[0] - 1) { ret[i, null] = m_map["end;:"]; continue; } else if (index < 0) { ret[i, null] = m_map["0;:"]; continue; } int find = (int)Math.Floor(index); if (find == index) { ret[i, null] = m_map[find, null]; continue; } // interpolate index = index - find; float r1 = m_map.GetValue(find, 0); float g1 = m_map.GetValue(find, 1); float b1 = m_map.GetValue(find, 2); r1 = (float)(index * (m_map.GetValue(find + 1, 0) - r1) + r1); g1 = (float)(index * (m_map.GetValue(find + 1, 1) - g1) + g1); b1 = (float)(index * (m_map.GetValue(find + 1, 2) - b1) + b1); ret.SetValue(r1, i, 0); ret.SetValue(g1, i, 1); ret.SetValue(b1, i, 2); } return(ret); }
private void TestQuickSortDescIDX() { try { ILArray <double> A = ILMath.counter(5, 4, 3); System.Diagnostics.Debug.Assert(!A.IsReference); ILArray <double> ind; ILArray <double> result = ILMath.sort(A, out ind, 1, true); ILArray <double> expect = A[":;3,2,1,0;:"]; if (!result.Equals(expect)) { throw new Exception("invalid values"); } expect = ILMath.repmat(new ILArray <double>(new double[] { 3, 2, 1, 0 }), 5, 1, 3); if (!ind.Equals(expect)) { throw new Exception("invalid indices"); } // test scalar A = 3.0; result = ILMath.sort(A, out ind, 0, true); if (result != 3.0) { throw new Exception("invalid values: scalar"); } if (ind != 0.0) { throw new Exception("invalid indices"); } // test empty A = ILArray <double> .empty(); if (!ILMath.sort(A, out ind, 0, true).IsEmpty) { throw new Exception("invalid values: empty"); } if (!ind.IsEmpty) { throw new Exception("invalid indices"); } Success(); } catch (Exception e) { Error(0, e.Message); } }
/// <summary> /// construct new filled graph /// </summary> /// <param name="panel">panel hosting the graph</param> /// <param name="X">X coords, if null, range 0..[cols of Z] will be created</param> /// <param name="Y">Y coords, if null, range 0..[rows of Z] will be created</param> /// <param name="Z">Z coords (heights)</param> /// <param name="C">Colors for Z</param> /// <param name="clippingContainer">gloabal limits of panel</param> public ILFilledGraph(ILPanel panel, ILBaseArray X, ILBaseArray Y, ILBaseArray Z, ILBaseArray C, ILClippingData clippingContainer) : base(panel, clippingContainer) { #region argument checking m_localClipping.EventingSuspend(); if (Z == null || !Z.IsMatrix) { throw new ILArgumentException("ILFilledGraph: Z must be matrix!"); } if (!Z.IsNumeric) { throw new ILArgumentException("ILFilledGraph: Z must be numeric!"); } m_sourceArray = ILMath.tosingle(Z); m_rows = m_sourceArray.Dimensions[0]; m_cols = m_sourceArray.Dimensions[1]; ILArray <float> tmp; if (!object.Equals(X, null) && !X.IsEmpty) { if (!X.IsMatrix || !X.IsNumeric) { throw new ILArgumentException("ILFilledGraph: X must be numeric matrix!"); } if (X.Dimensions.IsSameSize(Z.Dimensions)) { tmp = ILMath.tosingle(X); tmp.ExportValues(ref m_xCoords); m_localClipping.XMax = tmp.MaxValue; m_localClipping.XMin = tmp.MinValue; } else { throw new ILArgumentException("ILFilledGraph: X must be of same size than Z!"); } } else { ILMath.tosingle(ILMath.repmat(ILMath.counter(0.0, 1.0, 1, m_cols), m_rows, 1)).ExportValues(ref m_xCoords); m_localClipping.XMin = 0; m_localClipping.XMax = m_cols - 1; } if (!object.Equals(Y, null) && !Y.IsEmpty) { if (!Y.IsMatrix || !Y.IsNumeric) { throw new ILArgumentException("ILFilledGraph: Y must be numeric matrix!"); } if (Y.Dimensions.IsSameSize(Z.Dimensions)) { tmp = ILMath.tosingle(Y); tmp.ExportValues(ref m_yCoords); m_localClipping.YMax = tmp.MaxValue; m_localClipping.YMin = tmp.MinValue; } else { throw new ILArgumentException("ILFilledGraph: Y must be same size than Z!"); } } else { ILMath.tosingle(ILMath.repmat(ILMath.counter(0.0, 1.0, m_rows, 1), 1, m_cols)).ExportValues(ref m_yCoords); m_localClipping.YMax = m_rows - 1; m_localClipping.YMin = 0; } if (object.Equals(C, null) || C.IsEmpty) { m_colors = null; } else { m_colors = ILMath.tosingle(C); } m_localClipping.ZMax = m_sourceArray.MaxValue; m_localClipping.ZMin = m_sourceArray.MinValue; #endregion m_Vertcount = m_rows * m_cols; m_vertexReady = false; m_indexReady = false; // default view properties m_opacity = 1.0f; m_wireLines = new ILLineProperties(); m_wireLines.Changed += new EventHandler(m_wireLines_Changed); m_filled = true; m_localClipping.EventingResume(); }
private void TestQuickSortDescIDX(int dim, int len) { try { // asc along dimension (already ordered values) int[] dims = new int[4] { 5, 4, 3, 2 }; dims[dim] = len; ILArray <double> A = ILMath.counter(dims); ILArray <double> ind; ILArray <double> result = ILMath.sort(A, out ind, dim, true); ILArray <double> expect = null; ILArray <double> expectInd; expectInd = ILMath.counter(A.Dimensions[dim] - 1, -1.0, 1, A.Dimensions[dim]); ILBaseArray[] revDims = new ILBaseArray[dims.Length]; revDims[dim] = expectInd; expect = A[revDims]; if (!result.Equals(expect)) { throw new Exception("invalid values"); } int [] dimsEx = new int[dims.Length]; expectInd = ILMath.repmat(expectInd, A.Dimensions.SequentialIndexDistance(dim), 1); sortIDXTestHelper001(dim, dims, ref expectInd, dimsEx); expectInd = ILMath.repmat(expectInd, dimsEx); if (!ind.Equals(expectInd)) { throw new Exception("invalid indices"); } // reverse values ... A = ILMath.counter(A.Dimensions.NumberOfElements, -1.0, dims); result = ILMath.sort(A, out ind, dim, true); if (!result.Equals(A.C)) { throw new Exception("invalid values"); } expectInd = ILMath.counter(0.0, 1.0, 1, A.Dimensions[dim]); expectInd = ILMath.repmat(expectInd, A.Dimensions.SequentialIndexDistance(dim), 1); sortIDXTestHelper001(dim, dims, ref expectInd, dimsEx); expectInd = ILMath.repmat(expectInd, dimsEx); if (!ind.Equals(expectInd)) { throw new Exception("invalid indices"); } // test scalar A = 3.0; result = ILMath.sort(A, out ind, 0, true); if (result != 3.0) { throw new Exception("invalid values: scalar"); } if (ind != 0.0) { throw new Exception("invalid indices"); } // test empty A = ILArray <double> .empty(); if (!ILMath.sort(A, out ind, 0, true).IsEmpty) { throw new Exception("invalid values: empty"); } if (!ind.IsEmpty) { throw new Exception("invalid indices"); } Success(); } catch (Exception e) { Error(0, e.Message); } }