算法导论第4章strassen算法JAVA实现

2024-06-02 16:38

本文主要是介绍算法导论第4章strassen算法JAVA实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 

今天看了strassen算法,用java实现了一下。

另外题目4.2-3,如何修改Strassen算法,使之适应矩阵规模n不是2的幂的情况?

答:添加额外的行或列使之成为2的幂的方阵,添加的行或列均为0即可。

 

文章中提到在分解矩阵时使用复杂度为θ(1)的下标运算,本人为了方便,是采用拷贝赋值的方式进行的矩阵分解。

 

package answers.chapter04;import java.util.Arrays;public class MatrixMultiply {public static void SquareMatrixMultiply(int A[][], int B[][]) {int rows = A.length;int C[][] = new int[rows][rows];for (int i = 0; i < rows; i++) {for (int j = 0; j < rows; j++) {C[i][j] = 0;for (int k = 0; k < rows; k++) {C[i][j] += A[i][k] * B[k][j];}}}displaySquare(C);}public static void displaySquare(int matrix[][]) {for (int i = 0; i < matrix.length; i++) {for (int j : matrix[i]) {System.out.print(j+" ");}System.out.println();}}public static void copyToMatrixArray(int srcMatrix[][], int startI, int startJ, int iLen, int jLen, int destMatrix[][]) {for (int i = startI; i < startI + iLen; i++) {for (int j = startJ; j < startJ + jLen; j++) {destMatrix[i - startI][j - startJ] = srcMatrix[i][j]; }}}public static void copyFromMatrixArray(int destMatrix[][], int startI, int startJ, int iLen, int jLen, int srcMatrix[][]) {for (int i = 0; i < iLen; i++) {for (int j = 0; j < jLen; j++) {destMatrix[startI + i][startJ + j] = srcMatrix[i][j]; }}}public static void squareMatrixAdd(int A[][], int B[][], int C[][]) {for (int i = 0; i < A.length; i++) {for (int j = 0; j < A[i].length; j++) {C[i][j] = A[i][j] + B[i][j];}}}public static void squareMatrixSub(int A[][], int B[][], int C[][]) {for (int i = 0; i < A.length; i++) {for (int j = 0; j < A[i].length; j++) {C[i][j] = A[i][j] - B[i][j];}}}public static int[][] squareMatrixMultiplyRecursive(int A[][], int B[][]) {int n = A.length;int C[][] = new int[n][n];if (n == 1) {C[0][0] = A[0][0] * B[0][0];} else {int A11[][], A12[][], A21[][], A22[][];int B11[][], B12[][], B21[][], B22[][];int C11[][], C12[][], C21[][], C22[][];A11 = new int[n/2][n/2];A12 = new int[n/2][n/2];A21 = new int[n/2][n/2];A22 = new int[n/2][n/2];copyToMatrixArray(A, 0, 0, n/2, n/2, A11);copyToMatrixArray(A, 0, n/2, n/2, n/2, A12);copyToMatrixArray(A, n/2, 0, n/2, n/2, A21);copyToMatrixArray(A, n/2, n/2, n/2, n/2, A22);B11 = new int[n/2][n/2];B12 = new int[n/2][n/2];B21 = new int[n/2][n/2];B22 = new int[n/2][n/2];copyToMatrixArray(B, 0, 0, n/2, n/2, B11);copyToMatrixArray(B, 0, n/2, n/2, n/2, B12);copyToMatrixArray(B, n/2, 0, n/2, n/2, B21);copyToMatrixArray(B, n/2, n/2, n/2, n/2, B22);C11 = new int[n/2][n/2];C12 = new int[n/2][n/2];C21 = new int[n/2][n/2];C22 = new int[n/2][n/2];squareMatrixAdd(squareMatrixMultiplyRecursive(A11, B11), squareMatrixMultiplyRecursive(A12, B21),C11);squareMatrixAdd(squareMatrixMultiplyRecursive(A11, B12), squareMatrixMultiplyRecursive(A12, B22),C12);squareMatrixAdd(squareMatrixMultiplyRecursive(A21, B11), squareMatrixMultiplyRecursive(A22, B21),C21);squareMatrixAdd(squareMatrixMultiplyRecursive(A21, B12), squareMatrixMultiplyRecursive(A22, B22),C22);copyFromMatrixArray(C, 0, 0, n/2, n/2, C11);copyFromMatrixArray(C, 0, n/2, n/2, n/2, C12);copyFromMatrixArray(C, n/2, 0, n/2, n/2, C21);copyFromMatrixArray(C, n/2, n/2, n/2, n/2, C22);}return C;}public static int[][] strassenMatrixMultiplyRecursive(int A[][], int B[][]) {int n = A.length;int C[][] = new int[n][n];if (n == 1) {C[0][0] = A[0][0] * B[0][0];} else {int A11[][], A12[][], A21[][], A22[][];int B11[][], B12[][], B21[][], B22[][];int C11[][], C12[][], C21[][], C22[][];int S1[][], S2[][], S3[][], S4[][], S5[][], S6[][], S7[][], S8[][], S9[][], S10[][];int P1[][], P2[][], P3[][], P4[][], P5[][], P6[][], P7[][];A11 = new int[n/2][n/2];A12 = new int[n/2][n/2];A21 = new int[n/2][n/2];A22 = new int[n/2][n/2];copyToMatrixArray(A, 0, 0, n/2, n/2, A11);copyToMatrixArray(A, 0, n/2, n/2, n/2, A12);copyToMatrixArray(A, n/2, 0, n/2, n/2, A21);copyToMatrixArray(A, n/2, n/2, n/2, n/2, A22);B11 = new int[n/2][n/2];B12 = new int[n/2][n/2];B21 = new int[n/2][n/2];B22 = new int[n/2][n/2];copyToMatrixArray(B, 0, 0, n/2, n/2, B11);copyToMatrixArray(B, 0, n/2, n/2, n/2, B12);copyToMatrixArray(B, n/2, 0, n/2, n/2, B21);copyToMatrixArray(B, n/2, n/2, n/2, n/2, B22);S1 = new int[n/2][n/2];S2 = new int[n/2][n/2];S3 = new int[n/2][n/2];S4 = new int[n/2][n/2];S5 = new int[n/2][n/2];S6 = new int[n/2][n/2];S7 = new int[n/2][n/2];S8 = new int[n/2][n/2];S9 = new int[n/2][n/2];S10 = new int[n/2][n/2];squareMatrixSub(B12, B22, S1);squareMatrixAdd(A11, A12, S2);squareMatrixAdd(A21, A22, S3);squareMatrixSub(B21, B11, S4);squareMatrixAdd(A11, A22, S5);squareMatrixAdd(B11, B22, S6);squareMatrixSub(A12, A22, S7);squareMatrixAdd(B21, B22, S8);squareMatrixSub(A11, A21, S9);squareMatrixAdd(B11, B12, S10);P1 = new int[n/2][n/2];P2 = new int[n/2][n/2];P3 = new int[n/2][n/2];P4 = new int[n/2][n/2];P5 = new int[n/2][n/2];P6 = new int[n/2][n/2];P7 = new int[n/2][n/2];P1 = strassenMatrixMultiplyRecursive(A11, S1);P2 = strassenMatrixMultiplyRecursive(S2, B22);P3 = strassenMatrixMultiplyRecursive(S3, B11);P4 = strassenMatrixMultiplyRecursive(A22, S4);P5 = strassenMatrixMultiplyRecursive(S5, S6);P6 = strassenMatrixMultiplyRecursive(S7, S8);P7 = strassenMatrixMultiplyRecursive(S9, S10);C11 = new int[n/2][n/2];C12 = new int[n/2][n/2];C21 = new int[n/2][n/2];C22 = new int[n/2][n/2];int temp[][] = new int[n/2][n/2];squareMatrixAdd(P5, P4, temp);squareMatrixSub(temp, P2, temp);squareMatrixAdd(temp, P6, C11);squareMatrixAdd(P1, P2, C12);squareMatrixAdd(P3, P4, C21);squareMatrixAdd(P5, P1, temp);squareMatrixSub(temp, P3, temp);squareMatrixSub(temp, P7, C22);copyFromMatrixArray(C, 0, 0, n/2, n/2, C11);copyFromMatrixArray(C, 0, n/2, n/2, n/2, C12);copyFromMatrixArray(C, n/2, 0, n/2, n/2, C21);copyFromMatrixArray(C, n/2, n/2, n/2, n/2, C22);}return C;}public static int sMatrixA[][] = new int[][] {{1, 2, 3, 4, 5, 6, 7, 8},{1, 2, 3, 4, 5, 6, 7, 8},{1, 2, 3, 4, 5, 6, 7, 8},{1, 2, 3, 4, 5, 6, 7, 8},{1, 2, 3, 4, 5, 6, 7, 8},{1, 2, 3, 4, 5, 6, 7, 8},{1, 2, 3, 4, 5, 6, 7, 8},{1, 2, 3, 4, 5, 6, 7, 8},};public static int sMatrixB[][] = new int[][] {{5, 6, 7, 8, 1, 2, 3, 4},{5, 6, 7, 8, 1, 2, 3, 4},{5, 6, 7, 8, 1, 2, 3, 4},{5, 6, 7, 8, 1, 2, 3, 4},{5, 6, 7, 8, 1, 2, 3, 4},{5, 6, 7, 8, 1, 2, 3, 4},{5, 6, 7, 8, 1, 2, 3, 4},{5, 6, 7, 8, 1, 2, 3, 4},};public static void main(String[] args) {System.out.println("普通矩阵乘法");SquareMatrixMultiply(sMatrixA, sMatrixB);System.out.println("\n递归矩阵乘法");int C[][] = squareMatrixMultiplyRecursive(sMatrixA, sMatrixB);displaySquare(C);System.out.println("\n Strassen 递归矩阵乘法");C = strassenMatrixMultiplyRecursive(sMatrixA, sMatrixB);displaySquare(C);}
}

这篇关于算法导论第4章strassen算法JAVA实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1024516

相关文章

Java实现字节字符转bcd编码

《Java实现字节字符转bcd编码》BCD是一种将十进制数字编码为二进制的表示方式,常用于数字显示和存储,本文将介绍如何在Java中实现字节字符转BCD码的过程,需要的小伙伴可以了解下... 目录前言BCD码是什么Java实现字节转bcd编码方法补充总结前言BCD码(Binary-Coded Decima

SpringBoot全局域名替换的实现

《SpringBoot全局域名替换的实现》本文主要介绍了SpringBoot全局域名替换的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录 项目结构⚙️ 配置文件application.yml️ 配置类AppProperties.Ja

Java使用Javassist动态生成HelloWorld类

《Java使用Javassist动态生成HelloWorld类》Javassist是一个非常强大的字节码操作和定义库,它允许开发者在运行时创建新的类或者修改现有的类,本文将简单介绍如何使用Javass... 目录1. Javassist简介2. 环境准备3. 动态生成HelloWorld类3.1 创建CtC

JavaScript中的高级调试方法全攻略指南

《JavaScript中的高级调试方法全攻略指南》什么是高级JavaScript调试技巧,它比console.log有何优势,如何使用断点调试定位问题,通过本文,我们将深入解答这些问题,带您从理论到实... 目录观点与案例结合观点1观点2观点3观点4观点5高级调试技巧详解实战案例断点调试:定位变量错误性能分

Python实现批量CSV转Excel的高性能处理方案

《Python实现批量CSV转Excel的高性能处理方案》在日常办公中,我们经常需要将CSV格式的数据转换为Excel文件,本文将介绍一个基于Python的高性能解决方案,感兴趣的小伙伴可以跟随小编一... 目录一、场景需求二、技术方案三、核心代码四、批量处理方案五、性能优化六、使用示例完整代码七、小结一、

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

Java使用jar命令配置服务器端口的完整指南

《Java使用jar命令配置服务器端口的完整指南》本文将详细介绍如何使用java-jar命令启动应用,并重点讲解如何配置服务器端口,同时提供一个实用的Web工具来简化这一过程,希望对大家有所帮助... 目录1. Java Jar文件简介1.1 什么是Jar文件1.2 创建可执行Jar文件2. 使用java

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1

C#实现一键批量合并PDF文档

《C#实现一键批量合并PDF文档》这篇文章主要为大家详细介绍了如何使用C#实现一键批量合并PDF文档功能,文中的示例代码简洁易懂,感兴趣的小伙伴可以跟随小编一起学习一下... 目录前言效果展示功能实现1、添加文件2、文件分组(书签)3、定义页码范围4、自定义显示5、定义页面尺寸6、PDF批量合并7、其他方法

SpringBoot实现不同接口指定上传文件大小的具体步骤

《SpringBoot实现不同接口指定上传文件大小的具体步骤》:本文主要介绍在SpringBoot中通过自定义注解、AOP拦截和配置文件实现不同接口上传文件大小限制的方法,强调需设置全局阈值远大于... 目录一  springboot实现不同接口指定文件大小1.1 思路说明1.2 工程启动说明二 具体实施2