本文整理汇总了Python中sage.symbolic.expression.is_Expression函数的典型用法代码示例。如果您正苦于以下问题:Python is_Expression函数的具体用法?Python is_Expression怎么用?Python is_Expression使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了is_Expression函数的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: equify
def equify(f):
"""
Returns the equation rewritten as a symbolic function to give
negative values when True, positive when False.
EXAMPLES::
sage: from sage.plot.contour_plot import equify
sage: var('x, y')
(x, y)
sage: equify(x^2 < 2)
x^2 - 2
sage: equify(x^2 > 2)
-x^2 + 2
sage: equify(x*y > 1)
-x*y + 1
sage: equify(y > 0)
-y
sage: f=equify(lambda x,y: x>y)
sage: f(1,2)
1
sage: f(2,1)
-1
"""
import operator
from sage.calculus.all import symbolic_expression
from sage.symbolic.expression import is_Expression
if not is_Expression(f):
return lambda x,y: -1 if f(x,y) else 1
op = f.operator()
if op is operator.gt or op is operator.ge:
return symbolic_expression(f.rhs() - f.lhs())
else:
return symbolic_expression(f.lhs() - f.rhs())
示例2: set_output
def set_output(self, value):
self._has_output = True
self._type = str(type(value))
from sage.plot.graphics import is_Graphics
from sage.symbolic.expression import is_Expression
if is_Graphics(value):
self._text = str(value)
self._png = 'graphics_'+str(self.index())+'.png'
self._mime = CommandLog.OUTPUT_PNG
value.save(self._png, transparent=True)
elif is_Expression(value):
self._text = str(value)
self._latex = str(value._latex_())
self._mime = CommandLog.OUTPUT_LATEX
else:
self._text = value.__repr__()
self._mime = CommandLog.OUTPUT_TEXT
示例3: _eval_
def _eval_(self, x):
"""
EXAMPLES::
sage: cot(pi/4)
1
sage: cot(x).subs(x==pi/4)
1
sage: cot(pi/7)
cot(1/7*pi)
sage: cot(x)
cot(x)
"""
tan_x = tan(x)
if is_Expression(tan_x) and tan_x.operator() is tan:
return None
else:
return 1/tan_x
示例4: is_CallableSymbolicExpression
def is_CallableSymbolicExpression(x):
r"""
Returns ``True`` if ``x`` is a callable symbolic
expression.
EXAMPLES::
sage: from sage.symbolic.callable import is_CallableSymbolicExpression
sage: var('a x y z')
(a, x, y, z)
sage: f(x,y) = a + 2*x + 3*y + z
sage: is_CallableSymbolicExpression(f)
True
sage: is_CallableSymbolicExpression(a+2*x)
False
sage: def foo(n): return n^2
...
sage: is_CallableSymbolicExpression(foo)
False
"""
from sage.symbolic.expression import is_Expression
return is_Expression(x) and isinstance(x.parent(), CallableSymbolicExpressionRing_class)
示例5: implicit_plot
#.........这里部分代码省略.........
The same circle but with a different line width::
sage: implicit_plot(f, (-3,3), (-3,3), linewidth=6)
And again the same circle but this time with a dashdot border::
sage: implicit_plot(f, (-3,3), (-3,3), linestyle='dashdot')
You can also plot an equation::
sage: var("x y")
(x, y)
sage: implicit_plot(x^2+y^2 == 2, (x,-3,3), (y,-3,3))
You can even change the color of the plot::
sage: implicit_plot(x^2+y^2 == 2, (x,-3,3), (y,-3,3), color="red")
Here is a beautiful (and long) example which also tests that all
colors work with this::
sage: G = Graphics()
sage: counter = 0
sage: for col in colors.keys(): # long time
... G += implicit_plot(x^2+y^2==1+counter*.1, (x,-4,4),(y,-4,4),color=col)
... counter += 1
sage: G.show(frame=False)
We can define a level-`n` approximation of the boundary of the
Mandelbrot set::
sage: def mandel(n):
... c = polygen(CDF, 'c')
... z = 0
... for i in range(n):
... z = z*z + c
... def f(x, y):
... val = z(CDF(x, y))
... return val.norm() - 4
... return f
The first-level approximation is just a circle::
sage: implicit_plot(mandel(1), (-3, 3), (-3, 3))
A third-level approximation starts to get interesting::
sage: implicit_plot(mandel(3), (-2, 1), (-1.5, 1.5))
The seventh-level approximation is a degree 64 polynomial, and
``implicit_plot`` does a pretty good job on this part of the curve.
(``plot_points=200`` looks even better, but it takes over a second.)
::
sage: implicit_plot(mandel(7), (-0.3, 0.05), (-1.15, -0.9),plot_points=50)
When making a filled implicit plot using a python function rather than a
symbolic expression the user should increase the number of plot points to
avoid artifacts::
sage: implicit_plot(lambda x,y: x^2+y^2-2, (x,-3,3), (y,-3,3), fill=True, plot_points=500) # long time
TESTS::
sage: f(x,y) = x^2 + y^2 - 2
sage: implicit_plot(f, (-3, 3), (-3, 3),fill=5)
Traceback (most recent call last):
...
ValueError: fill=5 is not supported
"""
from sage.symbolic.expression import is_SymbolicEquation
if is_SymbolicEquation(f):
if f.operator() != operator.eq:
raise ValueError, "input to implicit plot must be function or equation"
f = f.lhs() - f.rhs()
linewidths = options.pop('linewidth', None)
linestyles = options.pop('linestyle', None)
if 'color' in options:
options['cmap']=[options.pop('color', None)]
if options['fill'] is True:
options.pop('fill')
options.pop('contours',None)
options.pop('cmap',None)
from sage.symbolic.expression import is_Expression
if not is_Expression(f):
return region_plot(lambda x,y: f(x,y)<0, xrange, yrange,
borderwidth=linewidths, borderstyle=linestyles,
**options)
else:
return region_plot(f<0, xrange, yrange, borderwidth=linewidths,
borderstyle=linestyles, **options)
elif options['fill'] is False:
return contour_plot(f, xrange, yrange, linewidths=linewidths,
linestyles=linestyles, **options)
else:
raise ValueError("fill=%s is not supported" % options['fill'])
示例6: solve_mod
#.........这里部分代码省略.........
factors are small, this can be efficient even if the modulus itself
is large::
sage: sorted(solve_mod([x^2 == 41], 10^20))
[(4538602480526452429,), (11445932736758703821,), (38554067263241296179,),
(45461397519473547571,), (54538602480526452429,), (61445932736758703821,),
(88554067263241296179,), (95461397519473547571,)]
We solve a simple equation modulo 2::
sage: x,y = var('x,y')
sage: solve_mod([x == y], 2)
[(0, 0), (1, 1)]
.. warning::
The current implementation splits the modulus into prime
powers, then naively enumerates all possible solutions
(starting modulo primes and then working up through prime
powers), and finally combines the solution using the Chinese
Remainder Theorem. The interface is good, but the algorithm is
very inefficient if the modulus has some larger prime factors! Sage
*does* have the ability to do something much faster in certain
cases at least by using Groebner basis, linear algebra
techniques, etc. But for a lot of toy problems this function as
is might be useful. At least it establishes an interface.
TESTS:
Make sure that we short-circuit in at least some cases::
sage: solve_mod([2*x==1], 2*next_prime(10^50))
[]
Try multi-equation cases::
sage: x, y, z = var("x y z")
sage: solve_mod([2*x^2 + x*y, -x*y+2*y^2+x-2*y, -2*x^2+2*x*y-y^2-x-y], 12)
[(0, 0), (4, 4), (0, 3), (4, 7)]
sage: eqs = [-y^2+z^2, -x^2+y^2-3*z^2-z-1, -y*z-z^2-x-y+2, -x^2-12*z^2-y+z]
sage: solve_mod(eqs, 11)
[(8, 5, 6)]
Confirm that modulus 1 now behaves as it should::
sage: x, y = var("x y")
sage: solve_mod([x==1], 1)
[(0,)]
sage: solve_mod([2*x^2+x*y, -x*y+2*y^2+x-2*y, -2*x^2+2*x*y-y^2-x-y], 1)
[(0, 0)]
"""
from sage.rings.all import Integer, Integers, crt_basis
from sage.symbolic.expression import is_Expression
from sage.misc.all import cartesian_product_iterator
from sage.modules.all import vector
from sage.matrix.all import matrix
if not isinstance(eqns, (list, tuple)):
eqns = [eqns]
eqns = [eq if is_Expression(eq) else (eq.lhs()-eq.rhs()) for eq in eqns]
modulus = Integer(modulus)
if modulus < 1:
raise ValueError("the modulus must be a positive integer")
vars = list(set(sum([list(e.variables()) for e in eqns], [])))
vars.sort(key=repr)
if modulus == 1: # degenerate case
ans = [tuple(Integers(1)(0) for v in vars)]
return ans
factors = modulus.factor()
crt_basis = vector(Integers(modulus), crt_basis([p**i for p,i in factors]))
solutions = []
has_solution = True
for p,i in factors:
solution =_solve_mod_prime_power(eqns, p, i, vars)
if len(solution) > 0:
solutions.append(solution)
else:
has_solution = False
break
ans = []
if has_solution:
for solution in cartesian_product_iterator(solutions):
solution_mat = matrix(Integers(modulus), solution)
ans.append(tuple(c.dot_product(crt_basis) for c in solution_mat.columns()))
# if solution_dict == True:
# Relaxed form suggested by Mike Hansen (#8553):
if solution_dict:
sol_dict = [dict(zip(vars, solution)) for solution in ans]
return sol_dict
else:
return ans
示例7: solve
#.........这里部分代码省略.........
[]
sage: solve((x==1,x==-1),x,solution_dict=1)
[]
This inequality holds for any real ``x`` (:trac:`8078`)::
sage: solve(x^4+2>0,x)
[x < +Infinity]
Test for user friendly input handling :trac:`13645`::
sage: poly.<a,b> = PolynomialRing(RR)
sage: solve([a+b+a*b == 1], a)
Traceback (most recent call last):
...
TypeError: The first argument to solve() should be a symbolic expression or a list of symbolic expressions, cannot handle <type 'bool'>
sage: solve([a, b], (1, a))
Traceback (most recent call last):
...
TypeError: 1 is not a valid variable.
sage: solve([x == 1], (1, a))
Traceback (most recent call last):
...
TypeError: (1, a) are not valid variables.
Test that the original version of a system in the French Sage book
now works (:trac:`14306`)::
sage: var('y,z')
(y, z)
sage: solve([x^2 * y * z == 18, x * y^3 * z == 24, x * y * z^4 == 6], x, y, z)
[[x == 3, y == 2, z == 1], [x == (1.337215067... - 2.685489874...*I), y == (-1.700434271... + 1.052864325...*I), z == (0.9324722294... - 0.3612416661...*I)], ...]
"""
from sage.symbolic.expression import is_Expression
if is_Expression(f): # f is a single expression
ans = f.solve(*args,**kwds)
return ans
if not isinstance(f, (list, tuple)):
raise TypeError("The first argument must be a symbolic expression or a list of symbolic expressions.")
if len(f)==1:
# f is a list with a single element
if is_Expression(f[0]):
# if its a symbolic expression call solve method of this expression
return f[0].solve(*args,**kwds)
# otherwise complain
raise TypeError("The first argument to solve() should be a symbolic "
"expression or a list of symbolic expressions, "
"cannot handle %s"%repr(type(f[0])))
# f is a list of such expressions or equations
from sage.symbolic.ring import is_SymbolicVariable
if len(args)==0:
raise TypeError("Please input variables to solve for.")
if is_SymbolicVariable(args[0]):
variables = args
else:
variables = tuple(args[0])
for v in variables:
if not is_SymbolicVariable(v):
raise TypeError("%s is not a valid variable."%repr(v))
try:
示例8: to_cartesian
def to_cartesian(self, func, params=None):
"""
Returns a 3-tuple of functions, parameterized over ``params``, that
represents the Cartesian coordinates of the value of ``func``.
INPUT:
- ``func`` - A function in this coordinate space. Corresponds to the
independent variable.
- ``params`` - The parameters of func. Corresponds to the dependent
variables.
EXAMPLE::
sage: from sage.plot.plot3d.plot3d import _ArbitraryCoordinates
sage: x, y, z = var('x y z')
sage: T = _ArbitraryCoordinates((x + y, x - y, z), z,[x,y])
sage: f(x, y) = 2*x+y
sage: T.to_cartesian(f, [x, y])
(x + y, x - y, 2*x + y)
sage: [h(1,2) for h in T.to_cartesian(lambda x,y: 2*x+y)]
[3.0, -1.0, 4.0]
We try to return a function having the same variable names as
the function passed in::
sage: from sage.plot.plot3d.plot3d import _ArbitraryCoordinates
sage: x, y, z = var('x y z')
sage: T = _ArbitraryCoordinates((x + y, x - y, z), z,[x,y])
sage: f(a, b) = 2*a+b
sage: T.to_cartesian(f, [a, b])
(a + b, a - b, 2*a + b)
sage: t1,t2,t3=T.to_cartesian(lambda a,b: 2*a+b)
sage: import inspect
sage: inspect.getargspec(t1)
ArgSpec(args=['a', 'b'], varargs=None, keywords=None, defaults=None)
sage: inspect.getargspec(t2)
ArgSpec(args=['a', 'b'], varargs=None, keywords=None, defaults=None)
sage: inspect.getargspec(t3)
ArgSpec(args=['a', 'b'], varargs=None, keywords=None, defaults=None)
sage: def g(a,b): return 2*a+b
sage: t1,t2,t3=T.to_cartesian(g)
sage: inspect.getargspec(t1)
ArgSpec(args=['a', 'b'], varargs=None, keywords=None, defaults=None)
sage: t1,t2,t3=T.to_cartesian(2*a+b)
sage: inspect.getargspec(t1)
ArgSpec(args=['a', 'b'], varargs=None, keywords=None, defaults=None)
If we cannot guess the right parameter names, then the
parameters are named `u` and `v`::
sage: from sage.plot.plot3d.plot3d import _ArbitraryCoordinates
sage: x, y, z = var('x y z')
sage: T = _ArbitraryCoordinates((x + y, x - y, z), z,[x,y])
sage: t1,t2,t3=T.to_cartesian(operator.add)
sage: inspect.getargspec(t1)
ArgSpec(args=['u', 'v'], varargs=None, keywords=None, defaults=None)
sage: [h(1,2) for h in T.to_cartesian(operator.mul)]
[3.0, -1.0, 2.0]
sage: [h(u=1,v=2) for h in T.to_cartesian(operator.mul)]
[3.0, -1.0, 2.0]
The output of the function `func` is coerced to a float when
it is evaluated if the function is something like a lambda or
python callable. This takes care of situations like f returning a
singleton numpy array, for example.
sage: from numpy import array
sage: v_phi=array([ 0., 1.57079637, 3.14159274, 4.71238911, 6.28318548])
sage: v_theta=array([ 0., 0.78539819, 1.57079637, 2.35619456, 3.14159274])
sage: m_r=array([[ 0.16763356, 0.25683223, 0.16649297, 0.10594339, 0.55282422],
... [ 0.16763356, 0.19993708, 0.31403568, 0.47359696, 0.55282422],
... [ 0.16763356, 0.25683223, 0.16649297, 0.10594339, 0.55282422],
... [ 0.16763356, 0.19993708, 0.31403568, 0.47359696, 0.55282422],
... [ 0.16763356, 0.25683223, 0.16649297, 0.10594339, 0.55282422]])
sage: import scipy.interpolate
sage: f=scipy.interpolate.RectBivariateSpline(v_phi,v_theta,m_r)
sage: spherical_plot3d(f,(0,2*pi),(0,pi))
Graphics3d Object
"""
from sage.symbolic.expression import is_Expression
from sage.rings.real_mpfr import is_RealNumber
from sage.rings.integer import is_Integer
if params is not None and (is_Expression(func) or is_RealNumber(func) or is_Integer(func)):
return self.transform(**{
self.dep_var: func,
self.indep_vars[0]: params[0],
self.indep_vars[1]: params[1]
})
else:
# func might be a lambda or a Python callable; this makes it slightly
# more complex.
import sage.symbolic.ring
dep_var_dummy = sage.symbolic.ring.var(self.dep_var)
indep_var_dummies = sage.symbolic.ring.var(','.join(self.indep_vars))
transformation = self.transform(**{
self.dep_var: dep_var_dummy,
self.indep_vars[0]: indep_var_dummies[0],
#.........这里部分代码省略.........
示例9: region_plot
#.........这里部分代码省略.........
Graphics object consisting of 1 graphics primitive
::
sage: region_plot(s>0,(s,-2,2),(t,-2,2))
Graphics object consisting of 1 graphics primitive
An example of a region plot in 'loglog' scale::
sage: region_plot(x^2+y^2<100, (x,1,10), (y,1,10), scale='loglog')
Graphics object consisting of 1 graphics primitive
TESTS:
To check that :trac:`16907` is fixed::
sage: x, y = var('x, y')
sage: disc1 = region_plot(x^2+y^2 < 1, (x, -1, 1), (y, -1, 1), alpha=0.5)
sage: disc2 = region_plot((x-0.7)^2+(y-0.7)^2 < 0.5, (x, -2, 2), (y, -2, 2), incol='red', alpha=0.5)
sage: disc1 + disc2
Graphics object consisting of 2 graphics primitives
To check that :trac:`18286` is fixed::
sage: x, y = var('x, y')
sage: region_plot([x == 0], (x, -1, 1), (y, -1, 1))
Graphics object consisting of 1 graphics primitive
sage: region_plot([x^2+y^2==1, x<y], (x, -1, 1), (y, -1, 1))
Graphics object consisting of 1 graphics primitive
"""
from sage.plot.all import Graphics
from sage.plot.misc import setup_for_eval_on_grid
from sage.symbolic.expression import is_Expression
from warnings import warn
import numpy
if not isinstance(f, (list, tuple)):
f = [f]
feqs = [equify(g) for g in f if is_Expression(g) and g.operator() is operator.eq and not equify(g).is_zero()]
f = [equify(g) for g in f if not (is_Expression(g) and g.operator() is operator.eq)]
neqs = len(feqs)
if neqs > 1:
warn("There are at least 2 equations; If the region is degenerated to points, plotting might show nothing.")
feqs = [sum([fn**2 for fn in feqs])]
neqs = 1
if neqs and not bordercol:
bordercol = incol
if not f:
return implicit_plot(feqs[0], xrange, yrange, plot_points=plot_points, fill=False, \
linewidth=borderwidth, linestyle=borderstyle, color=bordercol, **options)
f_all, ranges = setup_for_eval_on_grid(feqs + f, [xrange, yrange], plot_points)
xrange,yrange=[r[:2] for r in ranges]
xy_data_arrays = numpy.asarray([[[func(x, y) for x in xsrange(*ranges[0], include_endpoint=True)]
for y in xsrange(*ranges[1], include_endpoint=True)]
for func in f_all[neqs::]],dtype=float)
xy_data_array=numpy.abs(xy_data_arrays.prod(axis=0))
# Now we need to set entries to negative iff all
# functions were negative at that point.
neg_indices = (xy_data_arrays<0).all(axis=0)
xy_data_array[neg_indices]=-xy_data_array[neg_indices]
from matplotlib.colors import ListedColormap
incol = rgbcolor(incol)
示例10: _eval_
def _eval_(self, n, x):
"""
The :meth:`_eval_()` method decides which evaluation suits best
for the given input, and returns a proper value.
EXAMPLES::
sage: var('n,x')
(n, x)
sage: chebyshev_T(5,x)
16*x^5 - 20*x^3 + 5*x
sage: chebyshev_T(64, x)
2*(2*(2*(2*(2*(2*x^2 - 1)^2 - 1)^2 - 1)^2 - 1)^2 - 1)^2 - 1
sage: chebyshev_T(n,-1)
(-1)^n
sage: chebyshev_T(-7,x)
64*x^7 - 112*x^5 + 56*x^3 - 7*x
sage: chebyshev_T(3/2,x)
chebyshev_T(3/2, x)
sage: R.<t> = QQ[]
sage: chebyshev_T(2,t)
2*t^2 - 1
sage: chebyshev_U(2,t)
4*t^2 - 1
sage: parent(chebyshev_T(4, RIF(5)))
Real Interval Field with 53 bits of precision
sage: RR2 = RealField(5)
sage: chebyshev_T(100000,RR2(2))
8.9e57180
sage: chebyshev_T(5,Qp(3)(2))
2 + 3^2 + 3^3 + 3^4 + 3^5 + O(3^20)
sage: chebyshev_T(100001/2, 2)
doctest:...: RuntimeWarning: mpmath failed, keeping expression unevaluated
chebyshev_T(100001/2, 2)
sage: chebyshev_U._eval_(1.5, Mod(8,9)) is None
True
"""
# n is an integer => evaluate algebraically (as polynomial)
if n in ZZ:
n = ZZ(n)
# Expanded symbolic expression only for small values of n
if is_Expression(x) and n.abs() < 32:
return self.eval_formula(n, x)
return self.eval_algebraic(n, x)
if is_Expression(x) or is_Expression(n):
# Check for known identities
try:
return self._eval_special_values_(n, x)
except ValueError:
# Don't evaluate => keep symbolic
return None
# n is not an integer and neither n nor x is symbolic.
# We assume n and x are real/complex and evaluate numerically
try:
import sage.libs.mpmath.all as mpmath
return self._evalf_(n, x)
except mpmath.NoConvergence:
warnings.warn("mpmath failed, keeping expression unevaluated", RuntimeWarning)
return None
except Exception:
# Numerical evaluation failed => keep symbolic
return None
示例11: solve
#.........这里部分代码省略.........
[[s == 1, j == 0], [s == g/b, j == (b - g)*m/(b*g)]]
Inequalities can be also solved::
sage: solve(x^2>8,x)
[[x < -2*sqrt(2)], [x > 2*sqrt(2)]]
Use use_grobner if no solution is obtained from to_poly_solve::
sage: x,y=var('x y'); c1(x,y)=(x-5)^2+y^2-16; c2(x,y)=(y-3)^2+x^2-9
sage: solve([c1(x,y),c2(x,y)],[x,y])
[[x == -9/68*sqrt(55) + 135/68, y == -15/68*sqrt(5)*sqrt(11) + 123/68], [x == 9/68*sqrt(55) + 135/68, y == 15/68*sqrt(5)*sqrt(11) + 123/68]]
TESTS::
sage: solve([sin(x)==x,y^2==x],x,y)
[sin(x) == x, y^2 == x]
sage: solve(0==1,x)
Traceback (most recent call last):
...
TypeError: The first argument must be a symbolic expression or a list of symbolic expressions.
Test if the empty list is returned, too, when (a list of)
dictionaries (is) are requested (#8553)::
sage: solve([0==1],x)
[]
sage: solve([0==1],x,solution_dict=True)
[]
sage: solve([x==1,x==-1],x)
[]
sage: solve([x==1,x==-1],x,solution_dict=True)
[]
sage: solve((x==1,x==-1),x,solution_dict=0)
[]
Relaxed form, suggested by Mike Hansen (#8553)::
sage: solve([x^2-1],x,solution_dict=-1)
[{x: -1}, {x: 1}]
sage: solve([x^2-1],x,solution_dict=1)
[{x: -1}, {x: 1}]
sage: solve((x==1,x==-1),x,solution_dict=-1)
[]
sage: solve((x==1,x==-1),x,solution_dict=1)
[]
This inequality holds for any real ``x`` (trac #8078)::
sage: solve(x^4+2>0,x)
[x < +Infinity]
"""
from sage.symbolic.expression import is_Expression
if is_Expression(f): # f is a single expression
ans = f.solve(*args,**kwds)
return ans
if not isinstance(f, (list, tuple)):
raise TypeError("The first argument must be a symbolic expression or a list of symbolic expressions.")
if len(f)==1 and is_Expression(f[0]):
# f is a list with a single expression
return f[0].solve(*args,**kwds)
# f is a list of such expressions or equations
from sage.symbolic.ring import is_SymbolicVariable
if len(args)==0:
raise TypeError, "Please input variables to solve for."
if is_SymbolicVariable(args[0]):
variables = args
else:
variables = tuple(args[0])
for v in variables:
if not is_SymbolicVariable(v):
raise TypeError, "%s is not a valid variable."%v
try:
f = [s for s in f if s is not True]
except TypeError:
raise ValueError, "Unable to solve %s for %s"%(f, args)
if any(s is False for s in f):
return []
from sage.calculus.calculus import maxima
m = maxima(f)
try:
s = m.solve(variables)
except: # if Maxima gave an error, try its to_poly_solve
try:
s = m.to_poly_solve(variables)
except TypeError, mess: # if that gives an error, raise an error.
if "Error executing code in Maxima" in str(mess):
raise ValueError, "Sage is unable to determine whether the system %s can be solved for %s"%(f,args)
else:
raise
示例12: to_cartesian
def to_cartesian(self, func, params=None):
"""
Returns a 3-tuple of functions, parameterized over ``params``, that
represents the cartesian coordinates of the value of ``func``.
INPUT:
- ``func`` - A function in this coordinate space. Corresponds to the
independent variable.
- ``params`` - The parameters of func. Corresponds to the dependent
variables.
EXAMPLE::
sage: from sage.plot.plot3d.plot3d import _ArbitraryCoordinates
sage: x, y, z = var('x y z')
sage: T = _ArbitraryCoordinates((x + y, x - y, z), z,[x,y])
sage: f(x, y) = 2*x+y
sage: T.to_cartesian(f, [x, y])
(x + y, x - y, 2*x + y)
sage: [h(1,2) for h in T.to_cartesian(lambda x,y: 2*x+y)]
[3, -1, 4]
We try to return a function having the same variable names as
the function passed in::
sage: from sage.plot.plot3d.plot3d import _ArbitraryCoordinates
sage: x, y, z = var('x y z')
sage: T = _ArbitraryCoordinates((x + y, x - y, z), z,[x,y])
sage: f(a, b) = 2*a+b
sage: T.to_cartesian(f, [a, b])
(a + b, a - b, 2*a + b)
sage: t1,t2,t3=T.to_cartesian(lambda a,b: 2*a+b)
sage: import inspect
sage: inspect.getargspec(t1)
ArgSpec(args=['a', 'b'], varargs=None, keywords=None, defaults=None)
sage: inspect.getargspec(t2)
ArgSpec(args=['a', 'b'], varargs=None, keywords=None, defaults=None)
sage: inspect.getargspec(t3)
ArgSpec(args=['a', 'b'], varargs=None, keywords=None, defaults=None)
sage: def g(a,b): return 2*a+b
sage: t1,t2,t3=T.to_cartesian(g)
sage: inspect.getargspec(t1)
ArgSpec(args=['a', 'b'], varargs=None, keywords=None, defaults=None)
sage: t1,t2,t3=T.to_cartesian(2*a+b)
sage: inspect.getargspec(t1)
ArgSpec(args=['a', 'b'], varargs=None, keywords=None, defaults=None)
If we cannot guess the right parameter names, then the
parameters are named `u` and `v`::
sage: from sage.plot.plot3d.plot3d import _ArbitraryCoordinates
sage: x, y, z = var('x y z')
sage: T = _ArbitraryCoordinates((x + y, x - y, z), z,[x,y])
sage: t1,t2,t3=T.to_cartesian(operator.add)
sage: inspect.getargspec(t1)
ArgSpec(args=['u', 'v'], varargs=None, keywords=None, defaults=None)
sage: [h(1,2) for h in T.to_cartesian(operator.mul)]
[3, -1, 2]
sage: [h(u=1,v=2) for h in T.to_cartesian(operator.mul)]
[3, -1, 2]
"""
from sage.symbolic.expression import is_Expression
from sage.rings.real_mpfr import is_RealNumber
from sage.rings.integer import is_Integer
if params is not None and (is_Expression(func) or is_RealNumber(func) or is_Integer(func)):
return self.transform(**{
self.dep_var: func,
self.indep_vars[0]: params[0],
self.indep_vars[1]: params[1]
})
else:
# func might be a lambda or a Python callable; this makes it slightly
# more complex.
import sage.symbolic.ring
dep_var_dummy = sage.symbolic.ring.var(self.dep_var)
indep_var_dummies = sage.symbolic.ring.var(','.join(self.indep_vars))
transformation = self.transform(**{
self.dep_var: dep_var_dummy,
self.indep_vars[0]: indep_var_dummies[0],
self.indep_vars[1]: indep_var_dummies[1]
})
if params is None:
if callable(func):
params = _find_arguments_for_callable(func)
if params is None:
params=['u','v']
else:
raise ValueError, "function is not callable"
def subs_func(t):
# We use eval so that the lambda function has the same
# variable names as the original function
ll="""lambda {x},{y}: t.subs({{
dep_var_dummy: func({x}, {y}),
indep_var_dummies[0]: {x},
indep_var_dummies[1]: {y}
}})""".format(x=params[0], y=params[1])
return eval(ll,dict(t=t, func=func, dep_var_dummy=dep_var_dummy,
#.........这里部分代码省略.........