By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,677 Members | 1,070 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,677 IT Pros & Developers. It's quick & easy.

modify code 4k.cpp for Z=(inverse,A)*(B*B)+3*(A*A)*(inverse,B).

P: 2
how to declare to multiply a matrix by a constant value 3?

the value of matrix given is
A:
5 -1 6
2 7 -1
4 3 -5

B:
3 7 -2
-4 1 9
6 1 -1

/
Code4K.cpp
#include <fstream.h>
#include <iostream.h>
#define N 3

class MatAlgebra
{
public:
MatAlgebra() { }
~MatAlgebra() { }
void ReadData(double **,double **);
void MatAdd(bool,double **,double **,double **);
void MatInverse(double **,double **);
void MatMultiply(double **,double **,double **);
};

void MatAlgebra::ReadData(double **a,double **b)
{
int i,j;
ifstream InFile("Assg2b.in");
cout << "Matrix A:" << endl;
for (i=1;i<=N;i++)
{
for (j=1;j<=N;j++)
{
InFile >> a[i][j];
cout << a[i][j] << " ";
}
cout << endl;
}
cout << endl << "Matrix B:" << endl;
for (i=1;i<=N;i++)
{
for (j=1;j<=N;j++)
{
InFile >> b[i][j];
cout << b[i][j] << " ";
}
cout << endl;
}
InFile.close();
}

void MatAlgebra::MatAdd(bool flag,double **c,double **a,double **b)
{
int i,j;
for (i=1;i<=N;i++)
for (j=1;j<=N;j++)
c[i][j]=((flag)?a[i][j]+b[i][j]:a[i][j]-b[i][j]);
}

void MatAlgebra::MatMultiply(double **c,double **a,double **b)
{
int i,j,k;
for (i=1;i<=N;i++)
for (j=1;j<=N;j++)
{
c[i][j]=0;
for (k=1;k<=N;k++)
c[i][j] += a[i][k]*b[k][j];
}
}

void MatAlgebra::MatInverse(double **x,double **a)
{
int i,j,k;
double Sum,m;
double **b,**q;
b=new double *[N+1];
q=new double *[N+1];
for (i=0;i<=N;i++)
{
b[i]=new double [N+1];
q[i]=new double [N+1];
}
for (i=1;i<=N;i++)
for (j=1;j<=N;j++)
{
b[i][j]=0;
q[i][j]=a[i][j];
if (i==j)
b[i][j]=1;
}
// Perform row operations
for (k=1;k<=N-1;k++)
for (i=k+1;i<=N;i++)
{
m=q[i][k]/q[k][k];
for (j=1;j<=N;j++)
{
q[i][j]-=m*q[k][j];
b[i][j]-=m*b[k][j];
}
}
// Perform backward substitutions
for (i=N;i>=1;i--)
for (j=1;j<=N;j++)
{
Sum=0;
x[i][j]=0;
for (k=i+1;k<=N;k++)
Sum += q[i][k]*x[k][j];
x[i][j]=(b[i][j]-Sum)/q[i][i];
}
for (i=0;i<=N;i++)
delete b[i],q[i];
delete b,q;
}

void main()
{
int i,j;
double **A,**B;
double **P,**Q,**R,**S,**T,**U,**V,**W;
MatAlgebra g;
A=new double *[N+1];
B=new double *[N+1];
P=new double *[N+1];
Q=new double *[N+1];
R=new double *[N+1];
S=new double *[N+1];
T=new double *[N+1];
U=new double *[N+1];
V=new double *[N+1];
W=new double *[N+1];
for (i=0;i<=N;i++)
{
A[i]=new double [N+1];
B[i]=new double [N+1];
P[i]=new double [N+1];
Q[i]=new double [N+1];
R[i]=new double [N+1];
S[i]=new double [N+1];
T[i]=new double [N+1];
U[i]=new double [N+1];
V[i]=new double [N+1];
W[i]=new double [N+1];
}
cout.setf(ios::fixed);
cout.precision(12);
g.ReadData(A,B);
g.MatInverse(P,A);
g.MatMultiply(Q,B,B);
g.MatMultiply(R,A,A);
g.MatMultiply(T,M,R);
g.MatInverse(S,B);
g.MatMultiply(U,P,Q);
g.MatMultiply(V,T,S);
g.MatAdd(1,W,U,V);



cout << endl << "Results as Matrix W:" << endl;
for (i=1;i<=N;i++)
{
for (j=1;j<=N;j++)
cout << W[i][j] << " ";
cout << endl;
}
delete A,B,P,Q,R,S,T,U,V,W;
}

the error is D:\2b\2b.cpp(145) : error C2065: 'M' : undeclared identifier
Aug 13 '10 #1
Share this Question
Share on Google+
3 Replies


100+
P: 687
You declare A,B then **P,**Q,**R,**S,**T,**U,**V,**W; I don't see M here or later.
Aug 13 '10 #2

P: 2
actually i want to declare M=3.But seems it will ask me to convert from int to **double. So i delete it. So how to convert from int to double **.
Aug 14 '10 #3

100+
P: 687
If you declare M as a double value, you can't use it as matrix in the call to MatMultiply(). You can use E*3 matrix and multiply by it or add the method to multiply by scalar value.
Aug 16 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.