473,414 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

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

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
3 1617
newb16
687 512MB
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
erynn
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
newb16
687 512MB
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

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

Similar topics

13
by: python | last post by:
hello and thanks for reading this, i have been a dos/windows user using some form of the basic language for 30 years now. i own and run a small programming company and there is one feature that...
2
by: jdph40 | last post by:
In Access 97, I have a form named frmEmpList with a list box that contains the names of all our employees. I have a command button with the following code in the OnClick event so the form will...
2
by: Baldy | last post by:
Hi All is it possible to modify code at run time? I have a set of constants that change at deployment (from development to deployment server and a few other consts) I have a menu item that...
4
by: William | last post by:
After much frustration I was able to update my data store via code only. Using the data adapter was the only way I was able to set up all the objects written in my code. Basically, I cheated by...
0
by: richardkreidl | last post by:
I have the following hash script that I use to compare two text files. 'Class Public Class FileComparison Public Class FileComparisonException Public Enum ExceptionType U 'Unknown A 'Add...
1
by: Krishna | last post by:
In vb2005 I can't modify code in debug mode why? Is it possible to set? Thanks
8
by: Jesse Aldridge | last post by:
I've got a module that I use regularly. I want to make some extensive changes to this module but I want all of the programs that depend on the module to keep working while I'm making my changes. ...
1
by: G Love | last post by:
Hi, I am trying to modify some code in a toolbar I have for generating random 'spam' entries on my forum. I have managed to extract all the relevant files from the firefox .xpi file and the .jar...
10
by: timleonard | last post by:
How can the following code to be modified to include a duration Column or "Named Range"? I would like to modify it so that a task that has a duration for example, three to five days would show...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.