青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

posts - 0,  comments - 5,  trackbacks - 0
綜合網上的經驗實現了一個double型的Matrix C++類。
不想弄成template了。
支持各種基本運算+-*/ 和求逆矩陣
其中求逆矩陣是矩陣運算的難點,也是最耗時的模塊。
#ifndef __CMATRIX_H__
#define __CMATRIX_H__

#include 
<iostream>
using namespace std;

class CMatrix
{
public:
    
//default constructor
    CMatrix(void);

    
//unit Matrix constructor
    CMatrix(long n);

    
//one dimension CMatrix constructor
    CMatrix(double *pSourceValue, long nWidth);

    
//two dimension CMatrix constructor
    CMatrix(double *pSourceValue, long nWidth,long nHeight);

    
//copy constructor
    CMatrix(const CMatrix &);

    
//default destructor
    ~CMatrix(void);
public:

    
/***************basic matrix operator*******************************/
    
    
    
/*******************************************************************
      * Function: isVector
      * Describe: judge the matrix is a number
      * Param: NULL
      * Return Value: 
            false: is a number
            true: is a matrix or a vector
      * Author: Saha
      * Date Time: 2010-8-18 10:56:45
      ******************************************************************
*/

    
bool isVector();
    
    
/*******************************************************************
     * Function: GetDeterminant
     * Describe: get the value of the matrix's Determinant.
     * Param: NULL
     * Return Value: 
        double: the value of the matrix's Determinant
     * Author: Saha
     * Date Time: 2010-8-18 10:58:13
     ******************************************************************
*/

    
double GetDeterminant();

    
/*******************************************************************
     * Function: isPositive
     * Describe: judge the matrix is a Positive matrix
     * Param: NULL
     * Return Value: 
        true: is
        false: is not
     * Author: Saha
     * Date Time: 2010-8-18 11:00:18
     ******************************************************************
*/

    
bool isPositive();

    
/*******************************************************************
    * Function: Transpose
    * Describe: get the Transposed matrix
    * Param: NULL
    * Return Value: 
        CMatrix: the Transposed matrix
    * Author: Saha
    * Date Time: 2010-8-18 11:01:40
    ******************************************************************
*/

    CMatrix Transpose();

    
/*******************************************************************
     * Function: GetsubMatrix
     * Describe: get the subMatrix.
        example:  1 0 0 2   its subMatrix is 1 0 while offset is 2
                        2 1 3 1                             2 1
                        3 3 3 3 
     * Param: 
        long offset:the subMatrix's width and height
     * Return Value: 
        CMatrix: the subMatrix
     * Author: Saha
     * Date Time: 2010-8-18 11:04:34
     ******************************************************************
*/

    CMatrix GetsubMatrix(
long offset);
    
    
/*******************************************************************
     * Function: GetInverse
     * Describe: Get Inverse Matrix using adjoint matrix 
     * Param: 
        CMatrix &m: the Inversed matrix
     * Return Value: 
        true: has Inversed matrix
        false:the matrix is a singular matrix and hasn't Inversed matrix
     * Author: Saha
     * Date Time: 2010-8-18 14:04:37
     ******************************************************************
*/

    
bool GetInverse(CMatrix &m);
    
public:
    
/***********Overloaded Part*****************/
    
//matrix add
    CMatrix operator+(CMatrix &);

    
//matrix sub
    CMatrix operator-(CMatrix &);

    
//matrix multiply with matrix
    CMatrix operator*(CMatrix &);

    
//matrix multiply with real number
    CMatrix operator*(double alpha);

    
//real number multiply with matrix
    friend CMatrix operator*(double alpha, CMatrix &m);
    
    
//matrix divide with matrix
    CMatrix operator/(CMatrix &);

    CMatrix 
operator+=(CMatrix &);

    CMatrix 
operator-=(CMatrix &);

    CMatrix 
operator*=(double alpha);

    
//matrix evaluate operation
    CMatrix &operator=(CMatrix &);

    
//matrix suffix operation
    double *operator[](long heightPos);

    
//matrix out stream
    friend ostream & operator << (ostream &, CMatrix &);

public:
    
long getHeight();
    
long getWidth();

    
void GetValue(double *p);

private:
    
double GetAlgebraicCofactor(long nX, long nY); 

    
//the pointer to store the matrix data
    double * pMatrix;
    
long m_nWidth;
    
long m_nHeight;
}
;

#endif

#include "Matrix.h"
#include 
<assert.h>

CMatrix::CMatrix(
void)
{
    pMatrix 
= new double[1];
    m_nWidth 
= 0;
    m_nHeight 
= 0;
}


CMatrix::CMatrix(
long n)
{
    m_nHeight 
= m_nWidth = n;
    pMatrix 
= new double[n*n];

    
for(long i = 0; i < n; i++)
    
{
        
for(long j = 0; j < n; j++)
        
{
            
if(i == j)
            
{
                
*(pMatrix + n*+ j)=1;
            }

            
else 
            
{
                
*(pMatrix + n*+ j)=0;
            }

        }

    }

}


CMatrix::CMatrix(
double *pSourceValue,long nWidth)
{
    
long nHeight = 1;
    pMatrix 
= new double[nWidth*nHeight];

    
if (NULL == pSourceValue)
    
{
        
for(long i = 0; i < nHeight; i++)
        
{
            
for(long j = 0; j < nWidth; j++)
            
{
                
*(pMatrix + nWidth*+ j) = 0;
            }

        }

    }

    
else
    
{
        
for(long i = 0; i < nHeight; i++)
        
{
            
for(long j = 0; j < nWidth; j++)
            
{
                
*(pMatrix + nWidth*+ j) = *(pSourceValue + nWidth*+ j);
            }

        }

    }


    m_nWidth 
= nWidth;
    m_nHeight 
= nHeight;
}


CMatrix::CMatrix(
double *pSourceValue,long nWidth,long nHeight)
{
    pMatrix 
= new double[nWidth*nHeight];
    
if (NULL == pSourceValue)
    
{
        
for(long i = 0; i < nHeight; i++)
        
{
            
for(long j = 0; j < nWidth; j++)
            
{
                
*(pMatrix + nWidth*+ j) = 0;
            }

        }

    }

    
else
    
{
        
for(long i = 0; i < nHeight; i++)
        
{
            
for(long j = 0; j < nWidth; j++)
            
{
                
*(pMatrix + nWidth*+ j) = *(pSourceValue + nWidth*+ j);
            }

        }

    }


    m_nWidth 
= nWidth;
    m_nHeight 
= nHeight;
}


//copy constructor
CMatrix::CMatrix(const CMatrix & m)
{
    m_nHeight 
= m.m_nHeight;
    m_nWidth 
= m.m_nWidth;

    pMatrix 
= new double[m_nHeight*m_nWidth];

    
for(long i = 0; i < m_nHeight; i++)
    
{
        
for(long j = 0; j < m_nWidth; j++)
        
{
            
*(pMatrix + m_nWidth*+ j) = *(m.pMatrix + m_nWidth*+ j);
        }

    }

}


CMatrix::
~CMatrix(void)
{
    delete []pMatrix;
}


long CMatrix::getWidth()
{
    
return m_nWidth;
}


long CMatrix::getHeight()
{
    
return m_nHeight;
}


void CMatrix::GetValue(double *p)
{
    
for(long i = 0; i < m_nHeight; i++)
    
{
        
for(long j = 0; j < m_nWidth; j++)
        
{
            
*(p + m_nHeight*+ i) = *(pMatrix + m_nWidth*+ j);
        }

    }

}


bool CMatrix::isVector()
{
    
return !(m_nWidth == 1 && m_nHeight == 1);
}


CMatrix CMatrix::GetsubMatrix(
long offset)
{
    assert(m_nHeight 
== m_nWidth && offset <= m_nWidth && offset >= 0);
    
double * pTmp = new double[offset*offset];

    
for(long i = 0; i < offset; i++)
    
{
        
for(long j = 0; j < offset; j++)
        
{
            
*(pTmp + offset*+ j)=*(pMatrix + m_nWidth*+ j);
        }

    }

    CMatrix m(pTmp, offset, offset);
    delete []pTmp;
    
return m;
}



double CMatrix::GetDeterminant()
{
    
long i, j, m; 
    
long lop = 0;
    
double result = 0;
    
double mid = 1;

    
if(1 != m_nWidth)
    
{
        lop 
= (m_nWidth == 2)? 1 : m_nWidth;     
        
for(m = 0; m < lop; m++)
        
{
            mid
=1;
            
//order add 
            for(i = 0, j = m; i < m_nWidth; i++, j++)
            
{
                mid 
= mid * (*(pMatrix + i*m_nWidth + j%m_nWidth));
            }

            result 
+= mid;
        }


        
for(m = 0; m < lop; m++)
        
{                      
            mid 
= 1;          
            
//inverse order sub
            for(i = 0, j = m_nWidth - 1 - m + m_nWidth; i < m_nWidth; i++, j--)
            
{
                mid 
= mid * (*(pMatrix + i*m_nWidth + j%m_nWidth));
            }

            result 
-= mid;
        }

    }

    
else 
    
{
        result 
= *pMatrix;
    }

    
return (result);
}


bool CMatrix::isPositive()
{
    assert(m_nWidth 
== m_nHeight);
    
bool result = true;
    CMatrix m;

    
for(long i = 1; i <= m_nHeight; i++)
    
{
        m 
= this->GetsubMatrix(i);
        
if(m.GetDeterminant() <= 0)
        
{
            result 
= false;
            
break;
        }

    }

    
return result;
}


CMatrix  CMatrix::Transpose()
{
    
double * pTmp = new double[m_nWidth*m_nHeight];
    
for(long i = 0; i < m_nHeight; i++)
    
{
        
for(long j = 0; j < m_nWidth; j++)
        
{
            
*(pTmp + m_nHeight*+ i) = *(pMatrix + m_nWidth*+ j);
        }

    }

    CMatrix m(pTmp, m_nHeight, m_nWidth);
    delete []pTmp;
    
return m;
}


double CMatrix::GetAlgebraicCofactor(long nX, long nY)
{
    assert(nX 
< m_nWidth && nY < m_nHeight);
    assert(m_nHeight 
== m_nWidth);

    
long tmpHeight = m_nHeight - 1;
    
long tmpWidth = m_nWidth - 1;
    
double *ptmp = new double[tmpWidth*tmpHeight];
    
double *= ptmp;

    
for (int i = 0; i < m_nHeight; i++)
    
{
        
for (int j = 0; j < m_nWidth; j++)
        
{
            
if (i != nX && j != nY)
            
{
                
*= *(pMatrix + i*m_nWidth + j);
                p
++;
            }

        }

    }


    CMatrix m(ptmp, tmpWidth, tmpHeight);
    
int nGene = (nX + nY) % 2 == 0 ? 1 : -1;
    
return (double)nGene * m.GetDeterminant();
}


bool CMatrix::GetInverse(CMatrix &m)
{
    
double detA = GetDeterminant();
    
if (0 == detA)
    
{
        
return false;
    }

    
if (1 == m_nWidth && 1 == m_nHeight)
    
{
        CMatrix mIns(
1);
        m 
= 1/detA * mIns;        
        
return true;
    }

    
double *pTmp = new double[m_nWidth*m_nHeight];

    
for (int i = 0; i < m_nHeight; i++)
    
{
        
for (int j = 0; j < m_nWidth; j++)
        
{
            
*(pTmp + i*m_nWidth + j) = GetAlgebraicCofactor(j, i);
        }

    }

    CMatrix mIns(pTmp, m_nWidth, m_nHeight);
    detA 
= 1/detA;
    mIns 
= detA * mIns;
    m 
= mIns;
    delete []pTmp;
    
return true;
}


CMatrix CMatrix::
operator +(CMatrix &m1)
{
    assert(m1.m_nHeight 
== m_nHeight && m1.m_nWidth == m_nWidth);

    
long tmpHeight = m1.m_nHeight;
    
long tmpWidth = m1.m_nWidth;
    
double *pTmp = new double[tmpWidth*tmpHeight];

    
for(long i = 0; i < tmpHeight; i++)
    
{
        
for(long j = 0; j < tmpWidth; j++)
        
{
            
*(pTmp + tmpWidth*+ j) = *(m1.pMatrix + tmpWidth*+ j) + *(pMatrix + tmpWidth*+ j);
        }

    }

    CMatrix m(pTmp, tmpWidth, tmpHeight);
    delete []pTmp;
    
return m;
}


CMatrix CMatrix::
operator -(CMatrix &m1)
{
    assert(m1.m_nHeight 
== m_nHeight && m1.m_nWidth == m_nWidth);

    
long tmpHeight = m1.m_nHeight;
    
long tmpWidth = m1.m_nWidth;
    
double * pTmp = new double[tmpWidth*tmpHeight];

    
for(long i = 0; i < tmpHeight; i++)
    
{
        
for(long j = 0; j < tmpWidth; j++)
        
{
            
*(pTmp + tmpWidth*+ j) = *(pMatrix + tmpWidth*+ j) - *(m1.pMatrix + tmpWidth*+ j);
        }

    }

    CMatrix m(pTmp, tmpWidth, tmpHeight);
    delete []pTmp;
    
return m;
}


CMatrix CMatrix::
operator *(CMatrix &m1)
{
    
if(!this->isVector() && m1.isVector())//left number ,right matrix
    {
        CMatrix m;
        m
= pMatrix[0* m1;
        
return m;
    }

    
else if(this->isVector() && !m1.isVector())//left matrix, right number
    {
        CMatrix m;
        m 
= (*this* m1[0][0];
        
return m;
    }

    
else if(!this->isVector() && !m1.isVector())//left and right are numbers
    {
        
double * pTmp = new double[1];
        pTmp[
0= pMatrix[0* m1[0][0];
        CMatrix m(pTmp, 
11);
        delete []pTmp;
        
return m;
    }

    
else if(this->isVector() && m1.isVector() && m_nWidth == m1.m_nHeight)//left and right are matrix
    {
        
double sum;
        
double * pTmp=new double[m_nHeight*m1.m_nWidth];

        
for(long i = 0; i < m_nHeight; i++)
        
{
            
for(long j = 0; j < m1.m_nWidth; j++)
            
{
                sum 
= 0;
                
for(long k = 0; k < m_nWidth; k++)
                
{
                    sum 
+= (*(pMatrix + m_nWidth*+ k))*(m1[k][j]);
                }

                
*(pTmp + m1.m_nWidth*+ j) = sum;
            }

        }

        CMatrix m(pTmp, m1.m_nWidth, m_nHeight);
        delete []pTmp;
        
return m;
    }

    
else//unknown operation
    {
        assert(
0);
        
return *this;
    }

}


CMatrix 
operator*(double alpha,CMatrix & m1)
{
    CMatrix m 
= m1;
    
for(long i = 0; i < m.m_nHeight; i++)
    
{
        
for(long j = 0; j < m.m_nWidth; j++)
        
{
            m[i][j] 
= alpha*m1[i][j];
        }

    }

    
return m;
}


CMatrix CMatrix::
operator/(CMatrix & m1)
{
    CMatrix m2;
    m1.GetInverse(m2);
    
return *this * m2;
}


CMatrix CMatrix::
operator*(double alpha)
{
    
return alpha*(*this);
}


CMatrix CMatrix::
operator+=(CMatrix &m)
{
    
return *this + m;
}


CMatrix CMatrix::
operator-=(CMatrix &m)
{
    
return *this - m;
}


CMatrix CMatrix::
operator *=(double alpha)
{
    
return (*this* alpha;
}



CMatrix 
& CMatrix::operator =(CMatrix & m)
{
    
if(&== this
    
{
        
return *this;
    }

    m_nHeight 
= m.m_nHeight;
    m_nWidth 
= m.m_nWidth;
    delete []pMatrix;
    pMatrix 
= new double[m_nHeight*m_nWidth];

    
for(long i = 0; i < m_nHeight; i++)
    
{
        
for(long j = 0; j < m_nWidth; j++)
        
{
            
*(pMatrix + m_nWidth*+ j) = *(m.pMatrix + m_nWidth*+ j);
        }

    }

    
return *this;
}


double * CMatrix::operator [](long heightPos)
{
    assert(heightPos 
>= 0 && heightPos < m_nHeight);
    
return pMatrix + m_nWidth*heightPos;
}


ostream 
& operator<<(ostream & os,CMatrix & m)
{
    os 
<< "CMatrix:m_nHeight=" << m.m_nHeight << endl;
    os 
<< "CMatrix:m_nWidth=" << m.m_nWidth << endl;

    
for(long i = 0; i < m.m_nHeight; i++)
    
{
        
for(long j = 0; j < m.m_nWidth; j++)
        
{
            os 
<< m[i][j] << " "
        }
 
        os 
<< endl;
    }

    
return os;
}




測試:
#include "Matrix.h"


void main()
{
    
double arr[3][3]={{101}{210}{-32-5}};
    CMatrix m1(
&arr[0][0], 33), m2(&arr[0][0], 33);
    CMatrix m3;
    cout 
<< m3 << endl;
    m2.GetInverse(m3);
    cout 
<< m1 << endl;
    cout 
<< 4*m2 << endl;
    cout 
<< m3 << endl;
    cout 
<< m1 + m3 << endl;
    cout 
<< m1 - m3 << endl;
    cout 
<< m1 * m3 << endl;
    system(
"pause");
    
return;
}
posted on 2010-08-18 17:06 saha 閱讀(751) 評論(0)  編輯 收藏 引用

<2025年9月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用鏈接

留言簿

文章分類

文章檔案

收藏夾

搜索

  •  

最新評論

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            一区二区三区日韩| 亚洲国产va精品久久久不卡综合| 日韩一级免费| 久久综合国产精品| 久久久久久一区| 国产一区二区三区四区三区四| 欧美中文字幕| 欧美一区国产一区| 国产区精品在线观看| 久久精品99久久香蕉国产色戒| 亚洲日本电影| 欧美成人一区在线| 欧美精品午夜| 午夜精品偷拍| 久久精品国产精品亚洲| 91久久夜色精品国产网站| 亚洲人妖在线| 国产精品夜夜夜| 欧美成人综合网站| 欧美精品在线网站| 久久国产一区| 欧美国产综合一区二区| 欧美一区二区精品| 亚洲高清影视| 猛干欧美女孩| 亚洲激情视频网| 亚洲精品孕妇| 久久蜜桃精品| 亚洲第一福利社区| 一区二区三区毛片| 欧美日韩一级黄| 亚洲在线成人精品| 久久国产精品久久久久久| 国产亚洲欧美日韩美女| 中日韩男男gay无套 | 好看的日韩视频| 亚洲一区二区伦理| 亚洲欧美视频一区| 国产精品乱码一区二三区小蝌蚪| 在线一区视频| 久久久一二三| 一本一本久久a久久精品综合妖精 一本一本久久a久久精品综合麻豆 | 韩国av一区二区| 亚洲性视频h| 亚洲精品自在久久| 欧美男人的天堂| 日韩天堂在线视频| 日韩午夜高潮| 国产精品久久久久久久久久妞妞| 国产色产综合色产在线视频 | 亚洲六月丁香色婷婷综合久久| 亚洲区在线播放| 亚洲国产aⅴ天堂久久| 久热国产精品视频| 亚洲在线视频网站| 欧美伦理视频网站| 亚洲电影专区| 99热精品在线观看| 欧美日韩国产在线看| 欧美福利一区二区| 黄色国产精品一区二区三区| 日韩亚洲在线| 国产乱码精品1区2区3区| 老司机精品久久| 欧美色网在线| 亚洲一区二区综合| 欧美一级网站| 欧美精品激情在线观看| 免费视频一区| 久久夜色精品亚洲噜噜国产mv | 蜜桃av噜噜一区| 亚洲高清在线播放| 亚洲国产福利在线| 亚洲午夜视频在线| 国产精品久久久久久久午夜| 一区二区欧美视频| 亚洲欧美激情视频在线观看一区二区三区| 欧美三级中文字幕在线观看| 欧美亚洲在线| 亚洲东热激情| 亚洲免费电影在线| 久久精品一区二区三区中文字幕| 亚洲欧美一区二区在线观看| 欧美国产欧美亚洲国产日韩mv天天看完整| 欧美高清视频| 亚洲精品中文在线| 亚洲欧洲视频在线| 欧美在现视频| 欧美日韩天天操| 亚洲人成网站在线观看播放| 亚洲欧美在线视频观看| 欧美日韩国产在线看| 99国产精品视频免费观看一公开 | 亚洲国产欧美另类丝袜| 亚洲第一网站| 亚洲天堂成人在线观看| 亚洲精选久久| 亚洲人成亚洲人成在线观看| 国产精品成人va在线观看| 欧美成人在线网站| 亚洲狼人精品一区二区三区| 国外成人性视频| 国模叶桐国产精品一区| 亚洲国产小视频| 一区二区三区四区国产精品| 午夜精品亚洲| 亚洲国产cao| 欧美一区二区三区喷汁尤物| 欧美一级午夜免费电影| 亚洲免费观看高清完整版在线观看熊 | 噜噜噜噜噜久久久久久91| 亚洲激情成人在线| 国产精品99久久久久久www| 99视频在线精品国自产拍免费观看| 欧美激情一区二区三级高清视频| 亚洲人体大胆视频| 亚洲精品久久视频| 亚洲电影在线免费观看| 日韩视频免费在线| 毛片一区二区| 久久久精品久久久久| 亚洲看片免费| 亚洲精品欧洲精品| 国产午夜精品在线| 欧美精品在线观看一区二区| 国产亚洲视频在线| 国产欧美日韩综合一区在线播放| 欧美日韩亚洲一区三区 | 国产一区二区三区在线观看网站| 亚洲深夜福利网站| 久久中文字幕一区二区三区| 在线免费高清一区二区三区| 欧美xx69| 欧美日韩三级电影在线| 日韩视频在线观看| 亚洲网友自拍| 欧美韩国日本一区| 久久高清免费观看| 国产精品乱子久久久久| 日韩视频中文字幕| 亚洲丁香婷深爱综合| 久久久久www| 国产欧美va欧美va香蕉在| 在线看片日韩| 欧美成在线观看| 久久久亚洲人| 亚洲春色另类小说| 欧美一区二区精品| 亚洲美女在线国产| 欧美午夜精品一区二区三区| 99精品久久免费看蜜臀剧情介绍| 亚洲国产一区二区三区高清| 欧美日韩成人在线播放| 亚洲国内自拍| 亚洲精品在线免费| 亚洲激情综合| 欧美成人黑人xx视频免费观看| 亚洲精品欧美激情| 午夜精品久久久久久久男人的天堂| 国产精品国产精品| 国产毛片精品视频| 久久伊人精品天天| 欧美天堂亚洲电影院在线播放| 欧美一区二区三区视频免费播放| 欧美成人亚洲| 美女日韩在线中文字幕| 欧美人与性动交α欧美精品济南到| 欧美成人伊人久久综合网| 欧美视频在线免费| 欧美在线日韩| 欧美精彩视频一区二区三区| a91a精品视频在线观看| 国产欧美一区二区三区在线老狼| 国产麻豆综合| 午夜精品美女自拍福到在线| 中日韩男男gay无套| 久久久国产91| 亚洲精品视频免费观看| av成人手机在线| 亚洲一区精品视频| 欧美久久九九| 一区二区三区视频在线观看| 男男成人高潮片免费网站| 欧美成人小视频| 在线看视频不卡| 久久精品一区二区三区不卡| 久久久777| 欧美成人亚洲成人| 欧美激情按摩| 亚洲精品一区在线观看| 看片网站欧美日韩| 女人天堂亚洲aⅴ在线观看| 在线观看的日韩av| 久久青草欧美一区二区三区| 欧美一区日韩一区| 永久域名在线精品| 久久免费偷拍视频| 欧美一级在线视频| 好吊视频一区二区三区四区| 久久九九久精品国产免费直播|