Implementing Basic Functions in Python
メニューを表示するにはスワイプしてください
Functions define relationships between inputs and outputs, making them fundamental in mathematics, programming, and data science. In Python, we can define and visualize different types of functions, such as one-to-one, many-to-one, onto, into, and bijective functions.
Types of Functions in Python
One-to-One (Injective) Function
A one-to-one function ensures that each input maps to a unique output. As you'll see, no two inputs have the same output.
123456789# One-to-One Function: f(x) = x def one_to_one(x): return x # Example Outputs print("One-to-One Function Outputs:") print(one_to_one(2)) # Output is 2 print(one_to_one(5)) # Output is 5
Many-to-One Function
A many-to-one function allows multiple inputs to map to the same output.
12345678# Many-to-One Function: f(x) = x^2 def many_to_one(x): return x ** 2 # Example Outputs print("\nMany-to-One Function Outputs:") print(many_to_one(3)) # Output is 9 print(many_to_one(-3)) # Output is also 9 (Same output for different inputs)
Onto (Surjective) Function
An onto function ensures that every possible output in the codomain has at least one input mapped to it.
1234567891011import numpy as np # Onto Function: f(x) = tan(x) def onto(x): return np.tan(x) # Example Outputs print("\nOnto Function Outputs:") print(onto(1)) # Output is approximately 1.557 print(onto(-1)) # Output is approximately -2.185
Into Function
An into function means not all values in the codomain are covered—some outputs remain unused.
12345678910import numpy as np # Into Function: f(x) = sin(x) (Only outputs between -1 and 1) def into(x): return np.sin(x) # Example Outputs print("\nInto Function Outputs:") print(into(0)) # Output is approximately 0 print(into(np.pi / 2)) # Output is approximately 1
Bijective Function (One-to-One & Onto)
A bijective function is both one-to-one and onto, meaning it is invertible.
12345678# Bijective Function: f(x) = x def bijective(x): return x # Example Outputs print("\nBijective Function Outputs:") print(bijective(3)) # Output is 3 print(bijective(-4)) # Output is -4
フィードバックありがとうございます!
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください