def point_on_curve(Xi, Yi): """ Return the (x, y)-coordinates of the point on the curve closest to (Xi, Yi) """ global l_function # Pythagoras's theorem gives us the distance: distance = sp.real_root((Xi - x)**2 + (Yi - function)**2, 2) # We're interested in the points where the derivative of the distance equals zero: derivative = sp.fraction(distance.diff().simplify()) derivative_zero = sp.solveset(derivative[0], x) # Previous line returns all solutions in the complex plane while we only want the real answers: derivative_zero = np.array( [n if sp.re(n) == n else None for n in derivative_zero]) derivative_zero = derivative_zero[derivative_zero != None] # Return the result closest to the function shortest_distance = np.Inf x_new = np.Inf for x_i in derivative_zero: if r(x_i, l_function(x_i), Xi, Yi) < shortest_distance: shortest_distance = r(x_i, l_function(x_i), Xi, Yi) x_new = x_i return (x_new, l_function(x_new))
def test_issue_11461(): matplotlib = import_module('matplotlib', min_module_version='1.1.0', catch=(RuntimeError,)) if not matplotlib: skip("Matplotlib not the default backend") x = Symbol('x') p = plot(real_root((log(x/(x-2))), 3), show=False) # Random number of segments, probably more than 100, but we want to see # that there are segments generated, as opposed to when the bug was present # and that there are no exceptions. assert len(p[0].get_segments()) >= 30
def real_norm(input): output = 0 for i in range(input.shape[1]): output += input[i]**2 output = sympy.real_root(output, 2) return output
def test_issue_11461(): if not matplotlib: skip("Matplotlib not the default backend") x = Symbol('x') p = plot(real_root((log(x / (x - 2))), 3), show=False) # Random number of segments, probably more than 100, but we want to see # that there are segments generated, as opposed to when the bug was present # and that there are no exceptions. assert len(p[0].get_segments()) >= 30
def main(): fi = sm.real_root(x, 3) fi2 = x**(1 / 3) yx = sm.lambdify(x, fi) yx2 = sm.lambdify(x, fi2) n = 200 if n <= 100: M = int(math.sqrt(n)) else: M = int(4 * math.log10(n)) m = -1 d = 3**0.5 a, b = get_aba(m, d, yx) a2 = yx(a) fx = 1 / (b - a) X, Y = get_XY(a, b, yx, n, check=False) XX, YY, table = func2(Y, M, n) print('равновероятный метод:') print( tabulate(table, headers='keys', floatfmt=".2f", tablefmt='fancy_grid')) gy = get_XY_teor(fi2, fx, a2) Gy = get_XY_teor(fi2, fx, a2, func=True) print('Хи-квадрат Пирсона:') ## Хи-квадрат Пирсона table22 = func2_1(table, Gy, n) print( tabulate(table22, headers='keys', floatfmt=".2f", tablefmt='fancy_grid')) ## Колмогорова print("Колмогорова:") n = 30 X, Y = get_XY(a, b, yx, n, check=False) XX2, YY2, _ = func1(Y, n) maxdiff = func2_2(XX2, YY2, Gy, n) ## Мизеса print("Мизеса:") n = 50 X, Y = get_XY(a, b, yx, n, check=False) table23 = func2_3(Y, Gy, n) print( tabulate(table23, headers='keys', floatfmt=".2f", tablefmt='fancy_grid')) plots(XX, YY, gy, XX2, YY2, Gy, maxdiff, yx, a2, b)
def main(): fi = sm.real_root(x, 3) fi2 = x**(1 / 3) yx = sm.lambdify(x, fi) yx2 = sm.lambdify(x, fi2) n = 20 m = -1 d = 3**0.5 a, b = get_aba(m, d, yx) a2, b2 = yx(a), yx(b) fx = 1 / (b - a) X, Y = get_XY(a, b, yx, n, check=False) func = lambda n: get_XY(a, b, yx, n, check=False) xs = tochn_m(Y) ds2 = tochn_d(Y, xs) #print(xs, ds2, ds) gy = get_XY_teor(fi2, fx, a2) xs_teor = get_md_teor(gy, a2, b2, D=False) ds2_teor = get_md_teor(gy, a2, b2, D=True) print("ds и ds_teor:", ds2, ds2_teor) print_inter_m() inter_m, inter_m_teor = get_inter_m((xs, ds2), (xs_teor, ds2_teor), a_t, [n] * len(a_t)) #print(list(map(lambda x: round(x,3), inter_m))) #inter_m_teor = get_inter_m(xs, ds_teor, a_t, [n]*len(a_t), teor=True) #print(list(map(lambda x: [round(x[0], 3), round(x[1], 3)], inter_m_teor))) inter2_m, inter2_m_teor = get_inter_m(func, (xs_teor, ds2_teor), [0.01] * len(n_list), n_list) #inter2_m_teor = get_inter_m(func, ds_teor, [0.01]*len(n_list), n_list) #inter_d = [get_inter_d(ds2, a, n) for a in xi_t] inter_d, inter_d_teor = get_inter_d((xs, ds2), (xs_teor, ds2_teor), a_t, [n] * len(a_t)) #inter_d_teor = get_inter_d(ds2_teor, a_t, [n]*len(a_t)) #print(list(map(lambda x: round(x,3), inter_d))) inter2_d, inter2_d_teor = get_inter_d(func, (xs_teor, ds2_teor), [0.01] * len(n_list), n_list) #inter2_d_teor = get_inter_d(ds2_teor, [0.01]*len(n_list), n_list) #inter_m_2 = [get_inter_m(xs, ds, t, n, inter=True) for t in table_t[n]] #table1 = {"":table_t[20]} #print(tabulate(table1, headers='keys', floatfmt=".2f", tablefmt='fancy_grid')) plots(inter_m, inter_m_teor, a_t, inter2_m, inter2_m_teor, inter_d, inter_d_teor, inter2_d, inter2_d_teor)
def emit_root(n, rational): arg = posarg + FiniteSet(8, 9) if n % 2: arg += FiniteSet(*(-x for x in arg)) func = {2: "sqrt", 3: "cbrt"}[n] if not rational: # Later testcases can use .qml.sqrt to represent irrational numbers, # but while testing .qml.sqrt itself just check the inverse. output(" %s_i:{%s:.qml.%s x};" % (func, "*".join(["x"]*n), func)) for x in sorted(arg): y = sp.real_root(x, n) if bool(sp.ask(sp.Q.rational(y))) == bool(rational): if rational: test(func, x, y) else: test("%s_i" % func, x, x)
def emit_root(n, rational): arg = posarg + FiniteSet(8, 9) if n % 2: arg += FiniteSet(*(-x for x in arg)) func = {2: "sqrt", 3: "cbrt"}[n] if not rational: # Later testcases can use .qml.sqrt to represent irrational numbers, # but while testing .qml.sqrt itself just check the inverse. output(" %s_i:{%s:.qml.%s x};" % (func, "*".join(["x"] * n), func)) for x in sorted(arg): y = sp.real_root(x, n) if bool(sp.ask(sp.Q.rational(y))) == bool(rational): if rational: test(func, x, y) else: test("%s_i" % func, x, x)
def chinese_remainder_theorem(): """ Solves challange 3. Three encryptions of a text were given. The encryption was vanilla RSA. Use the chinese remainder theorem (CRT) :return: Nothing, but prints the flag """ message_de = 3999545484320691620582760666106855727053549021662410570083429799334896462058097237449452993493720397790227435476345796746350169898032571754431738796344192821893497314910675156060408828511224220581582267651003911249219982138536071681121746144489861384682069580518366312319281158322907487188395349879852550922320727712516788080905540183885824808830769333571423141968760237964225240345978930859865816046424226809982967625093916471686949351836460279672029156397296634161792608413714942060302950192875262254161154196090187563688426890555569975685998994856798884592116345112968858442266655851601596662913782292282171174885 message_us = 7156090217741040585758955899433965707162947606350521948050112381514262664247963697650055668324095568121356193295269338497644168513453950802075729741157428606617001908718212348868412342224351012838448314953813036299391241983248160741119053639242636496528707303681650997650419095909359735261506378554601448197330047261478549324349224272907044375254024488417128064991560328424530705840832289740420282298553780466036967138660308477595702475699772675652723918837801775022118361119700350026576279867546392616677468749480023097012345473460622347587495191385237437474584054083447681853670339780383259673339144195425181149815 message_ru = 9343715678106945233699669787842699250821452729365496523062308278114178149719235923445953522128410659220617418971359137459068077630717894445019972202645078435758918557351185577871693207368250243507266991929090173200996910881754217374691865096976051997491208921880703490275111904577396998775470664002942232492755888378994040358902392803421017545356248082413409915177589953816030273082416979477368273328755386893089597798104163894528521114946660635364704437632205696975201216810929650384600357902888251066301913255240181601332549134854827134537709002733583099558377965114809251454424800517166814936432579406541946707525 n_de = 13368388890946686131727968139222647635627171995393331225756908262294343216259723081458150905003600322756476137516299938365001972798137046621672975975457070465770187049834603521354462199081700902700733323087201964703391196426066952717511505120664658507099276380167252844734836468387820963170177521935571096868999638202790415914397116993003197932961837711222659120426461631108658146330240545816025557486272830688061978425683447522103977339616076727857816034089500594682018085999092378789197039633371210351470521621878994691517983319668541047042031499811379908242466040735576388227260217406960791406632454767045448789863 n_us = 14463626602170229427356167809091927075048214837573339781774138582390190263460223568524802570585480435667949138330700031482283411314199309061664373861923286634420548935259474128834717819970239283387732315996647938605905994532994027238099470750924616969478147212529380894358056424265545387574975098446117146942068553320197224781384410276446833888437566192029289304444125818681142352673878184276408904704077528699342956063922184456200815444422094356292649411256904543442078043661428831462400371961179888731725665328211651272084619341652653674440701885337593085045665899694222470709757866143325441669946933338126683188131 n_ru = 18343717802716940630601940481023526351437486074120550591161058762020703345710367605696446334690825248791560509373517279950125583944976720622084902078751153032339436688975255343139180338681178127688700797071320999813387670292350135483485169318320316776584245519471849328634745109353977968597175721348420576770527793000136160877295577014905354451575371196006765377541964045640054268423795610241643005381587433138330817893094851452345761462684724873155990606241842996499888181450611803912139827073505685135888393196549213527418583778495818537291115829823762105372358484486446314835437285354604977091862400207219042791731 # Use the chinese remainder theorem to solve efficiently number = crt([n_de, n_ru, n_us], [message_de, message_ru, message_us])[0] number = number % (n_de * n_us * n_ru) number = sympy.real_root(number, 3) print(long_to_bytes(number)) """
def real_root(x): return diffify(sympy.real_root(x))
def nth_rooth(number, degree): return sympy.real_root(number, degree)
def cube_root(number): return sympy.real_root(number, 3)
def test_piecewise(): # https://github.com/sympy/sympy/issues/18363 assert limit((real_root(x - 6, 3) + 2)/(x + 2), x, -2, '+') == Rational(1, 12)
5: sp.tanh, 5: sp.coth, 2: sp.sech, 2: sp.csch, 1: sp.asinh, 1: sp.acosh, 1: sp.atanh, 1: sp.acoth, 1: sp.asech, 1: sp.acsch, 25: sp.sqrt, 25: sp.cbrt, 50: (lambda x: x ** ( random.choices([2, 3, 4, 5, 6, 7, 8, 9, 10], weights=[10, 6, 6, 6, 5, 4, 3, 3, 3], k=1)[0] )), # x**a 30: (lambda x: random.choice([-1, 1]) * random.randint(1, 10)), # random constant term 30: (lambda x: random.randint(2, 10) ** x), # random constant term 10: (lambda x: sp.real_root(x, random.randint(4, 10))) } def randomFuncNoComp(inner): return ( random.choices(list(random_functions.values()), weights=list(random_functions.keys()), k=1)[0] )(inner) def randomFunc(variable, recur=0.67): assert recur < 1 if random.uniform(0.001, 1) < recur: # Recursion chance = random.randint(1, 20) if chance <= 4: return randomFunc(variable, recur=recur) + randomFunc(variable, recur=recur) elif chance <= 10: return randomFunc(variable, recur=recur) - randomFunc(variable, recur=recur) elif chance <= 15:
xx0 = xx[0] print('xx:', xx) #diff_xx = sm.diff(xx[0], y, 1) #print('diff_xx:', diff_xx) #Gy = fx * sm.integrate(diff_xx, (y, fa, y)) Gy = fx * (xx0 - xx0.subs({y: fa})) #===== #Gy = 0.5*sm.real_root(y,3) - 0.5*sm.real_root(-1,3) #Gy = 0.5*sm.sqrt(y) #===== print('Gy:', Gy) Gy = sm.lambdify(y, Gy) return Gy fi = sm.real_root(x, 3) fi2 = x**(1 / 3) yx = sm.lambdify(x, fi) yx2 = sm.lambdify(x, fi2) n = 500 m = -1 d = 3**0.5 a, b = get_aba(m, d, yx) a2 = yx(a) fx = 1 / (b - a) X, Y = get_XY(a, b, n) XX, YY, group = func1(X, Y) plt.plot(XX, YY, label='Адамс')