This project contains a Python script to determine whether a given function is injective (one-to-one), surjective (onto), or bijective (both one-to-one and onto). The script evaluates these properties based on a specified domain and codomain.
A function ( f: A \rightarrow B ) is injective if every element of the domain ( A ) maps to a unique element in the codomain ( B ). In other words, no two different elements in ( A ) map to the same element in ( B ).
A function ( f: A \rightarrow B ) is surjective if every element of the codomain ( B ) has at least one pre-image in the domain ( A ). In other words, the function covers the entire codomain.
A function ( f: A \rightarrow B ) is bijective if it is both injective and surjective. This means every element in ( A ) maps to a unique element in ( B ) and every element in ( B ) is covered.
The code provides a function IsInjective
, IsSurjective
, IsBijective
to check whether a given function is bijective by evaluating its injective and surjective properties. Here's how the code works:
- IsBijective(func, dom, codom)
- This is the main function that checks if the given function
func
is bijective. - It contains two helper functions:
- is_one_to_one(): Checks if
func
is injective by ensuring no two elements indom
map to the same element incodom
. - is_onto(): Checks if
func
is surjective by ensuring every element incodom
is mapped by some element indom
.
- is_one_to_one(): Checks if
- If both conditions are met,
IsBijective
returnsTrue
, indicating the function is bijective.
- This is the main function that checks if the given function
- f(x)
- This is a sample function defined as ( f(x) = 2x ).
- The domain and codomain for this example are
[2, 4, 6, 8]
and[4, 8, 12, 16]
, respectively.
To check if the function ( f(x) = 2x ) is bijective for the given domain and codomain, run the following code:
import numpy as np
import matplotlib.pyplot as plt
def IsBijective(func, dom, codom):
def is_one_to_one():
set = []
for x in dom:
value = func(x)
if value in set:
return False
set.append(value)
print(f"f({x}) = {value}")
return True
def is_onto():
set = set()
for x in dom:
value = func(x)
if value in codom:
set.append(value)
print(f"f({x}) = {value}")
else:
print(f"f({x}) = {value} (But it is not in the codomain)")
return len(set) == len(codom)
if is_one_to_one() and is_onto():
return True
else:
return False
def f(x):
return 2 * x
Domain = [2, 4, 6, 8]
Codomain = [4, 8, 12, 16]
result = IsBijective(f, Domain, Codomain)
print(f"Is the function bijective: {result}")