matrix

How to make Min-plus matrix multiplication in python faster?

China☆狼群 提交于 2020-04-15 04:27:20
问题 So I have two matrices, A and B, and I want to compute the min-plus product as given here: Min-plus matrix multiplication. For that I've implemented the following: def min_plus_product(A,B): B = np.transpose(B) Y = np.zeros((len(B),len(A))) for i in range(len(B)): Y[i] = (A + B[i]).min(1) return np.transpose(Y) This works fine, but is slow for big matrices, is there a way to make it faster? I've heard that implemeting in C or using the GPU might be good options. 回答1: Here is an algo that

How to make Min-plus matrix multiplication in python faster?

℡╲_俬逩灬. 提交于 2020-04-15 04:27:04
问题 So I have two matrices, A and B, and I want to compute the min-plus product as given here: Min-plus matrix multiplication. For that I've implemented the following: def min_plus_product(A,B): B = np.transpose(B) Y = np.zeros((len(B),len(A))) for i in range(len(B)): Y[i] = (A + B[i]).min(1) return np.transpose(Y) This works fine, but is slow for big matrices, is there a way to make it faster? I've heard that implemeting in C or using the GPU might be good options. 回答1: Here is an algo that

How to make Min-plus matrix multiplication in python faster?

故事扮演 提交于 2020-04-15 04:26:00
问题 So I have two matrices, A and B, and I want to compute the min-plus product as given here: Min-plus matrix multiplication. For that I've implemented the following: def min_plus_product(A,B): B = np.transpose(B) Y = np.zeros((len(B),len(A))) for i in range(len(B)): Y[i] = (A + B[i]).min(1) return np.transpose(Y) This works fine, but is slow for big matrices, is there a way to make it faster? I've heard that implemeting in C or using the GPU might be good options. 回答1: Here is an algo that

Matrix multiplication via std::vector is 10 times slower than numpy

心已入冬 提交于 2020-04-14 08:43:08
问题 Although it is known that using nested std::vector to represent matrices is a bad idea, let's use it for now since it is flexible and many existing functions can handle std::vector . I thought, in small cases, the speed difference can be ignored. But it turned out that vector<vector<double>> is 10+ times slower than numpy.dot() . Let A and B be matrices whose size is size x size . Assuming square matrices is just for simplicity. (We don't intend to limit discussion to the square matrices case

Matrix multiplication via std::vector is 10 times slower than numpy

≯℡__Kan透↙ 提交于 2020-04-14 08:40:30
问题 Although it is known that using nested std::vector to represent matrices is a bad idea, let's use it for now since it is flexible and many existing functions can handle std::vector . I thought, in small cases, the speed difference can be ignored. But it turned out that vector<vector<double>> is 10+ times slower than numpy.dot() . Let A and B be matrices whose size is size x size . Assuming square matrices is just for simplicity. (We don't intend to limit discussion to the square matrices case

Matrix multiplication via std::vector is 10 times slower than numpy

*爱你&永不变心* 提交于 2020-04-14 08:39:49
问题 Although it is known that using nested std::vector to represent matrices is a bad idea, let's use it for now since it is flexible and many existing functions can handle std::vector . I thought, in small cases, the speed difference can be ignored. But it turned out that vector<vector<double>> is 10+ times slower than numpy.dot() . Let A and B be matrices whose size is size x size . Assuming square matrices is just for simplicity. (We don't intend to limit discussion to the square matrices case

动态规划-1620. 收集硬币

感情迁移 提交于 2020-04-08 08:47:49
2020-04-08 07:45:18 问题描述: 给定一个 n * m 个的矩阵,矩阵的每个位置有一定数量的硬币,你从 (0,0) 位置出发,每次只能 往右 或者 往下 走,当他经过某个格子的时候,可以得到这个格子上的所有硬币,当它走到第 (n-1,M-1) 位置时游戏结束,在游戏开始前,你有 ķ 次机会,可以交换某两个格子中的硬币数量中,k次机会不一定要使用完,求从(0,0)走到第(n-1,M-1)所能得到的最大的硬币数量。 样例 输入: matrix = [[9,9,9,0,0],[0,0,9,0,0],[0,0,0,0,0],[0,0,9,0,0],[9,0,9,9,9]] k = 1 输出:81 注意事项 2≤n,m≤50 0<=k<=20 0<=matrix[I][j]<=1000000 问题求解: int nums[55]; int dp[55][55][25][25]; int collectingCoins(vector<vector<int>> &matrix, int k) { // Write your code here int n = matrix.size(); int m = matrix[0].size(); memset(dp, -0x3f,sizeof(dp)); dp[0][0][0][0] = matrix[0][0], dp[0][0]

leetcode 翻转数组

假如想象 提交于 2020-04-07 20:42:13
原题点这里 搞个矩阵,翻转一下就行。题目说不占额外内存,但是我这样做了,内存也是前100%.估计是JAVA启动的时候就加载了一些东西。 看题解也都是n^2复杂度,就这样了吧。 public static void rotate(int[][] matrix) { int rows = matrix.length; int cols = matrix[0].length; int[][] oldM = new int[rows][cols]; for(int i=0;i<rows;i++){ for(int j=0;j<cols;j++){ oldM[i][j]=matrix[i][j]; } } for(int i=0;i<rows;i++){ for(int j=0;j<cols;j++){ matrix[j][cols-i-1]=oldM[i][j]; } } for(int i=0;i<rows;i++){ for(int j=0;j<cols;j++){ System.out.print(matrix[i][j]+" "); } System.out.println(" "); } } 来源: https://www.cnblogs.com/superxuezhazha/p/12655588.html

LeetCode 面试题 01.07. 旋转矩阵

妖精的绣舞 提交于 2020-04-07 15:56:20
题目 给你一幅由 N × N 矩阵表示的图像,其中每个像素的大小为 4 字节。请你设计一种算法, 将图像旋转 90 度。不占用额外内存空间能否做到? 示例 给定 矩阵 [ [1,2,3], [4,5,6], [7,8,9] ], 原地旋转输入矩阵,使其变为: [ [7,4,1], [8,5,2], [9,6,3] ] 思路:先将其写斜对角线翻折,再对每行以中点为中心翻折 [1,2,3], [1,4,7], [7,4,1], [4,5,6], ===> [2,5,8], ===> [8,5,2], [7,8,9] [3,6,9] [9,6,3] 代码 public void rotate(int[][] matrix) { for(int i=0; i<matrix.length; i++){ for(int j=i+1; j<matrix[0].length; j++){ int t = matrix[i][j]; matrix[i][j] = matrix[j][i]; matrix[j][i] = t; } } int n = matrix.length; int mid = n/2; for(int i=0; i<matrix.length; i++){ for(int j=0; j<mid; j++){ int t = matrix[i][j]; matrix[i][j]

I need to alter my Matlab Gaussian elimination program to change the form of the final matrix before back substitution [closed]

回眸只為那壹抹淺笑 提交于 2020-04-07 06:19:07
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed last month . I have made this Matlab function that performs Gaussian elimination however, I need to alter my code so that the final matrix (C) before back substitution is in the form of the linked picture named 'Required form of matrix'(where it says A = ). Right now my code performs Gaussian elimination however