diff --git a/LICENSE b/LICENSE deleted file mode 100644 index 3166dfba9b13a26c9ca3d23a8da630e53093e55a..0000000000000000000000000000000000000000 --- 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. diff --git a/README.md b/README.md index d3f5a12faa99758192ecc4ed3fc22c9249232e86..fee6bc5b843fd8629611901ee9101fac58b45f5f 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ - +本分支是Python的mathprolib包。更多请见其它分支 \ No newline at end of file diff --git a/golang/simple.go b/golang/simple.go deleted file mode 100644 index c0d111424fe59da792f0f9cfd5a6383329279394..0000000000000000000000000000000000000000 --- 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 f783dcce7e1176e2f1e4c3d30bf8d18ff74ac75c..0000000000000000000000000000000000000000 --- 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 - - - - MATHLIB官网 - - - - diff --git a/mathprolib/Fraction.py b/mathprolib/Fraction.py new file mode 100644 index 0000000000000000000000000000000000000000..ad3d73d509d871a886983ddf61bedabbfa858ebe --- /dev/null +++ b/mathprolib/Fraction.py @@ -0,0 +1,223 @@ +from functools import total_ordering +from logging import warning as warn +from math import floor +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!=floor(self._numerator/factor) or self.denominator/factor!=floor(self.denominator/factor): + 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 + +@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 fra0: + nm-=self.denominator*temp + self.integer+=temp + if nm>=self.denominator: + 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 diff --git a/mathprolib/Scientific.py b/mathprolib/Scientific.py new file mode 100644 index 0000000000000000000000000000000000000000..313e2e17c676e4c1f37fb0b44b5cf47690496a54 --- /dev/null +++ b/mathprolib/Scientific.py @@ -0,0 +1,48 @@ +from math import nan as NaN +from math import log10 +from ctypes import Union + + +class Scientific_notation: + def __abs__(self): + 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))) + + 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): + 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)) + + + raise TypeError( + f"cannot add from type {str(type(other))[8:-2]} to Scientific_notation") + + 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(111,2)*Scientific_notation(111,4)) diff --git a/mathprolib/__init__.py b/mathprolib/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..59b79808313ff1925a05f091dd07a9b22930fb45 --- /dev/null +++ b/mathprolib/__init__.py @@ -0,0 +1,4 @@ +# Version: Test +# Author:LAI-1048576,Toby-LAI +# -*- coding:utf-8 -*- +import simple,Scientific \ No newline at end of file diff --git a/mathprolib/float2Fraction.py b/mathprolib/float2Fraction.py new file mode 100644 index 0000000000000000000000000000000000000000..146608d8d9e863fa8fb1ce2a8eed0dc6617159d8 --- /dev/null +++ b/mathprolib/float2Fraction.py @@ -0,0 +1,11 @@ +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) + +def float2MixedFraction(num:float): + return float2VulgarFraction(num).toMixed() + \ No newline at end of file diff --git a/mathprolib/simple.py b/mathprolib/simple.py new file mode 100644 index 0000000000000000000000000000000000000000..e805a1d2daaca7561b1af9c4368520d5506f4ac4 --- /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) + diff --git a/python/__init__.py b/python/__init__.py deleted file mode 100644 index 0132b430f1ffd351a4d922769363e61fec5fd028..0000000000000000000000000000000000000000 --- a/python/__init__.py +++ /dev/null @@ -1,49 +0,0 @@ -# Version: Test -# Author:LAI-1048576,Toby-LAI -# -*- coding:utf-8 -*- - -import math -from functools import total_ordering - -REAL = float - -e = math.e -π = math.pi -Ф = (math.sqrt(5)-1)/2 -γ = 0.5772156649015328 -@total_ordering -class INF: - def __lt__(self, other): - return False - - def __eq__(self, other): - return isinstance(other,INF) - - 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) -