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

下三角矩阵

作者头像
青木
发布2018-05-28 15:13:19
1.1K0
发布2018-05-28 15:13:19
举报

**下三角矩阵(lower triangular):**M是一个下三角矩阵,当且仅当i<j时,M(i,j)=0 在一个n行的下三角矩阵中,非0区域的第一行有1个元素,第二行有2个元素,……第n行有个元素。 在一个上三角矩阵中,非0区域的第一行有n个元素,第二行有n-1个元素,……,第n行有1个元素。 这两种三角形非0区域共有n(n+1)/2个非零元素。 考察一个下三角矩阵的元素L(i,j)。如果i<j,则L(i,j)=0; 如果i>=j,则L(i,j)位于非0区域。

上下三角矩阵
上下三角矩阵

lowerTriangularMatrix.cpp

/*
 * 下三角矩阵的测试函数
 * lowerTriangularMatrix.cpp
*/
#include<iostream>
#include"lowertriangularmatrix.h"

using namespace std;

int main(void)
{
    lowerTriangularMatrix<int> x(20);
    x.set(1,1,22);
    x.set(5,3,44);
    x.set(8,5,0);
    x.set(10,2,55);
    x.set(8,5,0);

    cout << x.get(10,2) << endl;
    cout << x.get(5,3) << endl;
    cout << x.get(1,1) << endl;
    cout << x.get(10,14) << endl;
    cout << x.get(8,5) << endl;

    return 0;
}

lowerTriangularMatrix.h

/*
 * 下三角矩阵的类定义
 * lowerTriangularMatrix.h
*/
#ifndef LOWERTRIANGULARMATRIX_H
#define LOWERTRIANGULARMATRIX_H

#include"myexceptions.h"
using namespace std;

template<class T>
class lowerTriangularMatrix
{
  public:
    //构造函数和析构函数
    lowerTriangularMatrix(int theN = 10);
    ~lowerTriangularMatrix(){delete [] element;}

    T get(int,int) const;//获取矩阵中元素
    void set(int,int,const T&);//设置矩阵元素值
private:
    int n;//矩阵非零元素最大个数
    T *element;//矩阵中元素存储所在数组
};

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

    n = theN;
    element = new T [n*(n + 1)/2];
}


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

    if( i >= j)
        //矩阵非零元素按照行主映射方式,第一行元素个数为1,第二行为2,……,第i行为i
        //所以元素在数组中的顺序应该为i*(i-1)/2+j-1
        return element[i*(i - 1)/2+j-1];
    else
        return 0;
}


//set()函数的实现
template<class T>
void lowerTriangularMatrix<T>::set(int i, int j, const T& newValue)
{
    if(i < 1 || j < 1 || i > n || j > n)
        throw matrixIndexOutOfBounds();

    if(i >= j)
        element[i*(i-1)/2+j-1] = newValue;
    else
        if(newValue != 0)
            throw illegalParameterValue
                ("elements not in lower triangle must be zero");
}

#endif // LOWERTRIANGULARMATRIX_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 归档