While reading this book, I will try to solve problems and problems.
-O'Reilly Japan --Matrix Programmer
Continuation of "Solving the problems / problems of" matrix programmer "(Chapter 0 function) --Qiita". I will raise it before "1.4.7 Euler's formula".
First, assign the above set or list of complex numbers to the variable S. To display points on a complex plane, use the procedure called plot included in the plotting module provided on this website. To load this procedure from the module:
>>> from plotting import plot
To display the S point, do the following:
>>> plot(S, 4)
Python will then open a browser window and display the points on the complex plane specified by S. The first argument of plot is a set of complex numbers (or tuples of two numbers), and the second argument specifies the scale of the figure to display. In this case, complex numbers with absolute values of 4 or less for the real and imaginary parts are displayed. The second argument is optional and has a default value of 1. There is another optional argument, which specifies the size of the point.
>>> from plotting import plot
>>> S = {2+2j, 3+2j, 1.75+1j, 2+1j, 2.25+1j, 2.5+1j, 2.75+1j, 3+1j, 3.25+1j}
>>> plot(S, 4)
Add 1 + 2i to each element of S using the comprehension notation and draw a new figure.
>>> plot({1+2j+z for z in S}, 4)
>>> plot({1+2j+z for z in S}, 4)
Draw a new figure that halves all the complex numbers in S, using a comprehension like Task 1.4.3.
>>> plot({z/2 for z in S}, 4)
Draw a diagram in which each point of S is rotated 90 degrees and then scaled in half. However, it can be achieved by multiplying each element of S by one complex number using the comprehension notation.
>>> plot({z*1j/2 for z in S}, 4)
Rotate each S store 90 degrees, scale it in half, and then draw a diagram with one scale down and two scales to the right. However, this can be achieved by multiplying each element of S by a complex number and adding the complex number using the comprehension notation.
>>> plot({z*1j/2+2-1j for z in S}, 4)
The image module provided on this website contains a file2image (filenae) procedure for loading images in png format. Specify the name of the image file in the argument of this procedure, execute it, and assign the return value to the variable data. A sample grayscale image, img01.png, can be downloaded from the website of this document. The value of data is a list of lists (lists with lists as elements), where data [y] [x] represents the intensity of the brightness of the pixel located at (x, y). (0, 0) is the upper left pixel of the image and (width-1, height-1) is the lower right pixel of the image. The intensity is represented by an integer from 0 to 255, where 0 is black and 255 is white. Use inclusion notation to define and illustrate a complex number x + iy k that represents the location (x, y) of pixels with an intensity less than 120 in the image.
>>> from image import file2image
>>> data = file2image('img01.png')
>>> pts = [x - y * 1j + len(data) * 1j for y in range(len(data)) for x in range(len(data[y])) if data[y][x][0] < 120]
>>> plot(pts, 190)
Write a procedure f (z) that shifts the image represented by S to the origin. Apply this procedure to S using comprehensions and illustrate the results.
util.py
def f(z):
x_list = [v.real for v in z]
y_list = [v.imag for v in z]
x_center = (min(x_list) + max(x_list)) / 2
y_center = (min(y_list) + max(y_list)) / 2
return [v - x_center - y_center * 1j for v in z]
>>> from util import f
>>> lot(f(list(S)), 4)
Repeat Exercise 1.4.8 with pts instead of S.
>>> plot([p*1j/2 for p in pts], 190)