Discrete Veerapat
DiscreteVRP เวอร์ชัน 0.1.5 คือ Python package สำหรับฝึกเขียนโปรแกรมและเรียนรู้คณิตศาสตร์ดีสครีต (Discrete Mathematics) ในระดับเบื้องต้น เหมาะสำหรับนักศึกษาที่กำลังเริ่มเรียน Python
Package นี้มี 3 โมดูลหลัก:
countingสำหรับหลักการนับ โครงสร้างข้อมูล และ recursionnumber_theoryสำหรับทฤษฎีจำนวน, SymPy และ OOP เบื้องต้นgraph_theoryสำหรับทฤษฎีกราฟ โดยใช้ NetworkX, NumPy, Pandas และ Matplotlib
การติดตั้ง
ติดตั้งจากโฟลเดอร์ package:
pip install .
หรือติดตั้งแบบแก้ไขโค้ดได้ทันทีระหว่างพัฒนา:
pip install -e .
การเริ่มต้นใช้งาน
import DiscreteVRP as dv
print(dv.__version__) # 0.1.5
1. Counting Module
โมดูล counting ใช้สำหรับแฟกทอเรียล การเรียงสับเปลี่ยน การจัดหมู่ และหลักการนับพื้นฐาน
import DiscreteVRP as dv
print(dv.factorial(5)) # 120
print(dv.permutation(5, 2)) # 20
print(dv.combination(5, 2)) # 10
print(dv.stars_and_bars(5, 3)) # 21
print(dv.stars_and_bars_at_least_one(5, 3)) # 6
print(dv.floor_function(3.7)) # 3
print(dv.ceiling_function(3.2)) # 4
print(dv.inclusion_exclusion_2(10, 15, 3)) # 22
print(dv.pigeonhole_principle(10, 3))
# At least 1 hole has 4 pigeons
Recursive Function และ Memoization
import DiscreteVRP as dv
print(dv.fibonacci(10)) # 55
factorial() และ fibonacci() ใช้ recursive function โดยใช้ memoization เพื่อเก็บค่าที่เคยคำนวณแล้ว
String, Set และ Dictionary
import DiscreteVRP as dv
print(dv.count_unique_permutations("AAB")) # 3
print(dv.word_frequency("python is fun python"))
# {'python': 2, 'is': 1, 'fun': 1}
Variable-length Arguments
import DiscreteVRP as dv
print(dv.sum_numbers(1, 2, 3, 4, 5)) # 15
2. Number Theory Module
โมดูล number_theory ใช้สำหรับจำนวนเฉพาะ ห.ร.ม. ค.ร.น. Euclidean Algorithm และเลขมอดูลาร์
import DiscreteVRP as dv
print(dv.is_even(4)) # True
print(dv.is_odd(7)) # True
print(dv.is_prime(29)) # True
print(dv.is_composite(10)) # True
print(dv.division_algorithm(70, 15))
# 70 = 4(15) + 10
print(dv.gcd(48, 18)) # 6
print(dv.lcm(12, 18)) # 36
Euclidean Algorithm, Set และ SymPy
import DiscreteVRP as dv
dv.euclidean_algorithm_steps(48, 18)
steps, result = dv.euclidean_algorithm_set(48, 18)
print(steps)
print(result) # 6
print(dv.euclidean_algorithm_pretty(48, 18))
euclidean_algorithm_set() แสดงตัวอย่างการใช้ set ส่วน euclidean_algorithm_pretty() ใช้ SymPy เพื่อจัดรูปแบบสมการให้อ่านง่าย
Extended Euclidean Algorithm, Modular Inverse และ CRT
import DiscreteVRP as dv
print(dv.extended_gcd(17, 43))
print(dv.modular_inverse(3, 7)) # 5
print(dv.chinese_remainder_theorem([2, 3, 2], [3, 5, 7]))
# 23
OOP, Magic Methods และ Inheritance
import DiscreteVRP as dv
result = dv.MathResult("GCD", dv.gcd(48, 18))
print(result) # GCD: 6
print(repr(result)) # MathResult(topic='GCD', value=6)
MathResult มี __str__() และ __repr__() เป็นตัวอย่าง magic methods
3. Graph Theory Module
โมดูล graph_theory ใช้ NetworkX สำหรับสร้างและวิเคราะห์กราฟ ใช้ Matplotlib สำหรับวาดกราฟ ใช้ NumPy สำหรับ adjacency matrix และใช้ Pandas สำหรับตาราง edge list
สร้างและวาดกราฟ
import DiscreteVRP as dv
graph = dv.create_graph([
("A", "B"),
("A", "C"),
("B", "C"),
("C", "D"),
])
dv.draw_graph(graph, title="My First Graph")
Connected Components และ NumPy Matrix
import DiscreteVRP as dv
graph = dv.create_graph([
("A", "B"),
("B", "C"),
("D", "E"),
])
print(dv.is_connected(graph)) # False
print(dv.connected_components(graph))
matrix = dv.adjacency_matrix(graph, ["A", "B", "C", "D", "E"])
print(matrix)
Pandas และ File I/O
import DiscreteVRP as dv
edges = [
("A", "B", 4),
("B", "C", 2),
]
print(dv.edges_to_dataframe(edges))
dv.save_edges_to_file(edges, "edges.txt")
graph = dv.load_graph_from_file("edges.txt", weighted=True)
print(list(graph.edges(data=True)))
Euler Trail, Tree และ Shortest Path
import DiscreteVRP as dv
triangle = dv.create_graph([
("A", "B"),
("B", "C"),
("C", "A"),
])
print(dv.has_euler_path(triangle)) # True
print(dv.has_euler_circuit(triangle)) # True
print(dv.euler_path(triangle))
import DiscreteVRP as dv
weighted_graph = dv.create_graph(
[
("A", "B", 4),
("A", "C", 2),
("B", "C", 1),
("B", "D", 5),
("C", "D", 8),
],
weighted=True,
)
path, distance = dv.shortest_path(weighted_graph, "A", "D")
print(path) # ['A', 'C', 'B', 'D']
print(distance) # 8
Minimum Spanning Tree และ Algorithm Comparison
import DiscreteVRP as dv
mst = dv.minimum_spanning_tree(weighted_graph, algorithm="kruskal")
print(list(mst.edges(data=True)))
print(dv.compare_mst_algorithms(weighted_graph))
compare_mst_algorithms() เปรียบเทียบเวลาในการทำงานของ Kruskal's Algorithm และ Prim's Algorithm
การทดสอบ
รันโค้ดทดสอบในแต่ละโมดูล:
python -m DiscreteVRP.counting
python -m DiscreteVRP.number_theory
python -m DiscreteVRP.graph_theory
รัน automated tests:
python -m unittest discover -s tests
การ Build และเผยแพร่บน PyPI
ติดตั้งเครื่องมือ:
pip install build twine
สร้างไฟล์ distribution:
python -m build
ตรวจสอบไฟล์:
twine check dist/*
อัปโหลดไปยัง PyPI:
twine upload dist/*
หมายเหตุ
- Package นี้เน้นโค้ดสั้น อ่านง่าย และเหมาะกับการเรียนรู้
adjacency_matrix()คืนค่าเป็นnumpy.ndarray- Graph Theory เหมาะสำหรับการทดลองกับกราฟขนาดเล็ก
- ไม่มี PARI/cypari2 จึงสามารถติดตั้งบน Windows ได้ง่ายขึ้น
Metadata
Release files for DiscreteVRP 0.1.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| discretevrp-0.1.5.tar.gz | 9.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| discretevrp-0.1.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 21.1 kB
Release files / discretevrp-0.1.5.tar.gz
| Download URL | discretevrp-0.1.5.tar.gz |
|---|---|
| Size | 9.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3de0032eac39794860db19bfc03fa8ec977b12f319e5e49cca3f71adcae7cfcc
|
|
BLAKE2b-256 checksum How to use checksums |
b72c5d6ae25e460283bffed9de51c6480be81ccf6fd0eea21a23ac899ad44eee
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Release files / discretevrp-0.1.5-py3-none-any.whl
| Download URL | discretevrp-0.1.5-py3-none-any.whl |
|---|---|
| Size | 11.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
520f72d41d53c45aad37490e56e2a64ab6969f68cfc865e33171c61f62d92b65
|
|
BLAKE2b-256 checksum How to use checksums |
bf6856a69ccb09dbe5f195fd8c6f0972eb14d0ee29c1d86ebf7b6371ef538766
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|