1

How to program this expression in Python:

min{cos(2xπ), 1/2}

?

I have tried:

x = np.array([1,2,3,4,5,3,2,5,7])

solution = np.min(np.cos(2*x*np.pi), 1/2)

But it does not work, and there is the following mistake:

TypeError: 'float' object cannot be interpreted as an integer.

Screenshot of my code

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
maril
  • 49
  • 1
  • 1
  • 6
  • 2
    Please have a look at: https://stackoverflow.com/help/mcve. Thank you! – Thomas Junk Feb 05 '18 at 08:39
  • are you meaning `cos` of each value in x ? – Vikas Periyadath Feb 05 '18 at 08:42
  • @VikasDamodar Yes, cos of each value in x is a part of solution – maril Feb 05 '18 at 08:48
  • I used your inputs and got an answer of `1.0` in `IPython`. I don't understand why `jupyter` is giving you an error. You can try creating temporary variables to store intermediate results. I am using `Python 2`. Are you on `Python 3`? – Vineet Bhat Feb 05 '18 at 09:02
  • Possible duplicate of [Short Python Code to say "Pick the lower value"?](https://stackoverflow.com/questions/432211/short-python-code-to-say-pick-the-lower-value) – Ulf Aslak Feb 05 '18 at 09:03
  • `numpy.min` expects second parameter to be `axis` which you are passing as`1/2`, Thats why you are getting this error. – Sohaib Farooqi Feb 05 '18 at 09:13

1 Answers1

1

I have tried your code with np.minimum like this :

import numpy as np

x = np.array([1,2,3,4,5,3,2,5,7])

solution = np.minimum(np.cos(2*x*np.pi), 1/2)
print(solution)

which gives something like this :

[ 0.5  0.5  0.5  0.5  0.5  0.5  0.5  0.5  0.5]

the minimum function will check through each element of array and returns an array. you can take a look here

Vikas Periyadath
  • 3,088
  • 1
  • 21
  • 33