## Compute nabla

This is the place for queries that don't fit in any of the other categories.

### Compute nabla

Hi everyone,

I'm not familiar with Python.
Could anyone help me to convert the following 2 functions into Matlab or languages like C#, C++ or pascal?

Code: Select all
`def nabla(I):    h, w = I.shape    G = np.zeros((h, w, 2), I.dtype)    G[:, :-1, 0] -= I[:, :-1]    G[:, :-1, 0] += I[:, 1:]    G[:-1, :, 1] -= I[:-1]    G[:-1, :, 1] += I[1:]    return Gdef nablaT(G):    h, w = G.shape[:2]    I = np.zeros((h, w), G.dtype)    # note that we just reversed left and right sides    # of each line to obtain the transposed operator    I[:, :-1] -= G[:, :-1, 0]    I[:, 1: ] += G[:, :-1, 0]    I[:-1]    -= G[:-1, :, 1]    I[1: ]    += G[:-1, :, 1]    return I`

Thanks a lot!
Last edited by Mekire on Sun Mar 30, 2014 1:21 pm, edited 1 time in total.
Reason: Lock.
dalspenguin

Posts: 3
Joined: Sun Mar 30, 2014 1:14 pm

### Python syntacsis

Hi everyone,

Could anyone explain what the following line means?
Code: Select all
`I[:, :-1] -= G[:, :-1, 0];`

?

Thanks a lot!
Last edited by stranac on Sun Mar 30, 2014 3:15 pm, edited 1 time in total.
Reason: Merged topics
dalspenguin

Posts: 3
Joined: Sun Mar 30, 2014 1:14 pm

### Re: Compute nabla

In your specific case(numpy), it takes all but the last element from each row of I, and replaces if with the first element of the matching list in G.
It's basically equivalent to:
Code: Select all
`for i in range(len(I)):    for j in range(len(I[0]) - 1):        I[i][j] = G[i][j][0]`
Friendship is magic!

R.I.P. Tracy M. You will be missed.

stranac

Posts: 1790
Joined: Thu Feb 07, 2013 3:42 pm

### Re: Compute nabla

stranac wrote:In your specific case(numpy), it takes all but the last element from each row of I, and replaces if with the first element of the matching list in G.
It's basically equivalent to:
Code: Select all
`for i in range(len(I)):    for j in range(len(I[0]) - 1):        I[i][j] = G[i][j][0]`

1. Are you sure that -= in expression I[:, :-1] -= G[:, :-1, 0]; is equal to I[i][j] = G[i][j][0] ?
And how it different from G[:, :-1, 0] += I[:, 1:] ?

2. If we fill the I massive from G[][][0], what these two lines do?
I[:-1] -= G[:-1, :, 1]
I[1: ] += G[:-1, :, 1]
Replace the same elemenet again?

Sorry, however I'm dummy in python....

Thanks a lot!
dalspenguin

Posts: 3
Joined: Sun Mar 30, 2014 1:14 pm

### Re: Compute nabla

It doesn't replace, it subtracts, and similarly, the part with += adds.

So yeah, there would be a -= there, instead of =
Friendship is magic!

R.I.P. Tracy M. You will be missed.

stranac

Posts: 1790
Joined: Thu Feb 07, 2013 3:42 pm

### Re: Compute nabla

Code: Select all
`I[:-1] -= G[:-1, :, 1]I[1: ] += G[:-1, :, 1]`

Those lines are very similar to those above them.

The only thing that's different is I is only indexed by a single index.
If I tell you that I[:-1] is the same as I[:-1, :], I'm sure you can figure out what those do.

Numpy calls this extended slicing, if you want to research exactly how it works.
Friendship is magic!

R.I.P. Tracy M. You will be missed.

stranac

Posts: 1790
Joined: Thu Feb 07, 2013 3:42 pm