Numpy 中的矩阵向量乘法

结论:

元素乘法:np.multiply(a,b)
矩阵乘法:np.dot(a,b) 或 np.matmul(a,b) 或 a.dot(b)
唯独注意:*,在 np.array 中重载为元素乘法,在 np.matrix 中重载为矩阵乘法!

对于 np.array 对象

>>> a
array([[1, 2],
       [3, 4]])
  1. array中对应元素乘法。用 a*b 或 np.multiply(a,b) ,
    >>> a*a
    array([[ 1,  4],
           [ 9, 16]])
    
    >>> np.multiply(a,a)
    array([[ 1,  4],
           [ 9, 16]])
    
  2. array中矩阵乘法(内积相乘) 。用 np.dot(a,b) 或 np.matmul(a,b) 或 a.dot(b)或者a@b。
    >>> np.dot(a,a)
    array([[ 7, 10],
           [15, 22]])
    
    >>> np.matmul(a,a)
    array([[ 7, 10],
           [15, 22]])
    
    >>> a.dot(a)
    array([[ 7, 10],
           [15, 22]])
    

     

对于 np.matrix 对象

>>> A
matrix([[1, 2],
        [3, 4]])
  1. 矩阵中元素乘法用 np.multiply(a,b)
    >>> np.multiply(A,A)
    matrix([[ 1,  4],
            [ 9, 16]])
    
  2. 矩阵中矩阵乘法 (内积相乘)用 a*b 或 np.dot(a,b) 或 np.matmul(a,b) 或 a.dot(b)或者a@b。
    >>> A*A
    matrix([[ 7, 10],
            [15, 22]])
    >>> np.dot(A,A)
    matrix([[ 7, 10],
            [15, 22]])
    >>> np.matmul(A,A)
    matrix([[ 7, 10],
            [15, 22]])
    >>> A.dot(A)
    matrix([[ 7, 10],
            [15, 22]])
    

     

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 答:以下是一个完整的矩阵向量乘法的MPI编程代码:#include <stdio.h> #include <stdlib.h> #include <mpi.h>int main(int argc, char **argv) { int my_rank; int comm_sz; int A[4][4]; int x[4]; int result[4]; int i, j; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); MPI_Comm_size(MPI_COMM_WORLD, &comm_sz); if(my_rank == 0){ // 初始化矩阵A和向量x for(i = 0; i < 4; i++){ x[i] = i; for(j = 0; j < 4; j++){ A[i][j] = i + j; } } } MPI_Bcast(x, 4, MPI_INT, 0, MPI_COMM_WORLD); MPI_Scatter(A, 4, MPI_INT, A[my_rank], 4, MPI_INT, 0, MPI_COMM_WORLD); result[my_rank] = 0; for(i = 0; i < 4; i++){ result[my_rank] += A[my_rank][i] * x[i]; } MPI_Gather(result, 4, MPI_INT, result, 4, MPI_INT, 0, MPI_COMM_WORLD); if(my_rank == 0){ for(i = 0; i < 4; i++){ printf("%d\n", result[i]); } } MPI_Finalize(); return 0; } ### 回答2: 下面是一个完整的MPI编程代码,用于实现矩阵向量乘法。该代码会将矩阵向量分割成块,并使用MPI的通信机制来计算乘法结果。 ``` python from mpi4py import MPI import numpy as np # 创建MPI环境 comm = MPI.COMM_WORLD rank = comm.Get_rank() size = comm.Get_size() # 定义矩阵向量的大小 matrix_size = 6 vector_size = 2 # 划分矩阵向量的块大小 matrix_block_size = matrix_size // size vector_block_size = vector_size // size # 初始化矩阵块和向量块 matrix_block = np.zeros((matrix_block_size, matrix_size)) vector_block = np.zeros(vector_block_size) # 生成全局矩阵向量(仅在rank 0进程有) if rank == 0: matrix = np.random.rand(matrix_size, matrix_size) vector = np.random.rand(vector_size) else: matrix = None vector = None # 将全局矩阵向量分发给各个进程 comm.Scatter(matrix, matrix_block, root=0) comm.Scatter(vector, vector_block, root=0) # 计算局部矩阵向量的乘积 local_result = np.matmul(matrix_block, vector_block) # 收集各个进程的局部结果(最终结果仅在rank 0进程有) result = None if rank == 0: result = np.zeros(matrix_size) comm.Gather(local_result, result, root=0) # 打印最终结果(仅在rank 0进程有) if rank == 0: print("Matrix:") print(matrix) print("Vector:") print(vector) print("Result:") print(result) ``` 该代码使用`mpi4py`模块来编写MPI程序,并使用`numpy`库来进行矩阵向量的计算。在代码,我们首先创建MPI环境,获取进程的秩(rank)和进程的总数(size)。然后,我们定义矩阵向量的大小,并计算每个进程处理的块的大小。 在主进程(rank 0)生成全局矩阵向量,并使用MPI的`Scatter`函数将它们分发给各个进程。然后,各个进程计算局部矩阵向量的乘积,并使用MPI的`Gather`函数将结果收集到主进程(rank 0)。最后,主进程打印最终的结果。 请注意,该代码仅适用于进程总数能够整除矩阵向量的大小的情况。如果进程总数不能整除矩阵向量的大小,你需要根据实际情况进行调整来确保计算的正确性。 ### 回答3: 下面是一个完整的矩阵向量乘法的MPI编程代码: ```c #include <stdio.h> #include <stdlib.h> #include <mpi.h> #define MATRIX_SIZE 3 #define VECTOR_SIZE 3 void generate_matrix(int matrix[MATRIX_SIZE][MATRIX_SIZE]) { for (int i = 0; i < MATRIX_SIZE; i++) { for (int j = 0; j < MATRIX_SIZE; j++) { matrix[i][j] = rand() % 10; } } } void generate_vector(int vector[VECTOR_SIZE]) { for (int i = 0; i < VECTOR_SIZE; i++) { vector[i] = rand() % 10; } } void print_vector(int vector[VECTOR_SIZE]) { for (int i = 0; i < VECTOR_SIZE; i++) { printf("%d ", vector[i]); } printf("\n"); } void matrix_vector_multiply(int matrix[MATRIX_SIZE][MATRIX_SIZE], int vector[VECTOR_SIZE], int result[VECTOR_SIZE]) { for (int i = 0; i < MATRIX_SIZE; i++) { result[i] = 0; for (int j = 0; j < MATRIX_SIZE; j++) { result[i] += matrix[i][j] * vector[j]; } } } int main(int argc, char** argv) { int matrix[MATRIX_SIZE][MATRIX_SIZE]; int vector[VECTOR_SIZE]; int result[VECTOR_SIZE]; int size, rank; MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &size); MPI_Comm_rank(MPI_COMM_WORLD, &rank); if (rank == 0) { generate_matrix(matrix); generate_vector(vector); } MPI_Bcast(matrix, MATRIX_SIZE*MATRIX_SIZE, MPI_INT, 0, MPI_COMM_WORLD); MPI_Bcast(vector, VECTOR_SIZE, MPI_INT, 0, MPI_COMM_WORLD); int local_vector[VECTOR_SIZE/size]; MPI_Scatter(vector, VECTOR_SIZE/size, MPI_INT, local_vector, VECTOR_SIZE/size, MPI_INT, 0, MPI_COMM_WORLD); int local_result[VECTOR_SIZE/size]; matrix_vector_multiply(matrix, local_vector, local_result); int* gathered_result = NULL; if (rank == 0) { gathered_result = (int*)malloc(sizeof(int) * VECTOR_SIZE); } MPI_Gather(local_result, VECTOR_SIZE/size, MPI_INT, gathered_result, VECTOR_SIZE/size, MPI_INT, 0, MPI_COMM_WORLD); if (rank == 0) { printf("Matrix:\n"); for (int i = 0; i < MATRIX_SIZE; i++) { for (int j = 0; j < MATRIX_SIZE; j++) { printf("%d ", matrix[i][j]); } printf("\n"); } printf("Vector: "); print_vector(vector); printf("Result: "); print_vector(gathered_result); free(gathered_result); } MPI_Finalize(); return 0; } ``` 这个代码通过MPI实现了一个简单的矩阵向量乘法。在主进程(rank为0)生成矩阵向量,然后通过MPI_Bcast广播给所有进程。然后使用MPI_Scatter将向量分发给所有进程。每个进程计算局部乘积,并使用MPI_Gather将结果收集到主进程。最后,在主进程打印矩阵向量和结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值