From 3d2cffb1ae30466245b63e18af69a402e0aa072d Mon Sep 17 00:00:00 2001 From: 2097152 Date: Sun, 24 May 2020 15:55:12 +0800 Subject: [PATCH 01/30] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20go?= =?UTF-8?q?lang?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- golang/simple.go | 28 -------------------- golang/simple_test.go | 60 ------------------------------------------- 2 files changed, 88 deletions(-) delete mode 100644 golang/simple.go delete mode 100644 golang/simple_test.go diff --git a/golang/simple.go b/golang/simple.go deleted file mode 100644 index c0d1114..0000000 --- a/golang/simple.go +++ /dev/null @@ -1,28 +0,0 @@ -package mathprolib -import ( - "math" -) -const pi = 3.14159265358979 -const e = 2.718281828459045 -func Pow(base float64,exp float64)(float64){ - return math.Pow(base,exp) -} -func Root(base float64,exp float64)(float64){ - return Pow(base,1/exp) -} -func Sqrt(num float64)(float64){ - return Root(num,2.0) -} -func Round(num float64)(int64){ - return int64(math.Floor(num+0.5)) -} -func Floor(num float64)(int64){ - return int64(math.Floor(num)) -} -func Ceil(num float64)(int64){ - return int64(math.Ceil(num)) -} -func Abs(num float64)(float64){ - if(num>0){return num} - return -num -} diff --git a/golang/simple_test.go b/golang/simple_test.go deleted file mode 100644 index f783dcc..0000000 --- a/golang/simple_test.go +++ /dev/null @@ -1,60 +0,0 @@ -package mathprolib -import "testing" - -//Tests - - -//Tested.ok -//1:Fail,3.326s -//2:ok,3.369s -func TestPowandSqrtandRoot(t *testing.T){ - if pr:=Pow(2.0,3.0);pr!=8.0{ - t.Fatal("Failed,result=",pr,".The result should be 4.") - }else{ - t.Log("Success.",pr) - } - if sr:=Sqrt(64.0);sr!=8.0{ - t.Fatal("Failed,result=",sr,".The result should be 8.") - }else{ - t.Log("Success.",sr) - } - if rr:=Root(27.0,3.0);!(2.999999 Date: Sun, 24 May 2020 15:55:19 +0800 Subject: [PATCH 02/30] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20LI?= =?UTF-8?q?CENSE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LICENSE | 21 --------------------- 1 file changed, 21 deletions(-) delete mode 100644 LICENSE diff --git a/LICENSE b/LICENSE deleted file mode 100644 index 3166dfb..0000000 --- a/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) 2020 LAI-1048576 - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -- Gitee From e8ca0bbae5fa4d065afc27cf5ad1cc1357b1c20b Mon Sep 17 00:00:00 2001 From: 2097152 Date: Sun, 24 May 2020 15:55:25 +0800 Subject: [PATCH 03/30] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20in?= =?UTF-8?q?dex.html?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.html | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 index.html diff --git a/index.html b/index.html deleted file mode 100644 index 88276a0..0000000 --- a/index.html +++ /dev/null @@ -1,9 +0,0 @@ - - - - - MATHLIB官网 - - - - -- Gitee From ad6ad9875bbac31b1894d7ac6ac732d376946260 Mon Sep 17 00:00:00 2001 From: 2097152 Date: Sun, 24 May 2020 15:55:40 +0800 Subject: [PATCH 04/30] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=20python=20?= =?UTF-8?q?=E4=B8=BA=20mathprolib?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- {python => mathprolib}/__init__.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {python => mathprolib}/__init__.py (100%) diff --git a/python/__init__.py b/mathprolib/__init__.py similarity index 100% rename from python/__init__.py rename to mathprolib/__init__.py -- Gitee From fe3eda6722755540d02c289a80dc98f639d9b541 Mon Sep 17 00:00:00 2001 From: 2097152 Date: Sun, 24 May 2020 16:15:42 +0800 Subject: [PATCH 05/30] update README.md. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d3f5a12..fee6bc5 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ - +本分支是Python的mathprolib包。更多请见其它分支 \ No newline at end of file -- Gitee From 07605ca3014b2464c933a0df15bb25fe34771af2 Mon Sep 17 00:00:00 2001 From: LAI-1048576 <7530270+index-1048576@user.noreply.gitee.com> Date: Tue, 26 May 2020 08:27:22 +0800 Subject: [PATCH 06/30] =?UTF-8?q?=E4=B8=89=E8=A7=92=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mathprolib/__init__.py | 55 ++++++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 16 deletions(-) diff --git a/mathprolib/__init__.py b/mathprolib/__init__.py index 0132b43..70392ef 100644 --- a/mathprolib/__init__.py +++ b/mathprolib/__init__.py @@ -6,44 +6,67 @@ import math from functools import total_ordering REAL = float - -e = math.e -π = math.pi -Ф = (math.sqrt(5)-1)/2 +Ф = (math.sqrt(5) - 1) / 2 γ = 0.5772156649015328 + +sin, cos, tan = lambda n: math.sin(math.radians(n)), lambda n: math.cos(math.radians(n)), lambda n: math.tan( + math.radians(n)) +sinh,cosh,tanh = math.sin,math.cos,math.tan +asin,acos,atan = math.asin,math.acos,math.atan +deg,rad = math.degrees,math.radians +ln,log,log2 = math.log,math.log10,math.log2 +π,e,fact = math.pi,math.e,math.factorial + @total_ordering class INF: def __lt__(self, other): return False def __eq__(self, other): - return isinstance(other,INF) + return isinstance(other, INF) + + def __str__(self): + return "∞" + + def __neg__(self): + return NEGINF() + + +@total_ordering +class NEGINF: + def __lt__(self, other): + return True + + def __eq__(self, other): + return isinstance(other, NEGINF) def __str__(self): return "∞" + Infinity = INF() -def root(num:REAL,exp:REAL): + +def root(num: REAL, exp: REAL): """ - + :rtype: REAL :param num: 开方底数 :param exp: 开方指数 :return: 结果 - + """ - result = num**(1/exp) - if not isinstance(result,complex): - return num**(1/exp) + result = num ** (1 / exp) + if not isinstance(result, complex): + return num ** (1 / exp) else: return math.nan -def sqrt(num:REAL): + +def sqrt(num: REAL): """ - - :param num:要开方的数 + + :param num:要开方的数 :return: 结果 """ - return root(num,2) - + return root(num, 2) -- Gitee From 5d68166a9c1b67208f54a834f1a2468331611109 Mon Sep 17 00:00:00 2001 From: LAI-1048576 <7530270+index-1048576@user.noreply.gitee.com> Date: Tue, 26 May 2020 08:32:32 +0800 Subject: [PATCH 07/30] =?UTF-8?q?=E6=9C=80=E5=A4=A7=E5=85=AC=E7=BA=A6?= =?UTF-8?q?=E6=95=B0&=E6=9C=80=E5=B0=8F=E5=85=AC=E5=80=8D=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mathprolib/__init__.py | 152 +++++++++++++++++++++++------------------ 1 file changed, 85 insertions(+), 67 deletions(-) diff --git a/mathprolib/__init__.py b/mathprolib/__init__.py index 70392ef..a1b2067 100644 --- a/mathprolib/__init__.py +++ b/mathprolib/__init__.py @@ -1,72 +1,90 @@ -# Version: Test -# Author:LAI-1048576,Toby-LAI -# -*- coding:utf-8 -*- - +from tkinter import * +import easygui import math -from functools import total_ordering - -REAL = float -Ф = (math.sqrt(5) - 1) / 2 -γ = 0.5772156649015328 - -sin, cos, tan = lambda n: math.sin(math.radians(n)), lambda n: math.cos(math.radians(n)), lambda n: math.tan( - math.radians(n)) +w = Tk() +equation,final = StringVar(),StringVar() +def func_set(name): + a = easygui.integerbox("数1?") + b = easygui.integerbox("数2?") + equation.set(f"{name}("+equation.get()+f"{a},{b})") +def lcm(x, y): + return x*y//gcd(x,y) +def gcd(x, y): + smaller = x if x < y else y + hcf = [i for i in range(1,smaller+1) if x % i == 0 and y % i == 0][-1] + return hcf +def num(n): + equation.set(equation.get() + str(n)) +sin,cos,tan = lambda n: math.sin(math.radians(n)),lambda n: math.cos(math.radians(n)),lambda n: math.tan(math.radians(n)) sinh,cosh,tanh = math.sin,math.cos,math.tan asin,acos,atan = math.asin,math.acos,math.atan -deg,rad = math.degrees,math.radians +deg,rad,sqrt = math.degrees,math.radians,math.sqrt +binary,october,hexadecimal = lambda n: bin(n)[2:],lambda n: oct(n)[2:],lambda n: hex(n)[2:] ln,log,log2 = math.log,math.log10,math.log2 π,e,fact = math.pi,math.e,math.factorial - -@total_ordering -class INF: - def __lt__(self, other): - return False - - def __eq__(self, other): - return isinstance(other, INF) - - def __str__(self): - return "∞" - - def __neg__(self): - return NEGINF() - - -@total_ordering -class NEGINF: - def __lt__(self, other): - return True - - def __eq__(self, other): - return isinstance(other, NEGINF) - - def __str__(self): - return "∞" - - -Infinity = INF() - - -def root(num: REAL, exp: REAL): - """ - - :rtype: REAL - :param num: 开方底数 - :param exp: 开方指数 - :return: 结果 - - """ - result = num ** (1 / exp) - if not isinstance(result, complex): - return num ** (1 / exp) - else: - return math.nan - - -def sqrt(num: REAL): - """ - - :param num:要开方的数 - :return: 结果 - """ - return root(num, 2) +decimal_fraction = lambda n: n - int(n) +def ce(): + equation.set('') + final.set('') +def calculate(): + try: + final.set(f"={str(eval(equation.get().replace('×','*').replace('÷','/').replace('^','**').replace('mod','%')))}") + except Exception: + easygui.exceptionbox() + ce() +def function(name): + equation.set(name + '(' + equation.get() + ')') +w.geometry('1500x1000') +Button(w, text=0, command=lambda: num(0), width=7, height=2, font=('Arial',20)).grid(row=1,column=0) +Button(w, text=1, command=lambda: num(1), width=7, height=2, font=('Arial',20)).grid(row=1,column=1) +Button(w, text=2, command=lambda: num(2), width=7, height=2, font=('Arial',20)).grid(row=1,column=2) +Button(w, text=3, command=lambda: num(3), width=7, height=2, font=('Arial',20)).grid(row=2,column=0) +Button(w, text=4, command=lambda: num(4), width=7, height=2, font=('Arial',20)).grid(row=2,column=1) +Button(w, text=5, command=lambda: num(5), width=7, height=2, font=('Arial',20)).grid(row=2,column=2) +Button(w, text=6, command=lambda: num(6), width=7, height=2, font=('Arial',20)).grid(row=3,column=0) +Button(w, text=7, command=lambda: num(7), width=7, height=2, font=('Arial',20)).grid(row=3,column=1) +Button(w, text=8, command=lambda: num(8), width=7, height=2, font=('Arial',20)).grid(row=3,column=2) +Button(w, text=9, command=lambda: num(9), width=7, height=2, font=('Arial',20)).grid(row=4,column=1) +Button(w, text='.', command=lambda: num('.'), width=7, height=2, font=('Arial',20)).grid(row=5,column=3) +Button(w, text='+', command=lambda: num('+'), width=7, height=2, font=('Arial',20)).grid(row=1, column=3) +Button(w, text='-', command=lambda: num('-'), width=7, height=2, font=('Arial',20)).grid(row=2, column=3) +Button(w, text='×', command=lambda: num('×'), width=7, height=2, font=('Arial',20)).grid(row=3, column=3) +Button(w, text='÷', command=lambda: num('÷'), width=7, height=2, font=('Arial',20)).grid(row=4, column=3) +Button(w, text='^', command=lambda: num('^'), width=7, height=2, font=('Arial',20)).grid(row=4, column=2) +Button(w, text='(', command=lambda: num('('), width=7, height=2, font=('Arial',20)).grid(row=5, column=1) +Button(w, text=')', command=lambda: num(')'), width=7, height=2, font=('Arial',20)).grid(row=5, column=2) +Button(w, text='阶乘', command=lambda: function('fact'), width=7, height=2, font=('Arial',20)).grid(row=5, column=0) +Button(w, text='sin(角度)', command=lambda: function('sin'), width=7, height=2, font=('Arial',20)).grid(row=6, column=0) +Button(w, text='cos(角度)', command=lambda: function('cos'), width=7, height=2, font=('Arial',20)).grid(row=6, column=1) +Button(w, text='tan(角度)', command=lambda: function('tan'), width=7, height=2, font=('Arial',20)).grid(row=6, column=2) +Button(w, text='sin(弧度)', command=lambda: function('sinh'), width=7, height=2, font=('Arial',20)).grid(row=7, column=0) +Button(w, text='cos(弧度)', command=lambda: function('cosh'), width=7, height=2, font=('Arial',20)).grid(row=7, column=1) +Button(w, text='tan(弧度)', command=lambda: function('tanh'), width=7, height=2, font=('Arial',20)).grid(row=7, column=2) +Button(w, text='asin', command=lambda: function('asin'), width=7, height=2, font=('Arial',20)).grid(row=8, column=0) +Button(w, text='acos', command=lambda: function('acos'), width=7, height=2, font=('Arial',20)).grid(row=8, column=1) +Button(w, text='atan', command=lambda: function('atan'), width=7, height=2, font=('Arial',20)).grid(row=8, column=2) +Button(w, text='平方根', command=lambda: function('sqrt'), width=7, height=2, font=('Arial',20)).grid(row=10, column=0) +Button(w, text='弧度-角度', command=lambda: function('deg'), width=7, height=2, font=('Arial',20)).grid(row=9, column=0) +Button(w, text='角度-弧度', command=lambda: function('rad'), width=7, height=2, font=('Arial',20)).grid(row=9, column=1) +Button(w, text='绝对值', command=lambda: function('abs'), width=7, height=2, font=('Arial',20)).grid(row=9, column=2) +Button(w, text='二进制', command=lambda: function('binary'), width=7, height=2, font=('Arial',20)).grid(row=10, column=1) +Button(w, text='八进制', command=lambda: function('october'), width=7, height=2, font=('Arial',20)).grid(row=10, column=2) +Button(w, text='十六进制', command=lambda: function('hexadecimal'), width=7, height=2, font=('Arial',20)).grid(row=10, column=3) +Button(w, text='log2', command=lambda: function('log2'), width=7, height=2, font=('Arial',20)).grid(row=1, column=4) +Button(w, text='log10', command=lambda: function('log'), width=7, height=2, font=('Arial',20)).grid(row=1, column=5) +Button(w, text='ln', command=lambda: function('ln'), width=7, height=2, font=('Arial',20)).grid(row=1, column=6) +Button(w, text='π', command=lambda: num('π'), width=7, height=2, font=('Arial',20)).grid(row=2, column=6) +Button(w, text='e', command=lambda: num('e'), width=7, height=2, font=('Arial',20)).grid(row=2, column=4) +Button(w, text='CE', command=ce, width=7, height=2, font=('Arial',20)).grid(row=3, column=5) +Button(w, text='取余', command=lambda: num(' mod '), width=7, height=2, font=('Arial',20)).grid(row=1, column=7) +Button(w, text='取整', command=lambda: function('int'), width=7, height=2, font=('Arial',20)).grid(row=2, column=7) +Button(w, text='取小', command=lambda: function('decimal_fraction'), width=7, height=2, font=('Arial',20)).grid(row=2, column=5) +Button(w, text='←', command=lambda: equation.set(equation.get()[:-1]), width=7, height=2, font=('Arial',20)).grid(row=2, column=6) +Button(w, text='=', command=calculate, width=7, height=2, font=('Arial',20)).grid(row=4,column=0) +Button(w, text='最小公倍数', command=lambda: func_set('lcm'), width=7, height=2, font=('Arial',20)).grid(row=3,column=4) +Button(w, text='最大公约数', command=lambda: func_set('gcd'), width=7, height=2, font=('Arial',20)).grid(row=3,column=5) + +Label(w, textvar=final, font=('Arial',30)).grid(row=0,column=6) +Label(w, textvar=equation, font=('Arial',30)).grid(row=0,column=2) +w.title('计算器') +w.mainloop() -- Gitee From e91e5f3ab7391755620a8836ba20204130dc9f06 Mon Sep 17 00:00:00 2001 From: LAI-1048576 <7530270+index-1048576@user.noreply.gitee.com> Date: Tue, 26 May 2020 08:33:24 +0800 Subject: [PATCH 08/30] =?UTF-8?q?=E5=95=8A=E8=BF=99=EF=BC=8C=E5=86=99?= =?UTF-8?q?=E9=94=99=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mathprolib/__init__.py | 158 +++++++++++++++++++---------------------- 1 file changed, 73 insertions(+), 85 deletions(-) diff --git a/mathprolib/__init__.py b/mathprolib/__init__.py index a1b2067..5b68540 100644 --- a/mathprolib/__init__.py +++ b/mathprolib/__init__.py @@ -1,90 +1,78 @@ -from tkinter import * -import easygui +# Version: Test +# Author:LAI-1048576,Toby-LAI +# -*- coding:utf-8 -*- + import math -w = Tk() -equation,final = StringVar(),StringVar() -def func_set(name): - a = easygui.integerbox("数1?") - b = easygui.integerbox("数2?") - equation.set(f"{name}("+equation.get()+f"{a},{b})") -def lcm(x, y): - return x*y//gcd(x,y) -def gcd(x, y): - smaller = x if x < y else y - hcf = [i for i in range(1,smaller+1) if x % i == 0 and y % i == 0][-1] - return hcf -def num(n): - equation.set(equation.get() + str(n)) -sin,cos,tan = lambda n: math.sin(math.radians(n)),lambda n: math.cos(math.radians(n)),lambda n: math.tan(math.radians(n)) +from functools import total_ordering + +REAL = float +Ф = (math.sqrt(5) - 1) / 2 +γ = 0.5772156649015328 + +sin, cos, tan = lambda n: math.sin(math.radians(n)), lambda n: math.cos(math.radians(n)), lambda n: math.tan( + math.radians(n)) sinh,cosh,tanh = math.sin,math.cos,math.tan asin,acos,atan = math.asin,math.acos,math.atan -deg,rad,sqrt = math.degrees,math.radians,math.sqrt -binary,october,hexadecimal = lambda n: bin(n)[2:],lambda n: oct(n)[2:],lambda n: hex(n)[2:] +deg,rad = math.degrees,math.radians ln,log,log2 = math.log,math.log10,math.log2 π,e,fact = math.pi,math.e,math.factorial -decimal_fraction = lambda n: n - int(n) -def ce(): - equation.set('') - final.set('') -def calculate(): - try: - final.set(f"={str(eval(equation.get().replace('×','*').replace('÷','/').replace('^','**').replace('mod','%')))}") - except Exception: - easygui.exceptionbox() - ce() -def function(name): - equation.set(name + '(' + equation.get() + ')') -w.geometry('1500x1000') -Button(w, text=0, command=lambda: num(0), width=7, height=2, font=('Arial',20)).grid(row=1,column=0) -Button(w, text=1, command=lambda: num(1), width=7, height=2, font=('Arial',20)).grid(row=1,column=1) -Button(w, text=2, command=lambda: num(2), width=7, height=2, font=('Arial',20)).grid(row=1,column=2) -Button(w, text=3, command=lambda: num(3), width=7, height=2, font=('Arial',20)).grid(row=2,column=0) -Button(w, text=4, command=lambda: num(4), width=7, height=2, font=('Arial',20)).grid(row=2,column=1) -Button(w, text=5, command=lambda: num(5), width=7, height=2, font=('Arial',20)).grid(row=2,column=2) -Button(w, text=6, command=lambda: num(6), width=7, height=2, font=('Arial',20)).grid(row=3,column=0) -Button(w, text=7, command=lambda: num(7), width=7, height=2, font=('Arial',20)).grid(row=3,column=1) -Button(w, text=8, command=lambda: num(8), width=7, height=2, font=('Arial',20)).grid(row=3,column=2) -Button(w, text=9, command=lambda: num(9), width=7, height=2, font=('Arial',20)).grid(row=4,column=1) -Button(w, text='.', command=lambda: num('.'), width=7, height=2, font=('Arial',20)).grid(row=5,column=3) -Button(w, text='+', command=lambda: num('+'), width=7, height=2, font=('Arial',20)).grid(row=1, column=3) -Button(w, text='-', command=lambda: num('-'), width=7, height=2, font=('Arial',20)).grid(row=2, column=3) -Button(w, text='×', command=lambda: num('×'), width=7, height=2, font=('Arial',20)).grid(row=3, column=3) -Button(w, text='÷', command=lambda: num('÷'), width=7, height=2, font=('Arial',20)).grid(row=4, column=3) -Button(w, text='^', command=lambda: num('^'), width=7, height=2, font=('Arial',20)).grid(row=4, column=2) -Button(w, text='(', command=lambda: num('('), width=7, height=2, font=('Arial',20)).grid(row=5, column=1) -Button(w, text=')', command=lambda: num(')'), width=7, height=2, font=('Arial',20)).grid(row=5, column=2) -Button(w, text='阶乘', command=lambda: function('fact'), width=7, height=2, font=('Arial',20)).grid(row=5, column=0) -Button(w, text='sin(角度)', command=lambda: function('sin'), width=7, height=2, font=('Arial',20)).grid(row=6, column=0) -Button(w, text='cos(角度)', command=lambda: function('cos'), width=7, height=2, font=('Arial',20)).grid(row=6, column=1) -Button(w, text='tan(角度)', command=lambda: function('tan'), width=7, height=2, font=('Arial',20)).grid(row=6, column=2) -Button(w, text='sin(弧度)', command=lambda: function('sinh'), width=7, height=2, font=('Arial',20)).grid(row=7, column=0) -Button(w, text='cos(弧度)', command=lambda: function('cosh'), width=7, height=2, font=('Arial',20)).grid(row=7, column=1) -Button(w, text='tan(弧度)', command=lambda: function('tanh'), width=7, height=2, font=('Arial',20)).grid(row=7, column=2) -Button(w, text='asin', command=lambda: function('asin'), width=7, height=2, font=('Arial',20)).grid(row=8, column=0) -Button(w, text='acos', command=lambda: function('acos'), width=7, height=2, font=('Arial',20)).grid(row=8, column=1) -Button(w, text='atan', command=lambda: function('atan'), width=7, height=2, font=('Arial',20)).grid(row=8, column=2) -Button(w, text='平方根', command=lambda: function('sqrt'), width=7, height=2, font=('Arial',20)).grid(row=10, column=0) -Button(w, text='弧度-角度', command=lambda: function('deg'), width=7, height=2, font=('Arial',20)).grid(row=9, column=0) -Button(w, text='角度-弧度', command=lambda: function('rad'), width=7, height=2, font=('Arial',20)).grid(row=9, column=1) -Button(w, text='绝对值', command=lambda: function('abs'), width=7, height=2, font=('Arial',20)).grid(row=9, column=2) -Button(w, text='二进制', command=lambda: function('binary'), width=7, height=2, font=('Arial',20)).grid(row=10, column=1) -Button(w, text='八进制', command=lambda: function('october'), width=7, height=2, font=('Arial',20)).grid(row=10, column=2) -Button(w, text='十六进制', command=lambda: function('hexadecimal'), width=7, height=2, font=('Arial',20)).grid(row=10, column=3) -Button(w, text='log2', command=lambda: function('log2'), width=7, height=2, font=('Arial',20)).grid(row=1, column=4) -Button(w, text='log10', command=lambda: function('log'), width=7, height=2, font=('Arial',20)).grid(row=1, column=5) -Button(w, text='ln', command=lambda: function('ln'), width=7, height=2, font=('Arial',20)).grid(row=1, column=6) -Button(w, text='π', command=lambda: num('π'), width=7, height=2, font=('Arial',20)).grid(row=2, column=6) -Button(w, text='e', command=lambda: num('e'), width=7, height=2, font=('Arial',20)).grid(row=2, column=4) -Button(w, text='CE', command=ce, width=7, height=2, font=('Arial',20)).grid(row=3, column=5) -Button(w, text='取余', command=lambda: num(' mod '), width=7, height=2, font=('Arial',20)).grid(row=1, column=7) -Button(w, text='取整', command=lambda: function('int'), width=7, height=2, font=('Arial',20)).grid(row=2, column=7) -Button(w, text='取小', command=lambda: function('decimal_fraction'), width=7, height=2, font=('Arial',20)).grid(row=2, column=5) -Button(w, text='←', command=lambda: equation.set(equation.get()[:-1]), width=7, height=2, font=('Arial',20)).grid(row=2, column=6) -Button(w, text='=', command=calculate, width=7, height=2, font=('Arial',20)).grid(row=4,column=0) -Button(w, text='最小公倍数', command=lambda: func_set('lcm'), width=7, height=2, font=('Arial',20)).grid(row=3,column=4) -Button(w, text='最大公约数', command=lambda: func_set('gcd'), width=7, height=2, font=('Arial',20)).grid(row=3,column=5) - -Label(w, textvar=final, font=('Arial',30)).grid(row=0,column=6) -Label(w, textvar=equation, font=('Arial',30)).grid(row=0,column=2) -w.title('计算器') -w.mainloop() +def gcd(x, y): + smaller = x if x < y else y + g = [i for i in range(1,smaller+1) if x % i == 0 and y % i == 0][-1] + return g + +def lcm(x, y): + return x*y//gcd(x,y) +@total_ordering +class INF: + def __lt__(self, other): + return False + + def __eq__(self, other): + return isinstance(other, INF) + + def __str__(self): + return "∞" + + def __neg__(self): + return NEGINF() + + +@total_ordering +class NEGINF: + def __lt__(self, other): + return True + + def __eq__(self, other): + return isinstance(other, NEGINF) + + def __str__(self): + return "∞" + + +Infinity = INF() + + +def root(num: REAL, exp: REAL): + """ + + :rtype: REAL + :param num: 开方底数 + :param exp: 开方指数 + :return: 结果 + + """ + result = num ** (1 / exp) + if not isinstance(result, complex): + return num ** (1 / exp) + else: + return math.nan + + +def sqrt(num: REAL): + """ + + :param num:要开方的数 + :return: 结果 + """ + return root(num, 2) -- Gitee From c7964d6b20b8b7f2da2ffb9c55af50fe7df88229 Mon Sep 17 00:00:00 2001 From: LAI-1048576 <7530270+index-1048576@user.noreply.gitee.com> Date: Wed, 27 May 2020 08:12:57 +0800 Subject: [PATCH 09/30] update mathprolib/__init__.py. --- mathprolib/__init__.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/mathprolib/__init__.py b/mathprolib/__init__.py index 5b68540..acf4bc0 100644 --- a/mathprolib/__init__.py +++ b/mathprolib/__init__.py @@ -9,11 +9,15 @@ REAL = float Ф = (math.sqrt(5) - 1) / 2 γ = 0.5772156649015328 -sin, cos, tan = lambda n: math.sin(math.radians(n)), lambda n: math.cos(math.radians(n)), lambda n: math.tan( - math.radians(n)) +def deg(num): + return num*180/math.pi +def rad(num): + return num/180*math.pi + + +sin, cos, tan = lambda n: math.sin(rad(n)), lambda n: math.cos(rad(n)), lambda n: math.tan(rad(n)) sinh,cosh,tanh = math.sin,math.cos,math.tan asin,acos,atan = math.asin,math.acos,math.atan -deg,rad = math.degrees,math.radians ln,log,log2 = math.log,math.log10,math.log2 π,e,fact = math.pi,math.e,math.factorial def gcd(x, y): @@ -76,3 +80,4 @@ def sqrt(num: REAL): :return: 结果 """ return root(num, 2) + -- Gitee From e964e51c0762e869ebafcac8e3051a42e4a405d6 Mon Sep 17 00:00:00 2001 From: LAI-1048576 <7530270+index-1048576@user.noreply.gitee.com> Date: Sat, 30 May 2020 08:39:29 +0800 Subject: [PATCH 10/30] update mathprolib/__init__.py. --- mathprolib/__init__.py | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/mathprolib/__init__.py b/mathprolib/__init__.py index acf4bc0..e805a1d 100644 --- a/mathprolib/__init__.py +++ b/mathprolib/__init__.py @@ -3,7 +3,8 @@ # -*- coding:utf-8 -*- import math -from functools import total_ordering +from functools import total_ordering,reduce +from operator import and_,mul REAL = float Ф = (math.sqrt(5) - 1) / 2 @@ -81,3 +82,33 @@ def sqrt(num: REAL): """ return root(num, 2) + +def calculate(equation:str): + return eval(equation.replace("÷","/").replace("^","**")) + + +def fibonacci(num:int): + b = 0 + temp = 1 + for _ in range(num): + yield temp + a = b + b = temp + temp = a+b + + +def isOdd(num:int) -> bool: + return num % 2 == 1 + + +def isEven(num:int) -> bool: + return num % 2 == 0 + + +def isPrime(num:int) -> bool: + return not reduce(and_,[num % i == 0 for i in range(2,int(sqrt(num))+1)]) + + +def product(iterable): + return reduce(mul,iterable) + -- Gitee From 0ff55ce2f467789eb0f506e0ffe13cfad0cd5158 Mon Sep 17 00:00:00 2001 From: 2097152 Date: Sat, 13 Jun 2020 12:36:40 +0800 Subject: [PATCH 11/30] add mathprolib/simple.py. --- mathprolib/simple.py | 114 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 mathprolib/simple.py diff --git a/mathprolib/simple.py b/mathprolib/simple.py new file mode 100644 index 0000000..e805a1d --- /dev/null +++ b/mathprolib/simple.py @@ -0,0 +1,114 @@ +# Version: Test +# Author:LAI-1048576,Toby-LAI +# -*- coding:utf-8 -*- + +import math +from functools import total_ordering,reduce +from operator import and_,mul + +REAL = float +Ф = (math.sqrt(5) - 1) / 2 +γ = 0.5772156649015328 + +def deg(num): + return num*180/math.pi +def rad(num): + return num/180*math.pi + + +sin, cos, tan = lambda n: math.sin(rad(n)), lambda n: math.cos(rad(n)), lambda n: math.tan(rad(n)) +sinh,cosh,tanh = math.sin,math.cos,math.tan +asin,acos,atan = math.asin,math.acos,math.atan +ln,log,log2 = math.log,math.log10,math.log2 +π,e,fact = math.pi,math.e,math.factorial +def gcd(x, y): + smaller = x if x < y else y + g = [i for i in range(1,smaller+1) if x % i == 0 and y % i == 0][-1] + return g + +def lcm(x, y): + return x*y//gcd(x,y) +@total_ordering +class INF: + def __lt__(self, other): + return False + + def __eq__(self, other): + return isinstance(other, INF) + + def __str__(self): + return "∞" + + def __neg__(self): + return NEGINF() + + +@total_ordering +class NEGINF: + def __lt__(self, other): + return True + + def __eq__(self, other): + return isinstance(other, NEGINF) + + def __str__(self): + return "∞" + + +Infinity = INF() + + +def root(num: REAL, exp: REAL): + """ + + :rtype: REAL + :param num: 开方底数 + :param exp: 开方指数 + :return: 结果 + + """ + result = num ** (1 / exp) + if not isinstance(result, complex): + return num ** (1 / exp) + else: + return math.nan + + +def sqrt(num: REAL): + """ + + :param num:要开方的数 + :return: 结果 + """ + return root(num, 2) + + +def calculate(equation:str): + return eval(equation.replace("÷","/").replace("^","**")) + + +def fibonacci(num:int): + b = 0 + temp = 1 + for _ in range(num): + yield temp + a = b + b = temp + temp = a+b + + +def isOdd(num:int) -> bool: + return num % 2 == 1 + + +def isEven(num:int) -> bool: + return num % 2 == 0 + + +def isPrime(num:int) -> bool: + return not reduce(and_,[num % i == 0 for i in range(2,int(sqrt(num))+1)]) + + +def product(iterable): + return reduce(mul,iterable) + -- Gitee From 3e57d78b8d457fe8cda666c3ba963f6a4f5caa7b Mon Sep 17 00:00:00 2001 From: 2097152 Date: Sat, 13 Jun 2020 12:37:23 +0800 Subject: [PATCH 12/30] update mathprolib/__init__.py. --- mathprolib/__init__.py | 112 +---------------------------------------- 1 file changed, 1 insertion(+), 111 deletions(-) diff --git a/mathprolib/__init__.py b/mathprolib/__init__.py index e805a1d..57f4784 100644 --- a/mathprolib/__init__.py +++ b/mathprolib/__init__.py @@ -1,114 +1,4 @@ # Version: Test # Author:LAI-1048576,Toby-LAI # -*- coding:utf-8 -*- - -import math -from functools import total_ordering,reduce -from operator import and_,mul - -REAL = float -Ф = (math.sqrt(5) - 1) / 2 -γ = 0.5772156649015328 - -def deg(num): - return num*180/math.pi -def rad(num): - return num/180*math.pi - - -sin, cos, tan = lambda n: math.sin(rad(n)), lambda n: math.cos(rad(n)), lambda n: math.tan(rad(n)) -sinh,cosh,tanh = math.sin,math.cos,math.tan -asin,acos,atan = math.asin,math.acos,math.atan -ln,log,log2 = math.log,math.log10,math.log2 -π,e,fact = math.pi,math.e,math.factorial -def gcd(x, y): - smaller = x if x < y else y - g = [i for i in range(1,smaller+1) if x % i == 0 and y % i == 0][-1] - return g - -def lcm(x, y): - return x*y//gcd(x,y) -@total_ordering -class INF: - def __lt__(self, other): - return False - - def __eq__(self, other): - return isinstance(other, INF) - - def __str__(self): - return "∞" - - def __neg__(self): - return NEGINF() - - -@total_ordering -class NEGINF: - def __lt__(self, other): - return True - - def __eq__(self, other): - return isinstance(other, NEGINF) - - def __str__(self): - return "∞" - - -Infinity = INF() - - -def root(num: REAL, exp: REAL): - """ - - :rtype: REAL - :param num: 开方底数 - :param exp: 开方指数 - :return: 结果 - - """ - result = num ** (1 / exp) - if not isinstance(result, complex): - return num ** (1 / exp) - else: - return math.nan - - -def sqrt(num: REAL): - """ - - :param num:要开方的数 - :return: 结果 - """ - return root(num, 2) - - -def calculate(equation:str): - return eval(equation.replace("÷","/").replace("^","**")) - - -def fibonacci(num:int): - b = 0 - temp = 1 - for _ in range(num): - yield temp - a = b - b = temp - temp = a+b - - -def isOdd(num:int) -> bool: - return num % 2 == 1 - - -def isEven(num:int) -> bool: - return num % 2 == 0 - - -def isPrime(num:int) -> bool: - return not reduce(and_,[num % i == 0 for i in range(2,int(sqrt(num))+1)]) - - -def product(iterable): - return reduce(mul,iterable) - +import simple \ No newline at end of file -- Gitee From bc6d1133e77a37f256bed1e21dde4b2e89593be0 Mon Sep 17 00:00:00 2001 From: LAI-1048576 <7530270+index-1048576@user.noreply.gitee.com> Date: Sun, 14 Jun 2020 12:29:48 +0800 Subject: [PATCH 13/30] add mathprolib/Fraction.py. --- mathprolib/Fraction.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 mathprolib/Fraction.py diff --git a/mathprolib/Fraction.py b/mathprolib/Fraction.py new file mode 100644 index 0000000..cf3d5ae --- /dev/null +++ b/mathprolib/Fraction.py @@ -0,0 +1,29 @@ +from math import nan as NaN +from simple import log10 +class Scientific_notation: + def __init__(self,num,exp): + self.num = round(num*1e3)/1e3/10**int(log(num)) + self.exp = int(exp)+int(log(num)) + + def __str__(self): + return "{} * 10^{}".format(self.num,self.exp) + + def __add__(self,other): + if isinstance(other,Scientific_notation): + if other.exp == self.exp: + return Scientific_notation(self.num+other.num,self.exp) + elif self.exp < other.exp: + return Scientific_notation(self.num/10**(self.exp-other.exp)+other.num,self.exp) + else: + return Scientific_notation(self.num*10**(self.exp-other.exp)+other.num, other.exp) + elif isinstance(other,int) or isinstance(other,float): + return self.__add__(Scientific_notation(other,0)) + + else: + raise TypeError( + f"cannot add from type {str(type(other))[8:-2]} to Scientific_notation") + + + +if __name__ == "__main__": + print(Scientific_notation(2,7)+Scientific_notation(1,6)) -- Gitee From 81d4e351e9fdd7494b0a6a7e8931796c1d57e58f Mon Sep 17 00:00:00 2001 From: 2097152 Date: Sun, 14 Jun 2020 12:34:48 +0800 Subject: [PATCH 14/30] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=20mathprolib/?= =?UTF-8?q?Fraction.py=20=E4=B8=BA=20mathprolib/Scientific.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mathprolib/{Fraction.py => Scientific.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename mathprolib/{Fraction.py => Scientific.py} (100%) diff --git a/mathprolib/Fraction.py b/mathprolib/Scientific.py similarity index 100% rename from mathprolib/Fraction.py rename to mathprolib/Scientific.py -- Gitee From e8054fe61982d4729c3690774462c470548e7c98 Mon Sep 17 00:00:00 2001 From: LAI-1048576 <7530270+index-1048576@user.noreply.gitee.com> Date: Sun, 14 Jun 2020 12:39:22 +0800 Subject: [PATCH 15/30] update mathprolib/Scientific.py. --- mathprolib/Scientific.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mathprolib/Scientific.py b/mathprolib/Scientific.py index cf3d5ae..0b94ef5 100644 --- a/mathprolib/Scientific.py +++ b/mathprolib/Scientific.py @@ -2,8 +2,8 @@ from math import nan as NaN from simple import log10 class Scientific_notation: def __init__(self,num,exp): - self.num = round(num*1e3)/1e3/10**int(log(num)) - self.exp = int(exp)+int(log(num)) + self.num = round(num*1e3)/1e3/10**int(log10(num)) + self.exp = int(exp)+int(log10(num)) def __str__(self): return "{} * 10^{}".format(self.num,self.exp) -- Gitee From b4f9526ead14e0e7a0577faace849799c341b578 Mon Sep 17 00:00:00 2001 From: LAI-1048576 <7530270+index-1048576@user.noreply.gitee.com> Date: Sun, 14 Jun 2020 12:40:44 +0800 Subject: [PATCH 16/30] update mathprolib/__init__.py. --- mathprolib/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mathprolib/__init__.py b/mathprolib/__init__.py index 57f4784..59b7980 100644 --- a/mathprolib/__init__.py +++ b/mathprolib/__init__.py @@ -1,4 +1,4 @@ # Version: Test # Author:LAI-1048576,Toby-LAI # -*- coding:utf-8 -*- -import simple \ No newline at end of file +import simple,Scientific \ No newline at end of file -- Gitee From 4cc1f45bebe1d2fe98fb944445d37d0a7f366cb3 Mon Sep 17 00:00:00 2001 From: 2097152 Date: Sun, 14 Jun 2020 12:48:24 +0800 Subject: [PATCH 17/30] add mathprolib/Fraction.py. --- mathprolib/Fraction.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 mathprolib/Fraction.py diff --git a/mathprolib/Fraction.py b/mathprolib/Fraction.py new file mode 100644 index 0000000..e69de29 -- Gitee From 880e10a86d64d3558c7354805019d6a54757952e Mon Sep 17 00:00:00 2001 From: LAI-1048576 <7530270+index-1048576@user.noreply.gitee.com> Date: Sun, 14 Jun 2020 12:59:44 +0800 Subject: [PATCH 18/30] update mathprolib/Scientific.py. --- mathprolib/Scientific.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/mathprolib/Scientific.py b/mathprolib/Scientific.py index 0b94ef5..0a58f62 100644 --- a/mathprolib/Scientific.py +++ b/mathprolib/Scientific.py @@ -2,11 +2,14 @@ from math import nan as NaN from simple import log10 class Scientific_notation: def __init__(self,num,exp): - self.num = round(num*1e3)/1e3/10**int(log10(num)) - self.exp = int(exp)+int(log10(num)) + self.num = round(num*1e3)/1e3/10**int(log10(abs(num))) + self.exp = int(exp)+int(log10(abs(num))) def __str__(self): return "{} * 10^{}".format(self.num,self.exp) + + def __neg__(self): + return Scientific_notation(-(self.num),self.exp) def __add__(self,other): if isinstance(other,Scientific_notation): @@ -22,6 +25,9 @@ class Scientific_notation: else: raise TypeError( f"cannot add from type {str(type(other))[8:-2]} to Scientific_notation") + + def __sub__(self,other): + return self+(-other) -- Gitee From 37fd376a3521577e0008a31da47117a853e80597 Mon Sep 17 00:00:00 2001 From: LAI-1048576 <7530270+index-1048576@user.noreply.gitee.com> Date: Mon, 15 Jun 2020 07:33:17 +0800 Subject: [PATCH 19/30] update mathprolib/Fraction.py. --- mathprolib/Fraction.py | 92 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/mathprolib/Fraction.py b/mathprolib/Fraction.py index e69de29..d174afa 100644 --- a/mathprolib/Fraction.py +++ b/mathprolib/Fraction.py @@ -0,0 +1,92 @@ +def gcd(x, y): + smaller = x if x < y else y + g = [i for i in range(1, smaller + 1) if x % i == 0 and y % i == 0][-1] + return g + + +class Fraction: +# def __init__(self, _fraction): + # self.numerator = _fraction.numerator + # self.denominator = _fraction.denominator + + def __init__(self, numerator, denominator): + self.numerator = numerator + self.denominator = denominator + + def __str__(self): + return f"{self.numerator}\n{'—' * len(str(max(self.numerator, self.denominator)))}\n{self.denominator}" + + def __add__(self, other): + if isinstance(other, Fraction): + tmp1, tmp2 = other.denominator * self.denominator, other.numerator * self.denominator + self.numerator * other.denominator + tmp3 = tmp1 // gcd(tmp1, tmp2) + tmp2 = tmp2 // (tmp1 // tmp2) + del tmp1 + return Fraction(tmp2, tmp3) + elif isinstance(other, int): + return self + Fraction(other, 1) + elif isinstance(other, float): + float_str = str(other) + numbers = len(float_str) - float_str.find(".") - 1 + return self + Fraction(int(float_str.strip("i")), 10 ** numbers) + else: + raise TypeError( + f"cannot add from type {str(type(other))[8:-2]} to Fraction") + + def __sub__(self, other): + if isinstance(other, Fraction): + tmp1, tmp2 = other.denominator * self.denominator, other.numerator * self.denominator - self.numerator * other.denominator + tmp3 = tmp1 // gcd(tmp1, tmp2) + tmp2 = tmp2 // (tmp1 // tmp2) + del tmp1 + return Fraction(tmp2, tmp3) + elif isinstance(other, int): + return self - Fraction(other, 1) + elif isinstance(other, float): + float_str = str(other) + numbers = len(float_str) - float_str.find(".") - 1 + return self - Fraction(int(float_str.strip("i")), 10 ** numbers) + else: + raise TypeError( + f"cannot sub from type {str(type(other))[8:-2]} to Fraction") + + def __mul__(self, other): + if isinstance(other, Fraction): + tmp1, tmp2 = other.denominator * self.denominator, self.numerator * self.numerator + tmp3 = tmp1 // gcd(tmp1, tmp2) + tmp2 = tmp2 // (tmp1 // tmp2) + del tmp1 + return Fraction(tmp2, tmp3) + elif isinstance(other, int): + return self * Fraction(other, 1) + elif isinstance(other, float): + float_str = str(other) + numbers = len(float_str) - float_str.find(".") - 1 + return self * Fraction(int(float_str.strip("i")), 10 ** numbers) + else: + raise TypeError( + f"cannot mul from type {str(type(other))[8:-2]} to Fraction") + + def __truediv__(self, other): + if isinstance(other, Fraction): + return self * Fraction(other.denominator, other) + elif isinstance(other, int): + return self * Fraction(1, other) + elif isinstance(other, float): + float_str = str(1 / other) + numbers = len(float_str) - float_str.find(".") - 1 + return self * Fraction(int(float_str.strip("i")), 10 ** numbers) + else: + raise TypeError( + f"cannot div from type {str(type(other))[8:-2]} to Fraction") + + def __int__(self): + return int(self.numerator / self.denominator) + + def __float__(self): + return self.numerator / self.denominator + + +if __name__ == '__main__': + fr = Fraction(1, 2) + print(fr) -- Gitee From 8e5883763695c0ccd45cbb37dee409091a57bb9f Mon Sep 17 00:00:00 2001 From: LAI-1048576 <7530270+index-1048576@user.noreply.gitee.com> Date: Tue, 16 Jun 2020 08:10:12 +0800 Subject: [PATCH 20/30] =?UTF-8?q?=E6=B2=A1=E5=86=99=E5=AE=8C=E5=95=8A?= =?UTF-8?q?=E5=95=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mathprolib/Fraction.py | 85 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 82 insertions(+), 3 deletions(-) diff --git a/mathprolib/Fraction.py b/mathprolib/Fraction.py index d174afa..6fe560e 100644 --- a/mathprolib/Fraction.py +++ b/mathprolib/Fraction.py @@ -1,13 +1,22 @@ + +from functools import total_ordering + + def gcd(x, y): smaller = x if x < y else y g = [i for i in range(1, smaller + 1) if x % i == 0 and y % i == 0][-1] return g +@total_ordering class Fraction: -# def __init__(self, _fraction): - # self.numerator = _fraction.numerator - # self.denominator = _fraction.denominator + + def __lt__(self, other): + if isinstance(other,Fraction): + numerator1 = self.numerator*other.denominator + numerator2 = self.denominator*other.numerator + return numerator1 < numerator2 + def __init__(self, numerator, denominator): self.numerator = numerator @@ -80,12 +89,82 @@ class Fraction: raise TypeError( f"cannot div from type {str(type(other))[8:-2]} to Fraction") + def reciprocal(self): + return Fraction(self.denominator,self.numerator) + + def to_ffraction(self): + num = self.numerator // self.denominator + numerator = self.numerator - (self.numerator // self.denominator)*self.denominator + denominator = self.denominator + return FFraction(num=num,numerator=numerator,denominator=denominator) + def __int__(self): return int(self.numerator / self.denominator) def __float__(self): return self.numerator / self.denominator +class FFraction: + def __init__(self,num,numerator,denominator): + self.fraction = Fraction.__init__(num*denominator+numerator,denominator) + self.num = num + self.numerator = numerator + self.denominator = denominator + + def __add__(self, other): + if isinstance(other,FFraction): + tmp1 = self.fraction + other.fraction + tmp2 = tmp1.denominator + tmp3 = tmp1.numerator + tmp4 = tmp3//tmp2 + tmp3 -= tmp4*tmp2 + return FFraction(tmp4,tmp3,tmp2) + elif isinstance(other,Fraction): + return self+FFraction(0,other.numerator,other.denominator) + elif isinstance(other,int): + return self+FFraction(other,0,1) + elif isinstance(other,float): + return self+(Fraction(0,1)+other) + else: + raise TypeError + + + def __sub__(self, other): + if isinstance(other,FFraction): + tmp1 = self.fraction - other.fraction + tmp2 = tmp1.denominator + tmp3 = tmp1.numerator + tmp4 = tmp3//tmp2 + tmp3 -= tmp4*tmp2 + return FFraction(tmp4,tmp3,tmp2) + elif isinstance(other,Fraction): + return self-FFraction(0,other.numerator,other.denominator) + elif isinstance(other,int): + return self-FFraction(other,0,1) + elif isinstance(other,float): + return self-(Fraction(0,1)+other) + else: + raise TypeError + + + def __mul__(self, other): + if isinstance(other,FFraction): + tmp1 = self.fraction * other.fraction + tmp2 = tmp1.denominator + tmp3 = tmp1.numerator + tmp4 = tmp3//tmp2 + tmp3 -= tmp4*tmp2 + return FFraction(tmp4,tmp3,tmp2) + elif isinstance(other,Fraction): + return self*FFraction(0,other.numerator,other.denominator) + elif isinstance(other,int): + return self*FFraction(other,0,1) + elif isinstance(other,float): + return self*(Fraction(0,1)+other) + else: + raise TypeError + + if __name__ == '__main__': fr = Fraction(1, 2) -- Gitee From 4ac1efb6293ba7656752cd47fa481cee29be167b Mon Sep 17 00:00:00 2001 From: LAI-1048576 <7530270+index-1048576@user.noreply.gitee.com> Date: Wed, 17 Jun 2020 07:17:36 +0800 Subject: [PATCH 21/30] update mathprolib/Scientific.py. --- mathprolib/Scientific.py | 51 ++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/mathprolib/Scientific.py b/mathprolib/Scientific.py index 0a58f62..bb81fee 100644 --- a/mathprolib/Scientific.py +++ b/mathprolib/Scientific.py @@ -1,35 +1,46 @@ from math import nan as NaN -from simple import log10 +from math import log10 +from ctypes import Union + + class Scientific_notation: - def __init__(self,num,exp): - self.num = round(num*1e3)/1e3/10**int(log10(abs(num))) + def __init__(self, num, exp): + self.num = round(num*1e3/10**int(log10(abs(num))))/1e3 self.exp = int(exp)+int(log10(abs(num))) - + def __str__(self): - return "{} * 10^{}".format(self.num,self.exp) - + return "{} * 10^{}".format(self.num, self.exp) + def __neg__(self): - return Scientific_notation(-(self.num),self.exp) + return Scientific_notation(-(self.num), self.exp) - def __add__(self,other): - if isinstance(other,Scientific_notation): + def __add__(self, other): + if isinstance(other, Scientific_notation): if other.exp == self.exp: - return Scientific_notation(self.num+other.num,self.exp) + return Scientific_notation(self.num+other.num, self.exp) elif self.exp < other.exp: - return Scientific_notation(self.num/10**(self.exp-other.exp)+other.num,self.exp) + return Scientific_notation(self.num/10**(self.exp-other.exp)+other.num, self.exp) else: return Scientific_notation(self.num*10**(self.exp-other.exp)+other.num, other.exp) - elif isinstance(other,int) or isinstance(other,float): - return self.__add__(Scientific_notation(other,0)) - - else: - raise TypeError( + elif isinstance(other, int) or isinstance(other, float): + return self.__add__(Scientific_notation(other, 0)) + + + raise TypeError( f"cannot add from type {str(type(other))[8:-2]} to Scientific_notation") - - def __sub__(self,other): - return self+(-other) + def __sub__(self, other): + return self+(-other) + def __mul__(self,other): + if isinstance(other, Scientific_notation): + return Scientific_notation(self.num*other.num,self.exp+other.exp) + elif isinstance(other,Union(int,float)): + return Scientific_notation(self.num*other, self.exp*other.exp) + raise TypeError( + f"cannot mul from type {str(type(other))[8:-2]} to Scientific_notation") + + if __name__ == "__main__": - print(Scientific_notation(2,7)+Scientific_notation(1,6)) + print(Scientific_notation(111,2)*Scientific_notation(111,4)) -- Gitee From 3c52352af38739e8120492d695435a6a64cd75a7 Mon Sep 17 00:00:00 2001 From: LAI-1048576 <7530270+index-1048576@user.noreply.gitee.com> Date: Wed, 17 Jun 2020 07:53:07 +0800 Subject: [PATCH 22/30] =?UTF-8?q?=E6=8A=80=E5=B7=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mathprolib/Fraction.py | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/mathprolib/Fraction.py b/mathprolib/Fraction.py index 6fe560e..6eb49ce 100644 --- a/mathprolib/Fraction.py +++ b/mathprolib/Fraction.py @@ -38,8 +38,7 @@ class Fraction: float_str = str(other) numbers = len(float_str) - float_str.find(".") - 1 return self + Fraction(int(float_str.strip("i")), 10 ** numbers) - else: - raise TypeError( + raise TypeError( f"cannot add from type {str(type(other))[8:-2]} to Fraction") def __sub__(self, other): @@ -55,8 +54,8 @@ class Fraction: float_str = str(other) numbers = len(float_str) - float_str.find(".") - 1 return self - Fraction(int(float_str.strip("i")), 10 ** numbers) - else: - raise TypeError( + + raise TypeError( f"cannot sub from type {str(type(other))[8:-2]} to Fraction") def __mul__(self, other): @@ -71,9 +70,8 @@ class Fraction: elif isinstance(other, float): float_str = str(other) numbers = len(float_str) - float_str.find(".") - 1 - return self * Fraction(int(float_str.strip("i")), 10 ** numbers) - else: - raise TypeError( + return self * Fraction(int(float_str.strip("i")), 10 ** numbers) + raise TypeError( f"cannot mul from type {str(type(other))[8:-2]} to Fraction") def __truediv__(self, other): @@ -85,8 +83,7 @@ class Fraction: float_str = str(1 / other) numbers = len(float_str) - float_str.find(".") - 1 return self * Fraction(int(float_str.strip("i")), 10 ** numbers) - else: - raise TypeError( + raise TypeError( f"cannot div from type {str(type(other))[8:-2]} to Fraction") def reciprocal(self): @@ -125,8 +122,7 @@ class FFraction: return self+FFraction(other,0,1) elif isinstance(other,float): return self+(Fraction(0,1)+other) - else: - raise TypeError + raise TypeError def __sub__(self, other): @@ -143,8 +139,7 @@ class FFraction: return self-FFraction(other,0,1) elif isinstance(other,float): return self-(Fraction(0,1)+other) - else: - raise TypeError + raise TypeError def __mul__(self, other): @@ -161,8 +156,7 @@ class FFraction: return self*FFraction(other,0,1) elif isinstance(other,float): return self*(Fraction(0,1)+other) - else: - raise TypeError + raise TypeError -- Gitee From ff08939e416f8380533d370468b6a42b58999d86 Mon Sep 17 00:00:00 2001 From: LAI-1048576 <7530270+index-1048576@user.noreply.gitee.com> Date: Thu, 18 Jun 2020 05:13:29 +0800 Subject: [PATCH 23/30] debug --- mathprolib/Fraction.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/mathprolib/Fraction.py b/mathprolib/Fraction.py index 6eb49ce..ad7088c 100644 --- a/mathprolib/Fraction.py +++ b/mathprolib/Fraction.py @@ -16,7 +16,16 @@ class Fraction: numerator1 = self.numerator*other.denominator numerator2 = self.denominator*other.numerator return numerator1 < numerator2 + else: + return float(self) < other + def __eq__(self, other): + if isinstance(other,Fraction): + numerator1 = self.numerator*other.denominator + numerator2 = self.denominator*other.numerator + return numerator1 == numerator2 + else: + return abs(float(self) - other) <= 1e-5 def __init__(self, numerator, denominator): self.numerator = numerator -- Gitee From c0542630afe23952d36c0a31fef72cc52ea0ead1 Mon Sep 17 00:00:00 2001 From: 2097152 Date: Sat, 20 Jun 2020 10:44:45 +0800 Subject: [PATCH 24/30] =?UTF-8?q?=E7=BB=88=E4=BA=8E=EF=BC=81=EF=BC=81?= =?UTF-8?q?=E6=88=91=E7=BB=88=E4=BA=8E=E5=81=9A=E5=AE=8C=E4=BA=86=EF=BC=81?= =?UTF-8?q?=EF=BC=81=EF=BC=81=EF=BC=81=20=E5=95=8A=E5=95=8A=E5=95=8A?= =?UTF-8?q?=E5=95=8A=E5=95=8A=E5=95=8A=E5=A5=BD=E4=B8=8D=E5=AE=B9=E6=98=93?= =?UTF-8?q?=E5=81=9A=E5=AE=8C=E5=95=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mathprolib/Fraction.py | 388 +++++++++++++++++++++++------------------ 1 file changed, 218 insertions(+), 170 deletions(-) diff --git a/mathprolib/Fraction.py b/mathprolib/Fraction.py index ad7088c..bec83c0 100644 --- a/mathprolib/Fraction.py +++ b/mathprolib/Fraction.py @@ -1,174 +1,222 @@ - from functools import total_ordering +from logging import warning as warn +class CalculateError(Exception): + pass + +class Fraction(object): + def __gongyue(self,a,b): + while b!=0: + temp=a%b + a=b + b=temp + return a + + def __init__(self,_numerator,denominator): + if denominator==0: + raise CalculateError("fatal error:Denominator = 0") + self._numerator=_numerator + self.denominator=denominator + + def __float__(self): + return self._numerator/self.denominator + + def general(self,factor): + self._numerator*=factor + self.denominator*=factor + + def all(self): + return (self._numerator,self.denominator) + + def reduction(self,factor=None): + if factor==None: + factor=self.__gongyue(self._numerator,self.denominator) + if self._numerator/factor!=int(str(self._numerator/factor).split('.')[0]) or self.denominator/factor!=int(str(self.denominator/factor).split('.')[0]): + warn(f"Reduction failed.Cannot reduction by {factor}") + return + self._numerator//=factor + self.denominator//=factor + + def common(self,other): + if type(other)!=int and (not isinstance(other,Fraction)) and type(other)!=float: + raise TypeError(f"can only concatenate Fraction (not {type(other)}) to Fraction") + else: + if type(other)==int: + ofra=VulgarFraction(self.denominator*other,self.denominator) + elif type(other)==float: + l=10**len(str(other).split(".")[1]) + ofra=VulgarFraction(int(other*l),l) + ofra.reduction() + p=ofra.denominator*self.denominator//self.__gongyue(ofra.denominator,self.denominator) + self._numerator*=p//self.denominator + ofra._numerator*=p//ofra.denominator + self.denominator,ofra.denominator=p,p + else: + ofra=VulgarFraction(other._numerator,other.denominator) + p=ofra.denominator*self.denominator//self.__gongyue(ofra.denominator,self.denominator) + self._numerator*=p//self.denominator + ofra._numerator*=p//ofra.denominator + self.denominator,ofra.denominator=p,p + return ofra - -def gcd(x, y): - smaller = x if x < y else y - g = [i for i in range(1, smaller + 1) if x % i == 0 and y % i == 0][-1] - return g - +@total_ordering +class VulgarFraction(Fraction): + def toMixed(self): + temp=MixedFraction(0,0,self.denominator) + temp.numerator=self.numerator + return temp + + @property + def numerator(self): + return self._numerator + + @numerator.setter + def numerator(self,value): + self._numerator=value + + def __str__(self): + if self.numerator==self.denominator: + return '1' + if self.numerator==0: + return '0' + return f"{self.numerator}\n{'—' * len(str(max(self.numerator, self.denominator)))}\n{self.denominator}" + + def __add__(self,other): + fra=VulgarFraction(self.numerator,self.denominator) + if type(other)==int: + fra.numerator+=other*fra.denominator + elif type(other)==float or isinstance(other,Fraction): + fra.numerator+=fra.common(other).numerator + else: + raise TypeError(f"can only concatenate Fraction (not {type(other)}) to Fraction") + fra.reduction() + return fra + + def __sub__(self,other): + fra=VulgarFraction(self.numerator,self.denominator) + if type(other)==int: + fra.numerator-=other*fra.denominator + elif type(other)==float or isinstance(other,Fraction): + fra.numerator-=fra.common(other).numerator + else: + raise TypeError(f"can only concatenate Fraction (not {type(other)}) to Fraction") + fra.reduction() + return fra + + def __mul__(self,other): + fra=VulgarFraction(self.numerator,self.denominator) + if type(other)==int: + fra.numerator*=other + elif type(other)==float or isinstance(other,Fraction): + temp=fra.common(other) + fra.numerator*=temp.numerator + fra.denominator*=temp.denominator + else: + raise TypeError(f"can only concatenate Fraction (not {type(other)}) to Fraction") + fra.reduction() + return fra + + def __truediv__(self,other): + fra=VulgarFraction(self.numerator,self.denominator) + if type(other)==int: + fra.denominator*=other + elif type(other)==float or isinstance(other,Fraction): + temp=fra.common(other) + fra.numerator*=temp.denominator + fra.denominator*=temp.numerator + else: + raise TypeError(f"can only concatenate Fraction (not {type(other)}) to Fraction") + fra.reduction() + return fra + + def __eq__(self,other): + fra=VulgarFraction(self.numerator,self.denominator) + if type(other)==MixedFraction: + otr=other.toVulgar() + otr=fra.common(otr) + else: + otr=fra.common(other) + return fra.numerator==otr.numerator + + def __lt__(self,other): + fra=VulgarFraction(self.numerator,self.denominator) + if type(other)==MixedFraction: + otr=other.toVulgar() + otr=fra.common(otr) + else: + otr=fra.common(other) + return fra.numerator=denominator: + raise CalculateError("Numerator should be smaller than denominator") + if integer<0: + raise CalculateError("fatal error:Calculate failed.Cause integer < 0") + super().__init__(numerator,denominator) + self.integer=integer + + def __eq__(self,other): + fra=MixedFraction(self.integer,self.numerator,self.denominator).toVulgar() + otr=fra.common(other) + return fra==otr + + def __lt__(self,other): + fra=MixedFraction(self.integer,self.numerator,self.denominator).toVulgar() + otr=fra.common(other) + return fra=self.denominator: + def itg(): + nonlocal self + nonlocal nm + temp=nm//self.denominator + if temp>0: + nm-=self.denominator*temp + self.integer+=temp + itg() + elif nm<0: + nm+=self.integer*self.denominator + self.integer=0 + itg() + else: + pass + self._numerator=nm + self.reduction() + + + def __str__(self): + if self.numerator==0: + return str(self.integer) + return f"{' '*len(str(self.integer)) if self.integer!=0 else ''}{self.numerator}\n{self.integer if self.integer!=0 else ''}{'—' * len(str(max(self.numerator, self.denominator)))}\n{' '*len(str(self.integer)) if self.integer!=0 else ''}{self.denominator}" + + def __add__(self,other): + fra=MixedFraction(self.integer,self.numerator,self.denominator).toVulgar() + return (fra+other).toMixed() + + def __sub__(self,other): + fra=MixedFraction(self.integer,self.numerator,self.denominator).toVulgar() + return (fra-other).toMixed() + + def __mul__(self,other): + fra=MixedFraction(self.integer,self.numerator,self.denominator).toVulgar() + return (fra*other).toMixed() + + def __truediv__(self,other): + fra=MixedFraction(self.integer,self.numerator,self.denominator).toVulgar() + return (fra/other).toMixed() \ No newline at end of file -- Gitee From 19b2a55e55e9ceada09d25545fda2b572cd6be1a Mon Sep 17 00:00:00 2001 From: 2097152 Date: Sat, 20 Jun 2020 10:52:08 +0800 Subject: [PATCH 25/30] =?UTF-8?q?e=E6=94=B9=E8=BF=9B=E4=B8=80=E4=B8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mathprolib/Fraction.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mathprolib/Fraction.py b/mathprolib/Fraction.py index bec83c0..2c43dbf 100644 --- a/mathprolib/Fraction.py +++ b/mathprolib/Fraction.py @@ -1,5 +1,6 @@ from functools import total_ordering from logging import warning as warn +from math import floor class CalculateError(Exception): pass @@ -30,7 +31,7 @@ class Fraction(object): def reduction(self,factor=None): if factor==None: factor=self.__gongyue(self._numerator,self.denominator) - if self._numerator/factor!=int(str(self._numerator/factor).split('.')[0]) or self.denominator/factor!=int(str(self.denominator/factor).split('.')[0]): + if self._numerator/factor!=floor(self._numerator/factor) or self.denominator/factor!=floor(self.denominator/factor): warn(f"Reduction failed.Cannot reduction by {factor}") return self._numerator//=factor -- Gitee From 83e3ee47512d55716fa6f98d9bb985cd821749bf Mon Sep 17 00:00:00 2001 From: LAI-1048576 <7530270+index-1048576@user.noreply.gitee.com> Date: Sat, 20 Jun 2020 11:25:23 +0800 Subject: [PATCH 26/30] add mathprolib/float2VulgarFraction.py. --- mathprolib/float2VulgarFraction.py | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 mathprolib/float2VulgarFraction.py diff --git a/mathprolib/float2VulgarFraction.py b/mathprolib/float2VulgarFraction.py new file mode 100644 index 0000000..cfcc87c --- /dev/null +++ b/mathprolib/float2VulgarFraction.py @@ -0,0 +1,8 @@ +from Fraction import VulgarFraction +def float2VulgarFraction(num:float): + i = 1 + while True: + i+=1 + if abs(round(num*i)-num*i) < 1e-3: + return VulgarFraction(round(num*i),i) + \ No newline at end of file -- Gitee From 8f6591d3f4362e9f3fbfc01938dcd4ff29c432ea Mon Sep 17 00:00:00 2001 From: LAI-1048576 <7530270+index-1048576@user.noreply.gitee.com> Date: Sat, 20 Jun 2020 11:37:24 +0800 Subject: [PATCH 27/30] rename mathprolib/float2VulgarFraction.py to mathprolib/float2Fraction.py. --- mathprolib/{float2VulgarFraction.py => float2Fraction.py} | 3 +++ 1 file changed, 3 insertions(+) rename mathprolib/{float2VulgarFraction.py => float2Fraction.py} (69%) diff --git a/mathprolib/float2VulgarFraction.py b/mathprolib/float2Fraction.py similarity index 69% rename from mathprolib/float2VulgarFraction.py rename to mathprolib/float2Fraction.py index cfcc87c..146608d 100644 --- a/mathprolib/float2VulgarFraction.py +++ b/mathprolib/float2Fraction.py @@ -5,4 +5,7 @@ def float2VulgarFraction(num:float): i+=1 if abs(round(num*i)-num*i) < 1e-3: return VulgarFraction(round(num*i),i) + +def float2MixedFraction(num:float): + return float2VulgarFraction(num).toMixed() \ No newline at end of file -- Gitee From 8a930102d0e92e5782efa90693bdc798591bd1dc Mon Sep 17 00:00:00 2001 From: 2097152 Date: Sat, 20 Jun 2020 13:13:36 +0800 Subject: [PATCH 28/30] =?UTF-8?q?=E4=BF=AEbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mathprolib/Fraction.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/mathprolib/Fraction.py b/mathprolib/Fraction.py index 2c43dbf..ad3d73d 100644 --- a/mathprolib/Fraction.py +++ b/mathprolib/Fraction.py @@ -182,14 +182,14 @@ class MixedFraction(Fraction): @numerator.setter def numerator(self,value): nm=value + def itg(): + nonlocal self + nonlocal nm + temp=nm//self.denominator + if temp>0: + nm-=self.denominator*temp + self.integer+=temp if nm>=self.denominator: - def itg(): - nonlocal self - nonlocal nm - temp=nm//self.denominator - if temp>0: - nm-=self.denominator*temp - self.integer+=temp itg() elif nm<0: nm+=self.integer*self.denominator -- Gitee From de653e0b60ac044f8406c581d5156d941c08ef1e Mon Sep 17 00:00:00 2001 From: LAI-1048576 <7530270+index-1048576@user.noreply.gitee.com> Date: Sun, 21 Jun 2020 14:57:51 +0800 Subject: [PATCH 29/30] update mathprolib/Scientific.py. --- mathprolib/Scientific.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mathprolib/Scientific.py b/mathprolib/Scientific.py index bb81fee..7a107b9 100644 --- a/mathprolib/Scientific.py +++ b/mathprolib/Scientific.py @@ -4,6 +4,8 @@ from ctypes import Union class Scientific_notation: + def __abs__(self): + return Scientific_notation(abs(self.num),exp) def __init__(self, num, exp): self.num = round(num*1e3/10**int(log10(abs(num))))/1e3 self.exp = int(exp)+int(log10(abs(num))) -- Gitee From 780c340e40991757135aa86c3eb3e2841123adf6 Mon Sep 17 00:00:00 2001 From: LAI-1048576 <7530270+index-1048576@user.noreply.gitee.com> Date: Sun, 21 Jun 2020 14:58:35 +0800 Subject: [PATCH 30/30] update mathprolib/Scientific.py. --- mathprolib/Scientific.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mathprolib/Scientific.py b/mathprolib/Scientific.py index 7a107b9..313e2e1 100644 --- a/mathprolib/Scientific.py +++ b/mathprolib/Scientific.py @@ -5,7 +5,7 @@ from ctypes import Union class Scientific_notation: def __abs__(self): - return Scientific_notation(abs(self.num),exp) + return Scientific_notation(abs(self.num),self.exp) def __init__(self, num, exp): self.num = round(num*1e3/10**int(log10(abs(num))))/1e3 self.exp = int(exp)+int(log10(abs(num))) -- Gitee