使用MR求解多个矩阵的乘积之后

待求解的问题如下所示:

X=(B+u(s+b+d+f))*F

代码如下:

  1 /**
  2  * Created with IntelliJ IDEA.
  3  * User: hadoop
  4  * Date: 16-3-5
  5  * Time: 上午9:24
  6  * To change this template use File | Settings | File Templates.
  7  */
  8 import org.apache.hadoop.conf.Configuration;
  9 import org.apache.hadoop.fs.FileSystem;
 10 import java.io.IOException;
 11 import java.lang.reflect.Array;
 12 import java.net.URI;
 13 import java.util.Arrays;
 14
 15 import org.apache.hadoop.fs.Path;
 16 import org.apache.hadoop.io.IntWritable;
 17 import org.apache.hadoop.io.ArrayWritable;
 18 import org.apache.hadoop.io.Text;
 19 import org.apache.hadoop.io.Writable;
 20 import org.apache.hadoop.mapreduce.InputSplit;
 21 import org.apache.hadoop.mapreduce.Job;
 22 import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
 23 import org.apache.hadoop.mapreduce.lib.input.FileSplit;
 24 import org.apache.hadoop.mapreduce.lib.input.SequenceFileInputFormat;
 25 import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
 26 import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
 27 import org.apache.hadoop.mapreduce.Reducer;
 28 import org.apache.hadoop.mapreduce.Mapper;
 29
 30 public class MutiInputMatrixProduct {
 31     public static class MyMapper extends Mapper<IntWritable,IntArrayWritable,IntWritable,IntArrayWritable>{
 32         public IntArrayWritable map_value=new IntArrayWritable();
 33         public static  int[][] rightMatrix=new int[][]{{10,10,10,10,10},{10,10,10,10,10},{10,10,10,10,10},
 34                 {10,10,10,10,10}};
 35         public Object obValue=null;
 36         public IntWritable[] arraySum=new IntWritable[rightMatrix[0].length];
 37         public int sum=0;
 38        public void map(IntWritable key,IntArrayWritable value,Context context) throws IOException, InterruptedException {
 39            obValue=value.toArray();
 40            InputSplit inputSplit=context.getInputSplit();
 41            String fileName=((FileSplit)inputSplit).getPath().getName();
 42
 43            if (!(fileName.equals("10IntArray"))) {
 44             for (int i=0;i<rightMatrix[0].length;++i){
 45                 sum=0;
 46                 for (int j=0;j<rightMatrix.length;++j){
 47                       sum+= Integer.parseInt(Array.get(obValue,j).toString())*2*rightMatrix[j][i];
 48                 }
 49                 arraySum[i]=new IntWritable(sum);
 50             }
 51              map_value.set(arraySum);
 52            }
 53             else{
 54                for (int i=0;i<rightMatrix[0].length;++i){
 55                    sum=0;
 56                    for (int j=0;j<rightMatrix.length;++j){
 57                        sum+= Integer.parseInt(Array.get(obValue,j).toString())*rightMatrix[j][i];
 58                    }
 59                    arraySum[i]=new IntWritable(sum);
 60                }
 61                map_value.set(arraySum);
 62            }
 63            context.write(key,map_value);
 64        }
 65     }
 66     public static class MyReducer extends Reducer<IntWritable,IntArrayWritable,IntWritable,Text>{
 67         public int[] sum=null;
 68         public Object obValue=null;
 69         public Text resultText=null;
 70
 71         public void setup(Context context){
 72             sum=new int[Integer.parseInt(context.getConfiguration().get("leftMatrixNum"))];
 73         }
 74
 75         public void reduce(IntWritable key,Iterable<IntArrayWritable>value,Context context) throws IOException, InterruptedException {
 76           for(IntArrayWritable intValue:value){
 77               obValue=intValue.toArray();
 78               for (int i=0;i<Array.getLength(obValue);++i){
 79                   sum[i]+=Integer.parseInt(Array.get(obValue,i).toString());
 80               }
 81
 82           }
 83          resultText=new Text(Arrays.toString(sum));
 84             for (int i=0;i<sum.length;++i){
 85                 sum[i]=0;
 86             }
 87           context.write(key,resultText);
 88         }
 89
 90     }
 91
 92     public static void main(String[]args) throws IOException, ClassNotFoundException, InterruptedException {
 93         String uri="/home/hadoop/2016Test/Input";
 94         String outUri="/home/hadoop/2016Test/Output";
 95         Configuration conf=new Configuration();
 96         FileSystem fs=FileSystem.get(URI.create(uri),conf);
 97         fs.delete(new Path(outUri),true);
 98         conf.set("leftMatrixNum","5");
 99         Job job=new Job(conf,"MultiMatrix");
100         job.setJarByClass(MutiInputMatrixProduct.class);
101         job.setInputFormatClass(SequenceFileInputFormat.class);
102         job.setOutputFormatClass(TextOutputFormat.class);
103         job.setMapperClass(MyMapper.class);
104         job.setReducerClass(MyReducer.class);
105         job.setMapOutputKeyClass(IntWritable.class);
106         job.setMapOutputValueClass(IntArrayWritable.class);
107         job.setOutputKeyClass(IntWritable.class);
108         job.setOutputValueClass(Text.class);
109         FileInputFormat.setInputPaths(job, new Path(uri));
110         FileOutputFormat.setOutputPath(job,new Path(outUri));
111         System.exit(job.waitForCompletion(true)?0:1);
112     }
113 }
114 class IntArrayWritable extends ArrayWritable {
115     public IntArrayWritable(){
116         super(IntWritable.class);
117     }
118     public String toString(){
119         StringBuilder sb=new StringBuilder();
120         for (Writable val:get()){
121             IntWritable intWritable=(IntWritable)val;
122             sb.append(intWritable.get());
123             sb.append(",");
124         }
125         sb.deleteCharAt(sb.length()-1);
126         return sb.toString();
127     }
128 }
时间: 2024-08-05 08:24:10

使用MR求解多个矩阵的乘积之后的相关文章

Codeforces 506E Mr. Kitayuta&#39;s Gift (矩阵乘法,动态规划)

描述: 给出一个单词,在单词中插入若干字符使其为回文串,求回文串的个数(|s|<=200,n<=10^9) 这道题超神奇,不可多得的一道好题 首先可以搞出一个dp[l][r][i]表示回文串左边i位匹配到第l位,右边i位匹配到第r位的状态数,可以发现可以用矩阵乘法优化(某人说看到n这么大就一定是矩阵乘法了= =) 但这样一共有|s|^2个节点,时间复杂度无法承受 我们先把状态树画出来:例如add 可以发现是个DAG 我们考虑把单独的每条链拿出来求解,那么最多会有|s|条不同的链,链长最多为|s

题目1489:计算两个矩阵的乘积

时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:5744 解决:1234 题目描述: 计算两个矩阵的乘积,第一个是2*3,第二个是3*2 输入: 输入为两个矩阵,其中一个为2*3的矩阵,另一个为3*2的矩阵 输出: 一个2*2的矩阵(每一个数字后都跟一个空格) 样例输入: 1 2 3 3 4 5 6 7 8 9 10 11 样例输出: 52 58 100 112 来源: 2012年哈尔滨工业大学计算机研究生机试真题 矩阵乘积的计算是  52 = 1*6+2*8+3*10  58 =

@大脑练习: 计算两个矩阵的乘积

题目1489:计算两个矩阵的乘积时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:134 解决:25 题目描述: 计算两个矩阵的乘积,第一个是2*3,第二个是3*2 输入: 输入为两个矩阵,其中一个为2*3的矩阵,另一个为3*2的矩阵 输出: 一个2*2的矩阵(每一个数字后都跟一个空格) 样例输入: 1 2 3 3 4 5 6 7 8 9 10 11样例输出: 52 58 100 112来源: 2012年哈尔滨工业大学计算机研究生机试真题 [cpp] /****************

opencv计算矩阵与数值的乘积,矩阵与矩阵的乘积

1.矩阵与数值的乘积 在进行数组与一个常量相乘的运算时,使用了Mat类中的mul函数. //! per-element matrix multiplication by means of matrix expressions MatExpr mul(InputArray m, double scale=1) const; 使用后,发现数据有些异常,于是就打印出每一个计算后的数值, 发现原来该函数是将,m中矩阵每个元素先做二次方运算,再与scale相乘,于是放弃用该函数,改用 //! comput

计算矩阵的乘积

问题: 编写程序,计算两矩阵a与b的乘积,并输出结果 分析: 根据两矩阵相乘的规则,矩阵a的列数应与矩阵b的行数相同.若a是4行3列矩阵,b是3行2列矩阵,设乘积为c,则c是4行两列矩阵,由于数学上的矩阵恰好和C语言上二维数组相对应,因此本例说明三个二维数组变量,其所分配的存储空间恰好可以存放a.b.c三个矩阵所有的元素值,为简便计,不妨设矩阵元素为整型数值,矩阵a.b的元素值均由键盘输入,为增强直观性,希望所输入的数据以及输出的结果数据形式均与矩阵的书写顺序相对应,,即各矩阵按行列左右分开.上

实现求解线性方程(矩阵、高斯消去法)------c++程序设计原理与实践(进阶篇)

步骤: 其中A是一个n*n的系数方阵 向量x和b分别是未知数和常量向量: 这个系统可能有0个.1个或者无穷多个解,这取决于系数矩阵A和向量b.求解线性系统的方法有很多,这里使用一种经典的方法--高斯消去法(https://zh.wikipedia.org/wiki/高斯消去法).首先,我们对A和b进行交换,使得A变为一个上三角矩阵.上三角矩阵就是对角线之下的所有元素均为0.即如下形式: 实现这个目标是很容易的.为了使a(i,j)变为0,我们先将它乘以一个常量,使它等于第j列上的另一个元素,比如说

横向量与矩阵的乘积

设Tj=(Tj1, Tj2, ..., Tjn)为横向量.而iδ是Tj中第δ位不为零的元素,1≤δ≤z. 令h′j=TjH, 则h′j是T·H的第j行.且有 h′j=Σ(k=i1, i2, ..., iz)Tj,k·Hk. 从上式看, (1)可以将横向量的第k位视为右边矩阵第k行是否叠加的控制信号. (2)事先排除0元素,从而排除H矩阵对应的行,可以节省计算(第i个元素为0,则排除第i行的叠加运算).

线性求解单应矩阵 Homography

定义: 2D单应:给定图像$\mathbb{P}^{2}$中的特征点集$\mathbf{x}_i$和另一幅图像在$\mathbb{P}^{2}$ 中对应的特征点集$\mathbf{x}_{i}^{'}$,  将$\mathbf{x}_i$映射到$\mathbf{x}^{'}_{i}$的射影变换.在实际情况中,点$\mathbf{x}_{i}$和$\mathbf{x}^{'}_{i}$是两幅图像上的点,每幅图像都视为一张射影平面$\mathbb{P}^{2}$ $\mathbf{x}^{'}_{i

使用numpy求解Zoepritz 方程矩阵伪逆时报错: SVD did not converge

笔者在使用numpy中的pinv函数求解伪逆时系统报错: SVD did not converge. 奇异值分解不收敛 具体原因不太清楚, 应该是因为函数在求解伪逆的算法在迭代过程中难以收敛导致的. 解决方法: 引入scipy中的求解伪逆的函数 scipy.linalg.pinv Zoepritz equations的python程序在: https://github.com/cui-xiaoang96/Zoepritz-equations 原文地址:https://www.cnblogs.co