/// <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); }
public static EigenObject GetEigen(double[,] a) { int width = a.GetLength(0); double[] array = new double[width * width]; for (int i = 0; i < width; i++) { for (int j = 0; j < width; j++) { array[i * width + j] = a[i, j]; } } ILRetArray <double> A = ILMath.array( array, width, width); ILArray <complex> eigVectors = ILMath.array(new complex(0, 0), width * width); ILArray <complex> eigValues = ILMath.eig(A, eigVectors); return(new EigenObject(eigVectors, eigValues, width)); }
public static EigenObject GetEigen(Bitmap bmp) { int width = bmp.Width; double[] array = new double[width * width]; for (int i = 0; i < width; i++) { for (int j = 0; j < width; j++) { array[i * width + j] = bmp.GetPixel(i, j).R; } } ILArray <double> A = ILMath.array( array, width, width); ILArray <complex> eigVectors = ILMath.array(new complex(0, 0), width * width); ILArray <complex> eigValues = ILMath.eig(A, eigVectors); return(new EigenObject(eigVectors, eigValues, width)); }
public static ILScene Plot(Func <double, double, double> zOfxy, double x0, double xf, double y0, double yf, double pointsPerDimension = 700) { // create some test data (RBF) ILArray <float> Y = 1; double dx = (xf - x0) / pointsPerDimension; double dy = (yf - y0) / pointsPerDimension; ILArray <float> result = ILMath.array <float>(new ILSize(xf, yf)); for (double x = x0; x < xf; x += dx) { for (double y = y0; y < yf; y += dy) { result[x, y] = (float)zOfxy(x, y); } } //ILArray<float> X = ILMath.meshgrid( // ILMath.linspace<float>(-2f, 2f, 100), // ILMath.linspace<float>(-2f, 2f, 100), Y); //Y = X * X; //Y = X * X + Y * Y; //ILArray<float> A = 1 - ILMath.exp(-Y * 2f);Charting.Plot(xyz, 0, 10, 0, 15) // create new scene, add plot cube var scene = new ILScene { new ILPlotCube(twoDMode: false) { // rotate around X axis only Rotation = Matrix4.Rotation(new Vector3(1, 0, 0), 1.1f), // set perspective projection Projection = Projection.Perspective, // add plot cube contents Childs = { // add surface plot, default configuration //new ILSurface(A, 1 - A) { Alpha = 0.9f }, new ILSurface(result) { Alpha = 0.9f }, //new ILSurface(A) { Alpha = 0.9f }, // add contour plot in 3D, configure individual levels //new ILContourPlot(A, new List<ContourLevel> { // new ContourLevel() { Value = 0.8f, LineWidth = 4, ShowLabel = false }, // new ContourLevel() { Value = 0.5f, LineWidth = 4, ShowLabel = false }, // new ContourLevel() { Value = 0.2f, LineWidth = 4, ShowLabel = false, LineColor = 1 }, // new ContourLevel() { Value = 0.02f, LineWidth = 4, ShowLabel = false}, //}, create3D: true), //// add contour plot in 2D, default levels, no labels //new ILContourPlot(1 - A, showLabels: false), //// add legend, label first contour plots levels only //new ILLegend("0.8","0.5","0.2","0.02") { // // move legend to upper right corner // Location = new System.Drawing.PointF(0.97f,0.05f), // Anchor = new System.Drawing.PointF(1,0) //} } } }; return(scene); }
//Finalval is the result of the look up of Lambda and Beta in the CP map. //The function uses a bilinear interpolation method, and has been developed //to replace interpn in an embedded matlab environment public void Interpolate(double Beta, double Lambda, ILArray <double> table2, ILArray <double> Betavec2, ILArray <double> Lambdavec2, out double Finalval) { ILArray <int> Bt; int B1; int B2; ILArray <int> Lt; int L1; int L2; ILArray <double> Yvals; ILArray <double> Yintervals; //Setting up persistent variables //Function initialization //The first time the function is run, it stores supplied map as a persistent //variable. if (_persistentCt == null)// Is only run once { _persistentCt = new PersistentVariables(); _persistentCt.Table = table2.C; _persistentCt.Betavec = Betavec2.C; _persistentCt.Lambdavec = Lambdavec2.C; } //Step 1, finding two adjecent indexes of the BetaVec, which contain the //supplied beta value Bt = ILMath.empty <int>(); ILMath.min(ILMath.abs(_persistentCt.Betavec - Beta), Bt); //Finding index 1 B1 = Bt.GetValue(0); //Necessary specification in embedded //matlab if (Beta > _persistentCt.Betavec.GetValue(B1)) //Finding index 2 { if (B1 == (_persistentCt.Betavec.Length - 1)) //testing if endpoint-extrapolation { B2 = B1; //should be used B1 = B1 - 1; } else { B2 = B1 + 1; } } else { if (B1 == 0) { B1 = 1; B2 = 0; } else { B2 = B1 - 1; } } //Step 2, finding two adjecent indexes of the LambdaVec, which contain the //supplied Lambda value Lt = ILMath.empty <int>(); ILMath.min(ILMath.abs(_persistentCt.Lambdavec - Lambda), Lt); L1 = Lt.GetValue(0); if (Lambda > _persistentCt.Lambdavec.GetValue(L1)) //Need to work out of indexes { if (L1 == (_persistentCt.Lambdavec.Length - 1)) { L2 = L1; L1 = L1 - 1; } else { L2 = L1 + 1; } } else { if (L1 == 0) { L1 = 1; L2 = 0; } else { L2 = L1 - 1; } } //Step 3 //Finding the four indexed values by means of the indexes Yvals = new double[, ] { { _persistentCt.Table.GetValue(B1, L1), _persistentCt.Table.GetValue(B2, L1) }, { _persistentCt.Table.GetValue(B1, L2), _persistentCt.Table.GetValue(B2, L2) } }; //Step 4 //Making two sets of linear interpolations by using the different lambda values Yintervals = ILMath.array(new double[] { ((Yvals.GetValue(0, 1) - Yvals.GetValue(0, 0)) / (_persistentCt.Lambdavec.GetValue(L2) - _persistentCt.Lambdavec.GetValue(L1)) * (Lambda - _persistentCt.Lambdavec.GetValue(L1)) + Yvals.GetValue(0, 0)), ((Yvals.GetValue(1, 1) - Yvals.GetValue(1, 0)) / (_persistentCt.Lambdavec.GetValue(L2) - _persistentCt.Lambdavec.GetValue(L1)) * (Lambda - _persistentCt.Lambdavec.GetValue(L1)) + Yvals.GetValue(1, 0)) }, 2, 1); //Step 5 //Making the final linear interpolation on the results obtained in //stepp 4 Finalval = ((Yintervals.GetValue(1) - Yintervals.GetValue(0)) / (_persistentCt.Betavec.GetValue(B2) - _persistentCt.Betavec.GetValue(B1))) * (Beta - _persistentCt.Betavec.GetValue(B1)) + Yintervals.GetValue(0); }
public Vector(int dimension, int dimensionMDF, double value) { this.values = ILMath.array <double>(value, dimension); this.valuesMDF = ILMath.array <double>(value, dimensionMDF); }