/// <summary> /// An IEnumerable<int> extension method that converts a seq to a vector. /// </summary> /// <param name="seq">The seq to act on.</param> /// <returns>seq as a Vector.</returns> public static Vector ToVector(this IEnumerable <double> seq) { Vector v = Vector.Zeros(seq.Count()); int i = -1; foreach (double item in seq) { v[++i] = item; } return(v); }
/// <summary>Diagrams the given m.</summary> /// <param name="m">Input Matrix.</param> /// <returns>A Vector.</returns> public static Vector Diag(Matrix m) { var length = m.Cols > m.Rows ? m.Rows : m.Cols; var v = Vector.Zeros(length); for (int i = 0; i < length; i++) { v[i] = m[i, i]; } return(v); }
/// <summary> /// An IEnumerable<int> extension method that converts a seq to a vector. /// </summary> /// <param name="seq">The seq to act on.</param> /// <returns>seq as a Vector.</returns> public static Vector ToVector(this IEnumerable <int> seq) { var v = Vector.Zeros(seq.Count()); var i = -1; foreach (double item in seq) { v[++i] = (double)item; } return(v); }
/// <summary> /// An IEnumerable<T> extension method that converts this object to a vector. /// </summary> /// <tparam name="T">Generic type parameter.</tparam> /// <param name="seq">The seq to act on.</param> /// <param name="f">The Func<T,double> to process.</param> /// <returns>The given data converted to a Vector.</returns> public static Vector ToVector <T>(this IEnumerable <T> seq, Func <T, double> f) { var v = Vector.Zeros(seq.Count()); var i = -1; foreach (var item in seq) { v[++i] = f(item); } return(v); }
/// <summary>Dot product between a matrix and a vector.</summary> /// <exception cref="InvalidOperationException">Thrown when the requested operation is invalid.</exception> /// <param name="v">Vector v.</param> /// <param name="x">Matrix x.</param> /// <returns>Vector dot product.</returns> public static Vector Dot(Vector v, Matrix x) { if (v.Length != x.Rows) { throw new InvalidOperationException("objects are not aligned"); } Vector toReturn = Vector.Zeros(x.Cols); for (int i = 0; i < toReturn.Length; i++) { toReturn[i] = Vector.Dot(x[i, VectorType.Col], v); } return(toReturn); }
/// <summary>Forwards.</summary> /// <param name="A">Input Matrix.</param> /// <param name="b">The Vector to process.</param> /// <returns>A Vector.</returns> internal static Vector Forward(Matrix A, Vector b) { Vector x = Vector.Zeros(b.Length); for (int i = 0; i < b.Length; i++) { double sum = 0; for (int j = 0; j < i; j++) { sum += A[i, j] * x[j]; } x[i] = (b[i] - sum) / A[i, i]; } return(x); }
/// <summary>Backwards.</summary> /// <param name="A">Input Matrix.</param> /// <param name="b">The Vector to process.</param> /// <returns>A Vector.</returns> internal static Vector Backward(Matrix A, Vector b) { Vector x = Vector.Zeros(b.Length); for (int i = b.Length - 1; i > -1; i--) { double sum = 0; for (int j = i + 1; j < b.Length; j++) { sum += A[i, j] * x[j]; } x[i] = (b[i] - sum) / A[i, i]; } return(x); }
/// <summary> /// An IEnumerable<IEnumerable<double>> extension method that converts a matrix to /// the examples. /// </summary> /// <param name="matrix">The matrix to act on.</param> /// <returns>matrix as a Tuple<Matrix,Vector></returns> public static Tuple <Matrix, Vector> ToExamples(this IEnumerable <IEnumerable <double> > matrix) { // materialize var x = (from v in matrix select v.ToArray()).ToArray(); // determine matrix // size and type var m = Build(x, true); // clip last col // fill 'er up! for (var i = 0; i < m.Rows; i++) { for (var j = 0; j < m.Cols; j++) { if (j >= x[i].Length) { // over bound limits m[i, j] = 0; // pad overlow to 0 } else { m[i, j] = x[i][j]; } } } // fill up vector var y = Vector.Zeros(m.Rows); for (var i = 0; i < m.Rows; i++) { if (m.Cols >= x[i].Length) { y[i] = 0; // pad overflow to 0 } else { y[i] = x[i][m.Cols]; } } return(new Tuple <Matrix, Vector>(m, y)); }
/// <summary> /// Sorts the given Matrix by the specified row or column selector and returns the new Matrix /// along with the original indices. /// </summary> /// <param name="source">The Matrix</param> /// <param name="keySelector">Property selector to sort by.</param> /// <param name="t">Specifies whether to sort horizontally or vertically.</param> /// <param name="ascending">Determines whether to sort ascending or descending (Default: True)</param> /// <param name="indices">Vector of <paramref name="t"/> indices in the original Matrix before the sort operation.</param> /// <returns>New Matrix and Vector of original indices.</returns> public static Matrix Sort(Matrix source, Func <Vector, double> keySelector, VectorType t, bool ascending, out Vector indices) { int max = (t == VectorType.Row ? source.Rows : source.Cols); indices = Vector.Zeros(max); List <Vector> vects = new List <Vector>(max); IEnumerable <Vector> arrays = (t == VectorType.Row ? source.GetRows() : source.GetCols()); KeyValuePair <Vector, int>[] sort = (ascending ? arrays.Select((i, v) => new KeyValuePair <Vector, int>(i, v)) .OrderBy(o => keySelector(o.Key)) : arrays.Select((i, v) => new KeyValuePair <Vector, int>(i, v)) .OrderByDescending(o => keySelector(o.Key))).ToArray(); indices = sort.Select(s => s.Value).ToVector(); return(sort.Select(s => s.Key).ToMatrix(t)); }
/// <summary>Ranges.</summary> /// <param name="s">The int to process.</param> /// <param name="e">(Optional) the int to process.</param> /// <returns>A Vector.</returns> public static Vector Range(int s, int e = -1) { if (e > 0) { Vector v = Vector.Zeros(e - s); for (int i = s; i < e; i++) { v[i - s] = i; } return(v); } else { Vector v = Vector.Zeros(s); for (int i = 0; i < s; i++) { v[i] = i; } return(v); } }