Exemple #1
0
def solvePositive(polynomes, discriminent):
    solution1 = (-polynomes[0][1] -
                 my_math.sqrt(discriminent)) / (2 * polynomes[0][2])
    solution2 = (-polynomes[0][1] +
                 my_math.sqrt(discriminent)) / (2 * polynomes[0][2])
    print("Deux solutions reelles : X = " + str(solution1) + " et X = " +
          str(solution2))
Exemple #2
0
    def solve_degree2(self):
        (left, right) = self.state
        degree0 = 0
        degree1 = 0
        degree2 = 0
        string = ''

        for index, term in enumerate(left):
            if term['degree'] == 0:
                degree0 = term['coeff']
            elif term['degree'] == 1:
                degree1 = term['coeff']
            elif term['degree'] == 2:
                degree2 = term['coeff']

        # Quadrantic formula
        # https://en.wikipedia.org/wiki/Quadratic_formula
        # https://en.wikipedia.org/wiki/Discriminant
        discriminant = my_math.pow(degree1, 2) - 4 * degree2 * degree0
        sqrt_discri = my_math.sqrt(discriminant)
        if discriminant == 0.0:
            solution = self.format_float(-degree1 / (2 * degree2))
            string += 'Discriminant is 0:\n{}'.format(solution)
        elif discriminant > 0.0:
            solution_a = (-degree1 + sqrt_discri) / (2 * degree2)
            solution_b = (-degree1 - sqrt_discri) / (2 * degree2)
            string += 'Discriminant is strictly positive, the two solutions are:\n{}\n{}'.format(solution_a, solution_b)
        elif discriminant < 0.0:
            a = self.format_float(-degree1 / (2 * degree2))
            b = self.format_float(sqrt_discri / (2 * degree2))
            string += 'Discriminant is strictly negative, the two complex solutions are:\n'
            string += '{} + i * {}\n'.format(a, b)
            string += '{} - i * {}'.format(a, b)

        return string
Exemple #3
0
 def normal_x(x, mu=mu, sigma=sigma):
     if not is_number(x):
         raise my_exceptions.ErrorDeTipo()
     if sigma == 0:
         raise my_exceptions.ErrorMatematico()
     f_x = (1 / sqrt(2 * pi * sigma**2)) * e**((-1 / 2) *
                                               ((x - mu) / sigma)**2)
     return f_x
Exemple #4
0
 def test_sqrt(self):
     self.assertAlmostEqual(m.sqrt(5),2.236067977)
     self.assertEqual(m.sqrt(-2),None)
     self.assertAlmostEqual(m.sqrt(5.12),2.2627416997)
     self.assertAlmostEqual(m.sqrt(-25.5),None)
     self.assertEqual(m.sqrt(64,3),4)
     self.assertEqual(m.sqrt(-64,3),None)
     self.assertAlmostEqual(m.sqrt(8,-3),0.5)
Exemple #5
0
def solve_degree2(polinom):
    a = 0
    b = 0
    c = 0
    for elem in polinom:
        if elem["pow_x"] == 0:
            c = elem["num"]
        if elem["pow_x"] == 1:
            b = elem["num"]
        if elem['pow_x'] == 2:
            a = elem["num"]
    D = b * b - 4 * a * c
    if D < 0:
        print("Discriminant is negative, there are no solutions.")
        exit(0)
    if D == 0:
        return -b / 2 * a
    x1 = (-b + sqrt(D)) / (2 * a)
    x2 = (-b - sqrt(D)) / (2 * a)
    return x1, x2
def DESV(column, o_instance):
    if isinstance(column, str):
        if column in dir(o_instance):
            column = getattr(o_instance, column)
        else:
            raise my_exceptions.ReferenciaInvalida()
    if not is_iterable(column):
        raise my_exceptions.ErrorDeTipo()

    l_col = list(column)
    avg = PROM(l_col, o_instance)
    sigma = sqrt(sum((i - avg)**2 for i in l_col) / len(l_col))

    return sigma
# import custom.my_math # this works

print("Module path patching...")
# alter pathes to be able to load custom module
import math
custom_module_path = os.path.join(os.getcwd(), "custom")
# os.path.abspath(os.path.dirname(__file__))
module_pathes.insert(0, custom_module_path)

import my_math
print("Python: D'oh, now I see my_math...")

print(" 9 / 5 to upper =", my_math.div_ceil(9, 5))

from my_math import sqrt
from math import sqrt

print("(my_math, math) Sqrt: ", sqrt(9))

from my_math import sqrt

print("(my_math, math, my_math) Sqrt: ", sqrt(9))

from my_math import gcd

print("gcd(21, 14) == ", gcd(21, 14))

# As your can see -- last import "wins" in case of name clashing.
# NB: if you try to rename my_math module to math to completely full python
#     if wouldn't work, since looks like math is one of buildins modules
Exemple #8
0
def solveNegative(polynomes, discriminent):
    solution1 = str(-polynomes[0][1] / 2 * polynomes[0][0]) + " + i * " + str(
        my_math.sqrt(-discriminent) / 2 * polynomes[0][0])
    solution2 = str(-polynomes[0][1] / 2 * polynomes[0][0]) + " - i * " + str(
        my_math.sqrt(-discriminent) / 2 * polynomes[0][0])
    print("Deux solutions reelles : X = " + solution1 + " et X = " + solution2)
Exemple #9
0
 def test_sqrt(self):
     self.assertEqual(my_math.sqrt(0), 0)
     self.assertEqual(my_math.sqrt(4), 2)
     self.assertEqual(my_math.sqrt(7), 2.6457513110645907)
     self.assertEqual(my_math.sqrt(125348), 354.04519485512014)
     self.assertEqual(my_math.sqrt(133333333387), 365148.37174359686)
Exemple #10
0
# import custom.my_math # this works

print("Module path patching...")
# alter pathes to be able to load custom module
import math
custom_module_path = os.path.join(os.getcwd(), "custom")
# os.path.abspath(os.path.dirname(__file__))
module_pathes.insert(0, custom_module_path)

import my_math
print("Python: D'oh, now I see my_math...")

print(" 9 / 5 to upper =", my_math.div_ceil(9, 5))

from my_math import sqrt
from math import sqrt

print("(my_math, math) Sqrt: ", sqrt(9))

from my_math import sqrt

print("(my_math, math, my_math) Sqrt: ", sqrt(9))

from my_math import gcd

print("gcd(21, 14) == ", gcd(21, 14))

# As your can see -- last import "wins" in case of name clashing.
# NB: if you try to rename my_math module to math to completely full python
#     if wouldn't work, since looks like math is one of buildins modules
Exemple #11
0
import time
from my_math import sqrt
start = time.time()
lim = 211
ls = [True for x in range(lim+1)]

for i in range(2, sqrt(lim)+1):
	if ls[i] == True:
		j = i**2
		while j < lim:
			ls[j] = False
			j += i

total = -2
for i in range(lim+1):
	if ls[i] == True:
		total += 1
print(total)

print('time:', time.time() - start)