//max public static SparseVector getMaxVec(SparseVector v1, SparseVector v2) { SparseVector maxVec = new SparseVector(); SortedDictionary <int, double> .KeyCollection keys1 = v1.getKeys(); SortedDictionary <int, double> .KeyCollection keys2 = v2.getKeys(); foreach (int ID in keys1) { if (v2.ContainsKey(ID)) { maxVec[ID] = Math.Max(v1[ID], v2[ID]); } else { maxVec[ID] = v1[ID]; } } foreach (int ID in keys2) { if (!v1.ContainsKey(ID)) { maxVec[ID] = v2[ID]; } } return(maxVec); }
//H(v1,v2); 0log0 = 0; xlog0 = -inf public static double crossEnt(SparseVector v1, SparseVector v2) { double res = 0; foreach (KeyValuePair <int, double> kvp in v2.vector) { if (v1.ContainsKey(kvp.Key)) { res -= v1[kvp.Key] * Math.Log(kvp.Value); } } //xlog0 foreach (KeyValuePair <int, double> kvp in v1.vector) { if (!v2.ContainsKey(kvp.Key)) { res -= kvp.Value * Math.Log(double.Epsilon); } } return(res); }
//H(v1,v2); 0log0 = 0; xlog0 = -inf public static double crossEnt(SparseVector v1, SparseVector v2) { double res = 0; foreach (KeyValuePair<int, double> kvp in v2.vector) { if (v1.ContainsKey(kvp.Key)) { res -= v1[kvp.Key] * Math.Log(kvp.Value); } } //xlog0 foreach (KeyValuePair<int, double> kvp in v1.vector) { if (!v2.ContainsKey(kvp.Key)) { res -= kvp.Value * Math.Log(double.Epsilon); } } return res; }
//max public static SparseVector getMaxVec(SparseVector v1, SparseVector v2) { SparseVector maxVec = new SparseVector(); SortedDictionary<int, double>.KeyCollection keys1 = v1.getKeys(); SortedDictionary<int, double>.KeyCollection keys2 = v2.getKeys(); foreach (int ID in keys1) { if (v2.ContainsKey(ID)) { maxVec[ID] = Math.Max(v1[ID], v2[ID]); } else maxVec[ID] = v1[ID]; } foreach (int ID in keys2) { if (!v1.ContainsKey(ID)) maxVec[ID] = v2[ID]; } return maxVec; }