コード例 #1
0
    public void multiply(Matrix multipliedMatrix)
    {
        if (multipliedMatrix.getLength(0) != matrix.GetLength(1))
        {
            throw new IndexOutOfRangeException();
        }

        Matrix newMatrix = new Matrix(matrix.GetLength(0), multipliedMatrix.getLength(1));

        for (int row = 0; row < matrix.GetLength(0); row++)
        {
            for (int col = 0; col < multipliedMatrix.getLength(1); col++)
            {
                int result = 0;
                for (int multipliedRow = 0; multipliedRow < matrix.GetLength(1); multipliedRow++)
                {
                    result += matrix[row, multipliedRow] * multipliedMatrix.get(multipliedRow, col);
                }

                newMatrix.set(row, col, result);
            }
        }

        matrix = newMatrix.getMatrix();
    }
コード例 #2
0
    public void add(Matrix addedMatrix)
    {
        if (addedMatrix.getLength(0) != matrix.GetLength(0) &&
            addedMatrix.getLength(1) != matrix.GetLength(1))
        {
            throw new IndexOutOfRangeException();
        }

        for (int row = 0; row < matrix.GetLength(0); row++)
        {
            for (int col = 0; col < matrix.GetLength(1); col++)
            {
                matrix[row, col] += addedMatrix.get(row, col);
            }
        }
    }
コード例 #3
0
    static void Main()
    {
        Matrix matrix = new Matrix(4, 4);

        for (int row = 0; row < matrix.getLength(0); row++)
        {
            for (int col = 0; col < matrix.getLength(1); col++)
            {
                matrix.set(row, col, randGenerator.Next(10));
            }
        }

        Matrix secondMatrix = new Matrix(4, 4);

        for (int row = 0; row < secondMatrix.getLength(0); row++)
        {
            for (int col = 0; col < secondMatrix.getLength(1); col++)
            {
                secondMatrix.set(row, col, randGenerator.Next(10));
            }
        }

        Console.WriteLine("First: ");
        Console.WriteLine(matrix.toString());
        Console.WriteLine(new string('-', 20));
        Console.WriteLine("Second:");
        Console.WriteLine(secondMatrix.toString());
        Console.WriteLine(new string('-', 20));

        matrix.add(secondMatrix);
        Console.WriteLine("Added: ");
        Console.WriteLine(matrix.toString());
        Console.WriteLine(new string('-', 20));
        
        matrix.subtract(secondMatrix);
        Console.WriteLine("Subtracted: ");
        Console.WriteLine(matrix.toString());
        Console.WriteLine(new string('-', 20));

        matrix.multiply(secondMatrix);
        Console.WriteLine("Multiplied: ");
        Console.WriteLine(matrix.toString());
        Console.WriteLine(new string('-', 20));
    }