矩阵相乘java练习--20191117

学习参考:1、Java实现矩阵相乘
https://blog.csdn.net/ares_xxm/article/details/72232467
2、java小练习(矩阵相乘)
https://blog.csdn.net/wfzczangpeng/article/details/8039850

3、题目要求:
输入1、矩阵阶数n
2、第一个n阶矩阵
3、第二个n阶矩阵
输出2个矩阵的乘积。
例如输入:

    3
1 2 3
1 2 3
1 2 3
3 2 1
3 2 1
3 2 1

输出:

18 12 6
18 12 6
18 12 6

思路分析:
考虑AB=C,其中A(R,N), B(N,M), C(R,M)为矩阵。

作者:whitefang
链接:https://www.zhihu.com/question/21351965/answer/727385709

算法实现:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // 获取输入参数
        Object[] inputs = getInputs();
        // 矩阵相乘运算
        Integer n = (Integer) inputs[0];
        Integer a[][] = (Integer[][]) inputs[1];
        Integer b[][] = (Integer[][]) inputs[2];
        int c[][] = new int[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                for (int k = 0; k < n; k++) {
                    c[i][j] += a[i][k] * b[k][j];
                }
                if(j < (n-1)) {
                    System.out.print(c[i][j] + " ");
                }else {
                    System.out.print(c[i][j]);
                }
            }
            System.out.println("");
        }
    }

    public static Object[] getInputs() {
        Scanner input = new Scanner(System.in);
        Integer n = input.nextInt();
        if(n <= 0) {
            System.out.println("Input number must great than 0, not " + n);
            return new Object[] {};
        }

        Integer a[][] = new Integer[n][n];
        Integer b[][] = new Integer[n][n];

        // 跳过n后面的换行符
        input.nextLine();
        // 获取矩阵a[][]的内容
        for (int i = 0; i < n; i++) {
            String strLine = input.nextLine();
            Scanner s = new Scanner(strLine);
            int j = 0;
            while (s.hasNextInt() && j < n ) {
                a[i][j++] = s.nextInt();
            }
        }
        // 获取矩阵b[][]的内容
        for (int i=0; i<n; i++) {
            String strLine = input.nextLine();
            Scanner s = new Scanner(strLine);
            int j = 0;
            while (s.hasNextInt() && j<n ) {
                b[i][j++] = s.nextInt();
            }
        }
        return new Object[] {n, a, b};
    }
}

验证:
在这里插入图片描述
4参考:矩阵的乘法算法
https://www.cnblogs.com/wuyudong/p/matrix-multiply.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Hadoop进行矩阵相乘需要进行以下步骤: 1. 将矩阵数据分小块,存储在Hadoop分布式文件系统中,每个块大小由用户指定。 2. 编写Map函数,将矩阵块进行分割,并将乘积放入context中。 3. 编写Reduce函数,对Map函数输出的结果进行合并,得到最终矩阵乘积。 下面是一个简单的矩阵相乘Java程序: ```java import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.DoubleWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class MatrixMultiply { public static class MatrixMapper extends Mapper<Object, Text, Text, DoubleWritable>{ private Text outKey = new Text(); private DoubleWritable outValue = new DoubleWritable(); public void map(Object key, Text value, Context context ) throws IOException, InterruptedException { String[] parts = value.toString().split(","); int row = Integer.parseInt(parts[0]); int col = Integer.parseInt(parts[1]); double val = Double.parseDouble(parts[2]); //处理矩阵A的每一行 if (parts[3].equals("A")) { for (int i = 1; i <= N; i++) { outKey.set(row + "," + i); outValue.set(val); context.write(outKey, outValue); } } //处理矩阵B的每一列 else if (parts[3].equals("B")) { for (int i = 1; i <= N; i++) { outKey.set(i + "," + col); outValue.set(val); context.write(outKey, outValue); } } } } public static class MatrixReducer extends Reducer<Text,DoubleWritable,Text,DoubleWritable> { private DoubleWritable outValue = new DoubleWritable(); public void reduce(Text key, Iterable<DoubleWritable> values, Context context ) throws IOException, InterruptedException { double sum = 0.0; for (DoubleWritable val : values) { sum += val.get(); } outValue.set(sum); context.write(key, outValue); } } public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "matrix multiply"); job.setJarByClass(MatrixMultiply.class); job.setMapperClass(MatrixMapper.class); job.setReducerClass(MatrixReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(DoubleWritable.class); FileInputFormat.setInputPaths(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); job.waitForCompletion(true); } } ``` 其中,输入文件的格式如下: ``` i,j,value,matrix(A/B) ``` 例如: ``` 1,1,2.0,A 1,2,3.0,A 2,1,1.0,A 2,2,4.0,A 1,1,5.0,B 1,2,6.0,B 2,1,7.0,B 2,2,8.0,B ``` 其中A矩阵为: ``` 2.0 3.0 1.0 4.0 ``` B矩阵为: ``` 5.0 6.0 7.0 8.0 ``` 输出结果为: ``` 1,1 19.0 1,2 26.0 2,1 33.0 2,2 50.0 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值