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

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| 欧美性猛交视频| 亚洲国产国产亚洲一二三| 午夜久久tv| 午夜在线精品偷拍| 欧美一区二区三区四区夜夜大片| 在线视频亚洲欧美| 亚洲午夜成aⅴ人片| 亚洲图片你懂的| 亚洲深夜福利| 欧美制服丝袜| 亚洲国产一区二区三区在线播| 在线电影院国产精品| 亚洲激情av| 亚洲午夜电影网| 久久久久国产成人精品亚洲午夜| 久久国产精品久久精品国产| 国产一区自拍视频| 亚洲高清av在线| 美女尤物久久精品| 欧美激情女人20p| 欧美色视频日本高清在线观看| 欧美三级中文字幕在线观看| 国产视频亚洲| 亚洲美女尤物影院| 久久aⅴ国产欧美74aaa| 欧美激情精品久久久| 一区二区三区产品免费精品久久75 | 国产精品日韩一区| 国内精品福利| 一区二区三区日韩欧美| 欧美在线观看网站| 亚洲黄色成人网| 亚洲欧美www| 男女视频一区二区| 国产麻豆精品视频| 欧美四级在线| 亚洲国产精品一区二区第一页| 亚洲一区二区3| 久久国产精品亚洲77777| 久久嫩草精品久久久精品| 99爱精品视频| 欧美激情第8页| 狠狠色狠狠色综合| 欧美一区二区私人影院日本 | 一区二区三区免费观看| 久久亚洲春色中文字幕久久久| 亚洲精品一区二区三区蜜桃久| 久久久999精品免费| 国产欧美精品日韩| 亚洲欧美电影院| 亚洲精品乱码久久久久久黑人| 欧美一级大片在线观看| 国产精品第一区| 一本色道久久88综合日韩精品| 久久综合99re88久久爱| 亚洲一线二线三线久久久| 欧美精品久久天天躁| 亚洲国产成人av| 麻豆成人综合网| 欧美中文字幕在线观看| 国产精品亚洲美女av网站| 亚洲综合国产| 一区二区三区免费网站| 欧美日韩视频在线| 中国成人在线视频| 久久av一区二区三区| 久久亚洲精品一区二区| 国产综合色精品一区二区三区| 国产精品免费网站在线观看| 99re成人精品视频| 91久久夜色精品国产九色| 欧美成人精品1314www| 亚洲区中文字幕| 亚洲国产精品一区制服丝袜| 免费在线观看精品| 日韩视频第一页| 亚洲精品一级| 国产精品女人久久久久久| 午夜精品久久久久久99热软件| 一区二区三区黄色| 国产精品一区二区三区免费观看| 欧美伊人久久久久久久久影院| 欧美一级夜夜爽| 国产精品二区三区四区| 欧美一区2区三区4区公司二百| 香蕉久久夜色精品| 一区在线免费观看| 亚洲精品看片| 国产偷久久久精品专区| 欧美成人精品| 欧美日韩在线一区二区| 久久国产精品久久久久久| 久久久夜夜夜| 亚洲视频一区二区在线观看| 亚洲曰本av电影| 欧美激情日韩| 最新精品在线| 国产欧美日韩一区二区三区在线观看| 裸体丰满少妇做受久久99精品| 欧美成在线视频| 亚洲欧美综合国产精品一区| 久久夜色精品国产| 亚洲欧美日韩国产一区二区| 久久成人一区二区| 亚洲视频欧美在线| 麻豆成人精品| 久久爱www久久做| 欧美极品在线观看| 久久一二三区| 国产精品区一区二区三| 亚洲二区在线视频| 国产视频一区欧美| 亚洲毛片在线看| 亚洲国产精品久久精品怡红院 | 欧美激情视频免费观看| 国产精品国产三级国产普通话三级| 久久久久久久久久看片| 欧美日韩一级视频| 欧美成年人视频网站| 国产伦精品一区二区三区视频黑人| 欧美www在线| 国产一区深夜福利| 在线一区亚洲| 宅男在线国产精品| 看欧美日韩国产| 久久九九热re6这里有精品| 国产精品任我爽爆在线播放| 久久综合国产精品| 国产综合香蕉五月婷在线| 亚洲一区二区在线观看视频| 亚洲一级片在线看| 一区二区三区日韩欧美精品| 亚洲精品在线观看免费| 免费在线观看精品| 亚洲黄色在线观看| 亚洲激情电影在线| 美国十次成人| 亚洲福利国产精品| 亚洲六月丁香色婷婷综合久久| 美女视频黄a大片欧美| 欧美大片一区| 亚洲美女91| 欧美片第一页| 在线亚洲激情| 久久精品91久久久久久再现| 国产色产综合色产在线视频| 亚洲欧美另类在线观看| 久久久久久高潮国产精品视| 一色屋精品视频在线看| 久久综合色综合88| 亚洲国产视频一区二区| 在线视频欧美一区| 国产欧美69| 久久这里有精品15一区二区三区| 欧美国产日产韩国视频| 亚洲精品影视| 欧美亚州一区二区三区| 午夜精品国产更新| 免费成人av资源网| 亚洲伦理精品| 国产精品无码专区在线观看| 欧美影视一区| 久久精品国产第一区二区三区| 国产午夜精品理论片a级探花| 亚洲欧美在线看| 老司机精品视频网站| 亚洲精品一区二区三| 欧美精选午夜久久久乱码6080| 亚洲免费一在线| 国产精品色午夜在线观看| 久久久91精品国产一区二区三区| 老牛影视一区二区三区| 亚洲视频在线观看视频| 国产精品久久久久久超碰| 亚洲欧美日韩中文视频| 久久性天堂网| 亚洲免费成人av| 国产精品久久久久久久久久免费 | 欧美激情一区二区三区不卡| 亚洲激情第一页| 欧美日韩一区二区免费视频| 久久精品夜色噜噜亚洲aⅴ| 嫩模写真一区二区三区三州| 亚洲欧洲日本专区| 欧美三级电影大全| 免费欧美在线| 夜夜狂射影院欧美极品| 亚洲欧美三级在线| 国产亚洲精品aa| 欧美成人精品激情在线观看| 亚洲国产一二三| 欧美一区国产二区| 夜夜躁日日躁狠狠久久88av| 正在播放亚洲| 国产欧美日韩| 欧美高清在线精品一区| 亚洲精品美女久久7777777| 欧美午夜电影在线| 久久一区亚洲|