Circular convolution in python - python

Is there a way with Python to perform circular convolution between two 1D arrays,
like with Matlab function cconv?
I tried numpy.convolve but it isn't the same, and I can’t find an equivalent.

You can try this command : scipy.ndimage.filters.convolve1d
You have an option which is named modeand you can write mode = wrap
With that, you get periodic boundary conditions as padding for the convolution
For example :
result = scipy.ndimage.convolve(image,kernel,mode='wrap')
import numpy as np
image = np.array([[0, 0, 0, 0],
[0, 0, 0, 1],
[0, 0, 0, 0]])
kernel = np.array([[1, 1, 1],
[1, 1, 1],
[1, 1, 1]])
from scipy.ndimage import convolve
convolve(image, kernel, mode='wrap')
array([[1, 0, 1, 1],
[1, 0, 1, 1],
[1, 0, 1, 1]])

Related

Python package Sympy: Why the conjugate of a matrix leads to an abstract tensor product

If I do
from sympy import *
from sympy.physics.quantum import TensorProduct
m = Matrix([[0,1],[0,0]])
TensorProduct(m,m)
I get
Matrix([
[0, 0, 0, 1],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]])
which is the correct result.
However, if I try
TensorProduct(conjugate(m),m)
I get
Matrix([
[0, 1],
[0, 0]])xMatrix([
[0, 1],
[0, 0]])
why does the conjugate function "force" the TensorProduct function to considerate conjugate(m) in an abstract way? Can anyone explain what is going on?

Merge three numpy arrays, keep largest value

I want to merge three numpy arrays, for example:
a = np.array([[0,0,1],[0,1,0],[1,0,0]])
b = np.array([[1,0,0],[0,1,0],[0,0,1]])
c = np.array([[0,1,0],[0,2,0],[0,1,0]])
a = array([[0, 0, 1],
[0, 1, 0],
[1, 0, 0]])
b = array([[1, 0, 0],
[0, 1, 0],
[0, 0, 1]])
c = array([[0, 1, 0],
[0, 2, 0],
[0, 1, 0]])
Desired result would be to overlay them but keep the largest value where multiple elements are not 0, like in the middle.
array([[1, 1, 1],
[0, 2, 0],
[1, 1, 1]])
I solved this by iterating over all elements with multiple if-conditions. Is there a more compact and more beautiful way to do this?
You can try of stacking arrays together in extra dimension with Numpy np.dstack method
and extract the maximum value specific to added dimension
# Stacking arrays together
d = np.dstack([a,b,c])
d.max(axis=2)
Out:
array([[1, 1, 1],
[0, 2, 0],
[1, 1, 1]])
NumPy's np.ufunc.reduce allows to apply a function cumulatively along a given axis. We can just concatenate the arrays and reduce with numpy.maximum to keep the accumulated elementwise maximum:
np.maximum.reduce([a,b,c])
array([[1, 1, 1],
[0, 2, 0],
[1, 1, 1]])

Replace some elements of numpy.ndarray with zero given another numpy array

I'd need to know the most efficient way for the following case. There is a numpy.ndarray of shape 11k*11k for which I need to force all elements of some rows to be zero given a binary numpy array of shape 11k. A toy example could be described as follows:
Inputs:
x = np.array([[2, 1, 1, 2],
[0, 2, 1, 0],
[1, 0, 1, 1],
[2, 2, 1, 0]])
ref = np.array([0, 1, 1, 0])
Output:
y = ([[0, 0, 0, 0],
[0, 2, 1, 0],
[1, 0, 1, 1],
[0, 0, 0, 0]])
Use this -
y = np.multiply(x.T,ref).T
array([[0, 0, 0, 0],
[0, 2, 1, 0],
[1, 0, 1, 1],
[0, 0, 0, 0]])

3D numpy array in Theano

I'm having a 3D NumpyArray which I want to through in a Keras Neural Network.
Because of one hot encoding the array became a 3D array.
[
[[0,1,0,0], [1,0,0,0]],
[[0,0,0,1], [1,1,0,0]],
[[0,0,1,0], [0,0,0,1]]
]
Since Keras can only compute 2D arrays, my question is, how can I reduce the dimensionality and use it into a sequencial keras NN?
I currently get the error:
TypeError: ('Bad input argument to theano function with name "D:\\Python27\\lib\\site-packages\\keras\\backend\\theano_backend.py:503" at index 0(0-based)', 'Wrong number of dimensions: expected 2, got 3 with shape (32L, 10L, 12L).')
You can use numpy.ndarray.flatten to make it into a 1D array. Example:
import numpy as np
a = np.array(
[
[[0, 1, 0, 0], [1, 0, 0, 0]],
[[0, 0, 0, 1], [1, 1, 0, 0]],
[[0, 0, 1, 0], [0, 0, 0, 1]]
]
)
a.flatten()
From this, if you want to split it by row, I'd recommend doing
import numpy as np
a = np.array(
[
[[0, 1, 0, 0], [1, 0, 0, 0]],
[[0, 0, 0, 1], [1, 1, 0, 0]],
[[0, 0, 1, 0], [0, 0, 0, 1]]
]
)
a = map(np.ndarray.flatten, a)

Compute Jordan normal form of matrix in Python / NumPy

In MATLAB you can compute the Jordan normal form of a matrix by using the the function jordan.
It there an equivalent function available in NumPy and SciPy?
The MATLAB jordan function is from the Symbolic Math Toolbox, so it does not seem unreasonable to get its Python replacement from the SymPy library. Specifically, the Matrix class has the method jordan_form. You can pass a numpy array as an argument when you create a sympy Matrix. For example, the following is from the wikipedia article on the Jordan normal form:
In [1]: import numpy as np
In [2]: from sympy import Matrix
In [3]: a = np.array([[5, 4, 2, 1], [0, 1, -1, -1], [-1, -1, 3, 0], [1, 1, -1, 2]])
In [4]: m = Matrix(a)
In [5]: m
Out[5]:
Matrix([
[ 5, 4, 2, 1],
[ 0, 1, -1, -1],
[-1, -1, 3, 0],
[ 1, 1, -1, 2]])
In [6]: P, J = m.jordan_form()
In [7]: J
Out[7]:
Matrix([
[1, 0, 0, 0],
[0, 2, 0, 0],
[0, 0, 4, 1],
[0, 0, 0, 4]])
There's this implementation.
It will definitely not be as fast as MATLAB, though.

Categories

Resources