public void TestViewColumn() { var a = DoubleFactory2D.Dense.Ascending(5, 4); DoubleMatrix1D d = a.ViewColumn(0); Assert.AreEqual(5, d.Size); Assert.AreEqual(1, d[0]); Assert.AreEqual(5, d[1]); Assert.AreEqual(9, d[2]); Assert.AreEqual(13, d[3]); Assert.AreEqual(17, d[4]); var b = new DenseDoubleMatrix2D(new[] { new[] { 1d, 0d, 0d, 1d, 0d, 0d, 0d, 0d, 0d }, new[] { 1d, 0d, 1d, 0d, 0d, 0d, 0d, 0d, 0d }, new[] { 1d, 1d, 0d, 0d, 0d, 0d, 0d, 0d, 0d }, new[] { 0d, 1d, 1d, 0d, 1d, 0d, 0d, 0d, 0d }, new[] { 0d, 1d, 1d, 2d, 0d, 0d, 0d, 0d, 0d }, new[] { 0d, 1d, 0d, 0d, 1d, 0d, 0d, 0d, 0d }, new[] { 0d, 1d, 0d, 0d, 1d, 0d, 0d, 0d, 0d }, new[] { 0d, 0d, 1d, 1d, 0d, 0d, 0d, 0d, 0d }, new[] { 0d, 1d, 0d, 0d, 0d, 0d, 0d, 0d, 1d }, new[] { 0d, 0d, 0d, 0d, 0d, 1d, 1d, 1d, 0d }, new[] { 0d, 0d, 0d, 0d, 0d, 0d, 1d, 1d, 1d }, new[] { 0d, 0d, 0d, 0d, 0d, 0d, 0d, 1d, 1d }, }); d = b.ViewColumn(0); Assert.AreEqual(1, d[0]); Assert.AreEqual(1, d[1]); Assert.AreEqual(1, d[2]); Assert.AreEqual(0, d[3]); }
/// <summary> /// Constructs and returns the covariance matrix of the given matrix. /// The covariance matrix is a square, symmetric matrix consisting of nothing but covariance coefficientsd /// The rows and the columns represent the variables, the cells represent covariance coefficientsd /// The diagonal cells (i.ed the covariance between a variable and itself) will equal the variances. /// The covariance of two column vectors x and y is given by <i>cov(x,y) = (1/n) * Sum((x[i]-mean(x)) * (y[i]-mean(y)))</i>. /// See the <A HREF="http://www.cquest.utoronto.ca/geog/ggr270y/notes/not05efg.html"> math definition</A>. /// Compares two column vectors at a timed Use dice views to compare two row vectors at a time. /// </summary> /// <param name="matrix">any matrix; a column holds the values of a given variable.</param> /// <returns>the covariance matrix (<i>n x n, n=matrix.Columns</i>).</returns> public static DoubleMatrix2D Covariance(DoubleMatrix2D matrix) { int rows = matrix.Rows; int columns = matrix.Columns; DoubleMatrix2D covariance = new DenseDoubleMatrix2D(columns, columns); double[] sums = new double[columns]; DoubleMatrix1D[] cols = new DoubleMatrix1D[columns]; for (int i = columns; --i >= 0;) { cols[i] = matrix.ViewColumn(i); sums[i] = cols[i].ZSum(); } for (int i = columns; --i >= 0;) { for (int j = i + 1; --j >= 0;) { double sumOfProducts = cols[i].ZDotProduct(cols[j]); double cov = (sumOfProducts - sums[i] * sums[j] / rows) / rows; covariance[i, j] = cov; covariance[j, i] = cov; // symmetric } } return(covariance); }
public void TestRandomly() { const int Runs = 100; var gen = new Random(); for (int run = 0; run < Runs; run++) { const int MaxSize = 50; int size = gen.Next(1, MaxSize); int from, to; if (size == 0) { from = 0; to = -1; } else { from = gen.Next(0, size - 1); to = gen.Next(Math.Min(from, size - 1), size - 1); } var a1 = new DenseDoubleMatrix2D(size, size); var p1 = a1.ViewPart(from, from, size - to, size - to); int intervalFrom = gen.Next(size / 2, 2 * size); int intervalTo = gen.Next(intervalFrom, 2 * size); for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { a1[i, j] = gen.Next(intervalFrom, intervalTo); } } var a2 = a1.Copy(); var p2 = a2.ViewPart(from, from, size - to, size - to); const int Column = 0; var s1 = Cern.Colt.Matrix.DoubleAlgorithms.Sorting.QuickSort.Sort(p1, Column); var s2 = Cern.Colt.Matrix.DoubleAlgorithms.Sorting.MergeSort.Sort(p2, Column); var v1 = s1.ViewColumn(Column); var sv1 = v1.ToString(); var v2 = s2.ViewColumn(Column); var sv2 = v2.ToString(); Assert.IsTrue(v1.Equals(v2)); } }
public void TestDiagonal() { var a = new DenseDoubleMatrix2D(new[] { new[] { 5d, 2d, 4d }, new[] { -3d, 6d, 2d }, new[] { 3d, -3d, 1d } }); var principal = DoubleFactory2D.Dense.Diagonal(a); Assert.AreEqual(5d, principal[0]); Assert.AreEqual(6d, principal[1]); Assert.AreEqual(1d, principal[2]); Assert.AreEqual(12d, Algebra.Trace(a)); }
/// <summary> /// Returns a string representation of the given argument. //// public String ToString(IHistogram1D h) { String columnAxisName = null; //"X"; String rowAxisName = null; Hep.Aida.Bin.BinFunction1D[] aggr = null; //{Hep.Aida.Bin.BinFunctions1D.sum}; String format = "G"; //"%G" //String format = "%1.2G"; Cern.Colt.Matrix.Former f = new Cern.Colt.Matrix.Implementation.FormerFactory().Create(format); String sep = "\n\r"; //"\n\r"; //System.getProperty("line.separator"); int[] minMaxBins = h.MinMaxBins; String title = h.Title + ":" + sep + " Entries=" + Form(f, h.Entries) + ", ExtraEntries=" + Form(f, h.ExtraEntries) + sep + " Mean=" + Form(f, h.Mean) + ", Rms=" + Form(f, h.Rms) + sep + " MinBinHeight=" + Form(f, h.BinHeight(minMaxBins[0])) + ", MaxBinHeight=" + Form(f, h.BinHeight(minMaxBins[1])) + sep + " Axis: " + "Bins=" + Form(f, h.XAxis.Bins) + ", Min=" + Form(f, h.XAxis.LowerEdge) + ", Max=" + Form(f, h.XAxis.UpperEdge); String[] xEdges = new String[h.XAxis.Bins]; for (int i = 0; i < h.XAxis.Bins; i++) { xEdges[i] = Form(f, h.XAxis.BinLowerEdge(i)); } String[] yEdges = new[] { "Data" }; Cern.Colt.Matrix.DoubleMatrix2D heights = new DenseDoubleMatrix2D(1, h.XAxis.Bins); heights.ViewRow(0).Assign(ToArrayHeights(h)); //Cern.Colt.Matrix.DoubleMatrix2D errors = new Cern.Colt.Matrix.DenseDoubleMatrix2D(1,h.XAxis.Bins); //errors.ViewRow(0).Assign(toArrayErrors(h)); return(title + sep + "Heights:" + sep + new Formatter().ToTitleString(heights, yEdges, xEdges, rowAxisName, columnAxisName, null, aggr)); /* + sep + + "Errors:" + sep + + new Cern.Colt.Matrix.doublealgo.Formatter().ToTitleString( + errors,yEdges,xEdges,rowAxisName,columnAxisName,null,aggr); */ }
/// <summary> /// Linear algebraic matrix-matrix multiplication; <tt>C = alpha * A x B + beta*C</tt>. /// </summary> /// <param name="b"> /// The second source matrix. /// </param> /// <param name="c"> /// The matrix where results are to be storedd Set this parameter to <tt>null</tt> to indicate that a new result matrix shall be constructed. /// </param> /// <param name="alpha"> /// The alpha. /// </param> /// <param name="beta"> /// The beta. /// </param> /// <param name="transposeA"> /// Whether A must be transposed. /// </param> /// <param name="transposeB"> /// Whether B must be transposed. /// </param> /// <returns> /// C (for convenience only). /// </returns> /// <exception cref="ArgumentOutOfRangeException"> /// If <tt>B.rows() != A.columns()</tt>. /// </exception> /// <exception cref="ArgumentException"> /// If <tt>C.rows() != A.rows() || C.columns() != B.columns()</tt>. /// </exception> /// <exception cref="ArithmeticException"> /// If <tt>A == C || B == C</tt>. /// </exception> public virtual DoubleMatrix2D ZMult(DoubleMatrix2D b, DoubleMatrix2D c, double alpha, double beta, bool transposeA, bool transposeB) { if (transposeA) { return(ViewDice().ZMult(b, c, alpha, beta, false, transposeB)); } if (transposeB) { return(ZMult(b.ViewDice(), c, alpha, beta, false, false)); } int m = Rows; int n = Columns; int p = b.Columns; if (c == null) { c = new DenseDoubleMatrix2D(m, p); } if (b.Rows != n) { throw new ArgumentOutOfRangeException("b", String.Format(Cern.LocalizedResources.Instance().Exception_Matrix2DInnerDimensionMustAgree, this, b)); } if (c.Rows != m || c.Columns != p) { throw new ArgumentException(String.Format(Cern.LocalizedResources.Instance().Exception_IncompatibleResultMatrix, this, b, c)); } if (this == c || b == c) { throw new ArithmeticException(Cern.LocalizedResources.Instance().Exception_MatricesMustNotBeIdentical); } for (int j = p; --j >= 0;) { for (int i = m; --i >= 0;) { double s = 0; for (int k = n; --k >= 0;) { s += this[i, k] * b[k, j]; } c[i, j] = (alpha * s) + (beta * c[i, j]); } } return(c); }
/// <summary> /// Linear algebraic matrix-matrix multiplication; <tt>C = alpha * A x B + beta*C</tt>. /// </summary> /// <param name="b"> /// The second source matrix. /// </param> /// <param name="c"> /// The matrix where results are to be storedd Set this parameter to <tt>null</tt> to indicate that a new result matrix shall be constructed. /// </param> /// <param name="alpha"> /// The alpha. /// </param> /// <param name="beta"> /// The beta. /// </param> /// <param name="transposeA"> /// Whether A must be transposed. /// </param> /// <param name="transposeB"> /// Whether B must be transposed. /// </param> /// <returns> /// C (for convenience only). /// </returns> /// <exception cref="ArgumentOutOfRangeException"> /// If <tt>B.rows() != A.columns()</tt>. /// </exception> /// <exception cref="ArgumentException"> /// If <tt>C.rows() != A.rows() || C.columns() != B.columns()</tt>. /// </exception> /// <exception cref="ArithmeticException"> /// If <tt>A == C || B == C</tt>. /// </exception> public virtual DoubleMatrix2D ZMult(DoubleMatrix2D b, DoubleMatrix2D c, double alpha, double beta, bool transposeA, bool transposeB) { if (transposeA) { return(ViewDice().ZMult(b, c, alpha, beta, false, transposeB)); } if (transposeB) { return(ZMult(b.ViewDice(), c, alpha, beta, false, false)); } int m = Rows; int n = Columns; int p = b.Columns; if (c == null) { c = new DenseDoubleMatrix2D(m, p); } if (b.Rows != n) { throw new ArgumentOutOfRangeException("b", "Matrix2D inner dimensions must agree:" + this + ", " + b); } if (c.Rows != m || c.Columns != p) { throw new ArgumentException("Incompatible result matrix: " + this + ", " + b + ", " + c); } if (this == c || b == c) { throw new ArithmeticException("Matrices must not be identical"); } for (int j = p; --j >= 0;) { for (int i = m; --i >= 0;) { double s = 0; for (int k = n; --k >= 0;) { s += this[i, k] * b[k, j]; } c[i, j] = (alpha * s) + (beta * c[i, j]); } } return(c); }
public void TestOperations() { var a = new DenseDoubleMatrix2D(new[] { new[] { 3d, 6d }, new[] { 5d, 8d }, new[] { -2d, 9d } }); var b = new DenseDoubleMatrix2D(new[] { new[] { -6d, 1d }, new[] { 0d, 9d }, new[] { 8d, 3d } }); Assert.AreEqual(new DenseDoubleMatrix2D(new[] { new[] { -3d, 7d }, new[] { 5d, 17d }, new[] { 6d, 12d } }), a.Copy().Assign(b, BinaryFunctions.Plus)); Assert.AreEqual(new DenseDoubleMatrix2D(new[] { new[] { 9d, 5d }, new[] { 5d, -1d }, new[] { -10d, 6d } }), a.Copy().Assign(b, BinaryFunctions.Minus)); Assert.AreEqual(new DenseDoubleMatrix2D(new[] { new[] { 6d, 12d }, new[] { 10d, 16d }, new[] { -4d, 18d } }), a.Copy().Assign(UnaryFunctions.Mult(2d))); Assert.AreEqual(new DenseDoubleMatrix2D(new[] { new[] { 1.5d, 3d }, new[] { 2.5d, 4d }, new[] { -1d, 4.5d } }), a.Copy().Assign(UnaryFunctions.Div(2d))); var c = new DenseDoubleMatrix2D( new[] { new[] { 4d, 1d, 9d }, new[] { 6d, 2d, 8d }, new[] { 7d, 3d, 5d }, new[] { 11d, 10d, 12d } }); var d = new DenseDoubleMatrix2D(new[] { new[] { 2d, 9d }, new[] { 5d, 12d }, new[] { 8d, 10d } }); Assert.AreEqual( new DenseDoubleMatrix2D(new[] { new[] { 85d, 138d }, new[] { 86d, 158d }, new[] { 69d, 149d }, new[] { 168d, 339d } }), Algebra.Mult(c, d)); }
/// <summary> /// Constructs and returns the distance matrix of the given matrix. /// The distance matrix is a square, symmetric matrix consisting of nothing but distance coefficientsd /// The rows and the columns represent the variables, the cells represent distance coefficientsd /// The diagonal cells (i.ed the distance between a variable and itself) will be zero. /// Compares two column vectors at a timed Use dice views to compare two row vectors at a time. /// </summary> /// <param name="matrix">any matrix; a column holds the values of a given variable (vector).</param> /// <param name="distanceFunction">(EUCLID, CANBERRA, ..d, or any user defined distance function operating on two vectors).</param> /// <returns>the distance matrix (<i>n x n, n=matrix.Columns</i>).</returns> public static DoubleMatrix2D Distance(DoubleMatrix2D matrix, VectorVectorFunction distanceFunction) { int columns = matrix.Columns; DoubleMatrix2D distance = new DenseDoubleMatrix2D(columns, columns); // cache views DoubleMatrix1D[] cols = new DoubleMatrix1D[columns]; for (int i = columns; --i >= 0;) { cols[i] = matrix.ViewColumn(i); } // work out all permutations for (int i = columns; --i >= 0;) { for (int j = i; --j >= 0;) { double d = distanceFunction(cols[i], cols[j]); distance[i, j] = d; distance[j, i] = d; // symmetric } } return(distance); }
/// <summary> /// Returns a string representation of the given argument. //// public String ToString(IHistogram2D h) { String columnAxisName = "X"; String rowAxisName = "Y"; Hep.Aida.Bin.BinFunction1D[] aggr = { Hep.Aida.Bin.BinFunctions1D.Sum }; String format = "G"; // "%G" //String format = "%1.2G"; Cern.Colt.Matrix.Former f = new Cern.Colt.Matrix.Implementation.FormerFactory().Create(format); String sep = "\n\r"; //System.getProperty("line.separator"); int[] minMaxBins = h.MinMaxBins; String title = h.Title + ":" + sep + " Entries=" + Form(f, h.Entries) + ", ExtraEntries=" + Form(f, h.ExtraEntries) + sep + " MeanX=" + Form(f, h.MeanX) + ", RmsX=" + Form(f, h.RmsX) + sep + " MeanY=" + Form(f, h.MeanY) + ", RmsY=" + Form(f, h.RmsX) + sep + " MinBinHeight=" + Form(f, h.BinHeight(minMaxBins[0], minMaxBins[1])) + ", MaxBinHeight=" + Form(f, h.BinHeight(minMaxBins[2], minMaxBins[3])) + sep + " xAxis: " + "Bins=" + Form(f, h.XAxis.Bins) + ", Min=" + Form(f, h.XAxis.LowerEdge) + ", Max=" + Form(f, h.XAxis.UpperEdge) + sep + " yAxis: " + "Bins=" + Form(f, h.YAxis.Bins) + ", Min=" + Form(f, h.YAxis.LowerEdge) + ", Max=" + Form(f, h.YAxis.UpperEdge); String[] xEdges = new String[h.XAxis.Bins]; for (int i = 0; i < h.XAxis.Bins; i++) { xEdges[i] = Form(f, h.XAxis.BinLowerEdge(i)); } String[] yEdges = new String[h.YAxis.Bins]; for (int i = 0; i < h.YAxis.Bins; i++) { yEdges[i] = Form(f, h.YAxis.BinLowerEdge(i)); } new List <Object>(yEdges).Reverse(); // keep coordd system Cern.Colt.Matrix.DoubleMatrix2D heights = new DenseDoubleMatrix2D(ToArrayHeights(h)); heights = heights.ViewDice().ViewRowFlip(); // keep the histo coordd system //heights = heights.ViewPart(1,1,heights.Rows()-2,heights.columns()-2); // ignore under&overflows //Cern.Colt.Matrix.DoubleMatrix2D errors = new Cern.Colt.Matrix.DenseDoubleMatrix2D(toArrayErrors(h)); //errors = errors.ViewDice().ViewRowFlip(); // keep the histo coord system ////errors = errors.ViewPart(1,1,errors.Rows()-2,errors.columns()-2); // ignore under&overflows return(title + sep + "Heights:" + sep + new Formatter().ToTitleString( heights, yEdges, xEdges, rowAxisName, columnAxisName, null, aggr)); /* + sep + + "Errors:" + sep + + new Cern.Colt.Matrix.doublealgo.Formatter().ToTitleString( + errors,yEdges,xEdges,rowAxisName,columnAxisName,null,aggr); */ }