コード例 #1
0
ファイル: link_extractor.py プロジェクト: J0s34h/DataMining
def page_rank_calcualtor(vector, matrix):
    old_vector = vector
    new_vector = [Fraction() for x in range(len(vector))]
    while True:
        vector_index = 0
        for matrix_row in matrix:
            multiplication_sum = Fraction()
            index = 0
            for number in matrix_row:
                multiplication_sum = multiplication_sum.__add__(
                    old_vector[index].__mul__(number))
                index += 1
            multiplication_sum = multiplication_sum.__mul__(Fraction(8, 10))
            multiplication_sum = multiplication_sum.__add__(
                Fraction(2, 10).__truediv__(Fraction(len(vector), 1)))

            new_vector[vector_index] = multiplication_sum
            vector_index += 1
        # Comparing old and new vectors
        min_difference = sys.maxsize
        for index in range(len(new_vector)):
            diff = abs(old_vector[index] - new_vector[index])
            if diff < min_difference:
                min_difference = diff
        old_vector = [new_vector[x] for x in range(len(new_vector))]

        if min_difference < 0.00001:
            break

    return new_vector
コード例 #2
0
class TestFraction(unittest.TestCase):
    def setUp(self):
        self.fraction = Fraction(3, 4)

    def test_smallest_denom(self):
        self.assertEqual(self.fraction.smallest_denom(self.fraction.numerator,
                         self.fraction.denominator), 12)

    def test_fraction_init(self):
        self.assertEqual(self.fraction.numerator, 3)
        self.assertEqual(self.fraction.denominator, 4)

    def test_fraction_str(self):
        self.assertEqual(str(self.fraction), "3 / 4")

    def test_fraction_eq(self):
        fraction2 = Fraction(3, 4)
        fraction3 = Fraction(4, 5)
        self.assertNotEqual(self.fraction, fraction3)
        self.assertEqual(self.fraction, fraction2)

    def test_fraction_mul(self):
        fraction2 = Fraction(6, 7)
        self.assertEqual(self.fraction.__mul__(fraction2), Fraction(9, 14))

    def test_fraction_add(self):
        fraction2 = Fraction(6, 7)
        self.assertEqual(self.fraction.__add__(fraction2), Fraction(45, 28))

    def test_fraction_sub(self):
        fraction2 = Fraction(6, 7)
        self.assertEqual(self.fraction.__sub__(fraction2), Fraction(-3, 28))
コード例 #3
0
def main():
    while True:
        try:
            num1 = float(input(" Enter the num1: "))
            break
        except:
            print("Invalid input!")
    while True:
        try:
            den1 = float(input(" Enter the den1: "))
            if den1 == 0:
                raise ZeroDivisionError
            break
        except:
            print("Invalid input!")
    while True:
        try:
            num2 = float(input(" Enter the num2: "))
            break
        except:
            print("Invalid input!")
    while True:
        try:
            den2 = float(input(" Enter the den2: "))
            if den2 == 0:
                raise ZeroDivisionError
            break
        except:
            print("Invalid input!")
           
    f1 = Fraction(num1,den1)
    f2 = Fraction(num2,den2)
     
    user = input("enter your choice: \n 1:add \n 2:sub \n 3:divide \n 4:__mul__ \n 5:compare \n ")
    if user == "1":
        print(f1.__add__(f2))
        #break
    elif user == "2":
        print(f1.__sub__(f2))
        #break
    elif user == "3":
        print(f1.__truediv__(f2))
        #break
    elif user == "4":
        print(f1.__mul__(f2))
        #break
    elif user == "5":
        print(f1.__eq__(f2))
        #break
    else:
        print("Invalid input, pls try again\n")
コード例 #4
0
ファイル: basic.py プロジェクト: Jianschang/cmat
 def __add__(a,b):
     f = Fraction.__add__(a,b)
     return NotImplemented if f is NotImplemented else Quarters(f)
コード例 #5
0
ファイル: 16-2-1.py プロジェクト: mydu27/practice
 def __add__(self, other):
     f = Fraction.__add__(self, other)
     return PFraction(f)
コード例 #6
0
ファイル: F.py プロジェクト: odrand/simplesX
class F(Fraction):
    '''
    Classe que representa um numero fracionario, extendendo fractions.Fraction, e implentando a lógica o "M grande"
    '''   
    def __init__(self,n,m=Fraction(0)):
        self.fraction = Fraction(n)
        self.m = Fraction(m)
   
    def __repr__(self):
        """repr(self)"""
        return str(float(self.fraction)) if self.m == 0 else str(float(self.fraction)) + ' + (' + str(float(self.m)) + '*M)'


    def __str__(self):
        """str(self)"""
        return str(float(self.fraction)) if self.m == 0 else str(float(self.fraction)) + ' + (' + str(float(self.m)) + '*M)'
        
    def __eq__(self, f):
        """a == b"""
        if type(f) is not type(self):
            f = F(f)
            
        return self.fraction.__eq__(f.fraction) and self.m.__eq__(f.m)

    def __add__(self, f):
        """a + b"""
        if type(f) is not type(self):
            f = F(f)
            
        return F(self.fraction.__add__(f.fraction),self.m.__add__(f.m))

    def ___sub__(self, f):
        """a - b"""
        if type(f) is not type(self):
            f = F(f)
            
        return F(self.fraction.__sub__(f.fraction),self.m.___sub__(f.m))

    def __mul__(self, f):
        """a * b"""
        if type(f) is not type(self):
            f = F(f)
        
        if f.m == 0:
            return F(self.fraction.__mul__(f.fraction))
        else:
            return F(self.fraction.__mul__(f.fraction),self.m.__mul__(f.m)) 

    def __div__(self, f):
        """a / b"""
        if type(f) is not type(self):
            f = F(f)
        
        if f.m == 0:
            return F(self.fraction.__div__(f.fraction))
        else:
            return F(self.fraction.__div__(f.fraction),self.m.__div__(f.m))
    
    def __lt__(self, f):
        """a < b"""
        if type(f) is not type(self):
            f = F(f)
            
        if self.m == f.m:
            return self.fraction.__lt__(f.fraction)
        
        else:   
            return self.m.__lt__(f.m)
        
    def __gt__(self, f):
        """a > b"""
        if type(f) is not type(self):
            f = F(f)
            
        if self.m == f.m:
            return self.fraction.__gt__(f.fraction)
        
        else:   
            return self.m.__gt__(f.m)

    def __le__(self, f):
        """a <= b"""
        if type(f) is not type(self):
            f = F(f)
            
        if self.m == f.m:
            return self.fraction.__le__(f.fraction)
        
        else:   
            return self.m.__le__(f.m)

    def __ge__(self, f):
        """a >= b"""
        if type(f) is not type(self):
            f = F(f)
            
        if self.m == f.m:
            return self.fraction.__ge__(f.fraction)
        
        else:   
            return self.m.__ge__(f.m)
コード例 #7
0
import mysql.connector
from fractions import Fraction
mydb = mysql.connector.connect(
    host = "localhost"
    user = "******"
    password = "******"
)
print(mydb)

f = Fraction(3, 4)
f1 = Fraction(1, 2)

add = f.__add__(f1)
sub = f.__sub__(f1)
mul = f.__mul__(f1)
div = f.__truediv__(f1)

print(add, sub, mul, div)
コード例 #8
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Apr  4 14:25:57 2020

@author: manuel
"""
from fractions import Fraction

#a)
f = Fraction(1, 2)
f1 = Fraction(8, 10)
print(f + f1)
print(
    f.__add__(f1))  #object fraction as an argument. returns a fraction object

#b
f2 = Fraction(8, 10)
print(f1.__eq__(f2))
print(f1 == f2)

#c

print(f1)  #note that the function is simplified
print(f1.__neg__())
print(-f1)

#d
print(f1.__gt__(f))
print(f1 > f)
コード例 #9
0
ファイル: Angle.py プロジェクト: BeiyanLuansheng/PyAngle
class Angle:
    """Class of Mathematic Angle
    > designed especially for angle in the format of DMS(Degree, Minute and Second)
    > immutable object
    > guarantee `0 <= degrees < 360` 
    `deg` (int): degree of the angle in the format of DMS;
    `min` (int): minute of the angle in the format of DMS;
    `sec` (float): second of the angle in the format of DMS;
    `degrees` (float): degree of the angle in the format of ONLY degree;
    `rad` (float): rad of the angle in the format of radians;
    `atan2` (float, float): (x, y) of the angle in the format of 2D-vector;
    """

    __deg: Fraction = Fraction(0)

    def __init__(self, a: "Angle"):
        self.__deg = Fraction(a.to_degrees() if isinstance(a, Angle) else a)
        self.__adjust()

    @classmethod
    def from_dms(cls, deg: float, min: float = 0, sec: float = 0) -> "Angle":
        """Factory Method for Degree, Minute and Second
        """
        return cls.from_degrees(degrees=Fraction(deg) + Fraction(min) / 60 +
                                Fraction(sec) / 3600)

    @classmethod
    def from_degrees(cls, degrees: float) -> "Angle":
        """Factory Method for ONLY Degree
        """
        return cls(degrees)

    @classmethod
    def from_rad(cls, rad: float) -> "Angle":
        """Factory Method for Radian
        """
        return cls.from_degrees(degrees=math.degrees(Fraction(rad)))

    @classmethod
    def from_atan2(cls, x: float, y: float) -> "Angle":
        """Factory Method for (x, y)
        """
        return cls.from_rad(rad=math.atan2(Fraction(y), Fraction(x)))

    @classmethod
    def from_fmt_str(cls,
                     angle_str: str,
                     fmt: str = "aaa°bbb′ccc″") -> "Angle":
        """Factory Method for Formatted String
        `aaa` is deg(int), `bbb` is minute(int), `ccc` is sec(float), `DDD` is only deg(float),
        `RRR` is radian(float), `XXX` is horizontal ordinate(float), `YYY` is vertical ordinate(float).
        > default: fmt = "aaa°bbb′ccc″"`
        Eg.
        >>> angle = Angle.from_fmt_str(angle_str="2°4′6″")
        >>> angle.get_deg(), angle.get_min(), angle.get_sec()
        2, 4, 6.0
        >>> angle = Angle.from_fmt_str(angle_str="1D2M3S", fmt="aaaDbbbMcccS")
        >>> angle.get_deg(), angle.get_min(), angle.get_sec()
        1, 2, 3.0
        >>> Angle.from_fmt_str(angle_str="1deg", fmt="DDDdeg").to_degrees()
        1.0
        >>> Angle.from_fmt_str(angle_str="1.57rad", fmt="RRRrad").to_rad()
        1.57
        >>> angle = Angle.from_fmt_str(angle_str="1.2, 3.4", fmt="XXX, YYY")
        >>> angle.to_atan2()
        1.2, 3.4
        """
        import re

        digit_pattern = "(-?\d+\.?\d*e?-?\d*?)"
        # DMS
        if fmt.find("aaa") >= 0 and fmt.find("bbb") >= 0 and fmt.find(
                "ccc") >= 0:
            pattern = (fmt.replace("aaa", digit_pattern, 1).replace(
                "bbb", digit_pattern, 1).replace("ccc", digit_pattern, 1))
            matched = re.match(pattern, angle_str)
            if matched:
                return cls.from_dms(deg=matched.group(1),
                                    min=matched.group(2),
                                    sec=matched.group(3))
        # Degree
        elif fmt.find("DDD") >= 0:
            pattern = fmt.replace("DDD", digit_pattern, 1)
            matched = re.match(pattern, angle_str)
            if matched:
                return cls.from_degrees(float(matched.group(1)))
        # Radian
        elif fmt.find("RRR") >= 0:
            pattern = fmt.replace("RRR", digit_pattern, 1)
            matched = re.match(pattern, angle_str)
            if matched:
                return cls.from_rad(float(matched.group(1)))
        # (x, y)
        elif fmt.find("XXX") >= 0 and fmt.find("YYY") >= 0:
            pattern = fmt.replace("XXX", digit_pattern,
                                  1).replace("YYY", digit_pattern, 1)
            matched = re.match(pattern, angle_str)
            if matched:
                return cls.from_atan2(x=matched.group(1), y=matched.group(2))
        raise Exception("Invalid fmt!")
        return None

    def __adjust(self):
        """Adjust the Format of the Angle, Satisfy: `0 <= __deg < 360`
        """
        while self.__deg < 0:
            self.__deg += 360
        while self.__deg >= 360:
            self.__deg -= 360

    def get_deg(self) -> int:
        """Get deg in the fmt of DMS
        """
        return int(float(self.__deg))

    def get_min(self) -> int:
        """Get minute in the fmt of DMS
        """
        return int((self.__deg - Fraction(self.get_deg())) * Fraction(60))

    def get_sec(self) -> float:
        """Get sec in the fmt of DMS
        """
        angle_min = self.__deg * 60
        return float((angle_min - int(angle_min)) * 60)

    def __add__(self, other: "Angle"):
        """(+)Calculate the sum of self and angle
        """
        return self.from_degrees(self.__deg.__add__(other.__deg))

    def __sub__(self, other: "Angle"):
        """(-)Calculate the difference of self(minuend) and angle(subtrahend)
        """
        return self.from_degrees(self.__deg.__sub__(other.__deg))

    def __mul__(self, n: float):
        """(*)Calculate the product of self and angle
        """
        return self.from_degrees(self.__deg.__mul__(Fraction(n)))

    def __truediv__(self, n: float):
        """(/)Calculate the true quotient of self(dividend) and angle(divisor)
        """
        return self.from_degrees(self.__deg.__truediv__(Fraction(n)))

    def __floordiv__(self, n: float):
        """(//)Calculate the floor quotient of self(dividend) and angle(divisor)
        """
        return self.from_degrees(self.__deg.__floordiv__(Fraction(n)))

    def __mod__(self, other: "Angle"):
        """(%)Calculate the remainder of self(dividend) and angle(divisor)
        """
        return self.from_degrees(self.__deg.__mod__(other.__deg))

    def __cmp__(self, other: "Angle"):
        """compare two angles
        negative if self <  other;
        zero     if self == other;
        positive if self >  other;
        """
        return self.__deg - other.__deg

    def __eq__(self, other: "Angle"):
        """==
        """
        return self.__cmp__(other) == 0

    def __ne__(self, other: "Angle"):
        """!=
        """
        return self.__cmp__(other) != 0

    def __le__(self, other: "Angle"):
        """\<=
        """
        return self.__cmp__(other) <= 0

    def __lt__(self, other: "Angle"):
        """\<
        """
        return self.__cmp__(other) < 0

    def __ge__(self, other: "Angle"):
        """\>=
        """
        return self.__cmp__(other) >= 0

    def __gt__(self, other: "Angle"):
        """\>
        """
        return self.__cmp__(other) > 0

    def __str__(self) -> str:
        """Convert angle into string(" " as interval)
        """
        return "{:d} {:d} {:.2f}".format(self.get_deg(), self.get_min(),
                                         self.get_sec())

    def __hash__(self) -> int:
        """hash(self)
        """
        return self.__deg.__hash__()

    def to_degrees(self) -> float:
        """Convert angle into degrees(only deg(float) without minute & sec)
        """
        return float(self.__deg)

    def to_rad(self) -> float:
        """Convert angle into radians(float)
        """
        return math.radians(self.to_degrees())

    def to_atan2(self, x: float = None, y: float = None) -> (float, float):
        """Convert angle into `(x', y')` by `(x, y)`.
        When one of x, y is None, calculate the value of the other one(if not None) that satisfies `y / x = tan`.
        When both of x, y is Nones, returns (x', y') that satisfies `x'^2 + y'^2 = 1`, that is, `x' = cos, y' = sin`
        When both of x, y is not None, return themselves if they are a coordinates of the valid ones, or raise Exception.
        Eg.
        >>> angle = Angle(deg=45)
        >>> angle.to_atan2(x=1.0)
        (1.0, 1.0)
        >>> angle.to_atan2(y=2.0)
        (2.0, 2.0)
        >>> angle.to_atan2()
        (0.707, 0.707)
        """
        # base: tan = y / x
        # default: x = cos, y = sin
        if x is None and y is None:
            return self.cos(), self.sin()
        # calculate x by y
        elif x is None:
            return y / self.tan(), y  # x = y / tan
        # calculate y by x
        elif y is None:
            return x, x * self.tan()  # y = x * tan
        # x, y are both not None and valid
        elif math.isclose(y / x, self.tan()):
            return x, y  # return themselves if checked
        raise Exception("Invalid x,y pair!")

    def to_fmt_str(self, fmt: str = "aaa°bbb′ccc″", decimal: int = 2) -> str:
        """Convert angle into string of fmt
        `aaa` is deg(int), `bbb` is minute(int), `ccc` is sec(float), `DDD` is only deg(float),
        `RRR` is radian(float), `XXX` is horizontal ordinate(float), `YYY` is vertical ordinate(float).
        > default: fmt = "aaa°bbb′ccc″"`, decimal = 2
        Eg.
        >>> angle = Angle(45)
        >>> angle.to_fmt_str()
        2°4′6″
        >>> angle.to_fmt_str(fmt="aaaDbbbMcccS")
        3D5M7S
        >>> angle.to_fmt_str(fmt="aaa bbb ccc")
        1 4 9
        >>> angle.to_fmt_str(fmt="DDDD")
        123D
        >>> angle.to_fmt_str(fmt="RRRrad")
        1.99rad
        >>> angle.to_fmt_str(fmt="XXX, YYY")
        0.5, 0.87
        """
        # DMS
        if fmt.find("aaa") >= 0 and fmt.find("bbb") >= 0 and fmt.find(
                "ccc") >= 0:
            return (fmt.replace("aaa", str(self.get_deg()), 1)  # deg
                    .replace("bbb", str(self.get_min()), 1)  # minute
                    .replace("ccc",
                             ("{:.%df}" % decimal).format(self.get_sec()),
                             1)  # sec
                    )
        # Degree
        elif fmt.find("DDD") >= 0:
            return fmt.replace("DDD",
                               ("{:.%df}" % decimal).format(self.to_degrees()),
                               1)
        # Radian
        elif fmt.find("RRR") >= 0:
            return fmt.replace("RRR",
                               ("{:.%df}" % decimal).format(self.to_rad()), 1)
        # (x, y)
        elif fmt.find("XXX") >= 0 and fmt.find("YYY") >= 0:
            x_y_pair = self.to_atan2()  # get the tuple of (x, y)
            return fmt.replace(
                "XXX", ("{:.%df}" % decimal).format(x_y_pair[0])).replace(
                    "YYY", ("{:.%df}" % decimal).format(x_y_pair[1]))
        raise Exception("Invalid fmt!")

    def sin(self) -> float:
        """sinθ
        """
        return math.sin(self.to_rad())

    def cos(self) -> float:
        """cosθ
        """
        return math.cos(self.to_rad())

    def tan(self) -> float:
        """tanθ
        """
        return math.tan(self.to_rad())

    def is_zero_angle(self) -> bool:
        """Judge if it is zero angle, that is, 0°
        """
        return math.isclose(self.to_degrees(), 0)

    def is_acute_angle(self) -> bool:
        """Judge if it is acute angle(0°, 90°)
        """
        return 0 < self.to_degrees() < 90

    def is_right_angle(self) -> bool:
        """Judge if it is right angle, that is, 90°
        """
        return math.isclose(self.to_degrees(), 90)

    def is_obtuse_angle(self) -> bool:
        """Judge if it is obtuse angle(90°, 180°)
        """
        return 90 < self.to_degrees() < 180

    def is_minor_angle(self) -> bool:
        """Judge if it is minor angle(0°, 180°)
        """
        return 0 < self.to_degrees() < 180

    def is_straight_angle(self) -> bool:
        """Judge if it is right angle, that is, 180°
        """
        return math.isclose(self.to_degrees(), 180)

    def is_major_angle(self) -> bool:
        """Judge if it is major angle(180°, 360°)
        """
        return 180 < self.to_degrees() < 360

    def is_complementary_angle_with(self, other: "Angle") -> bool:
        """Judge if they are complementary angles
        """
        return math.isclose((self + other).to_degrees(), 90)

    def is_supplementary_angle_with(self, other: "Angle") -> bool:
        """Judge if they are supplementary angles
        """
        return math.isclose((self + other).to_degrees(), 180)
コード例 #10
0
ファイル: F.py プロジェクト: Guibrother32/M210
class F(Fraction):
    '''
    Classe que representa um numero fracionario, extendendo fractions.Fraction
    '''
    def __init__(self, n, m=Fraction(0)):
        self.fraction = Fraction(n)
        self.m = Fraction(m)

    def __repr__(self):
        """repr(self)"""
        return str(float(self.fraction)) if self.m == 0 else str(
            float(self.fraction)) + ' + (' + str(float(self.m)) + '*M)'

    def __str__(self):
        """str(self)"""
        return str(float(self.fraction)) if self.m == 0 else str(
            float(self.fraction)) + ' + (' + str(float(self.m)) + '*M)'

    def __eq__(self, f):
        """a == b"""
        if type(f) is not type(self):
            f = F(f)

        return self.fraction.__eq__(f.fraction) and self.m.__eq__(f.m)

    def __add__(self, f):
        """a + b"""
        if type(f) is not type(self):
            f = F(f)

        return F(self.fraction.__add__(f.fraction), self.m.__add__(f.m))

    def ___sub__(self, f):
        """a - b"""
        if type(f) is not type(self):
            f = F(f)

        return F(self.fraction.__sub__(f.fraction), self.m.___sub__(f.m))

    def __mul__(self, f):
        """a * b"""
        if type(f) is not type(self):
            f = F(f)

        if f.m == 0:
            return F(self.fraction.__mul__(f.fraction))
        else:
            return F(self.fraction.__mul__(f.fraction), self.m.__mul__(f.m))

    def __div__(self, f):
        """a / b"""
        if type(f) is not type(self):
            f = F(f)

        if f.m == 0:
            return F(self.fraction.__div__(f.fraction))
        else:
            return F(self.fraction.__div__(f.fraction), self.m.__div__(f.m))

    def __lt__(self, f):
        """a < b"""
        if type(f) is not type(self):
            f = F(f)

        if self.m == f.m:
            return self.fraction.__lt__(f.fraction)

        else:
            return self.m.__lt__(f.m)

    def __gt__(self, f):
        """a > b"""
        if type(f) is not type(self):
            f = F(f)

        if self.m == f.m:
            return self.fraction.__gt__(f.fraction)

        else:
            return self.m.__gt__(f.m)

    def __le__(self, f):
        """a <= b"""
        if type(f) is not type(self):
            f = F(f)

        if self.m == f.m:
            return self.fraction.__le__(f.fraction)

        else:
            return self.m.__le__(f.m)

    def __ge__(self, f):
        """a >= b"""
        if type(f) is not type(self):
            f = F(f)

        if self.m == f.m:
            return self.fraction.__ge__(f.fraction)

        else:
            return self.m.__ge__(f.m)