12.07.2015 Views

Sage Developer's Guide - Mirrors

Sage Developer's Guide - Mirrors

Sage Developer's Guide - Mirrors

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Sage</strong> Developer’s <strong>Guide</strong>, Release 6.1.1sage: c.__hash__()1335416675971793195# randomHowever, most functions generating pseudorandom output do not need this tag since the doctesting frameworkguarantees the state of the pseudorandom number generators (PRNGs) used in <strong>Sage</strong> for a given doctest.See Randomized Testing for details on this framework. It is preferable to write tests that do not expose thisnon-determinism, for example rather than checking the value of the hash in a dockets, one could illustratesuccessfully using it as a key in a dict.• If a line contains the comment long time then that line is not tested unless the --long option is given, e.g.sage -t --long f.py. Use this to include examples that take more than about a second to run. Thesewill not be run regularly during <strong>Sage</strong> development, but will get run before major releases. No example shouldtake more than about 30 seconds.For instance, here is part of the docstring from the regulator method for rational elliptic curves, from thefile SAGE_ROOT/devel/sage/sage/schemes/elliptic_curves/ell_rational.py:sage: E = EllipticCurve([0, 0, 1, -1, 0])sage: E.regulator() # long time (1 second)0.0511114082399688• If a comment contains tol or tolerance, numerical results are only verified to the given tolerance. Thismay be prefixed by abs[olute] or rel[ative] to specify whether to measure absolute or relative error;this defaults to relative error except when the expected value is exactly zero:sage: RDF(pi) # abs tol 1e-53.14159sage: [10^n for n in [0.0 .. 4]] # rel tol 2e-4[0.9999, 10.001, 100.01, 999.9, 10001]This can be useful when the exact output is subject to rounding error and/or processor floating point arithmeticvariation. Here are some more examples.A singular value decomposition of a matrix will produce two unitary matrices. Over the reals, this means theinverse of the matrix is equal to its transpose. We test this result by applying the norm to a matrix difference.The result will usually be a “small” number, distinct from zero:sage: A = matrix(RDF, 8, range(64))sage: U, S, V = A.SVD()sage: (U.transpose()*U-identity_matrix(8)).norm(p=2) # abs tol 1e-100.0The 8-th cyclotomic field is generated by the complex number e iπ 4 . Here we compute a numerical approximation:sage: K. = CyclotomicField(8)sage: N(zeta8) # absolute tolerance 1e-100.7071067812 + 0.7071067812*IA relative tolerance on a root of a polynomial. Notice that the root should normally print as 1e+16, or somethingsimilar. However, the tolerance testing causes the doctest framework to use the output in a computation, so othervalid text representations of the predicted value may be used. However, they must fit the pattern defined by theregular expression float_regex in sage.doctest.parsing:sage: y = polygen(RDF, ’y’)sage: p = (y - 10^16)*(y-10^(-13))*(y-2); py^3 - 1e+16*y^2 + 2e+16*y - 2000.0sage: p.roots(multiplicities=False)[2] # relative tol 1e-10100000000000000004.1. General Conventions 29

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!