|
I think you should change to: CFileException*e, and e->cause
|
|
|
|
|
This is the code i am having in visual c++ 6. Matrix.h & matrix.cpp are in current project folder and included in project.
#include <iostream>
#include "matrix.cpp"
void main()
{
CMatrix:: A();
}
But i am getting an error that
matrix.h(191) :error C2440: '=' : cannot convert from 'int' to 'const class CMatrix *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
Please tell me what to be done.
Thanks
Nagarajan
|
|
|
|
|
Now that probelem is correted by explicit type casting.. The program compiles without any error.
#include <iostream>
#include "matrix.cpp"
using namespace std;
void main()
{
CMatrix A(3,4);
}
But while linking i get 151 errors.
like
Matrix.obj : error LNK2005: "public: static class CObject * __stdcall CMatrix::CreateObject(void)" (?CreateObject@CMatrix@@SGPAVCObject@@XZ) already defined in mat.obj
Matrix.obj : error LNK2005: "public: virtual struct CRuntimeClass * __thiscall CMatrix::GetRuntimeClass(void)const " (?GetRuntimeClass@CMatrix@@UBEPAUCRuntimeClass@@XZ) already defined in mat.obj
Matrix.obj : error LNK2005: "class CArchive & __stdcall operator>>(class CArchive &,class CMatrix * &)" (??5@YGAAVCArchive@@AAV0@AAPAVCMatrix@@@Z) already defined in mat.obj
Matrix.obj : error LNK2005: "public: __thiscall CMatrix::CMatrix(void)" (??0CMatrix@@QAE@XZ) already defined in mat.obj
Matrix.obj : error LNK2005: "public: __thiscall CMatrix::CMatrix(class CMatrix const &)" (??0CMatrix@@QAE@ABV0@@Z) already defined in mat.obj
Matrix.obj : error LNK2005: "public: __thiscall CMatrix::CMatrix(int,int)" (??0CMatrix@@QAE@HH@Z) already defined in mat.obj
Matrix.obj : error LNK2005: "public: __thiscall CMatrix::CMatrix(int,bool)" (??0CMatrix@@QAE@H_N@Z) already defined in mat.obj
Matrix.obj : error LNK2005: "public: __thiscall CMatrix::CMatrix(struct tagVARIANT &)" (??0CMatrix@@QAE@AAUtagVARIANT@@@Z) already defined in mat.obj
Matrix.obj : error LNK2005: "public: virtual __thiscall CMatrix::~CMatrix(void)" (??1CMatrix@@UAE@XZ) already defined in mat.obj
Matrix.obj : error LNK2005: "public: virtual void __thiscall CMatrix::Dump(class CDumpContext &)const " (?Dump@CMatrix@@UBEXAAVCDumpContext@@@Z) already defined in mat.obj
Matrix.obj : error LNK2005: "public: virtual void __thiscall CMatrix::AssertValid(void)const " (?AssertValid@CMatrix@@UBEXXZ) already defined in mat.obj
Matrix.obj : error LNK2005: "public: virtual void __thiscall CMatrix::Serialize(class CArchive &)" (?Serialize@CMatrix@@UAEXAAVCArchive@@@Z) already defined in mat.obj
Matrix.obj : error LNK2005: "private: double * __thiscall CMatrix::AllocateMemory(int,int)" (?AllocateMemory@CMatrix@@AAEPANHH@Z) already defined in mat.obj
Matrix.obj : error LNK2005: "public: class CMatrix & __thiscall CMatrix::operator=(class CMatrix const &)" (??4CMatrix@@QAEAAV0@ABV0@@Z) already defined in mat.obj
Matrix.obj : error LNK2005: "public: bool __thiscall CMatrix::operator==(class CMatrix const &)const " (??8CMatrix@@QBE_NABV0@@Z) already defined in mat.obj
Matrix.obj : error LNK2005: "public: class CMatrix __thiscall CMatrix::operator+(class CMatrix const &)const " (??HCMatrix@@QBE?AV0@ABV0@@Z) already defined in mat.obj
Matrix.obj : error LNK2005: "public: class CMatrix __thiscall CMatrix::operator-(class CMatrix const &)const " (??GCMatrix@@QBE?AV0@ABV0@@Z) already defined in mat.obj
Matrix.obj : error LNK2005: "public: class CMatrix __thiscall CMatrix::operator*(class CMatrix const &)const " (??DCMatrix@@QBE?AV0@ABV0@@Z) already defined in mat.obj
Please help me...
thanks
|
|
|
|
|
CMatrix CMatrix::operator*(const CMatrix &other) const
{
// first check for a valid multiplication operation
if (m_NumColumns != other.m_NumRows)
throw "Matrices do not have common size" ;
// now that we know that the operation is possible
ASSERT(FALSE == IsBadReadPtr(other.m_pData, sizeof(double) * other.m_NumColumns * other.m_NumRows)) ;
// construct the object we are going to return
CMatrix result(other.m_NumColumns, m_NumRows);
// e.g.
// [A][B][C] [G][H] [A*G + B*I + C*K][A*H + B*J + C*L]
// [D][E][F] * [I][J] = [D*G + E*I + F*K][D*H + E*J + F*L]
// [K][L]
//
double value ;
for (int i = 0 ; i < result.m_NumColumns ; i++)
{
for (int j = 0 ; j < result.m_NumRows ; j++)
{
value = 0.0 ;
for (int k = 0 ; k < m_NumColumns ; k++)
{
value += GetElement(k,j) * other.GetElement(k,i) ;
}
result.SetElement(i, j, value) ;
}
}
return result ;
}
Jon Gould
|
|
|
|
|
Or even this.
CMatrix CMatrix::operator*(const CMatrix &other) const
{
// first check for a valid multiplication operation
if (m_NumColumns != other.m_NumRows)
throw "Matrices do not have common size" ;
// now that we know that the operation is possible
ASSERT(FALSE == IsBadReadPtr(other.m_pData, sizeof(double) * other.m_NumColumns * other.m_NumRows)) ;
// construct the object we are going to return
CMatrix result(other.m_NumColumns, m_NumRows);
// e.g.
// [A][B][C] [G][H] [A*G + B*I + C*K][A*H + B*J + C*L]
// [D][E][F] * [I][J] = [D*G + E*I + F*K][D*H + E*J + F*L]
// [K][L]
//
double value ;
for (int i = 0 ; i < result.m_NumColumns ; i++)
{
for (int j = 0 ; j < result.m_NumRows ; j++)
{
value = 0.0 ;
for (int k = 0 ; k < m_NumColumns ; k++)
{
value += GetElement(k,j) * other.GetElement(i,k) ;
}
result.SetElement(i, j, value) ;
}
}
return result ;
}
|
|
|
|
|
In allthrough his code, the "m_NumColumns" should be "m_NumRows" and vice versa, I believe. But it works if you leave these names be.
Bill^2
|
|
|
|
|
Hi,
I think I might be missing a file or two (?), because when I compile the source, it's missing "stdafx.h" and when I comment it out I get like 50 errors (even after commenting out the MFC stuff as suggested by the author for VC++6 use) so I'm pretty sure I need that file at least. Any ideas on where I can find the misssing file(s)?
-milan
|
|
|
|
|
One thing this class is missing is an algorithm to reduce a matrix to complete row-echelon form. Here is the algorithm from a matrix class
that I wrote. With a few very slight changes, it can be rolled into Mr. Allen's Matrix class. This algorithm can be used to to solve systems of linear equations stored in a matrix. It also calculates the Determinant. You can also use this algorithm to find the inverse of a matrix.
The Set & Get functions will have to be revised per Mr. Allen's corresponding functions. This algorithm uses get(row, col) and set(row, col, val). Mr. Allen's equivalent functions have col first and row second.
If you find this algorithm useful, thank Gauss!
// Reduce to complete row echelon form
// By James D. Reilly, Fort Wayne, Indiana
double Matrix::Reduce(void)
{
int B, C, X;
double Pivot, Temp;
double Determinant;
int PivotRow, PivotCol;
PivotRow = 0;
PivotCol = 0;
Determinant = 1.0;
do
{
// Find largest pivot. Search for a number below
// the current pivot with a greater absolute value.
X = PivotRow;
for(C=PivotRow; C<rows; C++)
if(fabs(Get(C, PivotCol))>fabs(Get(X, PivotCol)))
X = C;
if(X != PivotRow)
{
// If here, there is a better pivot choice somewhere
// below the current pivot.
// Interchange the pivot row with the row
// containing the largest pivot, X.
for(B=0; B<cols; B++)
{
Temp = Get(PivotRow, B);
Set(PivotRow, B, Get(X, B));
Set(X, B, Temp);
}
Determinant = -Determinant;
}
Pivot = Get(PivotRow, PivotCol);
Determinant = Pivot*Determinant;
if(Pivot!= 0.0)
{
// Introduce a '1' at the pivot point
for(B=0; B<cols; B++)
Set(PivotRow, B, Get(PivotRow, B)/Pivot);
for(B=0; B<rows; B++)
{
// Eliminate (make zero) all elements above
// and below the pivot.
// Skip over the pivot row when we come to it.
if(B != PivotRow)
{
Pivot = Get(B, PivotCol);
for(C=PivotRow; C<cols; C++)
Set(B, C, Get(B,C)-Pivot*Get(PivotRow, C));
}
}
PivotRow++; // Next row
}
PivotCol++; // Next column
}
while((PivotRow<rows)&&(PivotCol<cols)); // Reached an edge yet?
return Determinant;
}
J. Reilly
|
|
|
|
|
hi,
I am facing some problems when I tried to compile the provided source codes. Can anybody pls help me to settle the errors?
Compiling...
Matrix.cpp
c:\foo\myownmaths\matrix.h(191) : error C2440: '=' : cannot convert from 'int' to 'const class CMatrix *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
c:\foo\myownmaths\matrix.cpp(99) : error C2039: 'vt' : is not a member of 'tagVARIANT'
c:\program files\microsoft visual studio\vc98\include\oaidl.h(402) : see declaration of 'tagVARIANT'
c:\foo\myownmaths\matrix.cpp(101) : error C2039: 'vt' : is not a member of 'tagVARIANT'
c:\program files\microsoft visual studio\vc98\include\oaidl.h(402) : see declaration of 'tagVARIANT'
c:\foo\myownmaths\matrix.cpp(104) : error C2039: 'parray' : is not a member of 'tagVARIANT'
c:\program files\microsoft visual studio\vc98\include\oaidl.h(402) : see declaration of 'tagVARIANT'
c:\foo\myownmaths\matrix.cpp(948) : error C2039: 'vt' : is not a member of 'tagVARIANT'
c:\program files\microsoft visual studio\vc98\include\oaidl.h(402) : see declaration of 'tagVARIANT'
c:\foo\myownmaths\matrix.cpp(967) : error C2039: 'parray' : is not a member of 'tagVARIANT'
c:\program files\microsoft visual studio\vc98\include\oaidl.h(402) : see declaration of 'tagVARIANT'
Error executing cl.exe.
Matrix.obj - 6 error(s), 0 warning(s)
thank you very much
vincent sim
|
|
|
|
|
I think I can help reduce your error count from 6 to 5.
replace line matrix.h:191 with:
m_pMatrixConst = (const class Matrix *)other.m_Col ;
An explicit cast seems to be necessary here.
As for the other errors, I don't think I ran into those,
I hope someone else can help.
Jim
|
|
|
|
|
The line should rather be:
m_pMatrixConst = (const class CMatrix *)other.m_Col;
and not:
m_pMatrixConst = (const class Matrix *)other.m_Col;
The only change being CMatrix class and not Matrix. I got just 1 error which seems to be corrected using the code mentioned above.
D
|
|
|
|
|
I'm an absolute beginner in programming and have no experiance with MFC.
But I would like to use this class for an simple console application.
What do I have to change in this class?
Is it possible to use it without MFC?
Thx for helping.
Mel
|
|
|
|
|
I realize this was addressed below, but no specifics were
provided. Subsequently, here it is:
double* CMatrix::AllocateMemory(int nCols, int nRows)
{
ASSERT(nCols > 0) ;
ASSERT(nRows > 0) ;
double *pData = NULL ;
pData = new double[nCols * nRows + 1] ;
ASSERT(pData != NULL) ;
ASSERT(FALSE == IsBadReadPtr(pData, sizeof(double) * (nCols * nRows + 1))) ;
memset(pData, 0, sizeof(double) * (nCols * nRows + 1)) ;
return pData ;
}
In the code above, you're using new to allocate
memory for pData, but you return pData without
deleting it. I suggest making pData a member variable
of the CMatrix class (probably the easiest solution).
Cheers,
John Theal
Digital Immersion Software Corporation
|
|
|
|
|
I think it is no problem in this case. When return this pointer for pData of CMatrix, so that this pData of CMatrix will get this location in memory. If you delete it, you will delete pData of CMatrix. Thank!
|
|
|
|
|
Nice code.
Here's a version of the CMatrix class that does not need a helper class.
template<class T>
class CMatrix
{
public:
CMatrix(int row, int col):m_row(row),m_col(col), m_data((row!=0&&col!=0)?new T[row*col]:NULL){}
~CMatrix(){if(m_data) delete []m_data;}
T* operator[](int i) {return (m_data + (m_col*i));}
T* operator[](int i) const {return (m_data + (m_col*i));}
private:
int m_row;
int m_col;
T* m_data;
};
By removing the helper class you optimize the code, because it removes the need for creating a temporary object, and having to pass the temp object by value.
Top ten member of C++ Expert Exchange.
http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/
|
|
|
|
|
I am getting a warning from VC7 about a cast that is being done in side of the
void operator=(CMatrixHelper& other) function of CMatrixHelper. The following line generates it:
m_pMatrixConst = (CMatrix*) other.m_col;
I believe that is a typo??? probably should be:
m_pMatrixConst = other.m_pMatrixConst;
Of course I could be wrong as I am not exactly sure what is going on there...
Kevin
|
|
|
|
|
Ignore the (CMatrix*) type cast as I think I added that in at my first, quick attempt to get past a compiler warning/error...
But it probably still points to a typo anyway in the original code.
Kevin
|
|
|
|
|
You're library is almost exactly what I've been looking for. I actually extended another library for MFC use, but I'd rather use a true MFC library if possible. Do you have any plans on adding more functionality? Specifically, I'm looking to do eigenvalue decomposition, jacobi, and sortsv.
Or, does anyone know of another MFC Matrix library that includes these funcctions?
|
|
|
|
|
Hi,
I'm trying to multiply 2 matrices together : [10 76800] x [76800 1].
It's crashing at this point. Could it be the size of the matrix multiply?
Thanks
|
|
|
|
|
The way your multiplying them above wold produce a matrix of [10, 1] in size which would be OK. If its the otehr way around you would get a throw occuring in the operator*() function. It could also be that the matrix size of 76800*10 failed to allocate the memory as you would need (768000+1)*8 bytes of memory, which is a little over 6000K !
Roger Allen
Sonork 100.10016
In case you're worried about what's going to become of the younger generation, it's going to grow up and start worrying about the younger generation. - Roger Allen, but not me!
|
|
|
|
|
Hi Roger,
I checked with my own MATRIX class and I think your exception is wrong.
if (m_NumRows != other.m_NumColumns)
throw "Matrices do not have common size" ;
It should be:
if (m_NumColumns != other.m_NumRows)
throw "Matrices do not have common size" ;
6000K much for a computer with 256 MB?
regards,
Michel Wassink
We must make user friendly software. Where are friendly users?
|
|
|
|
|
Michel Wassink wrote:
I checked with my own MATRIX class and I think your exception is wrong.
Actually, it is more than just the exception. The entire multiple operation is inverted. It should be:
// first check for a valid multiplication operation
if (m_NumColumns != other.m_NumRows) //
throw "Matrices do not have common size" ;
// now that we know that the operation is possible
ASSERT(FALSE == IsBadReadPtr(other.m_pData, sizeof(double) * other.m_NumColumns * other.m_NumRows)) ;
// construct the object we are going to return
CMatrix result(other.m_NumColumns, m_NumRows) ; // - changed stb 05/05/03
// e.g.
// [A][B][C] [G][H][M][N] [A*G + B*I + C*K][A*H + B*J + C*L][etc][etc]
// [D][E][F] * [I][J][O][P] = [D*G + E*I + F*K][D*H + E*J + F*L][etc][etc]
// [K][L][Q][R]
//
double value ;
for (int i = 0 ; i < result.m_NumRows ; ++i)
{
for (int j = 0 ; j < result.m_NumColumns ; ++j)
{
value = 0.0 ;
for (int k = 0 ; k < m_NumColumns ; ++k)
{
value += GetElement(k, i) * other.GetElement(j, k) ;
}
result.SetElement(j, i, value) ;
}
}
return result ;
|
|
|
|
|
Scott1965 wrote:
Actually, it is more than just the exception. The entire multiple operation is inverted.
Yes you are right. I did not look any further to see if the func was ok. I hope Roger will update this. This is a serious bug. and there is always one more bug
regards,
Michel Wassink
We must make user friendly software. Where are friendly users?
|
|
|
|
|
I think It's is OK if you solve that follow.
For example, you solve AX=B
you can write that: X=B*A.GetInverted(); It will be OK But it's not normal in mathematic
|
|
|
|
|
hi there
i have a question
if i must do that operation :
K = P * inverse(H) * inv [H * P * inv(H) +R]
K (9,18)
P(18,18)
H can be (18,6) or (18,9)
R can be (9,9) or (6,6)
when executing it reports,as normal,error from operations * . Can i use the GetSquareMatrix function or ot's intended for another use?
Thanks
by
|
|
|
|
|