Everything should be made as simple as possible, but not simpler. (Albert Einstein)

Tuesday, December 24, 2013

Graph Complex Set In Python with mpmath and matplotlib

In this case I use Python(x,y). This package comes with many standard plugins, such as sympy (in which we use its mpmath), numpy, scipy, matplotlib, and so on. http://code.google.com/p/pythonxy/wiki/StandardPlugins



Matplotlib is as dependency of mpmath.cplot.

Example 1: Graph a complex set {Z ∈ C : |z - 3 - 2j|}
Code: 

# example1.py
from sympy import mpmath as mp
# import numpy as np
# import matplotlib.pyplot as plt

def coloring1(z):
    if mp.fabs(z - 3 - 2j) <= 1:
        return (0.1, 0.1, 0.8)   # blue
    else:
        return (1.0, 1.0, 1.0)   # white
    
def Q1():
    mp.cplot(lambda z: z, re=[0, 5], im=[0, 4], color=coloring1, points=100000, dpi=72)  

if __name__ == "__main__":
    Q1()

Blue-filled circle is the area of solution set in complex plane.
on imaginary axis, 1.0 ≤ Im(z) ≤ 3.0
on real axis, 2.0 ≤ Re(z) ≤ 4.0

Note: in Python, imaginary "i" symbol is represented as "j" char.