numpy.triu() in Python (original) (raw)
Last Updated : 09 Mar, 2022
numpy.triu(a, k = 0) : Returns copy of array with upper part of the triangle w.r.t k
Parameters :
a : input array k : [int, optional, 0 by default] Diagonal we require; k>0 means diagonal above main diagonal or vice versa.
Returns :
Upper triangle of a, having same shape and data-type as a.
import
numpy as geek
a
=
geek.matrix([[
1
,
21
,
30
],
`` [
63
,
434
,
3
],
`` [
54
,
54
,
56
]])
print
(
"Main Diagonal elements : \n"
, geek. triu(a),
"\n"
)
print
(
"Diagonal above main Diagonal elements : \n"
, geek. triu(a,
1
),
"\n\n"
)
print
(
"Main Diagonal elements : \n"
, geek. triu(a,
-
1
))
Output :
Main Diagonal elements : [[ 1 21 30] [ 0 434 3] [ 0 0 56]]
Diagonal above main Diagonal elements : [[ 0 21 30] [ 0 0 3] [ 0 0 0]]
Main Diagonal elements : [[ 1 21 30] [ 63 434 3] [ 0 54 56]]
Reference :
https://docs.scipy.org/doc/numpy/reference/generated/numpy.triu.html#numpy.triu
Note :
These NumPy-Python programs won’t run on online IDE’s, so run them on your systems to explore them.
Similar Reads
- numpy.tri() in Python numpy.tri(R, C = None, k = 0, dtype = 'float') : Creates an array with 1's at and below the given diagonal(about k) and 0's elsewhere. Parameters : R : Number of rows C : [optional] Number of columns; By default R = C k : [int, optional, 0 by default] Diagonal we require; k>0 means diagonal above 2 min read
- numpy.tril() in Python numpy.tril(a, k=0) : Returns copy of array with lower part of the triangle w.r.t k Parameters : a : input array k : [int, optional, 0 by default] Diagonal we require; k>0 means diagonal above main diagonal or vice versa. Returns : Lower triangle of a, having same shape and data-type as a. # Pytho 1 min read
- numpy.tan() in Python numpy.tan(array[, out]) = ufunc 'tan') : This mathematical function helps user to calculate trigonometric tangent for all x(being the array elements). Parameters : array : [array_like]elements are in radians. out : [optional]shape same as array. 2pi Radians = 360 degrees tan(x) = sin(x) / cos(x) Ret 2 min read
- numpy.sin() in Python numpy.sin(x[, out]) = ufunc 'sin') : This mathematical function helps user to calculate trigonometric sine for all x(being the array elements). Parameters : array : [array_like]elements are in radians. 2pi Radians = 36o degrees Return : An array with trigonometric sine of x for all x i.e. array elem 1 min read
- numpy.sinh() in Python The numpy.sinh() is a mathematical function that helps user to calculate hyperbolic sine for all x(being the array elements). Equivalent to 1/2 * (np.exp(x) - np.exp(-x)) or -1j * np.sin(1j*x). Syntax: numpy.sinh(x[, out]) = ufunc 'sin') Parameters : array : [array_like] elements are in radians. 2pi 2 min read
- numpy.sinc() in Python numpy.sinc(array) : This mathematical function helps user to calculate sinc function for all x(being the array elements). Parameters : array : [array_like] elements are in radians. 2pi Radians = 36o degrees Return : An array with sinc value of x for all x i.e. array elements. Code #1 : Working # Pyt 1 min read
- numpy.tanh() in Python The numpy.tanh()is a mathematical function that helps user to calculate hyperbolic tangent for all x(being the array elements). Equivalent to np.sinh(x) / np.cosh(x) or -1j * np.tan(1j*x). Syntax : numpy.tanh(x[, out]) = ufunc 'tanh') Parameters : array : [array_like] elements are in radians. 2pi Ra 2 min read
- Python | Numpy np.triu_indices With the help of np.triu_indices() method, we can get the indices for the upper triangle of an [n, m] array by using np.triu_indices() method. Syntax : np.triu_indices(n, m) Return : Return the indices for the upper triangle. Example #1 : In this example we can see that by using np.triu_indices() me 1 min read
- numpy.zeros() in Python numpy.zeros() function creates a new array of specified shapes and types, filled with zeros. It is beneficial when you need a placeholder array to initialize variables or store intermediate results. We can create 1D array using numpy.zeros(). Let's understand with the help of an example: [GFGTABS] P 2 min read
- numpy.array_str() in Python numpy.array_str()function is used to represent the data of an array as a string. The data in the array is returned as a single string. This function is similar to array_repr, the difference being that array_repr also returns information on the kind of array and its data type. Syntax : numpy.array_st 2 min read