![]() |
Python_2
1.0
|
Class for generating and applying pascal triangles on some applications, such as probability and statistics. More...
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... | |
Class for generating and applying pascal triangles on some applications, such as probability and statistics.
def _05e_pascal.Pascal.__init__ | ( | self, | |
first_level, | |||
last_level, | |||
newline = '\n' , |
|||
center = True |
|||
) |
Create a binomial array as a list of lines.
def _05e_pascal.Pascal.__repr__ | ( | self | ) |
Creates the pascal triangle between two levels.
References _05e_pascal.Pascal.center, _05e_pascal.Pascal.first_level, _05e_pascal.Pascal.fmtRow(), _05e_pascal.Pascal.getNextRow(), _05e_pascal.Pascal.last_level, and _05e_pascal.Pascal.lineFeed.
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.
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.
def _05e_pascal.Pascal.binomial_coefficient | ( | self, | |
n, | |||
m, | |||
method = 0 |
|||
) |
Compute \(n\) choose \(m\) or Combination \((n,m)\).
n | number of objects to choose from. |
m | number of places. |
method | how to get the result. |
References _05e_pascal.Pascal.binomial_array.
Referenced by _05e_pascal.Pascal.binomial_cumulative_distribution(), and _05e_pascal.Pascal.binomial_expansion().
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 | probability of success. |
n | number of trials. |
x | number of successes. |
References _05e_pascal.Pascal.binomial_coefficient().
def _05e_pascal.Pascal.binomial_expansion | ( | self, | |
e, | |||
html = False |
|||
) |
Compute the binomial expansion of \((x+y)^n\).
For e = 5, return:
e | exponent n. |
html | exponent creation method. True for html. |
References _05e_pascal.Pascal.binomial_coefficient(), and _05e_pascal.Pascal.exponent().
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
n | number of lines. |
def _05e_pascal.Pascal.exponent | ( | self, | |
val, | |||
type = False |
|||
) |
Return a superscript string for the given value.
val | a numeric string. |
type | select the superscript method. |
References _05e_pascal.Pascal.superscript.
Referenced by _05e_pascal.Pascal.binomial_expansion().
def _05e_pascal.Pascal.fmtRow | ( | self, | |
r, | |||
pad = True |
|||
) |
Formats a row of the Pascal triangle.
r | row to be printed. |
pad | whether to pad the row with blanks to the left. |
References _05e_pascal.Pascal.last_level.
Referenced by _05e_pascal.Pascal.__repr__(), and _05e_pascal.Pascal.__str__().
def _05e_pascal.Pascal.getNextRow | ( | self, | |
curr_row | |||
) |
Computes the next row of a Pascal triangle, given the current row.
curr_row | current row. |
Referenced by _05e_pascal.Pascal.__repr__().
_05e_pascal.Pascal.binomial_array |
Binomial array.
Referenced by _05e_pascal.Pascal.__str__(), and _05e_pascal.Pascal.binomial_coefficient().
_05e_pascal.Pascal.center |
whether to center the lines of the triangle.
Referenced by _05e_pascal.Pascal.__repr__(), and _05e_pascal.Pascal.__str__().
_05e_pascal.Pascal.first_level |
first level of the triangle to be shown.
Referenced by _05e_pascal.Pascal.__repr__(), and _05e_pascal.Pascal.__str__().
_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().
_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__().
_05e_pascal.Pascal.subscript |
translation table for creating subscript.
_05e_pascal.Pascal.superscript |
translation table for creating superscript.
Referenced by _05e_pascal.Pascal.exponent().