Module: sage.misc.misc
Author Log:
Module-level Functions
a, b) |
) |
Returns the sum of a sequence of numbers (NOT strings) plus the value of parameter 'start'. When the sequence is empty, returns start.
seconds) |
Raise a KeyboardInterrupt exception in a given number of seconds. This is useful for automatically interrupting long computations and can be trapped using exception handling.
INPUT: seconds -- integer
) |
c) |
[t=0]) |
Return the time in CPU second since SAGE started, or with optional argument t, return the time since time t. This is how much time SAGE has spent using the CPU. It does not count time spent by subprocesses spawned by SAGE (e.g., Gap, Singular, etc.).
INPUT: t -- (optional) float, time in CPU seconds OUTPUT: float -- time in CPU seconds
sage: t = cputime() sage: F = factor(2^199-1) sage: cputime(t) # somewhat random 0.29000000000000004
sage: w = walltime() sage: F = factor(2^199-1) sage: walltime(w) # somewhat random 0.8823847770690918
) |
S, P) |
If S contains an element x such that P(x) is True, this function returns True and the element x. Otherwise it returns False and None.
INPUT: S -- object (that supports enumeration) P -- function that returns True or False OUTPUT: bool -- whether or not P is True for some element x of S object -- x
lambda functions are very useful when using the exists function:
sage: exists([1,2,5], lambda x : x > 7) (False, None) sage: exists([1,2,5], lambda x : x > 3) (True, 5)
The following example is similar to one in the MAGMA handbook. We check whether certain integers are a some of two (small) cubes:
sage: cubes = [t**3 for t in range(-10,11)] sage: exists([(x,y) for x in cubes for y in cubes], lambda v : v[0]+v[1] == 218) (True, (-125, 343)) sage: exists([(x,y) for x in cubes for y in cubes], lambda v : v[0]+v[1] == 219) (False, None)
S, P) |
If P(x) is true every x in S, return True and None. If there is some element x in S such that P is not True, return False and x.
INPUT: S -- object (that supports enumeration) P -- function that returns True or False OUTPUT: bool -- whether or not P is True for all elements of S object -- x
lambda functions are very useful when using the forall function. As a toy example we test whether certain integers are >3.
sage: forall([1,2,5], lambda x : x > 3) (False, 1) sage: forall([1,2,5], lambda x : x > 0) (True, None)
Next we ask whether every positive integer <100 is a product of at most 2 prime factors:
sage: forall(range(1,100), lambda n : len(factor(n)) <= 2) (False, 30)
The answer is no, and 30 is a counterexample. However, every positive integer < 100 is a product of at most 3 primes.
sage: forall(range(1,100), lambda n : len(factor(n)) <= 3) (True, None)
) |
Return the global SAGE verbosity level.
INPUT: int level: an integer between 0 and 2, inclusive. OUTPUT: changes the state of the verbosity flag.
sage: get_verbose() 0 sage: set_verbose(2) sage: get_verbose() 2 sage: set_verbose(0)
) |
x, [z=None]) |
Return the product of the elements in the list x. If optimal argument z is not given, start the product with the first element of the list, otherwise use z. The empty product is the int 1 if z is not specified, and is z if given.
sage: prod([1,2,34]) 68 sage: prod([2,3], 5) 30 sage: F = factor(-2006); F -1 * 2 * 17 * 59 sage: prod(F) -2006
X) |
Iterator over the list of all subsets of the iterable X, in no particular order. Each list appears exactly once, up to order.
INPUT: X -- an iterable OUTPUT: iterator of lists
sage: list(powerset([1,2,3])) [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]] sage: [z for z in powerset([0,[1,2]])] [[], [0], [[1, 2]], [0, [1, 2]]]
x, [z=None]) |
Return the product of the elements in the list x. If optimal argument z is not given, start the product with the first element of the list, otherwise use z. The empty product is the int 1 if z is not specified, and is z if given.
sage: prod([1,2,34]) 68 sage: prod([2,3], 5) 30 sage: F = factor(-2006); F -1 * 2 * 17 * 59 sage: prod(F) -2006
X, s) |
Return a pseudo-random sublist of the list X where the probability of including a particular element is s.
INPUT: X -- list s -- floating point number between 0 and 1 OUTPUT: list
sage: S = [1,7,3,4,18] sage: random_sublist(S, 0.5) [7] sage: random_sublist(S, 0.5) [1, 7, 3]
symbols, coeffs) |
Compute a string representation of a linear combination of some formal symbols.
INPUT: symbols -- list of symbols coeffs -- list of coefficients of the symbols OUTPUT: str -- a string
sage: repr_lincomb(['a','b','c'], [1,2,3]) 'a + 2*b + 3*c' sage: repr_lincomb(['a','b','c'], [1,'2+3*x',3]) 'a + (2+3*x)*b + 3*c' sage: repr_lincomb(['a','b','c'], ['1+x^2','2+3*x',3]) '(1+x^2)*a + (2+3*x)*b + 3*c' sage: repr_lincomb(['a','b','c'], ['1+x^2','-2+3*x',3]) '(1+x^2)*a + (-2+3*x)*b + 3*c' sage: repr_lincomb(['a','b','c'], [1,-2,-3]) 'a - 2*b - 3*c' sage: t = PolynomialRing(RationalField(),'t').gen() sage: repr_lincomb(['a', 's', ''], [-t,t-2,t**2+2]) '-t*a + (t-2)*s + (t^2+2)'
) |
level, [files=all]) |
Set the global SAGE verbosity level.
INPUT: int level: an integer between 0 and 2, inclusive. files (default: 'all'): list of files to make verbose, or 'all' to make ALL files verbose (the default). OUTPUT: changes the state of the verbosity flag and possibly appends to the list of files that are verbose.
sage: set_verbose(2) sage: _ = verbose("This is SAGE.", level=1) VERBOSE1 (?): This is SAGE. sage: _ = verbose("This is SAGE.", level=2) VERBOSE2 (?): This is SAGE. sage: _ = verbose("This is SAGE.", level=3) [no output]
file_name) |
object) |
a, [b=1], [step=None]) |
Return list of numbers a, a+step, ..., a+k*step
,
where a+k*step < b
and a+(k+1)*step > b
.
This is the best way to get an iterator over SAGE integers as opposed to Python int's.
INPUT: a -- number b -- number (default: None) step -- number (default: 1) OUTPUT: list
If b is None, then b is set equal to a and a is set equal to the 0 in the parent of b.
Unlike range, a and b can be any type of numbers, and the resulting list involves numbers of that type.
NOTE: This function is called srange
to distinguish
it from the builtin Python range
command. The s
at the beginning of the name stands for ``SAGE''.
SEE ALSO: xsrange - iterator version
sage: v = srange(5); v [0, 1, 2, 3, 4] sage: type(v[2]) <type 'integer.Integer'>
sage: srange(1, 10) [1, 2, 3, 4, 5, 6, 7, 8, 9]
sage: Q = RationalField() sage: srange(1,10,Q('1/2')) [1, 3/2, 2, 5/2, 3, 7/2, 4, 9/2, 5, 11/2, 6, 13/2, 7, 15/2, 8, 17/2, 9, 19/2]
sage: R = RealField() sage: srange(1,5,R('0.5')) [1, 1.5000000000000000, 2.0000000000000000, 2.5000000000000000, 3.0000000000000000, 3.5000000000000000, 4.0000000000000000, 4.5000000000000000] sage: srange(0,1,R('0.4')) [0, 0.40000000000000002, 0.80000000000000004]
name) |
Create and return a temporary directory in $HOME/.sage/tmp/pid/
[name=tmp]) |
x, [y=None]) |
Return the union of x and y, as a list. The resulting list need not be sorted and can change from call to call.
INPUT: x -- iterable y -- iterable (may optionally omitted) OUTPUT: list
sage: union([1,2,3,4], [5,6]) [1, 3, 2, 5, 4, 6] sage: union([1,2,3,4,5,6], [5,6]) [1, 3, 2, 5, 4, 6] sage: union((1,2,3,4,5,6), [5,6]) [1, 3, 2, 5, 4, 6] sage: union((1,2,3,4,5,6), set([5,6])) [1, 3, 2, 5, 4, 6]
x) |
Return the sublist of all elements in the list x that is sorted and is such that the entries in the sublist are unique.
sage: uniq([1,1,8,-5,3,-5,'a','x','a']) [-5, 1, 3, 8, 'a', 'x']
file_name) |
[mesg=None], [t=1], [level=0], [caller_name=]) |
Print a message if the current verbosity is at least level.
INPUT: mesg -- str, a message to print t -- int, optional, if included, will also print cputime(t), - which is the time since time t. Thus t should have been obtained with t=cputime() level -- int, (default: 1) the verbosity level of what we are printing caller_name -- string (default: None), the name of the calling function; in most cases Python can deduce this, so it need not be provided. OUTPUT: possibly prints a message to stdout; also returns cputime()
sage: set_verbose(1) sage: t = cputime() sage: t = verbose("This is SAGE.", t, level=1, caller_name="william") VERBOSE1 (william): This is SAGE. (time = 0.0)
) |
Return the version of SAGE.
INPUT: nothing OUTPUT: str
sage: print version() SAGE Version x.y.z, Build Date: xxxx-xx-xx-xxxx
[t=0]) |
Return the wall time in second, or with optional argument t, return the wall time since time t. "Wall time" means the time on a wall clock, i.e., the actual time.
INPUT: t -- (optional) float, time in CPU seconds OUTPUT: float -- time in seconds
sage: w = walltime() sage: F = factor(2^199-1) sage: walltime(w) # somewhat random 0.8823847770690918
s, [ncols=85]) |
a, [b=1], [step=None]) |
Return an iterator over numbers a, a+step, ..., a+k*step
,
where a+k*step < b
and a+(k+1)*step > b
.
INPUT: a -- number b -- number step -- number (default: 1) OUTPUT: iterator
Unlike range, a and b can be any type of numbers, and the resulting iterator involves numbers of that type.
SEE ALSO: srange.
NOTE: This function is called xsrange
to distinguish
it from the builtin Python xrange
command.
sage: list(xsrange(1,10)) [1, 2, 3, 4, 5, 6, 7, 8, 9]
sage: Q = RationalField() sage: list(xsrange(1, 10, Q('1/2'))) [1, 3/2, 2, 5/2, 3, 7/2, 4, 9/2, 5, 11/2, 6, 13/2, 7, 15/2, 8, 17/2, 9, 19/2]
sage: R = RealField() sage: list(xsrange(1, 5, R(0.5))) [1, 1.5000000000000000, 2.0000000000000000, 2.5000000000000000, 3.0000000000000000, 3.5000000000000000, 4.0000000000000000, 4.5000000000000000] sage: list(xsrange(0, 1, R('0.4'))) [0, 0.40000000000000002, 0.80000000000000004]
See About this document... for information on suggesting changes.