Ticket #7977: trac_7977-math-block.patch
| File trac_7977-math-block.patch, 15.8 KB (added by mvngu, 3 years ago) |
|---|
-
doc/en/developer/conventions.rst
# HG changeset patch # User Minh Van Nguyen <nguyenminh2@gmail.com> # Date 1263824332 28800 # Node ID 5ee55fff04cc159a812b3c4a318443a039bea808 # Parent 33c04bb5aea0255c4fac02782a9974deaf33f1ce trac 7977: explain how to use the MATH block and automatic numbering with hash-dot diff -r 33c04bb5aea0 -r 5ee55fff04cc doc/en/developer/conventions.rst
a b 55 55 .. You are encouraged 56 56 to include miscellaneous notes, emails, design 57 57 discussions, etc., in your package. Make these 58 plain text files (with extension ``.txt``) 58 plain text files (with extension ``.txt``) 59 59 in a subdirectory called ``notes``. (For example, see 60 60 ``SAGE_ROOT/devel/sage/sage/ext/notes/``.) 61 61 … … 81 81 82 82 r""" 83 83 <Very short 1-line summary> 84 84 85 85 <Paragraph description> 86 86 ... 87 88 AUTHORS: 89 87 88 AUTHORS: 89 90 90 - YOUR NAME (2005-01-03): initial version 91 92 - person (date in ISO year-month-day format): short desc93 94 ...95 91 96 92 - person (date in ISO year-month-day format): short desc 97 93 98 94 ... 99 95 96 - person (date in ISO year-month-day format): short desc 97 98 ... 99 100 100 Lots and lots of examples. 101 101 """ 102 102 103 103 #***************************************************************************** 104 104 # Copyright (C) 2008 YOUR NAME <your email> 105 105 # … … 117 117 118 118 """ 119 119 Elements of the ring m`\ZZ` of integers 120 120 121 121 AUTHORS: 122 122 123 123 - William Stein (2005): initial version 124 124 125 125 - Gonzalo Tornaria (2006-03-02): vastly improved python/GMP 126 126 conversion; hashing 127 127 128 128 - Didier Deshommes (2006-03-06): numerous examples 129 129 and docstrings 130 130 131 131 - William Stein (2006-03-31): changes to reflect GMP bug fixes 132 132 133 133 - William Stein (2006-04-14): added GMP factorial method (since it's 134 134 now very fast). 135 135 136 136 - David Harvey (2006-09-15): added nth_root, exact_log 137 137 138 138 - David Harvey (2006-09-16): attempt to optimise Integer constructor 139 139 140 140 - Rishikesh (2007-02-25): changed quo_rem so that the rem is positive 141 141 142 142 - David Harvey, Martin Albrecht, Robert Bradshaw (2007-03-01): 143 143 optimized Integer constructor and pool 144 144 145 145 - Pablo De Napoli (2007-04-01): multiplicative_order should return 146 146 +infinity for non zero numbers 147 147 148 148 - Robert Bradshaw (2007-04-12): is_perfect_power, Jacobi symbol (with 149 149 Kronecker extension). Convert some methods to use GMP directly 150 150 rather than pari, Integer(), PY_NEW(Integer) 151 151 152 152 - David Roe (2007-03-21): sped up valuation and is_square, added 153 153 val_unit, is_power, is_power_of and divide_knowing_divisible_by 154 154 155 155 - Robert Bradshaw (2008-03-26): gamma function, multifactorials 156 156 157 157 - Robert Bradshaw (2008-10-02): bounded squarefree part 158 158 159 159 EXAMPLES: 160 160 161 161 Add 2 integers:: 162 162 163 163 sage: a = Integer(3) ; b = Integer(4) 164 164 sage: a + b == 7 165 165 True 166 166 167 167 Add an integer and a real number:: 168 168 169 169 sage: a + 4.0 170 170 7.00000000000000 171 171 172 172 Add an integer and a rational number:: 173 173 174 174 sage: a + Rational(2)/5 175 175 17/5 176 176 177 177 Add an integer and a complex number:: 178 178 179 179 sage: b = ComplexField().0 + 1.5 180 180 sage: loads((a+b).dumps()) == a+b 181 181 True 182 182 183 183 :: 184 184 185 185 sage: z = 32 186 186 sage: -z 187 187 -32 … … 191 191 0 192 192 sage: z = -1; -z 193 193 1 194 194 195 195 Multiplication:: 196 196 197 197 sage: a = Integer(3) ; b = Integer(4) 198 198 sage: a * b == 12 199 199 True … … 201 201 True 202 202 sage: a * Rational(2)/5 203 203 6/5 204 204 205 205 :: 206 206 207 207 sage: list([2,3]) * 4 208 208 [2, 3, 2, 3, 2, 3, 2, 3] 209 209 210 210 :: 211 211 212 212 sage: 'sage'*Integer(3) 213 213 'sagesagesage' 214 214 215 215 COERCIONS: Returns version of this integer in the multi-precision 216 216 floating real field R. 217 217 218 218 :: 219 219 220 220 sage: n = 9390823 221 221 sage: RR = RealField(200) 222 222 sage: RR(n) … … 255 255 ----------------- 256 256 257 257 **Every** function must have a docstring that includes the 258 following information: 258 following information. Source files in the Sage library contain 259 numerous examples on how to format your documentation, so you could 260 use them as a guide. 259 261 260 262 261 263 - A one-sentence description of the function, followed by a blank … … 265 267 below for format). The type names should be descriptive, but do not 266 268 have to represent the exact Sage/Python types. For example, use 267 269 "integer" for anything that behaves like an integer; you do not 268 have to put a precise type name such as ``int``. 270 have to put a precise type name such as ``int``. The INPUT block 271 describes the expected input to your function or method, while the 272 OUTPUT block describes the expected output of the 273 function/method. If appropriate, you need to describe any default 274 values for the input arguments. For example:: 275 276 INPUT: 277 278 - ``p`` -- (default: 2) a positive prime integer. 279 280 OUTPUT: 281 282 A 5-tuple consisting of integers in this order: 283 284 1. the smallest primitive root modulo p 285 2. the smallest prime primitive root modulo p 286 3. the largest primitive root modulo p 287 4. the largest prime primitive root modulo p 288 5. total number of prime primitive roots modulo p 289 290 Some people prefer to format their OUTPUT section as a block by 291 using a dash. That is acceptable as well:: 292 293 OUTPUT: 294 295 - The plaintext resulting from decrypting the ciphertext ``C`` 296 using the Blum-Goldwasser decryption algorithm. 269 297 270 298 - Instead of INPUT and OUTPUT blocks, you can include descriptions of 271 299 the arguments and output using Sphinx/ReST markup, as described in … … 279 307 280 308 - An ALGORITHM block (optional) which indicates what software 281 309 and/or what algorithm is used. For example 282 ``ALGORITHM: Uses Pari``. 310 ``ALGORITHM: Uses Pari``. Here's a longer example that describes an 311 algorithm used. Note that it also cites the reference where this 312 algorithm can be found:: 313 314 ALGORITHM: 315 316 The following algorithm is adapted from page 89 of [Nat2000]_. 317 318 Let `p` be an odd (positive) prime and let `g` be a generator 319 modulo `p`. Then `g^k` is a generator modulo `p` if and only if 320 `\gcd(k, p-1) = 1`. Since `p` is an odd prime and positive, then 321 `p - 1` is even so that any even integer between 1 and `p - 1`, 322 inclusive, is not relatively prime to `p - 1`. We have now 323 narrowed our search to all odd integers `k` between 1 and `p - 1`, 324 inclusive. 325 326 So now start with a generator `g` modulo an odd (positive) prime 327 `p`. For any odd integer `k` between 1 and `p - 1`, inclusive, 328 `g^k` is a generator modulo `p` if and only if `\gcd(k, p-1) = 1`. 329 330 REFERENCES: 331 332 .. [Nat2000] M.B. Nathanson. Elementary Methods in Number Theory. 333 Springer, 2000. 334 335 You can also number the steps in your algorithm using the hash-dot 336 symbol. This way, the actual numbering of the steps are 337 automatically taken care of when you build the documentation:: 338 339 ALGORITHM: 340 341 The Blum-Goldwasser decryption algorithm is described in Algorithm 342 8.56, page 309 of [MenezesEtAl1996]_. The algorithm works as follows: 343 344 #. Let `C` be the ciphertext `C = (c_1, c_2, \dots, c_t, x_{t+1})`. 345 Then `t` is the number of ciphertext sub-blocks and `h` is the 346 length of each binary string sub-block `c_i`. 347 #. Let `(p, q, a, b)` be the private key whose corresponding 348 public key is `n = pq`. Note that `\gcd(p, q) = ap + bq = 1`. 349 #. Compute `d_1 = ((p + 1) / 4)^{t+1} \bmod{(p - 1)}`. 350 #. Compute `d_2 = ((q + 1) / 4)^{t+1} \bmod{(q - 1)}`. 351 #. Let `u = x_{t+1}^{d_1} \bmod p`. 352 #. Let `v = x_{t+1}^{d_2} \bmod q`. 353 #. Compute `x_0 = vap + ubq \bmod n`. 354 #. For `i` from 1 to `t`, do: 355 356 #. Compute `x_i = x_{t-1}^2 \bmod n`. 357 #. Let `p_i` be the `h` least significant bits of `x_i`. 358 #. Compute `m_i = p_i \oplus c_i`. 359 360 #. The plaintext is `m = m_1 m_2 \cdots m_t`. 283 361 284 362 - A NOTES block for special notes (optional). Include information 285 such as purpose etc. 363 such as purpose etc. A NOTES block should start with 364 ``.. NOTE::``. You can also use the lower-case version 365 ``.. note::``, but do not mix lower-case with upper-case. However, 366 you are encouraged to use the upper-case version ``.. NOTE::``. If 367 you want to put anything within the NOTES block, you should 368 indent it at least 4 spaces (no tabs). Here's an example of a NOTES 369 block:: 370 371 .. NOTE:: 372 373 You should note that this sentence is indented at least 4 374 spaces. Avoid tab characters as much as possible when 375 writing code or editing the Sage documentation. You should 376 follow Python conventions by using spaces only. 377 378 - A WARNING block for critical information about your code. For 379 example, the WARNING block might include information about when or 380 under which conditions your code might break, or information that 381 the user should be particularly aware of. A WARNING block should start 382 with ``.. WARNING::``. It can also be the lower-case form 383 ``.. warning::``. However, you are encouraged to use the upper-case 384 form ``.. WARNING::``. Here's an example of a WARNING block:: 385 386 .. WARNING:: 387 388 Whenever you edit the Sage documentation, make sure that 389 the edited version still builds. That is, you need to ensure 390 that you can still build the HTML and PDF versions of the 391 updated documentation. If the edited documentation fails to 392 build, it is very likely that you would be requested to 393 change your patch. 286 394 287 395 - A REFERENCES block to list books or papers (optional). This block serves 288 a similar purpose to a list of references in a research paper, or a 396 a similar purpose to a list of references in a research paper, or a 289 397 bibliography in a monograph. If your method, function or class uses an 290 398 algorithm that can be found in a standard reference, you should list 291 399 that reference under this block. The Sphinx/ReST markup for … … 303 411 def point(self, x=1, y=2): 304 412 r""" 305 413 This function returns the point `(x^5,y)`. 306 414 307 415 INPUT: 308 416 309 417 - ``x`` - integer (default: 1) the description of the 310 418 argument x goes here. If it contains multiple lines, all 311 419 the lines after the first need to be indented. 312 420 313 421 - ``y`` - integer (default: 2) the ... 314 422 315 423 OUTPUT: 316 424 317 425 integer -- the ... 318 426 319 427 EXAMPLES: 320 428 321 429 This example illustrates ... … … 325 433 sage: A = ModuliSpace() 326 434 sage: A.point(2,3) 327 435 xxx 328 436 329 437 We now ... 330 438 331 439 :: 332 440 333 441 sage: B = A.point(5,6) 334 442 sage: xxx 335 443 336 444 It is an error to ...:: 337 445 338 446 sage: C = A.point('x',7) 339 447 Traceback (most recent call last): 340 448 ... 341 449 TypeError: unable to convert x (=r) to an integer 342 450 343 451 NOTES: 344 452 345 453 This function uses the algorithm of [BCDT]_ to determine 346 454 whether an elliptic curve E over Q is modular. 347 455 348 456 ... 349 457 350 458 REFERENCES: 351 459 352 460 .. [BCDT] Breuil, Conrad, Diamond, Taylor, "Modularity ...." 353 354 AUTHORS: 355 461 462 AUTHORS: 463 356 464 - William Stein (2005-01-03) 357 465 358 466 - First_name Last_name (yyyy-mm-dd) 359 467 """ 360 468 <body of the function> … … 365 473 def point(self, x=1, y=2): 366 474 r""" 367 475 This function returns the point `(x^5,y)`. 368 476 369 477 :param x: the description of the argument x goes here. 370 478 If it contains multiple lines, all the lines after the 371 479 first need to be indented. … … 392 500 """ 393 501 Returns `\\cos(x)`. 394 502 """ 395 503 396 504 def sin(x): 397 505 r""" 398 506 Returns `\sin(x)`. 399 507 """ 400 508 509 You can also use the MATH block to format complicated mathematical 510 expressions:: 511 512 .. MATH:: 513 514 \sum_{i=1}^{\infty} (a_1 a_2 \cdots a_i)^{1/i} 515 \leq 516 e \sum_{i=1}^{\infty} a_i 517 401 518 .. note:: 402 519 403 520 In ReST documentation, you use backticks \` to mark LaTeX code … … 472 589 Create a Hill cryptosystem defined by the `m` x `m` matrix space 473 590 over `\mathbf{Z} / N \mathbf{Z}`, where `N` is the alphabet size of 474 591 the string monoid ``S``. 475 592 476 593 INPUT: 477 594 478 595 - ``S`` - a string monoid over some alphabet 479 596 480 597 - ``m`` - integer `> 0`; the block length of matrices that specify 481 598 block permutations 482 599 483 600 OUTPUT: 484 601 485 602 - A Hill cryptosystem of block length ``m`` over the alphabet ``S``. 486 603 487 604 EXAMPLES:: 488 605 489 606 sage: S = AlphabeticStrings() 490 607 sage: E = HillCryptosystem(S,3) 491 608 sage: E … … 508 625 (Before Sage 3.4, the reference manual used to include methods 509 626 starting with underscores, so you will probably find many examples 510 627 in the code which don't follow this advice...) 511 628 512 629 513 630 Automatic Testing 514 631 ----------------- … … 517 634 that if the above code is in the file ``f.py`` (or 518 635 ``f.sage``), then ``sage -t f.py`` should not give any 519 636 error messages. Testing occurs with full Sage preparsing of input 520 within the standard Sage shell environment, as described in 637 within the standard Sage shell environment, as described in 521 638 :ref:`section-preparsing`. **Important:** The file ``f.py`` is not imported 522 639 when running tests unless you have arranged that it be imported 523 640 into your Sage environment, i.e., unless its functions are … … 550 667 - All input is preparsed before being passed to Python, e.g., 551 668 ``2/3`` is replaced by ``Integer(2)/Integer(3)``, which evaluates 552 669 to ``2/3`` as a rational instead of the Python int 553 ``0``. For more information on preparsing, see 670 ``0``. For more information on preparsing, see 554 671 :ref:`section-preparsing`. 555 672 556 673 - If a test outputs to a file, the file should be in a temporary … … 559 676 560 677 sage: fig.savefig(os.path.join(SAGE_TMP, 'test.png')) 561 678 562 Here ``fig.savefig`` is the function doing the saving, 679 Here ``fig.savefig`` is the function doing the saving, 563 680 ``SAGE_TMP`` is a temporary directory -- this variable will always 564 681 be defined properly during automated testing -- and ``os.path.join`` is 565 682 the preferred way to construct a path from a directory and a file: … … 634 751 sage: E = EllipticCurve(GF(41),[2,5]) 635 752 sage: E._magma_init_() # optional - requires Magma 636 753 'EllipticCurve([...|GF(41)!0,GF(41)!0,GF(41)!0,GF(41)!2,GF(41)!5])' 637 754 638 755 639 756 - If the entire documentation string contains all three words 640 757 ``optional``, ``package``, and ``installed``, … … 674 791 675 792 :: 676 793 677 sage -t [--verbose] [--optional] [files and directories ... ] 794 sage -t [--verbose] [--optional] [files and directories ... ] 678 795 679 796 When you run ``sage -t <filename.py>``, Sage makes a copy of 680 797 ``<filename.py>`` with all the ``sage`` prompts … … 718 835 :: 719 836 720 837 sage: a = 1 721 838 722 839 723 840 Next we add 1 to ``a``. 724 841 … … 728 845 729 846 sage: 1 + a 730 847 2 731 848 732 849 You can also put ``.. skip::`` right before a verbatim 733 850 environment to have that example skipped when testing the file. This 734 851 goes right in the same place as the ``.. link::`` in the previous 735 example. 852 example. 736 853 737 854 See the files in ``SAGE_ROOT/devel/sage/doc/en/tutorial/`` for many 738 855 examples of how to include automated testing in ReST documentation
