# Python numpy.triu() Examples

The following are 30 code examples for showing how to use numpy.triu(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module , or try the search function .

Example 1
```def test_dc_gain_integrator(self):
"""DC gain when eigenvalue at DC returns appropriately sized array of nan."""
# the SISO case is also tested in test_dc_gain_{cont,discr}
import itertools
# iterate over input and output sizes, and continuous (dt=None) and discrete (dt=True) time
for inputs, outputs, dt in itertools.product(range(1, 6), range(1, 6), [None, True]):
states = max(inputs, outputs)

# a matrix that is singular at DC, and has no "useless" states as in
# _remove_useless_states
a = np.triu(np.tile(2, (states, states)))
# eigenvalues all +2, except for ...
a[0, 0] = 0 if dt is None else 1
b = np.eye(max(inputs, states))[:states, :inputs]
c = np.eye(max(outputs, states))[:outputs, :states]
d = np.zeros((outputs, inputs))
sys = StateSpace(a, b, c, d, dt)
dc = np.squeeze(np.tile(np.nan, (outputs, inputs)))
np.testing.assert_array_equal(dc, sys.dcgain()) ```
Example 2
```def test_dc_gain_integrator(self):
"""DC gain when eigenvalue at DC returns appropriately sized array of nan."""
# the SISO case is also tested in test_dc_gain_{cont,discr}
import itertools
# iterate over input and output sizes, and continuous (dt=None) and discrete (dt=True) time
for inputs, outputs, dt in itertools.product(range(1, 6), range(1, 6), [None, True]):
states = max(inputs, outputs)

# a matrix that is singular at DC, and has no "useless" states as in
# _remove_useless_states
a = np.triu(np.tile(2, (states, states)))
# eigenvalues all +2, except for ...
a[0, 0] = 0 if dt is None else 1
b = np.eye(max(inputs, states))[:states, :inputs]
c = np.eye(max(outputs, states))[:outputs, :states]
d = np.zeros((outputs, inputs))
sys = StateSpace(a, b, c, d, dt)
dc = np.squeeze(np.tile(np.nan, (outputs, inputs)))
np.testing.assert_array_equal(dc, sys.dcgain()) ```
Example 3
```def __init__(self,in_channel):
super(InvConv,self).__init__()

weight=np.random.randn(in_channel,in_channel)
q,_=linalg.qr(weight)
w_p,w_l,w_u=linalg.lu(q.astype(np.float32))
w_s=np.diag(w_u)
w_u=np.triu(w_u,1)

self.register_buffer('w_p',torch.from_numpy(w_p))
self.register_buffer('s_sign',torch.sign(torch.from_numpy(w_s)))
self.w_l=torch.nn.Parameter(torch.from_numpy(w_l))
self.w_s=torch.nn.Parameter(torch.log(1e-7+torch.abs(torch.from_numpy(w_s))))
self.w_u=torch.nn.Parameter(torch.from_numpy(w_u))

self.weight=None
self.invweight=None

return ```
Example 4
```def test_tril_triu_ndim3():
for dtype in np.typecodes['AllFloat'] + np.typecodes['AllInteger']:
a = np.array([
[[1, 1], [1, 1]],
[[1, 1], [1, 0]],
[[1, 1], [0, 0]],
], dtype=dtype)
a_tril_desired = np.array([
[[1, 0], [1, 1]],
[[1, 0], [1, 0]],
[[1, 0], [0, 0]],
], dtype=dtype)
a_triu_desired = np.array([
[[1, 1], [0, 1]],
[[1, 1], [0, 0]],
[[1, 1], [0, 0]],
], dtype=dtype)
a_triu_observed = np.triu(a)
a_tril_observed = np.tril(a)
assert_array_equal(a_triu_observed, a_triu_desired)
assert_array_equal(a_tril_observed, a_tril_desired)
assert_equal(a_triu_observed.dtype, a.dtype)
assert_equal(a_tril_observed.dtype, a.dtype) ```
Example 5
```def test_tril_triu_dtype():
# Issue 4916
# tril and triu should return the same dtype as input
for c in np.typecodes['All']:
if c == 'V':
continue
arr = np.zeros((3, 3), dtype=c)
assert_equal(np.triu(arr).dtype, arr.dtype)
assert_equal(np.tril(arr).dtype, arr.dtype)

# check special cases
arr = np.array([['2001-01-01T12:00', '2002-02-03T13:56'],
['2004-01-01T12:00', '2003-01-03T13:45']],
dtype='datetime64')
assert_equal(np.triu(arr).dtype, arr.dtype)
assert_equal(np.tril(arr).dtype, arr.dtype)

arr = np.zeros((3,3), dtype='f4,f4')
assert_equal(np.triu(arr).dtype, arr.dtype)
assert_equal(np.tril(arr).dtype, arr.dtype) ```
Example 6
```def test_qr_li():
cutoff = 1.e-10
for shape in [(5, 4), (4, 5)]:
print('shape =', shape)
A = np.arange(20).reshape(shape)  # linearly dependent: only two rows/columns independent
A[3, :] = np.random.random() * (cutoff / 100)  # nearly linear dependent
q, r = tools.math.qr_li(A)
assert np.linalg.norm(r - np.triu(r)) == 0.
qdq = q.T.conj().dot(q)
assert np.linalg.norm(qdq - np.eye(len(qdq))) < 1.e-13
assert np.linalg.norm(q.dot(r) - A) < cutoff * 20
r, q = tools.math.rq_li(A)
assert np.linalg.norm(r - np.triu(r, r.shape[1] - r.shape[0])) == 0.
qqd = q.dot(q.T.conj())
assert np.linalg.norm(qqd - np.eye(len(qqd))) < 1.e-13
assert np.linalg.norm(r.dot(q) - A) < cutoff * 20 ```
Example 7
```def test_tril_triu_ndim3():
for dtype in np.typecodes['AllFloat'] + np.typecodes['AllInteger']:
a = np.array([
[[1, 1], [1, 1]],
[[1, 1], [1, 0]],
[[1, 1], [0, 0]],
], dtype=dtype)
a_tril_desired = np.array([
[[1, 0], [1, 1]],
[[1, 0], [1, 0]],
[[1, 0], [0, 0]],
], dtype=dtype)
a_triu_desired = np.array([
[[1, 1], [0, 1]],
[[1, 1], [0, 0]],
[[1, 1], [0, 0]],
], dtype=dtype)
a_triu_observed = np.triu(a)
a_tril_observed = np.tril(a)
yield assert_array_equal, a_triu_observed, a_triu_desired
yield assert_array_equal, a_tril_observed, a_tril_desired
yield assert_equal, a_triu_observed.dtype, a.dtype
yield assert_equal, a_tril_observed.dtype, a.dtype ```
Example 8
```def test_tril_triu_dtype():
# Issue 4916
# tril and triu should return the same dtype as input
for c in np.typecodes['All']:
if c == 'V':
continue
arr = np.zeros((3, 3), dtype=c)
assert_equal(np.triu(arr).dtype, arr.dtype)
assert_equal(np.tril(arr).dtype, arr.dtype)

# check special cases
arr = np.array([['2001-01-01T12:00', '2002-02-03T13:56'],
['2004-01-01T12:00', '2003-01-03T13:45']],
dtype='datetime64')
assert_equal(np.triu(arr).dtype, arr.dtype)
assert_equal(np.tril(arr).dtype, arr.dtype)

arr = np.zeros((3,3), dtype='f4,f4')
assert_equal(np.triu(arr).dtype, arr.dtype)
assert_equal(np.tril(arr).dtype, arr.dtype) ```
Example 9
```def test_tril_triu_ndim3():
for dtype in np.typecodes['AllFloat'] + np.typecodes['AllInteger']:
a = np.array([
[[1, 1], [1, 1]],
[[1, 1], [1, 0]],
[[1, 1], [0, 0]],
], dtype=dtype)
a_tril_desired = np.array([
[[1, 0], [1, 1]],
[[1, 0], [1, 0]],
[[1, 0], [0, 0]],
], dtype=dtype)
a_triu_desired = np.array([
[[1, 1], [0, 1]],
[[1, 1], [0, 0]],
[[1, 1], [0, 0]],
], dtype=dtype)
a_triu_observed = np.triu(a)
a_tril_observed = np.tril(a)
yield assert_array_equal, a_triu_observed, a_triu_desired
yield assert_array_equal, a_tril_observed, a_tril_desired
yield assert_equal, a_triu_observed.dtype, a.dtype
yield assert_equal, a_tril_observed.dtype, a.dtype ```
Example 10
```def test_tril_triu_dtype():
# Issue 4916
# tril and triu should return the same dtype as input
for c in np.typecodes['All']:
if c == 'V':
continue
arr = np.zeros((3, 3), dtype=c)
assert_equal(np.triu(arr).dtype, arr.dtype)
assert_equal(np.tril(arr).dtype, arr.dtype)

# check special cases
arr = np.array([['2001-01-01T12:00', '2002-02-03T13:56'],
['2004-01-01T12:00', '2003-01-03T13:45']],
dtype='datetime64')
assert_equal(np.triu(arr).dtype, arr.dtype)
assert_equal(np.tril(arr).dtype, arr.dtype)

arr = np.zeros((3,3), dtype='f4,f4')
assert_equal(np.triu(arr).dtype, arr.dtype)
assert_equal(np.tril(arr).dtype, arr.dtype) ```
Example 11
```def _get_attn_subsequent_mask(self, size):
"""
Get an attention mask to avoid using the subsequent info.

Args:
size: int

Returns:
(`LongTensor`):

* subsequent_mask `[1 x size x size]`
"""
attn_shape = (1, size, size)
Example 12
```def _get_attn_subsequent_mask(self, size):
"""
Get an attention mask to avoid using the subsequent info.

Args:
size: int

Returns:
(`LongTensor`):

* subsequent_mask `[1 x size x size]`
"""
attn_shape = (1, size, size)
Example 13
```def _get_attn_subsequent_mask(self, size):
"""
Get an attention mask to avoid using the subsequent info.

Args:
size: int

Returns:
(`LongTensor`):

* subsequent_mask `[1 x size x size]`
"""
attn_shape = (1, size, size)
Example 14
```def test_tril_triu_ndim3():
for dtype in np.typecodes['AllFloat'] + np.typecodes['AllInteger']:
a = np.array([
[[1, 1], [1, 1]],
[[1, 1], [1, 0]],
[[1, 1], [0, 0]],
], dtype=dtype)
a_tril_desired = np.array([
[[1, 0], [1, 1]],
[[1, 0], [1, 0]],
[[1, 0], [0, 0]],
], dtype=dtype)
a_triu_desired = np.array([
[[1, 1], [0, 1]],
[[1, 1], [0, 0]],
[[1, 1], [0, 0]],
], dtype=dtype)
a_triu_observed = np.triu(a)
a_tril_observed = np.tril(a)
yield assert_array_equal, a_triu_observed, a_triu_desired
yield assert_array_equal, a_tril_observed, a_tril_desired
yield assert_equal, a_triu_observed.dtype, a.dtype
yield assert_equal, a_tril_observed.dtype, a.dtype ```
Example 15
```def test_tril_triu_dtype():
# Issue 4916
# tril and triu should return the same dtype as input
for c in np.typecodes['All']:
if c == 'V':
continue
arr = np.zeros((3, 3), dtype=c)
assert_equal(np.triu(arr).dtype, arr.dtype)
assert_equal(np.tril(arr).dtype, arr.dtype)

# check special cases
arr = np.array([['2001-01-01T12:00', '2002-02-03T13:56'],
['2004-01-01T12:00', '2003-01-03T13:45']],
dtype='datetime64')
assert_equal(np.triu(arr).dtype, arr.dtype)
assert_equal(np.tril(arr).dtype, arr.dtype)

arr = np.zeros((3,3), dtype='f4,f4')
assert_equal(np.triu(arr).dtype, arr.dtype)
assert_equal(np.tril(arr).dtype, arr.dtype) ```
Example 16
```def test_bravyi_kitaev_fast_edgeoperator_Bi(self):
# checking the edge operators
edge_matrix = numpy.triu(numpy.ones((4, 4)))
edge_matrix_indices = numpy.array(
numpy.nonzero(numpy.triu(edge_matrix) -
numpy.diag(numpy.diag(edge_matrix))))

correct_operators_b0 = ((0, 'Z'), (1, 'Z'), (2, 'Z'))
correct_operators_b1 = ((0, 'Z'), (3, 'Z'), (4, 'Z'))
correct_operators_b2 = ((1, 'Z'), (3, 'Z'), (5, 'Z'))
correct_operators_b3 = ((2, 'Z'), (4, 'Z'), (5, 'Z'))

qterm_b0 = QubitOperator(correct_operators_b0, 1)
qterm_b1 = QubitOperator(correct_operators_b1, 1)
qterm_b2 = QubitOperator(correct_operators_b2, 1)
qterm_b3 = QubitOperator(correct_operators_b3, 1)
self.assertTrue(qterm_b0 ==
_bksf.edge_operator_b(edge_matrix_indices, 0))
self.assertTrue(qterm_b1 ==
_bksf.edge_operator_b(edge_matrix_indices, 1))
self.assertTrue(qterm_b2 ==
_bksf.edge_operator_b(edge_matrix_indices, 2))
self.assertTrue(qterm_b3 ==
_bksf.edge_operator_b(edge_matrix_indices, 3)) ```
Example 17
```def _cov_matrix_diagonalization(self):
# Decomposition of cov_matrix into coords_matrix*diag(scaling_diag.^2)*coords_matrix'
# (diagonalization)
if (
self._count_eval - self.n_eigen_eval
> self.pop_size / (self.lr_covrank1_const + self.lr_mu_const) / self.n_dims / 10
):
self.n_eigen_eval = self._count_eval
self.cov_matrix = numpy.triu(self.cov_matrix) + numpy.triu(self.cov_matrix, 1).T
eigvals, eigvects = numpy.linalg.eig(self.cov_matrix)
self.scaling_diag = numpy.diag(eigvals)  # [::-1])
self.coords_matrix = eigvects  # [:, ::-1]
assert numpy.abs(numpy.imag(self.coords_matrix).sum()) == 0, self.coords_matrix
assert numpy.abs(numpy.imag(self.scaling_diag).sum()) == 0, self.scaling_diag
self.scaling_diag = numpy.sqrt(numpy.diag(self.scaling_diag)).reshape(-1, 1)
self.invsqrtC = numpy.matmul(
numpy.matmul(self.coords_matrix, numpy.diag(self.scaling_diag.flatten() ** -1)),
self.coords_matrix.T,
) ```
Example 18
```def test_syrk(self):
for f in _get_func('syrk'):
c = f(a=self.a, alpha=1.)
assert_array_almost_equal(np.triu(c), np.triu(self.t))

c = f(a=self.a, alpha=1., lower=1)
assert_array_almost_equal(np.tril(c), np.tril(self.t))

c0 = np.ones(self.t.shape)
c = f(a=self.a, alpha=1., beta=1., c=c0)
assert_array_almost_equal(np.triu(c), np.triu(self.t+c0))

c = f(a=self.a, alpha=1., trans=1)
assert_array_almost_equal(np.triu(c), np.triu(self.tt))

#prints '0-th dimension must be fixed to 3 but got 5', FIXME: suppress?
# FIXME: how to catch the _fblas.error? ```
Example 19
```def subsequent_mask(size):
attn_shape = (1, size, size)
Example 20
```def subsequent_mask(size):
attn_shape = (1, size, size)
Example 21
```def subsequent_mask(size):
attn_shape = (1, size, size)
Example 22
```def subsequent_mask(size):
attn_shape = (1, size, size)
Example 23
```def subsequent_mask(size):
attn_shape = (1, size, size)
Example 24
```def subsequent_mask(size):
attn_shape = (1, size, size)
Example 25
```def subsequent_mask(size):
attn_shape = (1, size, size)
Example 26
```def subsequent_mask(size):
attn_shape = (1, size, size)
Example 27
```def edgeFailSampling(W, p):
"""
edgeFailSampling: randomly delete the edges of a given graph

Input:
p (float): probability of deleting an edge

Output:
W (np.array): adjacency matrix with some edges randomly deleted

Obs.: The resulting graph need not be connected (even if the input graph is)
"""

assert 0 <= p <= 1
N = W.shape[0]
assert W.shape[1] == N
undirected = np.allclose(W, W.T, atol = zeroTolerance)

maskEdges = (maskEdges > p).astype(W.dtype) # Put a 1 with probability 1-p

if undirected:
W = np.triu(W)
W = W + W.T

return W ```
Example 28
```def __init__(self, graphType, N, graphOptions):
assert N > 0
#\\\ Create the graph (Outputs adjacency matrix):
self.W = createGraph(graphType, N, graphOptions)
# TODO: Let's start easy: make it just an N x N matrix. We'll see later
# the rest of the things just as handling multiple features and stuff.
#\\\ Number of nodes:
self.N = (self.W).shape[0]
#\\\ Bool for graph being undirected:
self.undirected = np.allclose(self.W, (self.W).T, atol = zeroTolerance)
#   np.allclose() gives true if matrices W and W.T are the same up to
#   atol.
#\\\ Bool for graph having self-loops:
self.selfLoops = True \
if np.sum(np.abs(np.diag(self.W)) > zeroTolerance) > 0 \
else False
#\\\ Degree matrix:
self.D = np.diag(np.sum(self.W, axis = 1))
#\\\ Number of edges:
self.M = int(np.sum(np.triu(self.W)) if self.undirected \
else np.sum(self.W))
self.A = (np.abs(self.W) > 0).astype(self.W.dtype)
#\\\ Laplacian matrix:
#   Only if the graph is undirected and has no self-loops
if self.undirected and not self.selfLoops:
else:
self.L = None
#\\\ GSO (Graph Shift Operator):
#   The weighted adjacency matrix by default
self.S = self.W
#\\\ GFT: Declare variables but do not compute it unless specifically
# requested
self.E = None # Eigenvalues
self.V = None # Eigenvectors ```
Example 29
```def subsequent_mask(size):
```def subsequent_mask(size):