Как умножить матрицу на вектор(алгоритм)?

319
19 мая 2018, 12:30

Нужно написать перегруженные функции, которые будут умножать матрицу на матрицу, вектор на матрицу, матрицу на вектор, число на вектор и тд. Единственный две функции, которые у меня не получилось реализовать это умножение Вектора на Матрицу и Матрицу на Вектор.

Если Матрица * Вектор-Столбец -> Вектор-Столбец

Если Вектор-Столбец * Матрицу -> Вектор-Строка

НО, результат не вектор, а матрица. Что нужно изменить?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Matrix {
    protected int row, column;
    protected int[,] ARRAY;
    public int ROW {
        get { return row; }
        set { row = value; }
    }
    public int COLUMN {
        get { return column; }
        set { column = value; }
    }
    public Matrix() {
    }
    public Matrix(int row, int column) {
        this.row = row;
        this.column = column;
        ARRAY = new int[this.COLUMN, this.ROW];
    }
    public void EnterMatrix() {
        Console.Write("Enter the numbers of matrix columns: ");
        COLUMN = int.Parse(Console.ReadLine());
        Console.Write("Enter the numbers of matrix rows: ");
        ROW = int.Parse(Console.ReadLine());
        ARRAY = new int[COLUMN, ROW];
        for (int col = 0; col < COLUMN; col++) {
            for (int row = 0; row < ROW; row++) {
                Console.Write("Enter the elements of matrix cell[" + (col + 1) + ":" + (row + 1) + "]: ");
                ARRAY[col, row] = int.Parse(Console.ReadLine());
            }
        }
    }
    public void Display() {
        for (int col = 0; col < COLUMN; col++) {
            Console.WriteLine();
            for (int row = 0; row < ROW; row++) {
                Console.Write("{0}\t", ARRAY[col, row]);
            }
        }
        Console.WriteLine();
    }
    public Matrix ARRAYxARRAY(Matrix Matrix, int number) {
        Matrix tMatrix = new Matrix(Matrix.ROW, Matrix.COLUMN);
        for (int i = 0; i < Matrix.ROW; i++) {
            for (int j = 0; j < Matrix.COLUMN; j++) {
                tMatrix.ARRAY[i, j] = Matrix.ARRAY[i, j] * number;
                ARRAY[i, j] = tMatrix.ARRAY[i, j];
            }
        }
        return tMatrix;
    }
    public Matrix ARRAYxARRAY(int number, Matrix Matrix) {
        Matrix tMatrix = new Matrix(Matrix.ROW, Matrix.COLUMN);
        for (int i = 0; i < Matrix.ROW; i++) {
            for (int j = 0; j < Matrix.COLUMN; j++) {
                tMatrix.ARRAY[i, j] = number * Matrix.ARRAY[i, j];
                ARRAY[i, j] = tMatrix.ARRAY[i, j];
            }
        }
        return Matrix;
    }
    public Matrix ARRAYxARRAY(Matrix fMatrix, Matrix sMatrix) {
        Matrix tMatrix = new Matrix(fMatrix.ROW, sMatrix.COLUMN);
        if (fMatrix.COLUMN == sMatrix.ROW) {
            for (int i = 0; i < fMatrix.ROW; i++) {
                for (int j = 0; j < sMatrix.COLUMN; j++) {
                    for (int k = 0; k < fMatrix.COLUMN; k++) {
                        tMatrix.ARRAY[i, j] += fMatrix.ARRAY[i, k] * sMatrix.ARRAY[k, j];
                        ARRAY[i, j] = tMatrix.ARRAY[i, j];
                    }
                }
            }
        }
        else
        {
            Console.WriteLine("Matrixes can't be multiplied.");
        }
        return tMatrix;
    }
    public Vector ARRAYxARRAY(Matrix Matrix, Vector Vector) {
        Vector rVector = new Vector(Vector.ROW, 1);
        if (Vector.COLUMN == 1 && Vector.ROW == Matrix.COLUMN) {
            for (int i = 0; i < Matrix.COLUMN; i++) {
                for (int j = 0; j < Vector.ROW; j++) {
                    rVector.ARRAY[i, j] += Matrix.ARRAY[i, j] * Vector.ARRAY[i, j];
                }
            }
        }
        else {
            Console.WriteLine("Matrix and vector can't be multiplied.");
        }
        return rVector;
    }
    public Vector ARRAYxARRAY(Vector Vector, Matrix Matrix) {
        Vector rVector = new Vector(Vector.ROW, 1);
        if (Vector.COLUMN == 1 && Vector.ROW == Matrix.COLUMN) {
            for (int i = 0; i < Matrix.COLUMN; i++) {
                for (int j = 0; j < Vector.ROW; j++) {
                    rVector.ARRAY[i, j] += Vector.ARRAY[i, j] * Matrix.ARRAY[i, j];
                }
            }
        }
        else {
            Console.WriteLine("Vectors can't be multiplied.");
        }
        return rVector;
    }
    ~Matrix() {
        Console.WriteLine("Matrix has been denied.");
    }
}
class Vector : Matrix {
    public Vector() {
    }
    public Vector(int row, int column) {
        this.row = row;
        this.column = column;
        ARRAY = new int[this.COLUMN, this.ROW];
    }
    public void EnterVector() {
        Console.WriteLine("Choose type of vector: " +
                          "\nvector-row= [1]" +
                          "\nvector-col= [2]");
        int variant = int.Parse(Console.ReadLine());
        if (variant == 1) {
            COLUMN = 1;
            Console.Write("Enter the number of vector elements: ");
            ROW = int.Parse(Console.ReadLine());
        }
        else if (variant == 2) {
            ROW = 1;
            Console.Write("Enter the numbers of vector elements: ");
            COLUMN = int.Parse(Console.ReadLine());
        }
        ARRAY = new int[COLUMN, ROW];
        for (int col = 0; col < COLUMN; col++) {
            for (int row = 0; row < ROW; row++) {
                Console.Write("Enter the elements of matrix cell[" + (col + 1) + ":" + (row + 1) + "]: ");
                ARRAY[col, row] = int.Parse(Console.ReadLine());
            }
        }
    }
    public Vector ARRAYxARRAY(Vector Vector, int number) {
        Vector tVector = new Vector(Vector.ROW, Vector.COLUMN);
        for (int i = 0; i < Vector.ROW; i++) {
            for (int j = 0; j < Vector.COLUMN; j++) {
                tVector.ARRAY[i, j] = Vector.ARRAY[i, j] * number;
                ARRAY[i, j] = tVector.ARRAY[i, j];
            }
        }
        return tVector;
    }
    public Vector ARRAYxARRAY(int number, Vector Vector) {
        Vector tVector = new Vector(Vector.ROW, Vector.COLUMN);
        for (int i = 0; i < Vector.ROW; i++) {
            for (int j = 0; j < Vector.COLUMN; j++) {
                tVector.ARRAY[i, j] = number * Vector.ARRAY[i, j];
                ARRAY[i, j] = tVector.ARRAY[i, j];
            }
        }
        return tVector;
    }
    ~Vector() {
        Console.WriteLine("Vector has been denied.");
    }
}
class Program {
    static void Main() {
        Matrix MATRIX = new Matrix();
               MATRIX.EnterMatrix();
        Console.WriteLine("The matrix is: ");
               MATRIX.Display();
        Vector VECTOR = new Vector();
               VECTOR.EnterVector();
        Console.WriteLine("The matrix is: ");
               VECTOR.Display();
        while (true) {
            Console.Write("Select anoperation:\n"
                + "\t1. matrix * matrix;\n"
                + "\t2. matrix * vector;\n" 
                + "\t3. matrix * number;\n"
                + "\t4. number * matrix;\n"
                + "\t5. vector * matrix;\n"
                + "\t6. vector * number;\n"
                + "\t7. number * vector;\n");
            int choice = int.Parse(Console.ReadLine());
            switch (choice) {
                case 1:
                    MATRIX.ARRAYxARRAY(MATRIX, MATRIX);
                    MATRIX.Display();
                    break;
                case 2:
                    VECTOR.ARRAYxARRAY(MATRIX, VECTOR);
                    VECTOR.Display();
                    break;
                case 3:
                    Console.Write("Enter the number to multiply by: ");
                    int number = int.Parse(Console.ReadLine());
                    MATRIX.ARRAYxARRAY(MATRIX, number);
                    MATRIX.Display();
                    break;
                case 4:
                    Console.Write("Enter the number to multiply by: ");
                    number = int.Parse(Console.ReadLine());
                    MATRIX.ARRAYxARRAY(number, MATRIX);
                    MATRIX.Display();
                    break;
                case 5:
                    MATRIX.ARRAYxARRAY(VECTOR, MATRIX);
                    MATRIX.Display();
                    break;
                case 6:
                    Console.Write("Enter the number to multiply by: ");
                    number = int.Parse(Console.ReadLine());
                    VECTOR.ARRAYxARRAY(VECTOR, number);
                    VECTOR.Display();
                    break;
                case 7:
                    Console.Write("Enter the number to multiply by: ");
                    number = int.Parse(Console.ReadLine());
                    VECTOR.ARRAYxARRAY(number, VECTOR);
                    VECTOR.Display();
                    break;
            }
        }
    }
}
READ ALSO
Цвет пикселя по заданным координатам

Цвет пикселя по заданным координатам

Как можно получить цвет пикселя по координатам x и y ?

210
Биндинг TextBox к одному string в WindowsForms

Биндинг TextBox к одному string в WindowsForms

Мне нужно привязать TextBox к одному stringНужно чтобы при изменении TextBox строка менялась и наоборот

238
Двойной словарь

Двойной словарь

Подскажите пожалуйста, как заполнить такой словарь?

250
List in List C# ASP.Core

List in List C# ASP.Core

Подскажите как правельно вложить List в модель с присвоением значений из модели - для формирования JsonResult в MasterDetail

244