Rows/columns operation in Jitter or Gen

sepulcky's icon

Hi folks!

Is there a way to do matrix operation like:

1. Multiply N×M by 1×M matrix (multiply each row value from the 1st matrix by the corresponding value from the second).
2. Calculate mean of each row.

This can be done with jit.spill/jit.fill, but I'm looking for a more FTM- or Matlab-like way on doing this for the whole matrices without doing loops on the scheduler/queue.

I checked the forum briefly and didn't find a solution.
Also I'm new to Gen and don't see a way to do this, because in Gen (as far as I understand) we make the algorithm to be evaluated on each pixel. I don't see a way to do more classical looping techniques there.

Any help is appreciated.

Best,
E.

Rob Ramirez's icon

for mean, check out jit.planeop
for matrix multiply, check out jit.la.mult

jit.gen has a sample operator, allowing you to access the values of all the pixels of the input matrix, and codebox allows you to do branching and looping. check out the GenExpr reference doc for more info.

Jesse's icon

I think for calculating the mean of each row jit.dimop would be more useful.

sepulcky's icon

jit.dimop is exactly what I needed! Thanks @jesse!

@rob, I didn't get how to use jit.la.mult for my task: I need to multiply values from each row from the first matrix by the value from the corresponding row from the second matrix. The output will be the matrix with N×M dimensions. jit.la.mult outputs 1×M.

Also, will the looping inside codebox be evaluated for each pixel?

And another question: how to calculate amount of zeros in each row?

Jesse's icon
Max Patch
Copy patch and select New From Clipboard in Max.

As long as the larger dim matrix enters the left inlet of a jit.* object it should do the multiplication as you expect.

Gussi's icon

A bit late,

but here's a simple way to count the number of zeroes per row,
using the jit.dimop object, if you still need it :-)

Max Patch
Copy patch and select New From Clipboard in Max.

Regards - Svend

Gussi's icon

Another note,

the jit.la.mult object cares about the order of the inputs,
and their respective sizes (true matrix multiplication).

A * B wont give you the same result as B * A.

if you're getting a 1xM size matrix after jit.la.mult,

try reversing the order of the matrices,
and/or consider using jit.transpose,
if you need to rotate / transpose one of the matrices,
in order to get the proper orientation before multiplication.

In other words,
if the jit.la.mult complains about "mismatch dim" in the max console,
the multiplication doesn't make sense.

Here's a couple of examples,
resulting in various output matrix sizes after jit.la.mult.

Max Patch
Copy patch and select New From Clipboard in Max.

Jit.la.mult is very useful when working with machine learning algorithms,
ie. for calculating covariance matrices,
in principal component analysis algorithms,

or when calculating basic distance matrices (self-similarity matrices).

Hope this helps

- Svend

loadmess's icon

Thank you@ @GUSSI for the enlightment !

Patrick_K's icon

@GUSSI, that's some serious wizarding. Many thanks for this quality educational material.