How to compute the elements of a matrix

Hello: I have 2D matrix of floats M 

0 0 0 0 1.5625

0 0 0 0 3.125

0 0 0 0 6.25

0 0 0 0 12.5

0 0 0 0 25

3.125 6.25 12.5 25 50

I could compute the last row and the last column using q:

M[r-1;]:{2*x}[c-1;M[r-1;0]]

M[;c-1]:reverse({0.5*x}[r-1;M[r-1;c-1]])

But now I have to compute the rest of the cells backwards by rows or by columns as each cell is the sum of the cells in the same row or lower rows of the next column  minus the sum of the cells in lower rows of the same column divided by 2:

M[4,3]<- 0.5*(Sum(M[4,4],M[5,4])-Sum(M[5,3]))

M[3,3]<- 0.5*(Sum(M[3,4],M[4,4],M[5,4])-Sum(M[4,3],M[5,3]))

c is the number columns (5) and r is the number of rows (6) in the matrix but c and r are variable

This is too advanced for me. Please can you help me giving me a hint?

Cheers

Francisco

If I’m not wrong this should be the final value of M:

12.30469 10.9375 8.203125 4.6875 1.5625

13.67188  13.67188 11.71875 7.8125 3.125

13.67188 15.625 15.625 12.5 6.25

11.71875 15.625 18.75     18.75    12.5

7.8125 12.5 18.75 25 25

3.125 6.25 12.5 25 50


Cheers


Francisco

One way is to calculate values column wise starting from last column.

// matrix with last row and last column values

q)  m:(0 0 0 0 1.5625f;0 0 0 0 3.125f;0 0 0 0 6.25f;0 0 0 0 12.5f;0 0 0 0 25f;3.125 6.25 12.5 25 50f)


q)f:{reverse y {x,0.5*y-sum x}/1_sums reverse x}


q) c:5 // total columnsp.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000}span.s1 {font-variant-ligatures: no-common-ligatures}

q) flip reverse enlist[a],(a:m[;c-1]) f\1_reverse last m

Output:

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000}span.s1 {font-variant-ligatures: no-common-ligatures}

12.30469 10.9375  8.203125 4.6875 1.5625

13.67188 13.67188 11.71875 7.8125 3.125 

13.67188 15.625   15.625   12.5   6.25  

11.71875 15.625   18.75    18.75  12.5  

7.8125   12.5     18.75    25     25    

3.125    6.25     12.5     25     50 

On Wednesday, 7 August 2019 13:41:03 UTC+1, Francisco wrote:

If I’m not wrong this should be the final value of M:

12.30469 10.9375 8.203125 4.6875 1.5625

13.67188  13.67188 11.71875 7.8125 3.125

13.67188 15.625 15.625 12.5 6.25

11.71875 15.625 18.75     18.75    12.5

7.8125 12.5 18.75 25 25

3.125 6.25 12.5 25 50


Cheers


Francisco

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000}span.s1 {font-variant-ligatures: no-common-ligatures} p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000}span.s1 {font-variant-ligatures: no-common-ligatures}

I found it easier to think about it by going from top left instead of bottom right. So here’s another way, not very optimized, but

f:{(1#x){x,enlist{y+.5*z+x}[0;y;0^prev[s]-s:sum x]}/1_x}

then you can just invert the input/result (eg invert:{reverse each reverse x})

eg. 

invert f invert input

Cheers 

Ryan 

Thanks a lot!

Hi, I have another version which does it all in the one function:

q)f:{{[x;c;r]x[c;r]:0.5*(sum x[c+til count-c;r+1])-sum x[(c+1)+til(count-(c+1));r];x}/[x;reverse raze n1#/:til n;reverse(n*n1)#til n1:-1+n:-1+count x]}

q)f M

12.30469 10.9375 8.203125 4.6875 1.5625

13.67188 13.67188 11.71875 7.8125 3.125

13.67188 15.625 15.625 12.5 6.25

11.71875 15.625 18.75 18.75 12.5

7.8125 12.5 18.75 25 25

3.125 6.25 12.5 25 50

q)\ts f M

0 1568

It applies your formula to a given cell in M (in this case, starting from 4,3)and returns a copy of the matrix with that value calculated, and then applies the function again on this copy of the matrix using the x,y values of the cell above, until all x,y values passed to the function are filled.

Another version…

q)m:6 5#0f ; m[5]:3.125 6.25 12.5 25 50 ; m[;4]:1.5625 3.125 6.25 12.5 25 50q)q)m0 0 0 0 1.56250 0 0 0 3.1250 0 0 0 6.250 0 0 0 12.50 0 0 0 253.125 6.25 12.5 25 50q)q)q)g:(flip reverse@'reverse@)q)q)f:{{y,.5*(sums[x]count y)-sum y}[x]/[-1+count x;first y]}q)q)g f\[g m]12.30469 10.9375 8.203125 4.6875 1.562513.67188 13.67188 11.71875 7.8125 3.12513.67188 15.625 15.625 12.5 6.2511.71875 15.625 18.75 18.75 12.57.8125 12.5 18.75 25 253.125 6.25 12.5 25 50

Jason