欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

對稱矩陣及稀疏矩陣的壓縮

對稱矩陣

東營區(qū)網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、APP開發(fā)、自適應網(wǎng)站建設等網(wǎng)站項目制作,到程序開發(fā),運營維護。創(chuàng)新互聯(lián)2013年開創(chuàng)至今到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設就選創(chuàng)新互聯(lián)

    設一個N*N的方陣A,A中的任意元素A[i][j],當且僅當A[i][j]=A[j][i],則矩陣A是對稱矩陣,以對角線分隔,分為上三角和下三角

    壓縮矩陣存儲對稱矩陣時只需要存儲其上三角或者下三角的數(shù)據(jù),即最多存儲n(n+1)/2個數(shù)據(jù),對應關于為:i>j,symmetricMatrix[i][j]=A[i*(i+1)/2+j]

  代碼實現(xiàn):

template<class T>
class SymmetricMatrix
{
public:
	SymmetricMatrix(T*a,size_t num)
		:_a(new T[num*(num+1)/2])//開辟一塊壓縮矩陣的空間,n*(n+1)/2
		,_size(num*(num+1)/2)
		,_n(num)	
	{
		for (size_t i = 0; i < _n; i++)
		{
			for (size_t j = 0; j <= i; j++)
			{
				
				_a[i*(i+1)/2+j] = a[i*num + j];//把矩陣中的元素存入壓縮矩陣中
				
			}
		}
	}
	~SymmetricMatrix()
	{
		if (_a)
		{
			delete[]_a;
			/*
			_a=NULL;
			_size=0;
			_n=0;
			*/
		}
	}
	void Display()
	{
		for (size_t i = 0; i < _n; i++)
		{
			for (size_t j = 0; j < _n; j++)
			{
			/*
			當i>=j打印下矩陣,當i<j,交換i,j打印上矩陣
			*/
			
				if (i < j)
					Access(i, j);
				cout << _a[i*(i + 1) / 2 + j] << " ";
				
				if(i< j)
				    Access(i, j);
			/*
			if(i>=j)
			cout<<_a[i*(i + 1) / 2 + j] << " ";
			else
			cout<<_a[j*(j + 1) / 2 + i] << " ";
			*/
			}
			cout << endl;
		}
		cout << endl;
	}
protected:
	void Access(size_t i, size_t j)
	{
		if (i < j)
		{
			swap(i, j);
		}
	}
private:
	T* _a;//數(shù)組
	size_t _size;//壓縮矩陣大小
	size_t _n;//矩陣為N*N
};

void test()
{

	int a[][4] =
	{
		0, 1, 2, 3,
		1, 0, 5, 6,
		2, 5, 0, 8,
		3, 6, 8, 0
	};
	size_t lenth = sizeof(a)/sizeof(a[0]);
	SymmetricMatrix<int> Array((int*)a,lenth);
	Array.Display();
}
int main()
{
	test();
}

稀疏矩陣

    M*N的矩陣,矩陣中有效值的個數(shù)遠遠小于無效值的個數(shù),這些數(shù)據(jù)的分布,沒有規(guī)律    

    壓縮存儲只需要存儲極少的有效數(shù)據(jù),用三元組{row,col,value}存儲,三元組按原矩陣中的位置,以行優(yōu)先級先后順序依次存放。

#include<iostream>
#include<vector>

using namespace std;

template<class T>
struct Triple//三元組
{
	T _value;//值
	size_t _row;//行
	size_t _col;//列

	Triple(const T&value = T(), size_t row = 0, size_t col = 0)
		:_value(value)
		, _row(row)
		, _col(col)
	{}
};

template<class T>
class SparseMatrix
{
public:
	SparseMatrix(T *a,size_t m,size_t n,const T&invalid)//構造
		:_rowSize(m)
		, _colSize(n)
		, _invalid(invalid)
	{
		for (size_t i = 0; i < _rowSize; i++)
		{
			for (size_t j = 0; j < _colSize; j++)
			{
				if (a[i*_colSize + j] != _invalid)
				{
					_a.push_back(Triple<T>(a[i*_colSize + j],i,j));
					/*
					壓縮矩陣,循環(huán)找到矩陣中不為_invalid的元素,存儲到順序表中
					*/
				}
			}
		}
	}
	void Display()
	{
		size_t index = 0;//順序表中元素序號
		for (size_t i = 0; i <_rowSize; i++)
		{
			for (size_t j = 0; j < _colSize; j++)
			{
				/*
				循環(huán)尋找與順序表中行號、列號相同的元素
				*/
				if (index<_a.size()&&_a[index]._row == i&&_a[index]._col == j)
				{
					cout << _a[index]._value << " ";
					++index;
				}
				else
					cout << _invalid << " ";
			}
		
			cout << endl;
		}
		cout << endl;
	}
	
private:
	vector<Triple<T>> _a;
	size_t _rowSize;
	size_t _colSize;
	T _invalid;
};
void test()
{
	int a[][5] =
	{
		1, 0, 3, 0, 5,
		0, 0, 0, 0, 0,
		0, 0, 0, 0, 0,
		2, 0, 4, 0, 6,
		0, 0, 0, 0, 0,
		0, 0, 0, 0, 0
	};
	SparseMatrix<int> Array((int*)a,6,5,0);
	Array.Display();

}
int main()
{
	test();
}

網(wǎng)頁題目:對稱矩陣及稀疏矩陣的壓縮
文章起源:http://chinadenli.net/article28/ihpgjp.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供域名注冊小程序開發(fā)ChatGPT網(wǎng)站建設虛擬主機App設計

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名