Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

ENH: linalg: add the function pascal() to special_matrices (ticket 632) #122

Closed
wants to merge 3 commits into
from

Conversation

Projects
None yet
2 participants
Member

WarrenWeckesser commented Dec 28, 2011

No description provided.

Owner

rgommers commented Dec 28, 2011

Looks good. A few minor points:

  • why specify n and return matrix of size (n+1, n+1) instead of (n, n)?

  • 2d --> 2-D

  • example should contain correct import, so::

    from scipy import linalg
    linalg.pascal(3)
    <etc>
    
Member

WarrenWeckesser commented Dec 28, 2011

why specify n and return matrix of size (n+1, n+1) instead of (n, n)?

Good question... without a good answer. :) I copied the function from the snippet given in ticket #632, and it evolved from there. I suppose one could argue that 'n' is the power that gives the binomial coefficients (i.e. (x+y)**n) in the last row (when kind='lower') or the anti-diagonal (when kind='symmetric'), but really it just makes the docstring awkward. I'll update the function to return an (n,n) matrix.

@rgommers rgommers commented on the diff Dec 28, 2011

scipy/linalg/special_matrices.py
@@ -687,3 +688,79 @@ def invhilbert(n, exact=False):
if i != j:
invh[j, i] = invh[i, j]
return invh
+
+
+def pascal(n, kind='symmetric', exact=True):
+ """Returns the n x n Pascal matrix.
+
+ The Pascal matrix is a matrix containing the binomial coefficients as
+ its elements.
+
+ Parameters
+ ----------
+ n : int
+ The size of the matrix to create; that is, the result is an n x n
+ matrix.
+ kind : str
@rgommers

rgommers Dec 28, 2011

Owner

Add , optional. Same for exact.

Member

WarrenWeckesser commented Dec 28, 2011

Ralf, thanks for taking a look. I've updated the function in c672b45.

@rgommers rgommers commented on an outdated diff Dec 28, 2011

scipy/linalg/special_matrices.py
+def pascal(n, kind='symmetric', exact=True):
+ """Returns the n x n Pascal matrix.
+
+ The Pascal matrix is a matrix containing the binomial coefficients as
+ its elements.
+
+ Parameters
+ ----------
+ n : int
+ The size of the matrix to create; that is, the result is an n x n
+ matrix.
+ kind : str
+ Must be one of 'symmetric', 'lower', or 'upper'.
+ Default is 'symmetric'.
+ exact : bool
+ If exact is True, the result is either an array of type numpy.uint64
@rgommers

rgommers Dec 28, 2011

Owner

Put exact in backticks, like

 `exact`

Same for scipy.misc.comb

@rgommers rgommers commented on the diff Dec 28, 2011

scipy/linalg/special_matrices.py
+ [ 1, 3, 6, 10],
+ [ 1, 4, 10, 20]], dtype=uint64)
+ >>> pascal(4, kind='lower')
+ array([[1, 0, 0, 0],
+ [1, 1, 0, 0],
+ [1, 2, 1, 0],
+ [1, 3, 3, 1]], dtype=uint64)
+ >>> pascal(50)[-1, -1]
+ 25477612258980856902730428600L
+ >>> from scipy.misc import comb
+ >>> comb(98, 49, exact=True)
+ 25477612258980856902730428600L
+ """
+
+ if kind not in ['symmetric', 'lower', 'upper']:
+ raise ValueError("kind must be 'symmetric', 'lower', or 'upper'")
@rgommers

rgommers Dec 28, 2011

Owner

PEP8 nitpick: need a space here

@rgommers rgommers commented on the diff Dec 28, 2011

scipy/linalg/special_matrices.py
+ if n > 35:
+ L_n = np.empty((n, n), dtype=object)
+ L_n.fill(0L)
+ else:
+ L_n = np.zeros((n, n), dtype=np.uint64)
+ for i in range(n):
+ for j in range(i+1):
+ L_n[i,j] = comb(i, j, exact=True)
+ else:
+ L_n = comb(*np.ogrid[:n, :n])
+ if kind is 'lower':
+ p = L_n
+ elif kind is 'upper':
+ p = L_n.T
+ else:
+ p = np.dot(L_n, L_n.T)
@rgommers

rgommers Dec 28, 2011

Owner

Same here, blank line

Owner

rgommers commented Dec 28, 2011

Few more very minor things (sorry, it's late). You can merge this I think.

Member

WarrenWeckesser commented Dec 28, 2011

Thanks for the quick feedback, Ralf.

Rebased and committed in f798286.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment