/// <summary> /// Returns a string representation of the given matrix. /// </summary> /// <param name="matrix">the matrix to convert.</param> /// <returns></returns> public String ToString(ObjectMatrix1D matrix) { ObjectMatrix2D easy = matrix.Like2D(1, matrix.Size); easy.ViewRow(0).Assign(matrix); return(ToString(easy)); }
/// <summary> /// Sorts the matrix slices into ascending order, according to the <i>natural ordering</i> of the matrix values in the given <i>[row,column]</i> position. /// The returned view is backed by this matrix, so changes in the returned view are reflected in this matrix, and vice-versa. /// To sort ranges use sub-ranging viewsd To sort by other dimensions, use dice viewsd To sort descending, use flip views ... /// <p> /// The algorithm compares two 2-d slices at a time, determinining whether one is smaller, equal or larger than the other. /// Comparison is based on the cell <i>[row,column]</i> within a slice. /// Let <i>A</i> and <i>B</i> be two 2-d slicesd Then we have the following rules /// <ul> /// <li><i>A < B iff A.Get(row,column) < B.Get(row,column)</i> /// <li><i>A == B iff A.Get(row,column) == B.Get(row,column)</i> /// <li><i>A > B iff A.Get(row,column) > B.Get(row,column)</i> /// </ul> /// /// @param matrix the matrix to be sorted. /// @param row the index of the row inducing the order. /// @param column the index of the column inducing the order. /// @return a new matrix view having slices sorted by the values of the slice view <i>matrix.viewRow(row).viewColumn(column)</i>. /// <b>Note that the original matrix is left unaffected.</b> /// @throws IndexOutOfRangeException if <i>row %lt; 0 || row >= matrix.Rows || column %lt; 0 || column >= matrix.Columns</i>. /// </summary> public ObjectMatrix3D sort(ObjectMatrix3D matrix, int row, int column) { if (row < 0 || row >= matrix.Rows) { throw new IndexOutOfRangeException("row=" + row + ", matrix=" + Formatter.Shape(matrix)); } if (column < 0 || column >= matrix.Columns) { throw new IndexOutOfRangeException("column=" + column + ", matrix=" + Formatter.Shape(matrix)); } int[] sliceIndexes = new int[matrix.Slices]; // indexes to reorder instead of matrix itself for (int i = sliceIndexes.Length; --i >= 0;) { sliceIndexes[i] = i; } ObjectMatrix1D sliceView = matrix.ViewRow(row).ViewColumn(column); IntComparator comp = new IntComparator((a, b) => { IComparable av = (IComparable)(sliceView[a]); IComparable bv = (IComparable)(sliceView[b]); int r = av.CompareTo(bv); return(r < 0 ? -1 : (r > 0 ? 1 : 0)); }); RunSort(sliceIndexes, 0, sliceIndexes.Length, comp); // view the matrix according to the reordered slice indexes // take all rows and columns in the original order return(matrix.ViewSelection(sliceIndexes, null, null)); }
/// <summary> /// Sorts the matrix rows into ascending order, according to the <i>natural ordering</i> of the matrix values in the given column. /// The returned view is backed by this matrix, so changes in the returned view are reflected in this matrix, and vice-versa. /// To sort ranges use sub-ranging viewsd To sort columns by rows, use dice viewsd To sort descending, use flip views ... /// <p> /// <b>Example:</b> /// <table border="1" cellspacing="0"> /// <tr nowrap> /// <td valign="top"><i>4 x 2 matrix: <br> /// 7, 6<br> /// 5, 4<br> /// 3, 2<br> /// 1, 0 <br> /// </i></td> /// <td align="left" valign="top"> /// <p><i>column = 0;<br> /// view = quickSort(matrix,column);<br> /// Console.WriteLine(view); </i><i><br> /// ==> </i></p> /// </td> /// <td valign="top"> /// <p><i>4 x 2 matrix:<br> /// 1, 0<br> /// 3, 2<br> /// 5, 4<br> /// 7, 6</i><br> /// The matrix IS NOT SORTED.<br> /// The new VIEW IS SORTED.</p> /// </td> /// </tr> /// </table> /// /// @param matrix the matrix to be sorted. /// @param column the index of the column inducing the order. /// @return a new matrix view having rows sorted by the given column. /// <b>Note that the original matrix is left unaffected.</b> /// @throws IndexOutOfRangeException if <i>column %lt; 0 || column >= matrix.Columns</i>. /// </summary> public ObjectMatrix2D sort(ObjectMatrix2D matrix, int column) { if (column < 0 || column >= matrix.Columns) { throw new IndexOutOfRangeException("column=" + column + ", matrix=" + Formatter.Shape(matrix)); } int[] rowIndexes = new int[matrix.Rows]; // row indexes to reorder instead of matrix itself for (int i = rowIndexes.Length; --i >= 0;) { rowIndexes[i] = i; } ObjectMatrix1D col = matrix.ViewColumn(column); IntComparator comp = new IntComparator((a, b) => { IComparable av = (IComparable)(col[a]); IComparable bv = (IComparable)(col[b]); int r = av.CompareTo(bv); return(r < 0 ? -1 : (r > 0 ? 1 : 0)); }); RunSort(rowIndexes, 0, rowIndexes.Length, comp); // view the matrix according to the reordered row indexes // take all columns in the original order return(matrix.ViewSelection(rowIndexes, null)); }
/// <summary> /// Assigns the result of a function to each cell; <i>x[i] = function(x[i],y[i])</i>. /// (Iterates downwards from <i>[size()-1]</i> to <i>[0]</i>). /// <p> /// <b>Example:</b> /// <pre> /// // assign x[i] = x[i]<sup>y[i]</sup> /// m1 = 0 1 2 3; /// m2 = 0 2 4 6; /// m1.assign(m2, Cern.Jet.Math.Functions.pow); /// --> /// m1 == 1 1 16 729 /// /// // for non-standard functions there is no shortcut: /// m1.assign(m2, /// new ObjectObjectFunction() { /// public Object apply(Object x, Object y) { return System.Math.Pow(x,y); } /// } /// ); /// </pre> /// For further examples, see the <a href="package-summary.html#FunctionObjects">package doc</a>. /// </summary> /// <param name="y">the secondary matrix to operate on.</param> /// <param name="function">a function object taking as first argument the current cell's value of <i>this</i>, and as second argument the current cell's value of <i>y</i>,</param> /// <returns><i>this</i> (for convenience only).</returns> /// <exception cref="ArgumentException">if <i>size() != y.Count</i>.</exception> /// <see cref="Cern.Jet.Math.Functions"/> public override ObjectMatrix1D Assign(ObjectMatrix1D y, Cern.Colt.Function.ObjectObjectFunction <Object> function) { // overriden for performance only if (!(y is DenseObjectMatrix1D)) { return(base.Assign(y, function)); } DenseObjectMatrix1D other = (DenseObjectMatrix1D)y; CheckSize(y); Object[] elems = this.Elements; Object[] otherElems = other.Elements; if (Elements == null || otherElems == null) { throw new NullReferenceException(); } int s = this.Stride; int ys = other.Stride; int index = base.Index(0); int otherIndex = other.Index(0); // the general case x[i] = f(x[i],y[i]) for (int k = Size; --k >= 0;) { elems[index] = function(elems[index], otherElems[otherIndex]); index += s; otherIndex += ys; } return(this); }
/// <summary> /// Converts a given cell to a String; no alignment considered. /// </summary> /// <param name="matrix"></param> /// <param name="index"></param> /// <param name="formatter"></param> /// <returns></returns> protected String Form(ObjectMatrix1D matrix, int index, Former formatter) { Object value = matrix[index]; if (value == null) { return(""); } return(value.ValueOf()); }
/// <summary> /// Converts a given cell to a String; no alignment considered. /// </summary> /// <param name="matrix"></param> /// <param name="index"></param> /// <returns></returns> protected String Form(ObjectMatrix1D matrix, int index) { Object value = matrix[index]; if (value == null) { return(""); } return(value.ToString()); }
/// <summary> /// Returns a string <i>s</i> such that <i>Object[] m = s</i> is a legal C# statement. /// </summary> /// <param name="matrix">the matrix to format.</param> /// <returns></returns> public String ToSourceCode(ObjectMatrix1D matrix) { Formatter copy = (Formatter)this.Clone(); copy.SetPrintShape(false); copy.SetColumnSeparator(", "); String lead = "{"; String trail = "};"; return(lead + copy.ToString(matrix) + trail); }
/// <summary> /// Returns <i>true</i> if both matrices share at least one identical cell. /// </summary> protected new Boolean HaveSharedCellsRaw(ObjectMatrix1D other) { if (other is SelectedSparseObjectMatrix1D) { SelectedSparseObjectMatrix1D otherMatrix = (SelectedSparseObjectMatrix1D)other; return(this.Elements == otherMatrix.Elements); } else if (other is SparseObjectMatrix1D) { SparseObjectMatrix1D otherMatrix = (SparseObjectMatrix1D)other; return(this.Elements == otherMatrix.Elements); } return(false); }
/// <summary> /// Replaces all cell values of the receiver with the values of another matrix. /// Both matrices must have the same size. /// If both matrices share the same cells (as is the case if they are views derived from the same matrix) and intersect in an ambiguous way, then replaces <i>as if</i> using an intermediate auxiliary deep copy of <i>other</i>. /// </summary> /// <param name="source">the source matrix to copy from (may be identical to the receiver).</param> /// <returns><i>this</i> (for convenience only).</returns> /// <exception cref="ArgumentException">if <i>size() != other.Count</i>.</exception> public override ObjectMatrix1D Assign(ObjectMatrix1D source) { // overriden for performance only if (!(source is DenseObjectMatrix1D)) { return(base.Assign(source)); } DenseObjectMatrix1D other = (DenseObjectMatrix1D)source; if (other == this) { return(this); } CheckSize(other); if (!IsView && !other.IsView) { // quickest Array.Copy(other.Elements, 0, this.Elements, 0, this.Elements.Length); return(this); } if (HaveSharedCells(other)) { ObjectMatrix1D c = other.Copy(); if (!(c is DenseObjectMatrix1D)) { // should not happen return(base.Assign(source)); } other = (DenseObjectMatrix1D)c; } Object[] elems = this.Elements; Object[] otherElems = other.Elements; if (Elements == null || otherElems == null) { throw new NullReferenceException(); } int s = this.Stride; int ys = other.Stride; int index = base.Index(0); int otherIndex = other.Index(0); for (int k = Size; --k >= 0;) { elems[index] = otherElems[otherIndex]; index += s; otherIndex += ys; } return(this); }
/// <summary> /// Sorts the vector into ascending order, according to the order induced by the specified comparator. /// The returned view is backed by this matrix, so changes in the returned view are reflected in this matrix, and vice-versa. /// The algorithm compares two cells at a time, determinining whether one is smaller, equal or larger than the other. /// To sort ranges use sub-ranging viewsd To sort descending, use flip views ... /// <p> /// <b>Example:</b> /// <pre> /// // sort by sinus of cells /// ObjectComparator comp = new ObjectComparator() { /// public int compare(Object a, Object b) { /// Object as = System.Math.Sin(a); Object bs = System.Math.Sin(b); /// return as %lt; bs ? -1 : as == bs ? 0 : 1; /// } /// }; /// sorted = quickSort(vector,comp); /// </pre> /// /// @param vector the vector to be sorted. /// @param c the comparator to determine the order. /// @return a new matrix view sorted as specified. /// <b>Note that the original vector (matrix) is left unaffected.</b> /// </summary> public ObjectMatrix1D sort(ObjectMatrix1D vector, IComparer <ObjectMatrix1D> c) { int[] indexes = new int[vector.Size]; // row indexes to reorder instead of matrix itself for (int i = indexes.Length; --i >= 0;) { indexes[i] = i; } IntComparator comp = new IntComparator((a, b) => { return(c.Compare((ObjectMatrix1D)vector[a], (ObjectMatrix1D)vector[b])); }); { }; RunSort(indexes, 0, indexes.Length, comp); return(vector.ViewSelection(indexes)); }
/// <summary> /// Sorts the vector into ascending order, according to the <i>natural ordering</i>. /// The returned view is backed by this matrix, so changes in the returned view are reflected in this matrix, and vice-versa. /// To sort ranges use sub-ranging viewsd To sort descending, use flip views ... /// <p> /// <b>Example:</b> /// <table border="1" cellspacing="0"> /// <tr nowrap> /// <td valign="top"><i> 7, 1, 3, 1<br> /// </i></td> /// <td valign="top"> /// <p><i> ==> 1, 1, 3, 7<br> /// The vector IS NOT SORTED.<br> /// The new VIEW IS SORTED.</i></p> /// </td> /// </tr> /// </table> /// /// @param vector the vector to be sorted. /// @return a new sorted vector (matrix) viewd /// <b>Note that the original matrix is left unaffected.</b> /// </summary> public ObjectMatrix1D sort(ObjectMatrix1D vector) { int[] indexes = new int[vector.Size]; // row indexes to reorder instead of matrix itself for (int i = indexes.Length; --i >= 0;) { indexes[i] = i; } IntComparator comp = new IntComparator((a, b) => { IComparable av = (IComparable)(vector[a]); IComparable bv = (IComparable)(vector[b]); int r = av.CompareTo(bv); return(r < 0 ? -1 : (r > 0 ? 1 : 0)); }); RunSort(indexes, 0, indexes.Length, comp); return(vector.ViewSelection(indexes)); }
/// <summary> /// Sorts the matrix rows according to the order induced by the specified comparator. /// The returned view is backed by this matrix, so changes in the returned view are reflected in this matrix, and vice-versa. /// The algorithm compares two rows (1-d matrices) at a time, determinining whether one is smaller, equal or larger than the other. /// To sort ranges use sub-ranging viewsd To sort columns by rows, use dice viewsd To sort descending, use flip views ... /// <p> /// <b>Example:</b> /// <pre> /// // sort by sum of values in a row /// ObjectMatrix1DComparator comp = new ObjectMatrix1DComparator() { /// public int compare(ObjectMatrix1D a, ObjectMatrix1D b) { /// Object as = a.zSum(); Object bs = b.zSum(); /// return as %lt; bs ? -1 : as == bs ? 0 : 1; /// } /// }; /// sorted = quickSort(matrix,comp); /// </pre> /// /// @param matrix the matrix to be sorted. /// @param c the comparator to determine the order. /// @return a new matrix view having rows sorted as specified. /// <b>Note that the original matrix is left unaffected.</b> /// </summary> public ObjectMatrix2D sort(ObjectMatrix2D matrix, ObjectMatrix1DComparator c) { int[] rowIndexes = new int[matrix.Rows]; // row indexes to reorder instead of matrix itself for (int i = rowIndexes.Length; --i >= 0;) { rowIndexes[i] = i; } ObjectMatrix1D[] views = new ObjectMatrix1D[matrix.Rows]; // precompute views for speed for (int i = views.Length; --i >= 0;) { views[i] = matrix.ViewRow(i); } IntComparator comp = new IntComparator((a, b) => { return(c(views[a], views[b])); }); RunSort(rowIndexes, 0, rowIndexes.Length, comp); // view the matrix according to the reordered row indexes // take all columns in the original order return(matrix.ViewSelection(rowIndexes, null)); }
/// <summary> /// Swaps each element <i>this[i]</i> with <i>other[i]</i>. /// </summary> /// <param name=""></param> /// <returns></returns> /// <exception cref="ArgumentException">if <i>size() != other.Count</i>.</exception> public override void Swap(ObjectMatrix1D other) { // overriden for performance only if (!(other is DenseObjectMatrix1D)) { base.Swap(other); } DenseObjectMatrix1D y = (DenseObjectMatrix1D)other; if (y == this) { return; } CheckSize(y); Object[] elems = this.Elements; Object[] otherElems = y.Elements; if (Elements == null || otherElems == null) { throw new NullReferenceException(); } int s = this.Stride; int ys = y.Stride; int index = base.Index(0); int otherIndex = y.Index(0); for (int k = Size; --k >= 0;) { Object tmp = elems[index]; elems[index] = otherElems[otherIndex]; otherElems[otherIndex] = tmp; index += s; otherIndex += ys; } return; }