Using jit.gl.pix and codebox to get a sum of all values within a matrix
I am trying to make a jit.gl.pix patch that gets different values from a matrix.
One of them is getting the sum of all of the values within a float32 matrix.
I have tried using the nearest() function and a for loop within codebox to iterate through all the values and sum them, thus generating a matrix the elements of which are the sum of the values.
However, this doesn't seem to work. Here's the code I've tried:
summed = 0;
for (i=0; i<dim; i+=1){
summed = nearest(in1, i);
}
out1 = summed;
I attach the patch I've been using for testing. I have included a non-gl.pix version that does what I want it to do.
i'm trying to get out of a fog lately, especially about matrix processing so i might not have much to offer... but...
2 things that might help:
-a matrix you're likely to work with in this space will have more than 1 dimension, another angle to think about this from is that 'jit.iter' has @mode for 'vertical' vs. 'horizontal' traversal... if i recall correctly, in the jit.gl.pix format, this can be handled similarly by using 'dim.y' along with 'dim.x'
-when you sum things you can use the "+=" op(not the "=" op)... so after adjusting for both dimensions properly(hint: try a nested for-loop), you would utilize something like this line:
summed += nearestpix(in1, vec(dim.x-i, dim.y-j));
//(where i, and j, are iterators in nested for-loops)
(notice i'm using 'nearestpix' because the i and j iterators will be integers you can use to decrement from the dimensions in pixels... i didn't actually test it to see if it works... i didn't want to spoil the fun, and also... just in case i turn out to be wrong about something it leaves it open to interpretation whether i was lazy or just randomly crazy 🤪)
hope it can help 🍻
Not sure what do you want to do with this, and not sure if it is the best idea to do it that way, but here is it:
I also simplified your [p sumValues] as it needed to receive the matrix twice to output the correct value.
What was missing:
A jit.world to make jit.gl.pix and asyncread to work
In jit.gl.pix, you were iterating through 1 dimension only
you were using '=' instead of '+=' as 👽'tW∆s ∆lienz👽 said
You were "summing" a vector (the value for the color of each channel - textures are always 4 channels here, even if the input was a 1 channel matrix)
The sample operation is made with normalized coordinates (between 0 and 1), not cell value. So you needed to divide the current cell value by the dimension of the texture.
There is a very small variation between both results (± 0.000005), probably because of difference between the way the GPU and the CPU deal with rounding.