前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >原 三对角矩阵

原 三对角矩阵

作者头像
青木
发布2018-05-28 15:11:46
1K0
发布2018-05-28 15:11:46
举报

**三对角矩阵(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
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档