Beispiel #1
0
def get_complex(delta, b, a):
    str_min, str_plus = '', ''
    min, plus, denum = -sqrt(-delta), sqrt(-delta), 2 * a
    complex_min, complex_plus, real = None, None, None
    if denum.is_integer():
        if min.is_integer():
            complex_min = irreductible(min, denum)
        if plus.is_integer():
            complex_plus = irreductible(plus, denum)
        if float(b).is_integer():
            real = irreductible(b, denum)
        if complex_min == '':
            complex_min = None
        if complex_plus == '':
            complex_plus = None
        if real == '':
            real = None
        if complex_min != None or real != None:
            if complex_min != None:
                str_min += '{}i + '.format(complex_min)
                if real != None:
                    str_min += real
                else:
                    str_min += str(b / (2 * a))
            elif real != None:
                str_min = '{}i + {}'.format(min / (2 * a), real)
        if complex_plus != None:
            str_plus += '{}i + '.format(complex_plus)
            if real != None:
                str_plus += real
            else:
                str_plus += str(b / (2 * a))
        elif real != None:
            str_plus = '{}i + {}'.format(plus / (2 * a), real)
    return (str_plus.strip(), str_min.strip())
Beispiel #2
0
def get_fraction(b, a, delta, sign):
    if sign == '-':
        num = -b - sqrt(delta)
    else:
        num = -b + sqrt(delta)
    denum = 2 * a
    return (irreductible(num, denum))
Beispiel #3
0
    def test_sqrt(self):
        test_sets = [
            [0.0, 0.0],
            [0.25, 0.5],
            [1.0, 1.0],
            [100.0, 10.0],
            [315.2354235, 17.754870416311128],
            [10 ** 10, 10 ** 5]
        ]
        error_values = [-0.25, -0.001, -123.5325, -645634.43]

        for arg, expected_result in test_sets:
            assert compare_floats_with_epsilon(sqrt(arg), expected_result)
        for error_value in error_values:
            with pytest.raises(ValueError):
                sqrt(error_value)
Beispiel #4
0
def rotMatrix2AxisAndAngle(R):
    """
    stackoverflow.com/questions/12463487/obtain-rotation-axis-from-rotation-matrix-and-translation-vector-in-opencv

    R : 3x3 rotation matrix
    returns axis, angle

    """
    angle = np.arccos(old_div((R[0, 0] + R[1, 1] + R[2, 2] - 1), 2))
    axis = np.array([
        # x
        old_div((R[2, 1] - R[1, 2]), utils.sqrt((R[2, 1] - R[1, 2])
                                                ** 2 + (R[0, 2] - R[2, 0])**2 + (R[1, 0] - R[0, 1])**2)),
        # y
        old_div((R[0, 2] - R[2, 0]), utils.sqrt((R[2, 1] - R[1, 2])
                                                ** 2 + (R[0, 2] - R[2, 0])**2 + (R[1, 0] - R[0, 1])**2)),
        # z
        old_div((R[1, 0] - R[0, 1]), utils.sqrt((R[2, 1] - R[1, 2])**2 + 
                                                (R[0, 2] - R[2, 0])**2 + (R[1, 0] - R[0, 1])**2))])
    return axis, angle
Beispiel #5
0
    def solveDeg2(self, reduced):
        a = reduced["2"]
        b = reduced["1"]
        c = reduced["0"]
        delta = b * b - 4 * a * c

        if delta < 0:
            sqr = sqrt(-delta, 0.001)
            print(
                "Discriminand is strictly negative, the 2 complex solutions are:"
            )
            print("(", -b, " + ", sqr, "i ) /", 2 * a)
            print("(", -b, " - ", sqr, "i ) /", 2 * a)
        elif delta == 0:
            print("Discriminant is equal to zero, the solution is:")
            print(-b / (2 * a))
        else:
            sqr = sqrt(delta, 0.001)
            print("Discriminant is strictly positive, the 2 solutions are:")
            print((-b + sqr) / (2 * a))
            print((-b - sqr) / (2 * a))
Beispiel #6
0
 def solve_quadratic_equation(self) -> Tuple[Any, ...]:
     discriminant: float = self.b * self.b - 4 * self.a * self.c
     if compare_floats_with_epsilon(discriminant, 0.0):
         x = -self.b / (2 * self.a)
         if self.verbose:
             print(f'Дискриминант равен нулю')
         print(
             f'Уравнение имеет 2 совпадающих вещественных решения:\nx0 = {x}\nx1 = {x}'
         )
         return x,
     elif discriminant > 0:
         discriminant_sqrt = sqrt(discriminant)
         x0 = (-self.b + discriminant_sqrt) / (2 * self.a)
         x1 = (-self.b - discriminant_sqrt) / (2 * self.a)
         if self.verbose:
             print(f'Дискриминант больше нуля')
         print(
             f'Уравнение имеет 2 вещественных решения:\nx0 = {x0}\nx1 = {x1}'
         )
         return x0, x1
     else:
         discriminant_sqrt = sqrt(-discriminant)
         operand1 = -self.b / (2 * self.a)
         if compare_floats_with_epsilon(operand1, 0.0):
             operand1 = abs(operand1)
         operand2 = discriminant_sqrt / (2 * self.a)
         if operand2 > 0.0:
             signs = ['+', '-']
         else:
             operand2 *= -1
             signs = ['-', '+']
         x0 = f'{operand1} {signs[0]} {operand2} * i'
         x1 = f'{operand1} {signs[1]} {operand2} * i'
         if self.verbose:
             print(f'Дискриминант меньше нуля')
         print(
             f'Уравнение не имеет вещественных решений и имеет 2 мнимых решения:\nx0 = {x0}\nx1 = {x1}'
         )
         return x0, x1
Beispiel #7
0
def axisAndAngle2RotMatrix(axis, angle):
    """
    http://stackoverflow.com/questions/6802577/python-rotation-of-3d-vector

    Return the rotation matrix associated with counterclockwise rotation about
    the given axis by angle radians.
    """
    axis = np.asarray(axis)
    angle = np.asarray(angle)
    axis = old_div(axis, utils.sqrt(np.dot(axis, axis)))
    a = utils.cos(old_div(angle, 2))
    b, c, d = -axis * utils.sin(old_div(angle, 2))
    aa, bb, cc, dd = a * a, b * b, c * c, d * d
    bc, ad, ac, ab, bd, cd = b * c, a * d, a * c, a * b, b * d, c * d
    return np.array([[aa + bb - cc - dd, 2 * (bc + ad), 2 * (bd - ac)],
                     [2 * (bc - ad), aa + cc - bb - dd, 2 * (cd + ab)],
                     [2 * (bd + ac), 2 * (cd - ab), aa + dd - bb - cc]])
Beispiel #8
0
 def distancia(self, lat, long):
     diffLat = lat - self._latitud
     diffLong = long - self._longitud
     return sqrt((diffLat * diffLat) + (diffLong * diffLong))