三对角矩阵

**三对角矩阵(tridiagonal):**M是一个三对角矩阵,当且仅当|i-j|>1时,M(i,j)=0。
在一个rows×rows的三对角矩阵中,非0元素排列在如下三条对角线上:
1)主对角线——i=j
2)主对角线之下的对角线(称低对角线)——i=j+1
3)主对角线之上的对角线(称高对角线)——i=j-1
这三条对角线的元素总数为3rows-2。可以用一个容量为3rows-2的一维数组element来描述三对角矩阵。
三对角矩阵
tridiagonalMatrix.cpp

/*
 * 三对角矩阵的测试函数
 * tridiagonalMatrix.cpp
*/
#include<iostream>
#include"tridiagonalmatrix.h"

using namespace std;

int main(void)
{
    tridiagonalMatrix<int> x(20);
    x.set(1,1,22);
    x.set(5,5,44);
    x.set(8,5,0);
    x.set(7,8,55);

    cout<< x.get(7,8)<<endl;
    cout << x.get(5,5) <<endl;
    cout << x.get(1,1) <<endl;
    cout << x.get(10,1) <<endl;
    cout << x.get(1,5) <<endl;

    return 0;
}

tridiagonalMatrix.h

/*
 * 三对角矩阵
 * tridiagonalMatrix.h
*/
#ifndef TRIDIAGONALMATRIX_H
#define TRIDIAGONALMATRIX_H

#include"myexceptions.h"
using namespace std;

template<class T>
class tridiagonalMatrix
{
public:
  //构造函数和析构函数
    tridiagonalMatrix(int theN = 10);
    ~tridiagonalMatrix(){delete [] element;}
    T get(int,int)const;//获取元素
    void set(int,int,const T&);//设置元素值
private:
    T* element;
    int n;
};

/*
 * 类中函数的具体实现
*/

//构造函数
template<class T>
tridiagonalMatrix<T>::tridiagonalMatrix(int theN)
{
    if(theN < 1)
        throw illegalParameterValue("Matrix size must be > 0");

    n = theN;
    element = new T[n*3-2];//三对角数组的最大非零元素个数为n*3-2
}

//get()函数实现
template<class T>
T tridiagonalMatrix<T>::get(int i, int j) const
{
    if(i < 1 || j < 1 || i > n || j > n)
        throw matrixIndexOutOfBounds();

    switch (i - j) {
    case 1:
        return element[i - 2];
    case 0:
        return element[n + i - 2];
    case -1:
        return element[2 * n + i - 2];
    default:
        return 0;
    }
}


//set()函数的具体实现
template<class T>
void tridiagonalMatrix<T>::set(int i, int j, const T& newValue)
{
    if(i < 1 || j < 1 || i > n || j > n)
        throw matrixIndexOutOfBounds();
    switch (i - j)
    {
    //数组存储元素是从最下面的对角线开始存的,依次的存储顺序是:
    //下对角线,主对角线,上对角线
    //所以才会有下面的数组计算方式
    case 1:
        element[i - 2] = newValue;//下对角线
        break;
    case 0:
        element[n + i - 2] = newValue;//主对角线
        break;
    case -1:
        element[2 * n + i - 2] = newValue;//上对角线
        break;
    default:if(newValue != 0)
            throw illegalParameterValue
                ("non-tridiagonal elements must be zero");
        break;
    }
}

#endif // TRIDIAGONALMATRIX_H

myExceptions.h

/*
 * 异常类
 * myExceptions.h
*/
#ifndef MYEXCEPTIONS_H
#define MYEXCEPTIONS_H

#include<string>
using namespace std;

//参数值不合法
class illegalParameterValue
{
  public:
    illegalParameterValue(string theMessage = "Illegal parameter value")
    {
        message = theMessage;
    }
    void outputMessage()
    {
        cout << message <<endl;
    }
private:
    string message;
};

//数组索引越界
class matrixIndexOutOfBounds
{
public:
    matrixIndexOutOfBounds(string theMessage = "Matrix index out of bounds")
    {
        message = theMessage;
    }
    void outputMessage()
    {
        cout << message <<endl;
    }
private:
    string message;
};

//数组大小不匹配
//其实这个异常这里用不到,只有在矩阵的加减法和乘法的时候才会用到
class matrixSizeMismatch
{
public:
    matrixSizeMismatch(string theMessage =
            "The size of the two matrics doesn't match")
    {
        message = theMessage;
    }
    void outputMessage()
    {
        cout << message <<endl;
    }
private:
    string message;
};
#endif // MYEXCEPTIONS_H

转载于:https://my.oschina.net/u/1771419/blog/1818524

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值