/// <summary> /// Apply op to each pair of left and right, collect the results. /// </summary> /// <param name="left">lhs of op</param> /// <param name="right">rhs of op</param> /// <param name="op">Operation function</param> /// <returns>A new Vector containing the results</returns> static Vector DoArithmetic(Vector left, IEnumerable<double> right, Func<double, double, double> op) { Vector result = new Vector(left.Dimension); for (var i = 0; i != left.Dimension; i++) { result[i] = op(left[i], right.ElementAt(i)); } return result; }
/// <summary> /// Return all numbers in a Vector. /// </summary> /// <param name="vector">The vector to be converted</param> /// <returns>A collection containing all numbers in vector.</returns> static IEnumerable<double> Numbers(Vector vector) { foreach (var i in vector.m_Vector) { yield return i; } }
/// <summary> /// Subtracts the specified vector from another specified vector. /// </summary> /// <param name="left">The vector from which right is subtracted.</param> /// <param name="right">The vector to subtract from left.</param> /// <returns>The difference between left and right. </returns> public static Vector Subtract(Vector left, Vector right) { if (left.Dimension != right.Dimension) { throw new ArgumentException("dimensions not match"); } return DoArithmetic(left, Numbers(right), (x, y) => x - y); }
/// <summary> /// Subtract that vector from this. This function is destructive. /// </summary> /// <param name="that">A vector to subtract from this.</param> /// <returns>This vector</returns> public Vector Subtract(Vector that) { var d = this.Dimension; if (that.Dimension != d) { throw new ArgumentException("dimensions not match"); } else { for (var i = 0; i != d; i++) { m_Vector[i] -= that.m_Vector[i]; } } return this; }
/// <summary> /// Multiplies the specified vector by the specified scalar and returns the resulting vector. /// </summary> /// <param name="vector">The vector to multiply.</param> /// <param name="m">The scalar to multiply.</param> /// <returns>The result of multiplying vector and m.</returns> public static Vector Multiply(Vector vector, double m) { return DoArithmetic(vector, SameNumbers(m, vector.Dimension), (x, y) => x * y); }
/// <summary> /// Divides the specified vector by the specified scalar and returns the result as a vector. /// </summary> /// <param name="vector">The vector to divide.</param> /// <param name="d">The amount by which vector is divided.</param> /// <returns>The result of dividing vector by d.</returns> /// <remarks>Divide by zero is not checked.</remarks> public static Vector Divide(Vector vector, double d) { return DoArithmetic(vector, SameNumbers(d, vector.Dimension), (x, y) => x / y); }
public Cell(Vector weight) { m_Weight = weight; m_AuxData = new Dictionary<string, dynamic>(); }
/// <summary> /// Find the Best Matching Unit of input in map /// </summary> /// <param name="input">Input vector</param> /// <returns>A cell in map</returns> Cell FindBMU(Vector input) { double minDistance = double.MaxValue; Cell bmu = null; m_Map.ForEachCell(cell => { double d = m_Calculator.DistanceOf(cell.Weight, input); if (d < minDistance) { minDistance = d; bmu = cell; } }); Debug.Assert(bmu != null, "bmu not found"); return bmu; }
/// <summary> /// Ajust a cell's weight in map. /// </summary> /// <param name="cell">The cell to be adjusted</param> /// <param name="input">The input vector</param> /// <param name="distance">The map distance between cell and current bmu cell</param> /// <param name="radius">Current neighbourhood radius</param> /// <param name="iteration">Current iteration</param> void AjustCellWeight(Cell cell, Vector input, double distance, double radius, int iteration) { /* * W(t+1) = W(t) + THETA(t) * L(t) * (V(t) - W(t)) * t is current iteration * W(t) is old weight to be adjusted * W(t+1) is the new weight * THETA(t) is the neighbourhood influence function * L(t) is the learning rate * V(t) current input vector * */ Vector w = cell.Weight; double theta = m_Calculator.Influence(cell, distance, radius); double learn = m_Calculator.LearningRate(iteration, m_TotalIterations); Vector diff = Vector.Subtract(input, w); w.Add(diff.Multiply(theta * learn)); }