public static double[] Add(this Sparse <double> a, double[] b, double[] result) { for (int j = 0; j < b.Length; j++) { result[j] = b[j]; } for (int j = 0; j < a.Indices.Length; j++) { result[a.Indices[j]] += a.Values[j]; } return(result); }
/// <summary> /// Computes the squared Euclidean distance of two vectors given in sparse representation. /// </summary> /// /// <param name="x">The first vector <c>x</c>.</param> /// <param name="y">The second vector <c>y</c>.</param> /// /// <returns> /// The squared Euclidean distance <c>d² = |x - y|²</c> between the given vectors. /// </returns> /// public static double SquareEuclidean(Sparse <double> x, Sparse <double> y) { double sum = 0; int i = 0, j = 0; while (i < x.Length && j < y.Length) { int posx = x.Indices[i]; int posy = y.Indices[j]; if (posx == posy) { double d = x.Values[i] - y.Values[j]; sum += d * d; i++; j++; } else if (posx < posy) { double d = x.Values[j]; sum += d * d; i++; } else if (posx > posy) { double d = y.Values[j]; sum += d * d; j++; } } for (; i < x.Length; i++) { sum += x.Values[i] * x.Values[i]; } for (; j < y.Length; j++) { sum += y.Values[j] * y.Values[j]; } return(sum); }
/// <summary> /// Parses a string containing a sparse array in LibSVM /// format into a <see cref="Sparse{T}"/> vector. /// </summary> /// /// <param name="values">An array of "index:value" strings indicating /// where each value belongs in the sparse vector.</param> /// <param name="intercept">Whether an intercept term should be added /// at the beginning of the vector.</param> /// public static Sparse <double> Parse(string[] values, double?intercept = null) { var result = new Sparse <double>(values.Length); int offset = intercept.HasValue ? 1 : 0; for (int i = 0; i < values.Length; i++) { string[] element = values[i].Split(':'); int index = Int32.Parse(element[0], CultureInfo.InvariantCulture) - 1; double value = Double.Parse(element[1], CultureInfo.InvariantCulture); result.Indices[i] = index + offset; result.Values[i] = value; } if (intercept.HasValue) { result.Values[0] = intercept.Value; } return(result); }
/// <summary> /// Gets the Euclidean norm for a matrix. /// </summary> /// public static double Euclidean(this Sparse <double> a) { return((double)Math.Sqrt(SquareEuclidean(a))); }
/// <summary> /// Gets the Euclidean norm for a matrix. /// </summary> /// public static float Euclidean(this Sparse <float> a) { return((float)Math.Sqrt(SquareEuclidean(a))); }
/// <summary> /// Computes the Euclidean distance of two vectors given in sparse representation. /// </summary> /// /// <param name="x">The first vector <c>x</c>.</param> /// <param name="y">The second vector <c>y</c>.</param> /// /// <returns> /// The squared Euclidean distance <c>d² = |x - y|²</c> between the given vectors. /// </returns> /// public static double Euclidean(Sparse <double> x, Sparse <double> y) { return(Math.Sqrt(SquareEuclidean(x, y))); }
/// <summary> /// Divides a sparse vector by a scalar. /// </summary> /// public static Sparse <double> Divide(this Sparse <double> a, double b, Sparse <double> result) { Elementwise.Divide(a.Values, b, result.Values); return(result); }
public static double SquareEuclidean(Sparse <double> x, Sparse <double> y) { // Note: this is an auto-generated method stub that forwards the call // to the actual implementation, indicated in the next line below: return(cacheSquareEuclidean.Distance(x, y)); }