def binom(n, k): if type(n) == str: n = polynomial.polynomial_parser(n) if type(k) == str: k = polynomial.polynomial_parser(k) num = expression_add([expression_mult([factor.factorial(n)])]) den = expression_add([ expression_mult([ factor.factorial(k), factor.factorial( polynomial.polynomial_parser('{}-({})'.format( n.to_string(), k.to_string()))) ]) ]) return expression_rat(num, den)
def test_function_validates_input(self): f = factorial(5) self.assertEqual(f, 126)
import random from cmath import sqrt, cos # from math import * from math import sqrt as normal_sqrt from factor import factorial mamama = ["м", "a"] print(random.choice(mamama)) print(sqrt(-1)) print(normal_sqrt(4)) # print(randint(0,10)) n = int(input("Введите число ")) print(factorial(n))
def expression_parser(s): def simplify(stack): if len(stack) == 1: return stack if stack[-2] == '(': return stack if stack[-1] == '(': stack.append(to_expr_r(polynomial.constant(0))) return stack b, op, a = stack.pop(), stack.pop(), stack.pop() if a == '(': stack.append(a) assert op == '-', 'Something is weird' stack.append(b.negate()) return simplify(stack) if op == '+': stack.append(a.add(b)) elif op == '-': stack.append(a.subtract(b)) elif op == '*': stack.append(a.multiply(b)) elif op == '/': stack.append(a.divide(b)) else: print('SOMETHING IS GOING WRONG') print('TRYING TO SIMPLIFY AND HAVE a, op, b AS') try: print(type(a)) a.PRINT() except: print(a) try: print(type(op)) op.PRINT() except: print(op) try: print(type(b)) b.PRINT() except: print(b) raise Exception('Parse general error') return simplify(stack) to_split = ['B[', 'F[', 'P[', ']', ',', '/', '*', '+', '-', '(', ')', '^'] s = s.replace(' ', '') for x in to_split: s = s.replace(x, ' {} '.format(x)) parts = s.split() stack = [] i = 0 while i < len(parts): part = parts[i] if len(part) == 0: i += 1 continue assert part != ',' and part != ']', 'Parse general error for string {}'.format( s) if part == '(': if stack and type(stack[-1]) == expression_rat: stack.append('*') stack.append('(') i += 1 elif part == ')': stack = simplify(stack) stack = stack[:-2] + stack[-1:] i += 1 elif part == 'B[': if stack and type(stack[-1]) == expression_rat: stack.append('*') j = i + 1 while parts[j] != ',': j += 1 n = polynomial.polynomial_parser(''.join(parts[i + 1:j])) i = j j = i + 1 while parts[j] != ']': j += 1 k = polynomial.polynomial_parser(''.join(parts[i + 1:j])) stack.append(to_expr_r(binom(n, k))) i = j + 1 elif part == 'F[': if stack and type(stack[-1]) == expression_rat: stack.append('*') j = i + 1 while parts[j] != ']': j += 1 stack.append( to_expr_r( factor.factorial( polynomial.polynomial_parser(''.join(parts[i + 1:j]))))) i = j + 1 elif part == 'P[': if stack and type(stack[-1]) == expression_rat: stack.append('*') j = i + 1 while parts[j] != ',': j += 1 a = int(''.join(parts[i + 1:j])) i = j j = i + 1 while parts[j] != ']': j += 1 n = polynomial.polynomial_parser(''.join(parts[i + 1:j])) stack.append(to_expr_r(factor.power(a, n))) i = j + 1 elif part in ['/', '*']: stack.append(part) i += 1 elif part in ['+', '-']: stack = simplify(stack) stack.append(part) i += 1 elif part == '^': cur = stack.pop() stack.append(cur.power(int(parts[i + 1]))) i += 2 else: if stack and type(stack[-1]) == expression_rat: stack.append('*') stack.append(to_expr_r(polynomial.polynomial_parser(part))) i += 1 stack = simplify(stack) return stack[0]
'''Used for testing. Gets F(n,k) and returns a_k/a_{k-1}''' def get_quotient(fnk): num_string = '({})-({})'.format(fnk.replace('n', '(n+1)'), fnk) den_string = num_string.replace('k', '(k-1)') quot_string = '({})/({})'.format(num_string, den_string) out = expression_parser(quot_string) out.simplify_complete() return out if __name__ == '__main__': print('TESTING EXPRESSIONS') p1 = polynomial.polynomial_parser('mn+k^2') f1 = factor.factorial(polynomial.polynomial_parser('n')) p2 = polynomial.polynomial_parser('m^2n^2+k') f2 = factor.factorial(polynomial.polynomial_parser('n+m')) em1 = expression_mult([p1, f1]) em2 = expression_mult([p1, f2]) em3 = expression_mult([p2, f1]) em4 = expression_mult([p2, f2]) ea1 = expression_add([em1, em2]) ea2 = expression_add([em3, em4]) er1 = expression_rat(ea1, ea2) p1.PRINT() f1.PRINT() p2.PRINT() f2.PRINT() em1.PRINT() em2.PRINT()