Multidimensional array calculation without Numpy Part 2

Multidimensional array calculation without Numpy

Previously, the article http://qiita.com/ponnhide/items/c919f3bc549d1228c800 dealt with array calculation without numpy, but this article is an advanced version of it.

Why not use numpy?

Why not think of a way to use numpy when you can? It's just a hobby, but simply using numpy on pypy slows it down abnormally. Well, I'm sure there are other uses.

Last review (addition, subtraction, multiplication and division)

In the previous article, I wrote that "If you make full use of map and lambda, the operation between arrays is awkward", but using the operatar module is much simpler than using lambda. The operator module allows you to use operators in a functional form. For example, ʻoperator.add (a, b) is synonymous with ʻa + b.

First of all __ Sum of 1D arrays __

from operator import add
>>> array1 = [4,5,6]
>>> array2 = [1,2,3]
>>> list(map(add, array1, array2))
[5,7,9]

__ Sum of 2D arrays __ After all, I use lambda here,

>>> array1 = [[1,2,3],[1,2,3]]
>>> array2 = [[2,3,4],[2,3,4]]
>>> list(map(lambda x,y: list(map(add, x,y)), array1, array2))
[[3, 5, 7], [3, 5, 7]]

If you really don't rely on lambda, use list comprehension

>>> [list(map(add, x,y)) for x,y in zip(array1,array2)]
[[3, 5, 7], [3, 5, 7]]

You can also write like this. Is this one cleaner?

Matrix multiplication

I didn't deal with it last time, but if I boasted __ without using __numpy, I thought that I should do matrix multiplication (dot product, not product of elements), so I forcibly thought about it.

>>> from operator import mul
>>> array1 = [[2,3],[1,4],[2,1]]
>>> array2 = [[3,1,2],[2,4,2]]
>>> [[sum(map(mul, row, col)) for col in zip(*array2)] for row in array1]
[[12, 14, 10], [11, 17, 10], [8, 6, 6]]

I was able to do it. However, if array1 is 1 row xn column, it cannot be written as above. If array1 is 1 row xn columns

>>> from operator import mul
>>> array1 = [1,0,0,1]
>>> array2 = [[0,1],[1,1],[1,0],[1,0]]
>>> [sum(map(mul, array1, col)) for col in zip(*array2)]
[1,1]

Hmmm. If you make full use of lambda and ternary operators, you can write in a unified way. ..

Statistics related

I won't touch on how to use it, but in the environment of python3.4 or later, the statistics module is included by default. If you use this, you can calculate basic statistics while keeping the list type. https://docs.python.jp/3/library/statistics.html

Recommended Posts

Multidimensional array calculation without Numpy Part 2
Multidimensional array calculation without Numpy
python numpy array calculation
numpy part 1
Python application: Numpy Part 3: Double array
numpy part 2
Python multidimensional array
NumPy array manipulation (1)
Calculation speed of indexing for numpy quadratic array
List of array (ndarray) operations of Python's numerical calculation library "Numpy"
Empty multidimensional array in python
Create a python numpy array
Multidimensional array initialization of list
About Numpy array and asarray