8000 Added an 'elementary' function. by mttpgn · Pull Request #4573 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

Added an 'elementary' function. #4573

New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Prev Previous commit
Next Next commit
Tightened up code per github suggestions.
  • Loading branch information
Matt Pagan committed Apr 9, 2014
commit 0e10a449ee2c483594d4d1303f0313eba91b9ae5
41 changes: 21 additions & 20 deletions numpy/lib/twodim_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1000,22 +1000,22 @@ def triu_indices_from(arr, k=0):
return triu_indices(arr.shape[-2], k=k, m=arr.shape[-1])


def elementary(N, i, j=None, t=None, dtype=float):
def elementary(N, i, j=None, multiplier=None, dtype=float):
"""
Return an array arranged like an elementary matrix.

Parameters
----------
N : int
The size of the NxN array to be returned. Elementary matrices
are be square.
are square by definition.
i : int
The index of the first row on which operations are to be
performed.
j : int
If set, the index of the second row on which operations are to
be performed.
t : scalar
multiplier : scalar
If set, the factor by which a given row will be multiplied.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason not to document the dtype arg?

Returns
Expand All @@ -1028,8 +1028,12 @@ def elementary(N, i, j=None, t=None, dtype=float):
--------
eye, identity

Notes
-----
This function's primary utility is in matrix multiplication.

Examples
-------
--------
To swap the the first and third rows of a 4x4 identity matirx:

>>> L = elementary(4, 0, 2)
Expand All @@ -1046,7 +1050,7 @@ def elementary(N, i, j=None, t=None, dtype=float):
[11, 13, 17, 19],
[23, 29, 31, 37],
[41, 43, 47, 53]])
>>> L*H
>>> L * H
matrix([[ 23., 29., 31., 37.],
[ 11., 13., 17., 19.],
[ 2., 3., 5., 7.],
Expand All @@ -1056,7 +1060,7 @@ def elementary(N, i, j=None, t=None, dtype=float):
multiplication takes place in reverse order), the result is the
given matrix with its first and third columns swapped.

>>> H*L
>>> H * L
matrix([[ 5., 3., 2., 7.],
[ 17., 13., 11., 19.],
[ 31., 29., 23., 37.],
Expand All @@ -1070,13 +1074,13 @@ def elementary(N, i, j=None, t=None, dtype=float):
[11, 13, 17, 19],
[23, 29, 31, 37],
[41, 43, 47, 53]])
>> M=elementary(4, 1, 3, 10000, int)
>> M = elementary(4, 1, 3, 10000, int)
>> M
array([[ 1, 0, 0, 0],
[ 0, 1, 0, 0],
[ 0, 0, 1, 0],
[ 0, 10000, 0, 1]])
>> M*H
>> M * H
matrix([[ 2, 3, 5, 7],
[ 11, 13, 17, 19],
[ 23, 29, 31, 37],
Expand All @@ -1089,7 +1093,7 @@ def elementary(N, i, j=None, t=None, dtype=float):
[11, 13, 17, 19],
[23, 29, 31, 37],
[41, 43, 47, 53]])
>>> H*elementary(len(H), len(H)-1, t=2)
>>> H * elementary(len(H), len(H)-1, multiplier=2)
matrix([[ 2., 3., 5., 14.],
[ 11., 13., 17., 38.],
[ 23., 29., 31., 74.],
Expand All @@ -1099,18 +1103,15 @@ def elementary(N, i, j=None, t=None, dtype=float):
of elementary matrices.

"""
m=eye(N, dtype=dtype)
if j==None and t==None:
raise ValueError("One or more of {0} and {1} must be set.".format( \
'j', 't'))
elif t==None:
swap = array(m[i])
m[i] = m[j]
m[j] = swap
m = eye(N, dtype=dtype)
if j is None and multiplier is None:
raise ValueError("One or more of 'j' and 'multiplier' must be set.")
elif multiplier is None:
m[i], m[j] = m[j], m[i]
return m
elif j==None:
m[i] *= t
elif j is None:
m[i] *= multiplier
return m
else:
m[j] += (t * m[i])
m[j] += (multiplier * m[i])
return m
0