Python_2  1.0
Public Member Functions | Public Attributes | List of all members
_05e_pascal.Pascal Class Reference

Class for generating and applying pascal triangles on some applications, such as probability and statistics. More...

Inheritance diagram for _05e_pascal.Pascal:
Inheritance graph
[legend]
Collaboration diagram for _05e_pascal.Pascal:
Collaboration graph
[legend]

Public Member Functions

def __init__ (self, first_level, last_level, newline='\n', center=True)
 Create a binomial array as a list of lines. More...
 
def binomialArray (self, n)
 Compute n lines of the binomial array. More...
 
def binomial_coefficient (self, n, m, method=0)
 Compute \(n\) choose \(m\) or Combination \((n,m)\). More...
 
def binomial_cumulative_distribution (self, 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 (self, val, type=False)
 Return a superscript string for the given value. More...
 
def binomial_expansion (self, e, html=False)
 Compute the binomial expansion of \((x+y)^n\). More...
 
def getNextRow (self, curr_row)
 Computes the next row of a Pascal triangle, given the current row. More...
 
def fmtRow (self, r, pad=True)
 Formats a row of the Pascal triangle. More...
 
def __repr__ (self)
 Creates the pascal triangle between two levels. More...
 
def __str__ (self)
 Creates the pascal triangle between two levels, using the binomial array. More...
 

Public Attributes

 first_level
 first level of the triangle to be shown. More...
 
 last_level
 last level of the triangle to be shown. More...
 
 center
 whether to center the lines of the triangle. More...
 
 lineFeed
 string used to signify the end of a line of text and the start of a new one. More...
 
 superscript
 translation table for creating superscript. More...
 
 subscript
 translation table for creating subscript. More...
 
 binomial_array
 Binomial array. More...
 

Detailed Description

Class for generating and applying pascal triangles on some applications, such as probability and statistics.

Constructor & Destructor Documentation

◆ __init__()

def _05e_pascal.Pascal.__init__ (   self,
  first_level,
  last_level,
  newline = '\n',
  center = True 
)

Create a binomial array as a list of lines.

Member Function Documentation

◆ __repr__()

def _05e_pascal.Pascal.__repr__ (   self)

◆ __str__()

def _05e_pascal.Pascal.__str__ (   self)

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

The lines are centered in respect to the last line of the triangle.

Returns
a string with the lines of this pascal triangle.

References _05e_pascal.Pascal.binomial_array, _05e_pascal.Pascal.center, _05e_pascal.Pascal.first_level, _05e_pascal.Pascal.fmtRow(), _05e_pascal.Pascal.last_level, and _05e_pascal.Pascal.lineFeed.

◆ binomial_coefficient()

def _05e_pascal.Pascal.binomial_coefficient (   self,
  n,
  m,
  method = 0 
)

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 _05e_pascal.Pascal.binomial_array.

Referenced by _05e_pascal.Pascal.binomial_cumulative_distribution(), and _05e_pascal.Pascal.binomial_expansion().

◆ binomial_cumulative_distribution()

def _05e_pascal.Pascal.binomial_cumulative_distribution (   self,
  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 _05e_pascal.Pascal.binomial_coefficient().

◆ binomial_expansion()

def _05e_pascal.Pascal.binomial_expansion (   self,
  e,
  html = False 
)

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.
htmlexponent creation method. True for html.
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 _05e_pascal.Pascal.binomial_coefficient(), and _05e_pascal.Pascal.exponent().

◆ binomialArray()

def _05e_pascal.Pascal.binomialArray (   self,
  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.

◆ exponent()

def _05e_pascal.Pascal.exponent (   self,
  val,
  type = False 
)

Return a superscript string for the given value.

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

References _05e_pascal.Pascal.superscript.

Referenced by _05e_pascal.Pascal.binomial_expansion().

◆ fmtRow()

def _05e_pascal.Pascal.fmtRow (   self,
  r,
  pad = True 
)

Formats a row of the Pascal triangle.

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

References _05e_pascal.Pascal.last_level.

Referenced by _05e_pascal.Pascal.__repr__(), and _05e_pascal.Pascal.__str__().

◆ getNextRow()

def _05e_pascal.Pascal.getNextRow (   self,
  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 _05e_pascal.Pascal.__repr__().

Member Data Documentation

◆ binomial_array

_05e_pascal.Pascal.binomial_array

◆ center

_05e_pascal.Pascal.center

whether to center the lines of the triangle.

Referenced by _05e_pascal.Pascal.__repr__(), and _05e_pascal.Pascal.__str__().

◆ first_level

_05e_pascal.Pascal.first_level

first level of the triangle to be shown.

Referenced by _05e_pascal.Pascal.__repr__(), and _05e_pascal.Pascal.__str__().

◆ last_level

_05e_pascal.Pascal.last_level

last level of the triangle to be shown.

Referenced by _05e_pascal.Pascal.__repr__(), _05e_pascal.Pascal.__str__(), and _05e_pascal.Pascal.fmtRow().

◆ lineFeed

_05e_pascal.Pascal.lineFeed

string used to signify the end of a line of text and the start of a new one.

Referenced by _05e_pascal.Pascal.__repr__(), and _05e_pascal.Pascal.__str__().

◆ subscript

_05e_pascal.Pascal.subscript

translation table for creating subscript.

◆ superscript

_05e_pascal.Pascal.superscript

translation table for creating superscript.

Referenced by _05e_pascal.Pascal.exponent().


The documentation for this class was generated from the following file: