Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add pi caculation #57

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"

[project]
name = "simple_functions" # 你的包名称
version = "0.1.0" # 包的版本
description = "A package for simple math functions"
authors = [{name = "Your Name", email = "[email protected]"}]
license = {text = "MIT"} # 包的许可证类型
dependencies = ["numpy", "functools"] # 运行时依赖
readme = "README.md" # 包含的 README 文件

[tool.setuptools.packages.find]
where = ["simple_functions"]

3 changes: 3 additions & 0 deletions simple_functions.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Metadata-Version: 2.1
Name: simple_functions
Version: 0.1.0
12 changes: 12 additions & 0 deletions simple_functions.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
README.md
pyproject.toml
setup.py
simple_functions/__init__.py
simple_functions/cacu_pi.py
simple_functions/functions1.py
simple_functions.egg-info/PKG-INFO
simple_functions.egg-info/SOURCES.txt
simple_functions.egg-info/dependency_links.txt
simple_functions.egg-info/top_level.txt
tests/test_constants.py
tests/test_simple_functions.py
1 change: 1 addition & 0 deletions simple_functions.egg-info/dependency_links.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions simple_functions.egg-info/top_level.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
simple_functions
3 changes: 2 additions & 1 deletion simple_functions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .functions1 import * # noqa
from simple_functions.functions1 import my_sum, my_prod # noqa
from simple_functions.cacu_pi import pi, rsum

try:
from importlib.metadata import version, PackageNotFoundError # Python 3.8+
Expand Down
Binary file not shown.
Binary file added simple_functions/__pycache__/cacu_pi.cpython-312.pyc
Binary file not shown.
Binary file not shown.
18 changes: 18 additions & 0 deletions simple_functions/cacu_pi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

from numpy import sqrt
from functions1 import factorial
from functools import cache

__all__ = ['pi']


def pi(terms=1):
return 1./(2.*sqrt(2.)/9801.*rsum(terms))


@cache # 递归/重复调用时记得使用 @cache
def rsum(n):
t = factorial(4*n)*(1103+26390*n)/(factorial(n)**4*396**(4*n))
return t + rsum(n-1) if n else t

print(pi(100))
15 changes: 14 additions & 1 deletion simple_functions/functions1.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@

__all__ = ['my_sum']
from numpy import sqrt
from functools import cache

__all__ = ['my_sum', 'my_prod'] # module rsum 只是服务于计算pi,因此不需要导入


def my_sum(iterable):
tot = 0
for i in iterable:
tot += i
return tot

def my_prod(iterable):
result = 1
for i in iterable:
result *= i
return result

@cache
def factorial(n):
return n * factorial(n-1) if n else 1
12 changes: 12 additions & 0 deletions simple_functions/simple_functions.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Metadata-Version: 2.1
Name: simple_functions
Version: 0.1.0
Summary: A package for simple math functions
Author-email: Your Name <[email protected]>
License: MIT
Description-Content-Type: text/markdown
Requires-Dist: numpy
Requires-Dist: pandas

# CI MPM
Toy repo with some simple functions for the CI lecture
10 changes: 10 additions & 0 deletions simple_functions/simple_functions.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
README.md
pyproject.toml
setup.py
simple_functions/simple_functions.egg-info/PKG-INFO
simple_functions/simple_functions.egg-info/SOURCES.txt
simple_functions/simple_functions.egg-info/dependency_links.txt
simple_functions/simple_functions.egg-info/requires.txt
simple_functions/simple_functions.egg-info/top_level.txt
tests/test_constants.py
tests/test_simple_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

2 changes: 2 additions & 0 deletions simple_functions/simple_functions.egg-info/requires.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
numpy
pandas
1 change: 1 addition & 0 deletions simple_functions/simple_functions.egg-info/top_level.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Binary file not shown.
Binary file not shown.
13 changes: 13 additions & 0 deletions tests/test_constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import numpy as np
import pytest

from simple_functions import pi


class TestPi(object):
'''Class to test our constants are computed correctly'''

def test_pi(self):
'''Test computation of pi'''
my_pi = pi(2)
assert np.isclose(my_pi, np.pi, atol=1e-12)
2 changes: 1 addition & 1 deletion tests/test_simple_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ class TestSimpleFunctions(object):
def test_my_add(self, iterable, expected):
'''Test our add function'''
isum = my_sum(iterable)
assert isum == expected
assert isum == expected