In [1]:
import numpy as np
In [2]:
a = np.arange(16).reshape(4, 4)
print(a)
In [3]:
print(np.tril(a))
In [4]:
print(np.triu(a))
In [5]:
print(np.tril(a).T)
In [6]:
print(np.tril(a) + np.tril(a).T)
In [7]:
print(a.diagonal())
In [8]:
print(np.diag(a.diagonal()))
In [9]:
print(np.tril(a) + np.tril(a).T - np.diag(a.diagonal()))
In [10]:
def get_symmetric(a, use_tril=True):
if use_tril:
a = np.tril(a)
else:
a = np.triu(a)
return a + a.T - np.diag(a.diagonal())
In [11]:
print(get_symmetric(a))
In [12]:
print(get_symmetric(a, False))
In [13]:
def is_symmetric(a):
return np.array_equal(a, a.T)
In [14]:
a_sym = get_symmetric(a)
print(a_sym)
In [15]:
print(is_symmetric(a_sym))
In [16]:
print(is_symmetric(a))
In [17]:
def get_skew_symmetric(a, use_tril=True):
if use_tril:
a = np.tril(a)
else:
a = np.triu(a)
return a - a.T
In [18]:
print(get_skew_symmetric(a))
In [19]:
print(get_skew_symmetric(a, False))
In [20]:
def is_skew_symmetric(a):
return np.array_equal(a, -a.T)
In [21]:
a_sk_sym = get_skew_symmetric(a)
print(a_sk_sym)
In [22]:
print(is_skew_symmetric(a_sk_sym))
In [23]:
print(is_skew_symmetric(a))