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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • erynn
    New Member
    • Aug 2010
    • 2

    #1

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

    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,dou ble **,double **,double **);
    void MatInverse(doub le **,double **);
    void MatMultiply(dou ble **,double **,double **);
    };

    void MatAlgebra::Rea dData(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::Mat Add(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::Mat Multiply(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::Mat Inverse(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(14 5) : error C2065: 'M' : undeclared identifier
  • newb16
    Contributor
    • Jul 2008
    • 687

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

    Comment

    • erynn
      New Member
      • Aug 2010
      • 2

      #3
      declare m

      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 **.

      Comment

      • newb16
        Contributor
        • Jul 2008
        • 687

        #4
        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.

        Comment

        Working...