Home c++ Multiplication of the matrix to the vector. C++

Multiplication of the matrix to the vector. C++

Author

Date

Category

created class matrix and class vector. How to implement a multiplication function on each other?

class matrix
  {
  Public:
    INT ** ARR;
    int rows;
    int columns;
  Public:
    Matrix () {};
    Matrix (int rows, int columns)
    {
      this- & gt; rows = rows;
      this- & gt; columns = columns;
      arr = new int * [rows];
      for (int i = 0; i & lt; rows; i ++)
      {
        arr [i] = new int [columns];
      }
      for (int i = 0; i & lt; rows; i ++)
      {
        For (int j = 0; j & lt; columns; j ++)
        {
          Arr [i] [j] = 0;
        }
      }
    }
  };
Class Vector
  {
  Private:
    int * vec;
    int size;
  Public:
    Friend Matrix;
    Vector (Int Size)
    {
      This- & gt; Size = Size;
      Vec = New int [Size];
      for (int i = 0; i & lt; size; i ++)
      {
        VEC [i] = 0;
      }
    }
};

Answer 1, Authority 100%

Such an action is possible if there is some matrix Matrix [M, N] and some vector VEKTOR [N], the number of elements of which is equal to the number of lines of the matrix. As a result, we obtain the vector new_vektor [n], the first element of which is the amount of the works of the first string of the Matrix [M, n] array of the Matrix [N] array elements, the second element is the amount of the elements of the second line of the MATRIX array [m, n] on the elements Array Vektor [n], etc.

Not knowing what kind of classes are for the matrix and vector, I can only offer a solution for arrays:

void matrmultiply (int n, int m, float * matrix, float * vektor, float * res) // if integer values ​​are needed, can be replaced by int
{
 for (int i = 0; i & lt; n; i ++)
 {
  Float temp = 0;
  for (int j = 0; j & lt; m; j ++)
  {
   TEMP + = MATRIX [I * M + J] * VEKTOR [J];
  }
  res [i] = temp;
 }
}}

And then implement a similar solution for your classes.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions