A package for set operations including union, intersection, difference, and symmetric difference.
Project description
Set_Function
A comprehensive Python package for mathematical set operations including union, intersection, difference, symmetric difference, complement, cartesian product, and power set operations.
- PyPI package: https://pypi.org/project/himpunan-discrete-sets-afl2/
- GitHub Repository: https://github.com/1nnocentia/set_function
- Free software: MIT License
Features
- Complete Set Operations: Union, intersection, difference, symmetric difference
- Advanced Operations: Complement, cartesian product, power set
- Set Relationships: Subset, superset, equality checking
- Element Operations: Add elements, membership testing
- Intuitive Syntax: Uses Python operators for natural mathematical notation
- Type Safety: Built-in type checking and error handling
- Comprehensive Testing: Full test suite with 100% coverage
Installation
pip install himpunan-discrete-sets-afl2
Quick Start
from set_function.set_function import Himpunan
# Create sets
S = Himpunan([1, 2, 3, 4, 5, 6, 7, 8, 9]) # Universal set
h1 = Himpunan([1, 2, 3])
h2 = Himpunan([3, 4, 5])
# Basic operations
print(len(h1)) # Output: 3
print(3 in h1) # Output: True
print(h1 == h2) # Output: False
Usage Examples
Basic Set Properties
from set_function.set_function import Himpunan
# Creating sets
h1 = Himpunan([1, 2, 3])
h2 = Himpunan([3, 4, 5])
# Length and membership
print(len(h1)) # 3
print(3 in h1) # True
print(6 in h1) # False
# Set equality
print(h1 == h2) # False
print(h1 == Himpunan([1, 2, 3])) # True
Adding Elements
h1 = Himpunan([1, 2, 3])
h1 += 4 # Add element 4 to h1
print(h1) # Himpunan({1, 2, 3, 4})
Set Operations
Union (Addition: +)
h1 = Himpunan([1, 2, 3, 4])
h2 = Himpunan([3, 4, 5])
h_union = h1 + h2 # Union
print(h_union) # Himpunan({1, 2, 3, 4, 5})
Intersection (Division: /)
h1 = Himpunan([1, 2, 3, 4])
h2 = Himpunan([3, 4, 5])
h_intersection = h1 / h2 # Intersection
print(h_intersection) # Himpunan({3, 4})
Difference (Subtraction: -)
h1 = Himpunan([1, 2, 3, 4])
h2 = Himpunan([3, 4, 5])
h_difference = h1 - h2 # Difference
print(h_difference) # Himpunan({1, 2})
Symmetric Difference (Multiplication: *)
h1 = Himpunan([1, 2, 3])
h2 = Himpunan([3, 4, 5])
h_sym_diff = h1 * h2 # Symmetric difference
print(h_sym_diff) # Himpunan({1, 2, 4, 5})
Advanced Operations
Complement
S = Himpunan([1, 2, 3, 4, 5, 6, 7, 8, 9]) # Universal set
h1 = Himpunan([1, 2, 3, 4])
h_complement = h1.komplement(S) # Complement
print(h_complement) # Himpunan({5, 6, 7, 8, 9})
Power Set
h1 = Himpunan([1, 2, 3, 4])
power_set = abs(h1) # Power set (all subsets)
print(len(power_set)) # 16 (2^4 subsets)
Cartesian Product
h1 = Himpunan([1, 2])
h2 = Himpunan(['a', 'b'])
cartesian = h1 ** h2 # Cartesian product
print(cartesian) # Himpunan({(1, 'a'), (1, 'b'), (2, 'a'), (2, 'b')})
Set Relationships
Subset and Superset
h1 = Himpunan([1, 2])
h2 = Himpunan([1, 2, 3, 4])
print(h1 <= h2) # True (h1 is subset of h2)
print(h1 < h2) # True (h1 is proper subset of h2)
print(h2 >= h1) # True (h2 is superset of h1)
print(h2 > h1) # True (h2 is proper superset of h1)
Set Equality
h1 = Himpunan([1, 2, 3])
h2 = Himpunan([3, 2, 1]) # Order doesn't matter
print(h1 // h2) # True (sets are equal)
print(h1 == h2) # True (alternative equality check)
Complete Example
from set_function.set_function import Himpunan
# Define universal set and subsets
S = Himpunan([1, 2, 3, 4, 5, 6, 7, 8, 9])
h1 = Himpunan([1, 2, 3])
h2 = Himpunan([3, 4, 5])
# Basic properties
print(f"Length of h1: {len(h1)}") # 3
print(f"3 in h1: {3 in h1}") # True
print(f"h1 equals h2: {h1 == h2}") # False
# Add element
h1 += 4
print(f"h1 after adding 4: {h1}") # {1, 2, 3, 4}
# Set operations
intersection = h1 / h2
union = h1 + h2
difference = h1 - h2
complement = h1.komplement(S)
power_set = abs(h1)
print(f"Intersection: {intersection}") # {3, 4}
print(f"Union: {union}") # {1, 2, 3, 4, 5}
print(f"Difference: {difference}") # {1, 2}
print(f"Complement: {complement}") # {5, 6, 7, 8, 9}
print(f"Power set size: {len(power_set)}") # 16
Operator Reference
| Operation | Operator | Method | Description |
|---|---|---|---|
| Union | + |
__add__ |
Elements in either set |
| Intersection | / |
__truediv__ |
Elements in both sets |
| Difference | - |
__sub__ |
Elements in first but not second |
| Symmetric Difference | * |
__mul__ |
Elements in either set but not both |
| Cartesian Product | ** |
__pow__ |
All ordered pairs between sets |
| Subset | <= |
__le__ |
First set is subset of second |
| Proper Subset | < |
__lt__ |
First set is proper subset of second |
| Superset | >= |
__ge__ |
First set is superset of second |
| Proper Superset | > |
__gt__ |
First set is proper superset of second |
| Equality | == |
__eq__ |
Sets contain same elements |
| Set Equality | // |
__floordiv__ |
Alternative equality check |
| Add Element | += |
__iadd__ |
Add single element to set |
| Power Set | abs() |
__abs__ |
All possible subsets |
| Complement | .komplement() |
komplement |
Elements in universal set but not in this set |
Error Handling
The package includes comprehensive error handling:
h = Himpunan([1, 2, 3])
# These will raise TypeError
try:
result = h + 5 # Cannot union with non-Himpunan
except TypeError as e:
print(e) # "Operasi hanya bisa dilakukan antar Himpunan"
Credits
This package was created with Cookiecutter and the audreyfeldroy/cookiecutter-pypackage project template.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file himpunan_discrete_sets_afl2-0.1.1.tar.gz.
File metadata
- Download URL: himpunan_discrete_sets_afl2-0.1.1.tar.gz
- Upload date:
- Size: 18.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a8d1b470a6fee7bfcb3dce3bf4c058be1f0a3b957789ac0e476d8b00c1333bf5
|
|
| MD5 |
aeb54089f40d7935add2fcf955d53e60
|
|
| BLAKE2b-256 |
b81a379c595d0f4d694d3c7ab5bce4bfb6a85caf24d7bea0218fe34ea9f9e0c1
|
File details
Details for the file himpunan_discrete_sets_afl2-0.1.1-py3-none-any.whl.
File metadata
- Download URL: himpunan_discrete_sets_afl2-0.1.1-py3-none-any.whl
- Upload date:
- Size: 9.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3c4e3849d56a117af074d2c1ceb895cf890de61be0b447d439a0eb7287e6726b
|
|
| MD5 |
b9a803dc3a49fae94299daa062560cee
|
|
| BLAKE2b-256 |
f5746f0ee9473a923e622772ff9c657ee113aadd76b18b26d3ca89451613ee9e
|