diff --git a/Basis_Function.py b/Basis_Function.py index 43fba039966184b62cd46287afcc6543fe277aaf..1563cf823593312520ea758bc252a3af8ccdfe3e 100644 --- a/Basis_Function.py +++ b/Basis_Function.py @@ -412,3 +412,82 @@ class OrthonormalLegendre(Legendre): row.append(np.float64(entry)) matrix.append(row) return matrix + + def calculate_cell_average(self, projection, stencil_length, + add_reconstructions=True): + """Calculate cell averages for a given projection. + + Calculate the cell averages of all cells in a projection. + If desired, reconstructions are calculated for the middle cell + and added left and right to it, respectively. + + Notes + ----- + To increase speed. this function uses a simplified calculation + specific to the orthonormal Legendre polynomial basis. + + Parameters + ---------- + projection : ndarray + Matrix of projection for each polynomial degree. + stencil_length : int + Size of data array. + add_reconstructions: bool, optional + Flag whether reconstructions of the middle cell are included. + Default: True. + + Returns + ------- + ndarray + Matrix containing cell averages (and reconstructions) for given + projection. + + """ + + cell_averages = np.array([projection[0] / np.sqrt(2)]) + + if add_reconstructions: + middle_idx = stencil_length // 2 + left_reconstructions, right_reconstructions = \ + self._calculate_reconstructions( + projection[:, middle_idx:middle_idx+1]) + return np.array(list(map( + np.float64, zip(cell_averages[:, :middle_idx], + left_reconstructions, + cell_averages[:, middle_idx], + right_reconstructions, + cell_averages[:, middle_idx+1:])))) + return np.array(list(map(np.float64, cell_averages))) + + def _calculate_reconstructions(self, projection): + """Calculate left and right reconstructions for a given projection. + + Notes + ----- + To increase speed. this function uses a simplified calculation + specific to the orthonormal Legendre polynomial basis. + + Parameters + ---------- + projection : ndarray + Matrix of projection for each polynomial degree. + + Returns + ------- + left_reconstruction: list + List containing left reconstructions for given projection. + right_reconstruction: list + List containing right reconstructions for given projection. + + """ + + left_reconstructions = [ + sum(projection[degree][cell] * (-1)**degree + * np.sqrt(degree + 0.5) + for degree in range(self._polynomial_degree+1)) + for cell in range(len(projection[0]))] + right_reconstructions = [ + sum(projection[degree][cell] * np.sqrt(degree + 0.5) + for degree in range(self._polynomial_degree+1)) + for cell in range(len(projection[0]))] + return left_reconstructions, right_reconstructions