[PYTHON] Try to solve the problems / problems of "Matrix Programmer" (Chapter 1)

Introduction

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".

Chapter 1 body

1.4 Let's play with ℂ

Challenge 1.4.1

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)

スクリーンショット 2017-01-06 2.59.22.png

1.4.1 Absolute value of complex number

1.4.2 Adding complex numbers

Challenge 1.4.3

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)

スクリーンショット 2017-01-06 3.06.41.png

1.4.3 Multiplying a complex number by a positive real number

Challenge 1.4.7

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)

スクリーンショット 2017-01-06 3.12.53.png

1.4.4 Multiplying a complex number by a negative real number: 180 degree rotation

1.4.5 Multiply i: 90 degree rotation

Challenge 1.4.8

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)

スクリーンショット 2017-01-06 3.18.01.png

Challenge 1.4.9

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)

スクリーンショット 2017-01-06 3.21.02.png

Challenge 1.4.10

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)

スクリーンショット 2017-01-06 4.46.55.png

Challenge 1.4.11

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)

スクリーンショット 2017-01-06 5.32.51.png

Challenge 1.4.12

Repeat Exercise 1.4.8 with pts instead of S.

>>> plot([p*1j/2 for p in pts], 190)

スクリーンショット 2017-01-06 5.38.44.png

1.4.6 Unit circle on the complex plane: argument and angle

Recommended Posts

Try to solve the problems / problems of "Matrix Programmer" (Chapter 1)
Try to solve the problems / problems of "Matrix Programmer" (Chapter 0 Functions)
Try to solve the N Queens problem with SA of PyQUBO
I tried to solve the 2020 version of 100 language processing [Chapter 3: Regular expressions 25-29]
Try to solve the fizzbuzz problem with Keras
Try to solve the Python class inheritance problem
Try to solve the man-machine chart with Python
Try singular value decomposition of the daimyo matrix
Try to simulate the movement of the solar system
I tried to solve the 2020 version of 100 language processing knocks [Chapter 3: Regular expressions 20 to 24]
I tried to solve the 2020 version of 100 language processing knocks [Chapter 1: Preparatory movement 00-04]
I tried to solve the 2020 version of 100 language processing knocks [Chapter 1: Preparatory movement 05-09]
Try to solve the programming challenge book with python3
Try to solve the internship assignment problem with Python
Try to estimate the number of likes on Twitter
Try to get the contents of Word with Golang
Try to get the function list of Python> os package
Try to evaluate the performance of machine learning / regression model
Try to evaluate the performance of machine learning / classification model
Introduction to Statistics The University of Tokyo Press Chapter 2 Exercises
Try to improve the accuracy of Twitter like number estimation
Try to automate the operation of network devices with Python
Try to extract the features of the sensor data with CNN
[Note] Let's try to predict the amount of electricity used! (Part 1)
[Cloudian # 9] Try to display the metadata of the object in Python (boto3)
[Python] Try to graph from the image of Ring Fit [OCR]
The 17th Offline Real-time How to Solve Writing Problems in Python
First python ② Try to write code while examining the features of python
Try to introduce the theme to Pelican
Solving the Maze with Python-Supplement to Chapter 6 of the Algorithm Quick Reference-
Solve the delay of interferometer observation
Try to model the cumulative return of rollovers in futures trading
Cython to try in the shortest
Try to predict the triplet of boat race by ranking learning
The fastest way to try EfficientNet
Try to solve the shortest path with Python + NetworkX + social data
Supplement to the explanation of vscode
The easiest way to try PyQtGraph
Try to solve the function minimization problem using particle swarm optimization
Ford-Fulkerson Method and Its Applications-Supplement to Chapter 8 of the Algorithm Quick Reference-
Use Raspberry Pi to solve the problem of insufficient mobile Wi-Fi connection
Try to estimate the parameters of the gamma distribution while simply implementing MCMC
Try to image the elevation data of the Geographical Survey Institute with Python
Try to solve the traveling salesman problem with a genetic algorithm (Theory)
Try to get the road surface condition using big data of road surface management
Try using n to downgrade the version of Node.js you have installed
Try to react only the carbon at the end of the chain with SMARTS
I tried to solve the E qualification problem collection [Chapter 1, 5th question]
Try to solve a set problem of high school math with Python
Try to separate the background and moving object of the video with OpenCV
The story of trying to reconnect the client
Stack problem: Try to solve "20. Valid Parentheses"
Script to change the description of fasta
10 methods to improve the accuracy of BERT
Try to face the integration by parts
How to solve the bin packing problem
Try to make a kernel of Jupyter
The story of adding MeCab to ubuntu 16.04
Run the Matrix to your boss's terminal!
[Python] Try pydash of the Python version of lodash
[Python] Solve 10 past elite problems of Atcoder