## Maximum sum path in a matrix

Given a square matrix of size N X N (1 <= N <= 1000) containing positive and negative integers with absolute value not larger than 1000, we need to compute the greatest sum achievable by walking a path, starting at any cell of the matrix and always moving downwards or rightwards. Additionally we also have to find the number of times that this sum is achievable.

For example:
For the matrix
1 1 1
2 2 2
3 3 3
The maximum sum is 12 and it occurs only once

## Matrix With Produnct

Kx1 = 1.75; n = 10; \[Theta]1 = 0; \[Theta]2 = \[Pi] / 2; rmax =  2 \[Pi]; nr = 10; 

fxy[[Theta], r] := N[{Mod[[Theta] + r – Kx1 Sin[[Theta]], 2 [Pi], -[Pi]], Mod[r – Kx1 Sin[[Theta]], 2 [Pi], -[Pi]]}];

MapData1 = Flatten[Table[Join[NestList[fxy @@ # &, {[Theta]1, i rmax / nr}, n], NestList[fxy @@ # &, {[Theta]2, i rmax / nr}, n]], {i, 0, nr}], 1];

[Chi]1 = -1/2 Sqrt[(2 MapData1[[All, 2]])/(.2)] Cos[MapData1[[All, 1]]];

[Zeta] = -1/2 0.2 10;

K1 = Sin[[Zeta]] Cot[[Chi]1];

[Eta][n_] = 1/(Sqrt[2] Sqrt[Sqrt[1 + K1^2]] Sqrt[Sqrt[1 + K1^2] + K1]);

[Xi][n_] = (Sqrt[1 + K1^2] + K1)/(Sqrt[2] Sqrt[Sqrt[1 + K1^2]] Sqrt[ Sqrt[1 + K1^2] + K1]);

[Phi]1 = ArcCos[Cos[Cos[[Chi]1] Cos[[Zeta]]]];

A11[n_] = Exp[-I [Phi]1] ( [Eta][n] [Eta][n – 1] + [Xi][n] [Xi][n – 1]);

A12[n_] = Exp[-I [Phi]1] ( [Eta][n] [Xi][n – 1] – [Xi][n] [Eta][n – 1]);

A21[n_] = Exp[I [Phi]1] (- [Eta][n] [Xi][n – 1] + [Xi][n] [Eta][n – 1]);

A22[n_] = Exp[I [Phi]1] ( [Eta][n] [Eta][n – 1] + [Xi][n] [Xi][n – 1]);

Matrix[n_] = {{A11, A12}, {A21, A22}};

bigN = 100;

matrices = Map[matrix, Range[bigN]];

Apply[Dot, matrices]

## Matrix multiplication over range in $O(n)$

Let $$M$$ denote the time it takes to multiply 2 matrices, and $$Q$$ denote the number of queries.

Is it possible to create a data structure with $$O((n+Q)M)$$ pre-computation time, and can answer range matrix multiplication queries that satisfy $$l_{i-1}\le l_i$$ and $$r_{i-1}\le r_i$$ with an overall time complexity of $$O((n+Q)M)$$. I’ve been thinking a lot on how to manipulate 2 pointers to get the intended results, but haven’t come up with any approach yet. The matrices are not necessarily invertible.

## Select rows of a matrix in which entires in a column contain particular text

data={{one,a,16,2},{one,b,18,2},{one,c,28,3},{one_ave,d,45,4},{two,a,47,2},{two,b,8,6},{two,c,33,17},{two_ave,d,4,4}};

For the matrix above, I want to select all those rows in which the first column contains the text “ave” (i.e. rows 4 and 6). To get those columns containing “two_ave”…

Select[data, #[[1]] == two_ave &]

…obviously works. I was hoping replacing the “two’ with a wildcard might get the job done, but I can’t seem to get it to work. I’ve had a good search around but can’t find the answer. Can anybody help?

## How to predict the time complexity of matrix multiplication and inverse on a GPU?

Nvidia GPU can speed up the matrix manipulation greatly. I want to have a basic idea to predict the consumed time for matrix manipulation.

How can I analyze the time complexity of matrix multiplication and inverse operation on a GPU?

## Find best sequences of moves in matrix using Dynamic Programming

I tried to write a dynamic program to solve the following problem.

Given a $$n \times m$$ matrix $$A$$, select a one element per row in order to maximize the following measure $$m$$:

• Each selected element $$a_{i,j}$$ that is on the last row $$(i=n)$$ or directly above the element $$a_{i-1,j}$$ selected in the previous row contributes $$a_{i,j}$$ to $$m$$.
• Each element $$a_{i,j}$$ that is not on the last row and not directly above the element selected in the previous row contributes $$-a_{i,j}$$ to $$m$$.

For example:

Matrix: $$\begin{bmatrix} 4 & 9 & 4 \ 3 & 1 & 2 \ 3 & 5 & 8 \ \end{bmatrix}$$

Output: 16 (8 – 1 + 9) (From bottom row to top)

Another example:

Grid:

$$\begin{bmatrix} 9 & 2 & 1 & 3 \ 3 & 7 & 6 & 1 \ 1 & 2 & 3 & 9 \end{bmatrix}$$

Output: 15 (9 – 3 + 9) (From bottom row to top)

My idea is to compare between 3+3+4 and 3-1+9 and 3-2+4 and so on.

Here is my method of doing it:

int row, col; int max_score(vector<vector<int> > grid, int r, int c) {     int result = 0;     if(c > 0 && c < col - 1 && r > 0) {         result = max_score(grid, r-1, c) + grid[r-1][c];         result = max(result, max_score(grid, r-1, c-1) - grid[r-1][c-1]);         result = max(result, max_score(grid, r-1, c+1) - grid[r-1][c+1]);     }     else if(c == 0 && r > 0) {         result = max_score(grid, r-1, c) + grid[r-1][c];         result = max(result, max_score(grid, r-1, c+1) - grid[r-1][c+1]);     }     else if(c == col - 1 && r > 0) {         result = max_score(grid, r-1, c) + grid[r-1][c];         result = max(result, max_score(grid, r-1, c-1) - grid[r-1][c-1]);     }     return result; }  int main() {     int a, result;     cin >> row >> col;     vector<vector<int> > grid( row , vector<int> (col));        for (int i = 0; i < row; i++) {          for (int j = 0; j < col; j++){              cin >> a;             grid[i][j] = a;          }      }       for(int i = row - 1; i > 0; i--) {         for(int j = 0; j < col; j++) {             result = max_score(grid, i, j);         }     }      cout << result << endl;     return 0; } 

I was getting the wrong result of this. Any suggestion will help. Thank you!

## Rounding numbers in matrix over reals

Given $$n \times n$$ matrix of real numbers, such that total value in every matrix row and column is integer-valued, find a way to round every number in matrix so that row and column totals do not change.

Is it possible to round all numbers is such way? What could be an algorithm for the problem? (it looks like we should use some kind of graph that represents relation between fractional parts of the numbers)

## connected path in a matrix

The problem statement is :
A group of people is going for a fun trip to a city coordinated at (1,1). During their visit, a network started spreading all over the world. After knowing about the network, they decided to safely return to their home town coordinated at (n,m). Among all the paths from (1,1) to (n,m), some got in the contact with this network. They want to avoid networked paths and hence started calculating the total number of safe paths. Since it can take them a lot of time to find all the safe paths, so they have asked you to help.

You have been given a map in the form of a matrix of size n*m. Each coordinate represents a city on the map. You are required to find the total number of safe paths starting from the city (1,1) and ending at the city (n,m). You are allowed to move either right or down in a single move, that is, if you are at the city (x,y), then you can go to either(x+1,y) or (x,y+1) in a single move. You are not allowed to move outside the map.

A path is networked if the product of all the numbers in the path is divisible by X.

Input format

Note: Since the input matrix can be very large, therefore you are given only K coordinate’s values and remaining coordinates have values W.

The first line contains four space-separated integers n,m,k,w . The next k lines contain three space-separated integers x,y,v denoting the coordinate (x,y) has value v.

Output format

Print a single integer denoting the total number of safe paths modulo 10^9 +7.

my approach toward code:

  #include <iostream>   #include <string>  #include <limits>   #include <algorithm>   using namespace std;   // M x N matrix  #define M 5  #define N 5   // Naive recursive function to find the minimum cost to reach  // cell (m, n) from cell (0, 0) int findMinCost(int cost[M][N], int m, int n)  {   // base case   if (n == 0 || m == 0)     return INT_MAX;  // if we're at first cell (0, 0) if (m == 1 && n == 1)     return cost[0][0];  // include cost of the current cell in path and recur to find minimum // of the path from adjacent left cell and adjacent top cell. return min (findMinCost(cost, m - 1, n), findMinCost(cost, m, n - 1))             + cost[m - 1][n - 1];  }  // main function int main() { int cost[M][N] = {     { 4, 7, 8, 6, 4 },     { 6, 7, 3, 9, 2 },     { 3, 8, 1, 2, 4 },     { 7, 1, 7, 3, 7 },     { 2, 9, 8, 9, 3 } };  cout << "The minimum cost is " << findMinCost(cost, M, N);  return 0; } 

I am getting trouble on how to product the coordinate”s value after every move with having to give a proper matrix of size n*m. Somone please help me .

## How to find a rectangular matrix in Mathematica?

Let

    A={{729, -243, 243, 243, 243, 243}, {-243, 729, 243, 243, 243,    243}, {243, 243, 729, -243, 243, 243}, {243, 243, -243, 729, 243,    243}, {243, 243, 243, 243, 729, -243}, {243, 243, 243, 243, -243,    729}} 

Could anyone please help me use Mathematica to find a $$6\times 4$$ matrix B, such that $$BB^{T}=A$$? Many thanks.

## Using the elements of one Matrix to form a new Matrix with specified rules

Given a matrix [a], how to get matrices [b] and [c] based on the following two rules?

1. rule [a]->[b]: Strike out corresponding term in [a] and take product of the remaining two terms in the same column.
2. rule [a]->[c]: Strike out the row and column containing the corresponding term in [a] and take sum of cross products in the 2×2 matrix remaining.

x,y,z can be replaced with 1,2,3; For example, $$a_{xy},a_{yz}$$ can be replaced with a12,a23; [a] can be replace with:

a = {{a11, a12, a13}, {a21, a22, a23}, {a31, a32, a33}} 

Thank you

Matrix [a]

Matrix [b]

Matrix [c]