agd.Metrics.misc

 1# Copyright 2020 Jean-Marie Mirebeau, University Paris-Sud, CNRS, University Paris-Saclay
 2# Distributed WITHOUT ANY WARRANTY. Licensed under the Apache License, Version 2.0, see http://www.apache.org/licenses/LICENSE-2.0
 3
 4from .. import AutomaticDifferentiation as ad
 5from .. import FiniteDifferences as fd
 6import numpy as np
 7
 8def flatten_symmetric_matrix(m):
 9	"""
10	Input : a square (symmetric) matrix.
11	Output : a vector containing the lower triangular entries
12	"""
13	d=m.shape[0]
14	assert d==m.shape[1]
15	return np.concatenate([m[i,:(i+1)] for i in range(d)],axis=0)
16
17def expand_symmetric_matrix(arr,d=None,extra_length=False):
18	if d is None:
19		d=0
20		while (d*(d+1))//2 < len(arr):
21			d+=1
22	assert (extra_length or len(arr)==(d*(d+1))//2)
23	
24	def index(i,j):
25		i,j = max(i,j),min(i,j)
26		return (i*(i+1))//2+j
27	return ad.asarray([ [ arr[index(i,j)] for i in range(d)] for j in range(d) ])
def flatten_symmetric_matrix(m):
 9def flatten_symmetric_matrix(m):
10	"""
11	Input : a square (symmetric) matrix.
12	Output : a vector containing the lower triangular entries
13	"""
14	d=m.shape[0]
15	assert d==m.shape[1]
16	return np.concatenate([m[i,:(i+1)] for i in range(d)],axis=0)

Input : a square (symmetric) matrix. Output : a vector containing the lower triangular entries

def expand_symmetric_matrix(arr, d=None, extra_length=False):
18def expand_symmetric_matrix(arr,d=None,extra_length=False):
19	if d is None:
20		d=0
21		while (d*(d+1))//2 < len(arr):
22			d+=1
23	assert (extra_length or len(arr)==(d*(d+1))//2)
24	
25	def index(i,j):
26		i,j = max(i,j),min(i,j)
27		return (i*(i+1))//2+j
28	return ad.asarray([ [ arr[index(i,j)] for i in range(d)] for j in range(d) ])