def test_sets(): x = Integer(2) y = Integer(3) x1 = sympy.Integer(2) y1 = sympy.Integer(3) assert Interval(x, y) == Interval(x1, y1) assert Interval(x1, y) == Interval(x1, y1) assert Interval(x, y)._sympy_() == sympy.Interval(x1, y1) assert sympify(sympy.Interval(x1, y1)) == Interval(x, y) assert sympify(sympy.EmptySet()) == EmptySet() assert sympy.EmptySet() == EmptySet()._sympy_() assert FiniteSet(x, y) == FiniteSet(x1, y1) assert FiniteSet(x1, y) == FiniteSet(x1, y1) assert FiniteSet(x, y)._sympy_() == sympy.FiniteSet(x1, y1) assert sympify(sympy.FiniteSet(x1, y1)) == FiniteSet(x, y) x = Interval(1, 2) y = Interval(2, 3) x1 = sympy.Interval(1, 2) y1 = sympy.Interval(2, 3) assert Union(x, y) == Union(x1, y1) assert Union(x1, y) == Union(x1, y1) assert Union(x, y)._sympy_() == sympy.Union(x1, y1) assert sympify(sympy.Union(x1, y1)) == Union(x, y) assert Complement(x, y) == Complement(x1, y1) assert Complement(x1, y) == Complement(x1, y1) assert Complement(x, y)._sympy_() == sympy.Complement(x1, y1) assert sympify(sympy.Complement(x1, y1)) == Complement(x, y)
def detect_multi_stability(cls, chnk_stable, chnk_unstable, bi_data_np): if cls.is_list_empty(chnk_stable) or cls.is_list_empty(chnk_unstable): return False chnk_stable_pcp_ranges = [] for i in range(len(chnk_stable)): end_ind = len(chnk_stable[i]) - 1 chnk_stable_pcp_ranges.append([ bi_data_np[chnk_stable[i][0], 0], bi_data_np[chnk_stable[i][end_ind], 0] ]) [i.sort() for i in chnk_stable_pcp_ranges] chnk_unstable_pcp_ranges = [] for i in range(len(chnk_unstable)): end_ind = len(chnk_unstable[i]) - 1 chnk_unstable_pcp_ranges.append([ bi_data_np[chnk_unstable[i][0], 0], bi_data_np[chnk_unstable[i][end_ind], 0] ]) [i.sort() for i in chnk_unstable_pcp_ranges] chnk_unstable_pcp_intervals = [] chnk_stable_pcp_intervals = [] for i in range(len(chnk_unstable)): chnk_unstable_pcp_intervals.append( sympy.Interval(chnk_unstable_pcp_ranges[i][0], chnk_unstable_pcp_ranges[i][1])) for i in range(len(chnk_stable)): chnk_stable_pcp_intervals.append( sympy.Interval(chnk_stable_pcp_ranges[i][0], chnk_stable_pcp_ranges[i][1])) # building intersections of unstable branch with stable branches unstable_intersections = [] for i in chnk_unstable_pcp_intervals: temp_list = [] for j in chnk_stable_pcp_intervals: temp = i.intersect(j) if temp != sympy.EmptySet(): if not temp.is_FiniteSet: temp_list.append(1) elif temp.is_FiniteSet and len(list(temp)) > 1: temp_list.append(1) unstable_intersections.append(temp_list) return any([sum(i) >= 2 for i in unstable_intersections])
def manualSolveSystem(linear, total): """linear should be A+B*X, total should be number returns set X values so that linear <= total""" coeffs = sympy.Poly(linear, X).all_coeffs() A,B = 0,0 if len(coeffs) == 1: A = coeffs[0] B = 0 else: B, A = coeffs if B > 0: return sympy.Interval(-sympy.oo, (total-A)/B) elif B < 0: return sympy.Interval((total-A)/B, sympy.oo) else: if A <= total: return sympy.Interval(-sympy.oo, sympy.oo) else: return sympy.EmptySet()
def findCutPoints(eVals, intervals, aNum, dNum, kNum): """Finds values of X where below that value, some e variable must be 0. returns (list of constraints that always happen, cut list) cut list is ordered list of (Xcut, constraint, expressionForXToBeGreaterThan) intervals is a list of [(min,max), (min,max)] where min and max are sympy expressions with X as a variable""" substitutions = {a: aNum, d: dNum, k: kNum} always = [] cuts = [] total = sympy.S(a+d+3*d*k)/(a+3*d*k) #Loop through each e variable, and find the minimum X that allows it to be nonzero. for index in eVals: eVar = eVals[index] #minimum amount of muffin that can be found in given intervals from index minValue = sum(numInInterval * interval[0] for numInInterval, interval in zip(index, intervals)) #maximum amount of muffin that can be found in given intervals from index maxValue = sum(numInInterval * interval[1] for numInInterval, interval in zip(index, intervals)) #X values which will allow such a student to exist #allowedXValues = sympy.solve([minValue <= total, total <= maxValue], X).as_set() allowedXValues, wasMinAndNotMax = manualSolveALessBLessC(minValue.subs(substitutions), total.subs(substitutions), maxValue.subs(substitutions)) XshouldBeGreaterThan = None if wasMinAndNotMax: XshouldBeGreaterThan = sympy.solve([minValue - total], X) else: XshouldBeGreaterThan = sympy.solve([maxValue - total], X) XshouldBeGreaterThan = getValFromDict(XshouldBeGreaterThan) #note 2a/5 below. Bill sent me this bound in an email. Is this actually where X should start? if allowedXValues == sympy.EmptySet() or allowedXValues.end <= 0:# 2*a/5:#if the constraint can never be met or is smaller than X can be always.append(eVar == 0)#add constraint that variable is zero else:#otherwise if the constraint can be met cuts.append((allowedXValues.end, eVar == 0, XshouldBeGreaterThan))#add on (min val of X where constraint can't be met, constraint that variable is zero) cuts = sorted(cuts, key=lambda cut: cut[0]) return (always, cuts)
def full_solve(text, *, doprint=True): if not doprint: print = NOPRINT else: print = CPRINT # Common OCR mistakes text = text_utils.fix_common_mistakes(text) text = text_utils.fix_syntax_mistakes(text) text = text_utils.casefix(text) text = text_utils.fix_exponentation(text) # Left and right side of the equation text1, text2 = text_utils.find_equation_sides(text) # Extract the variable in the equation vars1 = text_utils.find_variables(text1) vars2 = text_utils.find_variables(text2) # Get both the left and right side variables = list(set(vars1) | set(vars2)) print("===Start computing===") expr1 = sympy_utils.simple_expr_parse(text1) expr2 = sympy_utils.simple_expr_parse(text2) equation = "{} = {}".format(expr1, expr2) print("The equation: {}".format(equation)) if not variables: if sp.simplify(expr1) == sp.simplify(expr2): return sp.S.Reals else: return sp.EmptySet() #result = sympy_utils.simple_solve(expr1, expr2, variables) #print("Result: {}".format(result)) #return result result = list(sympy_utils.solve_all(expr1, expr2, variables)) for example in result: toprint = "{} ∈ {}".format(example[0], example[1]) print(toprint) return result
print("VERFAHREN FUR + - oo:\n") plus = function.subs(x, sp.oo) minus = function.subs(x, -sp.oo) print("X gegen Plus Unendlich = ", plus, ";\n") print("X gegen Minus Unendlich = ", minus, ";\n") # Schritt 4 Schnittpunkten mit Achsen xSchnitt = sp.solveset(sp.Eq(function, 0), domain=sp.S.Reals) ySchnitt = function.subs(x, 0) print("SCHNITTPUNKTEN MIT ACHSEN:\n") if (xSchnitt == sp.EmptySet()): print("Kein Schnittpunkten mit X-Achse;\n") else: print("Mit X-Achsee: ", xSchnitt, ";\n") if (ySchnitt == sp.EmptySet()): print("Kein Schnittpunkten mit Y-Achse;\n") else: print("Mit Y-Achsee: ", ySchnitt, ";\n") #Schritt 5 Extremstellen firstDiff = sp.diff(function) mEST = sp.solveset(sp.Eq(firstDiff, 0))
def domain_remove_asymptotes(interval, asymptotes_general_equation): if interval.is_left_unbounded: raise ValueError('The interval given is left-unbounded') elif interval.is_right_unbounded: raise ValueError('The interval given is right-unbounded') x0, x1 = sympy.Wild('x0'), sympy.Wild('x1') match = asymptotes_general_equation.match(x0*k + x1) # solving inequalities over integer symbols is not yet implemented in SymPy. We need to use a workaround # we transform the interval by doing (interval - x1)/x0, then finding integer values in this range, then transforming back by # doing (integers * x0) + x1 to give us all the asymptotes in the interval intermediate_interval = sympy.Interval((interval.left - match[x1]) / match[x0], (interval.right - match[x1]) / match[x0], False, False) # for the left bound, if it is not an integer we must round towards the middle of the interval (i.e. up) # likewise, we must round the right bound towards the middle of the interval (i.e. down) if isinstance(intermediate_interval.left, sympy.Integer): left_bound = intermediate_interval.left elif isinstance(intermediate_interval.left, sympy.Rational): left_bound = int(math.floor(intermediate_interval.left)) + 1 if isinstance(intermediate_interval.right, sympy.Integer): right_bound = intermediate_interval.right elif isinstance(intermediate_interval.right, sympy.Rational): right_bound = int(math.floor(intermediate_interval.right)) untransformed_asymptotes = list(range(left_bound, right_bound + 1)) asymptotes = [i * match[x0] + match[x1] for i in untransformed_asymptotes] # now we have the original interval, as well as all asymptotes that we need to exclude - let's make the # sympy.Interval/sympy.Union object exclude_left, exclude_right = False, False if interval.left_open & (interval.left == asymptotes[0]): # the left of the domain is an asymptote, exclude it from the final domain exclude_left = True asymptotes.pop(0) elif interval.left_open: exclude_left = True if interval.right_open & len(asymptotes) > 0 & (interval.right == asymptotes[-1]): # the right of the domain is an # asymptote, exclude it from the final domain exclude_right = True asymptotes.pop(-1) elif interval.right_open: exclude_right = True # starting from the left of the interval, we make a union of all intervals separated by asymptotes domain = sympy.EmptySet() if len(asymptotes) > 0: domain += sympy.Interval(interval.left, asymptotes[0], exclude_left, True) else: domain += sympy.Interval(interval.left, interval.right, exclude_left, exclude_right) while len(asymptotes) > 1: domain += sympy.Interval(asymptotes[0], asymptotes[1], True, True) asymptotes.pop(0) if len(asymptotes) == 1: domain += sympy.Interval(asymptotes[0], interval.right, True, exclude_right) else: domain += sympy.Interval(domain.right, interval_right, True, exclude_right) return domain
def symlabels(labels=None): if labels: return sympy.FiniteSet(*(sympy.Symbol(l) if isinstance(l, str) else l for l in labels)) else: return sympy.EmptySet()