5,136,034 members and growing! (12,412 online)
Email Password   helpLost your password?
Languages » C / C++ Language » General     Advanced

A templated Matrix class using 2d dynamic array declaration & having The most needed Mathematical functions of Matrix & also all the operators needed in standard c++ (without using any vc commands)

By shahin namini

This program is written By Shahin Namini
C++, Windows, Visual Studio, Dev

Posted: 22 Feb 2007
Updated: 22 Feb 2007
Views: 6,760
Announcements



Search    
Advanced Search
Sitemap
15 votes for this Article.
Popularity: 2.37 Rating: 2.02 out of 5
8 votes, 53.3%
1
1 vote, 6.7%
2
0 votes, 0.0%
3
1 vote, 6.7%
4
5 votes, 33.3%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

This Matrix class is a templated class that can be initialized as any kind of numeric types(int, double,float,& etc). it includes Inverse & determinant functions (which are not so easy to write the code) & also all the operators needed for a Matrix class like Multiplying 2 matrix or multiplying a digit to the matrix. it is able to be any size & also it is easy to use.

Instruction

the uploaded file check.zip contains a file which shows how the Matrix can get initialized & how the operators or functions work. Note that parentheses is maybe the most important operator.it allows you to get access to the array elements ,as :

matrix1(2,0)=5;

Member functions of class Matrix

These are all the member functions & operators of the class :

template<class T>
class Matrix
{
public:

  1. Matrix(int Row_Size,int Column_Size)
  2. Matrix() >
  3. Matrix(const Matrix<T> &)
  4. ~Matrix(void)
  5. void Set_Element(int Row_,int Column,const T& Value)
  6. T Get_Element(int Row ,int Column) const
  7. bool operator!() const;
  8. const Matrix<T> & operator=(const Matrix<T> &)
  9. const Matrix<T> & operator=(const T&)
  10. bool operator==(const Matrix<T> &) const
  11. bool operator==(const T & ) const
  12. bool operator!=(const Matrix<T> & second) const
  13. bool operator!=(const T & number)
  14. T &operator() (int Row,int Column)
  15. Matrix<T> operator+(const T &)
  16. Matrix<T> operator+(const Matrix<T> &)
  17. const Matrix<T> &operator+=(const T&)
  18. const Matrix<T> & operator+=(const Matrix<T> &)
  19. Matrix<T> operator-(const T&)
  20. Matrix<T> operator - (const Matrix<T> &)
  21. const Matrix<T> & operator -=(const T & )
  22. const Matrix<T> & operator -=(Matrix<T> & )
  23. Matrix<T> operator*(const T&)
  24. Matrix<T> operator* (const Matrix<T> &)
  25. const Matrix<T> & operator*=(const T&)
  26. const Matrix<T> & operator*=(const Matrix<T> &)
  27. void Transposed();>
  28. Matrix<T> GetTransposed() const
  29. bool Inverse();>
  30. Matrix<T> GetInversed() const
  31. double Det() const
  32. void Set_Size(int RowSize,int ColumnSize)
  33. const int& Get_ColumnSize() const
  34. const int & Get_RowSize() const
private:
  • int m_RowSize
  • int m_ColumnSize
  • T**m_Data

};

Notes


note:The returned argument of Inverse (a boolean ) Shows weather the matrix is Invertible & if it is ,inverts the Matrix


note:I have written this code in vc 2005 but it is not a managed code.if you compile it in vc6 you will see some errors that as an instance i is redeclared .thats because vc6 is alittle differant as when you declare i this way :for (int i=0;i<5;i++){ /*statements*/}

i is destroyed after for statement but in vc6 it is a declared variable even after for statement.but it is easy to change it to vc6 by debugging such errors in vc.you can do it yourself.


note:the only code i have not written my self is determinant because i have found a great one being written by Paul Bourke .to find the original code go to http://local.wasp.uwa.edu.au/~pbourke/other/determinant/

Some Technics that are used in Class

to announce one,I have to point to the way the element array (m_Data) is declared using new command:

consider the m_RowSize & m_ColumnSize are given

m_Data=new T* [m_RowSize];

for(int i=0;i<m_RowSize;i++)

m_ColumnSize=new T [m_ColumnSize];

HOPE YOU USE & ENJOY IT

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

shahin namini


just a Geomatics engineering student who loves programming & will denote his life this way.
Occupation: Other
Location: Iran, Islamic Republic Of Iran, Islamic Republic Of

Other popular C / C++ Language articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
Subject  Author Date 
QuestionI think there is a bug [modified]membersizheng20:54 13 Aug '07  
AnswerRe: I think there is a bugmembershahin namini22:09 18 Aug '07  
QuestionA problemmembercclever23:48 10 Apr '07  
AnswerRe: A problemmembershahin namini2:50 12 Apr '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 22 Feb 2007
Editor:
Copyright 2007 by shahin namini
Everything else Copyright © CodeProject, 1999-2008
Web08 | Advertise on the Code Project