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

6
  • 2
    Please have a look at: stackoverflow.com/help/mcve. Thank you! Commented Feb 5, 2018 at 8:39
  • are you meaning cos of each value in x ? Commented Feb 5, 2018 at 8:42
  • @VikasDamodar Yes, cos of each value in x is a part of solution Commented Feb 5, 2018 at 8: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? Commented Feb 5, 2018 at 9:02
  • Possible duplicate of Short Python Code to say "Pick the lower value"? Commented Feb 5, 2018 at 9:03

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Actually, with both python 2 and 3, I get output [0. 0. 0. 0. 0. 0. 0. 0. 0.] (yes, 0., not 0.5)
@ChatterOne iam giving here a link you can check the code there : repl.it/repls/NimbleWhisperedHalicore
This is correct solution if you want element wise minimum.

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.