Calculate the sum of all cells in a jitter matrix

juanx's icon

Hi,
I am trying to calculate the sum of all cells in a jitter matrix.
Does anyone know how to do this with jit.gen ?
Thanks

Jean-Francois Charles's icon

[jit.3m] gives you the average: multiply by the number of cells to get total sum.
or [jit.dimop @op + @step -1 -1]

juanx's icon

Thanks Jean-François.
I was already using [jit.3m].
I need this operation to calculate the euclidean distance between two matrices (not the distance from cell to cell but from the matrices as a whole).
This gets rather slow so I moved to [jit.gen].
It works much faster but I don't seem to find a way to sum all cells inside [jit.gen].

Also: I get slightly incorrect numbers working with [jit.3m].
I can't figure out why ...

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

I didn't know of this one [jit.dimop @op + @step -1 -1]
Thanks again.

Jean-Francois Charles's icon
Max Patch
Copy patch and select New From Clipboard in Max.

Very interesting. I'm not sure the incorrect result comes from jit.3m. Don't have the time right know to make a proper test, but it would be easy, using a defined matrix instead of jit.noise.
Anyway, here is a patch with 2 other solutions.

Jean-Francois Charles's icon
Max Patch
Copy patch and select New From Clipboard in Max.

And here, a patch to kind-of-test how fast each solution is. Best solution is indeed with jit.gen, but jit.op is already more than twice as fast as jit.expr.

Jean-Francois Charles's icon
Max Patch
Copy patch and select New From Clipboard in Max.

Precision problems indeed. Both jit.3m and jit.dimop solutions sometimes give false results. Are c74 listening?

juanx's icon

Yes, it's very strange, there's some precision issue.
Anyway ... I still wonder if the sum of all cells could be achieved using [jit.gen].
Any ideas in this direction would be much appreciated.

Jean-Francois Charles's icon

Given the way jit.gen works, and given the operators available, it looks like it is not possible. Remember that the output of jit.gen is a matrix with the same {size, type, planecount} as its leftmost input incoming matrix, and that gen operators are applied to all cells in a matrix. jit.gen is really like jit.expr & jit.op, (or jit.repos if you use swiz & sample). Yet, I hope someone with a deeper jit.gen knowledge will jump in with a solution!

juanx's icon

Thanks again Jean-François. That seems very reasonable.
Do you think a new topic should be created about those precision issues in [jit.3m] and [jit.dimop] ?

loadmess's icon

I was looking for this as well, and after some digging I found a solution for jit.gen, I believe. Here goes, perhaps someone else will be looking for this someday.
I tested your previous patches and apparently the precision errors still exist.

====
UPDATE:
If I use this in a more elaborate gen patch I get the following error which I don't understand how to solve:

gen_domain: jit.gen: [string "gen2.jit.Types"]:207: type is not a sampler

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