HISE Logo Forum
    • Categories
    • Register
    • Login

    Cartesian product function

    Scheduled Pinned Locked Moved Scripting
    20 Posts 3 Posters 739 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • ustkU
      ustk @d.healey
      last edited by

      @d-healey 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 😛

      Can't help pressing F5 in the forum...

      1 Reply Last reply Reply Quote 0
      • d.healeyD
        d.healey
        last edited by d.healey

        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]);
        }
        

        Libre Wave - Freedom respecting instruments and effects
        My Patreon - HISE tutorials
        YouTube Channel - Public HISE tutorials

        1 Reply Last reply Reply Quote 0
        • ustkU
          ustk
          last edited by

          @d-healey Will the inner arrays inside data have the same size?

          Can't help pressing F5 in the forum...

          d.healeyD 1 Reply Last reply Reply Quote 0
          • d.healeyD
            d.healey @ustk
            last edited by

            @ustk Not always.

            Libre Wave - Freedom respecting instruments and effects
            My Patreon - HISE tutorials
            YouTube Channel - Public HISE tutorials

            1 Reply Last reply Reply Quote 0
            • d.healeyD
              d.healey
              last edited by

              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.

              Libre Wave - Freedom respecting instruments and effects
              My Patreon - HISE tutorials
              YouTube Channel - Public HISE tutorials

              1 Reply Last reply Reply Quote 0
              • ustkU
                ustk
                last edited by

                @d-healey Ouch...
                Do you mean like:

                [
                [1, 2, 3],
                [a, b, c, d, e, f],
                [15, 16]
                ]
                

                That complicates the beast a wee bit...

                Can't help pressing F5 in the forum...

                d.healeyD 1 Reply Last reply Reply Quote 0
                • d.healeyD
                  d.healey @ustk
                  last edited by

                  @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.

                  Libre Wave - Freedom respecting instruments and effects
                  My Patreon - HISE tutorials
                  YouTube Channel - Public HISE tutorials

                  1 Reply Last reply Reply Quote 0
                  • ustkU
                    ustk
                    last edited by

                    @d-healey Found this, it may help:
                    https://rosettacode.org/wiki/Cartesian_product_of_two_or_more_lists

                    Can't help pressing F5 in the forum...

                    d.healeyD 1 Reply Last reply Reply Quote 0
                    • d.healeyD
                      d.healey @ustk
                      last edited by

                      @ustk I found that too :) I'm currently using it to make a new concat function.

                      Libre Wave - Freedom respecting instruments and effects
                      My Patreon - HISE tutorials
                      YouTube Channel - Public HISE tutorials

                      1 Reply Last reply Reply Quote 0
                      • d.healeyD
                        d.healey
                        last edited by

                        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;
                        }
                        

                        Libre Wave - Freedom respecting instruments and effects
                        My Patreon - HISE tutorials
                        YouTube Channel - Public HISE tutorials

                        ustkU 1 Reply Last reply Reply Quote 0
                        • ustkU
                          ustk @d.healey
                          last edited by

                          @d-healey Woww! Good job man! That was a hard one!
                          Could I ask what for you need it?

                          Can't help pressing F5 in the forum...

                          d.healeyD 1 Reply Last reply Reply Quote 0
                          • d.healeyD
                            d.healey @ustk
                            last edited by d.healey

                            @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

                            Peek 2019-12-22 01-48.gif

                            Libre Wave - Freedom respecting instruments and effects
                            My Patreon - HISE tutorials
                            YouTube Channel - Public HISE tutorials

                            ustkU NatanN 2 Replies Last reply Reply Quote 3
                            • ustkU
                              ustk @d.healey
                              last edited by ustk

                              @d-healey 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.

                              Can't help pressing F5 in the forum...

                              1 Reply Last reply Reply Quote 0
                              • d.healeyD
                                d.healey
                                last edited by

                                My function requires filtering too.

                                Libre Wave - Freedom respecting instruments and effects
                                My Patreon - HISE tutorials
                                YouTube Channel - Public HISE tutorials

                                1 Reply Last reply Reply Quote 0
                                • d.healeyD
                                  d.healey
                                  last edited by d.healey

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

                                  689d111f-2d1a-4899-8a4f-3e94ebabddb1-image.png

                                  Libre Wave - Freedom respecting instruments and effects
                                  My Patreon - HISE tutorials
                                  YouTube Channel - Public HISE tutorials

                                  1 Reply Last reply Reply Quote 2
                                  • NatanN
                                    Natan @d.healey
                                    last edited by

                                    @d-healey Sir David, Any Chance You Suplly The Sacles Interval Numbers?
                                    Thanks

                                    d.healeyD 1 Reply Last reply Reply Quote 0
                                    • d.healeyD
                                      d.healey @Natan
                                      last edited by

                                      @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.

                                      Libre Wave - Freedom respecting instruments and effects
                                      My Patreon - HISE tutorials
                                      YouTube Channel - Public HISE tutorials

                                      1 Reply Last reply Reply Quote 0
                                      • First post
                                        Last post

                                      17

                                      Online

                                      1.8k

                                      Users

                                      12.0k

                                      Topics

                                      104.1k

                                      Posts