Hyluss

Mnozenie macierzy Matrix multiplication

Mar 4th, 2017
621
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.59 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package mnozeniemaceirzy;
  7.  
  8.  
  9.  
  10. import static java.lang.System.*;
  11. import java.util.Scanner;
  12. import java.lang.ArrayIndexOutOfBoundsException;
  13. /**
  14.  *
  15.  * @author Hylus
  16.  */
  17. public class Mnozeniemaceirzy {
  18.  
  19.     /**
  20.      * @param args the command line arguments
  21.      */
  22.     public static void main(String[] args) {
  23.        
  24.         //Scanner input = new Scanner (System.in);
  25.         double[][] mac1;
  26.         double[][] mac2;
  27.         double[][] mac3=null;      
  28.         out.printf ("Wprowadz pierwszy macierz:");
  29.         mac1 = Wprowadz();
  30.         out.printf ("Wprowadz 2 macierz:");
  31.         mac2 = Wprowadz();      
  32.         try
  33.         {
  34.             mac3 = PomnozMacierz (mac1,mac2);      
  35.         }
  36.         catch (Exception e)
  37.         {
  38.             out.printf ("macierze maja inny rozmair\n");
  39.         }
  40.         WyswietlMacierz (mac3);      
  41.         try
  42.         {
  43.             out.printf ("Transponuje macierz\n");
  44.             mac3 = TransponujMacierz (mac3);
  45.         }
  46.         catch (ArrayIndexOutOfBoundsException e)
  47.         {
  48.             out.printf ("Ta macierz nie istnieje\n");
  49.         }
  50.         WyswietlMacierz (mac3);
  51.     }
  52.    
  53.    
  54.     public static double[][] Wprowadz()
  55.     {
  56.         double[][] mac;
  57.         int x=0, y=0;
  58.         Scanner input = new Scanner (System.in);
  59.         out.printf ("Podaj rozmiar macierzu (y i x) \n");
  60.         x = input.nextInt();
  61.         y = input.nextInt();
  62.        
  63.         mac = new double [x][y];
  64.        
  65.         out.printf ("Wprowadz dane:\n");
  66.         for (int i=0; i<x; i++)
  67.         {
  68.             for (int j=0; j<y ; j++)
  69.             {                                
  70.                 out.printf ("[%d][%d] - ",i,j);
  71.                 mac[i][j] = input.nextDouble();
  72.             }        
  73.         }
  74.  
  75.         return mac;      
  76.     }
  77.    
  78.     public static double[][] PomnozMacierz( double[][] mac1,double[][] mac2)
  79.     {
  80.         double[][] mac3;
  81.         mac3 = new double [0][0];
  82.  
  83.         if (mac1.length == mac2[0].length)
  84.         {
  85.             mac3 = new double [mac1.length][mac2[0].length];
  86.  
  87.             for (int y=0 ; y < mac1.length; y++)
  88.             {
  89.                 for (int x=0; x < mac2[0].length; x++)
  90.                 {
  91.                     for (int i=0; i < mac1[0].length; i++)
  92.                     {
  93.                         mac3[y][x] = mac3[y][x] + mac1[y][i] * mac2[i][x];
  94.                     }
  95.                 }
  96.             }
  97.         }
  98.         else
  99.         {
  100.             out.printf ("macierze nie sa tych samych rozmairow\n");
  101.         }
  102.        
  103.         return mac3;
  104.     }
  105.    
  106.     public static void WyswietlMacierz (double[][] mac)
  107.     {        
  108.         out.printf ("Wyswietlam wynik mnozenia macierzy\n");
  109.         for (int i=0; i<mac.length; i++)
  110.         {
  111.             for (int j=0; j<mac[0].length ; j++)
  112.             {                
  113.                 out.printf ("%.2f ",mac[i][j]);
  114.             }
  115.             out.printf ("\n");
  116.         }
  117.     }
  118.    
  119.     public static double[][] TransponujMacierz (double[][] macierz)
  120.     {
  121.         double[][] macwyjsciowa;
  122.         macwyjsciowa = new double[macierz[0].length][macierz.length];
  123.         for (int y=0; y< macierz[0].length; y++)
  124.         {
  125.             for (int x=0; x < macierz.length; x++ )
  126.             {
  127.                 macwyjsciowa[y][x] = macierz[x][y];
  128.             }            
  129.         }            
  130.         return macwyjsciowa;
  131.     }
  132.    
  133.    
  134. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment