Python  1.0
Functions | Variables
_03d_pascal Namespace Reference

Showing a range of rows of a Pascal triangle. More...

Functions

def binomial_array (n)
 Compute n lines of the binomial array. More...
 
def binomial_coefficient (n, m, method=2)
 Compute \(n\) choose \(m\) or Combination \((n,m)\). More...
 
def binomial_cumulative_distribution (p, n, x=None)
 Compute the probability of getting at most \(x\) successes in \(n\) trials, which is the sum of the first \(x\) terms of the binomial expansion \((p+q)^n\). More...
 
def exponent (val, type=False)
 Return a superscript string for the given value. More...
 
def binomial_expansion (e)
 Compute the binomial expansion of \((x+y)^n\). More...
 
def getNextRow (curr_row)
 Computes the next row of a Pascal triangle, given the current row. More...
 
def fmtRow (last_level, r, pad=True)
 Formats a row of the Pascal triangle. More...
 
def pascal (first_level, last_level, center=True)
 Creates the pascal triangle between two levels. More...
 
def pascal2 (first_level, last_level, center=True)
 Creates the pascal triangle between two levels, using the binomial array. More...
 
def main (argv=None)
 Main function for testing. More...
 

Variables

dictionary superscript_map
 dictionary for mapping characters to superscript. More...
 
dictionary subscript_map
 
 trans
 character translation table. More...
 
 sub_trans
 

Detailed Description

Showing a range of rows of a Pascal triangle.

   0       1                                 C(0,0)
   1      1 1                            C(1,0)  C(1,1)
   2     1 2 1                        C(2,0)  C(2,1)  C(2,2)
   3    1 3 3 1                   C(3,0)  C(3,1)  C(3,2)  C(3,3)
   4   1 4 6 4 1              C(4,0)  C(4,1)  C(4,2)  C(4,3)  C(4,4)
   5  1 5 10 10 5 1       C(5,0)  C(5,1)  C(5,2)  C(5,3)  C(5,4)  C(5,5)
      0 1 2  3  4 5
  
Author
Paulo Roma
Since
13/04/2009
See also
http://en.wikipedia.org/wiki/Pascal_triangle

Function Documentation

◆ binomial_array()

def _03d_pascal.binomial_array (   n)

Compute n lines of the binomial array.

Complexity is \(1 + 2\ +\ ...\ +\ n\) elements. This is arithmetic progression that sums to \({n*(n+1)}\over{2}\), which is in \(O(n^2)\).

   0 - 1
   1 - 1 1
   2 - 1 2 1
   3 - 1 3 3 1
   4 - 1 4 6 4 1
   5 - 1 5 10 10 5 1
   
Parameters
nnumber of lines.
Returns
binomial array as a list of lines.

Referenced by binomial_coefficient(), binomial_cumulative_distribution(), binomial_expansion(), and pascal2().

◆ binomial_coefficient()

def _03d_pascal.binomial_coefficient (   n,
  m,
  method = 2 
)

Compute \(n\) choose \(m\) or Combination \((n,m)\).

Parameters
nnumber of objects to choose from.
mnumber of places.
methodhow to get the result.
Returns
\(C{{n}\choose{m}} = {{n!} \over {(n-m)!\ m!}} = {{n (n-1) ... (n-m+1)} \over {m!}}\), number of ways of choosing m objects from n.
See also
https://en.wikipedia.org/wiki/Combination
https://docs.scipy.org/doc/scipy/reference/tutorial/special.html
https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.comb.html#scipy.special.comb

References binomial_array().

◆ binomial_cumulative_distribution()

def _03d_pascal.binomial_cumulative_distribution (   p,
  n,
  x = None 
)

Compute the probability of getting at most \(x\) successes in \(n\) trials, which is the sum of the first \(x\) terms of the binomial expansion \((p+q)^n\).

In other words, the probability of obtaining at most \(x\) successes in \(n\) independent trials, each of which has a probability \(p\) of success, and \(q = (1-p)\) of failure.

That is, if \(X\) denotes the number of successes, it returns:

  • \(P(X \le x) = \sum_{i=0}^{x} {C {{n}\choose{i}}\ p^i\ (1-p)^{n-i}}\).
Parameters
pprobability of success.
nnumber of trials.
xnumber of successes.
Returns
the probability of getting at most x successes in n independent trials.
See also
https://www.khanacademy.org/math/ap-statistics/probability-ap/probability-multiplication-rule/v/getting-at-least-one-heads
http://www.wolframalpha.com/widgets/view.jsp?id=d821210668c6cc5a02db1069cc52464f
https://mat.iitm.ac.in/home/vetri/public_html/statistics/binomial.pdf
https://www.khanacademy.org/math/statistics-probability/random-variables-stats-library/binomial-random-variables/v/visualizing-a-binomial-distribution

References binomial_array().

Referenced by main().

◆ binomial_expansion()

def _03d_pascal.binomial_expansion (   e)

Compute the binomial expansion of \((x+y)^n\).

For e = 5, return:

  • \(x^5 + 5x^4y + 10x^3y^2 + 10x^2y^3 + 5xy^4 + y^5\) (html)
  • x⁵ + 5x⁴y + 10x³y² + 10x²y³ + 5xy⁴ + y⁵ (unicode translation table)
  • x^5 + 5x^4y + 10x^3y^2 + 10x^2y^3 + 5xy^4 + y^5 (None)
Parameters
eexponent n.
Returns
a string representing the expansion.
See also
https://www.khanacademy.org/math/precalculus/prob-comb/prob-combinatorics-precalc/v/exactly-three-heads-in-five-flips
https://www.khanacademy.org/math/precalculus/prob-comb/combinations/v/combination-formula
https://www.khanacademy.org/math/statistics-probability/random-variables-stats-library/binomial-random-variables/v/binomial-distribution

References binomial_array(), and exponent().

Referenced by main().

◆ exponent()

def _03d_pascal.exponent (   val,
  type = False 
)

Return a superscript string for the given value.

Parameters
vala numeric string.
typeselect the superscript method.
Returns
a new superscript string.

Referenced by binomial_expansion().

◆ fmtRow()

def _03d_pascal.fmtRow (   last_level,
  r,
  pad = True 
)

Formats a row of the Pascal triangle.

Parameters
last_levellast_level.
rrow to be printed.
padwhether to pad the row with blanks to the left.
Returns
string representing the row.

Referenced by pascal(), and pascal2().

◆ getNextRow()

def _03d_pascal.getNextRow (   curr_row)

Computes the next row of a Pascal triangle, given the current row.

Parameters
curr_rowcurrent row.
Returns
next_row: list(map(sum, zip([0] + curr_row, curr_row + [0])))

Referenced by pascal().

◆ main()

def _03d_pascal.main (   argv = None)

Main function for testing.

Parameters
argvfirst and last levels of the Pascal triangle.

References binomial_cumulative_distribution(), binomial_expansion(), _01d_dec2bin.input, pascal(), and pascal2().

◆ pascal()

def _03d_pascal.pascal (   first_level,
  last_level,
  center = True 
)

Creates the pascal triangle between two levels.

Parameters
first_levelfirst level to be printed.
last_levelheight of the Pascal triangle.
centerwhether to center the lines.
See also
https://www.cut-the-knot.org/arithmetic/combinatorics/PascalTriangleProperties.shtml
https://www.johndcook.com/blog/2016/07/05/distribution-of-numbers-in-pascals-triangle/

References fmtRow(), and getNextRow().

Referenced by main().

◆ pascal2()

def _03d_pascal.pascal2 (   first_level,
  last_level,
  center = True 
)

Creates the pascal triangle between two levels, using the binomial array.

The lines are centered in respect to the last line of the triangle. The output is written to a docx file.

Parameters
first_levelfirst level to be printed.
last_levelheight of the Pascal triangle.
centerwhether to center the lines.

References binomial_array(), and fmtRow().

Referenced by main().

Variable Documentation

◆ sub_trans

dictionary _03d_pascal.sub_trans
Initial value:
1 = str.maketrans(
2  ''.join(subscript_map.keys()),
3  ''.join(subscript_map.values()))

◆ subscript_map

dictionary _03d_pascal.subscript_map
Initial value:
1 = {
2  "0": "₀", "1": "₁", "2": "₂", "3": "₃", "4": "₄", "5": "₅", "6": "₆",
3  "7": "₇", "8": "₈", "9": "₉", "a": "ₐ", "b": "♭", "c": "꜀", "d": "ᑯ",
4  "e": "ₑ", "f": "բ", "g": "₉", "h": "ₕ", "i": "ᵢ", "j": "ⱼ", "k": "ₖ",
5  "l": "ₗ", "m": "ₘ", "n": "ₙ", "o": "ₒ", "p": "ₚ", "q": "૧", "r": "ᵣ",
6  "s": "ₛ", "t": "ₜ", "u": "ᵤ", "v": "ᵥ", "w": "w", "x": "ₓ", "y": "ᵧ",
7  "z": "₂", "A": "ₐ", "B": "₈", "C": "C", "D": "D", "E": "ₑ", "F": "բ",
8  "G": "G", "H": "ₕ", "I": "ᵢ", "J": "ⱼ", "K": "ₖ", "L": "ₗ", "M": "ₘ",
9  "N": "ₙ", "O": "ₒ", "P": "ₚ", "Q": "Q", "R": "ᵣ", "S": "ₛ", "T": "ₜ",
10  "U": "ᵤ", "V": "ᵥ", "W": "w", "X": "ₓ", "Y": "ᵧ", "Z": "Z", "+": "₊",
11  "-": "₋", "=": "₌", "(": "₍", ")": "₎"}

◆ superscript_map

dictionary _03d_pascal.superscript_map
Initial value:
1 = {
2  "0": "⁰", "1": "¹", "2": "²", "3": "³", "4": "⁴", "5": "⁵", "6": "⁶",
3  "7": "⁷", "8": "⁸", "9": "⁹", "a": "ᵃ", "b": "ᵇ", "c": "ᶜ", "d": "ᵈ",
4  "e": "ᵉ", "f": "ᶠ", "g": "ᵍ", "h": "ʰ", "i": "ᶦ", "j": "ʲ", "k": "ᵏ",
5  "l": "ˡ", "m": "ᵐ", "n": "ⁿ", "o": "ᵒ", "p": "ᵖ", "q": "۹", "r": "ʳ",
6  "s": "ˢ", "t": "ᵗ", "u": "ᵘ", "v": "ᵛ", "w": "ʷ", "x": "ˣ", "y": "ʸ",
7  "z": "ᶻ", "A": "ᴬ", "B": "ᴮ", "C": "ᶜ", "D": "ᴰ", "E": "ᴱ", "F": "ᶠ",
8  "G": "ᴳ", "H": "ᴴ", "I": "ᴵ", "J": "ᴶ", "K": "ᴷ", "L": "ᴸ", "M": "ᴹ",
9  "N": "ᴺ", "O": "ᴼ", "P": "ᴾ", "Q": "Q", "R": "ᴿ", "S": "ˢ", "T": "ᵀ",
10  "U": "ᵁ", "V": "ⱽ", "W": "ᵂ", "X": "ˣ", "Y": "ʸ", "Z": "ᶻ", "+": "⁺",
11  "-": "⁻", "=": "⁼", "(": "⁽", ")": "⁾"}

dictionary for mapping characters to superscript.

◆ trans

dictionary _03d_pascal.trans
Initial value:
1 = str.maketrans(
2  ''.join(superscript_map.keys()),
3  ''.join(superscript_map.values()))

character translation table.