def newtonRaphson(exp, x, error): #PARSEO LA FUNCION f = mathematica(exp) print('f(x) ingresada = ' + str(f)) #CALCULO LA DERIVADA df = diff(f) print('f\'(x) = ' + str(df)) while (abs(f.subs('x', x)) > error): xAnt = x #IMPRIMO X y F(X) EN EL PUNTO print('f(x) = ' + str(float(f.subs('x', xAnt)))) print('x = ' + str(float(xAnt))) # EL X SIGUIENTE ES: X(n+1) = Xn - (f(Xn)/f'(Xn)) x = x - (f.subs('x', x) / df.subs('x', x)) # DEFINO LA RECTA TANGENTE rect = str(float(df.subs('x', xAnt))) + '*x+' + str( float(f.subs('x', xAnt) - df.subs('x', xAnt) * xAnt)) frect = mathematica(rect) print('Recta: ', frect) print('X(n+1) = ', float(x)) print('f(X(n+1)) = ', float(f.subs('x', x))) # if (f.subs('x', x) <= error): print('El algoritmo ha convergido. f(X(n+1))<=error') #GRAFICO DE LA FUNCION Y LA RECTA TANGENTE plot(f, frect)
def test_mathematica(): d = { '- 6x': '-6*x', 'Sin[x]^2': 'sin(x)**2', '2(x-1)': '2*(x-1)', '3y+8': '3*y+8', 'Arcsin[2x+9(4-x)^2]/x': 'asin(2*x+9*(4-x)**2)/x', 'x+y': 'x+y', '355/113': '355/113', '2.718281828': '2.718281828', 'Sin[12]': 'sin(12)', 'Exp[Log[4]]': 'exp(log(4))', '(x+1)(x+3)': '(x+1)*(x+3)', 'Cos[Arccos[3.6]]': 'cos(acos(3.6))', 'Cos[x]==Sin[y]': 'cos(x)==sin(y)', '2*Sin[x+y]': '2*sin(x+y)', 'Sin[x]+Cos[y]': 'sin(x)+cos(y)', 'Sin[Cos[x]]': 'sin(cos(x))', '2*Sqrt[x+y]': '2*sqrt(x+y)', # Test case from the issue 4259 '+Sqrt[2]': 'sqrt(2)', '-Sqrt[2]': '-sqrt(2)', '-1/Sqrt[2]': '-1/sqrt(2)', '-(1/Sqrt[3])': '-(1/sqrt(3))', '1/(2*Sqrt[5])': '1/(2*sqrt(5))', 'Mod[5,3]': 'Mod(5,3)', '-Mod[5,3]': '-Mod(5,3)', '(x+1)y': '(x+1)*y', 'x(y+1)': 'x*(y+1)', 'Sin[x]Cos[y]': 'sin(x)*cos(y)', 'Sin[x]**2Cos[y]**2': 'sin(x)**2 * cos(y)**2', 'Cos[x]^2(1 - Cos[y]^2)': 'cos(x)**2*(1-cos(y)**2)', } for e in d: assert mathematica(e) == sympify(d[e])
def parse_equation(self, equation): """ :param equation: equation from string input, to be converted :return: equation in form understand by Sympy library """ equation.replace("^", "**") return mathematica(equation)
def __init__(self, exprStringOrObj, exprStringType="sympy"): '''Initializes the object using string in a specific format or from an object''' self.properties = {} self.inRelation = {} self.outRelation = {} #jugaad for function overloading if type(exprStringOrObj) is str: flag = False try: if exprStringType == 'sympy': self.symObj = parse_expr(exprStringOrObj) #Note: Can use transformations as given in http://docs.sympy.org/dev/modules/parsing.html to improve the generality of possible expressions elif exprStringType == 'mathematica': self.symObj = mathematica(exprStringOrObj) elif exprStringType == 'maxima': self.symObj = parse_maxima(exprStringOrObj) else: flag = true except: raise Exception("Error in parsing \'"+exprStringType+"\' code: \'"+exprStringOrObj+"\'") if flag: raise Exception("Currently parsing strings of type \'"+exprStringType+"\' has not been implemented for the symPy backend. Please use \'sympy\', \'mathematica\' or \'maxima\' syntax") else: #it is a sympy object directly self.symObj = exprStringOrObj
def test_mathematica(): d = { '- 6x': '-6*x', 'Sin[x]^2': 'sin(x)**2', '2(x-1)': '2*(x-1)', '3y+8': '3*y+8', 'Arcsin[2x+9(4-x)^2]/x': 'asin(2*x+9*(4-x)**2)/x', 'x+y': 'x+y', '355/113': '355/113', '2.718281828': '2.718281828', 'Sin[12]': 'sin(12)', 'Exp[Log[4]]': 'exp(log(4))', '(x+1)(x+3)': '(x+1)*(x+3)', 'Cos[Arccos[3.6]]': 'cos(acos(3.6))', 'Cos[x]==Sin[y]': 'cos(x)==sin(y)', '2*Sin[x+y]': '2*sin(x+y)', 'Sin[x]+Cos[y]': 'sin(x)+cos(y)', 'Sin[Cos[x]]': 'sin(cos(x))', '2*Sqrt[x+y]': '2*sqrt(x+y)', # Test case from the issue 4259 'x y': 'x*y', 'x Sin[x]': 'x*sin(x)', 'x Sin[1/x]': 'x*sin(1/x)', 'Sin[1/x] x': 'x*sin(1/x)', 'x*Sin[1/x]': 'x*sin(1/x)', # Test case from the issue 8501 'Sin[1/x]*x': 'x*sin(1/x)'} for e in d: assert mathematica(e) == sympify(d[e])
def secante(exp, xAnt, x, error): #PARSEO LA FUNCION f = mathematica(exp) print('f(x) ingresada = ' + str(f)) print('X1 = ' + str(xAnt) + ' X2 = ' + str(x)) while (abs(f.subs('x', x)) > error): xAux = xAnt xAnt = x #IMPRIMO X y F(X) EN EL PUNTO print('f(x) = ' + str(float(f.subs('x', xAnt)))) print('x = ' + str(float(xAnt))) # DEFINO LA RECTA TANGENTE m = float((f.subs('x', x) - f.subs('x', xAux)) / (x - xAux)) print('m', m) b = float(f.subs('x', xAux) - (m * xAux)) print('b', b) rect = '(' + str(m) + ') * x+ (' + str(b) + ')' # FORMATO STRING print(rect) frect = mathematica(rect) #PARSEO EL STRING # CALCULO EL X SIGUIENTE: # X(n+1) = Xn - (Xn - X(n-1)) / (f(Xn) - f(X(n-1)))*f(Xn) x = x - ((x - xAux) / (f.subs('x', x) - f.subs('x', xAux)) * f.subs('x', x)) print('Recta: ', frect) print('X(n+1) = ', float(x)) print('f(X(n+1)) = ', float(f.subs('x', x))) if (abs(f.subs('x', x)) <= error): print('El algoritmo ha convergido. f(X(n+1))<=error') #GRAFICO DE LA FUNCION Y LA RECTA TANGENTE plot(f, frect)
def test_mathematica(): d = { '- 6x': '-6*x', 'Sin[x]^2': 'sin(x)**2', '2(x-1)': '2*(x-1)', '3y+8': '3*y+8', 'ArcSin[2x+9(4-x)^2]/x': 'asin(2*x+9*(4-x)**2)/x', 'x+y': 'x+y', '355/113': '355/113', '2.718281828': '2.718281828', 'Sin[12]': 'sin(12)', 'Exp[Log[4]]': 'exp(log(4))', '(x+1)(x+3)': '(x+1)*(x+3)', 'Cos[ArcCos[3.6]]': 'cos(acos(3.6))', 'Cos[x]==Sin[y]': 'cos(x)==sin(y)', '2*Sin[x+y]': '2*sin(x+y)', 'Sin[x]+Cos[y]': 'sin(x)+cos(y)', 'Sin[Cos[x]]': 'sin(cos(x))', '2*Sqrt[x+y]': '2*sqrt(x+y)', # Test case from the issue 4259 '+Sqrt[2]': 'sqrt(2)', '-Sqrt[2]': '-sqrt(2)', '-1/Sqrt[2]': '-1/sqrt(2)', '-(1/Sqrt[3])': '-(1/sqrt(3))', '1/(2*Sqrt[5])': '1/(2*sqrt(5))', 'Mod[5,3]': 'Mod(5,3)', '-Mod[5,3]': '-Mod(5,3)', '(x+1)y': '(x+1)*y', 'x(y+1)': 'x*(y+1)', 'Sin[x]Cos[y]': 'sin(x)*cos(y)', 'Sin[x]**2Cos[y]**2': 'sin(x)**2*cos(y)**2', 'Cos[x]^2(1 - Cos[y]^2)': 'cos(x)**2*(1-cos(y)**2)', 'x y': 'x*y', '2 x': '2*x', 'x 8': 'x*8', '2 8': '2*8', '1 2 3': '1*2*3', ' - 2 * Sqrt[ 2 3 * ( 1 + 5 ) ] ': '-2*sqrt(2*3*(1+5))', 'Log[2,4]': 'log(4,2)', 'Log[Log[2,4],4]': 'log(4,log(4,2))', 'Exp[Sqrt[2]^2Log[2, 8]]': 'exp(sqrt(2)**2*log(8,2))', 'ArcSin[Cos[0]]': 'asin(cos(0))', 'Log2[16]': 'log(16,2)', 'Max[1,-2,3,-4]': 'Max(1,-2,3,-4)', 'Min[1,-2,3]': 'Min(1,-2,3)', 'Exp[I Pi/2]': 'exp(I*pi/2)', 'ArcTan[x,y]': 'atan2(y,x)', 'Pochhammer[x,y]': 'rf(x,y)' } for e in d: assert mathematica(e) == sympify(d[e])
def readout(flag, tempdir, returnhash): if (flag == 't') or (flag == 'n'): with open(os.path.join(tempdir, returnhash), 'r') as f: ret = f.read() return ret elif flag == 'p': with open(os.path.join(tempdir, returnhash), 'r') as f: ret = f.read() return M.mathematica(ret) elif flag == 'g': ret = Image(os.path.join(tempdir, returnhash), format='png') return ret else: raise Exception('Flag must be one of (\'t\', \'n\', \'p\', \'g\').')
def test_mathematica(): d = { "- 6x": "-6*x", "Sin[x]^2": "sin(x)**2", "2(x-1)": "2*(x-1)", "3y+8": "3*y+8", "ArcSin[2x+9(4-x)^2]/x": "asin(2*x+9*(4-x)**2)/x", "x+y": "x+y", "355/113": "355/113", "2.718281828": "2.718281828", "Sin[12]": "sin(12)", "Exp[Log[4]]": "exp(log(4))", "(x+1)(x+3)": "(x+1)*(x+3)", "Cos[ArcCos[3.6]]": "cos(acos(3.6))", "Cos[x]==Sin[y]": "cos(x)==sin(y)", "2*Sin[x+y]": "2*sin(x+y)", "Sin[x]+Cos[y]": "sin(x)+cos(y)", "Sin[Cos[x]]": "sin(cos(x))", "2*Sqrt[x+y]": "2*sqrt(x+y)", # Test case from the issue 4259 "+Sqrt[2]": "sqrt(2)", "-Sqrt[2]": "-sqrt(2)", "-1/Sqrt[2]": "-1/sqrt(2)", "-(1/Sqrt[3])": "-(1/sqrt(3))", "1/(2*Sqrt[5])": "1/(2*sqrt(5))", "Mod[5,3]": "Mod(5,3)", "-Mod[5,3]": "-Mod(5,3)", "(x+1)y": "(x+1)*y", "x(y+1)": "x*(y+1)", "Sin[x]Cos[y]": "sin(x)*cos(y)", "Sin[x]**2Cos[y]**2": "sin(x)**2*cos(y)**2", "Cos[x]^2(1 - Cos[y]^2)": "cos(x)**2*(1-cos(y)**2)", "x y": "x*y", "2 x": "2*x", "x 8": "x*8", "2 8": "2*8", "1 2 3": "1*2*3", " - 2 * Sqrt[ 2 3 * ( 1 + 5 ) ] ": "-2*sqrt(2*3*(1+5))", "Log[2,4]": "log(4,2)", "Log[Log[2,4],4]": "log(4,log(4,2))", "Exp[Sqrt[2]^2Log[2, 8]]": "exp(sqrt(2)**2*log(8,2))", "ArcSin[Cos[0]]": "asin(cos(0))", "Log2[16]": "log(16,2)", "Max[1,-2,3,-4]": "Max(1,-2,3,-4)", "Min[1,-2,3]": "Min(1,-2,3)", "Exp[I Pi/2]": "exp(I*pi/2)", } for e in d: assert mathematica(e) == sympify(d[e])
def test_mathematica(): d = { '- 6x': '-6*x', 'Sin[x]^2': 'sin(x)**2', '2(x-1)': '2*(x-1)', '3y+8': '3*y+8', 'ArcSin[2x+9(4-x)^2]/x': 'asin(2*x+9*(4-x)**2)/x', 'x+y': 'x+y', '355/113': '355/113', '2.718281828': '2.718281828', 'Sin[12]': 'sin(12)', 'Exp[Log[4]]': 'exp(log(4))', '(x+1)(x+3)': '(x+1)*(x+3)', 'Cos[ArcCos[3.6]]': 'cos(acos(3.6))', 'Cos[x]==Sin[y]': 'cos(x)==sin(y)', '2*Sin[x+y]': '2*sin(x+y)', 'Sin[x]+Cos[y]': 'sin(x)+cos(y)', 'Sin[Cos[x]]': 'sin(cos(x))', '2*Sqrt[x+y]': '2*sqrt(x+y)', # Test case from the issue 4259 '+Sqrt[2]': 'sqrt(2)', '-Sqrt[2]': '-sqrt(2)', '-1/Sqrt[2]': '-1/sqrt(2)', '-(1/Sqrt[3])': '-(1/sqrt(3))', '1/(2*Sqrt[5])': '1/(2*sqrt(5))', 'Mod[5,3]': 'Mod(5,3)', '-Mod[5,3]': '-Mod(5,3)', '(x+1)y': '(x+1)*y', 'x(y+1)': 'x*(y+1)', 'Sin[x]Cos[y]': 'sin(x)*cos(y)', 'Sin[x]**2Cos[y]**2': 'sin(x)**2*cos(y)**2', 'Cos[x]^2(1 - Cos[y]^2)': 'cos(x)**2*(1-cos(y)**2)', 'x y': 'x*y', '2 x': '2*x', 'x 8': 'x*8', '2 8': '2*8', '1 2 3': '1*2*3', ' - 2 * Sqrt[ 2 3 * ( 1 + 5 ) ] ': '-2*sqrt(2*3*(1+5))', 'Log[2,4]': 'log(4,2)', 'Log[Log[2,4],4]': 'log(4,log(4,2))', 'Exp[Sqrt[2]^2Log[2, 8]]': 'exp(sqrt(2)**2*log(8,2))', 'ArcSin[Cos[0]]': 'asin(cos(0))', 'Log2[16]': 'log(16,2)', 'Max[1,-2,3,-4]': 'Max(1,-2,3,-4)', 'Min[1,-2,3]': 'Min(1,-2,3)', 'Exp[I Pi/2]': 'exp(I*pi/2)', } for e in d: assert mathematica(e) == sympify(d[e])
def test_mathematica(): d = {'Sin[x]^2': 'sin(x)**2', '2(x-1)': '2*(x-1)', '3y+8': '3*y+8', 'Arcsin[2x+9(4-x)^2]/x': 'asin(2*x+9*(4-x)**2)/x', 'x+y': 'x+y', '355/113': '355/113', '2.718281828': '2.718281828', 'Sin[12]': 'sin(12)', 'Exp[Log[4]]': 'exp(log(4))', '(x+1)(x+3)': '(x+1)*(x+3)', 'Cos[Arccos[3.6]]': 'cos(acos(3.6))', 'Cos[x]==Sin[y]': 'cos(x)==sin(y)'} for e in d: assert mathematica(e) == sympify(d[e])
def test_mathematica(): d = { '- 6x': '-6*x', 'Sin[x]^2': 'sin(x)**2', '2(x-1)': '2*(x-1)', '3y+8': '3*y+8', 'Arcsin[2x+9(4-x)^2]/x': 'asin(2*x+9*(4-x)**2)/x', 'x+y': 'x+y', '355/113': '355/113', '2.718281828': '2.718281828', 'Sin[12]': 'sin(12)', 'Exp[Log[4]]': 'exp(log(4))', '(x+1)(x+3)': '(x+1)*(x+3)', 'Cos[Arccos[3.6]]': 'cos(acos(3.6))', 'Cos[x]==Sin[y]': 'cos(x)==sin(y)', '2*Sin[x+y]': '2*sin(x+y)', 'Sin[x]+Cos[y]': 'sin(x)+cos(y)', 'Sin[Cos[x]]': 'sin(cos(x))', '2*Sqrt[x+y]': '2*sqrt(x+y)'} # Test case from the issue 4259 for e in d: assert mathematica(e) == sympify(d[e])
sqrtand = (proall * (9 * a * sumall + sqrt(81 * a**2 * sumall**2 + 48 * proall * (a - 1)**3)))**(1 / 3) D = (-2 * 6**(2 / 3) * proall * (a - 1) + 6**(1 / 3) * sqrtand**2) / (3 * sqrtand) return D.real express2 = """ (-2 6^(2/3) (-1 + a) proall + 6^(1/3) (proall (9 a sumall + Sqrt[ 48 (-1 + a)^3 proall + 81 a^2 sumall^2]))^( 2/3))/(3 (proall (9 a sumall + Sqrt[ 48 (-1 + a)^3 proall + 81 a^2 sumall^2]))^(1/3)) """ expr2 = M.mathematica(express2) print(expr2) express3 = """ (Sqrt[-8 a proall sumall + 2^(1/3) (27 (-1 + a)^2 proall^2 + Sqrt[ proall^3 (729 (-1 + a)^4 proall + 256 a^3 sumall^3)])^( 2/3)] - \[Sqrt](8 a proall sumall - 2^(1/3) (27 (-1 + a)^2 proall^2 + Sqrt[ proall^3 (729 (-1 + a)^4 proall + 256 a^3 sumall^3)])^( 2/3) - (12 Sqrt[3] (-1 + a) proall)/ Sqrt[(-8 a proall sumall + 2^(1/3) (27 (-1 + a)^2 proall^2 + Sqrt[ proall^3 (729 (-1 + a)^4 proall + 256 a^3 sumall^3)])^( 2/3))/(27 (-1 + a)^2 proall^2 + Sqrt[ proall^3 (729 (-1 + a)^4 proall +
import sympy import tensorflow as tf import sympy.parsing.mathematica as symath math_expr = '(t^3+10t^2*a*Sin[x]+b*32t+32)/(t^2+2t-15)' parsed_expr = symath.mathematica(s=math_expr) print("parsed sympy expression", parsed_expr) tf_expr = sympy.lambdify(parsed_expr.free_symbols, parsed_expr, 'tensorflow') data = tf.linspace(0., 10., num=10) print(tf_expr) a = tf.Variable(15.) b = tf.Variable(13.) tensor = tf_expr(a=a, b=b, x=data, t=data) print(tensor)
def test_mathematica(): d = { '- 6x': '-6*x', 'Sin[x]^2': 'sin(x)**2', '2(x-1)': '2*(x-1)', '3y+8': '3*y+8', 'ArcSin[2x+9(4-x)^2]/x': 'asin(2*x+9*(4-x)**2)/x', 'x+y': 'x+y', '355/113': '355/113', '2.718281828': '2.718281828', 'Sin[12]': 'sin(12)', 'Exp[Log[4]]': 'exp(log(4))', '(x+1)(x+3)': '(x+1)*(x+3)', 'Cos[ArcCos[3.6]]': 'cos(acos(3.6))', 'Cos[x]==Sin[y]': 'Eq(cos(x), sin(y))', '2*Sin[x+y]': '2*sin(x+y)', 'Sin[x]+Cos[y]': 'sin(x)+cos(y)', 'Sin[Cos[x]]': 'sin(cos(x))', '2*Sqrt[x+y]': '2*sqrt(x+y)', # Test case from the issue 4259 '+Sqrt[2]': 'sqrt(2)', '-Sqrt[2]': '-sqrt(2)', '-1/Sqrt[2]': '-1/sqrt(2)', '-(1/Sqrt[3])': '-(1/sqrt(3))', '1/(2*Sqrt[5])': '1/(2*sqrt(5))', 'Mod[5,3]': 'Mod(5,3)', '-Mod[5,3]': '-Mod(5,3)', '(x+1)y': '(x+1)*y', 'x(y+1)': 'x*(y+1)', 'Sin[x]Cos[y]': 'sin(x)*cos(y)', 'Sin[x]^2Cos[y]^2': 'sin(x)**2*cos(y)**2', 'Cos[x]^2(1 - Cos[y]^2)': 'cos(x)**2*(1-cos(y)**2)', 'x y': 'x*y', 'x y': 'x*y', '2 x': '2*x', 'x 8': 'x*8', '2 8': '2*8', '4.x': '4.*x', '4. 3': '4.*3', '4. 3.': '4.*3.', '1 2 3': '1*2*3', ' - 2 * Sqrt[ 2 3 * ( 1 + 5 ) ] ': '-2*sqrt(2*3*(1+5))', 'Log[2,4]': 'log(4,2)', 'Log[Log[2,4],4]': 'log(4,log(4,2))', 'Exp[Sqrt[2]^2Log[2, 8]]': 'exp(sqrt(2)**2*log(8,2))', 'ArcSin[Cos[0]]': 'asin(cos(0))', 'Log2[16]': 'log(16,2)', 'Max[1,-2,3,-4]': 'Max(1,-2,3,-4)', 'Min[1,-2,3]': 'Min(1,-2,3)', 'Exp[I Pi/2]': 'exp(I*pi/2)', 'ArcTan[x,y]': 'atan2(y,x)', 'Pochhammer[x,y]': 'rf(x,y)', 'ExpIntegralEi[x]': 'Ei(x)', 'SinIntegral[x]': 'Si(x)', 'CosIntegral[x]': 'Ci(x)', 'AiryAi[x]': 'airyai(x)', 'AiryAiPrime[5]': 'airyaiprime(5)', 'AiryBi[x]': 'airybi(x)', 'AiryBiPrime[7]': 'airybiprime(7)', 'LogIntegral[4]': ' li(4)', 'PrimePi[7]': 'primepi(7)', 'Prime[5]': 'prime(5)', 'PrimeQ[5]': 'isprime(5)' } for e in d: assert mathematica(e) == sympify(d[e]) # The parsed form of this expression should not evaluate the Lambda object: assert mathematica("Sin[#]^2 + Cos[#]^2 &[x]") == sin(x)**2 + cos(x)**2 d1, d2, d3 = symbols("d1:4", cls=Dummy) assert mathematica("Sin[#] + Cos[#3] &").dummy_eq( Lambda((d1, d2, d3), sin(d1) + cos(d3))) assert mathematica("Sin[#^2] &").dummy_eq(Lambda(d1, sin(d1**2))) assert mathematica("Function[x, x^3]") == Lambda(x, x**3) assert mathematica("Function[{x, y}, x^2 + y^2]") == Lambda((x, y), x**2 + y**2)
from sympy.parsing.mathematica import mathematica from sympy import * from mpmath import * mp.dps = 32 mp.pretty = True nu1 = var('nu1') nu2 = var('nu2') with open("matter.txt", "r") as file: expr = file.read() mathexpr = mathematica(expr) M12temp = lambdify([nu1, nu2], mathexpr, "mpmath") def J(nu1, nu2): return (gamma(1.5 - nu1) * gamma(1.5 - nu2) * gamma(nu1 + nu2 - 1.5) / (gamma(nu1) * gamma(nu2) * gamma(3. - nu2 - nu1))) / (8. * pi**(1.5)) def M12(nu1, nu2): return J(nu1, nu2) * M12temp(nu1, nu2) b = -0.8
def convert(s): s = s.replace(b'(', b'').replace(b')', b'') s = s.replace(b'^', b'10^') s = s.replace(b'e', b'*10^') return complex(mathematica(s.decode('utf-8')))
from sympy.parsing.mathematica import mathematica from clipboard import copy, paste additional_translations = { 'Pochhammer[x,y]': 'rf(x,y)', 'ArcTan[x,y]': 'atan2(y,x)' } copy(str(mathematica(paste(), additional_translations=additional_translations))) atan2(y, x) """ Sin[x] ArcTan[x,y] """