protected internal override void TakeStep(AbstractStochasticCachingDiffFunction dfunction) { for (int i = 0; i < x.Length; i++) { double thisGain = fixedGain * GainSchedule(k, 5 * numBatches) / (diag[i]); newX[i] = x[i] - thisGain * grad[i]; } //Get a new pair... Say(" A "); double[] s; double[] y; if (pairMem > 0 && sList.Count == pairMem || sList.Count == pairMem) { s = sList.Remove(0); y = yList.Remove(0); } else { s = new double[x.Length]; y = new double[x.Length]; } s = ArrayMath.PairwiseSubtract(newX, x); dfunction.recalculatePrevBatch = true; System.Array.Copy(dfunction.DerivativeAt(newX, bSize), 0, y, 0, grad.Length); ArrayMath.PairwiseSubtractInPlace(y, newGrad); // newY = newY-newGrad double[] comp = new double[x.Length]; sList.Add(s); yList.Add(y); UpdateDiag(diag, s, y); }
public void TestForExample() { int[] expectedFactorial = new int[] { 3, 6, 2, 8, 8, 0, 0 }; int[] factorial = ArrayMath.Factorial(10); Assert.AreEqual(expectedFactorial, factorial); }
private double CalculateZExpectationsScaled() { CalculatePredScaled(); var residuals = ArrayMath.Pow(dys - predY, 2.0); var residuals1 = residuals / (-2 * dsds[0] * dsds[0]); //Console.WriteLine(residuals); var residuals2 = residuals / (-2 * dsds[1] * dsds[1]); //Console.WriteLine(dsds[1]); var weights = dpAs.ElementDivide(dsds); var t1 = weights[0] * ArrayMath.Exp(residuals1); var t2 = weights[1] * ArrayMath.Exp(residuals2); var l = t1.Zip(t2, (x, y) => (x + y)); var ll = l.Select(x => Math.Log(x)).Sum(); var denom = t1 + t2; t1 = t1.ElementDivide(denom); t2 = t2.ElementDivide(denom); for (int i = 0; i < ys.Length; i++) { zs[i, 0] = t1[i]; zs[i, 1] = t2[i]; } //return the log likelihood return(ll); }
/// <summary>Samples a single position in the sequence.</summary> /// <remarks> /// Samples a single position in the sequence. /// Does not modify the sequence passed in. /// returns the score of the new label for the position to sample /// </remarks> /// <param name="sequence">the sequence to start with</param> /// <param name="pos">the position to sample.</param> /// <param name="temperature">the temperature to control annealing</param> private Pair <int, double> SamplePositionHelper(ISequenceModel model, int[] sequence, int pos, double temperature) { double[] distribution = model.ScoresOf(sequence, pos); if (temperature != 1.0) { if (temperature == 0.0) { // set the max to 1.0 int argmax = ArrayMath.Argmax(distribution); Arrays.Fill(distribution, double.NegativeInfinity); distribution[argmax] = 0.0; } else { // take all to a power // use the temperature to increase/decrease the entropy of the sampling distribution ArrayMath.MultiplyInPlace(distribution, 1.0 / temperature); } } ArrayMath.LogNormalize(distribution); ArrayMath.ExpInPlace(distribution); int newTag = ArrayMath.SampleFromDistribution(distribution, random); double newProb = distribution[newTag]; return(new Pair <int, double>(newTag, newProb)); }
/// <summary>Feed a feature vector forward through the network.</summary> /// <remarks> /// Feed a feature vector forward through the network. Returns the /// values of the output layer. /// </remarks> private double[] ComputeScores(int[] feature, IDictionary <int, int> preMap) { double[] hidden = new double[config.hiddenSize]; int numTokens = config.numTokens; int embeddingSize = config.embeddingSize; int offset = 0; for (int j = 0; j < feature.Length; j++) { int tok = feature[j]; int index = tok * numTokens + j; int idInteger = preMap[index]; if (idInteger != null) { ArrayMath.PairwiseAddInPlace(hidden, saved[idInteger]); } else { MatrixMultiplySliceSum(hidden, W1, E[tok], offset); } offset += embeddingSize; } AddCubeInPlace(hidden, b1); return(MatrixMultiply(W2, hidden)); }
private async void MergeBuffersGPU(float[] color, float[] normal, float[] p3D, float[] tex) { _pixelBufferImg.Clear((byte)0); var specular = Settings.Specular; Gpu.Default.For(0, _imgHeight * _imgWidth, i => { var k = i * 3; ColorPass(_enviromentLight, color, normal, p3D, tex, specular, k); ArrayMath.Clamp(color, 0, 255, k, 3); _pixelBufferImg[k + 0] = (byte)(color[k + 0]); _pixelBufferImg[k + 1] = (byte)(color[k + 1]); _pixelBufferImg[k + 2] = (byte)(color[k + 2]); }); Gpu.Free(color); Gpu.Free(normal); Gpu.Free(p3D); Gpu.Free(tex); await App.Current?.Dispatcher.InvokeAsync(() => { if (Bitmap == null) { return; } WriteToBitmap(Bitmap, _pixelBufferImg); }); }
/// <summary>Read the Word2Vec word vector flat txt file.</summary> /// <param name="file">The word2vec text file.</param> /// <returns>The word vectors in the file.</returns> public static VectorMap ReadWord2Vec(string file) { VectorMap vectors = new VectorMap(); int dim = -1; foreach (string line in IOUtils.ReadLines(file)) { string[] split = line.ToLower().Split("\\s+"); if (split.Length < 100) { continue; } float[] vector = new float[split.Length - 1]; if (dim == -1) { dim = vector.Length; } System.Diagnostics.Debug.Assert(dim == vector.Length); for (int i = 1; i < split.Length; i++) { vector[i - 1] = float.ParseFloat(split[i]); } ArrayMath.L2normalize(vector); vectors[split[0]] = vector; } return(vectors); }
public virtual double ComputeStochastic(double[] x, double[] grad, double fractionOfData) { if (type == LogPrior.LogPriorType.Adapt) { double[] newX = ArrayMath.PairwiseSubtract(x, means); return(otherPrior.ComputeStochastic(newX, grad, fractionOfData)); } else { if (type == LogPrior.LogPriorType.MultipleQuadratic) { double[] sigmaSquaredOld = GetSigmaSquaredM(); double[] sigmaSquaredTemp = sigmaSquaredOld.MemberwiseClone(); for (int i = 0; i < x.Length; i++) { sigmaSquaredTemp[i] /= fractionOfData; } SetSigmaSquaredM(sigmaSquaredTemp); double val = Compute(x, grad); SetSigmaSquaredM(sigmaSquaredOld); return(val); } else { double sigmaSquaredOld = GetSigmaSquared(); SetSigmaSquared(sigmaSquaredOld / fractionOfData); double val = Compute(x, grad); SetSigmaSquared(sigmaSquaredOld); return(val); } } }
protected internal override void TakeStep(AbstractStochasticCachingDiffFunction dfunction) { dfunction.returnPreviousValues = true; System.Array.Copy(dfunction.HdotVAt(x, v, grad, bSize), 0, Hv, 0, Hv.Length); //Update the weights for (int i = 0; i < x.Length; i++) { meta = 1 - mu * grad[i] * v[i]; if (0.5 > meta) { gains[i] = gains[i] * 0.5; } else { gains[i] = gains[i] * meta; } //Update gain history v[i] = lam * (1 + cPosDef * gains[i]) * v[i] - gains[i] * (grad[i] + lam * Hv[i]); //Get the next X newX[i] = x[i] - gains[i] * grad[i]; } if (printMinMax) { Say("vMin = " + ArrayMath.Min(v) + " "); Say("vMax = " + ArrayMath.Max(v) + " "); Say("gainMin = " + ArrayMath.Min(gains) + " "); Say("gainMax = " + ArrayMath.Max(gains) + " "); } }
public virtual void TestFliterNaNAndInfinite() { double[] f_d3 = ArrayMath.FilterNaNAndInfinite(d3); NUnit.Framework.Assert.AreEqual(ArrayMath.NumRows(f_d3), 1); NUnit.Framework.Assert.AreEqual(ArrayMath.CountInfinite(f_d3), 0); NUnit.Framework.Assert.AreEqual(ArrayMath.CountNaN(f_d3), 0); }
private void ColorPass(float[] eniromentLight, float[] color, float[] normal, float[] p3D, float[] tex, bool specular, int from) { ArrayMath.Sub(_lightPosArray, p3D, _l, 0, from, from, 3); ArrayMath.Normalize(_l, from, 3); var dotNl = ArrayMath.Dot(normal, _l, from, from, 3); ArrayMath.Mul(color, Math.Max(0f, dotNl), _Id, from, from, 3); ArrayMath.Mul(normal, dotNl * 2, _r, from, from, 3); ArrayMath.Sub(_r, _l, _r, from, from, from, 3); ArrayMath.Normalize(_r, from, 3); ArrayMath.Normalize(p3D, from, 3); var rDotP3D = -ArrayMath.Dot(_r, p3D, from, from, 3); var Is = 0f; if (specular) { var max = Math.Max(0, rDotP3D); Is = DeviceFunction.Pow(max, 50); } ArrayMath.Mul(eniromentLight, color, color, 0, from, from, 3); ArrayMath.Mul(tex, _Id, tex, from, from, from, 3); ArrayMath.Add(color, tex, color, from, from, from, 3); ArrayMath.Add(color, Is, color, from, 3); ArrayMath.Mul(color, 255, color, from, from, 3); }
// // Create chart // private void createChart(RazorChartViewer viewer) { // 4 data points to represent the cash flow for the Q1 - Q4 double[] data = { 230, 140, 220, 330, 150 }; // We want to plot a waterfall chart showing the 4 quarters as well as the total string[] labels = { "Product 1", "Product 2", "Product 3", "Product 4", "Product 5", "Total" }; // The top side of the bars in a waterfall chart is the accumulated data. We use the // ChartDirector ArrayMath utility to accumulate the data. The "total" is handled by // inserting a zero point at the end before accumulation (after accumulation it will become // the total). double[] boxTop = new ArrayMath(data).insert2(0, 1).acc().result(); // The botom side of the bars is just the top side of the previous bar. So we shifted the top // side data to obtain the bottom side data. double[] boxBottom = new ArrayMath(boxTop).shift(1, 0).result(); // The last point (total) is different. Its bottom side is always 0. boxBottom[boxBottom.Length - 1] = 0; // Create a XYChart object of size 500 x 280 pixels. Set background color to light blue // (ccccff), with 1 pixel 3D border effect. XYChart c = new XYChart(500, 290, 0xccccff, 0x000000, 1); // Add a title to the chart using 13 points Arial Bold Itatic font, with white (ffffff) text // on a deep blue (0x80) background c.addTitle("Product Revenue - Year 2004", "Arial Bold Italic", 13, 0xffffff).setBackground( 0x000080); // Set the plotarea at (55, 50) and of size 430 x 215 pixels. Use alternative white/grey // background. c.setPlotArea(55, 45, 430, 215, 0xffffff, 0xeeeeee); // Set the labels on the x axis using Arial Bold font c.xAxis().setLabels(labels).setFontStyle("Arial Bold"); // Set the x-axis ticks and grid lines to be between the bars c.xAxis().setTickOffset(0.5); // Use Arial Bold as the y axis label font c.yAxis().setLabelStyle("Arial Bold"); // Add a title to the y axis c.yAxis().setTitle("USD (in millions)"); // Add a multi-color box-whisker layer to represent the waterfall bars BoxWhiskerLayer layer = c.addBoxWhiskerLayer2(boxTop, boxBottom); // Put data labels on the bars to show the cash flow using Arial Bold font layer.setDataLabelFormat("{={top}-{bottom}}M"); layer.setDataLabelStyle("Arial Bold").setAlignment(Chart.Center); // Output the chart viewer.Image = c.makeWebImage(Chart.PNG); // Include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("", "", "title='{xLabel}: {={top}-{bottom}} millions'"); }
public virtual void TestPairwiseSubtract() { double[] diff = ArrayMath.PairwiseSubtract(d1, d2); for (int i = 0; i < ArrayMath.NumRows(d1); i++) { NUnit.Framework.Assert.AreEqual(d1[i] - d2[i], 1e-5, diff[i]); } }
public virtual void TestPowInPlace() { ArrayMath.PowInPlace(d1, 3); for (int i = 0; i < ArrayMath.NumRows(d1); i++) { NUnit.Framework.Assert.AreEqual(System.Math.Pow(d2[i], 3), 1e-5, d1[i]); } }
public virtual void TestMultiplyInPlace() { ArrayMath.MultiplyInPlace(d1, 3); for (int i = 0; i < ArrayMath.NumRows(d1); i++) { NUnit.Framework.Assert.AreEqual(d2[i] * 3, 1e-5, d1[i]); } }
public virtual void TestInnerProduct() { double inner = ArrayMath.InnerProduct(d4, d4); NUnit.Framework.Assert.AreEqual("Wrong inner product", 0.14, inner, 1e-6); inner = ArrayMath.InnerProduct(d5, d5); NUnit.Framework.Assert.AreEqual("Wrong inner product", 0.78, inner, 1e-6); }
public virtual void TestSumAndMean() { NUnit.Framework.Assert.AreEqual(ArrayMath.Mean(d1) * d1.Length, 1e-5, ArrayMath.Sum(d1)); NUnit.Framework.Assert.AreEqual(ArrayMath.Mean(d2) * d2.Length, 1e-5, ArrayMath.Sum(d2)); NUnit.Framework.Assert.AreEqual(ArrayMath.Mean(d3) * d3.Length, 1e-5, ArrayMath.Sum(d3)); // comes out as NaN but works! NUnit.Framework.Assert.AreEqual(ArrayMath.Mean(d4) * d4.Length, 1e-5, ArrayMath.Sum(d4)); }
public virtual void TestPairwiseMultiply() { double[] product = ArrayMath.PairwiseMultiply(d1, d2); for (int i = 0; i < ArrayMath.NumRows(d1); i++) { NUnit.Framework.Assert.AreEqual(d1[i] * d2[i], 1e-5, product[i]); } }
public virtual void TestPairwiseAdd() { double[] sum = ArrayMath.PairwiseAdd(d1, d2); for (int i = 0; i < ArrayMath.NumRows(d1); i++) { NUnit.Framework.Assert.AreEqual(d1[i] + d2[i], 1e-5, sum[i]); } }
public virtual void TestPow() { double[] d1prime = ArrayMath.Pow(d1, 3); for (int i = 0; i < ArrayMath.NumRows(d1prime); i++) { NUnit.Framework.Assert.AreEqual(System.Math.Pow(d1[i], 3), 1e-5, d1prime[i]); } }
public virtual void TestMultiply() { double[] d1prime = ArrayMath.Multiply(d1, 3); for (int i = 0; i < ArrayMath.NumRows(d1prime); i++) { NUnit.Framework.Assert.AreEqual(d1[i] * 3, 1e-5, d1prime[i]); } }
public virtual void TestExpLog() { double[] d1prime = ArrayMath.Log(ArrayMath.Exp(d1)); double[] diff = ArrayMath.PairwiseSubtract(d1, d1prime); double norm2 = ArrayMath.Norm(diff); NUnit.Framework.Assert.AreEqual(norm2, 1e-5, 0.0); }
public virtual double[] GetConditionalDistribution(int[] sequence, int position) { double[] probs = ScoresOf(sequence, position); ArrayMath.LogNormalize(probs); probs = ArrayMath.Exp(probs); //System.out.println(this); return(probs); }
/** * Gets arrays {xyz,uvw,rgb} of cell coordinates, normals and colors. In * these arrays, the specified cells are represented by quads with specified * size, and colors corresponding to a property gotten from each cell. * @param size the size (in samples) of the quads. * @param cmap the colormap used to compute rgb colors from floats. * @param cells the cells for which to compute quads. * @param get1 used to get the float from a cell to be colormapped. * @param lhc true, if left-handed coordinate system; false, otherwise. * @return arrays {xyz,uvw,rgb}. */ private static float[][] getXyzUvwRgb( float size, ColorMap cmap, FaultCell[] cells, Get1 get1, bool lhc) { FloatList xyz = new FloatList(); FloatList uvw = new FloatList(); FloatList fcl = new FloatList(); size *= 0.5f; float[] qa = { 0.0f, -size, -size }; float[] qb = { 0.0f, size, -size }; float[] qc = { 0.0f, size, size }; float[] qd = { 0.0f, -size, size }; if (lhc) { float[] qt = qb; qb = qc; qc = qt; qt = qd; qa = qd; qd = qt; } foreach (FaultCell cell in cells) { float x1 = cell.x1; float x2 = cell.x2; float x3 = cell.x3; float w1 = cell.w1; float w2 = cell.w2; float w3 = cell.w3; float fp = ArrayMath.toRadians(cell.fp); float ft = ArrayMath.toRadians(cell.ft); float cp = (float)Math.Cos(fp); float sp = (float)Math.Sin(fp); float ct = (float)Math.Cos(ft); float st = (float)Math.Sin(ft); float[] ra = rotatePoint(cp, sp, ct, st, qa); float[] rb = rotatePoint(cp, sp, ct, st, qb); float[] rc = rotatePoint(cp, sp, ct, st, qc); float[] rd = rotatePoint(cp, sp, ct, st, qd); float a1 = x1 + ra[0], a2 = x2 + ra[1], a3 = x3 + ra[2]; float b1 = x1 + rb[0], b2 = x2 + rb[1], b3 = x3 + rb[2]; float c1 = x1 + rc[0], c2 = x2 + rc[1], c3 = x3 + rc[2]; float d1 = x1 + rd[0], d2 = x2 + rd[1], d3 = x3 + rd[2]; xyz.add(a3); xyz.add(a2); xyz.add(a1); xyz.add(b3); xyz.add(b2); xyz.add(b1); xyz.add(c3); xyz.add(c2); xyz.add(c1); xyz.add(d3); xyz.add(d2); xyz.add(d1); uvw.add(w3); uvw.add(w2); uvw.add(w1); uvw.add(w3); uvw.add(w2); uvw.add(w1); uvw.add(w3); uvw.add(w2); uvw.add(w1); uvw.add(w3); uvw.add(w2); uvw.add(w1); float fc = get1(cell); fcl.add(fc); fcl.add(fc); fcl.add(fc); fcl.add(fc); } float[] fc2 = fcl.trim(); float[] rgb = cmap.getRgbFloats(fc2); return(new float[][] { xyz.trim(), uvw.trim(), rgb }); }
/* * This function is used to get a lower bound on the condition number. as it stands this is pretty straight forward: * * a random point (x) and vector (v) are generated, the Raleigh quotient ( v.H(x).v / v.v ) is then taken which provides both * a lower bound on the largest eigenvalue, and an upper bound on the smallest eigenvalue. This can then be used to * come up with a lower bound on the condition number of the hessian. */ public virtual double TestConditionNumber(int samples) { double maxSeen = 0.0; double minSeen = 0.0; double[] thisV = new double[thisFunc.DomainDimension()]; double[] thisX = new double[thisV.Length]; gradFD = new double[thisV.Length]; HvFD = new double[thisV.Length]; double thisVHV; bool isNeg = false; bool isPos = false; bool isSemi = false; thisFunc.method = StochasticCalculateMethods.ExternalFiniteDifference; for (int j = 0; j < samples; j++) { for (int i = 0; i < thisV.Length; i++) { thisV[i] = generator.NextDouble(); } for (int i_1 = 0; i_1 < thisX.Length; i_1++) { thisX[i_1] = generator.NextDouble(); } log.Info("Evaluating Hessian Product"); System.Array.Copy(thisFunc.DerivativeAt(thisX, thisV, testBatchSize), 0, gradFD, 0, gradFD.Length); thisFunc.recalculatePrevBatch = true; System.Array.Copy(thisFunc.HdotVAt(thisX, thisV, gradFD, testBatchSize), 0, HvFD, 0, HvFD.Length); thisVHV = ArrayMath.InnerProduct(thisV, HvFD); if (System.Math.Abs(thisVHV) > maxSeen) { maxSeen = System.Math.Abs(thisVHV); } if (System.Math.Abs(thisVHV) < minSeen) { minSeen = System.Math.Abs(thisVHV); } if (thisVHV < 0) { isNeg = true; } if (thisVHV > 0) { isPos = true; } if (thisVHV == 0) { isSemi = true; } log.Info("It:" + j + " C:" + maxSeen / minSeen + "N:" + isNeg + "P:" + isPos + "S:" + isSemi); } System.Console.Out.WriteLine("Condition Number of: " + maxSeen / minSeen); System.Console.Out.WriteLine("Is negative: " + isNeg); System.Console.Out.WriteLine("Is positive: " + isPos); System.Console.Out.WriteLine("Is semi: " + isSemi); return(maxSeen / minSeen); }
protected internal virtual Pair <double[][][], double[][][]> GetCondProbs(CRFCliqueTree <string> cTree, int[][][] docData) { // first index position is curr index, second index curr-class, third index prev-class // e.g. [1][2][3] means curr is at position 1 with class 2, prev is at position 0 with class 3 double[][][] prevGivenCurr = new double[docData.Length][][]; // first index position is curr index, second index curr-class, third index next-class // e.g. [0][2][3] means curr is at position 0 with class 2, next is at position 1 with class 3 double[][][] nextGivenCurr = new double[docData.Length][][]; for (int i = 0; i < docData.Length; i++) { prevGivenCurr[i] = new double[numClasses][]; nextGivenCurr[i] = new double[numClasses][]; for (int j = 0; j < numClasses; j++) { prevGivenCurr[i][j] = new double[numClasses]; nextGivenCurr[i][j] = new double[numClasses]; } } // computing prevGivenCurr and nextGivenCurr for (int i_1 = 0; i_1 < docData.Length; i_1++) { int[] labelPair = new int[2]; for (int l1 = 0; l1 < numClasses; l1++) { labelPair[0] = l1; for (int l2 = 0; l2 < numClasses; l2++) { labelPair[1] = l2; double prob = cTree.LogProb(i_1, labelPair); // log.info(prob); if (i_1 - 1 >= 0) { nextGivenCurr[i_1 - 1][l1][l2] = prob; } prevGivenCurr[i_1][l2][l1] = prob; } } for (int j = 0; j < numClasses; j++) { if (i_1 - 1 >= 0) { // ArrayMath.normalize(nextGivenCurr[i-1][j]); ArrayMath.LogNormalize(nextGivenCurr[i_1 - 1][j]); for (int k = 0; k < nextGivenCurr[i_1 - 1][j].Length; k++) { nextGivenCurr[i_1 - 1][j][k] = System.Math.Exp(nextGivenCurr[i_1 - 1][j][k]); } } // ArrayMath.normalize(prevGivenCurr[i][j]); ArrayMath.LogNormalize(prevGivenCurr[i_1][j]); for (int k_1 = 0; k_1 < prevGivenCurr[i_1][j].Length; k_1++) { prevGivenCurr[i_1][j][k_1] = System.Math.Exp(prevGivenCurr[i_1][j][k_1]); } } } return(new Pair <double[][][], double[][][]>(prevGivenCurr, nextGivenCurr)); }
// extracting these small methods makes things faster; hotspot likes them private static double[] MatrixMultiply(double[][] matrix, double[] vector) { double[] result = new double[matrix.Length]; for (int i = 0; i < matrix.Length; i++) { result[i] = ArrayMath.DotProduct(matrix[i], vector); } return(result); }
/// <summary> /// Merge the given /// <c>Cost</c> /// data with the data in this /// instance. /// </summary> /// <param name="otherCost"/> public virtual void Merge(Classifier.Cost otherCost) { this.cost += otherCost.GetCost(); this.percentCorrect += otherCost.GetPercentCorrect(); ArrayMath.AddInPlace(this.gradW1, otherCost.GetGradW1()); ArrayMath.PairwiseAddInPlace(this.gradb1, otherCost.GetGradb1()); ArrayMath.AddInPlace(this.gradW2, otherCost.GetGradW2()); ArrayMath.AddInPlace(this.gradE, otherCost.GetGradE()); }
public virtual void TestJensenShannon() { double[] a = new double[] { 0.1, 0.1, 0.7, 0.1, 0.0, 0.0 }; double[] b = new double[] { 0.0, 0.1, 0.1, 0.7, 0.1, 0.0 }; NUnit.Framework.Assert.AreEqual(ArrayMath.JensenShannonDivergence(a, b), 1e-5, 0.46514844544032313); double[] c = new double[] { 1.0, 0.0, 0.0 }; double[] d = new double[] { 0.0, 0.5, 0.5 }; NUnit.Framework.Assert.AreEqual(ArrayMath.JensenShannonDivergence(c, d), 1e-5, 1.0); }
public virtual void TestExpLogInplace() { ArrayMath.ExpInPlace(d1); ArrayMath.LogInPlace(d1); ArrayMath.PairwiseSubtractInPlace(d1, d2); double norm2 = ArrayMath.Norm(d1); NUnit.Framework.Assert.AreEqual(norm2, 1e-5, 0.0); }
private void UpdateM2() { double[] calcData; if (radioM1FromGraphData.Checked == true) { calcData = chartLayers.Items[0].subDataGetTable(); } else { CDetector.session ses = detector.getSession(sourceIp, sourcePort, destinationIp, destinationPort); calcData = ses.getLastNTimes(ses.Packets.Count); } if (calcData.Length == 0) { txtRegularity.Text = ""; } int blockSize = (int)Math.Ceiling((double)calcData.Count() / (double)txtWindowsCount.Value); txtWindowSize.Value = blockSize; double[] S = new double[(int)txtWindowsCount.Value]; for (int i = 0; i < S.Count(); i++) { double[] tmp = Util.subTable(calcData, i * blockSize, (i + 1) * blockSize); double Si = new ArrayMath(tmp).stdDev(); S[i] = Si; } double[] X = new double[S.Count() * S.Count() - S.Count()]; int cnt = 0; for (int i = 0; i < S.Count(); i++) { for (int j = 0; j < S.Count(); j++) { if (i != j) { double tmp = Math.Abs(S[i] - S[j]);// / S[i]; if (double.IsNaN(tmp)) { tmp = double.MaxValue; } X[cnt++] = tmp; } } } txtRegularity.Text = new ArrayMath(X).stdDev().ToString(); }
public override void UpdateVisualization() { List<string>[] chartLabels = null; double[] dataArray = null; Image img = new Image(); int i = 0; List<string> xLabels = new List<string>(); List<string> yLabels = new List<string>(); // Get the Factor Labels chartLabels = GetConfigDisplayLabels(); if (chartLabels == null) { return; } int zCount = 0; if (chartLabels[2] == null) { zCount = 1; } else { zCount = chartLabels[2].Count; } xLabels.Add(""); for (i = 0; i < chartLabels[0].Count; i++) { xLabels.Add(chartLabels[0][i]); } xLabels.Add(""); yLabels.Add(""); for (i = 0; i < chartLabels[1].Count; i++) { yLabels.Add(chartLabels[1][i]); } yLabels.Add(""); // Obtain grid if ((configDisplayPanel.Children == null) || (configDisplayPanel.Children.Count != 1) || (!(configDisplayPanel.Children[0] is Grid))) { return; } Grid grid = configDisplayPanel.Children[0] as Grid; // Clear the grid images List<UIElement> removalList = new List<UIElement>(); foreach (UIElement child in grid.Children) { if (child is Image) { removalList.Add(child); } } if (removalList.Count > 0) { foreach (UIElement child in removalList) { grid.Children.Remove(child); } } // Create the Stacked Histogram XYChart c = new XYChart(configDisplay.Width, configDisplay.Height); c.setPlotArea(100, 0, configDisplay.Width - 200, configDisplay.Height - 100); c.xAxis().setLabels(xLabels.ToArray()); c.yAxis().setLabels(yLabels.ToArray()); c.xAxis().setTickOffset(-0.5); c.yAxis().setTickOffset(-0.5); // Create x and y value list List<double>[] dataX = new List<double>[zCount]; List<double>[] dataY = new List<double>[zCount]; for (int z = 0; z < zCount; z++) { dataX[z] = new List<double>(); dataY[z] = new List<double>(); for (int y = 0; y < chartLabels[1].Count; y++) { for (int x = 0; x < chartLabels[0].Count; x++) { dataX[z].Add(x + 1 + xOffsets[z]); dataY[z].Add(y + 1 + yOffsets[z]); } } } List<double> dataZ = new List<double>(); double maxBubbleSize = (configDisplay.Width - 200) / xLabels.Count / 2.0; i = 0; for (int z = 0; z < zCount; z++) { if (GetConfigDisplayData(i, ref dataArray)) { ArrayMath a = new ArrayMath(dataArray.ToArray()); double maxValue = a.max(); if (maxValue < maxBubbleSize) { for (int j = 0; j < dataArray.Length; j++) { if (dataArray[j] == 0) { dataZ.Add(0.0); } else { dataZ.Add(dataArray[j] + 10); } } } else { for (int j = 0; j < dataArray.Length; j++) { if (dataArray[j] == 0) { dataZ.Add(0.0); } else { dataZ.Add(dataArray[j] / maxValue * maxBubbleSize + 10); } } } } i++; if (chartLabels[2] == null) { c.addScatterLayer(dataX[z].ToArray(), dataY[z].ToArray(), configDisplay.MetricName, Chart.CircleSymbol, 9, (int) barcodeColorsTrans[z], (int) barcodeColorsTrans[z]).setSymbolScale(dataZ.ToArray()); } else { c.addScatterLayer(dataX[z].ToArray(), dataY[z].ToArray(), chartLabels[2][z], Chart.CircleSymbol, 9, (int) barcodeColorsTrans[z], (int) barcodeColorsTrans[z]).setSymbolScale(dataZ.ToArray()); } dataZ.Clear(); } // Generate an image of the chart System.Drawing.Image imgWinForms = c.makeImage(); BitmapImage bi = new BitmapImage(); bi.BeginInit(); MemoryStream ms = new MemoryStream(); // Save to a memory stream... imgWinForms.Save(ms, ImageFormat.Bmp); // Rewind the stream... ms.Seek(0, SeekOrigin.Begin); // Tell the WPF image to use this stream... bi.StreamSource = ms; bi.EndInit(); img.Source = bi; img.Stretch = Stretch.Uniform; Grid.SetColumn(img, 0); Grid.SetRow(img, 2); grid.Children.Add(img); }