public Matrix(Matrix matrix) { rows = matrix.x.GetLength(0); cols = matrix.x.Length / rows; x = new MNode[rows, cols]; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { x[i, j] = new MNode(); x[i, j].Copyfrom(matrix.x[i, j]); } } }
public Matrix(int col, int row) { x = new MNode[row, col]; rows = row; cols = col; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { x[i, j] = new MNode(); } } }
/// <summary> /// 依据某一节点的起终点来搜寻某一节点 /// </summary> /// <param name="sPoint"></param> /// <param name="ePoint"></param> /// <returns></returns> public MNode getMNode(string sPoint, string ePoint) { MNode node = null; for (int i = 0; i < this.rows; i++) { for (int j = 0; j < this.cols; j++) { if (this.x[i, j].EName == ePoint && this.x[i, j].SName == sPoint) { node = this.x[i, j]; } } } return(node); }
public Matrix GetSimilarityMatrix() { Matrix mat = new Matrix(graph.Rows, graph.Cols); for (int i = 0; i < graph.Rows; i++) { for (int j = 0; j < graph.Cols; j++) { MNode node = mat.getMNode(i, j); node.Value = getSimilarity(i, j); } } System.Console.WriteLine("**************** 相似度矩阵 ***********************"); mat.Print(); System.Console.WriteLine("****************************************************"); return(mat); }
/// <summary> /// 初始化matrix(取值为 0 或者 1) /// </summary> public void initialize() { int count = MyXml.getNodesCount(); graph = new Matrix(count, count); for (int i = 0; i < graph.Rows; i++) { for (int j = 0; j < graph.Cols; j++) { MNode node = graph.getMNode(i, j); node.SName = i.ToString(); node.EName = j.ToString(); LinkBean bean = new LinkBean(); // 正向 bean.source = MyXml.findNodeById(node.SName); bean.target = MyXml.findNodeById(node.EName); LinkBean bean_2 = new LinkBean(); // 反向 bean_2.source = MyXml.findNodeById(node.EName); bean_2.target = MyXml.findNodeById(node.SName); foreach (LinkBean lb in MyXml.getAllLinks()) { if (lb.Equals(bean) || lb.Equals(bean_2)) { node.Value = 1; break; } } } } for (int i = 0; i < graph.Rows; i++) { for (int j = 0; j < graph.Cols; j++) { if (i == j) { graph.getMNode(i, j).Value = 1; continue; } } } }
/// <summary> /// 判断两个路径节点起始点和终结点是否一致 /// </summary> /// <param name="src">点一</param> /// <param name="des">点二</param> /// <returns>返回判断值</returns> public static bool IsSamePath(MNode src, MNode des) { return (src.sSPName == des.sSPName) && (src.sEPName == des.sEPName); }
public static MNode operator /(MNode src, MNode des) { MNode res = new MNode(src.sSPName, src.sEPName); res.val = src.val / des.val; return res; }
/// <summary> /// 内部使用的矩阵生成式 /// </summary> /// <param name="s"></param> /// <param name="value"></param> /// <param name="nodes"></param> /// <returns></returns> private static Matrix GenerateMatrix(int s, ArrayList value, MNode[,] nodes) { Matrix m = new Matrix(s, s); int x = 0; for (int i = 0; i < m.rows; i++) { for (int j = 0; j < m.cols; x++, j++) { m.x[i, j].Value = double.Parse(value[x].ToString()); m.x[i, j].EName = nodes[i, j].EName; m.x[i, j].SName = nodes[i, j].SName; } } return m; }
/// <summary> /// 设置当前矩阵某一位置的节点值 /// </summary> /// <param name="node">源节点</param> /// <param name="col">列</param> /// <param name="row">行</param> public void setMNode(MNode node, int col, int row) { this.x[row, col].Copyfrom(node); }
public void reduceMatrixByIndex(List<int> lIndex) { if (lIndex == null || lIndex.Count <= 0) return; foreach (int i in lIndex) { if (i >= this.Cols || i >= this.Rows) return; } int r = this.rows - lIndex.Count; int c = this.cols - lIndex.Count; //int r = lIndex.Count; //int c = lIndex.Count; if (r == 0 || c == 0) return; MNode[,] x = new MNode[r, c]; int pi = 0, pj = 0; for (int i = 0; i < this.Rows; i++) { if (lIndex.Contains(i)) continue; for (int j = 0; j < this.Rows; j++) { if (lIndex.Contains(j)) continue; x[pi, pj] = new MNode(); x[pi, pj].Copyfrom(this.x[i, j]); pj = (++pj) % c; } pi = (++pi) % r; } this.x = x; this.rows = r; this.cols = c; }
/// <summary> /// 判断两个路径节点起始点和终结点是否一致 /// </summary> /// <param name="src">点一</param> /// <param name="des">点二</param> /// <returns>返回判断值</returns> public static bool IsSamePath(MNode src, MNode des) { return((src.sSPName == des.sSPName) && (src.sEPName == des.sEPName)); }
/// <summary> /// 返回与x矩阵同名的随机方阵 /// </summary> /// <param name="s">行列数</param> /// <param name="n">命名阵</param> /// <returns>返回生成的矩阵</returns> public static Matrix Random(int s, MNode[,] n) { long tick = System.DateTime.Now.Ticks; //Random ran = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32)); System.Random rand = new System.Random((int)(tick & 0xffffffffL) | (int)(tick >> 32)); ArrayList f = new ArrayList(); //for(int i = 0; i < s * s; f.Add(2 * rand.NextDouble() - 1), i++); for (int i = 0; i < s * s; f.Add(rand.NextDouble()), i++) ; //for (int i = 0; i < s * s; System.Console.Write(f[i]+" "), i++); //System.Console.WriteLine(); return Matrix.GenerateMatrix(s, f, n); }
/// <summary> /// 返回以n阵命名的单位阵 /// </summary> /// <param name="s">行列数</param> /// <param name="n">命名阵</param> /// <returns>返回生成的单位阵</returns> public static Matrix Eye(int s, MNode[,] n) { ArrayList f = new ArrayList(); int x = 0; for (int i = 0; i < s * s; i++) { if (i == (s + 1) * x) { f.Add(1); x++; } else f.Add(0); } return Matrix.GenerateMatrix(s, f, n); }
/// <summary> /// /// </summary> /// <param name="matrix"></param> /// <returns></returns> //private MNode[] MatrixToArray(Matrix matrix) //{ // MNode[] array = new MNode[matrix.Rows * matrix.Cols]; // int m = 0; // for (int i = 0; i < matrix.Rows; i++) // { // for (int j = 0; j < matrix.Cols; j++) // { // array[m++] = matrix.x[i, j]; // } // } // return array; //} /// <summary> /// /// </summary> /// <param name="array"></param> /// <returns></returns> public static Matrix ArrayToMatrix(MNode[] array, int col, int row) { if (array.Length != col * row) { System.Console.WriteLine("col * row isn't coincide to the array length"); return null; } Matrix matrix = new Matrix(col, row); int m = 0; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { matrix.x[i, j] = array[m++]; } } return matrix; }
/// <summary> /// 节点的复制方法 /// </summary> /// <param name="des">复制的源节点</param> public void Copyfrom(MNode des) { this.val = des.val; this.sEPName = des.sEPName; this.sSPName = des.sSPName; }
/// <summary> /// 返回与x矩阵同名的零方阵 /// </summary> /// <param name="s">行列数</param> /// <param name="x">命名阵</param> /// <returns>返回的矩阵</returns> public static Matrix Zeros(int s, MNode[,] x) { ArrayList f = new ArrayList(); for (int i = 0; i < s * s; f.Add(0), i++) ; return Matrix.GenerateMatrix(s, f, x); }