1

I'm coding a function that tells which numbers are prime numbers in a given array. I would also like to do it without a for loop if it is possible

The numpy.divide function and division symbole (/) do not work as they throw a ValueError because the two arrays do not have the same shape

numbers = np.array([2, 3, 6])

def prime(numbers):
    biggest = np.max(numbers)
    division_array = np.arange(2, ceil(biggest / 2) + 1)
    # division_array = [2, 3]
    return numbers / division_array

print(numbers)

Expected output:

[[1, 0.6],
 [1.5, 1],
 [3, 2]]

Output:

ValueError: operands could not be broadcast together with shapes (4,) (3,)

6
  • 1
    So, by your (apparent) reasoning, dividing a 3-element array by another should produce a 3x3 array of results? Commented Jul 29, 2019 at 17:37
  • 3
    Add a new axis to numbers to use broadcasting: return numbers[:, np.newaxis] / division_array Commented Jul 29, 2019 at 17:37
  • 1
    @ScottHunter No but a 3-element array by a 2-element array should produce a 3x2 array, not a 3x3. Commented Jul 29, 2019 at 17:40
  • 1
    @jdehesa Thanks it work, could you put it as an answer so that i accept it. Commented Jul 29, 2019 at 17:43
  • 1
    np.divide.outer([2, 3, 6], [2, 3]) Commented Jul 29, 2019 at 17:48

2 Answers 2

2

Since you want an "all-to-all" operation, the best thing is to broadcast the arrays. Maybe the easiest way is to add a dimension to numbers with np.newaxis and divide:

import numpy as np

numbers = np.array([2, 3, 6])

def prime(numbers):
    biggest = np.max(numbers)
    division_array = np.arange(2, np.ceil(biggest / 2) + 1)
    # division_array = [2, 3]
    return numbers[:, np.newaxis] / division_array

print(prime(numbers))
# [[1.         0.66666667]
#  [1.5        1.        ]
#  [3.         2.        ]]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, you just reminded me of how I did it : I reshaped it to a Nx1 array
1

For anyone who doesn't want to use numpy, I created a plain python function for this.

I created this function that you could use to get what you need: https://repl.it/repls/ImpartialVainWebsites

array1 = [2, 3, 6]
array2 = [2, 3]

def divide_array(array1, array2):
  new_array = list()
  for element in array1:
    array1_list = list()
    for element2 in array2:
      array1_list.append(round(element/element2, 3))
    new_array.append(array1_list)
  return new_array

print(divide_array(array1, array2))

Returns:

[[1.0, 0.667], [1.5, 1.0], [3.0, 2.0]]

Or in a more compact form:

array1 = [2, 3, 6]
array2 = [2, 3]

def divide_array(array1, array2):
  new_array = list()
  for element in array1:
    new_array.append([round(element/element2, 3) for element2 in array2])
  return new_array

print(divide_array(array1, array2))

5 Comments

Is it possible to do it without a for loop ? I know i did it before but i forgot how...
@Docaro I changed it to use list comprehension, would that work better for you?
Not really as it is still a for loop so for really big lists it will take a long time
Okay I guess, I'll just keep the answer as it as for people in the future who might want to do it without numpy
Thanks anyway for helping :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.