2

Suppose I have the regular cartesian coordinate system $(x,y)$ and I consider a rectangular mesh region $D$ (split into little squares). I want to see how the domain D would be mapped under a coordinate transform T:(x,y) -> (u(x,y) ,v(x,y) ) in Python?

I'm looking for something like this:

enter image description here

See here.

Could I be advised on how this could be done? I am a total beginner at python and programming.

2 Answers2

5

If I understand you correctly, you want to be able to see a sort of a grid plot of a transformed cartesian space? In that case, maybe something like this, using Numpy and Matplotlib. (You could do the same with just Pillow to draw some lines, but this is more convenient...)

EDIT: Following the discussion in the comments, I changed this a bit compared to the simpler original (see edit history), to make it easier to plot multiple transformations, as well as to color the lines to make it easier to follow how they're transformed. It's still pretty simple, though. (For fun, I also threw in a couple of extra transformations.)

import math

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm

colormap = cm.get_cmap("rainbow")


def plot_grid(
    xmin: float,
    xmax: float,
    ymin: float,
    ymax: float,
    n_lines: int,
    line_points: int,
    map_func,
):
    """
    Plot a transformation of a regular grid.

    :param xmin: Minimum x value
    :param xmax: Maximum x value
    :param ymin: Minimum y value
    :param ymax: Maximum y value
    :param n_lines: Number of lines per axis
    :param line_points: Number of points per line
    :param map_func: Function to map the grid points to new coordinates
    """
    # List for gathering the lines into.
    lines = []

    # Iterate over horizontal lines.
    for y in np.linspace(ymin, ymax, n_lines):
        lines.append([map_func(x, y) for x in np.linspace(xmin, xmax, line_points)])

    # Iterate over vertical lines.
    for x in np.linspace(xmin, xmax, n_lines):
        lines.append([map_func(x, y) for y in np.linspace(ymin, ymax, line_points)])

    # Plot all the lines.
    for i, line in enumerate(lines):
        p = i / (len(lines) - 1)  # Normalize to 0-1.
        # Transpose the list of points for passing to plot.
        xs, ys = zip(*line)
        # Get the line color from the colormap.
        plt.plot(xs, ys, color=colormap(p))


# Define some mapping functions.


def identity(x, y):
    return x, y


def example(x, y):
    c = complex(x, y) ** 2
    return (c.real, c.imag)


def wobbly(x: float, y: float):
    return x + math.sin(y * 2) * 0.2, y + math.cos(x * 2) * 0.3


def vortex(x: float, y: float):
    dst = (x - 2) ** 2 + (y - 2) ** 2
    ang = math.atan2(y - 2, x - 2)
    return math.cos(ang - dst * 0.1) * dst, math.sin(ang - dst * 0.1) * dst


# Set up the plot surface...
plt.figure(figsize=(8, 8))
plt.tight_layout()

plt.subplot(2, 2, 1)
plt.title("Identity")
plot_grid(0, 4, 0, 4, 10, 10, identity)

plt.subplot(2, 2, 2)
plt.title("Example")
plot_grid(0, 4, 0, 4, 10, 10, example)

plt.subplot(2, 2, 3)
plt.title("Wobbly")
plot_grid(0, 4, 0, 4, 10, 40, wobbly)

plt.subplot(2, 2, 4)
plt.title("Vortex")
plot_grid(0, 4, 0, 4, 10, 40, vortex)

plt.savefig("so71735261-2.png")
plt.show()

The result image is: enter image description here

AKX
  • 152,115
  • 15
  • 115
  • 172
  • Quick question how do I change the function? For example how do I put sin(z) – Reine Abstraktion Apr 04 '22 at 20:05
  • Change what's inside get_point. – AKX Apr 05 '22 at 04:19
  • I see, I think the reason I got confused is that we don't know how the initial mesh looked like before we mapped it to the final one (different meshes give different mapped results). Could there be anyway to include that on the side? – Reine Abstraktion Apr 05 '22 at 04:24
  • 1
    Of course, just a moment... – AKX Apr 05 '22 at 05:08
  • @Buraian Please see edit :-) – AKX Apr 05 '22 at 05:26
  • Wow! I was actually doubting if programming would help me do the things I Wanted but your answer is 100% convinced me that programming will allow me to do exactly what I want. While I can use your answer, I can't understand it yet. I am will be spending a long time on understanding all the commands and syntax you've used. Thank you so much :) – Reine Abstraktion Apr 05 '22 at 05:28
  • 1
    @Buraian I'm glad I could help! (As an aside, the holeless cow deformation in your profile gave me a chuckle, so thanks for that!) – AKX Apr 05 '22 at 05:37
-2

It's not completely trivial. You need to learn how to use a plotting library to create the plots, e.g. matplotlib. You also need to figure out how to create, discretize and transform your regular grid. The numpy library is very useful for such operations. Anyway, I'm not sure this is such a good problem for a "total beginner". I'd advise to start with something simpler, if you don't know programming or Python at all.

Jussi Nurminen
  • 2,257
  • 1
  • 9
  • 16
  • Actually the motivation is that I am trying to go over a book known as Visual Complex Analysis by Tristan Needham. It is suggested in the book to use computer programs to plot the complex functions many times. Many of the programs which were suggested in the book are unvailable now/ not free to use, so I wanted to see how If this was posisble through python only – Reine Abstraktion Apr 04 '22 at 10:40
  • If you could perhaps give me a road map of the things I need to know to solve this problem , then that would be really great. This is something I really want to do. – Reine Abstraktion Apr 04 '22 at 10:40
  • The roadmap would be 1) learn the basics of Python and then 2) learn to use the matplotlib and numpy libraries. If you try to learn these (pretty big and complex) libraries without knowing the basics of Python first, it will probably just lead to frustration. – Jussi Nurminen Apr 04 '22 at 10:44
  • I've gone over the libraries and while they do seem related I can't exactly see how I could equipped with the knowledge of those libraries solve this situation. – Reine Abstraktion Apr 04 '22 at 10:48
  • Could you perhaps give me more specific advice if you could? And perhaps where I could learn those things – Reine Abstraktion Apr 04 '22 at 10:49
  • 1
    I would start by defining a regular grid of complex numbers (representing grid points) in a numpy array, then transform it into another array using the desired complex operation. Then I would try to plot both arrays as a bunch of curves, probably using some kind of interpolation between the calculated points to get smooth curves. For an experienced user of these libraries, it wouldn't be too hard. But I don't think it's a good first problem. – Jussi Nurminen Apr 04 '22 at 10:55
  • I'm confused a bit here, array is a discrete block of numbers right? So how does one go from that to a curve? – Reine Abstraktion Apr 04 '22 at 11:02
  • Well, you can't. A curve has an infinite number of points, and computers cannot represent that. But you can determine a limited number of points on the curve. If you use a sufficiently large amount of points, it will start looking like a smooth curve. – Jussi Nurminen Apr 04 '22 at 11:06