"""
# Convolutional and Relu With Arrays 
"""
# Sympy is a python library for symbolic mathematics. It has a nice
# pretty printer for matrices, which is all we'll use it for.
import sympy
sympy.init_printing()
from IPython.display import display
"""
Define a image
"""
import numpy as np
#It is a image contains
#1. Horizontal line
#2. a small vertical line at left bottom
image = np.array([
    [0,0,0,0,0,0],
    [1,1,1,1,1,1],
    [0,1,0,0,0,0],
    [0,1,0,0,0,0],
    [0,1,0,0,0,0],
    [0,1,0,0,0,0],
])
#uncomment to show a difference of using sympy library
#print(image)
display(sympy.Matrix(image))
#kernel1 detect horizontal line
kernel1 = np.array([
    [1,1],
    [-1,-1]
])

display(sympy.Matrix(kernel1))
# Reformat for Tensorflow
image = tf.cast(image, dtype=tf.float32)
image = tf.reshape(image, [1, *image.shape, 1])
kernel1 = tf.reshape(kernel1, [*kernel1.shape, 1, 1])
kernel1 = tf.cast(kernel1, dtype=tf.float32)
"""
Detection of Horizontal Line
"""
import tensorflow as tf
#get feature map 
image_filter = tf.nn.conv2d(
    input=image,
    filters=kernel1,
    strides=1,
    padding='VALID',
)
#Detect filter
image_detect = tf.nn.relu(image_filter)

# The first matrix is the image after convolution, and the second is
# the image after ReLU.
display(sympy.Matrix(tf.squeeze(image_filter).numpy()))
display(sympy.Matrix(tf.squeeze(image_detect).numpy()))
"""
Detection of Vertical Line
"""
image2 = np.array([
    [0,0,0,0,0,0],
    [1,1,1,1,1,1],
    [0,1,0,0,0,0],
    [0,1,0,0,0,0],
    [0,1,0,0,0,0],
    [0,1,0,0,0,0],
])
#kernel2 detect vertical line
kernel2 = np.array([
    [1,-1],
    [1,-1]
])
# Reformat for Tensorflow
image2 = tf.cast(image2, dtype=tf.float32)
image2 = tf.reshape(image2, [1, *image2.shape, 1])
kernel2 = tf.reshape(kernel2, [*kernel2.shape, 1, 1])
kernel2 = tf.cast(kernel2, dtype=tf.float32)
import tensorflow as tf
#get feature map 
image_filter2 = tf.nn.conv2d(
    input=image2,
    filters=kernel2,
    strides=1,
    padding='VALID',
)
#Detect filter
image_detect2 = tf.nn.relu(image_filter2)

# The first matrix is the image after convolution, and the second is
# the image after ReLU.
display(sympy.Matrix(tf.squeeze(image_filter2).numpy()))
display(sympy.Matrix(tf.squeeze(image_detect2).numpy()))