# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3 (ipykernel)
#     language: python
#     name: python3
# ---

# + [markdown] tags=[]
# # Exercise 6 - Fuzzy Set Operations

# + [markdown] tags=[]
# #### Fuzzy Set Operations:
#
# | S. no. | Operation            | Definition using membership function                      |
# | ------ | -------------------- | --------------------------------------------------------- |
# | 1.     | Complement           | $$\mu_{\bar A}(x)= 1-\mu_A(x)$$                           |
# | 2.     | Union                | $$\mu_{A\cup B}(x) = \max(\mu_A(x),\mu_B(x)))$$           |
# | 3.     | Intersection         | $$\mu_{A\cap B}(x) = \min(\mu_A(x),\mu_B(x))$$            |
# | 4.     | Algebraic Product    | $$\mu_{A \cdot B}(x) = \mu_A(x)\cdot \mu_B(x)$$           |
# | 5.     | Algebraic Sum        | $$\mu_{A+B}(x) = \mu_A(x)+\mu_B(x) - \mu_{A \cdot B}(x)$$ |
# | 6.     | Algebraic Difference | $$\mu_{A-B}(x) = \mu_{A\cap \bar B}(x)$$                  |
# | 7.     | Bounded Product      | $$\mu_{A\odot B}(x) =\max(\mu_A(x)+\mu_B(x)-1,0)$$        |
# | 8.     | Bounded Sum          | $$\mu_{A\oplus B}(x) =\min(\mu_A(x)+\mu_B(x),1)$$         |
# | 9.     | Bounded Difference   | $$\mu_{A\ominus B}(x) = \max(\mu_A(x)-\mu_B(x),0)$$       |
# -

# ### Implementation using Matlab or Octave

# + [markdown] tags=[]
# #### Aim:
# To write a MATLAB or Octave program to find algebraic sum, algebraic subtraction, algebraic product, bounded sum,  bounded subtraction and bounded product of two fuzzy sets.

# + [markdown] tags=[]
# #### Program:
# -

# ```octave
# % Defining operations as functions
# complement = @(A) 1 - A
# union = @(A,B) max(A,B)
# intersection = @(A,B) min(A,B)
# algebraic_prod = @(A,B) A.*B
# algebraic_sum = @(A,B) A+B - algebraic_prod(A,B)
# algebraic_diff = @(A,B) A + complement(B)
# bounded_prod = @(A,B) union(A+B-1,0)
# bounded_sum = @(A,B) intersection(A+B,1)
# bounded_diff = @(A,B) union(A-B,0)
#
# % Declaring two fuzzy sets for testing
# A = [1 .5]
# B = [.4 .2]
#
# % Displaying the results
# disp('The algebraic sum')
# disp(algebraic_sum(A,B))
# disp('The algebraic difference')
# disp(algebraic_diff(A,B))
# disp('The algebraic product')
# disp(algebraic_prod(A,B))
# disp('The bounded sum')
# disp(bounded_sum(A,B))
# disp('The bounded difference')
# disp (bounded_diff(A,B))
# disp('The bounded product')
# disp(bounded_prod(A,B))
# ```

# #### Output:
# ```
# The algebraic sum
#    1.0000   0.6000
# The algebraic difference
#    1.6000   1.3000
# The algebraic product
#    0.4000   0.1000
# The bounded sum
#    1.0000   0.7000
# The bounded difference
#    0.6000   0.3000
# The bounded product
#    0.4000        0
# ```

# ### Implementation using Python

# + [markdown] tags=[]
# #### Aim:
# To write a python program to find algebraic sum, algebraic subtraction, algebraic product, bounded sum,  bounded subtraction and bounded product of two fuzzy sets.
# -

# #### Program

import numpy as np


# + tags=[]
def complement(A):
    return 1-A

def union(A,B):
    return np.max([A,B],axis=0)

def intersection(A,B):
    return np.min([A,B],axis=0)

def algebraic_prod(A,B):
    return A*B

def algebraic_sum(A,B):
    return A+B-algebraic_prod(A,B)

def algebraic_diff(A,B):
    return A+complement(B)

def bounded_prod(A,B):
    return union(A+B-1,np.zeros_like(A))

def bounded_sum(A,B):
    return intersection(A+B,np.ones_like(A))

def bounded_diff(A,B):
    return union(A-B,np.zeros_like(A))


# +
A=np.array([1, .5])
B=np.array([.4, .2])

print('The algebraic sum',algebraic_sum(A,B))
print('The algebraic difference',algebraic_diff(A,B))
print('The algebraic product',algebraic_prod(A,B))
print('The bounded sum',bounded_sum(A,B))
print('The bounded difference',bounded_diff(A,B))
print('The bounded product',bounded_prod(A,B))

StarcoderdataJupyterScriptsDedupFiltered