## Convert 3D matrix to 2D matrix (for mesh, multiple, etc)

Apr 18, 2007 at 12:12am

# Convert 3D matrix to 2D matrix (for mesh, multiple, etc)

Hi all,

I’m trying to feed a 3D matrix (NxNxN), the output of a jit.expr, as
data for jit.gl.multiple and/or jit.gl.mesh. Since neither multiple
nor mesh support 3D matrices, I’m wondering if there’s a way to
efficiently convert a 3D matrix of arbitrary size to a 2D matrix
(e.g. Nx(NxN)). I can’t figure it out.

Schema:

[bang]
|
[jit.matrix 3 float32 10 10 10]
|
[ ??? ]
|
|jit.matrix 3 float32 1000] or [jit.matrix 3 float32 10 100]
|
[jit.gl.multiple]

Thanks

grrr waaa
http://www.grahamwakefield.net

#31435
Apr 18, 2007 at 1:40am

On Apr 17, 2007, at 5:12 PM, Graham Wakefield wrote:

> I’m trying to feed a 3D matrix (NxNxN), the output of a jit.expr,
> as data for jit.gl.multiple and/or jit.gl.mesh. Since neither
> multiple nor mesh support 3D matrices, I’m wondering if there’s a
> way to efficiently convert a 3D matrix of arbitrary size to a 2D
> matrix (e.g. Nx(NxN)). I can’t figure it out.

#1, you should be able to convert your expr to a 2D expression to
begin with (thinking of the large 2D matrix as a tiling of a 3D matrix).

However, if that doesn’t work for you, you’ll probably want to use
jit.matrix and something like the following email client JS (or
patcher equivalent)…watch for typos and or other oversights in the
following as I didn’t take the time to test.

dest.dim = [width, height*depth];
dest.usesrcdim = 1;
dest.usedstdim = 1;

for (i=0;i
{
dest.srcdimstart = [0, 0, i];
dest.srcdimend = [width-1, height-1, i];
dest.dstdimstart = [0, i*height];
dest.srcdimend = [width-1, (i*height) + (height-1)];

dest.frommatrix(source);
}

Unfortunately jit.scanwrap doesn’t work for > 2 dimensions, which
would be ideal for this sort of job.

-Joshua

#102222
Apr 18, 2007 at 3:35am

Hmm, I’d like to do this efficiently. Sounds like a simple jitter
external; but my matrix SDK knowledge is rusty…

Can I simply pass the data from input to output

jit_object_method(out_matrix,_jit_sym_data,in_bp);

and change the out_minfo.dim[0] key to be equiv. in_minfo.dim[0] *
in_minfo.dim[1] * in_minfo.dim[2] etc., out_minfo.dimcount = 1, and
so on, followed by

err = (t_jit_err) jit_object_method(out_matrix, _jit_sym_setinfo_ex,
&out_minfo);

Or do I need to manually copy the data in for loops?

Thanks

On Apr 17, 2007, at 6:40 PM, Joshua Kit Clayton wrote:

>
> On Apr 17, 2007, at 5:12 PM, Graham Wakefield wrote:
>
>> I’m trying to feed a 3D matrix (NxNxN), the output of a jit.expr,
>> as data for jit.gl.multiple and/or jit.gl.mesh. Since neither
>> multiple nor mesh support 3D matrices, I’m wondering if there’s a
>> way to efficiently convert a 3D matrix of arbitrary size to a 2D
>> matrix (e.g. Nx(NxN)). I can’t figure it out.
>
> #1, you should be able to convert your expr to a 2D expression to
> begin with (thinking of the large 2D matrix as a tiling of a 3D
> matrix).
>
> However, if that doesn’t work for you, you’ll probably want to use
> jit.matrix and something like the following email client JS (or
> patcher equivalent)…watch for typos and or other oversights in
> the following as I didn’t take the time to test.
>
> dest.dim = [width, height*depth];
> dest.usesrcdim = 1;
> dest.usedstdim = 1;
>
> for (i=0;i
> {
> dest.srcdimstart = [0, 0, i];
> dest.srcdimend = [width-1, height-1, i];
> dest.dstdimstart = [0, i*height];
> dest.srcdimend = [width-1, (i*height) + (height-1)];
>
> dest.frommatrix(source);
> }
>
> Unfortunately jit.scanwrap doesn’t work for > 2 dimensions, which
> would be ideal for this sort of job.
>
> -Joshua

grrr waaa
http://www.grahamwakefield.net

#102223
Apr 18, 2007 at 3:45am

Hmm, I’d like to do this efficiently. Sounds like a simple jitter
external; but my matrix SDK knowledge is rusty…

Can I simply pass the data from input to output

jit_object_method(out_matrix,_jit_sym_data,in_bp);

and change the out_minfo.dim[0] key to be equiv. in_minfo.dim[0] *
in_minfo.dim[1] * in_minfo.dim[2] etc., out_minfo.dimcount = 1, and
so on, followed by

err = (t_jit_err) jit_object_method(out_matrix, _jit_sym_setinfo_ex,
&out_minfo);

Or do I need to manually copy the data in for loops?

Thanks

On Apr 17, 2007, at 6:40 PM, Joshua Kit Clayton wrote:

>
> On Apr 17, 2007, at 5:12 PM, Graham Wakefield wrote:
>
>> I’m trying to feed a 3D matrix (NxNxN), the output of a jit.expr,
>> as data for jit.gl.multiple and/or jit.gl.mesh. Since neither
>> multiple nor mesh support 3D matrices, I’m wondering if there’s a
>> way to efficiently convert a 3D matrix of arbitrary size to a 2D
>> matrix (e.g. Nx(NxN)). I can’t figure it out.
>
> #1, you should be able to convert your expr to a 2D expression to
> begin with (thinking of the large 2D matrix as a tiling of a 3D
> matrix).
>
> However, if that doesn’t work for you, you’ll probably want to use
> jit.matrix and something like the following email client JS (or
> patcher equivalent)…watch for typos and or other oversights in
> the following as I didn’t take the time to test.
>
> dest.dim = [width, height*depth];
> dest.usesrcdim = 1;
> dest.usedstdim = 1;
>
> for (i=0;i
> {
> dest.srcdimstart = [0, 0, i];
> dest.srcdimend = [width-1, height-1, i];
> dest.dstdimstart = [0, i*height];
> dest.srcdimend = [width-1, (i*height) + (height-1)];
>
> dest.frommatrix(source);
> }
>
> Unfortunately jit.scanwrap doesn’t work for > 2 dimensions, which
> would be ideal for this sort of job.
>
> -Joshua

grrr waaa
http://www.grahamwakefield.net

#102224
Apr 18, 2007 at 3:58am

Ok I see now that this won’t work, because of different dimstrides.
So, next Q: can I use _matrix_conv_info to do the dirty work, or do I
really need to manually write out for loops?

On Apr 17, 2007, at 8:45 PM, Graham Wakefield wrote:

> Hmm, I’d like to do this efficiently. Sounds like a simple jitter
> external; but my matrix SDK knowledge is rusty…
>
> Can I simply pass the data from input to output
>
> jit_object_method(out_matrix,_jit_sym_data,in_bp);
>
> and change the out_minfo.dim[0] key to be equiv. in_minfo.dim[0] *
> in_minfo.dim[1] * in_minfo.dim[2] etc., out_minfo.dimcount = 1, and
> so on, followed by
>
> err = (t_jit_err) jit_object_method(out_matrix,
> _jit_sym_setinfo_ex, &out_minfo);
>
> Or do I need to manually copy the data in for loops?
>
> Thanks
>
> On Apr 17, 2007, at 6:40 PM, Joshua Kit Clayton wrote:
>
>>
>> On Apr 17, 2007, at 5:12 PM, Graham Wakefield wrote:
>>
>>> I’m trying to feed a 3D matrix (NxNxN), the output of a jit.expr,
>>> as data for jit.gl.multiple and/or jit.gl.mesh. Since neither
>>> multiple nor mesh support 3D matrices, I’m wondering if there’s a
>>> way to efficiently convert a 3D matrix of arbitrary size to a 2D
>>> matrix (e.g. Nx(NxN)). I can’t figure it out.
>>
>> #1, you should be able to convert your expr to a 2D expression to
>> begin with (thinking of the large 2D matrix as a tiling of a 3D
>> matrix).
>>
>> However, if that doesn’t work for you, you’ll probably want to use
>> jit.matrix and something like the following email client JS (or
>> patcher equivalent)…watch for typos and or other oversights in
>> the following as I didn’t take the time to test.
>>
>> dest.dim = [width, height*depth];
>> dest.usesrcdim = 1;
>> dest.usedstdim = 1;
>>
>> for (i=0;i
>> {
>> dest.srcdimstart = [0, 0, i];
>> dest.srcdimend = [width-1, height-1, i];
>> dest.dstdimstart = [0, i*height];
>> dest.srcdimend = [width-1, (i*height) + (height-1)];
>>
>> dest.frommatrix(source);
>> }
>>
>> Unfortunately jit.scanwrap doesn’t work for > 2 dimensions, which
>> would be ideal for this sort of job.
>>
>> -Joshua
>
> grrr waaa
> http://www.grahamwakefield.net
>
>
>
>

grrr waaa
http://www.grahamwakefield.net

#102225
Apr 18, 2007 at 5:11am

I don’t get it. I thought that internally, the matrices are all
stored in a single array, so an N-D matrix is stored sequentially,
thus for example a [jit.matrix 3 char 2 2 2] is accessed like this:

[[[r g b] [r g b]] [[r g b] [r g b]]] [[[r g b] [r g b]] [[r g b] [r
g b]]]

but is actually stored in memory like this:

[r g b r g b r g b r g b r g b r g b r g b r g b]

Since the actual size of memory isn’t changing, only the matrix_info
interface, shouldn’t I be able to ‘change the header’ without
touching the data?

On Apr 17, 2007, at 9:07 PM, Wesley Smith wrote:

> You can use it to do the 2D slices for you, but not a oneshot
> conversion. At a minimum, you’ll need a for loop across the 3rd
> dimension.
>
> wes
>
> On 4/17/07, Graham Wakefield

wrote:
>>
>> Ok I see now that this won’t work, because of different
>> dimstrides. So,
>> next Q: can I use _matrix_conv_info to do the dirty work, or do I
>> really
>> need to manually write out for loops?
>>
>>
>> On Apr 17, 2007, at 8:45 PM, Graham Wakefield wrote:
>>
>> Hmm, I’d like to do this efficiently. Sounds like a simple jitter
>> external;
>> but my matrix SDK knowledge is rusty…
>>
>> Can I simply pass the data from input to output
>>
>> jit_object_method(out_matrix,_jit_sym_data,in_bp);
>>
>> and change the out_minfo.dim[0] key to be equiv. in_minfo.dim[0] *
>> in_minfo.dim[1] * in_minfo.dim[2] etc., out_minfo.dimcount = 1,
>> and so on,
>> followed by
>>
>> err = (t_jit_err) jit_object_method(out_matrix, _jit_sym_setinfo_ex,
>> &out_minfo);
>>
>> Or do I need to manually copy the data in for loops?
>>
>> Thanks
>>
>> On Apr 17, 2007, at 6:40 PM, Joshua Kit Clayton wrote:
>>
>>
>>
>> On Apr 17, 2007, at 5:12 PM, Graham Wakefield wrote:
>>
>>
>> I’m trying to feed a 3D matrix (NxNxN), the output of a jit.expr,
>> as data
>> for jit.gl.multiple and/or jit.gl.mesh. Since neither multiple
>> nor mesh
>> support 3D matrices, I’m wondering if there’s a way to efficiently
>> convert a
>> 3D matrix of arbitrary size to a 2D matrix (e.g. Nx(NxN)). I
>> can’t figure
>> it out.
>>
>> #1, you should be able to convert your expr to a 2D expression to
>> begin with
>> (thinking of the large 2D matrix as a tiling of a 3D matrix).
>>
>> However, if that doesn’t work for you, you’ll probably want to use
>> jit.matrix and something like the following email client JS (or
>> patcher
>> equivalent)…watch for typos and or other oversights in the
>> following as I
>> didn’t take the time to test.
>>
>> dest.dim = [width, height*depth];
>> dest.usesrcdim = 1;
>> dest.usedstdim = 1;
>>
>> for (i=0;i
>> {
>> dest.srcdimstart = [0, 0, i];
>> dest.srcdimend = [width-1, height-1, i];
>> dest.dstdimstart = [0, i*height];
>> dest.srcdimend = [width-1, (i*height) + (height-1)];
>>
>> dest.frommatrix(source);
>> }
>>
>> Unfortunately jit.scanwrap doesn’t work for > 2 dimensions, which
>> would be
>> ideal for this sort of job.
>>
>> -Joshua
>>
>> grrr waaa
>> http://www.grahamwakefield.net
>>
>>
>>
>>
>>
>> grrr waaa
>> http://www.grahamwakefield.net
>>
>>
>>
>>
>>
>>
>>

grrr waaa
http://www.grahamwakefield.net

#102227
Apr 18, 2007 at 7:43am

You mean, a matrix may have holes?

If all cells are of the same datatype, and same planecount, and row
sizes are constant between columns (etc to higher dimensions), why
would there be any holes? Or, if you’re talking about the output
matrix, since it would be a 1xM matrix only the dimstride[0] matters
(which should be sizeof(datatype)*planecount). I’m only asking
because I don’t understand. I wondered whether perhaps nD matrices
are actually sets of distinct 2D matrices and therefore not
contiguous in memory?

BTW an important point is that I do not need to worry about the order
of the cells (so long as the plane order remains constant), so
however the ‘flattening’ of the matrix re-orders them, I do not mind.
But I really would like to find a simple & efficient solution so I
can drive jit.gl.mesh (@draw_mode points) and/or jit.gl.multiple with
3D (and later n-D) matrices.

For Josh’s #1 solution, wouldn’t it be expensive to include modulus
terms into the expr for a 2-D simulation of 3-D, not to mention
making the expression itself a degree more difficult to read and
interact with? What I’m making is going to be for other people to
use, so I’d like to avoid unnecessary complications.

I tried to write something in C, but I think I would need to spend
quite a bit more time getting to know the Jitter SDK. Some kind of
recursive function call per dimension to copy each cell one by one
from source to destination, resulting in a 1xM matrix, seems the
solution, but I wouldn’t want to do this if there is a more efficient
solution.

Thanks

On Apr 17, 2007, at 10:23 PM, Wesley Smith wrote:

> Not necessarily because of the potential difference between
>
> dim*planecount*sizeof(datatype)
>
> and
>
> dimstride
>
> data from dimension to dimension isn’t guaranteed to be tightly packed
> except for within a row.
>
> wes
>
> On 4/18/07, Graham Wakefield

wrote:
>>
>> I don’t get it. I thought that internally, the matrices are all
>> stored in a
>> single array, so an N-D matrix is stored sequentially, thus for
>> example a
>> [jit.matrix 3 char 2 2 2] is accessed like this:
>>
>> [[[r g b] [r g b]] [[r g b] [r g b]]] [[[r g b] [r g b]] [[r g b]
>> [r g b]]]
>>
>> but is actually stored in memory like this:
>>
>> [r g b r g b r g b r g b r g b r g b r g b r g b]
>>
>> Since the actual size of memory isn’t changing, only the matrix_info
>> interface, shouldn’t I be able to ‘change the header’ without
>> touching the
>> data?
>>
>>
>> On Apr 17, 2007, at 9:07 PM, Wesley Smith wrote:
>>
>> You can use it to do the 2D slices for you, but not a oneshot
>> conversion. At a minimum, you’ll need a for loop across the 3rd
>> dimension.
>>
>> wes
>>
>> On 4/17/07, Graham Wakefield
wrote:
>>
>> Ok I see now that this won’t work, because of different
>> dimstrides. So,
>> next Q: can I use _matrix_conv_info to do the dirty work, or do I
>> really
>> need to manually write out for loops?
>>
>>
>> On Apr 17, 2007, at 8:45 PM, Graham Wakefield wrote:
>>
>> Hmm, I’d like to do this efficiently. Sounds like a simple jitter
>> external;
>> but my matrix SDK knowledge is rusty…
>>
>> Can I simply pass the data from input to output
>>
>> jit_object_method(out_matrix,_jit_sym_data,in_bp);
>>
>> and change the out_minfo.dim[0] key to be equiv. in_minfo.dim[0] *
>> in_minfo.dim[1] * in_minfo.dim[2] etc., out_minfo.dimcount = 1,
>> and so on,
>> followed by
>>
>> err = (t_jit_err) jit_object_method(out_matrix, _jit_sym_setinfo_ex,
>> &out_minfo);
>>
>> Or do I need to manually copy the data in for loops?
>>
>> Thanks
>>
>> On Apr 17, 2007, at 6:40 PM, Joshua Kit Clayton wrote:
>>
>>
>>
>> On Apr 17, 2007, at 5:12 PM, Graham Wakefield wrote:
>>
>>
>> I’m trying to feed a 3D matrix (NxNxN), the output of a jit.expr,
>> as data
>> for jit.gl.multiple and/or jit.gl.mesh. Since neither multiple
>> nor mesh
>> support 3D matrices, I’m wondering if there’s a way to efficiently
>> convert a
>> 3D matrix of arbitrary size to a 2D matrix (e.g. Nx(NxN)). I
>> can’t figure
>> it out.
>>
>> #1, you should be able to convert your expr to a 2D expression to
>> begin with
>> (thinking of the large 2D matrix as a tiling of a 3D matrix).
>>
>> However, if that doesn’t work for you, you’ll probably want to use
>> jit.matrix and something like the following email client JS (or
>> patcher
>> equivalent)…watch for typos and or other oversights in the
>> following as I
>> didn’t take the time to test.
>>
>> dest.dim = [width, height*depth];
>> dest.usesrcdim = 1;
>> dest.usedstdim = 1;
>>
>> for (i=0;i
>> {
>> dest.srcdimstart = [0, 0, i];
>> dest.srcdimend = [width-1, height-1, i];
>> dest.dstdimstart = [0, i*height];
>> dest.srcdimend = [width-1, (i*height) + (height-1)];
>>
>> dest.frommatrix(source);
>> }
>>
>> Unfortunately jit.scanwrap doesn’t work for > 2 dimensions, which
>> would be
>> ideal for this sort of job.
>>
>> -Joshua
>>
>> grrr waaa
>> http://www.grahamwakefield.net
>>
>>
>>
>>
>>
>> grrr waaa
>> http://www.grahamwakefield.net
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> grrr waaa
>> http://www.grahamwakefield.net
>>
>>
>>
>>
>>
>>
>>

grrr waaa
http://www.grahamwakefield.net

#102229
Apr 18, 2007 at 3:02pm

On Apr 18, 2007, at 12:43 AM, Graham Wakefield wrote:

> I wouldn’t want to do this if there is a more efficient solution.

Graham, I would suggest you try the solution I sent you, before you
worry about the fastest possible way. frommatrix is *quite* fast, and
when you have an NxNxN size matrix, N steps is a relatively small
fraction. jit.expr with anything non-trivial is going to be way more
expensive than this. Doing this in C is relatively simple if you want
to code it up. However, I’ll bet that the performance difference will
be negligible since you are working on optimizing something which is
most likely not the bottleneck.

FWIW, each row is 16 byte aligned (unless you use your own memory and
dimstrides which aren’t 16 byte aligned) which is what explains the
possible holes.

-Joshua

#102231
Apr 18, 2007 at 5:10pm

On Apr 18, 2007, at 8:02 AM, Joshua Kit Clayton wrote:

>
> On Apr 18, 2007, at 12:43 AM, Graham Wakefield wrote:
>
>> I wouldn’t want to do this if there is a more efficient solution.
>
> Graham, I would suggest you try the solution I sent you, before you
> worry about the fastest possible way. frommatrix is *quite* fast,
> and when you have an NxNxN size matrix, N steps is a relatively
> small fraction. jit.expr with anything non-trivial is going to be
> way more expensive than this.
> Doing this in C is relatively simple if you want to code it up.
> However, I’ll bet that the performance difference will be
> negligible since you are working on optimizing something which is
> most likely not the bottleneck.
>
> FWIW, each row is 16 byte aligned (unless you use your own memory
> and dimstrides which aren’t 16 byte aligned) which is what explains
> the possible holes.

Right – I will do this. Thanks for all your help, both you and Wes.

#102232
Nov 2, 2013 at 9:35pm

“Unfortunately jit.scanwrap doesn’t work for > 2 dimensions, which
would be ideal for this sort of job.”

-> i guess there is still no jit.scanwrap alternative that can work for >2 dim as one simple object?

#269981
Nov 3, 2013 at 6:36am

i want to fill 1d, 2d and 3d matrices with lists generated by a single multislider and do that with jit.fill for 1d and 2d, and a iter + setcell3d + expr combo for 3d. That works now fine for me. Still if anybody knows a faster way for 3d:

<code>

– Pasted Max Patch, click to expand. –

</code>

#270004

You must be logged in to reply to this topic.