Search code examples
pythonplotcomplex-numberssage

`implicit_plot` complex function in SageMath


I'd like to plot the circle described by the equation |z - 1| = 1 for complex z. I expected the following to work:

f(x, y) = abs(x +  i * y -1)

implicit_plot(f(x, y) - 1, xrange=(-2, 2), yrange=(-2, 2))

but it fails with:

TypeError: unable to coerce to a real number

which seems strange, since abs does't return a complex number. The following fails with the same error:

f(x, y) = abs(x +  i * y -1)

implicit_plot((f(x, y) - 1).real_part(), xrange=(-2, 2), yrange=(-2, 2))

What's the right way to plot this function?


Solution

  • The command defining f defines it as a symbolic function:

    sage: f(x, y) = abs(x +  i * y - 1)
    
    sage: f
    (x, y) |--> abs(x + I*y - 1)
    sage: f(x, y)
    abs(x + I*y - 1)
    
    sage: parent(f)
    Callable function ring with arguments (x, y)
    sage: parent(f(x, y))
    Symbolic Ring
    

    The plotting commands to plot f(x, y) - 1 as in the question trigger internal code to convert the required expression into a "fast-callable function", which fails.

    A workaround, valid for many plotting problems in Sage, is to use a lambda function instead.

    For this, just introduce lambda x, y: before the expression in x and y to be plotted:

    sage: implicit_plot(lambda x, y: f(x, y) - 1, xrange=(-2, 2), yrange=(-2, 2))
    Launched png viewer for Graphics object consisting of 1 graphics primitive
    

    Implicit plot of a circle using its complex numbers definition