Other functions¶
- class sage.functions.other.Function_Order[source]¶
Bases:
GinacFunctionThe order function.
This function gives the order of magnitude of some expression, similar to \(O\)-terms.
EXAMPLES:
sage: x = SR('x') # needs sage.symbolic sage: x.Order() # needs sage.symbolic Order(x) sage: (x^2 + x).Order() # needs sage.symbolic Order(x^2 + x)
>>> from sage.all import * >>> x = SR('x') # needs sage.symbolic >>> x.Order() # needs sage.symbolic Order(x) >>> (x**Integer(2) + x).Order() # needs sage.symbolic Order(x^2 + x)
- class sage.functions.other.Function_abs[source]¶
Bases:
GinacFunctionThe absolute value function.
EXAMPLES:
sage: abs(-2) 2 sage: # needs sage.symbolic sage: var('x y') (x, y) sage: abs(x) abs(x) sage: abs(x^2 + y^2) abs(x^2 + y^2) sage: sqrt(x^2) sqrt(x^2) sage: abs(sqrt(x)) sqrt(abs(x)) sage: complex(abs(3*I)) (3+0j) sage: f = sage.functions.other.Function_abs() sage: latex(f) \mathrm{abs} sage: latex(abs(x)) # needs sage.symbolic {\left| x \right|} sage: abs(x)._sympy_() # needs sympy sage.symbolic Abs(x)
>>> from sage.all import * >>> abs(-Integer(2)) 2 >>> # needs sage.symbolic >>> var('x y') (x, y) >>> abs(x) abs(x) >>> abs(x**Integer(2) + y**Integer(2)) abs(x^2 + y^2) >>> sqrt(x**Integer(2)) sqrt(x^2) >>> abs(sqrt(x)) sqrt(abs(x)) >>> complex(abs(Integer(3)*I)) (3+0j) >>> f = sage.functions.other.Function_abs() >>> latex(f) \mathrm{abs} >>> latex(abs(x)) # needs sage.symbolic {\left| x \right|} >>> abs(x)._sympy_() # needs sympy sage.symbolic Abs(x)
Test pickling:
sage: loads(dumps(abs(x))) # needs sage.symbolic abs(x)
>>> from sage.all import * >>> loads(dumps(abs(x))) # needs sage.symbolic abs(x)
- class sage.functions.other.Function_arg[source]¶
Bases:
BuiltinFunctionThe argument function for complex numbers.
EXAMPLES:
sage: # needs sage.symbolic sage: arg(3+i) arctan(1/3) sage: arg(-1+i) 3/4*pi sage: arg(2+2*i) 1/4*pi sage: arg(2+x) arg(x + 2) sage: arg(2.0+i+x) arg(x + 2.00000000000000 + 1.00000000000000*I) sage: arg(-3) pi sage: arg(3) 0 sage: arg(0) 0 sage: # needs sage.symbolic sage: latex(arg(x)) {\rm arg}\left(x\right) sage: maxima(arg(x)) atan2(0,_SAGE_VAR_x) sage: maxima(arg(2+i)) atan(1/2) sage: maxima(arg(sqrt(2)+i)) atan(1/sqrt(2)) sage: arg(x)._sympy_() # needs sympy arg(x) sage: arg(2+i) # needs sage.symbolic arctan(1/2) sage: arg(sqrt(2)+i) # needs sage.symbolic arg(sqrt(2) + I) sage: arg(sqrt(2)+i).simplify() # needs sage.symbolic arctan(1/2*sqrt(2))
>>> from sage.all import * >>> # needs sage.symbolic >>> arg(Integer(3)+i) arctan(1/3) >>> arg(-Integer(1)+i) 3/4*pi >>> arg(Integer(2)+Integer(2)*i) 1/4*pi >>> arg(Integer(2)+x) arg(x + 2) >>> arg(RealNumber('2.0')+i+x) arg(x + 2.00000000000000 + 1.00000000000000*I) >>> arg(-Integer(3)) pi >>> arg(Integer(3)) 0 >>> arg(Integer(0)) 0 >>> # needs sage.symbolic >>> latex(arg(x)) {\rm arg}\left(x\right) >>> maxima(arg(x)) atan2(0,_SAGE_VAR_x) >>> maxima(arg(Integer(2)+i)) atan(1/2) >>> maxima(arg(sqrt(Integer(2))+i)) atan(1/sqrt(2)) >>> arg(x)._sympy_() # needs sympy arg(x) >>> arg(Integer(2)+i) # needs sage.symbolic arctan(1/2) >>> arg(sqrt(Integer(2))+i) # needs sage.symbolic arg(sqrt(2) + I) >>> arg(sqrt(Integer(2))+i).simplify() # needs sage.symbolic arctan(1/2*sqrt(2))
- class sage.functions.other.Function_binomial[source]¶
Bases:
GinacFunctionReturn the binomial coefficient.
\[\binom{x}{m} = x (x-1) \cdots (x-m+1) / m!\]which is defined for \(m \in \ZZ\) and any \(x\). We extend this definition to include cases when \(x-m\) is an integer but \(m\) is not by
\[\binom{x}{m}= \binom{x}{x-m}\]If \(m < 0\), return \(0\).
INPUT:
x,m– numbers or symbolic expressions; eithermorx-mmust be an integer, else the output is symbolic
OUTPUT: number or symbolic expression (if input is symbolic)
EXAMPLES:
sage: # needs sage.symbolic sage: binomial(5, 2) 10 sage: binomial(2, 0) 1 sage: binomial(1/2, 0) # needs sage.libs.pari 1 sage: binomial(3, -1) 0 sage: binomial(20, 10) 184756 sage: binomial(-2, 5) -6 sage: n = var('n'); binomial(n, 2) 1/2*(n - 1)*n sage: n = var('n'); binomial(n, n) 1 sage: n = var('n'); binomial(n, n - 1) n sage: binomial(2^100, 2^100) 1 sage: binomial(RealField()('2.5'), 2) # needs sage.rings.real_mpfr 1.87500000000000
>>> from sage.all import * >>> # needs sage.symbolic >>> binomial(Integer(5), Integer(2)) 10 >>> binomial(Integer(2), Integer(0)) 1 >>> binomial(Integer(1)/Integer(2), Integer(0)) # needs sage.libs.pari 1 >>> binomial(Integer(3), -Integer(1)) 0 >>> binomial(Integer(20), Integer(10)) 184756 >>> binomial(-Integer(2), Integer(5)) -6 >>> n = var('n'); binomial(n, Integer(2)) 1/2*(n - 1)*n >>> n = var('n'); binomial(n, n) 1 >>> n = var('n'); binomial(n, n - Integer(1)) n >>> binomial(Integer(2)**Integer(100), Integer(2)**Integer(100)) 1 >>> binomial(RealField()('2.5'), Integer(2)) # needs sage.rings.real_mpfr 1.87500000000000
sage: k, i = var('k,i') # needs sage.symbolic sage: binomial(k,i) # needs sage.symbolic binomial(k, i)
>>> from sage.all import * >>> k, i = var('k,i') # needs sage.symbolic >>> binomial(k,i) # needs sage.symbolic binomial(k, i)
We can use a
holdparameter to prevent automatic evaluation:sage: SR(5).binomial(3, hold=True) # needs sage.symbolic binomial(5, 3) sage: SR(5).binomial(3, hold=True).simplify() # needs sage.symbolic 10
>>> from sage.all import * >>> SR(Integer(5)).binomial(Integer(3), hold=True) # needs sage.symbolic binomial(5, 3) >>> SR(Integer(5)).binomial(Integer(3), hold=True).simplify() # needs sage.symbolic 10
- class sage.functions.other.Function_cases[source]¶
Bases:
GinacFunctionFormal function holding
(condition, expression)pairs.Numbers are considered conditions with zero being
False. A true condition marks a default value. The function is not evaluated as long as it contains a relation that cannot be decided by Pynac.EXAMPLES:
sage: # needs sage.symbolic sage: ex = cases([(x==0, pi), (True, 0)]); ex cases(((x == 0, pi), (1, 0))) sage: ex.subs(x==0) pi sage: ex.subs(x==2) 0 sage: ex + 1 cases(((x == 0, pi), (1, 0))) + 1 sage: _.subs(x==0) pi + 1
>>> from sage.all import * >>> # needs sage.symbolic >>> ex = cases([(x==Integer(0), pi), (True, Integer(0))]); ex cases(((x == 0, pi), (1, 0))) >>> ex.subs(x==Integer(0)) pi >>> ex.subs(x==Integer(2)) 0 >>> ex + Integer(1) cases(((x == 0, pi), (1, 0))) + 1 >>> _.subs(x==Integer(0)) pi + 1
The first encountered default is used, as well as the first relation that can be trivially decided:
sage: cases(((True, pi), (True, 0))) # needs sage.symbolic pi sage: # needs sage.symbolic sage: _ = var('y') sage: ex = cases(((x==0, pi), (y==1, 0))); ex cases(((x == 0, pi), (y == 1, 0))) sage: ex.subs(x==0) pi sage: ex.subs(x==0, y==1) pi
>>> from sage.all import * >>> cases(((True, pi), (True, Integer(0)))) # needs sage.symbolic pi >>> # needs sage.symbolic >>> _ = var('y') >>> ex = cases(((x==Integer(0), pi), (y==Integer(1), Integer(0)))); ex cases(((x == 0, pi), (y == 1, 0))) >>> ex.subs(x==Integer(0)) pi >>> ex.subs(x==Integer(0), y==Integer(1)) pi
- class sage.functions.other.Function_ceil[source]¶
Bases:
BuiltinFunctionThe ceiling function.
The ceiling of \(x\) is computed in the following manner.
The
x.ceil()method is called and returned if it is there. If it is not, then Sage checks if \(x\) is one of Python’s native numeric data types. If so, then it calls and returnsInteger(math.ceil(x)).Sage tries to convert \(x\) into a
RealIntervalFieldwith 53 bits of precision. Next, the ceilings of the endpoints are computed. If they are the same, then that value is returned. Otherwise, the precision of theRealIntervalFieldis increased until they do match up or it reachesbitsof precision.If none of the above work, Sage returns a
Expressionobject.
EXAMPLES:
sage: # needs sage.symbolic sage: a = ceil(2/5 + x); a ceil(x + 2/5) sage: a(x=4) 5 sage: a(x=4.0) 5 sage: ZZ(a(x=3)) 4 sage: a = ceil(x^3 + x + 5/2); a ceil(x^3 + x + 5/2) sage: a.simplify() ceil(x^3 + x + 1/2) + 2 sage: a(x=2) 13
>>> from sage.all import * >>> # needs sage.symbolic >>> a = ceil(Integer(2)/Integer(5) + x); a ceil(x + 2/5) >>> a(x=Integer(4)) 5 >>> a(x=RealNumber('4.0')) 5 >>> ZZ(a(x=Integer(3))) 4 >>> a = ceil(x**Integer(3) + x + Integer(5)/Integer(2)); a ceil(x^3 + x + 5/2) >>> a.simplify() ceil(x^3 + x + 1/2) + 2 >>> a(x=Integer(2)) 13
sage: ceil(sin(8)/sin(2)) # needs sage.symbolic 2
>>> from sage.all import * >>> ceil(sin(Integer(8))/sin(Integer(2))) # needs sage.symbolic 2
sage: ceil(5.4) 6 sage: type(ceil(5.4)) <class 'sage.rings.integer.Integer'>
>>> from sage.all import * >>> ceil(RealNumber('5.4')) 6 >>> type(ceil(RealNumber('5.4'))) <class 'sage.rings.integer.Integer'>
sage: ceil(factorial(50)/exp(1)) # needs sage.symbolic 11188719610782480504630258070757734324011354208865721592720336801 sage: ceil(SR(10^50 + 10^(-50))) # needs sage.symbolic 100000000000000000000000000000000000000000000000001 sage: ceil(SR(10^50 - 10^(-50))) # needs sage.symbolic 100000000000000000000000000000000000000000000000000
>>> from sage.all import * >>> ceil(factorial(Integer(50))/exp(Integer(1))) # needs sage.symbolic 11188719610782480504630258070757734324011354208865721592720336801 >>> ceil(SR(Integer(10)**Integer(50) + Integer(10)**(-Integer(50)))) # needs sage.symbolic 100000000000000000000000000000000000000000000000001 >>> ceil(SR(Integer(10)**Integer(50) - Integer(10)**(-Integer(50)))) # needs sage.symbolic 100000000000000000000000000000000000000000000000000
Small numbers which are extremely close to an integer are hard to deal with:
sage: ceil((33^100 + 1)^(1/100)) # needs sage.symbolic Traceback (most recent call last): ... ValueError: cannot compute ceil(...) using 256 bits of precision
>>> from sage.all import * >>> ceil((Integer(33)**Integer(100) + Integer(1))**(Integer(1)/Integer(100))) # needs sage.symbolic Traceback (most recent call last): ... ValueError: cannot compute ceil(...) using 256 bits of precision
This can be fixed by giving a sufficiently large
bitsargument:sage: ceil((33^100 + 1)^(1/100), bits=500) # needs sage.symbolic Traceback (most recent call last): ... ValueError: cannot compute ceil(...) using 512 bits of precision sage: ceil((33^100 + 1)^(1/100), bits=1000) # needs sage.symbolic 34
>>> from sage.all import * >>> ceil((Integer(33)**Integer(100) + Integer(1))**(Integer(1)/Integer(100)), bits=Integer(500)) # needs sage.symbolic Traceback (most recent call last): ... ValueError: cannot compute ceil(...) using 512 bits of precision >>> ceil((Integer(33)**Integer(100) + Integer(1))**(Integer(1)/Integer(100)), bits=Integer(1000)) # needs sage.symbolic 34
sage: ceil(sec(e)) # needs sage.symbolic -1 sage: latex(ceil(x)) # needs sage.symbolic \left \lceil x \right \rceil sage: ceil(x)._sympy_() # needs sympy sage.symbolic ceiling(x)
>>> from sage.all import * >>> ceil(sec(e)) # needs sage.symbolic -1 >>> latex(ceil(x)) # needs sage.symbolic \left \lceil x \right \rceil >>> ceil(x)._sympy_() # needs sympy sage.symbolic ceiling(x)
sage: import numpy # needs numpy sage: a = numpy.linspace(0,2,6) # needs numpy sage: ceil(a) # needs numpy array([0., 1., 1., 2., 2., 2.])
>>> from sage.all import * >>> import numpy # needs numpy >>> a = numpy.linspace(Integer(0),Integer(2),Integer(6)) # needs numpy >>> ceil(a) # needs numpy array([0., 1., 1., 2., 2., 2.])
Test pickling:
sage: loads(dumps(ceil)) ceil
>>> from sage.all import * >>> loads(dumps(ceil)) ceil
- class sage.functions.other.Function_conjugate[source]¶
Bases:
GinacFunctionReturn the complex conjugate of the input.
It is possible to prevent automatic evaluation using the
holdparameter:sage: conjugate(I, hold=True) # needs sage.symbolic conjugate(I)
>>> from sage.all import * >>> conjugate(I, hold=True) # needs sage.symbolic conjugate(I)
To then evaluate again, we currently must use Maxima via
sage.symbolic.expression.Expression.simplify():sage: conjugate(I, hold=True).simplify() # needs sage.symbolic -I
>>> from sage.all import * >>> conjugate(I, hold=True).simplify() # needs sage.symbolic -I
- class sage.functions.other.Function_crootof[source]¶
Bases:
BuiltinFunctionFormal function holding
(polynomial, index)pairs.The expression evaluates to a floating point value that is an approximation to a specific complex root of the polynomial. The ordering is fixed so you always get the same root.
The functionality is imported from SymPy, see http://docs.sympy.org/latest/_modules/sympy/polys/rootoftools.html
EXAMPLES:
sage: # needs sage.symbolic sage: c = complex_root_of(x^6 + x + 1, 1); c complex_root_of(x^6 + x + 1, 1) sage: c.n() -0.790667188814418 + 0.300506920309552*I sage: c.n(100) -0.79066718881441764449859281847 + 0.30050692030955162512001002521*I sage: (c^6 + c + 1).n(100) < 1e-25 True
>>> from sage.all import * >>> # needs sage.symbolic >>> c = complex_root_of(x**Integer(6) + x + Integer(1), Integer(1)); c complex_root_of(x^6 + x + 1, 1) >>> c.n() -0.790667188814418 + 0.300506920309552*I >>> c.n(Integer(100)) -0.79066718881441764449859281847 + 0.30050692030955162512001002521*I >>> (c**Integer(6) + c + Integer(1)).n(Integer(100)) < RealNumber('1e-25') True
- class sage.functions.other.Function_elementof[source]¶
Bases:
BuiltinFunctionFormal set membership function that is only accessible internally.
This function is called to express a set membership statement, usually as part of a solution set returned by
solve(). Seesage.sets.set.Setandsage.sets.real_set.RealSetfor possible set arguments.EXAMPLES:
sage: # needs sage.symbolic sage: from sage.functions.other import element_of sage: element_of(x, SR(ZZ)) element_of(x, Integer Ring) sage: element_of(sin(x), SR(QQ)) element_of(sin(x), Rational Field) sage: element_of(x, SR(RealSet.open_closed(0,1))) element_of(x, (0, 1]) sage: element_of(x, SR(Set([4,6,8]))) element_of(x, {8, 4, 6})
>>> from sage.all import * >>> # needs sage.symbolic >>> from sage.functions.other import element_of >>> element_of(x, SR(ZZ)) element_of(x, Integer Ring) >>> element_of(sin(x), SR(QQ)) element_of(sin(x), Rational Field) >>> element_of(x, SR(RealSet.open_closed(Integer(0),Integer(1)))) element_of(x, (0, 1]) >>> element_of(x, SR(Set([Integer(4),Integer(6),Integer(8)]))) element_of(x, {8, 4, 6})
- class sage.functions.other.Function_factorial[source]¶
Bases:
GinacFunctionReturn the factorial of \(n\).
INPUT:
n– a nonnegative integer, a complex number (except negative integers) or any symbolic expression
OUTPUT: integer or symbolic expression
EXAMPLES:
sage: factorial(0) 1 sage: factorial(4) 24 sage: factorial(10) 3628800 sage: factorial(6) == 6*5*4*3*2 True sage: # needs sage.symbolic sage: x = SR.var('x') sage: f = factorial(x + factorial(x)); f factorial(x + factorial(x)) sage: f(x=3) 362880 sage: factorial(x)^2 factorial(x)^2
>>> from sage.all import * >>> factorial(Integer(0)) 1 >>> factorial(Integer(4)) 24 >>> factorial(Integer(10)) 3628800 >>> factorial(Integer(6)) == Integer(6)*Integer(5)*Integer(4)*Integer(3)*Integer(2) True >>> # needs sage.symbolic >>> x = SR.var('x') >>> f = factorial(x + factorial(x)); f factorial(x + factorial(x)) >>> f(x=Integer(3)) 362880 >>> factorial(x)**Integer(2) factorial(x)^2
To prevent automatic evaluation use the
holdargument:sage: factorial(5, hold=True) # needs sage.symbolic factorial(5)
>>> from sage.all import * >>> factorial(Integer(5), hold=True) # needs sage.symbolic factorial(5)
To then evaluate again, we currently must use Maxima via
sage.symbolic.expression.Expression.simplify():sage: factorial(5, hold=True).simplify() # needs sage.symbolic 120
>>> from sage.all import * >>> factorial(Integer(5), hold=True).simplify() # needs sage.symbolic 120
We can also give input other than nonnegative integers. For other nonnegative numbers, the
sage.functions.gamma.gamma()function is used:sage: factorial(1/2) # needs sage.symbolic 1/2*sqrt(pi) sage: factorial(3/4) # needs sage.symbolic gamma(7/4) sage: factorial(2.3) # needs sage.symbolic 2.68343738195577
>>> from sage.all import * >>> factorial(Integer(1)/Integer(2)) # needs sage.symbolic 1/2*sqrt(pi) >>> factorial(Integer(3)/Integer(4)) # needs sage.symbolic gamma(7/4) >>> factorial(RealNumber('2.3')) # needs sage.symbolic 2.68343738195577
But negative input always fails:
sage: factorial(-32) Traceback (most recent call last): ... ValueError: factorial only defined for nonnegative integers
>>> from sage.all import * >>> factorial(-Integer(32)) Traceback (most recent call last): ... ValueError: factorial only defined for nonnegative integers
And very large integers remain unevaluated:
sage: factorial(2**64) # needs sage.symbolic factorial(18446744073709551616) sage: SR(2**64).factorial() # needs sage.symbolic factorial(18446744073709551616)
>>> from sage.all import * >>> factorial(Integer(2)**Integer(64)) # needs sage.symbolic factorial(18446744073709551616) >>> SR(Integer(2)**Integer(64)).factorial() # needs sage.symbolic factorial(18446744073709551616)
- class sage.functions.other.Function_floor[source]¶
Bases:
BuiltinFunctionThe floor function.
The floor of \(x\) is computed in the following manner.
The
x.floor()method is called and returned if it is there. If it is not, then Sage checks if \(x\) is one of Python’s native numeric data types. If so, then it calls and returnsInteger(math.floor(x)).Sage tries to convert \(x\) into a
RealIntervalFieldwith 53 bits of precision. Next, the floors of the endpoints are computed. If they are the same, then that value is returned. Otherwise, the precision of theRealIntervalFieldis increased until they do match up or it reachesbitsof precision.If none of the above work, Sage returns a symbolic
Expressionobject.
EXAMPLES:
sage: floor(5.4) 5 sage: type(floor(5.4)) <class 'sage.rings.integer.Integer'> sage: # needs sage.symbolic sage: var('x') x sage: a = floor(5.25 + x); a floor(x + 5.25000000000000) sage: a.simplify() floor(x + 0.25) + 5 sage: a(x=2) 7
>>> from sage.all import * >>> floor(RealNumber('5.4')) 5 >>> type(floor(RealNumber('5.4'))) <class 'sage.rings.integer.Integer'> >>> # needs sage.symbolic >>> var('x') x >>> a = floor(RealNumber('5.25') + x); a floor(x + 5.25000000000000) >>> a.simplify() floor(x + 0.25) + 5 >>> a(x=Integer(2)) 7
sage: # needs sage.symbolic sage: floor(cos(8) / cos(2)) 0 sage: floor(log(4) / log(2)) 2 sage: a = floor(5.4 + x); a floor(x + 5.40000000000000) sage: a.subs(x==2) 7 sage: floor(log(2^(3/2)) / log(2) + 1/2) 2 sage: floor(log(2^(-3/2)) / log(2) + 1/2) -1
>>> from sage.all import * >>> # needs sage.symbolic >>> floor(cos(Integer(8)) / cos(Integer(2))) 0 >>> floor(log(Integer(4)) / log(Integer(2))) 2 >>> a = floor(RealNumber('5.4') + x); a floor(x + 5.40000000000000) >>> a.subs(x==Integer(2)) 7 >>> floor(log(Integer(2)**(Integer(3)/Integer(2))) / log(Integer(2)) + Integer(1)/Integer(2)) 2 >>> floor(log(Integer(2)**(-Integer(3)/Integer(2))) / log(Integer(2)) + Integer(1)/Integer(2)) -1
sage: floor(factorial(50)/exp(1)) # needs sage.symbolic 11188719610782480504630258070757734324011354208865721592720336800 sage: floor(SR(10^50 + 10^(-50))) # needs sage.symbolic 100000000000000000000000000000000000000000000000000 sage: floor(SR(10^50 - 10^(-50))) # needs sage.symbolic 99999999999999999999999999999999999999999999999999 sage: floor(int(10^50)) 100000000000000000000000000000000000000000000000000
>>> from sage.all import * >>> floor(factorial(Integer(50))/exp(Integer(1))) # needs sage.symbolic 11188719610782480504630258070757734324011354208865721592720336800 >>> floor(SR(Integer(10)**Integer(50) + Integer(10)**(-Integer(50)))) # needs sage.symbolic 100000000000000000000000000000000000000000000000000 >>> floor(SR(Integer(10)**Integer(50) - Integer(10)**(-Integer(50)))) # needs sage.symbolic 99999999999999999999999999999999999999999999999999 >>> floor(int(Integer(10)**Integer(50))) 100000000000000000000000000000000000000000000000000
Small numbers which are extremely close to an integer are hard to deal with:
sage: floor((33^100 + 1)^(1/100)) # needs sage.symbolic Traceback (most recent call last): ... ValueError: cannot compute floor(...) using 256 bits of precision
>>> from sage.all import * >>> floor((Integer(33)**Integer(100) + Integer(1))**(Integer(1)/Integer(100))) # needs sage.symbolic Traceback (most recent call last): ... ValueError: cannot compute floor(...) using 256 bits of precision
This can be fixed by giving a sufficiently large
bitsargument:sage: floor((33^100 + 1)^(1/100), bits=500) # needs sage.symbolic Traceback (most recent call last): ... ValueError: cannot compute floor(...) using 512 bits of precision sage: floor((33^100 + 1)^(1/100), bits=1000) # needs sage.symbolic 33
>>> from sage.all import * >>> floor((Integer(33)**Integer(100) + Integer(1))**(Integer(1)/Integer(100)), bits=Integer(500)) # needs sage.symbolic Traceback (most recent call last): ... ValueError: cannot compute floor(...) using 512 bits of precision >>> floor((Integer(33)**Integer(100) + Integer(1))**(Integer(1)/Integer(100)), bits=Integer(1000)) # needs sage.symbolic 33
sage: import numpy # needs numpy sage: a = numpy.linspace(0,2,6) # needs numpy sage: floor(a) # needs numpy array([0., 0., 0., 1., 1., 2.]) sage: floor(x)._sympy_() # needs sympy sage.symbolic floor(x)
>>> from sage.all import * >>> import numpy # needs numpy >>> a = numpy.linspace(Integer(0),Integer(2),Integer(6)) # needs numpy >>> floor(a) # needs numpy array([0., 0., 0., 1., 1., 2.]) >>> floor(x)._sympy_() # needs sympy sage.symbolic floor(x)
Test pickling:
sage: loads(dumps(floor)) floor
>>> from sage.all import * >>> loads(dumps(floor)) floor
- class sage.functions.other.Function_frac[source]¶
Bases:
BuiltinFunctionThe fractional part function \(\{x\}\).
frac(x)is defined as \(\{x\} = x - \lfloor x\rfloor\).EXAMPLES:
sage: frac(5.4) # needs sage.rings.real_mpfr 0.400000000000000 sage: type(frac(5.4)) # needs sage.rings.real_mpfr <class 'sage.rings.real_mpfr.RealNumber'> sage: frac(456/123) 29/41 sage: # needs sage.symbolic sage: var('x') x sage: a = frac(5.4 + x); a frac(x + 5.40000000000000) sage: frac(cos(8)/cos(2)) cos(8)/cos(2) sage: latex(frac(x)) \operatorname{frac}\left(x\right) sage: frac(x)._sympy_() # needs sympy frac(x)
>>> from sage.all import * >>> frac(RealNumber('5.4')) # needs sage.rings.real_mpfr 0.400000000000000 >>> type(frac(RealNumber('5.4'))) # needs sage.rings.real_mpfr <class 'sage.rings.real_mpfr.RealNumber'> >>> frac(Integer(456)/Integer(123)) 29/41 >>> # needs sage.symbolic >>> var('x') x >>> a = frac(RealNumber('5.4') + x); a frac(x + 5.40000000000000) >>> frac(cos(Integer(8))/cos(Integer(2))) cos(8)/cos(2) >>> latex(frac(x)) \operatorname{frac}\left(x\right) >>> frac(x)._sympy_() # needs sympy frac(x)
Test pickling:
sage: loads(dumps(floor)) floor
>>> from sage.all import * >>> loads(dumps(floor)) floor
- class sage.functions.other.Function_imag_part[source]¶
Bases:
GinacFunctionReturn the imaginary part of the (possibly complex) input.
It is possible to prevent automatic evaluation using the
holdparameter:sage: imag_part(I, hold=True) # needs sage.symbolic imag_part(I)
>>> from sage.all import * >>> imag_part(I, hold=True) # needs sage.symbolic imag_part(I)
To then evaluate again, we currently must use Maxima via
sage.symbolic.expression.Expression.simplify():sage: imag_part(I, hold=True).simplify() # needs sage.symbolic 1
>>> from sage.all import * >>> imag_part(I, hold=True).simplify() # needs sage.symbolic 1
- class sage.functions.other.Function_limit[source]¶
Bases:
BuiltinFunctionPlaceholder symbolic limit function that is only accessible internally.
This function is called to create formal wrappers of limits that Maxima can’t compute:
sage: a = lim(exp(x^2)*(1-erf(x)), x=infinity); a # needs sage.symbolic -limit((erf(x) - 1)*e^(x^2), x, +Infinity)
>>> from sage.all import * >>> a = lim(exp(x**Integer(2))*(Integer(1)-erf(x)), x=infinity); a # needs sage.symbolic -limit((erf(x) - 1)*e^(x^2), x, +Infinity)
EXAMPLES:
sage: # needs sage.symbolic sage: from sage.functions.other import symbolic_limit as slimit sage: slimit(1/x, x, +oo) limit(1/x, x, +Infinity) sage: var('minus,plus') (minus, plus) sage: slimit(1/x, x, +oo) limit(1/x, x, +Infinity) sage: slimit(1/x, x, 0, plus) limit(1/x, x, 0, plus) sage: slimit(1/x, x, 0, minus) limit(1/x, x, 0, minus)
>>> from sage.all import * >>> # needs sage.symbolic >>> from sage.functions.other import symbolic_limit as slimit >>> slimit(Integer(1)/x, x, +oo) limit(1/x, x, +Infinity) >>> var('minus,plus') (minus, plus) >>> slimit(Integer(1)/x, x, +oo) limit(1/x, x, +Infinity) >>> slimit(Integer(1)/x, x, Integer(0), plus) limit(1/x, x, 0, plus) >>> slimit(Integer(1)/x, x, Integer(0), minus) limit(1/x, x, 0, minus)
- class sage.functions.other.Function_prod[source]¶
Bases:
BuiltinFunctionPlaceholder symbolic product function that is only accessible internally.
EXAMPLES:
sage: from sage.functions.other import symbolic_product as sprod sage: r = sprod(x, x, 1, 10); r # needs sage.symbolic product(x, x, 1, 10) sage: r.unhold() # needs sage.symbolic 3628800
>>> from sage.all import * >>> from sage.functions.other import symbolic_product as sprod >>> r = sprod(x, x, Integer(1), Integer(10)); r # needs sage.symbolic product(x, x, 1, 10) >>> r.unhold() # needs sage.symbolic 3628800
- class sage.functions.other.Function_real_nth_root[source]¶
Bases:
BuiltinFunctionReal \(n\)-th root function \(x^\frac{1}{n}\).
The function assumes positive integer \(n\) and real number \(x\).
EXAMPLES:
sage: real_nth_root(2, 3) # needs sage.symbolic 2^(1/3) sage: real_nth_root(-2, 3) # needs sage.symbolic -2^(1/3) sage: real_nth_root(8, 3) 2 sage: real_nth_root(-8, 3) -2 sage: real_nth_root(-2, 4) Traceback (most recent call last): ... ValueError: no real nth root of negative real number with even n
>>> from sage.all import * >>> real_nth_root(Integer(2), Integer(3)) # needs sage.symbolic 2^(1/3) >>> real_nth_root(-Integer(2), Integer(3)) # needs sage.symbolic -2^(1/3) >>> real_nth_root(Integer(8), Integer(3)) 2 >>> real_nth_root(-Integer(8), Integer(3)) -2 >>> real_nth_root(-Integer(2), Integer(4)) Traceback (most recent call last): ... ValueError: no real nth root of negative real number with even n
For numeric input, it gives a numerical approximation.
sage: real_nth_root(2., 3) # needs sage.rings.real_mpfr 1.25992104989487 sage: real_nth_root(-2., 3) # needs sage.rings.real_mpfr -1.25992104989487
>>> from sage.all import * >>> real_nth_root(RealNumber('2.'), Integer(3)) # needs sage.rings.real_mpfr 1.25992104989487 >>> real_nth_root(-RealNumber('2.'), Integer(3)) # needs sage.rings.real_mpfr -1.25992104989487
Some symbolic calculus:
sage: # needs sage.symbolic sage: f = real_nth_root(x, 5)^3; f real_nth_root(x^3, 5) sage: f.diff() 3/5*x^2*real_nth_root(x^(-12), 5) sage: result = f.integrate(x) ... sage: result integrate((abs(x)^3)^(1/5)*sgn(x^3), x) sage: _.diff() (abs(x)^3)^(1/5)*sgn(x^3)
>>> from sage.all import * >>> # needs sage.symbolic >>> f = real_nth_root(x, Integer(5))**Integer(3); f real_nth_root(x^3, 5) >>> f.diff() 3/5*x^2*real_nth_root(x^(-12), 5) >>> result = f.integrate(x) ... >>> result integrate((abs(x)^3)^(1/5)*sgn(x^3), x) >>> _.diff() (abs(x)^3)^(1/5)*sgn(x^3)
- class sage.functions.other.Function_real_part[source]¶
Bases:
GinacFunctionReturn the real part of the (possibly complex) input.
It is possible to prevent automatic evaluation using the
holdparameter:sage: real_part(I, hold=True) # needs sage.symbolic real_part(I)
>>> from sage.all import * >>> real_part(I, hold=True) # needs sage.symbolic real_part(I)
To then evaluate again, we currently must use Maxima via
sage.symbolic.expression.Expression.simplify():sage: real_part(I, hold=True).simplify() # needs sage.symbolic 0
>>> from sage.all import * >>> real_part(I, hold=True).simplify() # needs sage.symbolic 0
EXAMPLES:
sage: z = 1+2*I # needs sage.symbolic sage: real(z) # needs sage.symbolic 1 sage: real(5/3) 5/3 sage: a = 2.5 sage: real(a) # needs sage.rings.real_mpfr 2.50000000000000 sage: type(real(a)) # needs sage.rings.real_mpfr <class 'sage.rings.real_mpfr.RealLiteral'> sage: real(1.0r) 1.0 sage: real(complex(3, 4)) 3.0
>>> from sage.all import * >>> z = Integer(1)+Integer(2)*I # needs sage.symbolic >>> real(z) # needs sage.symbolic 1 >>> real(Integer(5)/Integer(3)) 5/3 >>> a = RealNumber('2.5') >>> real(a) # needs sage.rings.real_mpfr 2.50000000000000 >>> type(real(a)) # needs sage.rings.real_mpfr <class 'sage.rings.real_mpfr.RealLiteral'> >>> real(1.0) 1.0 >>> real(complex(Integer(3), Integer(4))) 3.0
Sage can recognize some expressions as real and accordingly return the identical argument:
sage: # needs sage.symbolic sage: SR.var('x', domain='integer').real_part() x sage: SR.var('x', domain='integer').imag_part() 0 sage: real_part(sin(x)+x) x + sin(x) sage: real_part(x*exp(x)) x*e^x sage: imag_part(sin(x)+x) 0 sage: real_part(real_part(x)) x sage: forget()
>>> from sage.all import * >>> # needs sage.symbolic >>> SR.var('x', domain='integer').real_part() x >>> SR.var('x', domain='integer').imag_part() 0 >>> real_part(sin(x)+x) x + sin(x) >>> real_part(x*exp(x)) x*e^x >>> imag_part(sin(x)+x) 0 >>> real_part(real_part(x)) x >>> forget()
- class sage.functions.other.Function_sqrt¶
Bases:
object
- class sage.functions.other.Function_sum[source]¶
Bases:
BuiltinFunctionPlaceholder symbolic sum function that is only accessible internally.
EXAMPLES:
sage: from sage.functions.other import symbolic_sum as ssum sage: r = ssum(x, x, 1, 10); r # needs sage.symbolic sum(x, x, 1, 10) sage: r.unhold() # needs sage.symbolic 55
>>> from sage.all import * >>> from sage.functions.other import symbolic_sum as ssum >>> r = ssum(x, x, Integer(1), Integer(10)); r # needs sage.symbolic sum(x, x, 1, 10) >>> r.unhold() # needs sage.symbolic 55