矩阵*向量乘法(随机数)

时间:2018-05-20 12:51:01

标签: c++ matrix-multiplication

正如标题所说,我想在矩阵和向量之间创建一个乘法,它们都填充了随机数(double类型)。这是我用来生成它们的函数:

double random(double min, double max) {

    double number = (double)rand() / RAND_MAX;
    return min + number * (max - min);
}

我还编写了Vec,RandomVec和RandomMat类,我可以毫无问题地创建它们:

" VEC"和" RandomVector"类

struct Vec {

double* v;
unsigned int size;

    Vec()
        : v(new double[4]), size(4) {

        memset(v, 0, size * 4);
    }

    Vec(const unsigned int size)
        : v(new double[size]), size(size) {

        memset(v, 0, size * 4);
    }

    Vec(double input[]) 
        : v(input), size(sizeof(input) / sizeof(double)) {

    }

    void set(const unsigned int row, const double value) {

        v[row] = value;
    }

    void print() {

        for (unsigned int i = 0; i < size; i++) {

            std::cout << v[i] << std::endl;
        }
    }

    ~Vec() {

        delete[] v;
    }
};

struct RandomVec {

    double* v;
    unsigned int size;

    RandomVec() 
        : v(new double[4]), size(4) {

        for (unsigned int i = 0; i < size; i++) {

            v[i] = random(0, 1);
        }
    }

    RandomVec(const unsigned int size) 
        : v(new double[size]), size(size) {

        for (unsigned int i = 0; i < size; i++) {

            v[i] = random(0, 1);
        }
    }

    RandomVec(const unsigned int size, const double min, const double max)
        : v(new double[size]), size(size) {

        for (unsigned int i = 0; i < size; i++) {

            v[i] = random(min, max);
        }
    }

    void set(const unsigned int row, const double value) {

        v[row] = value;
    }

    void print() {

        for (unsigned int i = 0; i < size; i++) {

            std::cout << v[i] << std::endl;
        }
    }

    ~RandomVec() {

        delete[] v;
    }
};

&#34; RandomMat&#34;

struct RandomMat {

    double* m;
    unsigned int rows;
    unsigned int columns;

    RandomMat()
        : m(new double[4 * 4]), rows(4), columns(4) {

        for (unsigned int i = 0; i < rows * columns; i++) {

            m[i] = random(0, 1);
        }
    }

    RandomMat(const unsigned int rows, const unsigned int columns) 
        : m(new double[rows * columns]), rows(rows), columns(columns) {

        for (unsigned int i = 0; i < rows * columns; i++) {

            m[i] = random(0, 1);
        }
    }

    RandomMat(const unsigned int rows, const unsigned int columns, const double min, const double max)
        : m(new double[rows * columns]), rows(rows), columns(columns) {

        for (unsigned int i = 0; i < rows * columns; i++) {

            m[i] = random(min, max);
        }
    }

    void print() {

        for (unsigned int i = 0; i < rows * columns; i++) {

            std::cout << m[i] << std::endl;
        }
    }

    ~RandomMat() {

        delete[] m;
    }
};

现在,当我尝试将这些函数与具有以下函数的对象相乘时,程序崩溃了:

Vec operator*(RandomMat& RandomMat, RandomVec& RandomVec) {

    Vec Result;

    for (unsigned int row = 0; row < RandomVec.size; row++) {

        double sum = 0;

        for (unsigned int column = 0; column < RandomVec.size; column++) {

            sum += RandomMat.m[row * RandomVec.size + column] * RandomVec.v[column];
        }

        Result.set(row, sum);
    }

    return Result;
}

我真的不知道为什么会这样,希望你能帮我解决这个问题。 &GT;。&LT;此外,我对编程还是一个新手,所以我真的很感激你有任何改进我的代码的提示(性能明智吗?)。 :)

0 个答案:

没有答案