Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168695
Image: ubuntu2004
pi.n(1000)
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127
for i in [1..15]: x = i^2 y = i^3 if is_even(i): print i, x, y print "done"
2 4 8 4 16 64 6 36 216 8 64 512 10 100 1000 12 144 1728 14 196 2744 done
L = range(15)
L.append(15)
L
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
for i in [1..100]: if is_prime(i): print i
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
binomial(100,25)*binomial(75,
713641766177019511629053851682159814620118240
factorial(100)/factorial(25)/factorial(35)/factorial(40)
713641766177019511629053851682159814620118240
multinomial?

File: /sagenb/sage_install/sage-4.8-sage.math.washington.edu-x86_64-Linux/local/lib/python2.6/site-packages/sage/rings/arith.py

Type: <type ‘function’>

Definition: multinomial(*ks)

Docstring:

Return the multinomial coefficient

INPUT:

  • An arbitrary number of integer arguments k_1,\dots,k_n
  • A list of integers [k_1,\dots,k_n]

OUTPUT:

Returns the integer:

\binom{k_1 + \cdots + k_n}{k_1, \cdots, k_n} =\frac{\left(\sum_{i=1}^n k_i\right)!}{\prod_{i=1}^n k_i!} = \prod_{i=1}^n \binom{\sum_{j=1}^i k_j}{k_i}

EXAMPLES:

sage: multinomial(0, 0, 2, 1, 0, 0)
3
sage: multinomial([0, 0, 2, 1, 0, 0])
3
sage: multinomial(3, 2)
10
sage: multinomial(2^30, 2, 1)
618970023101454657175683075
sage: multinomial([2^30, 2, 1])
618970023101454657175683075

AUTHORS:

  • Gabriel Ebner
1032480*3018911
3116965229280
1038000*10010888
10391301744000
factorial(10)
3628800
3116965229280*10391301744000
32389326223004623864320000
S=['a','b','c','d', 'e','f']
S
['a', 'b', 'c', 'd', 'e', 'f']
for x in S: for y in S: for z in S: print(x+y+z)
WARNING: Output truncated!
aaa aab aac aad aae aaf aba abb abc abd abe abf aca acb acc acd ace acf ada adb adc add ade adf aea aeb aec aed aee aef afa afb afc afd afe aff baa bab bac bad bae baf bba bbb bbc bbd bbe bbf bca bcb bcc bcd bce bcf bda bdb bdc bdd bde ... eca ecb ecc ecd ece ecf eda edb edc edd ede edf eea eeb eec eed eee eef efa efb efc efd efe eff faa fab fac fad fae faf fba fbb fbc fbd fbe fbf fca fcb fcc fcd fce fcf fda fdb fdc fdd fde fdf fea feb fec fed fee fef ffa ffb ffc ffd ffe fff
S = ['a','b','c','d','e','f'] n=0 for x in [0..3]: for y in [x+1..4]: for z in [y+1..5]: n=n+1 print(n,S[x]+S[y]+S[z])
(1, 'abc') (2, 'abd') (3, 'abe') (4, 'abf') (5, 'acd') (6, 'ace') (7, 'acf') (8, 'ade') (9, 'adf') (10, 'aef') (11, 'bcd') (12, 'bce') (13, 'bcf') (14, 'bde') (15, 'bdf') (16, 'bef') (17, 'cde') (18, 'cdf') (19, 'cef') (20, 'def')
function?

File: /sagenb/sage_install/sage-4.8-sage.math.washington.edu-x86_64-Linux/devel/sage/sage/calculus/var.pyx

Type: <type ‘builtin_function_or_method’>

Definition: function(s, *args, **kwds)

Docstring:

Create a formal symbolic function with the name s.

INPUT:

  • s - a string, either a single variable name, or a space or comma separated list of variable names.

  • **kwds - keyword arguments. Either one of the following two

    keywords can be used to customize latex representation of symbolic functions:

    1. latex_name=LaTeX where LaTeX is any valid latex expression. Ex: f = function(‘f’, x, latex_name=”mathcal{F}”) See EXAMPLES for more.
    2. print_latex_func=my_latex_print where my_latex_print is any callable function that returns a valid latex expression. Ex: f = function(‘f’, x, print_latex_func=my_latex_print) See EXAMPLES for an explicit usage.

Note

The new function is both returned and automatically injected into the global namespace. If you use this function in library code, it is better to use sage.symbolic.function_factory.function, since it won’t touch the global namespace.

EXAMPLES:

We create a formal function called supersin:

sage: f = function('supersin', x)
sage: f
supersin(x)

We can immediately use supersin in symbolic expressions:

sage: y, z, A = var('y z A')
sage: supersin(y+z) + A^3
A^3 + supersin(y + z)

We can define other functions in terms of supersin:

sage: g(x,y) = supersin(x)^2 + sin(y/2)
sage: g
(x, y) |--> supersin(x)^2 + sin(1/2*y)
sage: g.diff(y)
(x, y) |--> 1/2*cos(1/2*y)
sage: k = g.diff(x); k
(x, y) |--> 2*supersin(x)*D[0](supersin)(x)

Custom typesetting of symbolic functions in LaTeX:

(1) Either using latex_name keyword::
sage: riemann(x) = function(‘riemann’, x, latex_name=”mathcal{R}”) sage: latex(riemann(x)) mathcal{R}left(xright)
  1. Or passing a custom callable function that returns a latex expression:

    sage: mu,nu = var(‘mu,nu’) sage: def my_latex_print(self, *args): return “psi_{%s}”%(‘, ‘.join(map(latex, args))) sage: psi(mu,nu) = function(‘psi’, mu, nu, print_latex_func=my_latex_print) sage: latex(psi(mu,nu)) psi_{mu, nu}

In Sage 4.0, you must now use the substitute_function() method to replace functions:

sage: k.substitute_function(supersin, sin)
2*sin(x)*cos(x)
default_mip_solver
No object 'def' currently defined.
def nx(perm): return perm^2
def perm2inv(p): n = len(p) a = [0]*n for i in [1..n]: j=0 while p[j] != i: if p[j] > i : a[i-1]=a[i-1]+1 j=j+1 return a
print perm2inv([5,4,3,2,1])
[4, 3, 2, 1, 0]
perm2inv([1,2,6,4,5,3])
[0, 0, 3, 1, 1, 0]
L = [1,2,3]
L.remove(2)
print L
[1, 3]
def inv2perm(i): n = len(i) p = [0]*n j = range(n) for k in range(n): p[j[i[k]]] = k+1 j.remove(j[i[k]]) return p
inv2perm([3,2,1,0])
[4, 3, 2, 1]
inv2perm([0,0,0,0])
[1, 2, 3, 4]
def next_inversion(inv): n = len(inv) jnv = inv for i in range(n): if inv[n-i-1] < i: jnv[n-i-1] = jnv[n-i-1] + 1 for k in [n-i..n-1]: jnv[k] = 0 break else: return "done" return jnv
next_inversion([0,0,0,0,0])
[0, 0, 0, 1, 0]
next_inversion([4,3,2,1,0])
'done'
a = [0,0,0,0,0] while a != 'done': p = inv2perm(a) print a, p a = next_inversion(a)
WARNING: Output truncated!
[0, 0, 0, 0, 0] [1, 2, 3, 4, 5] [0, 0, 0, 1, 0] [1, 2, 3, 5, 4] [0, 0, 1, 0, 0] [1, 2, 4, 3, 5] [0, 0, 1, 1, 0] [1, 2, 5, 3, 4] [0, 0, 2, 0, 0] [1, 2, 4, 5, 3] [0, 0, 2, 1, 0] [1, 2, 5, 4, 3] [0, 1, 0, 0, 0] [1, 3, 2, 4, 5] [0, 1, 0, 1, 0] [1, 3, 2, 5, 4] [0, 1, 1, 0, 0] [1, 4, 2, 3, 5] [0, 1, 1, 1, 0] [1, 5, 2, 3, 4] [0, 1, 2, 0, 0] [1, 4, 2, 5, 3] [0, 1, 2, 1, 0] [1, 5, 2, 4, 3] [0, 2, 0, 0, 0] [1, 3, 4, 2, 5] [0, 2, 0, 1, 0] [1, 3, 5, 2, 4] [0, 2, 1, 0, 0] [1, 4, 3, 2, 5] [0, 2, 1, 1, 0] [1, 5, 3, 2, 4] [0, 2, 2, 0, 0] [1, 4, 5, 2, 3] [0, 2, 2, 1, 0] [1, 5, 4, 2, 3] [0, 3, 0, 0, 0] [1, 3, 4, 5, 2] [0, 3, 0, 1, 0] [1, 3, 5, 4, 2] [0, 3, 1, 0, 0] [1, 4, 3, 5, 2] [0, 3, 1, 1, 0] [1, 5, 3, 4, 2] [0, 3, 2, 0, 0] [1, 4, 5, 3, 2] [0, 3, 2, 1, 0] [1, 5, 4, 3, 2] [1, 0, 0, 0, 0] [2, 1, 3, 4, 5] [1, 0, 0, 1, 0] [2, 1, 3, 5, 4] [1, 0, 1, 0, 0] [2, 1, 4, 3, 5] [1, 0, 1, 1, 0] [2, 1, 5, 3, 4] [1, 0, 2, 0, 0] [2, 1, 4, 5, 3] [1, 0, 2, 1, 0] [2, 1, 5, 4, 3] [1, 1, 0, 0, 0] [3, 1, 2, 4, 5] [1, 1, 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