matrix5


//http://www.linuxidc.com/Linux/2012-08/67662.htm
#include "mpi.h"   
#include <stdio.h>   
#include <stdlib.h>   
  
#define ROOT 0   
#define TAG 0   
int main(int argc,char *argv[]) {  
    float matrix[][4]={{2,3,4,5},//the upper trangular matrix representing a equation set   
                {0,2,3,4},  
                {0,0,2,3}};  
    int xcnt=3;//cout of x   
    int self,size,tag=0;  
    MPI_Init(&argc,&argv);  
    MPI_Comm_rank(MPI_COMM_WORLD,&self);  
    MPI_Comm_size(MPI_COMM_WORLD,&size);  
  
    MPI_Request r;  
    MPI_Status s;  
  
    MPI_Datatype MPI_VEC;  
    MPI_Type_vector(xcnt+1,1,1,MPI_FLOAT,&MPI_VEC);  
    MPI_Type_commit(&MPI_VEC);  
    float* equation=(float*)malloc((xcnt+1)*sizeof(float));  
    float xs,xr;  
    if(0==self) {//send each process the correspond equation   
        //parellel send and copy   
        MPI_Issend(matrix[1],1,MPI_VEC,1,TAG,MPI_COMM_WORLD,&r);  
        for(int i=0;i<=xcnt;++i) {  
            equation[i]=matrix[0][i];  
        }  
        MPI_Wait(&r,&s);  
        for(int i=2;i<size;++i) {  
            MPI_Ssend(matrix[i],1,MPI_VEC,i,TAG,MPI_COMM_WORLD);  
        }  
    } else {  
        MPI_Recv(equation,1,MPI_VEC,ROOT,TAG,MPI_COMM_WORLD,&s);  
    }  
      
    for(int i=xcnt-1;i>=0;--i) {  
        if(i==self) {  
            xs=equation[xcnt]/equation[i];  
            printf("x%d = %f \n",self,xs);  
            MPI_Bcast(&xs,1,MPI_FLOAT,i,MPI_COMM_WORLD);  
        } else {  
            MPI_Bcast(&xs,1,MPI_FLOAT,i,MPI_COMM_WORLD);  
            if(i>self) {  
                equation[xcnt]-=equation[i]*xs;  
            }  
        }  
    }  
    free(equation);  
    MPI_Finalize();  
    return 0;  
}  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我已经了解了您的问题。以下是矩阵类Matrix的Java代码实现: public class Matrix { private int row_size; private int col_size; private int[][] values; public void setMatrix(int m, int n) { row_size = m; col_size = n; values = new int[row_size][col_size]; Scanner input = new Scanner(System.in); System.out.println("请输入 " + row_size + " 行 " + col_size + " 列的整数矩阵:"); for (int i=0; i<row_size; i++) { for (int j=0; j<col_size; j++) { values[i][j] = input.nextInt(); } } } public void setMatrix(int m, int n, int v[][]) { row_size = m; col_size = n; values = new int[row_size][col_size]; for (int i=0; i<row_size; i++) { System.arraycopy(v[i], 0, values[i], 0, col_size); } } public Matrix add(Matrix m) { Matrix result = new Matrix(); result.setMatrix(row_size, col_size); for (int i=0; i<row_size; i++) { for (int j=0; j<col_size; j++) { result.values[i][j] = values[i][j] + m.values[i][j]; } } return result; } public Matrix mul(Matrix m) { Matrix result = new Matrix(); result.setMatrix(row_size, m.col_size); for (int i=0; i<row_size; i++) { for (int j=0; j<m.col_size; j++) { for (int k=0; k<col_size; k++) { result.values[i][j] += values[i][k] * m.values[k][j]; } } } return result; } public void print() { System.out.println("矩阵内容为:"); for (int i=0; i<row_size; i++) { for (int j=0; j<col_size; j++) { System.out.print(values[i][j] + " "); } System.out.println(); } } } 在Main类的main方法中,我们可以按照以下方式使用Matrix类: public static void main(String[] args) { Matrix m1 = new Matrix(); int v1[][] = {{1,0}, {0,1},{1,1}}; m1.setMatrix(3,2,v1); int v2[][] = {{1,2}, {3,4},{5,6}}; Matrix m2 = new Matrix(); m2.setMatrix(3,2,v2); Matrix m3 = m1.add(m2); m3.print(); // 输出m1与m2两个矩阵的和矩阵 System.out.println(); Matrix m4 = new Matrix(); m4.setMatrix(2,3); // 完成m4矩阵的初始化,其间需要从键盘输入2行3列的整数矩阵 Matrix m5 = m1.mul(m4); m5.print(); // 输出m1与m4两个矩阵的积矩阵 } 这样就可以完成矩阵类Matrix的编写,并且实现了根据用户输入的矩阵大小和数据,计算矩阵的和、积,以及输出矩阵的功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值