好久没有写过C#了。不过C#确实非常方便,代码写起来也很顺。这个代码是描述行逻辑链接的三元组顺序表。(处理稀疏矩阵效率要比经典算法高得多的~而且也更节省存储空间。) //#defineOUTPUT_DEBUG using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MatrixCSharp { public class Matrix { public struct PolyEx { publicint i; publicint j; publicint val; } publicMatrix() { polyExList = new List<PolyEx>(); row = col = tu = 0; } publicMatrix(int[,] array) :this(){ LoadPolyExList(array); } public void LoadPolyExList(int[,]array) { polyExList.Clear(); row = array.GetLength(0); col = array.GetLength(1); tu = 0; for(int i = 0; i < row; ++i) { for(int j = 0; j < col; ++j) { if(array[i, j] != 0) { PolyEx pe = new PolyEx(); pe.i = i; pe.j = j; pe.val = array[i, j]; polyExList.Add(pe); ++tu; } } } firstIndexs = GetFirstIndex(1); } // // 1 =>row // 0 =>col // privateint[] GetFirstIndex(intrOrC) { // Readall the column numbers. intsize = 0; if(rOrC == 1){ size = row; } else{ size = col; } int[]num4Col = new int[size]; int[]indexs = new int[size]; foreach(PolyEx pe inpolyExList) { if(rOrC == 1){ num4Col[pe.i]++; } else{ num4Col[pe.j]++; } } for(int i = 0; i < size; ++i){ if(i > 0) { indexs[i] += num4Col[i - 1]+ indexs[i - 1]; } else { indexs[i] = 0; } } #if OUTPUT_DEBUG Console.WriteLine("FirstIndexs:"); foreach (int i in indexs) { Console.Write(i + ""); } Console.WriteLine(); #endif returnindexs; } public void Translate() { PolyEx[] resList = new PolyEx[tu]; int[]colFirstIndexs = GetFirstIndex(0); intindex = -1; foreach(PolyEx pe inpolyExList) { PolyExpeNew = new PolyEx(); peNew.j = pe.i; peNew.i = pe.j; peNew.val = pe.val; index = colFirstIndexs[pe.j]; resList[index] = peNew; colFirstIndexs[pe.j]++; } polyExList = new List<PolyEx>(resList); firstIndexs = colFirstIndexs; inttemp = row; row = col; col = row; } public Matrix Mul(Matrixm2) { if(this.col != m2.row) { returnnull; } MatrixmRes = new Matrix(); // 累加器 int[] acc = new int[m2.col]; for(int i = 0; i < polyExList.Count; ++i) { if(i > 0) { if(polyExList[i].i != polyExList[i - 1].i) { for (int j = 0; j < m2.col;++j) { if (acc[j] != 0) { PolyEx pe = new PolyEx(); pe.i =polyExList[i].i; pe.j = j; pe.val =acc[j]; Console.WriteLine("Col:"+ j + " Val:" + pe.val); mRes.polyExList.Add(pe); } } setZero(acc); } } intcolIndex = m2.firstIndexs[polyExList[i].j]; while(colIndex < m2.polyExList.Count &&m2.polyExList[colIndex].i == polyExList[i].j) { #if OUTPUT_DEBUG Console.WriteLine("i=> " + polyExList[i].i); Console.WriteLine("m2j => " + m2.polyExList[colIndex].j); Console.WriteLine(); #endif acc[m2.polyExList[colIndex].j] +=m2.polyExList[colIndex].val * polyExList[i].val; colIndex++; } } for(int j = 0; j < m2.col; ++j) { if(acc[j] != 0) { PolyExpe = new PolyEx(); pe.i =polyExList[polyExList.Count - 1].i; pe.j = j; pe.val = acc[j]; #if OUTPUT_DEBUG Console.WriteLine("Col:" + j + " Val:" + pe.val); #endif mRes.polyExList.Add(pe); } } mRes.row = row; mRes.col = m2.col; mRes.firstIndexs = mRes.GetFirstIndex(1); mRes.tu = mRes.polyExList.Count; returnmRes; } privatevoid setZero(int[]array) { for(int i = 0; i < array.Length; ++i) { array[i] = 0; } } public int Row { get{ return row; } } public int Col { get{ return col; } } public override stringToString() { StringBuildersb = new StringBuilder(); sb.AppendLine("i\tj\tvalue"); sb.AppendLine("--\t--\t-----"); foreach(PolyEx pe inpolyExList) { sb.AppendFormat("{0}\t{1}\t{2}\n", pe.i, pe.j, pe.val); } returnsb.ToString(); } privateList<PolyEx>polyExList; privateint[] firstIndexs; // 记录每一行非零元的起始位置 privateint row; privateint col; privateint tu; } class Program { static int[,] SimpleMul(int[,]m1, int[,] m2) { introw1 = m1.GetLength(0); intcol1 = m1.GetLength(1); introw2 = m2.GetLength(0); intcol2 = m2.GetLength(1); int[,]mRes = new int[row1,col2]; for(int i = 0; i < row1; ++i) { for(int j = 0; j < col2; ++j) { mRes[i, j] = 0; for(int k = 0; k < col1; ++k) { mRes[i,j] += m1[i,k] *m2[k,j]; } } } for(int i = 0; i < row1; ++i) { for(int j = 0; j < col2; ++j) { if(mRes[i, j] == 0){ Console.Write("-\t"); } else{ Console.Write(mRes[i, j] + "\t"); } } Console.WriteLine(); } returnmRes; } static void Main(string[]args) { int[,]test = { { 0, 0, 1, 0, 0, 0, 1, 0, 3,4}, { 3, 0, 0, 0, 0, 0, 7, 0, 3,4}, { 0, 4, 9, 0, 0, 5, 1, 0, 3,4}, { 0, 0, 0, 13, 0, 0, 8, 0, 3, 4}, { 0, 0, 0, 0, 0, 0, 9, 0, 3,4}, { 0, 0, 0, 0, 0, 0, 1, 0, 3,4}, }; int[,]test2 = { { 1, 0, 8, 0, 0, 1}, { 0, 0, 0, 0, 3, 0}, { 0, 3, 0, 0, 0, 0}, { 0, 5, 0, 0, 0, 0}, { 0, 5, 0, 0, 0, 0}, { 0, 5, 4, 0, 0, 0}, { 0, 9, 0, 0, 1, 0}, { 0, 5, 0, 0, 0, 0}, { 0, 5, 0, 5, 0, 0}, { 0, 5, 0, 0, 0, 0}, }; Matrixm = new Matrix(test); Matrixm2 = new Matrix(test2); Console.WriteLine(m); Console.WriteLine(m2); Matrixres = m.Mul(m2); Console.WriteLine(res); SimpleMul(test, test2); //m.Translate(); //Console.WriteLine(m); Console.ReadKey(); } } }
相关推荐
稀疏矩阵是指那些多数元素为零的矩阵。利用“稀疏”特点进行存储和计算 可以大大节省存储空间,提高计算效率。实现一个能进行稀疏矩阵基本运算的运算器。 功能要求: 1. 以“带行逻辑链接信息”的三元组顺序表表示...
稀疏矩阵是指那些多数元素为零的矩阵。利用“稀疏”特点进行存储和计算可以大大节省存储空间,握高计算效率。实现一个能进行稀硫矩阵基本运算的运算器。 二、基本要求 以“带行逻辑链接信息”的三元组顺序表表示...
稀疏矩阵十字链表
printf("输入稀疏矩阵A的行数,列数和非零元个数:"); scanf("%d %d %d",&A.m,&A.n,&A.t); for(k=1;k;k++) { printf("输入第%d个非0元素的行数,列数和值:",k); scanf("%d %d %d",&A.data[k].i,&A.data[k].j,&...
使用C语言以实现稀疏矩阵之间相乘的简单矩阵运算,亦可以使用于非稀疏矩阵之间的矩阵乘法运算,因为机制是一样的,只是因为稀疏矩阵在算法设计中比较特殊,故而特意加以区分
inter MKL 求解大型稀疏矩阵 实例(C/C++)。适用与对称或者非对称 稀疏矩阵求解,求解速度非常快。
稀疏矩阵 数据结构 课程设计稀疏矩阵 数据结构 课程设计稀疏矩阵 数据结构 课程设计稀疏矩阵 数据结构 课程设计稀疏矩阵 数据结构 课程设计稀疏矩阵 数据结构 课程设计稀疏矩阵 数据结构 课程设计稀疏矩阵 数据结构 ...
已知稀疏矩阵M和矩阵N,求C=M+N。 输入有多行。分为两部分。 第一部为矩阵 M 的信息: 第一行为m1、n1、len1,表示矩阵 M 的行数、列数和非零元素的个数。 接着下来有len1行数据:每一行为矩阵元素 M 所在的行、...
稀疏矩阵转置: 输入稀疏矩阵中每个元素的行号、列号、值,建立稀疏矩阵的三元组存储结构,并将此矩阵转置,显示转置前后的三元组结构。
稀疏矩阵是机器学习中的重要工具。本代码为自己编写。初学者,希望大家批评指正。
广工数据结构稀疏矩阵课程设计,要过的就可以参考一下
稀疏矩阵的转置的程序及实验报告,数据结构作业。
c 语言实现 稀疏矩阵 的创建 和 C语言 压缩存储 压缩矩阵转置
设计一个稀疏矩阵运算器。实现两个矩阵相加、相减和相乘等的运算。矩阵的输入输出均按通常的阵列形式
数据结构课程设计,稀疏矩阵的加法乘法转置。
//稀疏矩阵的三元组顺序表存储表示 #define MAXSIZE 100 //非零元个数最大为100 typedef struct {int i,j; //非零元的行下标和列下标 ElemType e; //非零元 }Triple; typedef struct {Triple data[MAXSIZE+1]; //...
稀疏矩阵是指那些多数元素为零的矩阵。利用“稀疏”特点进行存储和计算可以大大节省存储空间,提高计算效率。实现一个能进行稀疏矩阵基本运算的运算器。 以“带行逻辑链接信息”的三元组顺序表表示稀疏矩阵,实现...
个稀疏矩阵计算器,能够:⑴输入并建立稀疏矩阵;⑵输出稀疏矩阵;⑶执行两个矩阵相加;⑷求一个矩阵的转置矩阵;
特殊矩阵、稀疏矩阵的表示实现与运算