0

I want to plot this function defined on complex numbers: if floor(Re(z))%2==0, f(z)=z else f(z)=sin(z)

I tried to code this:

import cplot
import math
import numpy as np

def f(z):
    if math.floor(np.real(z)[0])%2==0:
        res =z
    else:
        res=np.sin(z)  
    return res

plt = cplot.plot(f, (-10, +10, 3000), (-10, 10, 3000),5)
plt.show()

But it didn't work, the program completely ignores the existence of the sin function and I don't know why.


I thought this was because of my usage of math. and np. so I tested a simpler function:

if Re(z)>=0, f(z) =z, else f(z)= sin(z)

import cplot
import numpy as np

def f(z):
    if np.real(z)[0]>=0:
        res =z
    else:
        res=np.sin(z)  
    return res

plt = cplot.plot(f, (-10, +10, 3000), (-10, 10, 3000),5)
plt.show()

This time the existence of z was completely ignored.


I thought maybe the problem came from the [0] in np.real(z)[0] but removing it would yield an error

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I used them and the result is still the same, it completely ignores z in my second code, also the problem seems to be in the > sign; if I change it to < it completely ignores the sin.

3
  • "completely ignore the existence of the sin function": what do you mean with "ignore"? Do you mean a statement is not executed? What is the result of your debugging session? Commented Aug 1, 2024 at 7:53
  • @trincot It plot the other function (z) and didn't plot the sin at all as if I never wrote it. Commented Aug 1, 2024 at 7:54
  • What happens when you run this in a debugger and set breakpoints and inspect variables? Surely you can monitor what exactly executes in f? Commented Aug 1, 2024 at 7:59

2 Answers 2

1

The function you plot should return an array of results, not just the static result for the first item in the input array.

I'm not a Numpy person, but based on Applying a function along a numpy array I would try something like

def f(x):
    if np.real(x) >= 0:
        res = x
    else:
        res = np.sin(x)  
    return res

f_v = np.vectorize(f)
plt = cplot.plot(f_v, (-10, +10, 3000), (-10, 10, 3000),5)
plt.show()

I'm sure this could be rewritten more elegantly but at least this should show the direction.

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

Comments

1

There is a a problem in how you are treating z. You are treating it as a regular array and not as a complex number. So, here is how it should possibly be

def f(z):
   if np.floor(z.real) % 2 == 0:
       res = z
   else:
        res = np.sin(z)
   return res

Comments

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.