最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java實(shí)現(xiàn)任意矩陣Strassen算法

 更新時(shí)間:2016年02月10日 21:52:02   作者:SamYjy  
這篇文章主要介紹了java實(shí)現(xiàn)任意矩陣Strassen算法的相關(guān)資料,需要的朋友可以參考下

本例輸入為兩個(gè)任意尺寸的矩陣m * n, n * m,輸出為兩個(gè)矩陣的乘積。計(jì)算任意尺寸矩陣相乘時(shí),使用了Strassen算法。程序?yàn)樽跃?,?jīng)過測試,請放心使用?;舅惴ㄊ牵?br /> 1.對于方陣(正方形矩陣),找到最大的l, 使得l = 2 ^ k, k為整數(shù)并且l < m。邊長為l的方形矩陣則采用Strassen算法,其余部分以及方形矩陣中遺漏的部分用蠻力法。
2.對于非方陣,依照行列相應(yīng)添加0使其成為方陣。
StrassenMethodTest.java

package matrixalgorithm;
 
import java.util.Scanner;
 
public class StrassenMethodTest {
 
  private StrassenMethod strassenMultiply;
   
   StrassenMethodTest(){
    strassenMultiply = new StrassenMethod();
  }//end cons
 
   public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    System.out.println("Input row size of the first matrix: ");
    int arow = input.nextInt();
    System.out.println("Input column size of the first matrix: ");
    int acol = input.nextInt();
    System.out.println("Input row size of the second matrix: ");
    int brow = input.nextInt();
    System.out.println("Input column size of the second matrix: ");
    int bcol = input.nextInt();
 
    double[][] A = new double[arow][acol];
    double[][] B = new double[brow][bcol];
    double[][] C = new double[arow][bcol];
    System.out.println("Input data for matrix A: ");
     
    /*In all of the codes later in this project,
    r means row while c means column.
    */
    for (int r = 0; r < arow; r++) {
      for (int c = 0; c < acol; c++) {
        System.out.printf("Data of A[%d][%d]: ", r, c);
        A[r][c] = input.nextDouble();
      }//end inner loop
    }//end loop
 
    System.out.println("Input data for matrix B: ");
    for (int r = 0; r < brow; r++) {
      for (int c = 0; c < bcol; c++) {
        System.out.printf("Data of A[%d][%d]: ", r, c);
        B[r][c] = input.nextDouble();
      }//end inner loop
    }//end loop
 
    StrassenMethodTest algorithm = new StrassenMethodTest();
    C = algorithm.multiplyRectMatrix(A, B, arow, acol, brow, bcol);
 
    //Display the calculation result:
    System.out.println("Result from matrix C: ");
    for (int r = 0; r < arow; r++) {
      for (int c = 0; c < bcol; c++) {
        System.out.printf("Data of C[%d][%d]: %f\n", r, c, C[r][c]);
      }//end inner loop
    }//end outter loop
   }//end main
   
  //Deal with matrices that are not square:
  public double[][] multiplyRectMatrix(double[][] A, double[][] B,
      int arow, int acol, int brow, int bcol) {
    if (arow != bcol) //Invalid multiplicatio
      return new double[][]{{0}};
    
    double[][] C = new double[arow][bcol];
 
    if (arow < acol) {
       
      double[][] newA = new double[acol][acol];
      double[][] newB = new double[brow][brow];
 
      int n = acol;
       
      for (int r = 0; r < acol; r++) 
        for (int c = 0; c < acol; c++) 
          newA[r][c] = 0.0;
 
      for (int r = 0; r < brow; r++) 
        for (int c = 0; c < brow; c++) 
          newB[r][c] = 0.0;
 
      for (int r = 0; r < arow; r++) 
        for (int c = 0; c < acol; c++) 
          newA[r][c] = A[r][c];
 
      for (int r = 0; r < brow; r++) 
        for (int c = 0; c < bcol; c++) 
          newB[r][c] = B[r][c];
       
      double[][] C2 = multiplySquareMatrix(newA, newB, n);
      for(int r = 0; r < arow; r++)
        for(int c = 0; c < bcol; c++)
          C[r][c] = C2[r][c];
    }//end if
     
    else if(arow == acol)
      C = multiplySquareMatrix(A, B, arow);
       
    else {
      int n = arow;
      double[][] newA = new double[arow][arow];
      double[][] newB = new double[bcol][bcol];
 
      for (int r = 0; r < arow; r++) 
        for (int c = 0; c < arow; c++) 
          newA[r][c] = 0.0;
 
      for (int r = 0; r < bcol; r++) 
        for (int c = 0; c < bcol; c++) 
          newB[r][c] = 0.0;
 
 
      for (int r = 0; r < arow; r++) 
        for (int c = 0; c < acol; c++) 
          newA[r][c] = A[r][c];
 
      for (int r = 0; r < brow; r++)
        for (int c = 0; c < bcol; c++) 
          newB[r][c] = B[r][c];
 
      double[][] C2 = multiplySquareMatrix(newA, newB, n);
      for(int r = 0; r < arow; r++)
        for(int c = 0; c < bcol; c++)
          C[r][c] = C2[r][c];
    }//end else
       
     return C;
   }//end method
   
  //Deal with matrices that are square matrices. 
   public double[][] multiplySquareMatrix(double[][] A2, double[][] B2, int n){
      
     double[][] C2 = new double[n][n];
    
     for(int r = 0; r < n; r++)
       for(int c = 0; c < n; c++)
         C2[r][c] = 0;
     
     if(n == 1){
      C2[0][0] = A2[0][0] * B2[0][0];
      return C2;
     }//end if
          
     int exp2k = 2;
     
     while(exp2k <= (n / 2) ){
       exp2k *= 2;
     }//end loop
     
     if(exp2k == n){
       C2 = strassenMultiply.strassenMultiplyMatrix(A2, B2, n);
       return C2;
     }//end else
     
     //The "biggest" strassen matrix:
     double[][][] A = new double[6][exp2k][exp2k];
     double[][][] B = new double[6][exp2k][exp2k];
     double[][][] C = new double[6][exp2k][exp2k];
     
     for(int r = 0; r < exp2k; r++){
       for(int c = 0; c < exp2k; c++){
         A[0][r][c] = A2[r][c];
         B[0][r][c] = B2[r][c];
       }//end inner loop
     }//end outter loop
     
    C[0] = strassenMultiply.strassenMultiplyMatrix(A[0], B[0], exp2k);
     
    for(int r = 0; r < exp2k; r++)
      for(int c = 0; c < exp2k; c++)
        C2[r][c] = C[0][r][c];
     
    int middle = exp2k / 2;
     
    for(int r = 0; r < middle; r++){
      for(int c = exp2k; c < n; c++){
        A[1][r][c - exp2k] = A2[r][c];
        B[3][r][c - exp2k] = B2[r][c];
      }//end inner loop     
    }//end outter loop
     
    for(int r = exp2k; r < n; r++){
      for(int c = 0; c < middle; c++){
        A[3][r - exp2k][c] = A2[r][c];
        B[1][r - exp2k][c] = B2[r][c];
      }//end inner loop     
    }//end outter loop
     
    for(int r = middle; r < exp2k; r++){
      for(int c = exp2k; c < n; c++){
        A[2][r - middle][c - exp2k] = A2[r][c];
        B[4][r - middle][c - exp2k] = B2[r][c];
      }//end inner loop     
    }//end outter loop
     
    for(int r = exp2k; r < n; r++){
      for(int c = middle; c < n - exp2k + 1; c++){
        A[4][r - exp2k][c - middle] = A2[r][c];
        B[2][r - exp2k][c - middle] = B2[r][c];     
      }//end inner loop     
    }//end outter loop
    
    for(int i = 1; i <= 4; i++)
      C[i] = multiplyRectMatrix(A[i], B[i], middle, A[i].length, A[i].length, middle);
     
    /*
    Calculate the final results of grids in the "biggest 2^k square,
    according to the rules of matrice multiplication.
    */
    for (int row = 0; row < exp2k; row++) {
       for (int col = 0; col < exp2k; col++) {
         for (int k = exp2k; k < n; k++) {
           C2[row][col] += A2[row][k] * B2[k][col];
         }//end loop
       }//end inner loop
     }//end outter loop
     
    //Use brute force to solve the rest, will be improved later:
    for(int col = exp2k; col < n; col++){
      for(int row = 0; row < n; row++){
        for(int k = 0; k < n; k++)
          C2[row][col] += A2[row][k] * B2[k][row];
      }//end inner loop
    }//end outter loop
     
    for(int row = exp2k; row < n; row++){
      for(int col = 0; col < exp2k; col++){
        for(int k = 0; k < n; k++)
          C2[row][col] += A2[row][k] * B2[k][row];
      }//end inner loop
    }//end outter loop   
     
    return C2;
   }//end method
   
}//end class

StrassenMethod.java

package matrixalgorithm;
 
import java.util.Scanner;
 
public class StrassenMethod {
 
  private double[][][][] A = new double[2][2][][];
  private double[][][][] B = new double[2][2][][];
  private double[][][][] C = new double[2][2][][];
 
  /*//Codes for testing this class:
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Input size of the matrix: ");
    int n = input.nextInt();
 
    double[][] A = new double[n][n];
    double[][] B = new double[n][n];
    double[][] C = new double[n][n];
    System.out.println("Input data for matrix A: ");
    for (int r = 0; r < n; r++) {
      for (int c = 0; c < n; c++) {
        System.out.printf("Data of A[%d][%d]: ", r, c);
        A[r][c] = input.nextDouble();
      }//end inner loop
    }//end loop
 
    System.out.println("Input data for matrix B: ");
    for (int r = 0; r < n; r++) {
      for (int c = 0; c < n; c++) {
        System.out.printf("Data of A[%d][%d]: ", r, c);
        B[r][c] = input.nextDouble();
      }//end inner loop
    }//end loop
 
    StrassenMethod algorithm = new StrassenMethod();
    C = algorithm.strassenMultiplyMatrix(A, B, n);
 
    System.out.println("Result from matrix C: ");
    for (int r = 0; r < n; r++) {
      for (int c = 0; c < n; c++) {
        System.out.printf("Data of C[%d][%d]: %f\n", r, c, C[r][c]);
      }//end inner loop
    }//end outter loop
 
  }//end main*/
   
   public double[][] strassenMultiplyMatrix(double[][] A2, double B2[][], int n){
    double[][] C2 = new double[n][n];
    //Initialize the matrix:
    for(int rowIndex = 0; rowIndex < n; rowIndex++)
      for(int colIndex = 0; colIndex < n; colIndex++)
        C2[rowIndex][colIndex] = 0.0;
 
    if(n == 1)
      C2[0][0] = A2[0][0] * B2[0][0];
    //"Slice matrices into 2 * 2 parts: 
    else{
      double[][][][] A = new double[2][2][n / 2][n / 2];
      double[][][][] B = new double[2][2][n / 2][n / 2];
      double[][][][] C = new double[2][2][n / 2][n / 2];
       
      for(int r = 0; r < n / 2; r++){
        for(int c = 0; c < n / 2; c++){          
          A[0][0][r][c] = A2[r][c];
          A[0][1][r][c] = A2[r][n / 2 + c];
          A[1][0][r][c] = A2[n / 2 + r][c];
          A[1][1][r][c] = A2[n / 2 + r][n / 2 + c];
           
          B[0][0][r][c] = B2[r][c];
          B[0][1][r][c] = B2[r][n / 2 + c];
          B[1][0][r][c] = B2[n / 2 + r][c];
          B[1][1][r][c] = B2[n / 2 + r][n / 2 + c];
        }//end loop
      }//end loop
       
      n = n / 2;
       
      double[][][] S = new double[10][n][n];
      S[0] = minusMatrix(B[0][1], B[1][1], n);
      S[1] = addMatrix(A[0][0], A[0][1], n);
      S[2] = addMatrix(A[1][0], A[1][1], n);
      S[3] = minusMatrix(B[1][0], B[0][0], n);
      S[4] = addMatrix(A[0][0], A[1][1], n);
      S[5] = addMatrix(B[0][0], B[1][1], n);
      S[6] = minusMatrix(A[0][1], A[1][1], n);
      S[7] = addMatrix(B[1][0], B[1][1], n);
      S[8] = minusMatrix(A[0][0], A[1][0], n);
      S[9] = addMatrix(B[0][0], B[0][1], n);
       
      double[][][] P = new double[7][n][n];
      P[0] = strassenMultiplyMatrix(A[0][0], S[0], n);
      P[1] = strassenMultiplyMatrix(S[1], B[1][1], n);
      P[2] = strassenMultiplyMatrix(S[2], B[0][0], n);
      P[3] = strassenMultiplyMatrix(A[1][1], S[3], n);
      P[4] = strassenMultiplyMatrix(S[4], S[5], n);
      P[5] = strassenMultiplyMatrix(S[6], S[7], n);
      P[6] = strassenMultiplyMatrix(S[8], S[9], n);
       
      C[0][0] = addMatrix(minusMatrix(addMatrix(P[4], P[3], n), P[1], n), P[5], n);
      C[0][1] = addMatrix(P[0], P[1], n);
      C[1][0] = addMatrix(P[2], P[3], n);
      C[1][1] = minusMatrix(minusMatrix(addMatrix(P[4], P[0], n), P[2], n), P[6], n);
       
      n *= 2;
       
       for(int r = 0; r < n / 2; r++){
        for(int c = 0; c < n / 2; c++){
          C2[r][c] = C[0][0][r][c];
          C2[r][n / 2 + c] = C[0][1][r][c];
          C2[n / 2 + r][c] = C[1][0][r][c];
          C2[n / 2 + r][n / 2 + c] = C[1][1][r][c];
        }//end inner loop
      }//end outter loop
    }//end else     
 
    return C2;
  }//end method
   
   //Add two matrices according to matrix addition.
   private double[][] addMatrix(double[][] A, double[][] B, int n){
    double C[][] = new double[n][n];
     
    for(int r = 0; r < n; r++)
      for(int c = 0; c < n; c++)
        C[r][c] = A[r][c] + B[r][c];
     
    return C;
  }//end method 
   
   //Substract two matrices according to matrix addition.
   private double[][] minusMatrix(double[][] A, double[][] B, int n){
    double C[][] = new double[n][n];
     
    for(int r = 0; r < n; r++)
      for(int c = 0; c < n; c++)
        C[r][c] = A[r][c] - B[r][c];
     
    return C;
  }//end method
   
}//end class

希望本文所述對大家學(xué)習(xí)java程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Spring?Boot整合log4j2日志配置的詳細(xì)教程

    Spring?Boot整合log4j2日志配置的詳細(xì)教程

    這篇文章主要介紹了SpringBoot項(xiàng)目中整合Log4j2日志框架的步驟和配置,包括常用日志框架的比較、配置參數(shù)介紹、Log4j2配置詳解以及使用步驟,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-02-02
  • java工程師進(jìn)階之MyBatis延遲加載的使用

    java工程師進(jìn)階之MyBatis延遲加載的使用

    本文是java工程師進(jìn)階篇,主要介紹了java應(yīng)用開發(fā)中MyBatis延遲加載及如何使用,有需要的朋友 可以借鑒參考下,希望能夠有所幫助
    2021-09-09
  • Java HttpClient用法的示例詳解

    Java HttpClient用法的示例詳解

    Java開發(fā)語言中實(shí)現(xiàn)HTTP請求的方法主要有兩種:一種是JAVA的標(biāo)準(zhǔn)類HttpUrlConnection;另一種是第三方開源框架HTTPClient。本文就將詳細(xì)講講Java中HttpClient的使用,需要的可以參考一下
    2022-07-07
  • Java后端接口中提取請求頭中的Cookie和Token的方法

    Java后端接口中提取請求頭中的Cookie和Token的方法

    在現(xiàn)代 Web 開發(fā)中,HTTP 請求頭(Header)是客戶端與服務(wù)器之間傳遞信息的重要方式之一,本文將詳細(xì)介紹如何在 Java 后端(以 Spring Boot 為例)中提取請求頭中的 Cookie 和 Token,并提供完整的代碼示例和優(yōu)化建議,需要的朋友可以參考下
    2025-01-01
  • Java分布式session存儲解決方案圖解

    Java分布式session存儲解決方案圖解

    這篇文章主要介紹了Java分布式session存儲解決方案圖解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • java生成二維碼并且給二維碼添加logo

    java生成二維碼并且給二維碼添加logo

    這篇文章主要介紹了java生成二維碼并且給二維碼添加logo的實(shí)例代碼,代碼簡單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-12-12
  • Spring boot自定義http反饋狀態(tài)碼詳解

    Spring boot自定義http反饋狀態(tài)碼詳解

    這篇文章主要給大家介紹了Spring boot自定義http反饋狀態(tài)碼的相關(guān)資料,文中介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。
    2017-06-06
  • SpringBoot結(jié)合mockito測試實(shí)戰(zhàn)

    SpringBoot結(jié)合mockito測試實(shí)戰(zhàn)

    與集成測試將系統(tǒng)作為一個(gè)整體測試不同,單元測試更應(yīng)該專注于某個(gè)類。所以當(dāng)被測試類與外部類有依賴的時(shí)候,尤其是與數(shù)據(jù)庫相關(guān)的這種費(fèi)時(shí)且有狀態(tài)的類,很難做單元測試。但好在可以通過“Mockito”這種仿真框架來模擬這些比較費(fèi)時(shí)的類,從而專注于測試某個(gè)類內(nèi)部的邏輯
    2022-11-11
  • Spring Boot中使用AOP統(tǒng)一處理web層異常的方法

    Spring Boot中使用AOP統(tǒng)一處理web層異常的方法

    這篇文章主要介紹了Spring Boot中使用AOP統(tǒng)一處理web層異常的相關(guān)資料,需要的朋友可以參考下
    2018-03-03
  • SpringBoot中請求參數(shù)綁定及使用詳解

    SpringBoot中請求參數(shù)綁定及使用詳解

    這篇文章主要介紹了SpringBoot中請求參數(shù)綁定及使用詳解,在Web應(yīng)用程序中,請求參數(shù)綁定是非常重要的操作,Spring?Boot框架使得請求參數(shù)綁定變得非常簡單,通過使用注解和預(yù)定義的類可以輕松地實(shí)現(xiàn)此操作,需要的朋友可以參考下
    2023-08-08

最新評論

曲阜市| 辽阳县| 沐川县| 亳州市| 呼图壁县| 朝阳市| 湖北省| 大理市| 永昌县| 麦盖提县| 延安市| 潮州市| 玛曲县| 阳西县| 西城区| 包头市| 广河县| 紫阳县| 平邑县| 南靖县| 株洲县| 晋江市| 克拉玛依市| 道孚县| 黑河市| 扎囊县| 休宁县| 彭山县| 辽阳县| 彭泽县| 施秉县| 收藏| 清涧县| 富裕县| 青州市| 高雄市| 秦皇岛市| 孝昌县| 江城| 南汇区| 井研县|