当前位置: 首页 > 编程日记 > 正文

矩阵乘法递归求解

给定两个N×N的矩阵,求乘积

如下图所示,乘法执行过程如下:

1.矩阵1先拿出一行,矩阵2先拿出一列

2.行与列相乘得到value1

3.行与剩下矩阵2相乘得到value2(递归过程)

4.剩下矩阵1与列相乘得到value3(递归过程)

5.剩下矩阵1与剩下矩阵2相乘得到value4(递归过程)

6.将value1~value4合并成结果数组

写代码时注意约定,value1一定是数值,value2、value3、value4是数值、空数组或者非空的二维数组

由php实现,大体思路不难,但是要非常注意合并结果的细节:

  1 <?php
  2 
  3 class MatrixProduct
  4 {
  5     protected $matrix1, $matrix2;
  6     protected $result;
  7 
  8     public function __construct($matrix1, $matrix2)
  9     {
 10         $this->matrix1 = $matrix1;
 11         $this->matrix2 = $matrix2;
 12         $this->result = $this->matrixProduction($this->matrix1, $this->matrix2);
 13     }
 14 
 15     public function show()
 16     {
 17         foreach ($this->result as $row) {
 18             foreach ($row as $data) {
 19                 printf('%5d', $data);
 20             }
 21             echo PHP_EOL;
 22         }
 23     }
 24 
 25     protected function popRow(&$matrix)
 26     {
 27         return empty($matrix) ? [] : [array_shift($matrix)];
 28     }
 29 
 30     protected function popColumn(&$matrix)
 31     {
 32         $column = [];
 33         foreach ($matrix as $key => &$row) {
 34             $data = array_shift($row);
 35             if (empty($data) || empty($row)) {
 36                 unset($matrix[$key]);
 37             }
 38             $column[] = [$data];
 39         }
 40         return $column;
 41     }
 42 
 43     protected function countProduction($row, $column)
 44     {
 45         for ($i = 0, $sum = 0; $i < count($row[0]); $i++) {
 46             $sum += $row[0][$i] * $column[$i][0];
 47         }
 48         return $sum;
 49     }
 50 
 51     protected function merger($value1, $value2, $value3, $value4)
 52     {
 53         if (empty($value2) && empty($value3)) {
 54             return $value1;
 55         } else {
 56             $array12 = array_merge([$value1], !is_array($value2) ? [$value2] : $value2[0] ?? []);
 57             if (!is_array($value3)) {
 58                 $array34 = array_merge([$value3], !is_array($value4) ? [$value4] : $value4[0] ?? []);
 59                 return [$array12, $array34];
 60             } else {
 61                 for ($i = 0, $array34 = []; $i < count($value3); $i++) {
 62                     $array34[] = array_merge($value3[$i], $value4[$i] ?? []);
 63                 }
 64                 return array_merge([$array12], $array34);
 65             }
 66         }
 67     }
 68 
 69     protected function matrixProduction($matrix1, $matrix2)
 70     {
 71         $row = $this->popRow($matrix1);
 72         $column = $this->popColumn($matrix2);
 73         if (empty($row) || empty($column)) {
 74             return [];
 75         }
 76 
 77         $value1 = $this->countProduction($row, $column);
 78         $value2 = $this->matrixProduction($row, $matrix2);
 79         $value3 = $this->matrixProduction($matrix1, $column);
 80         $value4 = $this->matrixProduction($matrix1, $matrix2);
 81 
 82         return $this->merger($value1, $value2, $value3, $value4);
 83     }
 84 }
 85 
 86 $matrix1 = [
 87     [1, 2, 3, 4],
 88     [1, 2, 3, 4],
 89     [1, 2, 3, 4],
 90     [1, 2, 3, 4],
 91 ];
 92 
 93 $matrix2 = [
 94     [1, 2, 3, 4],
 95     [5, 6, 7, 8],
 96     [9, 10, 11, 12],
 97     [13, 14, 15, 16]
 98 ];
 99 
100 $production = new MatrixProduct($matrix1, $matrix2);
101 $production->show();

转载于:https://www.cnblogs.com/SHQHDMR/p/11089228.html

相关文章:

What is acceptable cell and suitable cell in LTE?

The difference between acceptable cell and suitable cell in LTE is given below. acceptable cell In a area where UE is not able to find any suitable cell to camp on, it goes for acceptable cell. An “acceptable cell” is a cell on which the UE may camp to o…

appium 控件定位

转自&#xff1a;http://www.2cto.com/kf/201410/340345.html AppiumDriver的各种findElement方法的尝试&#xff0c;尝试的目标应用是SDK自带的Notepad应用。 1. findElementByName 1.1 示例 ?12el driver.findElementByName("Add note");assertThat(el.getText()…

android应用去掉状态栏_Android 显示、隐藏状态栏和导航栏

Android 显示、隐藏状态栏和导航栏控制状态栏显示&#xff0c;Activity的主题中配置全屏属性true控制状态栏显示&#xff0c;在setContentView之前设置全屏的flaggetWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREE…

hdu1305Immediate Decodability(字典树)

这题看是否 这题能A是侥幸&#xff0c;解决的办法是先存一下输入的字符串&#xff0c;进行排序。 Problem DescriptionAn encoding of a set of symbols is said to be immediately decodable if no code for one symbol is the prefix of a code for another symbol. We will …

python isdigit()

isdigit(): Python isdigit() 方法检测字符串是否只由数字组成。如果字符串只包含数字则返回 True 否则返回 False。 实例 以下实例展示了isdigit()方法的实例&#xff1a; #!/usr/bin/python3str "123456"; print (str.isdigit()) str "Runoob example....wo…

Control Channel Element (CCE)

1. CCE是PDCCH的资源单元 (resourceunit)。 A PDCCH is transmitted on one CCE or anaggregation of several consecutive CCEs, where a CCE corresponds to 9 Resource ElementGroups (REGs). In PDCCH transmission, only those REGs are used which are notassigned …

phpstrom+xdebug调试PHP代码

众所周知开发PHP的IDE种类繁多&#xff0c;然而开发PHP并不能像开发其他语言一样&#xff0c;调试PHP代码对诸多新手来说&#xff0c;搭建调试环境就比较麻烦&#xff01;其实哈&#xff0c;我发现NuSphere-phped-16.0很强大&#xff0c;集成了很强大的debug功能&#xff0c;只…

ubuntu 设置开机执行脚本_ubuntu-18.04 设置开机启动脚本

ubuntu-18.04 设置开机启动脚本参阅下列链接ubuntu-18.04不能像ubuntu14一样通过编辑rc.local来设置开机启动脚本&#xff0c;通过下列简单设置后&#xff0c;可以使rc.local重新发挥作用。1、建立rc-local.service文件sudo vi /etc/systemd/system/rc-local.service2、将下列内…

Web Api单元测试写法

例如我们在Web Api项目中有个Controller public class SomeController : ApiController { public HttpResponseMessage Get() { // 一些操作 return Request.CreateResponse(HttpStatusCode.OK&#xff0c; someModel); } }如果你在单元测试中直接调用 SomeController 的Get()方…

数据挖掘深入理解和学习路径

上一篇文章中分享了数据分析的学习全景路径 其中最关键的部分就是数据挖掘&#xff0c;那什么是数据挖掘呢&#xff1f; 数据挖掘就是通过分析采集而来的数据源&#xff0c;从庞大的数据中发现规律&#xff0c;找到宝藏。 一&#xff0c;数据挖掘的基本流程 数据挖掘可分为6个步…

3G重选至4G--基于优先级

3G重选至4G--基于优先级 1. Specification 1.1 Measurementrules 是否需要开启测量 3GPP 25.304 - 5.2.6.1.2aMeasurement rules for inter-frequency and inter-RAT cell reselection when absolutepriorities are used 1.2 Evaluation/ ReselectionCriteria 对测量结…

C#_Socket网络编程实现的简单局域网内即时聊天,发送文件,抖动窗口。

C#_Socket网络编程实现的简单局域网内即时聊天&#xff0c;发送文件&#xff0c;抖动窗口。 最近接触了C#Socket网络编程&#xff0c;试着做了试试(*^__^*) 实现多个客户端和服务端互相发送消息 发送文件抖动窗口功能 服务端&#xff1a; using System; using System.Colle…

移动端大图缩放模糊_关于移动端小图标模糊问题的解决方法

前言之前给大家讲到图片和文字垂直方向不对齐的问题&#xff0c;其中举的小例子中用到了一个小图标&#xff0c;这个小图标我用的是背景图来显示&#xff1a;.del .icon{ display: inline-block; width: 20px; height: 25px; margin-right: 5px;vertical-align: middle; backgr…

T-SQL WITH 分号问题

使用with 前面有sql语句时候 运行 with tempTbale(id) as ( select ..... )select * from tempTbale 运行上面语句 提示下面错误 Incorrect syntax near the keyword with. If this statement is a common table expression, an xmlnamespaces clause or a change tracking con…

SVN 撤回(回滚)提交的代码

转&#xff1a; SVN 撤回&#xff08;回滚&#xff09;提交的代码 2016年12月20日 17:20:58 怀色 阅读数 68614 标签&#xff1a; svn svn回滚 版本回滚 更多 个人分类&#xff1a; svn 版权声明&#xff1a;本文为博主原创文章&#xff0c;未经博主允许不得转载。 https://…

LTE-怎么获取上行资源

LTE中&#xff0c;UE如果要发送上行数据(Control sig or Data)&#xff0c;需要上行pusch资源&#xff0c;如果没有被分配pusch的话&#xff0c;则需要申请。 有3种方法进行申请&#xff1a; 这3中方法对应不同的场景&#xff0c;详细逻辑如下&#xff1a;

mysql中sql语句

<数据定义语言DDL> 一. create TABLE tableName 创建表 二. alter TABLE tableName 修改表 三. drop TBALE tableName 删除表 <数据控制语言DCL> GRANT/REVOKE <数据操纵语言DML> 一. insert INTO tableName(,,,) values(,,,); 插入数据(增) 二. update tabl…

wxpython有没有可视化设计_wxPython - GUI Builder工具( GUI Builder Tools)

wxPython - GUI Builder工具( GUI Builder Tools)通过手动编码创建美观的GUI可能很乏味。 可视化GUI设计器工具总是很方便。 许多针对wxPython的GUI开发IDE都可用。 以下是其中一些 -wxFormBuilderwxDesignerwxGladeBoaConstructorgui2pywxFormBuilder是一个开源的&#xff0c;…

微信网页JSDK接口-wx.chooseImage问题

wx.chooseImage({count: 1, // 默认9sizeType: [original, compressed], // 可以指定是原图还是压缩图&#xff0c;默认二者都有sourceType: [album, camera], // 可以指定来源是相册还是相机&#xff0c;默认二者都有success: function (res) {var localIds res.localIds; //…

ARM 的几个重要特点

ARM 采用RISC指令集 ARM: Acorn RISC Machine; //Acorn: 公司的名字 它支持的指令比较简单&#xff0c;所以功耗小、价格便宜&#xff0c;特别合适移动设备。 RISC 和CISC的区别&#xff1a; 举例子&#xff0c;乘加运算&#xff0c;比如&#xff1a; ya*b c; 在CISC里面…

html文字中横线_谈PPT课件中自定义动画应用之内容控制

本案例来源于一位资深政治教师的课件应用经验。在她的朋友圈看到&#xff1a;讲解高考政治主观题课件要这样做才好&#xff0c;材料全部显示完后&#xff0c;再把一些关键字词句用彩色字标注或横线或圆圈标注&#xff0c;然后再分析归纳&#xff0c;哪些字词句是设问范围内应该…

PowerShell过滤文件中的重复内容

Get-Content -Path E:\test11\data.txt | Sort-Object | Get-Unique 源文件&#xff1a; AA0001 2014-06-30 15:27:13.073 AA0001 2014-06-30 15:27:13.073 AA0001 2014-06-30 15:27:13.073 AA0002 2014-06-30 15:27:30.607 AA0002 2014-06-30 15:28:00.467 AA0003 2014-06-30 …

pstree进程管理

功能&#xff1a;pstree命令列出当前的进程&#xff0c;以及它们的树状结构。 格式&#xff1a;pstree [选项] [pid|user] 主要选项如下&#xff1a; -a&#xff1a;显示执行程序的命令与完整参数。 -c&#xff1a;取消同名程序&#xff0c;合并显示。 -h&#xff1a;对输出结果…

LTE MIB 的发送周期

MIB在PBCH上发送&#xff0c;PBCH 采用QPSK调制。 PBCH的时频资源位置固定&#xff0c;可以参考我的博文” LTE FDD PSS/SSS/MIB时频资源位置”. 一个SFN发送一次MIB&#xff0c;接下来3个SFN重复发送同样的信息(但是以不同的扰码加扰)&#xff0c;也就是说MIB的发送周期为4…

吸顶wifi_分享 | 酒店WiFi网络的三种部署模式

酒店的无线网络&#xff0c;在酒店部署移动网络业务的时候&#xff0c;很多酒店会发现实际效果远达不到预期。酒店员工和入住用户经常会抱怨无线网络不稳定、视频无限缓冲中、经常掉线……&#xff0c;那么今天我们来了解酒店无线网络的部署。一、影响WiFI漫游的因素导致出现以…

最后一片蓝海的终极狂欢-写在Win10发布前夕

作为一名Windows8.x系统平台从业者&#xff0c;从工作伊始&#xff0c;耳边不断充斥着Windows将走向没落的言论&#xff0c;Win10今日晚些时候即将发布&#xff0c;笔者借此机会&#xff0c;说说自己的看法。 早在2012年的时候&#xff0c;IDC曾预测&#xff0c;WP系统将在2016…

错误信息输出,重定向到文件

将错误重定向到文件remove-item none 2> d:\ee.txt 将错误追加到已有文件remove-item none 2>> d:\ee.txt 将错误发送到成功输出流。如果报错后&#xff0c;代码依然继续执行&#xff0c;则Exception不会被捕获到$myerror Remove-Item "NoSuchDirectory" 2…

spark-submit --files 动态加载外部资源文件

在做spark时&#xff0c;有些时候需要加载资源文件&#xff0c;需要在driver或者worker端访问。在client模式下可以使用IO流直接读取,但是在cluster模式下却不能直接读取&#xff0c;需要如下代码&#xff1a; val is: InputStream this.getClass.getResourceAsStream(“./xxx…

LTE SIB1时频资源

1.时域资源 参考3GPP 36.331 – 5.2.1.2Scheduling The SystemInformationBlockType1 uses a fixed schedule with a periodicity of 80 msand repetitions made within80 ms. Thefirst transmission of SystemInformationBlockType1 is scheduled insubframe #5 of radio fram…

ssm框架mysql配置_ssm框架使用详解配置两个数据源

学习ssm框架已经快一年了&#xff0c;今天把这个框架总结一下。SSM 就是指 spring、SpringMVC和Mybatis。先说一下基本概念(百度上搜的)1、基本概念1.1、SpringSpring是一个开源框架&#xff0c;Spring是于2003 年兴起的一个轻量级的Java 开发框架&#xff0c;由Rod Johnson 在…