Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
137 views
in Technique[技术] by (71.8m points)

matrix - Trying to reload an operator in c++, but it doesn't seem to work

Problem: I have overloaded operators * and *= with the same solution, though using operator *= doesn't seem to change the contents of the Matrix, maybe I am declaring the operator overload method incorrectly.

At the same time, operator * works properly and actually multiplies Matrix, I have checked it beforehand.

Output:

3 4 -5 
8 0 7 
8 9 -4 

8 7 7 
-6 0 6 
2 2 9 

3 4 -5 
8 0 7 
8 9 -4 

Here is the code itself:

struct WrappedMatrix{
        int n;
        int ** Matrix;
    };

    struct WrappedVector{
        int n;
        int * Vector;
    };

    WrappedVector linearizedMatrix(WrappedMatrix matrix){
        WrappedVector vector;
        vector.n = matrix.n * matrix.n;
        vector.Vector = new int[vector.n];
        for(int i = 0; i < matrix.n; i++){
            for(int j = 0; j < matrix.n; j++){
                 int k = j + (int) (i*sqrt(vector.n));
                 vector.Vector[k] = matrix.Matrix[i][j];
            }
        }
        return vector;
    }

    WrappedMatrix normalMatrix(WrappedVector vector){
        WrappedMatrix matrix;
        matrix.n = sqrt(vector.n);
        matrix.Matrix = new int * [matrix.n];
        for(int i = 0; i < matrix.n; i++){
            matrix.Matrix[i] = new int[matrix.n];
            for(int j = 0; j < matrix.n; j++){
                int k = j + (int) (i*sqrt(vector.n));
                matrix.Matrix[i][j] = vector.Vector[k];
            }
        }
        return matrix;
    }

    WrappedVector operator*(const WrappedVector& vector1, const WrappedVector& vector2) {
        if(vector1.n != vector2.n) {
            cout << "Матриц? р?зних розм?р?в!" << endl;
            return vector1;
        }
        WrappedMatrix matrix1 = normalMatrix(vector1);
        WrappedMatrix matrix2 = normalMatrix(vector2);
        WrappedMatrix result;
        result.n = matrix1.n;
        result.Matrix = new int * [result.n];
        for(int i = 0; i < result.n; i++){
            result.Matrix[i] = new int[result.n];
        }
        for(int i = 0; i < result.n; i++){
            for(int j = 0; j < result.n; j++){
                for(int k = 0; k < result.n; k++){
                    int p1 = matrix1.Matrix[i][k];
                    int p2 = matrix2.Matrix[k][j];
                    result.Matrix[i][j] += p1 * p2;
                }
            }
        }
        WrappedVector resultV = linearizedMatrix(result);
        return resultV;
    }

    //?
    WrappedVector operator*=(const WrappedVector& vector1, const WrappedVector& vector2) {
        if(vector1.n != vector2.n) {
            cout << "Матриц? р?зних розм?р?в!" << endl;
            return vector1;
        }
        WrappedMatrix matrix1 = normalMatrix(vector1);
        WrappedMatrix matrix2 = normalMatrix(vector2);
        WrappedMatrix result;
        result.n = matrix1.n;
        result.Matrix = new int * [result.n];
        for(int i = 0; i < result.n; i++){
            result.Matrix[i] = new int[result.n];
        }
        for(int i = 0; i < result.n; i++){
            for(int j = 0; j < result.n; j++){
                for(int k = 0; k < result.n; k++){
                    int p1 = matrix1.Matrix[i][k];
                    int p2 = matrix2.Matrix[k][j];
                    result.Matrix[i][j] += p1 * p2;
                }
            }
        }
        WrappedVector resultV = linearizedMatrix(result);
        return resultV;
    }


    int main() {

        WrappedMatrix matrix;
        matrix.n = 3;
        matrix.Matrix = new int * [matrix.n];
        matrix.Matrix[0] = new int[matrix.n];
        matrix.Matrix[1] = new int[matrix.n];
        matrix.Matrix[2] = new int[matrix.n];
        matrix.Matrix[0][0] = 3;
         matrix.Matrix[0][1] = 4;
         matrix.Matrix[0][2] = -5;
        matrix.Matrix[1][0] = 8;
         matrix.Matrix[1][1] = 0;
         matrix.Matrix[1][2] = 7;
         matrix.Matrix[2][0] = 8;
         matrix.Matrix[2][1] = 9;
         matrix.Matrix[2][2] = -4;
        WrappedVector vector = linearizedMatrix(matrix);

        cout << vector << endl;

        WrappedMatrix matrix1;
        matrix1.n = 3;
        matrix1.Matrix = new int * [matrix1.n];
        matrix1.Matrix[0] = new int[matrix1.n];
        matrix1.Matrix[1] = new int[matrix1.n];
        matrix1.Matrix[2] = new int[matrix1.n];
        matrix1.Matrix[0][0] = 8;
        matrix1.Matrix[0][1] = 7;
        matrix1.Matrix[0][2] = 7;
        matrix1.Matrix[1][0] = -6;
        matrix1.Matrix[1][1] = 0;
        matrix1.Matrix[1][2] = 6;
        matrix1.Matrix[2][0] = 2;
        matrix1.Matrix[2][1] = 2;
        matrix1.Matrix[2][2] = 9;
        WrappedVector vector1 = linearizedMatrix(matrix1);

        cout << vector1 << endl;

        vector *= vector1;

        cout << vector;


        return 0;
    }

Thank you in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This isn't an answer technically, I just revamped the code thus far, I'll add onto it later tonight if I have time. I just managed to put something together this morning, figured you might as well have a look so it doesn't just lay around doing nothing:

#include <iostream>
#include <vector>
#include <cstdarg>

class matrix{
    public:
        matrix(){};
        matrix(std::initializer_list<std::vector<int>> vectors):x(vectors){}
        ~matrix(){};

        const int& size(){ return this->x.size(); } //# of vectors
        //ALT: ex. print: 3x3, 4x5, 7x3
        //void size(){ std::cout<<"Dim: "<<this->x.size<<"x"<<this->x.front().size()<<std::endl;
        void add(const std::vector<int>& arr){ this->x.push_back(arr); }
        const std::vector<std::vector<int>>& extract(){ return this->x; } //Returns entire matrix
        const std::vector<int>& getVector(const int& row){ return this->x.at(row); } //Returns specific vector from matrix

        matrix operator*(const matrix& m){ //More params
            //Stuff here
        }
        matrix operator*=(const matrix& m){ //More params
            //Stuff here
        }

    private:
        std::vector<std::vector<int>> x;
};

//Can break the center out to make one for vectors too
std::ostream& operator<<(std::ostream& os, matrix& m){
    for (auto& it:m.extract()){
        for (auto& jt:it){
            os<<jt<<" ";
        }
        os<<std::endl;
    }
    os<<std::endl;
    return os;
}


int main(){
    matrix m({{2,5,8,11,14},
              {3,6,9,12,15},
              {4,7,10,13,16}});
    std::cout<<m;

    return 0;
}

This is, from what I gather, what the comments were about. Much easier to use in my opinion, though it may be something other than what you need if you are demanded to make structs and access vectors in some particular way.

Also, I didn't figure out what mathematical result you had in mind there, so I didn't put anything into the operators, I might add some crossproduct or dotproduct example later on, if that's what you were doing.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...