How to define a iterative function involving for loop where the iterator i,j,l.. are also used in the defined...












0














I'm trying to use a recursive function to simplify below 'for-loops' for any number of loops that I may have to run the program based on the requirement. I can define a recursive function without the use of i, j, k, l.... in the function but I have no idea or I'm having difficulty in defining a iterative function with the use of i,j k...
I would be glad if someone could aid my imagination a little bit on it.



adjcent_spin_product_matrix = 
z_partial = 0
spin = [-1, 1]
for i in spin:
for j in spin:
for k in spin:
for l in spin:
for m in spin:
for o in spin:
for p in spin:
adjcent_spin_product_matrix = adjcent_spin_product_matrix + [i*j+ j*k+ k*l+ l*n+ m*o+ o*p]









share|improve this question
























  • I really don't understand this. For one thing, spin is a list of only 2 elements, so referring to spin[3] etc. will crash your script with an error. And despite all those loops defining loop variables i up to p, you're only ever using i, j and k. No-one's going to be able to help you simplify this code if you can't provide a working version, or we can't understand what it is you're trying to do.
    – Robin Zigmond
    Nov 12 '18 at 9:47










  • But now that makes even less sense. Why are you iterating over the same values so many times?
    – Daniel Roseman
    Nov 12 '18 at 9:54






  • 2




    The problem I'm trying to solve asks to get a matrix of adjacent spin products and the final function needs to use the i, j, k... to describe the matrix. so, for each spin pair[-1, 1] there is one for-loop and the number of for loops depends on the number of lattice points.
    – bitbitbitter
    Nov 12 '18 at 10:01






  • 2




    Its just spin, not spin[3] etc.. I re-edited the code. Basically, I want to condense all the for-loops to a recursive function and I would also like to use the i, j, k.... from each for-loop in writing final function. So, I want you to understand that i, j, k, l...... play part in the code.
    – bitbitbitter
    Nov 12 '18 at 10:02






  • 1




    As others have remarked your question is not the clearest ever posed... I have posted an answer and I'm confident that its current version is to the point, but not 100% (nor 95% ;-) sure that it's exactly what you've asked for. Could you please tell me, is it OK or I'd better remove it? thanks
    – gboffi
    Nov 12 '18 at 11:44
















0














I'm trying to use a recursive function to simplify below 'for-loops' for any number of loops that I may have to run the program based on the requirement. I can define a recursive function without the use of i, j, k, l.... in the function but I have no idea or I'm having difficulty in defining a iterative function with the use of i,j k...
I would be glad if someone could aid my imagination a little bit on it.



adjcent_spin_product_matrix = 
z_partial = 0
spin = [-1, 1]
for i in spin:
for j in spin:
for k in spin:
for l in spin:
for m in spin:
for o in spin:
for p in spin:
adjcent_spin_product_matrix = adjcent_spin_product_matrix + [i*j+ j*k+ k*l+ l*n+ m*o+ o*p]









share|improve this question
























  • I really don't understand this. For one thing, spin is a list of only 2 elements, so referring to spin[3] etc. will crash your script with an error. And despite all those loops defining loop variables i up to p, you're only ever using i, j and k. No-one's going to be able to help you simplify this code if you can't provide a working version, or we can't understand what it is you're trying to do.
    – Robin Zigmond
    Nov 12 '18 at 9:47










  • But now that makes even less sense. Why are you iterating over the same values so many times?
    – Daniel Roseman
    Nov 12 '18 at 9:54






  • 2




    The problem I'm trying to solve asks to get a matrix of adjacent spin products and the final function needs to use the i, j, k... to describe the matrix. so, for each spin pair[-1, 1] there is one for-loop and the number of for loops depends on the number of lattice points.
    – bitbitbitter
    Nov 12 '18 at 10:01






  • 2




    Its just spin, not spin[3] etc.. I re-edited the code. Basically, I want to condense all the for-loops to a recursive function and I would also like to use the i, j, k.... from each for-loop in writing final function. So, I want you to understand that i, j, k, l...... play part in the code.
    – bitbitbitter
    Nov 12 '18 at 10:02






  • 1




    As others have remarked your question is not the clearest ever posed... I have posted an answer and I'm confident that its current version is to the point, but not 100% (nor 95% ;-) sure that it's exactly what you've asked for. Could you please tell me, is it OK or I'd better remove it? thanks
    – gboffi
    Nov 12 '18 at 11:44














0












0








0


3





I'm trying to use a recursive function to simplify below 'for-loops' for any number of loops that I may have to run the program based on the requirement. I can define a recursive function without the use of i, j, k, l.... in the function but I have no idea or I'm having difficulty in defining a iterative function with the use of i,j k...
I would be glad if someone could aid my imagination a little bit on it.



adjcent_spin_product_matrix = 
z_partial = 0
spin = [-1, 1]
for i in spin:
for j in spin:
for k in spin:
for l in spin:
for m in spin:
for o in spin:
for p in spin:
adjcent_spin_product_matrix = adjcent_spin_product_matrix + [i*j+ j*k+ k*l+ l*n+ m*o+ o*p]









share|improve this question















I'm trying to use a recursive function to simplify below 'for-loops' for any number of loops that I may have to run the program based on the requirement. I can define a recursive function without the use of i, j, k, l.... in the function but I have no idea or I'm having difficulty in defining a iterative function with the use of i,j k...
I would be glad if someone could aid my imagination a little bit on it.



adjcent_spin_product_matrix = 
z_partial = 0
spin = [-1, 1]
for i in spin:
for j in spin:
for k in spin:
for l in spin:
for m in spin:
for o in spin:
for p in spin:
adjcent_spin_product_matrix = adjcent_spin_product_matrix + [i*j+ j*k+ k*l+ l*n+ m*o+ o*p]






python list function loops iterator






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 15:02









nick

216




216










asked Nov 12 '18 at 9:44









bitbitbitterbitbitbitter

133110




133110












  • I really don't understand this. For one thing, spin is a list of only 2 elements, so referring to spin[3] etc. will crash your script with an error. And despite all those loops defining loop variables i up to p, you're only ever using i, j and k. No-one's going to be able to help you simplify this code if you can't provide a working version, or we can't understand what it is you're trying to do.
    – Robin Zigmond
    Nov 12 '18 at 9:47










  • But now that makes even less sense. Why are you iterating over the same values so many times?
    – Daniel Roseman
    Nov 12 '18 at 9:54






  • 2




    The problem I'm trying to solve asks to get a matrix of adjacent spin products and the final function needs to use the i, j, k... to describe the matrix. so, for each spin pair[-1, 1] there is one for-loop and the number of for loops depends on the number of lattice points.
    – bitbitbitter
    Nov 12 '18 at 10:01






  • 2




    Its just spin, not spin[3] etc.. I re-edited the code. Basically, I want to condense all the for-loops to a recursive function and I would also like to use the i, j, k.... from each for-loop in writing final function. So, I want you to understand that i, j, k, l...... play part in the code.
    – bitbitbitter
    Nov 12 '18 at 10:02






  • 1




    As others have remarked your question is not the clearest ever posed... I have posted an answer and I'm confident that its current version is to the point, but not 100% (nor 95% ;-) sure that it's exactly what you've asked for. Could you please tell me, is it OK or I'd better remove it? thanks
    – gboffi
    Nov 12 '18 at 11:44


















  • I really don't understand this. For one thing, spin is a list of only 2 elements, so referring to spin[3] etc. will crash your script with an error. And despite all those loops defining loop variables i up to p, you're only ever using i, j and k. No-one's going to be able to help you simplify this code if you can't provide a working version, or we can't understand what it is you're trying to do.
    – Robin Zigmond
    Nov 12 '18 at 9:47










  • But now that makes even less sense. Why are you iterating over the same values so many times?
    – Daniel Roseman
    Nov 12 '18 at 9:54






  • 2




    The problem I'm trying to solve asks to get a matrix of adjacent spin products and the final function needs to use the i, j, k... to describe the matrix. so, for each spin pair[-1, 1] there is one for-loop and the number of for loops depends on the number of lattice points.
    – bitbitbitter
    Nov 12 '18 at 10:01






  • 2




    Its just spin, not spin[3] etc.. I re-edited the code. Basically, I want to condense all the for-loops to a recursive function and I would also like to use the i, j, k.... from each for-loop in writing final function. So, I want you to understand that i, j, k, l...... play part in the code.
    – bitbitbitter
    Nov 12 '18 at 10:02






  • 1




    As others have remarked your question is not the clearest ever posed... I have posted an answer and I'm confident that its current version is to the point, but not 100% (nor 95% ;-) sure that it's exactly what you've asked for. Could you please tell me, is it OK or I'd better remove it? thanks
    – gboffi
    Nov 12 '18 at 11:44
















I really don't understand this. For one thing, spin is a list of only 2 elements, so referring to spin[3] etc. will crash your script with an error. And despite all those loops defining loop variables i up to p, you're only ever using i, j and k. No-one's going to be able to help you simplify this code if you can't provide a working version, or we can't understand what it is you're trying to do.
– Robin Zigmond
Nov 12 '18 at 9:47




I really don't understand this. For one thing, spin is a list of only 2 elements, so referring to spin[3] etc. will crash your script with an error. And despite all those loops defining loop variables i up to p, you're only ever using i, j and k. No-one's going to be able to help you simplify this code if you can't provide a working version, or we can't understand what it is you're trying to do.
– Robin Zigmond
Nov 12 '18 at 9:47












But now that makes even less sense. Why are you iterating over the same values so many times?
– Daniel Roseman
Nov 12 '18 at 9:54




But now that makes even less sense. Why are you iterating over the same values so many times?
– Daniel Roseman
Nov 12 '18 at 9:54




2




2




The problem I'm trying to solve asks to get a matrix of adjacent spin products and the final function needs to use the i, j, k... to describe the matrix. so, for each spin pair[-1, 1] there is one for-loop and the number of for loops depends on the number of lattice points.
– bitbitbitter
Nov 12 '18 at 10:01




The problem I'm trying to solve asks to get a matrix of adjacent spin products and the final function needs to use the i, j, k... to describe the matrix. so, for each spin pair[-1, 1] there is one for-loop and the number of for loops depends on the number of lattice points.
– bitbitbitter
Nov 12 '18 at 10:01




2




2




Its just spin, not spin[3] etc.. I re-edited the code. Basically, I want to condense all the for-loops to a recursive function and I would also like to use the i, j, k.... from each for-loop in writing final function. So, I want you to understand that i, j, k, l...... play part in the code.
– bitbitbitter
Nov 12 '18 at 10:02




Its just spin, not spin[3] etc.. I re-edited the code. Basically, I want to condense all the for-loops to a recursive function and I would also like to use the i, j, k.... from each for-loop in writing final function. So, I want you to understand that i, j, k, l...... play part in the code.
– bitbitbitter
Nov 12 '18 at 10:02




1




1




As others have remarked your question is not the clearest ever posed... I have posted an answer and I'm confident that its current version is to the point, but not 100% (nor 95% ;-) sure that it's exactly what you've asked for. Could you please tell me, is it OK or I'd better remove it? thanks
– gboffi
Nov 12 '18 at 11:44




As others have remarked your question is not the clearest ever posed... I have posted an answer and I'm confident that its current version is to the point, but not 100% (nor 95% ;-) sure that it's exactly what you've asked for. Could you please tell me, is it OK or I'd better remove it? thanks
– gboffi
Nov 12 '18 at 11:44












3 Answers
3






active

oldest

votes


















1














No particular need for a recursive function.



You can use a single loop for every number of indices if you use itertools.product; you can simplify the computation too, using sum and zip



from itertools import product
...
spin_products =
n = int(input('how many indices? '))
...
for indices in product([-1, 1], repeat=n):
spin_products.append(sum(i*j for i, j in zip(indices, indices[1:])))





share|improve this answer



















  • 2




    Of course loops containing list.append(...) can usually be changed to a list comprehension and this is no exception, ... = [sum(i*j for i, j in zip(ix, ix[1:])) for ix in product((-1,1), repeat=n)]
    – gboffi
    Nov 12 '18 at 12:15






  • 2




    zip(indices, indices[1:]) : That's very clever logic!!
    – bitbitbitter
    Nov 13 '18 at 18:44






  • 2




    How did you come up with that? If you may walk me through your thought process in arriving at that logic because for me it wasn't immediately obvious I had to write everything down and look at the pattern to come to a realization that that can be done.
    – bitbitbitter
    Nov 13 '18 at 18:48








  • 3




    Pattern recognition maybe? ① any nested loops with code only in the deepest level is a pattern that suggests itertools.product - your case was particularly simple because the same iterable was repeated n-times - and ② the actual computation is a sum of products of pairs, hence sum and zip that's the Python's way to create pairs of values.
    – gboffi
    Nov 13 '18 at 22:22






  • 3




    And, zip(a_list, a_list[1:]) is THE idiom to get consecutive pairs from a list.
    – gboffi
    Nov 13 '18 at 22:28



















2














My earlier answer was against your unedited post, so here's an update:



From the code you've provided, it's clear that you must have at least the loops for i, and j.



Evaluating that loop on it's own gives the following list:
[1, -1, -1, 1]



Now, each subsequent nested loop in your coded merely duplicates each element in this list, and then adds the array (repeated twice) from the previous loop:



The i, j, and k loops give [1, 1, -1, -1, -1, -1, 1, 1] + [1, -1, -1,
1, 1, -1, -1, 1] = [2, 0, -2, 0, 0, -2, 0, 2]



The i, j, k, and l loops give [1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1] + [1, 1, -1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, 1, 1] + [1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1] = [3, 1, -1, 1, -1, -3, -1, 1, 1, -1, -3, -1, 1, -1, 1, 3]



And so on...



Therefore, you could simply try something like the following:



adjacent_spin_product_matrix = 
ij_result = [1, -1, -1, 1]

def calc_product(adjacent_spin_product_matrix, number_of_indices):
if number_of_indices == 2:
return ij_result
else:
number_of_duplicates = 2 ** (number_of_indices - 2)
curr_array =
for elem in ij_result:
for dup in range(number_of_duplicates):
curr_array.append(elem)
prev_array = calc_product(adjacent_spin_product_matrix, number_of_indices - 1)
temp_array =
temp_array.extend(prev_array)
temp_array.extend(prev_array)
result =
for i, elem in enumerate(curr_array):
result.append(elem + temp_array[i])

return result


Now, you should find that:



print(calc_product(, 2))  # equivalent to i and j case
print(calc_product(, 3)) # equivalent to i, j, and k case
print(calc_product(, 4)) # equivalent to i, j, k, and l case
print(calc_product(, 5)) # equivalent to i, j, k, l, and m case
print(calc_product(, 6)) # equivalent to i, j, k, l, m, and o case
print(calc_product(, 7)) # equivalent to i, j, k, l, m, o, and p case


These give the expected results:



[1, -1, -1, 1]
[2, 0, -2, 0, 0, -2, 0, 2]
[3, 1, -1, 1, -1, -3, -1, 1, 1, -1, -3, -1, 1, -1, 1, 3]
[4, 2, 0, 2, 0, -2, 0, 2, 0, -2, -4, -2, 0, -2, 0, 2, 2, 0, -2, 0, -2, -4, -2, 0, 2, 0, -2, 0, 2, 0, 2, 4]
[5, 3, 1, 3, 1, -1, 1, 3, 1, -1, -3, -1, 1, -1, 1, 3, 1, -1, -3, -1, -3, -5, -3, -1, 1, -1, -3, -1, 1, -1, 1, 3, 3, 1, -1, 1, -1, -3, -1, 1, -1, -3, -5, -3, -1, -3, -1, 1, 3, 1, -1, 1, -1, -3, -1, 1, 3, 1, -1, 1, 3, 1, 3, 5]
[6, 4, 2, 4, 2, 0, 2, 4, 2, 0, -2, 0, 2, 0, 2, 4, 2, 0, -2, 0, -2, -4, -2, 0, 2, 0, -2, 0, 2, 0, 2, 4, 2, 0, -2, 0, -2, -4, -2, 0, -2, -4, -6, -4, -2, -4, -2, 0, 2, 0, -2, 0, -2, -4, -2, 0, 2, 0, -2, 0, 2, 0, 2, 4, 4, 2, 0, 2, 0, -2, 0, 2, 0, -2, -4, -2, 0, -2, 0, 2, 0, -2, -4, -2, -4, -6, -4, -2, 0, -2, -4, -2, 0, -2, 0, 2, 4, 2, 0, 2, 0, -2, 0, 2, 0, -2, -4, -2, 0, -2, 0, 2, 4, 2, 0, 2, 0, -2, 0, 2, 4, 2, 0, 2, 4, 2, 4, 6]





share|improve this answer



















  • 2




    "Now, each subsequent nested loop in your coded merely duplicates each element in this list, and then adds the array (repeated twice) from the previous loop" can you elaborate more on this statement? what do you mean adds the array repeated twice from previous loop?
    – bitbitbitter
    Nov 12 '18 at 13:06






  • 1




    Certainly. If you look at the output from the iteration with only 2 elements (i and j), it is [1, -1, -1,1] I create the output for the 3-element iteration as follows: 1) create an array by duplicating every element from the previous (2-element) array. This gives me [1, 1, -1, -1, -1, -1, 1, 1]. 2) create another temporary array by concatenating the previous (2-element) array to itself. This produces [1, -1, -1, 1] + [1, -1, -1, 1] = [1, -1, -1, 1, 1, -1, -1, 1] 3) Add the temporary arrays from steps 1 and 2 together to get [2, 0, -2, 0, 0, -2, 0, 2].
    – DatHydroGuy
    Nov 12 '18 at 13:28






  • 1




    The above can be repeated when creating the 4-element array: 1) Take [1, -1, -1, 1], and repeat every element 4 times: [1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1] 2) Take [1, -1, -1, 1] and repeat every element twice: [1, 1, -1, -1, -1, -1, 1, 1] 3) Concatenate the array from step 2 to itself: [1, 1, -1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, 1, 1]. 4) Concatenate [1, -1, -1,1] to itself 4 times: [1, -1, -1,1, 1, -1, -1,1, 1, -1, -1,1, 1, -1, -1,1] 5) Add the arrays from 1, 3, and 4: [3, 1, -1, 1, -1, -3, -1, 1, 1, -1, -3, -1, 1, -1, 1, 3]
    – DatHydroGuy
    Nov 12 '18 at 13:32






  • 2




    The pattern just repeats for each subsequent iteration. I hope that helps.
    – DatHydroGuy
    Nov 12 '18 at 13:33



















1














If you don't want to use a recursive function to do this, what you can do is use a stack. Push each level of your loop to the stack, so you can pop them later on.



A stack is a fundamental data structure in most languages as it's a very natural way to store function call contexts, it's also where Stack Overflow got its name from.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53259459%2fhow-to-define-a-iterative-function-involving-for-loop-where-the-iterator-i-j-l%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    No particular need for a recursive function.



    You can use a single loop for every number of indices if you use itertools.product; you can simplify the computation too, using sum and zip



    from itertools import product
    ...
    spin_products =
    n = int(input('how many indices? '))
    ...
    for indices in product([-1, 1], repeat=n):
    spin_products.append(sum(i*j for i, j in zip(indices, indices[1:])))





    share|improve this answer



















    • 2




      Of course loops containing list.append(...) can usually be changed to a list comprehension and this is no exception, ... = [sum(i*j for i, j in zip(ix, ix[1:])) for ix in product((-1,1), repeat=n)]
      – gboffi
      Nov 12 '18 at 12:15






    • 2




      zip(indices, indices[1:]) : That's very clever logic!!
      – bitbitbitter
      Nov 13 '18 at 18:44






    • 2




      How did you come up with that? If you may walk me through your thought process in arriving at that logic because for me it wasn't immediately obvious I had to write everything down and look at the pattern to come to a realization that that can be done.
      – bitbitbitter
      Nov 13 '18 at 18:48








    • 3




      Pattern recognition maybe? ① any nested loops with code only in the deepest level is a pattern that suggests itertools.product - your case was particularly simple because the same iterable was repeated n-times - and ② the actual computation is a sum of products of pairs, hence sum and zip that's the Python's way to create pairs of values.
      – gboffi
      Nov 13 '18 at 22:22






    • 3




      And, zip(a_list, a_list[1:]) is THE idiom to get consecutive pairs from a list.
      – gboffi
      Nov 13 '18 at 22:28
















    1














    No particular need for a recursive function.



    You can use a single loop for every number of indices if you use itertools.product; you can simplify the computation too, using sum and zip



    from itertools import product
    ...
    spin_products =
    n = int(input('how many indices? '))
    ...
    for indices in product([-1, 1], repeat=n):
    spin_products.append(sum(i*j for i, j in zip(indices, indices[1:])))





    share|improve this answer



















    • 2




      Of course loops containing list.append(...) can usually be changed to a list comprehension and this is no exception, ... = [sum(i*j for i, j in zip(ix, ix[1:])) for ix in product((-1,1), repeat=n)]
      – gboffi
      Nov 12 '18 at 12:15






    • 2




      zip(indices, indices[1:]) : That's very clever logic!!
      – bitbitbitter
      Nov 13 '18 at 18:44






    • 2




      How did you come up with that? If you may walk me through your thought process in arriving at that logic because for me it wasn't immediately obvious I had to write everything down and look at the pattern to come to a realization that that can be done.
      – bitbitbitter
      Nov 13 '18 at 18:48








    • 3




      Pattern recognition maybe? ① any nested loops with code only in the deepest level is a pattern that suggests itertools.product - your case was particularly simple because the same iterable was repeated n-times - and ② the actual computation is a sum of products of pairs, hence sum and zip that's the Python's way to create pairs of values.
      – gboffi
      Nov 13 '18 at 22:22






    • 3




      And, zip(a_list, a_list[1:]) is THE idiom to get consecutive pairs from a list.
      – gboffi
      Nov 13 '18 at 22:28














    1












    1








    1






    No particular need for a recursive function.



    You can use a single loop for every number of indices if you use itertools.product; you can simplify the computation too, using sum and zip



    from itertools import product
    ...
    spin_products =
    n = int(input('how many indices? '))
    ...
    for indices in product([-1, 1], repeat=n):
    spin_products.append(sum(i*j for i, j in zip(indices, indices[1:])))





    share|improve this answer














    No particular need for a recursive function.



    You can use a single loop for every number of indices if you use itertools.product; you can simplify the computation too, using sum and zip



    from itertools import product
    ...
    spin_products =
    n = int(input('how many indices? '))
    ...
    for indices in product([-1, 1], repeat=n):
    spin_products.append(sum(i*j for i, j in zip(indices, indices[1:])))






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 12 '18 at 11:27

























    answered Nov 12 '18 at 11:09









    gboffigboffi

    8,85822454




    8,85822454








    • 2




      Of course loops containing list.append(...) can usually be changed to a list comprehension and this is no exception, ... = [sum(i*j for i, j in zip(ix, ix[1:])) for ix in product((-1,1), repeat=n)]
      – gboffi
      Nov 12 '18 at 12:15






    • 2




      zip(indices, indices[1:]) : That's very clever logic!!
      – bitbitbitter
      Nov 13 '18 at 18:44






    • 2




      How did you come up with that? If you may walk me through your thought process in arriving at that logic because for me it wasn't immediately obvious I had to write everything down and look at the pattern to come to a realization that that can be done.
      – bitbitbitter
      Nov 13 '18 at 18:48








    • 3




      Pattern recognition maybe? ① any nested loops with code only in the deepest level is a pattern that suggests itertools.product - your case was particularly simple because the same iterable was repeated n-times - and ② the actual computation is a sum of products of pairs, hence sum and zip that's the Python's way to create pairs of values.
      – gboffi
      Nov 13 '18 at 22:22






    • 3




      And, zip(a_list, a_list[1:]) is THE idiom to get consecutive pairs from a list.
      – gboffi
      Nov 13 '18 at 22:28














    • 2




      Of course loops containing list.append(...) can usually be changed to a list comprehension and this is no exception, ... = [sum(i*j for i, j in zip(ix, ix[1:])) for ix in product((-1,1), repeat=n)]
      – gboffi
      Nov 12 '18 at 12:15






    • 2




      zip(indices, indices[1:]) : That's very clever logic!!
      – bitbitbitter
      Nov 13 '18 at 18:44






    • 2




      How did you come up with that? If you may walk me through your thought process in arriving at that logic because for me it wasn't immediately obvious I had to write everything down and look at the pattern to come to a realization that that can be done.
      – bitbitbitter
      Nov 13 '18 at 18:48








    • 3




      Pattern recognition maybe? ① any nested loops with code only in the deepest level is a pattern that suggests itertools.product - your case was particularly simple because the same iterable was repeated n-times - and ② the actual computation is a sum of products of pairs, hence sum and zip that's the Python's way to create pairs of values.
      – gboffi
      Nov 13 '18 at 22:22






    • 3




      And, zip(a_list, a_list[1:]) is THE idiom to get consecutive pairs from a list.
      – gboffi
      Nov 13 '18 at 22:28








    2




    2




    Of course loops containing list.append(...) can usually be changed to a list comprehension and this is no exception, ... = [sum(i*j for i, j in zip(ix, ix[1:])) for ix in product((-1,1), repeat=n)]
    – gboffi
    Nov 12 '18 at 12:15




    Of course loops containing list.append(...) can usually be changed to a list comprehension and this is no exception, ... = [sum(i*j for i, j in zip(ix, ix[1:])) for ix in product((-1,1), repeat=n)]
    – gboffi
    Nov 12 '18 at 12:15




    2




    2




    zip(indices, indices[1:]) : That's very clever logic!!
    – bitbitbitter
    Nov 13 '18 at 18:44




    zip(indices, indices[1:]) : That's very clever logic!!
    – bitbitbitter
    Nov 13 '18 at 18:44




    2




    2




    How did you come up with that? If you may walk me through your thought process in arriving at that logic because for me it wasn't immediately obvious I had to write everything down and look at the pattern to come to a realization that that can be done.
    – bitbitbitter
    Nov 13 '18 at 18:48






    How did you come up with that? If you may walk me through your thought process in arriving at that logic because for me it wasn't immediately obvious I had to write everything down and look at the pattern to come to a realization that that can be done.
    – bitbitbitter
    Nov 13 '18 at 18:48






    3




    3




    Pattern recognition maybe? ① any nested loops with code only in the deepest level is a pattern that suggests itertools.product - your case was particularly simple because the same iterable was repeated n-times - and ② the actual computation is a sum of products of pairs, hence sum and zip that's the Python's way to create pairs of values.
    – gboffi
    Nov 13 '18 at 22:22




    Pattern recognition maybe? ① any nested loops with code only in the deepest level is a pattern that suggests itertools.product - your case was particularly simple because the same iterable was repeated n-times - and ② the actual computation is a sum of products of pairs, hence sum and zip that's the Python's way to create pairs of values.
    – gboffi
    Nov 13 '18 at 22:22




    3




    3




    And, zip(a_list, a_list[1:]) is THE idiom to get consecutive pairs from a list.
    – gboffi
    Nov 13 '18 at 22:28




    And, zip(a_list, a_list[1:]) is THE idiom to get consecutive pairs from a list.
    – gboffi
    Nov 13 '18 at 22:28













    2














    My earlier answer was against your unedited post, so here's an update:



    From the code you've provided, it's clear that you must have at least the loops for i, and j.



    Evaluating that loop on it's own gives the following list:
    [1, -1, -1, 1]



    Now, each subsequent nested loop in your coded merely duplicates each element in this list, and then adds the array (repeated twice) from the previous loop:



    The i, j, and k loops give [1, 1, -1, -1, -1, -1, 1, 1] + [1, -1, -1,
    1, 1, -1, -1, 1] = [2, 0, -2, 0, 0, -2, 0, 2]



    The i, j, k, and l loops give [1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1] + [1, 1, -1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, 1, 1] + [1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1] = [3, 1, -1, 1, -1, -3, -1, 1, 1, -1, -3, -1, 1, -1, 1, 3]



    And so on...



    Therefore, you could simply try something like the following:



    adjacent_spin_product_matrix = 
    ij_result = [1, -1, -1, 1]

    def calc_product(adjacent_spin_product_matrix, number_of_indices):
    if number_of_indices == 2:
    return ij_result
    else:
    number_of_duplicates = 2 ** (number_of_indices - 2)
    curr_array =
    for elem in ij_result:
    for dup in range(number_of_duplicates):
    curr_array.append(elem)
    prev_array = calc_product(adjacent_spin_product_matrix, number_of_indices - 1)
    temp_array =
    temp_array.extend(prev_array)
    temp_array.extend(prev_array)
    result =
    for i, elem in enumerate(curr_array):
    result.append(elem + temp_array[i])

    return result


    Now, you should find that:



    print(calc_product(, 2))  # equivalent to i and j case
    print(calc_product(, 3)) # equivalent to i, j, and k case
    print(calc_product(, 4)) # equivalent to i, j, k, and l case
    print(calc_product(, 5)) # equivalent to i, j, k, l, and m case
    print(calc_product(, 6)) # equivalent to i, j, k, l, m, and o case
    print(calc_product(, 7)) # equivalent to i, j, k, l, m, o, and p case


    These give the expected results:



    [1, -1, -1, 1]
    [2, 0, -2, 0, 0, -2, 0, 2]
    [3, 1, -1, 1, -1, -3, -1, 1, 1, -1, -3, -1, 1, -1, 1, 3]
    [4, 2, 0, 2, 0, -2, 0, 2, 0, -2, -4, -2, 0, -2, 0, 2, 2, 0, -2, 0, -2, -4, -2, 0, 2, 0, -2, 0, 2, 0, 2, 4]
    [5, 3, 1, 3, 1, -1, 1, 3, 1, -1, -3, -1, 1, -1, 1, 3, 1, -1, -3, -1, -3, -5, -3, -1, 1, -1, -3, -1, 1, -1, 1, 3, 3, 1, -1, 1, -1, -3, -1, 1, -1, -3, -5, -3, -1, -3, -1, 1, 3, 1, -1, 1, -1, -3, -1, 1, 3, 1, -1, 1, 3, 1, 3, 5]
    [6, 4, 2, 4, 2, 0, 2, 4, 2, 0, -2, 0, 2, 0, 2, 4, 2, 0, -2, 0, -2, -4, -2, 0, 2, 0, -2, 0, 2, 0, 2, 4, 2, 0, -2, 0, -2, -4, -2, 0, -2, -4, -6, -4, -2, -4, -2, 0, 2, 0, -2, 0, -2, -4, -2, 0, 2, 0, -2, 0, 2, 0, 2, 4, 4, 2, 0, 2, 0, -2, 0, 2, 0, -2, -4, -2, 0, -2, 0, 2, 0, -2, -4, -2, -4, -6, -4, -2, 0, -2, -4, -2, 0, -2, 0, 2, 4, 2, 0, 2, 0, -2, 0, 2, 0, -2, -4, -2, 0, -2, 0, 2, 4, 2, 0, 2, 0, -2, 0, 2, 4, 2, 0, 2, 4, 2, 4, 6]





    share|improve this answer



















    • 2




      "Now, each subsequent nested loop in your coded merely duplicates each element in this list, and then adds the array (repeated twice) from the previous loop" can you elaborate more on this statement? what do you mean adds the array repeated twice from previous loop?
      – bitbitbitter
      Nov 12 '18 at 13:06






    • 1




      Certainly. If you look at the output from the iteration with only 2 elements (i and j), it is [1, -1, -1,1] I create the output for the 3-element iteration as follows: 1) create an array by duplicating every element from the previous (2-element) array. This gives me [1, 1, -1, -1, -1, -1, 1, 1]. 2) create another temporary array by concatenating the previous (2-element) array to itself. This produces [1, -1, -1, 1] + [1, -1, -1, 1] = [1, -1, -1, 1, 1, -1, -1, 1] 3) Add the temporary arrays from steps 1 and 2 together to get [2, 0, -2, 0, 0, -2, 0, 2].
      – DatHydroGuy
      Nov 12 '18 at 13:28






    • 1




      The above can be repeated when creating the 4-element array: 1) Take [1, -1, -1, 1], and repeat every element 4 times: [1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1] 2) Take [1, -1, -1, 1] and repeat every element twice: [1, 1, -1, -1, -1, -1, 1, 1] 3) Concatenate the array from step 2 to itself: [1, 1, -1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, 1, 1]. 4) Concatenate [1, -1, -1,1] to itself 4 times: [1, -1, -1,1, 1, -1, -1,1, 1, -1, -1,1, 1, -1, -1,1] 5) Add the arrays from 1, 3, and 4: [3, 1, -1, 1, -1, -3, -1, 1, 1, -1, -3, -1, 1, -1, 1, 3]
      – DatHydroGuy
      Nov 12 '18 at 13:32






    • 2




      The pattern just repeats for each subsequent iteration. I hope that helps.
      – DatHydroGuy
      Nov 12 '18 at 13:33
















    2














    My earlier answer was against your unedited post, so here's an update:



    From the code you've provided, it's clear that you must have at least the loops for i, and j.



    Evaluating that loop on it's own gives the following list:
    [1, -1, -1, 1]



    Now, each subsequent nested loop in your coded merely duplicates each element in this list, and then adds the array (repeated twice) from the previous loop:



    The i, j, and k loops give [1, 1, -1, -1, -1, -1, 1, 1] + [1, -1, -1,
    1, 1, -1, -1, 1] = [2, 0, -2, 0, 0, -2, 0, 2]



    The i, j, k, and l loops give [1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1] + [1, 1, -1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, 1, 1] + [1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1] = [3, 1, -1, 1, -1, -3, -1, 1, 1, -1, -3, -1, 1, -1, 1, 3]



    And so on...



    Therefore, you could simply try something like the following:



    adjacent_spin_product_matrix = 
    ij_result = [1, -1, -1, 1]

    def calc_product(adjacent_spin_product_matrix, number_of_indices):
    if number_of_indices == 2:
    return ij_result
    else:
    number_of_duplicates = 2 ** (number_of_indices - 2)
    curr_array =
    for elem in ij_result:
    for dup in range(number_of_duplicates):
    curr_array.append(elem)
    prev_array = calc_product(adjacent_spin_product_matrix, number_of_indices - 1)
    temp_array =
    temp_array.extend(prev_array)
    temp_array.extend(prev_array)
    result =
    for i, elem in enumerate(curr_array):
    result.append(elem + temp_array[i])

    return result


    Now, you should find that:



    print(calc_product(, 2))  # equivalent to i and j case
    print(calc_product(, 3)) # equivalent to i, j, and k case
    print(calc_product(, 4)) # equivalent to i, j, k, and l case
    print(calc_product(, 5)) # equivalent to i, j, k, l, and m case
    print(calc_product(, 6)) # equivalent to i, j, k, l, m, and o case
    print(calc_product(, 7)) # equivalent to i, j, k, l, m, o, and p case


    These give the expected results:



    [1, -1, -1, 1]
    [2, 0, -2, 0, 0, -2, 0, 2]
    [3, 1, -1, 1, -1, -3, -1, 1, 1, -1, -3, -1, 1, -1, 1, 3]
    [4, 2, 0, 2, 0, -2, 0, 2, 0, -2, -4, -2, 0, -2, 0, 2, 2, 0, -2, 0, -2, -4, -2, 0, 2, 0, -2, 0, 2, 0, 2, 4]
    [5, 3, 1, 3, 1, -1, 1, 3, 1, -1, -3, -1, 1, -1, 1, 3, 1, -1, -3, -1, -3, -5, -3, -1, 1, -1, -3, -1, 1, -1, 1, 3, 3, 1, -1, 1, -1, -3, -1, 1, -1, -3, -5, -3, -1, -3, -1, 1, 3, 1, -1, 1, -1, -3, -1, 1, 3, 1, -1, 1, 3, 1, 3, 5]
    [6, 4, 2, 4, 2, 0, 2, 4, 2, 0, -2, 0, 2, 0, 2, 4, 2, 0, -2, 0, -2, -4, -2, 0, 2, 0, -2, 0, 2, 0, 2, 4, 2, 0, -2, 0, -2, -4, -2, 0, -2, -4, -6, -4, -2, -4, -2, 0, 2, 0, -2, 0, -2, -4, -2, 0, 2, 0, -2, 0, 2, 0, 2, 4, 4, 2, 0, 2, 0, -2, 0, 2, 0, -2, -4, -2, 0, -2, 0, 2, 0, -2, -4, -2, -4, -6, -4, -2, 0, -2, -4, -2, 0, -2, 0, 2, 4, 2, 0, 2, 0, -2, 0, 2, 0, -2, -4, -2, 0, -2, 0, 2, 4, 2, 0, 2, 0, -2, 0, 2, 4, 2, 0, 2, 4, 2, 4, 6]





    share|improve this answer



















    • 2




      "Now, each subsequent nested loop in your coded merely duplicates each element in this list, and then adds the array (repeated twice) from the previous loop" can you elaborate more on this statement? what do you mean adds the array repeated twice from previous loop?
      – bitbitbitter
      Nov 12 '18 at 13:06






    • 1




      Certainly. If you look at the output from the iteration with only 2 elements (i and j), it is [1, -1, -1,1] I create the output for the 3-element iteration as follows: 1) create an array by duplicating every element from the previous (2-element) array. This gives me [1, 1, -1, -1, -1, -1, 1, 1]. 2) create another temporary array by concatenating the previous (2-element) array to itself. This produces [1, -1, -1, 1] + [1, -1, -1, 1] = [1, -1, -1, 1, 1, -1, -1, 1] 3) Add the temporary arrays from steps 1 and 2 together to get [2, 0, -2, 0, 0, -2, 0, 2].
      – DatHydroGuy
      Nov 12 '18 at 13:28






    • 1




      The above can be repeated when creating the 4-element array: 1) Take [1, -1, -1, 1], and repeat every element 4 times: [1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1] 2) Take [1, -1, -1, 1] and repeat every element twice: [1, 1, -1, -1, -1, -1, 1, 1] 3) Concatenate the array from step 2 to itself: [1, 1, -1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, 1, 1]. 4) Concatenate [1, -1, -1,1] to itself 4 times: [1, -1, -1,1, 1, -1, -1,1, 1, -1, -1,1, 1, -1, -1,1] 5) Add the arrays from 1, 3, and 4: [3, 1, -1, 1, -1, -3, -1, 1, 1, -1, -3, -1, 1, -1, 1, 3]
      – DatHydroGuy
      Nov 12 '18 at 13:32






    • 2




      The pattern just repeats for each subsequent iteration. I hope that helps.
      – DatHydroGuy
      Nov 12 '18 at 13:33














    2












    2








    2






    My earlier answer was against your unedited post, so here's an update:



    From the code you've provided, it's clear that you must have at least the loops for i, and j.



    Evaluating that loop on it's own gives the following list:
    [1, -1, -1, 1]



    Now, each subsequent nested loop in your coded merely duplicates each element in this list, and then adds the array (repeated twice) from the previous loop:



    The i, j, and k loops give [1, 1, -1, -1, -1, -1, 1, 1] + [1, -1, -1,
    1, 1, -1, -1, 1] = [2, 0, -2, 0, 0, -2, 0, 2]



    The i, j, k, and l loops give [1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1] + [1, 1, -1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, 1, 1] + [1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1] = [3, 1, -1, 1, -1, -3, -1, 1, 1, -1, -3, -1, 1, -1, 1, 3]



    And so on...



    Therefore, you could simply try something like the following:



    adjacent_spin_product_matrix = 
    ij_result = [1, -1, -1, 1]

    def calc_product(adjacent_spin_product_matrix, number_of_indices):
    if number_of_indices == 2:
    return ij_result
    else:
    number_of_duplicates = 2 ** (number_of_indices - 2)
    curr_array =
    for elem in ij_result:
    for dup in range(number_of_duplicates):
    curr_array.append(elem)
    prev_array = calc_product(adjacent_spin_product_matrix, number_of_indices - 1)
    temp_array =
    temp_array.extend(prev_array)
    temp_array.extend(prev_array)
    result =
    for i, elem in enumerate(curr_array):
    result.append(elem + temp_array[i])

    return result


    Now, you should find that:



    print(calc_product(, 2))  # equivalent to i and j case
    print(calc_product(, 3)) # equivalent to i, j, and k case
    print(calc_product(, 4)) # equivalent to i, j, k, and l case
    print(calc_product(, 5)) # equivalent to i, j, k, l, and m case
    print(calc_product(, 6)) # equivalent to i, j, k, l, m, and o case
    print(calc_product(, 7)) # equivalent to i, j, k, l, m, o, and p case


    These give the expected results:



    [1, -1, -1, 1]
    [2, 0, -2, 0, 0, -2, 0, 2]
    [3, 1, -1, 1, -1, -3, -1, 1, 1, -1, -3, -1, 1, -1, 1, 3]
    [4, 2, 0, 2, 0, -2, 0, 2, 0, -2, -4, -2, 0, -2, 0, 2, 2, 0, -2, 0, -2, -4, -2, 0, 2, 0, -2, 0, 2, 0, 2, 4]
    [5, 3, 1, 3, 1, -1, 1, 3, 1, -1, -3, -1, 1, -1, 1, 3, 1, -1, -3, -1, -3, -5, -3, -1, 1, -1, -3, -1, 1, -1, 1, 3, 3, 1, -1, 1, -1, -3, -1, 1, -1, -3, -5, -3, -1, -3, -1, 1, 3, 1, -1, 1, -1, -3, -1, 1, 3, 1, -1, 1, 3, 1, 3, 5]
    [6, 4, 2, 4, 2, 0, 2, 4, 2, 0, -2, 0, 2, 0, 2, 4, 2, 0, -2, 0, -2, -4, -2, 0, 2, 0, -2, 0, 2, 0, 2, 4, 2, 0, -2, 0, -2, -4, -2, 0, -2, -4, -6, -4, -2, -4, -2, 0, 2, 0, -2, 0, -2, -4, -2, 0, 2, 0, -2, 0, 2, 0, 2, 4, 4, 2, 0, 2, 0, -2, 0, 2, 0, -2, -4, -2, 0, -2, 0, 2, 0, -2, -4, -2, -4, -6, -4, -2, 0, -2, -4, -2, 0, -2, 0, 2, 4, 2, 0, 2, 0, -2, 0, 2, 0, -2, -4, -2, 0, -2, 0, 2, 4, 2, 0, 2, 0, -2, 0, 2, 4, 2, 0, 2, 4, 2, 4, 6]





    share|improve this answer














    My earlier answer was against your unedited post, so here's an update:



    From the code you've provided, it's clear that you must have at least the loops for i, and j.



    Evaluating that loop on it's own gives the following list:
    [1, -1, -1, 1]



    Now, each subsequent nested loop in your coded merely duplicates each element in this list, and then adds the array (repeated twice) from the previous loop:



    The i, j, and k loops give [1, 1, -1, -1, -1, -1, 1, 1] + [1, -1, -1,
    1, 1, -1, -1, 1] = [2, 0, -2, 0, 0, -2, 0, 2]



    The i, j, k, and l loops give [1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1] + [1, 1, -1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, 1, 1] + [1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1] = [3, 1, -1, 1, -1, -3, -1, 1, 1, -1, -3, -1, 1, -1, 1, 3]



    And so on...



    Therefore, you could simply try something like the following:



    adjacent_spin_product_matrix = 
    ij_result = [1, -1, -1, 1]

    def calc_product(adjacent_spin_product_matrix, number_of_indices):
    if number_of_indices == 2:
    return ij_result
    else:
    number_of_duplicates = 2 ** (number_of_indices - 2)
    curr_array =
    for elem in ij_result:
    for dup in range(number_of_duplicates):
    curr_array.append(elem)
    prev_array = calc_product(adjacent_spin_product_matrix, number_of_indices - 1)
    temp_array =
    temp_array.extend(prev_array)
    temp_array.extend(prev_array)
    result =
    for i, elem in enumerate(curr_array):
    result.append(elem + temp_array[i])

    return result


    Now, you should find that:



    print(calc_product(, 2))  # equivalent to i and j case
    print(calc_product(, 3)) # equivalent to i, j, and k case
    print(calc_product(, 4)) # equivalent to i, j, k, and l case
    print(calc_product(, 5)) # equivalent to i, j, k, l, and m case
    print(calc_product(, 6)) # equivalent to i, j, k, l, m, and o case
    print(calc_product(, 7)) # equivalent to i, j, k, l, m, o, and p case


    These give the expected results:



    [1, -1, -1, 1]
    [2, 0, -2, 0, 0, -2, 0, 2]
    [3, 1, -1, 1, -1, -3, -1, 1, 1, -1, -3, -1, 1, -1, 1, 3]
    [4, 2, 0, 2, 0, -2, 0, 2, 0, -2, -4, -2, 0, -2, 0, 2, 2, 0, -2, 0, -2, -4, -2, 0, 2, 0, -2, 0, 2, 0, 2, 4]
    [5, 3, 1, 3, 1, -1, 1, 3, 1, -1, -3, -1, 1, -1, 1, 3, 1, -1, -3, -1, -3, -5, -3, -1, 1, -1, -3, -1, 1, -1, 1, 3, 3, 1, -1, 1, -1, -3, -1, 1, -1, -3, -5, -3, -1, -3, -1, 1, 3, 1, -1, 1, -1, -3, -1, 1, 3, 1, -1, 1, 3, 1, 3, 5]
    [6, 4, 2, 4, 2, 0, 2, 4, 2, 0, -2, 0, 2, 0, 2, 4, 2, 0, -2, 0, -2, -4, -2, 0, 2, 0, -2, 0, 2, 0, 2, 4, 2, 0, -2, 0, -2, -4, -2, 0, -2, -4, -6, -4, -2, -4, -2, 0, 2, 0, -2, 0, -2, -4, -2, 0, 2, 0, -2, 0, 2, 0, 2, 4, 4, 2, 0, 2, 0, -2, 0, 2, 0, -2, -4, -2, 0, -2, 0, 2, 0, -2, -4, -2, -4, -6, -4, -2, 0, -2, -4, -2, 0, -2, 0, 2, 4, 2, 0, 2, 0, -2, 0, 2, 0, -2, -4, -2, 0, -2, 0, 2, 4, 2, 0, 2, 0, -2, 0, 2, 4, 2, 0, 2, 4, 2, 4, 6]






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 12 '18 at 12:08

























    answered Nov 12 '18 at 10:51









    DatHydroGuyDatHydroGuy

    6662312




    6662312








    • 2




      "Now, each subsequent nested loop in your coded merely duplicates each element in this list, and then adds the array (repeated twice) from the previous loop" can you elaborate more on this statement? what do you mean adds the array repeated twice from previous loop?
      – bitbitbitter
      Nov 12 '18 at 13:06






    • 1




      Certainly. If you look at the output from the iteration with only 2 elements (i and j), it is [1, -1, -1,1] I create the output for the 3-element iteration as follows: 1) create an array by duplicating every element from the previous (2-element) array. This gives me [1, 1, -1, -1, -1, -1, 1, 1]. 2) create another temporary array by concatenating the previous (2-element) array to itself. This produces [1, -1, -1, 1] + [1, -1, -1, 1] = [1, -1, -1, 1, 1, -1, -1, 1] 3) Add the temporary arrays from steps 1 and 2 together to get [2, 0, -2, 0, 0, -2, 0, 2].
      – DatHydroGuy
      Nov 12 '18 at 13:28






    • 1




      The above can be repeated when creating the 4-element array: 1) Take [1, -1, -1, 1], and repeat every element 4 times: [1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1] 2) Take [1, -1, -1, 1] and repeat every element twice: [1, 1, -1, -1, -1, -1, 1, 1] 3) Concatenate the array from step 2 to itself: [1, 1, -1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, 1, 1]. 4) Concatenate [1, -1, -1,1] to itself 4 times: [1, -1, -1,1, 1, -1, -1,1, 1, -1, -1,1, 1, -1, -1,1] 5) Add the arrays from 1, 3, and 4: [3, 1, -1, 1, -1, -3, -1, 1, 1, -1, -3, -1, 1, -1, 1, 3]
      – DatHydroGuy
      Nov 12 '18 at 13:32






    • 2




      The pattern just repeats for each subsequent iteration. I hope that helps.
      – DatHydroGuy
      Nov 12 '18 at 13:33














    • 2




      "Now, each subsequent nested loop in your coded merely duplicates each element in this list, and then adds the array (repeated twice) from the previous loop" can you elaborate more on this statement? what do you mean adds the array repeated twice from previous loop?
      – bitbitbitter
      Nov 12 '18 at 13:06






    • 1




      Certainly. If you look at the output from the iteration with only 2 elements (i and j), it is [1, -1, -1,1] I create the output for the 3-element iteration as follows: 1) create an array by duplicating every element from the previous (2-element) array. This gives me [1, 1, -1, -1, -1, -1, 1, 1]. 2) create another temporary array by concatenating the previous (2-element) array to itself. This produces [1, -1, -1, 1] + [1, -1, -1, 1] = [1, -1, -1, 1, 1, -1, -1, 1] 3) Add the temporary arrays from steps 1 and 2 together to get [2, 0, -2, 0, 0, -2, 0, 2].
      – DatHydroGuy
      Nov 12 '18 at 13:28






    • 1




      The above can be repeated when creating the 4-element array: 1) Take [1, -1, -1, 1], and repeat every element 4 times: [1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1] 2) Take [1, -1, -1, 1] and repeat every element twice: [1, 1, -1, -1, -1, -1, 1, 1] 3) Concatenate the array from step 2 to itself: [1, 1, -1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, 1, 1]. 4) Concatenate [1, -1, -1,1] to itself 4 times: [1, -1, -1,1, 1, -1, -1,1, 1, -1, -1,1, 1, -1, -1,1] 5) Add the arrays from 1, 3, and 4: [3, 1, -1, 1, -1, -3, -1, 1, 1, -1, -3, -1, 1, -1, 1, 3]
      – DatHydroGuy
      Nov 12 '18 at 13:32






    • 2




      The pattern just repeats for each subsequent iteration. I hope that helps.
      – DatHydroGuy
      Nov 12 '18 at 13:33








    2




    2




    "Now, each subsequent nested loop in your coded merely duplicates each element in this list, and then adds the array (repeated twice) from the previous loop" can you elaborate more on this statement? what do you mean adds the array repeated twice from previous loop?
    – bitbitbitter
    Nov 12 '18 at 13:06




    "Now, each subsequent nested loop in your coded merely duplicates each element in this list, and then adds the array (repeated twice) from the previous loop" can you elaborate more on this statement? what do you mean adds the array repeated twice from previous loop?
    – bitbitbitter
    Nov 12 '18 at 13:06




    1




    1




    Certainly. If you look at the output from the iteration with only 2 elements (i and j), it is [1, -1, -1,1] I create the output for the 3-element iteration as follows: 1) create an array by duplicating every element from the previous (2-element) array. This gives me [1, 1, -1, -1, -1, -1, 1, 1]. 2) create another temporary array by concatenating the previous (2-element) array to itself. This produces [1, -1, -1, 1] + [1, -1, -1, 1] = [1, -1, -1, 1, 1, -1, -1, 1] 3) Add the temporary arrays from steps 1 and 2 together to get [2, 0, -2, 0, 0, -2, 0, 2].
    – DatHydroGuy
    Nov 12 '18 at 13:28




    Certainly. If you look at the output from the iteration with only 2 elements (i and j), it is [1, -1, -1,1] I create the output for the 3-element iteration as follows: 1) create an array by duplicating every element from the previous (2-element) array. This gives me [1, 1, -1, -1, -1, -1, 1, 1]. 2) create another temporary array by concatenating the previous (2-element) array to itself. This produces [1, -1, -1, 1] + [1, -1, -1, 1] = [1, -1, -1, 1, 1, -1, -1, 1] 3) Add the temporary arrays from steps 1 and 2 together to get [2, 0, -2, 0, 0, -2, 0, 2].
    – DatHydroGuy
    Nov 12 '18 at 13:28




    1




    1




    The above can be repeated when creating the 4-element array: 1) Take [1, -1, -1, 1], and repeat every element 4 times: [1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1] 2) Take [1, -1, -1, 1] and repeat every element twice: [1, 1, -1, -1, -1, -1, 1, 1] 3) Concatenate the array from step 2 to itself: [1, 1, -1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, 1, 1]. 4) Concatenate [1, -1, -1,1] to itself 4 times: [1, -1, -1,1, 1, -1, -1,1, 1, -1, -1,1, 1, -1, -1,1] 5) Add the arrays from 1, 3, and 4: [3, 1, -1, 1, -1, -3, -1, 1, 1, -1, -3, -1, 1, -1, 1, 3]
    – DatHydroGuy
    Nov 12 '18 at 13:32




    The above can be repeated when creating the 4-element array: 1) Take [1, -1, -1, 1], and repeat every element 4 times: [1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1] 2) Take [1, -1, -1, 1] and repeat every element twice: [1, 1, -1, -1, -1, -1, 1, 1] 3) Concatenate the array from step 2 to itself: [1, 1, -1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, 1, 1]. 4) Concatenate [1, -1, -1,1] to itself 4 times: [1, -1, -1,1, 1, -1, -1,1, 1, -1, -1,1, 1, -1, -1,1] 5) Add the arrays from 1, 3, and 4: [3, 1, -1, 1, -1, -3, -1, 1, 1, -1, -3, -1, 1, -1, 1, 3]
    – DatHydroGuy
    Nov 12 '18 at 13:32




    2




    2




    The pattern just repeats for each subsequent iteration. I hope that helps.
    – DatHydroGuy
    Nov 12 '18 at 13:33




    The pattern just repeats for each subsequent iteration. I hope that helps.
    – DatHydroGuy
    Nov 12 '18 at 13:33











    1














    If you don't want to use a recursive function to do this, what you can do is use a stack. Push each level of your loop to the stack, so you can pop them later on.



    A stack is a fundamental data structure in most languages as it's a very natural way to store function call contexts, it's also where Stack Overflow got its name from.






    share|improve this answer


























      1














      If you don't want to use a recursive function to do this, what you can do is use a stack. Push each level of your loop to the stack, so you can pop them later on.



      A stack is a fundamental data structure in most languages as it's a very natural way to store function call contexts, it's also where Stack Overflow got its name from.






      share|improve this answer
























        1












        1








        1






        If you don't want to use a recursive function to do this, what you can do is use a stack. Push each level of your loop to the stack, so you can pop them later on.



        A stack is a fundamental data structure in most languages as it's a very natural way to store function call contexts, it's also where Stack Overflow got its name from.






        share|improve this answer












        If you don't want to use a recursive function to do this, what you can do is use a stack. Push each level of your loop to the stack, so you can pop them later on.



        A stack is a fundamental data structure in most languages as it's a very natural way to store function call contexts, it's also where Stack Overflow got its name from.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 '18 at 10:04









        Lie RyanLie Ryan

        44.5k968121




        44.5k968121






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53259459%2fhow-to-define-a-iterative-function-involving-for-loop-where-the-iterator-i-j-l%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Full-time equivalent

            Bicuculline

            さくらももこ