“Multiplication matricielle” Réponses codées

Multiplication matricielle

/*
 * This C++ program can multiply any two square or rectangular matrices.
 * The below program multiplies two square matrices of size 4 * 4.
 * There is also an example of a rectangular matrix for the same code (commented below).
 * We can change the Matrix value with the number of rows and columns (from MACROs) for Matrix-1
 * and Matrix-2 for different dimensions.
 */
 
/*
 * Note:  i- The number of columns in Matrix-1 must be equal to the number of rows in Matrix-2.
 *       ii- Output of multiplication of Matrix-1 and Matrix-2, results with equal to the number
 *           of rows of Matrix-1 and the number of columns of Matrix-2 i.e. rslt[R1][C2].
 */
 
#include <iostream>
 
using namespace std;
 
// Edit MACROs here, according to your Matrix Dimensions for mat1[R1][C1] and mat2[R2][C2]
#define R1 4            // number of rows in Matrix-1
#define C1 4            // number of columns in Matrix-1
#define R2 4            // number of rows in Matrix-2
#define C2 4            // number of columns in Matrix-2
 
void mulMat(int mat1[][C1], int mat2[][C2]) {
    int rslt[R1][C2];
 
    cout << "Multiplication of given two matrices is:\n" << endl;
 
    for (int i = 0; i < R1; i++) {
        for (int j = 0; j < C2; j++) {
            rslt[i][j] = 0;
 
            for (int k = 0; k < R2; k++) {
                rslt[i][j] += mat1[i][k] * mat2[k][j];
            }
 
            cout << rslt[i][j] << "\t";
        }
 
        cout << endl;
    }
}
 
int main(void) {
    // Square Matrices
    // R1 = 4, C1 = 4 and R2 = 4, C2 = 4 (Update these values in MACROs)
    int mat1[R1][C1] = {
            {1, 1, 1, 1},
            {2, 2, 2, 2},
            {3, 3, 3, 3},
            {4, 4, 4, 4}
    };
 
    int mat2[R2][C2] = {
            {1, 1, 1, 1},
            {2, 2, 2, 2},
            {3, 3, 3, 3},
            {4, 4, 4, 4}
    };
 
    /*
    // Rectangular Matrices
    // R1 = 3, C1 = 4 and R2 = 4, C2 = 3 (Update these values in MACROs)
    int mat1[R1][C1] = {
                {1, 1, 1, 1},
                {2, 2, 2, 2},
                {3, 3, 3, 3}
    };
 
    int mat2[R2][C2] = {
                {1, 1, 1},
                {2, 2, 2},
                {3, 3, 3},
                {4, 4, 4}
    };
    */
 
    if (C1 != R2) {
        cout << "The number of columns in Matrix-1  must be equal to the number of rows in "
                "Matrix-2" << endl;
        cout << "Please update MACROs according to your array dimension in #define section"
                << endl;
 
        exit(EXIT_FAILURE);
    }
 
    mulMat(mat1, mat2);
 
    return 0;
}
 
// This code is contributed by Manish Kumar (mkumar2789)
Thoughtful Tamarin

Multiplication matricielle

import java.util.Arrays;
import java.util.Scanner;
public class Matrix {

    private static int counter =0;
    private static Scanner scanner = new Scanner(System.in);
    private int rawForArr1 , columnforarr1Andrawsforarr2, columnForArr2 =0; //

    public Matrix(){
    }


    public Matrix(int rawForArr1, int columnforarr1Andrawsforarr2, int columnForArr2) {
        this.rawForArr1 = rawForArr1;
        this.columnforarr1Andrawsforarr2 = columnforarr1Andrawsforarr2;
        this.columnForArr2 = columnForArr2;
    }

    public void setRawsAndColumn(){
        setRawsAndColumns();
    }

    private void setRawsAndColumns(){
        System.out.println("Enter numbers of raws for the (LEFT ARRAY)");
        int r1=scanner.nextInt();
        this.rawForArr1 =r1;
        System.out.println("\nEnter numbers of columns for the (LEFT ARRAY).\nThis value automatically will be assigned for the raws in the right matrix ensuring 'Valid Multiplication'\n");
        int colArr1RawArr2 = scanner.nextInt();
        this.columnforarr1Andrawsforarr2 =colArr1RawArr2;
        System.out.println("Enter numbers of columns for the (RIGHT ARRAY)");
        int c2=scanner.nextInt();
        this.columnForArr2 = c2;
        double [][] firstArr = new double[r1][colArr1RawArr2];
        double [][] secondArr = new double[colArr1RawArr2][c2];
        enterElements(firstArr);
        enterElements(secondArr);
        double[][] multiplied =multiplieMatrix(firstArr,secondArr);
        printValue(multiplied);
    }


    // Print the Matrix as a string of Array
    public static void printValue(double[][] arr)
    {
                System.out.println(Arrays.deepToString(arr));
    }

    public static double [][] multiplieMatrix(double [][] leftArr,double[][] rightArr){
        int m = leftArr.length;
        int n = rightArr[0].length;
        double [][] sumOfMultiplie = new double[m][n];
        int lARawIndex =0,rACIndex=0,iterateOnLArrColAndRArrRaw=0;
        double sum = 0 ;
        for(lARawIndex =0 ; lARawIndex < leftArr.length ; ++lARawIndex)
        {
            for(rACIndex = 0 ; rACIndex < rightArr[0].length ; ++rACIndex)
            {
                for(iterateOnLArrColAndRArrRaw = 0 ; iterateOnLArrColAndRArrRaw < rightArr.length ; ++iterateOnLArrColAndRArrRaw)
                {
                    sum+= (leftArr[lARawIndex][iterateOnLArrColAndRArrRaw]) * (rightArr[iterateOnLArrColAndRArrRaw][rACIndex]);
                }
                sumOfMultiplie[lARawIndex][rACIndex]=sum ;
                sum=0;
            }
        }
        return sumOfMultiplie ;
    }
    /* enter elements of the array*/
    public static double[][] enterElements(double[][] arr)
    {
        counter++;
        if(counter%2==0){
            System.out.println("\t***(Enter the elements for the RIGHT array)***\n");
        }
        System.out.println("\t ***(Enter the elements for the LEFT array)***\n");
        for(int i = 0 ; i < arr.length ; ++i) // numbers of raws
        {
            for(int j = 0 ; j<arr[0].length ; ++j) // numbers of columns
            {
                System.out.println("Enter element in raw #"+(i+1)+" column #"+(j+1));
                arr[i][j] = scanner.nextDouble();
            }
        }
        return arr ;

    }

//
//    public static  double[][] recursiveMultiplicationMatrix(double [][] multilplies ){
//        double[][] nextMatrix =initilizeMatrix();
//        enterElements(nextMatrix);
//        int m = nextMatrix.length;
//        int n = multilplies[0].length;
//        double [][] sumOfMultiplie = new double[m][n];
//        int lARawIndex =0,rACIndex=0,iterateOnLArrColAndRArrRaw=0;
//        int sum = 0 ;
//        for(lARawIndex =0 ; lARawIndex < nextMatrix.length ; ++lARawIndex)
//        {
//            for(rACIndex = 0 ; rACIndex < multilplies[0].length ; ++rACIndex)
//            {
//                for(iterateOnLArrColAndRArrRaw = 0 ; iterateOnLArrColAndRArrRaw < multilplies.length ; ++iterateOnLArrColAndRArrRaw)
//                {
//                    sum+= (nextMatrix[lARawIndex][iterateOnLArrColAndRArrRaw]) * (multilplies[iterateOnLArrColAndRArrRaw][rACIndex]);
//                }
//                sumOfMultiplie[lARawIndex][rACIndex]=sum ;
//                sum=0;
//            }
//        }
//
//            String answer = answer();
//            if (answer.equals("yes")) {
//                return recursiveMultiplicationMatrix(sumOfMultiplie);
//
//            }
//        return sumOfMultiplie ;
//
//    }
//
//    public static String answer(){
//        System.out.println("Do you want to continue to multiple? "+'\n'+"enter yes to continue or any other key to print the 2d matrix");
//        String answer ;
//        answer = scanner.nextLine();
//        return answer;
//    }



}
Mero

Réponses similaires à “Multiplication matricielle”

Questions similaires à “Multiplication matricielle”

Plus de réponses similaires à “Multiplication matricielle” dans Java

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code