Skip to content

Commit

Permalink
Merge pull request #502 from Saumya-ranjan/change_gramian_to_gram
Browse files Browse the repository at this point in the history
Changed Gramian matrix to Gram matrix for better consistency. Issue #501.
  • Loading branch information
vnmabus committed Dec 29, 2022
2 parents 0b578ef + 5a222a6 commit 38118d5
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 34 deletions.
2 changes: 1 addition & 1 deletion skfda/misc/_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ def _inner_product_integrate(
len_arg2 = len(arg2)
else:
# If the arguments are callables, we need to pass the domain range
# explicitly. This is used internally for computing the gramian
# explicitly. This is used internally for computing the gram
# matrix of operators.
assert _domain_range is not None
domain_range = _domain_range
Expand Down
8 changes: 4 additions & 4 deletions skfda/misc/operators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"_operators": [
"MatrixOperator",
"Operator",
"gramian_matrix",
"gramian_matrix_optimization",
"gram_matrix",
"gram_matrix_optimization",
],
"_srvf": ["SRSF"],
},
Expand All @@ -28,7 +28,7 @@
from ._operators import (
MatrixOperator as MatrixOperator,
Operator as Operator,
gramian_matrix as gramian_matrix,
gramian_matrix_optimization as gramian_matrix_optimization,
gram_matrix as gram_matrix,
gram_matrix_optimization as gram_matrix_optimization,
)
from ._srvf import SRSF as SRSF
6 changes: 3 additions & 3 deletions skfda/misc/operators/_identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from ...representation import FDataGrid
from ...representation.basis import Basis
from ...typing._numpy import NDArrayFloat
from ._operators import InputType, Operator, gramian_matrix_optimization
from ._operators import InputType, Operator, gram_matrix_optimization

T = TypeVar("T", bound=InputType)

Expand All @@ -28,7 +28,7 @@ def __call__(self, f: T) -> T: # noqa: D102
return f


@gramian_matrix_optimization.register
@gram_matrix_optimization.register
def basis_penalty_matrix_optimized(
linear_operator: Identity[Any],
basis: Basis,
Expand All @@ -37,7 +37,7 @@ def basis_penalty_matrix_optimized(
return basis.gram_matrix()


@gramian_matrix_optimization.register
@gram_matrix_optimization.register
def fdatagrid_penalty_matrix_optimized(
linear_operator: Identity[Any],
basis: FDataGrid,
Expand Down
14 changes: 7 additions & 7 deletions skfda/misc/operators/_linear_differential_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
)
from ...typing._base import DomainRangeLike
from ...typing._numpy import NDArrayFloat
from ._operators import Operator, gramian_matrix_optimization
from ._operators import Operator, gram_matrix_optimization

Order = int

Expand Down Expand Up @@ -218,12 +218,12 @@ def applied_linear_diff_op(

#############################################################
#
# Optimized implementations of gramian matrix for each basis.
# Optimized implementations of gram matrix for each basis.
#
#############################################################


@gramian_matrix_optimization.register
@gram_matrix_optimization.register
def constant_penalty_matrix_optimized(
linear_operator: LinearDifferentialOperator,
basis: ConstantBasis,
Expand Down Expand Up @@ -299,7 +299,7 @@ def _monomial_evaluate_constant_linear_diff_op(
return polynomials # type: ignore[no-any-return]


@gramian_matrix_optimization.register
@gram_matrix_optimization.register
def monomial_penalty_matrix_optimized(
linear_operator: LinearDifferentialOperator,
basis: MonomialBasis,
Expand Down Expand Up @@ -427,7 +427,7 @@ def _fourier_penalty_matrix_optimized_orthonormal(
return penalty_matrix


@gramian_matrix_optimization.register
@gram_matrix_optimization.register
def fourier_penalty_matrix_optimized(
linear_operator: LinearDifferentialOperator,
basis: FourierBasis,
Expand All @@ -445,7 +445,7 @@ def fourier_penalty_matrix_optimized(
return _fourier_penalty_matrix_optimized_orthonormal(basis, weights)


@gramian_matrix_optimization.register
@gram_matrix_optimization.register
def bspline_penalty_matrix_optimized(
linear_operator: LinearDifferentialOperator,
basis: BSplineBasis,
Expand Down Expand Up @@ -576,7 +576,7 @@ def bspline_penalty_matrix_optimized(
return penalty_matrix


@gramian_matrix_optimization.register
@gram_matrix_optimization.register
def fdatagrid_penalty_matrix_optimized(
linear_operator: LinearDifferentialOperator,
basis: FDataGrid,
Expand Down
22 changes: 11 additions & 11 deletions skfda/misc/operators/_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,26 @@ def __call__(self, vector: OperatorInput) -> OperatorOutput:


@multimethod.multidispatch
def gramian_matrix_optimization(
def gram_matrix_optimization(
linear_operator: Any,
basis: OperatorInput,
) -> NDArrayFloat:
"""
Efficient implementation of gramian_matrix.
Efficient implementation of gram_matrix.
Generic function that can be subclassed for different combinations of
operator and basis in order to provide a more efficient implementation
for the gramian matrix.
for the gram matrix.
"""
return NotImplemented


def gramian_matrix_numerical(
def gram_matrix_numerical(
linear_operator: Operator[OperatorInput, OutputType],
basis: OperatorInput,
) -> NDArrayFloat:
"""
Return the gramian matrix given a basis, computed numerically.
Return the gram matrix given a basis, computed numerically.
This method should work for every linear operator.
Expand All @@ -70,19 +70,19 @@ def gramian_matrix_numerical(
return inner_product_matrix(evaluated_basis, _domain_range=domain_range)


def gramian_matrix(
def gram_matrix(
linear_operator: Operator[OperatorInput, OutputType],
basis: OperatorInput,
) -> NDArrayFloat:
r"""
Return the gramian matrix given a basis.
Return the gram matrix given a basis.
The gramian operator of a linear operator :math:`\Gamma` is
The gram operator of a linear operator :math:`\Gamma` is
.. math::
G = \Gamma*\Gamma
This method evaluates that gramian operator in a given basis,
This method evaluates that gram operator in a given basis,
which is necessary for performing Tikhonov regularization,
among other things.
Expand All @@ -91,11 +91,11 @@ def gramian_matrix(
"""
# Try to use a more efficient implementation
matrix = gramian_matrix_optimization(linear_operator, basis)
matrix = gram_matrix_optimization(linear_operator, basis)
if matrix is not NotImplemented:
return matrix

return gramian_matrix_numerical(linear_operator, basis)
return gram_matrix_numerical(linear_operator, basis)


class MatrixOperator(Operator[NDArrayFloat, NDArrayFloat]):
Expand Down
4 changes: 2 additions & 2 deletions skfda/misc/regularization/_regularization.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from ...representation import FData
from ...representation.basis import Basis
from ...typing._numpy import NDArrayFloat
from ..operators import Identity, Operator, gramian_matrix
from ..operators import Identity, Operator, gram_matrix
from ..operators._operators import OperatorInput


Expand Down Expand Up @@ -101,7 +101,7 @@ def penalty_matrix(
else self.linear_operator
)

return self.regularization_parameter * gramian_matrix(
return self.regularization_parameter * gram_matrix(
linear_operator,
basis,
)
Expand Down
12 changes: 6 additions & 6 deletions skfda/tests/test_regularization.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
from skfda.misc.operators import (
Identity,
LinearDifferentialOperator,
gramian_matrix,
gram_matrix,
)
from skfda.misc.operators._linear_differential_operator import (
_monomial_evaluate_constant_linear_diff_op,
)
from skfda.misc.operators._operators import gramian_matrix_numerical
from skfda.misc.operators._operators import gram_matrix_numerical
from skfda.misc.regularization import L2Regularization
from skfda.ml.regression import LinearRegression
from skfda.representation.basis import (
Expand Down Expand Up @@ -58,8 +58,8 @@ def _test_penalty(

operator = LinearDifferentialOperator(linear_diff_op)

penalty = gramian_matrix(operator, basis)
numerical_penalty = gramian_matrix_numerical(operator, basis)
penalty = gram_matrix(operator, basis)
numerical_penalty = gram_matrix_numerical(operator, basis)

np.testing.assert_allclose(
penalty,
Expand Down Expand Up @@ -222,8 +222,8 @@ def test_bspline_penalty_special_case(self) -> None:
])

operator = LinearDifferentialOperator(basis.order - 1)
penalty = gramian_matrix(operator, basis)
numerical_penalty = gramian_matrix_numerical(operator, basis)
penalty = gram_matrix(operator, basis)
numerical_penalty = gram_matrix_numerical(operator, basis)

np.testing.assert_allclose(
penalty,
Expand Down

0 comments on commit 38118d5

Please sign in to comment.