Cartesian product function

Anyone got one that I can use in HISE, nonrealtime of course.

@dhealey Does this can help somehow?
HiseSnippet 802.3ocsUEtSZDDDdWjqof0lZRe.1vuv.kbXs1lPapUQaHspjh0zDCwrr2hr3wtj81yJw3aae.5aP6r2gbG0SiRRWRHLey2b7MyMyrs0JFOHPoQ3BGMYLGgelSmIRyfcFPERTqlH7yc1mFX3ZRLz1SFSCB3dHLdoOaAvExihN+9iaS8oRFOABgNVIX7uJFILIns25KBe+8nd7iDiRwdisZwTxcT9pPPOK43hFSYmSOie.0RKmCB+jc8DFktigZ3A.msUdS5LP8SYL+iEAhd9bqQcTG3AECi1Yfv2q8M4Z.Bgy2NIyWJNyeoy9BOwL7jJvKhbPRhHcM.madIkeNIU+tjzdJeO6C3NjGNk7xGKuUc5vzhwlDOVssrSKI7xoOEJ6okULWTtegc1QALjlZinmy2SCFyhn7lttUIuw0csFqTDnEn740X9bptrEw9AdgDXHWP0DJ4CjSpWkrd2Fog6YgK4oNqTURIF0Tpai4iiQ0PgQPkVhwNIvYkhVmBuKAX2Hz9JMorHxjHHumPq4ykmYF.VUpr1U2Dl86HpCioNDn1aF0gITSnaOyjwIv+YWqVnmH5Vkz6jgcsp51Q.7pToQj40Eu1JvPIyHTRhRdfxvOTVdshWUr.3i7ut52OSe1WEZkuOWmoa63f99BrrLbTOttJTX8C4yHB8Hy2Dt7CqIjE2YjhnR1RJLGNlO0NoM05KNquwqKZVxlFJIGSiFkZYPKA51SAnoctvu9dqlTC0NXLEC3MlqMBa1haxu.1xDOlTvoIO3biZLrm4VyPvjrxKzmZle71tGapCnbO2bjcVQFHLSRum6QLy6l4Le8rx1GnbW0osvvFjsdykgdgp1+a8NcC5JN61uOmYRDadm89whtt7QHkuoBMB4Y6SMZwkHryAgi5.Whv3fRjRtO7G4fyY6nhscs11JSGtzKx3OvYpy5Va7Tm0uwIZDkoUmxhaas6neZDBnIYz0SEf6IAaRcTzvIDmiaMWzH3piSYLao3Uf1yNl0WfXd8BDyFKPLuYAhYyEHl2t.w7t6MF6s1eJznFEOl..s2MZMBFuqjBcYQcjn+BDRxXFB

Thanks, I need it to work with an unknown number of arrays but I think this gets me in the right direction.

@dhealey said in Cartesian product function:
Thanks, I need it to work with an unknown number of arrays but I think this gets me in the right direction.
I was sure of that, damn it

I feel I'm getting close with this but I'm missing something. Any ideas?
const var data = [ [1, 2, 3], ["dog", "cat", "fish"] ]; const var cp = cartesianProduct(data); inline function cartesianProduct(array) { local i; local j; local k; local result = [[]]; for (i = 0; i < array.length; i++) { local subArray = array[i]; local temp = []; for (j = 0; j < result.length; j++) { for (k = 0; k < subArray.length; k++) { concatArrays(result[j], [subArray[k]]); temp.push(result[j]); } } result = temp; } return result; } inline function concatArrays(a, b) { local i; for (i = 0; i < b.length; i++) a.push(b[i]); }

@dhealey Will the inner arrays inside
data
have the same size? 
@ustk Not always.

I just did a test in jsfiddle using the proper javascript concat function
temp.push(result[j].concat(array[i][k]));
and it worked. So I think it's something to do with the way I've implemented my concat function in HISE. 
@dhealey Ouch...
Do you mean like:[ [1, 2, 3], [a, b, c, d, e, f], [15, 16] ]
That complicates the beast a wee bit...

@ustk Yeah like that.
My function works in normal JS once I swap out the concat function. You can see it here  https://jsfiddle.net/aktLe5d9/  open your developer tools console (F12) to see the output.

@dhealey Found this, it may help:
https://rosettacode.org/wiki/Cartesian_product_of_two_or_more_lists 
@ustk I found that too I'm currently using it to make a new concat function.

Yippie, I got it working.
const var data = [ [1, 2, 3], ["dog", "cat", "fish"] ]; const var cp = cartesianProduct(data); inline function cartesianProduct(array) { local i; local j; local k; local result = [[]]; for (i = 0; i < array.length; i++) { local subArray = array[i]; local temp = []; for (j = 0; j < result.length; j++) { for (k = 0; k < subArray.length; k++) { local c = concatArrays(result[j], [subArray[k]]); temp.push(c); } } result = temp; } return result; } inline function concatArrays(a, b) { local c = []; local i; for (i = 0; i < a.length; i++) { c[i] = a[i]; } for (i = 0; i < b.length; i++) { c.push(b[i]); } return c; }

@dhealey Woww! Good job man! That was a hard one!
Could I ask what for you need it? 
@ustk I'm trying to find ways to generate playable guitar chords. I came up with one method already but I was looking for a more efficient way and found this article which uses the cartesian product.
I just tried it in HISE but I'm getting an execution time out error. I think it's quite a slow method to implement in HISE so I'll stick with my existing function for now. Still I'm sure it will come in useful for other things in the future.
This is how it looks so far

@dhealey Very interesting solution indeed, although I thought there was a simpler solution (just saying, I don't know how...)
Especially since with the cartesian product solution, you have a lot of filtering to implement, apparently. 
My function requires filtering too.

I realised I'd made a mistake implementing Pete Corey's method so I'm attempting it again.

@dhealey Sir David, Any Chance You Suplly The Sacles Interval Numbers?
Thanks 
@Natanr Not sure what you mean. In the image above it's just a C major chord, so the intervals would be 0, 4, and 7.