def get_eigenvector(self, i): eigv_i = self.eigv[:, i] eigv_i_real = Vector.Type()(self.A.N, real(eigv_i)) eigv_i_imag = Vector.Type()(self.A.N, imag(eigv_i)) eigv_i_real_fun = Function(eigv_i_real) eigv_i_imag_fun = Function(eigv_i_imag) return (eigv_i_real_fun, eigv_i_imag_fun)
def __mul__(self, other): if isinstance(other, Vector.Type()): # copied from BasicMatrix because ndarray uses __matul__ instead of __mul__ for matrix-vector product self._arithmetic_operations_assert_attributes(other, other_order=1) output_content = self.content.__matmul__(other.content) output_size = self.M output = Vector.Type()(output_size, output_content) self._arithmetic_operations_preserve_attributes(output, other_order=1) return output else: return _Matrix_Type_Base.__mul__(self, other)
def _abs(vector: Vector.Type()): abs_vector = numpy_abs(vector) i_max = argmax(abs_vector) i_max = int( i_max ) # numpy.intXX types are not subclasses of int, but can be converted to int return AbsOutput(vector[i_max], (i_max, ))
def __imul__(self, other): if isinstance(other, Vector.Type()): # copied from BasicMatrix because ndarray uses __matul__ instead of __mul__ for matrix-vector product self._arithmetic_operations_assert_attributes(other, other_order=1) self.content.__imatmul__(other.content) return self else: return _Matrix_Type_Base.__imul__(self, other)
# Copyright (C) 2015-2020 by the RBniCS authors # # This file is part of RBniCS. # # SPDX-License-Identifier: LGPL-3.0-or-later from rbnics.backends.online.basic import NonAffineExpansionStorage as BasicNonAffineExpansionStorage from rbnics.backends.online.numpy.matrix import Matrix from rbnics.backends.online.numpy.vector import Vector from rbnics.utils.decorators import BackendFor, tuple_of NonAffineExpansionStorage_Base = BasicNonAffineExpansionStorage @BackendFor("numpy", inputs=((int, tuple_of(Matrix.Type()), tuple_of(Vector.Type())), (int, None))) class NonAffineExpansionStorage(NonAffineExpansionStorage_Base): pass
# SPDX-License-Identifier: LGPL-3.0-or-later # from rbnics.backends.online.basic import evaluate as basic_evaluate # from rbnics.backends.online.numpy.function import Function from rbnics.backends.online.numpy.matrix import Matrix # from rbnics.backends.online.numpy.parametrized_expression_factory import ParametrizedExpressionFactory # from rbnics.backends.online.numpy.parametrized_tensor_factory import ParametrizedTensorFactory # from rbnics.backends.online.numpy.reduced_mesh import ReducedMesh # from rbnics.backends.online.numpy.reduced_vertices import ReducedVertices # from rbnics.backends.online.numpy.tensors_list import TensorsList from rbnics.backends.online.numpy.vector import Vector from rbnics.utils.decorators import backend_for, tuple_of # backend = ModuleWrapper(Function, FunctionsList, Matrix, ParametrizedExpressionFactory, ParametrizedTensorFactory, # ReducedMesh, ReducedVertices, TensorsList, Vector) # wrapping = ModuleWrapper(evaluate_and_vectorize_sparse_matrix_at_dofs, evaluate_sparse_function_at_dofs, # evaluate_sparse_vector_at_dofs, expression_on_reduced_mesh, expression_on_truth_mesh, # form_on_reduced_function_space, form_on_truth_function_space) # online_backend = ModuleWrapper(OnlineFunction=Function, OnlineMatrix=Matrix, OnlineVector=Vector) # online_wrapping = ModuleWrapper() # evaluate_base = basic_evaluate(backend, wrapping, online_backend, online_wrapping) evaluate_base = None # TODO # Evaluate a parametrized expression, possibly at a specific location @backend_for("numpy", inputs=((Matrix.Type(), Vector.Type()), (tuple_of(int), tuple_of(tuple_of(int)), None))) def evaluate(expression, at=None): return evaluate_base(expression, at)
# # RBniCS is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with RBniCS. If not, see <http://www.gnu.org/licenses/>. # from rbnics.backends.basic import transpose as basic_transpose from rbnics.backends.online.numpy.basis_functions_matrix import BasisFunctionsMatrix from rbnics.backends.online.numpy.function import Function from rbnics.backends.online.numpy.functions_list import FunctionsList from rbnics.backends.online.numpy.matrix import Matrix from rbnics.backends.online.numpy.non_affine_expansion_storage import NonAffineExpansionStorage from rbnics.backends.online.numpy.tensors_list import TensorsList from rbnics.backends.online.numpy.vector import Vector from rbnics.backends.online.numpy.wrapping import function_to_vector, matrix_mul_vector, vector_mul_vector, vectorized_matrix_inner_vectorized_matrix from rbnics.utils.decorators import backend_for, ModuleWrapper backend = ModuleWrapper(BasisFunctionsMatrix, Function, FunctionsList, Matrix, NonAffineExpansionStorage, TensorsList, Vector) wrapping = ModuleWrapper(function_to_vector, matrix_mul_vector, vector_mul_vector, vectorized_matrix_inner_vectorized_matrix) online_backend = ModuleWrapper(OnlineMatrix=Matrix, OnlineVector=Vector) online_wrapping = ModuleWrapper() transpose_base = basic_transpose(backend, wrapping, online_backend, online_wrapping) @backend_for("numpy", inputs=((BasisFunctionsMatrix, Function.Type(), FunctionsList, TensorsList, Vector.Type()), )) def transpose(arg): return transpose_base(arg)
# Copyright (C) 2015-2021 by the RBniCS authors # # This file is part of RBniCS. # # SPDX-License-Identifier: LGPL-3.0-or-later from numpy import argmax, abs as numpy_abs, unravel_index from rbnics.backends.online.numpy.matrix import Matrix from rbnics.backends.online.numpy.vector import Vector from rbnics.utils.decorators import backend_for, overload # abs function to compute maximum absolute value of an expression, matrix or vector (for EIM). # To be used in combination with max, even though here we actually carry out both the max and the abs! @backend_for("numpy", inputs=((Matrix.Type(), Vector.Type()), )) def abs(expression): return _abs(expression) @overload def _abs(matrix: Matrix.Type()): abs_matrix = numpy_abs(matrix) (i_max, j_max) = unravel_index(argmax(abs_matrix), abs_matrix.shape) # i_max and j_max are of type numpy.intXX which is not a subclass of int, but can be converted to int (i_max, j_max) = (int(i_max), int(j_max)) return AbsOutput(matrix[(i_max, j_max)], (i_max, j_max)) @overload def _abs(vector: Vector.Type()): abs_vector = numpy_abs(vector)
# it under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # RBniCS is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with RBniCS. If not, see <http://www.gnu.org/licenses/>. # from rbnics.backends.online.basic.assign import assign as basic_assign from rbnics.backends.online.numpy.function import Function from rbnics.backends.online.numpy.matrix import Matrix from rbnics.backends.online.numpy.vector import Vector from rbnics.utils.decorators import backend_for, list_of, ModuleWrapper backend = ModuleWrapper(Function, Matrix, Vector) assign_base = basic_assign(backend) @backend_for("numpy", inputs=((Function.Type(), list_of(Function.Type()), Matrix.Type(), Vector.Type()), (Function.Type(), list_of(Function.Type()), Matrix.Type(), Vector.Type()))) def assign(object_to, object_from): assign_base(object_to, object_from)
# Copyright (C) 2015-2021 by the RBniCS authors # # This file is part of RBniCS. # # SPDX-License-Identifier: LGPL-3.0-or-later from rbnics.backends.basic import copy as basic_copy from rbnics.backends.online.numpy.function import Function from rbnics.backends.online.numpy.matrix import Matrix from rbnics.backends.online.numpy.vector import Vector from rbnics.backends.online.numpy.wrapping.function_copy import basic_function_copy from rbnics.backends.online.numpy.wrapping.tensor_copy import basic_tensor_copy from rbnics.utils.decorators import backend_for, list_of, ModuleWrapper backend = ModuleWrapper(Function, Matrix, Vector) wrapping_for_wrapping = ModuleWrapper() function_copy = basic_function_copy(backend, wrapping_for_wrapping) tensor_copy = basic_tensor_copy(backend, wrapping_for_wrapping) wrapping = ModuleWrapper(function_copy=function_copy, tensor_copy=tensor_copy) copy_base = basic_copy(backend, wrapping) @backend_for("numpy", inputs=((Function.Type(), list_of(Function.Type()), Matrix.Type(), Vector.Type()), )) def copy(arg): return copy_base(arg)
# Copyright (C) 2015-2020 by the RBniCS authors # # This file is part of RBniCS. # # SPDX-License-Identifier: LGPL-3.0-or-later from rbnics.backends.basic import export as basic_export from rbnics.backends.online.numpy.function import Function from rbnics.backends.online.numpy.matrix import Matrix from rbnics.backends.online.numpy.vector import Vector from rbnics.backends.online.numpy.wrapping import function_save, tensor_save from rbnics.utils.decorators import backend_for, ModuleWrapper from rbnics.utils.io import Folders backend = ModuleWrapper(Function, Matrix, Vector) wrapping = ModuleWrapper(function_save, tensor_save) export_base = basic_export(backend, wrapping) # Export a solution to file @backend_for("numpy", inputs=((Function.Type(), Matrix.Type(), Vector.Type()), (Folders.Folder, str), str, (int, None), (int, str, None))) def export(solution, directory, filename, suffix=None, component=None): export_base(solution, directory, filename, suffix, component)
# Copyright (C) 2015-2021 by the RBniCS authors # # This file is part of RBniCS. # # SPDX-License-Identifier: LGPL-3.0-or-later from rbnics.backends.online.basic.assign import assign as basic_assign from rbnics.backends.online.numpy.function import Function from rbnics.backends.online.numpy.matrix import Matrix from rbnics.backends.online.numpy.vector import Vector from rbnics.utils.decorators import backend_for, list_of, ModuleWrapper backend = ModuleWrapper(Function, Matrix, Vector) assign_base = basic_assign(backend) @backend_for("numpy", inputs=((Function.Type(), list_of(Function.Type()), Matrix.Type(), Vector.Type()), (Function.Type(), list_of(Function.Type()), Matrix.Type(), Vector.Type()))) def assign(object_to, object_from): assign_base(object_to, object_from)
# but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with RBniCS. If not, see <http://www.gnu.org/licenses/>. # from rbnics.backends.online.basic import transpose as basic_transpose from rbnics.backends.online.basic.wrapping import DelayedTransposeWithArithmetic as BasicDelayedTransposeWithArithmetic from rbnics.backends.online.numpy.basis_functions_matrix import BasisFunctionsMatrix from rbnics.backends.online.numpy.function import Function from rbnics.backends.online.numpy.functions_list import FunctionsList from rbnics.backends.online.numpy.matrix import Matrix from rbnics.backends.online.numpy.non_affine_expansion_storage import NonAffineExpansionStorage from rbnics.backends.online.numpy.tensors_list import TensorsList from rbnics.backends.online.numpy.vector import Vector from rbnics.backends.online.numpy.wrapping import function_to_vector, matrix_mul_vector, vector_mul_vector, vectorized_matrix_inner_vectorized_matrix from rbnics.utils.decorators import backend_for, ModuleWrapper backend = ModuleWrapper(BasisFunctionsMatrix, Function, FunctionsList, Matrix, NonAffineExpansionStorage, TensorsList, Vector) DelayedTransposeWithArithmetic = BasicDelayedTransposeWithArithmetic(backend) wrapping = ModuleWrapper(function_to_vector, matrix_mul_vector, vector_mul_vector, vectorized_matrix_inner_vectorized_matrix, DelayedTransposeWithArithmetic=DelayedTransposeWithArithmetic) online_backend = ModuleWrapper(OnlineMatrix=Matrix, OnlineVector=Vector) online_wrapping = ModuleWrapper() transpose_base = basic_transpose(backend, wrapping, online_backend, online_wrapping) @backend_for("numpy", inputs=((BasisFunctionsMatrix, DelayedTransposeWithArithmetic, Function.Type(), FunctionsList, TensorsList, Vector.Type()), )) def transpose(arg): return transpose_base(arg)
# Copyright (C) 2015-2021 by the RBniCS authors # # This file is part of RBniCS. # # SPDX-License-Identifier: LGPL-3.0-or-later from rbnics.utils.decorators import backend_for, OnlineSizeType from rbnics.backends.online.basic import Function as BasicFunction from rbnics.backends.online.numpy.vector import Vector _Function_Type = BasicFunction(Vector) @backend_for("numpy", inputs=(OnlineSizeType + (Vector.Type(), ), )) def Function(arg): return _Function_Type(arg) # Attach a Type() function def Type(): return _Function_Type Function.Type = Type
# RBniCS is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with RBniCS. If not, see <http://www.gnu.org/licenses/>. # from numpy.linalg import solve from rbnics.backends.online.basic import LinearSolver as BasicLinearSolver from rbnics.backends.online.numpy.matrix import Matrix from rbnics.backends.online.numpy.product import DelayedTransposeWithArithmetic from rbnics.backends.online.numpy.vector import Vector from rbnics.backends.online.numpy.function import Function from rbnics.utils.decorators import BackendFor, DictOfThetaType, ModuleWrapper, ThetaType backend = ModuleWrapper(Matrix, Vector) wrapping = ModuleWrapper(DelayedTransposeWithArithmetic=DelayedTransposeWithArithmetic) LinearSolver_Base = BasicLinearSolver(backend, wrapping) @BackendFor("numpy", inputs=(Matrix.Type(), Function.Type(), Vector.Type(), ThetaType + DictOfThetaType + (None,))) class LinearSolver(LinearSolver_Base): def set_parameters(self, parameters): assert len(parameters) == 0, "NumPy linear solver does not accept parameters yet" def solve(self): solution = solve(self.lhs, self.rhs) self.solution.vector()[:] = solution return self.solution
# GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with RBniCS. If not, see <http://www.gnu.org/licenses/>. # from rbnics.backends.online.basic import AffineExpansionStorage as BasicAffineExpansionStorage from rbnics.backends.online.numpy.copy import function_copy, tensor_copy from rbnics.backends.online.numpy.function import Function from rbnics.backends.online.numpy.matrix import Matrix from rbnics.backends.online.numpy.vector import Vector from rbnics.backends.online.numpy.wrapping import function_load, function_save, tensor_load, tensor_save from rbnics.utils.decorators import BackendFor, ModuleWrapper, tuple_of backend = ModuleWrapper(Function, Matrix, Vector) wrapping = ModuleWrapper(function_load, function_save, tensor_load, tensor_save, function_copy=function_copy, tensor_copy=tensor_copy) AffineExpansionStorage_Base = BasicAffineExpansionStorage(backend, wrapping) @BackendFor("numpy", inputs=((int, tuple_of(Matrix.Type()), tuple_of(Vector.Type())), (int, None))) class AffineExpansionStorage(AffineExpansionStorage_Base): def __init__(self, arg1, arg2=None): AffineExpansionStorage_Base.__init__(self, arg1, arg2)
# # SPDX-License-Identifier: LGPL-3.0-or-later from numpy.linalg import solve from rbnics.backends.abstract import LinearProblemWrapper from rbnics.backends.online.basic import LinearSolver as BasicLinearSolver from rbnics.backends.online.numpy.function import Function from rbnics.backends.online.numpy.matrix import Matrix from rbnics.backends.online.numpy.transpose import DelayedTransposeWithArithmetic from rbnics.backends.online.numpy.vector import Vector from rbnics.utils.decorators import BackendFor, DictOfThetaType, ModuleWrapper, ThetaType backend = ModuleWrapper(Function, Matrix, Vector) wrapping = ModuleWrapper(DelayedTransposeWithArithmetic=DelayedTransposeWithArithmetic) LinearSolver_Base = BasicLinearSolver(backend, wrapping) @BackendFor("numpy", inputs=((Matrix.Type(), DelayedTransposeWithArithmetic, LinearProblemWrapper), Function.Type(), (Vector.Type(), DelayedTransposeWithArithmetic, None), ThetaType + DictOfThetaType + (None,))) class LinearSolver(LinearSolver_Base): def set_parameters(self, parameters): assert len(parameters) == 0, "NumPy linear solver does not accept parameters yet" def solve(self): solution = solve(self.lhs, self.rhs) self.solution.vector()[:] = solution if self.monitor is not None: self.monitor(self.solution)
# Copyright (C) 2015-2021 by the RBniCS authors # # This file is part of RBniCS. # # SPDX-License-Identifier: LGPL-3.0-or-later from rbnics.backends.basic import export as basic_export from rbnics.backends.online.numpy.function import Function from rbnics.backends.online.numpy.matrix import Matrix from rbnics.backends.online.numpy.vector import Vector from rbnics.backends.online.numpy.wrapping import function_save, tensor_save from rbnics.utils.decorators import backend_for, ModuleWrapper from rbnics.utils.io import Folders backend = ModuleWrapper(Function, Matrix, Vector) wrapping = ModuleWrapper(function_save, tensor_save) export_base = basic_export(backend, wrapping) # Export a solution to file @backend_for("numpy", inputs=((Function.Type(), Matrix.Type(), Vector.Type()), (Folders.Folder, str), str, (int, None), (int, str, None)) ) def export(solution, directory, filename, suffix=None, component=None): export_base(solution, directory, filename, suffix, component)
# but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with RBniCS. If not, see <http://www.gnu.org/licenses/>. # from numpy.linalg import solve from rbnics.backends.online.basic import LinearSolver as BasicLinearSolver from rbnics.backends.online.numpy.matrix import Matrix from rbnics.backends.online.numpy.vector import Vector from rbnics.backends.online.numpy.function import Function from rbnics.utils.decorators import BackendFor, DictOfThetaType, ThetaType LinearSolver_Base = BasicLinearSolver @BackendFor("numpy", inputs=(Matrix.Type(), Function.Type(), Vector.Type(), ThetaType + DictOfThetaType + (None, ))) class LinearSolver(LinearSolver_Base): def set_parameters(self, parameters): assert len(parameters ) == 0, "NumPy linear solver does not accept parameters yet" def solve(self): solution = solve(self.lhs, self.rhs) self.solution.vector()[:] = solution return self.solution