/// <summary> /// An IEnumerable<double[]> extension method that converts the source into a matrix. /// </summary> /// <exception cref="InvalidOperationException">Thrown when the requested operation is invalid.</exception> /// <param name="source">The source to act on.</param> /// <param name="vectorType">The <see cref="VectorType"/> of the input array.</param> /// <returns>e as a Matrix.</returns> public static Matrix ToMatrix(this IEnumerable <Vector> source, VectorType vectorType = VectorType.Row) { var c = source.Count(); if (c == 0) { throw new InvalidOperationException("Cannot create matrix from an empty set."); } return(Matrix.Stack(vectorType, source.ToArray())); }
/// <summary> /// Slices the Matrix returning only the cells specified by their location. /// </summary> /// <param name="m">Matrix.</param> /// <param name="indices">An 2-D location array of indexes to include.</param> /// <param name="t">Type of the first dimension in the location array.</param> /// <returns></returns> public static Matrix Slice(this Matrix m, IEnumerable <IEnumerable <int> > indices, VectorType t = VectorType.Row) { List <Vector> vectors = new List <Vector>(); int top = (t == VectorType.Row ? m.Rows : m.Cols); for (int i = 0; i < top; i++) { vectors.Add(m[i, t].Slice(indices.ElementAt(i))); } return(Matrix.Stack(t, vectors.ToArray())); }
/// <summary>Stack a set of vectors into a matrix.</summary> /// <param name="vectors">.</param> /// <returns>A Matrix.</returns> public static Matrix Stack(params Vector[] vectors) { return(Matrix.Stack(VectorType.Row, vectors)); }
/// <summary>A Matrix extension method that stacks.</summary> /// <param name="m">Matrix.</param> /// <param name="t">Row or Column sum.</param> /// <returns>A Matrix.</returns> public static Matrix Stack(this Matrix m, Matrix t) { return(Matrix.Stack(m, t)); }