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

posts - 0,  comments - 5,  trackbacks - 0
綜合網(wǎng)上的經(jīng)驗實現(xiàn)了一個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)  編輯 收藏 引用

只有注冊用戶登錄后才能發(fā)表評論。
網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理



<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>
            午夜亚洲性色视频| 亚洲欧美激情四射在线日| 亚洲欧美一区二区三区久久 | 国产精品美女xx| 一本久道久久综合狠狠爱| 亚洲国产成人91精品| 久久精品国产精品亚洲综合| 国产精品一区二区在线观看网站| 亚洲在线视频一区| 亚洲女人av| 国产欧美日韩精品一区| 久久久久综合网| 久久一本综合频道| 一本一本大道香蕉久在线精品| 亚洲精品国精品久久99热一| 国产精品成人播放| 久久九九国产精品| 免费观看久久久4p| 正在播放亚洲| 久久福利资源站| 亚洲日本欧美天堂| 亚洲一区二区三区视频| 黄色成人91| 亚洲日本中文字幕区| 国产精品入口福利| 欧美成人小视频| 欧美午夜片欧美片在线观看| 久久久久久穴| 欧美日韩国产色视频| 久久高清一区| 欧美激情视频网站| 久久精品日产第一区二区| 欧美91视频| 久久久久se| 欧美日韩一卡| 欧美成人免费播放| 国产精品天天看| 亚洲精品黄网在线观看| 国外成人在线视频网站| 9l国产精品久久久久麻豆| 黄色成人91| 亚洲午夜影视影院在线观看| 亚洲黄色高清| 性高湖久久久久久久久| 宅男噜噜噜66国产日韩在线观看| 久久裸体视频| 欧美在线观看视频在线| 欧美日韩国产精品自在自线| 欧美mv日韩mv国产网站| 国产酒店精品激情| 日韩图片一区| 亚洲精品视频免费观看| 久久精品卡一| 久久精品一区蜜桃臀影院| 国产精品v欧美精品∨日韩| 亚洲国产小视频| 亚洲成色精品| 久久精品免费播放| 久久久久国产一区二区| 国产精品永久免费| 亚洲一级影院| 午夜视频在线观看一区二区| 欧美日韩免费观看一区=区三区 | 亚洲靠逼com| 欧美r片在线| 欧美国产视频在线| 亚洲第一黄网| 麻豆成人小视频| 久久在线免费观看视频| 国产在线播精品第三| 欧美在线1区| 老色批av在线精品| 激情亚洲网站| 免费欧美在线视频| 亚洲国产精品一区二区www在线 | 欧美小视频在线观看| 亚洲精品资源美女情侣酒店| 一本色道综合亚洲| 国产精品国产馆在线真实露脸| 亚洲精选视频免费看| 亚洲免费网址| 国产欧美一区二区白浆黑人| 欧美一区二区日韩一区二区| 久久久久久亚洲精品杨幂换脸| 精品成人久久| 欧美大秀在线观看| aa级大片欧美三级| 久久高清福利视频| 亚洲成在人线av| 欧美日韩国产大片| 夜夜嗨一区二区三区| 久久黄色网页| 在线精品观看| 欧美日韩精品一区| 亚洲欧美日韩在线不卡| 免费亚洲电影在线| 一区二区三区导航| 国产一区清纯| 欧美激情综合在线| 亚洲欧美日韩国产中文在线| 免费日韩成人| 亚洲女同精品视频| 在线观看久久av| 欧美视频在线观看一区| 久久不射2019中文字幕| 亚洲精品中文字幕在线| 久久婷婷综合激情| 一区二区三区成人| 黄色在线一区| 国产精品美女午夜av| 免费日韩av电影| 欧美一级久久久| 亚洲精品久久久久中文字幕欢迎你 | 亚洲男女自偷自拍图片另类| 男人的天堂亚洲在线| 午夜在线一区| 日韩一区二区精品视频| 韩日在线一区| 国产精品分类| 欧美国产精品劲爆| 久久久久久久999精品视频| 亚洲天堂男人| 最新国产成人在线观看| 久久中文字幕导航| 久久国产精品99久久久久久老狼 | 国产精品亚洲а∨天堂免在线| 模特精品在线| 久久九九免费视频| 午夜视频在线观看一区二区| 日韩一级大片在线| 亚洲黄色小视频| 美女成人午夜| 久久一区欧美| 久久久亚洲高清| 欧美亚洲综合久久| 亚洲午夜未删减在线观看| 日韩亚洲精品视频| 亚洲精品视频在线播放| 亚洲第一网站| 1204国产成人精品视频| 国产综合av| 一区二区在线视频播放| 国内成+人亚洲| 国产自产精品| 激情另类综合| 亚洲大胆av| 亚洲七七久久综合桃花剧情介绍| 国产亚洲欧美日韩一区二区| 国产一区二区福利| 国产一区二区三区高清| 国产一区观看| 亚洲福利国产| 亚洲破处大片| 亚洲一区二区伦理| 亚洲欧美日韩视频一区| 欧美一区深夜视频| 久久久久久精| 欧美成人亚洲成人日韩成人| 亚洲大黄网站| 91久久精品美女高潮| 99精品免费网| 亚洲欧美日韩在线| 久久久久综合| 欧美精品一区二区三区视频| 欧美日韩一区在线观看视频| 国产精品入口66mio| 国内精品视频在线观看| 亚洲国产日韩美| 亚洲视频香蕉人妖| 久久精品国产欧美激情| 欧美二区在线播放| 亚洲色无码播放| 久久精品中文字幕免费mv| 欧美极品色图| 国产美女高潮久久白浆| 亚洲高清久久网| 一区二区三区免费网站| 久久精品国产亚洲一区二区三区| 麻豆精品网站| 亚洲天堂av综合网| 久久综合国产精品| 国产精品高潮粉嫩av| 国产一区二区高清视频| 亚洲精品欧美| 欧美影院一区| 亚洲国产日韩综合一区| 亚洲一区二区精品在线| 欧美成人精品在线视频| 国产亚洲欧美日韩美女| 亚洲新中文字幕| 欧美福利在线观看| 亚洲一区二区三区在线看| 欧美精品成人91久久久久久久| 国产亚洲成人一区| 亚洲永久免费av| 亚洲国产国产亚洲一二三| 久久国产免费| 国产欧美日韩高清| 亚洲尤物在线视频观看| 亚洲国产精品一区制服丝袜|