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

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>
            欧美日韩国产在线| 免费一级欧美在线大片| 99视频+国产日韩欧美| 日韩亚洲欧美成人一区| 亚洲在线视频网站| 久久久久九九九| 另类图片综合电影| 国产精品麻豆成人av电影艾秋| 欧美精品亚洲精品| 国产午夜精品在线| 99精品国产高清一区二区| 久久久久久**毛片大全| 在线观看日韩av| 亚洲中字黄色| 欧美成人精品一区二区三区| 日韩视频免费观看| 国产精品视频成人| 99在线|亚洲一区二区| 久久久久综合网| 一区二区三区欧美在线观看| 女人色偷偷aa久久天堂| 亚洲无人区一区| 亚洲国产婷婷| 在线一区二区三区做爰视频网站 | 国产欧美一区二区精品仙草咪| 亚洲国产精品嫩草影院| 欧美在线在线| 亚洲天堂成人| 国产精品久久久久久久久久久久久| 亚洲国产精品福利| 亚洲综合日韩在线| 国产精品久久久久久久久久尿| 久久久精品五月天| 欧美在线视频全部完| 国产精品99一区二区| 亚洲午夜激情在线| 久久久久久亚洲精品杨幂换脸 | 亚洲电影下载| 欧美激情第8页| 亚洲精品国偷自产在线99热| 久久一区二区三区四区五区| 久久精品电影| 永久免费毛片在线播放不卡| 在线观看三级视频欧美| 99国产精品久久久久久久| 亚洲国产mv| 久久国产精品一区二区三区四区| 亚洲一区二区三区免费观看| 久久综合久久美利坚合众国| 久久久精彩视频| 国产精品视频午夜| 亚洲视频第一页| 国产精品日韩在线播放| 亚洲精品久久在线| 欧美亚州韩日在线看免费版国语版| 老牛影视一区二区三区| 欧美电影免费观看高清完整版| 日韩一级在线观看| 麻豆精品一区二区综合av | 欧美日韩亚洲一区二区三区在线 | 欧美一二三视频| 亚洲欧美日韩国产中文| 国产一区二区丝袜高跟鞋图片 | 亚洲精品在线观看视频| 亚洲电影免费观看高清完整版在线观看 | 欧美精品一区二区三| 亚洲综合色在线| 欧美视频在线不卡| 久久精品视频在线看| 欧美激情视频在线免费观看 欧美视频免费一| 欧美一区精品| 欧美v日韩v国产v| 欧美a级片一区| 亚洲人成欧美中文字幕| 亚洲永久免费精品| 欧美一站二站| 国产在线观看91精品一区| 亚洲激情视频在线播放| 国产美女诱惑一区二区| 亚洲欧洲综合另类| 中文国产成人精品| 久久欧美中文字幕| 欧美一级大片在线免费观看| 国产日韩在线不卡| 久久久久久91香蕉国产| 亚洲第一黄网| 亚洲视频 欧洲视频| 国产情人综合久久777777| 欧美在线日韩在线| 亚洲激情影视| 欧美在线视频不卡| 亚洲三级电影在线观看| 国产精品成人av性教育| 欧美在线视频全部完| 亚洲黄色小视频| 亚洲欧美另类国产| 欧美色123| 久久精品午夜| 久久久91精品国产一区二区精品| 亚洲第一在线综合在线| 欧美在线中文字幕| 亚洲黑丝一区二区| 欧美在线黄色| 亚洲人成在线观看一区二区| 国产精品乱码人人做人人爱| 久久先锋影音av| 一区二区三区色| 麻豆精品在线播放| 亚洲欧美中文日韩v在线观看| 欧美三级视频在线| 久久久水蜜桃av免费网站| 亚洲一区二区欧美| 亚洲国产中文字幕在线观看| 欧美亚洲三区| 这里只有精品在线播放| 亚洲承认在线| 国产精品伊人日日| 欧美日韩精品二区| 蜜桃av一区二区三区| 亚洲综合色丁香婷婷六月图片| 亚洲国产另类 国产精品国产免费| 欧美一区二区播放| 一区二区三区精品国产| 亚洲国产欧美一区二区三区久久| 欧美成人激情视频免费观看| 久久成人一区| 午夜在线精品| 免费看亚洲片| 日韩视频永久免费| 亚洲成色www8888| 韩国女主播一区| 欧美韩日一区二区| 久久亚洲精品一区二区| 欧美一激情一区二区三区| 久久精品欧美日韩| 欧美在线视频观看免费网站| 亚洲自拍偷拍一区| 一本色道88久久加勒比精品| 日韩午夜免费| 日韩网站在线观看| 日韩视频一区| 妖精视频成人观看www| 亚洲美女色禁图| 日韩视频在线一区| 在线综合欧美| 亚洲一卡二卡三卡四卡五卡| 亚洲一区二区三区视频播放| 亚洲图片你懂的| 亚洲欧美大片| 久久精品国产96久久久香蕉| 香蕉乱码成人久久天堂爱免费| 亚洲综合色噜噜狠狠| 欧美亚洲视频在线观看| 久久国产精品久久久久久久久久 | 欧美多人爱爱视频网站| 亚洲一区国产视频| 欧美在线视频观看| 久久夜色精品国产| 亚洲国产99| 99re热这里只有精品视频| 亚洲在线成人| 久久久久久亚洲精品中文字幕 | 欧美性开放视频| 国产麻豆成人精品| 在线欧美影院| 亚洲视频免费观看| 久久精品一二三区| 亚洲国产精品va在线看黑人动漫| 亚洲狼人综合| 亚洲大胆人体在线| 香蕉久久夜色| 久久一二三区| 亚洲精品自在久久| 午夜精品成人在线| 欧美顶级艳妇交换群宴| 欧美系列精品| 亚洲成人在线网站| 亚洲欧美日韩中文在线制服| 女女同性精品视频| 一本大道久久a久久精二百| 久久大香伊蕉在人线观看热2| 欧美www视频在线观看| 国产精品嫩草影院av蜜臀| 亚洲国产欧美在线人成| 欧美一区久久| 日韩视频精品在线| 久久久综合视频| 国产精品男gay被猛男狂揉视频| 亚洲国产日韩欧美| 久久裸体视频| 亚洲一级片在线观看| 欧美成人国产| 国产在线观看精品一区二区三区| a91a精品视频在线观看| 欧美成人高清视频| 欧美一区日本一区韩国一区| 欧美日韩视频在线一区二区 | 欧美日韩一卡二卡| 亚洲人成7777| 久久综合久色欧美综合狠狠|