def main2(): t, n = var('t, n') G = symbolic_expression(t**n * (t - Integer(1))**n / factorial(n) * e**t).function(t) T = symbolic_expression(G.integral(t, Integer(0), Integer(1))).function(n) print([T(j) for j in range(Integer(10))]) print([T(j).n() for j in range(Integer(10))])
def __init__(self, fx, fy): x, y = var('x,y') fx = EnsurephyFunction(fx) fy = EnsurephyFunction(fy) self.fx = symbolic_expression(fx.sage).function(x, y) self.fy = symbolic_expression(fy.sage).function(x, y) # g=[fx,fy] # for i in [0,1]: # if isinstance(g[i],phyFunction): # g[i]=g[i].sage # self.fx=g[0] # self.fy=g[1] self.vector_field = self
def divergence(self): """ return the divergence of the vector field. OUTPUT: a two-variable function EXAMPLES:: sage: from yanntricks.BasicGeometricObjects import * sage: x,y=var('x,y') sage: F = GeometricVectorField( x , y ) sage: F.divergence() (x, y) |--> 2 The divergence of the gravitational field vanishes:: sage: G=GeometricVectorField(x/(x**2+y**2),y/(x**2+y**2)) sage: G.divergence().simplify_full() 0 The divergence is a function:: sage: a,b=var('a,b') sage: H=GeometricVectorField( x**2,y**3 ) sage: H.divergence()(a,b) 3*b^2 + 2*a """ x, y = var('x,y') formula = self.fx.diff(x) + self.fy.diff(y) divergence = symbolic_expression(formula).function(x, y) return divergence
def phyFunction(fun, mx=None, Mx=None): """ Represent a function. INPUT: - ``fun`` - a function. - ``mx,Mx`` - initial and final value of the argument. EXAMPLES:: sage: from phystricks import * sage: f=phyFunction(cos(x)) sage: f(pi/2) 0 sage: g=phyFunction(2*f,0,pi) sage: g(pi) -2 One can deal with probability distributions : sage: C=RealDistribution('chisquared',10).distribution_function sage: f=phyFunction(C) sage: f(4) 0.0451117610789 EXAMPLES with function for which one don't know analytic form .. literalinclude:: phystricksChiSquared.py .. image:: Picture_FIGLabelFigChiSquaredPICTChiSquared-for_eps.png OTHER EXAMPLE .. literalinclude:: phystricksNonAnalyticOne.py .. image:: Picture_FIGLabelFigNonAnalyticOnePICTNonAnalyticOne-for_eps.png """ from phystricks.src.phyFunctionGraph import phyFunctionGraph # The first try is that the given expression is already a phyFunction. try: return fun.graph(mx, Mx) except (AttributeError, TypeError): pass # The second try is that `fun` is something that Sage knows. try: sy = symbolic_expression(fun) except TypeError: # Happens with probability distributions. return phyFunctionGraph(fun, mx, Mx) x = var('x') return phyFunctionGraph(sy.function(x), mx, Mx)
def grade(self, guess, data): try: from sage.all import symbolic_expression, var except: raise ImportError('could not import from sage.all') output = { 'score': 0, 'max_score': 1, 'data': { 'info': 'incorrect expression' } } for v in data['variable_names']: var(v) try: guess_expression = symbolic_expression(guess) except: output.update({ 'data': { 'info': 'could not convert input into a valid expression' } }) return output answer = symbolic_expression(data['answer_string']) if data['method'] == 'identity': if (guess_expression - answer).is_zero(): output.update({'score': 1, 'data': {}}) else: pass return output else: raise ValueError('Unknown method in ExpressionInput')
def __init__(self, convection_field, forcing_function): self._forcing_function = forcing_function self._convection_field = convection_field dx0 = sg.diff(convection_field[0], x) dx1 = sg.diff(convection_field[1], x) dy0 = sg.diff(convection_field[0], y) dy1 = sg.diff(convection_field[1], y) to_ccode = lambda u: sp.ccode( sympy.sympify(sg.symbolic_expression(u)._sympy_())) self._forcing_code = _forcing_template.format( **{'forcing_expr': to_ccode(forcing_function)}) self._convection_code = _convection_template.format(** {'value0_expr': to_ccode(convection_field[0]), 'value1_expr': to_ccode(convection_field[1]), 'dx0_expr': to_ccode(dx0), 'dx1_expr': to_ccode(dx1), 'dy0_expr': to_ccode(dy0), 'dy1_expr': to_ccode(dy1)}) self.so_folder = tf.mkdtemp(prefix="cfem_") + os.sep # even if the object is not instantiated, we can still clean it up at # exit. atexit.register(lambda folder=self.so_folder: shutil.rmtree(folder)) shutil.copy(_module_path + "makefile", self.so_folder) shutil.copy(_module_path + "cfem.h", self.so_folder) with open(self.so_folder + "funcs.c", 'w') as fhandle: fhandle.write("#include <math.h>\n") fhandle.write("#include \"cfem.h\"\n") fhandle.write(self._forcing_code) fhandle.write(self._convection_code) current_directory = os.getcwd() try: os.chdir(self.so_folder) util.run_make(command="autogenerated_functions") self.so = np.ctypeslib.load_library("libfuncs.so", "./") self.so.cf_forcing.restype = ct.c_double self.so.cf_forcing.argtypes = [ct.c_double, ct.c_double, ct.c_double] self.so.cf_convection.restype = CConvection self.so.cf_convection.argtypes = [ct.c_double, ct.c_double] finally: os.chdir(current_directory)