Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extending support for all the hyperbolic functions #193

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions simulai/residuals/_pytorch_residuals.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def __init__(
self.processing = processing
self.periodic_bc_protected_key = "periodic"

self.protected_funcs = ["cos", "sin", "sqrt", "exp", "tanh"]
self.protected_funcs = ["cos", "sin", "sqrt", "exp", "tanh", "cosh", "sech", "sinh"]
self.protected_operators = ["L", "Div", "Identity", "Kronecker"]

self.protected_funcs_subs = self._construct_protected_functions()
Expand Down Expand Up @@ -186,9 +186,18 @@ def _construct_protected_functions(self):
Returns:
dict: A dictionary of function names and their corresponding function objects.
"""
protected_funcs = {
func: getattr(self.engine, func) for func in self.protected_funcs
}
protected_funcs = dict()

for func in self.protected_funcs:
try:
func_op = getattr(self.engine, func)
except:
try:
func_op = getattr(self, func)
except:
raise Exception(f"Operator {func} is not defined.")

protected_funcs[func] = func_op

return protected_funcs

Expand Down Expand Up @@ -618,6 +627,25 @@ def inner(inputs):

return jacobian(inner, inputs)

def sech(self, x):

cosh = getattr(self.engine, "cosh")

return 1/cosh(x)

def csch(self, x):

sinh = getattr(self.engine, "sinh")

return 1/sinh(x)

def coth(self, x):

cosh = getattr(self.engine, "cosh")
sinh = getattr(self.engine, "sinh")

return cosh(x)/sinh(x)


def diff(feature: torch.Tensor, param: torch.Tensor) -> torch.Tensor:
"""Calculates the gradient of the given feature with respect to the given parameter.
Expand Down
4 changes: 2 additions & 2 deletions tests/residuals/test_symbolicoperator.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def model_operator():
branch_network=branch_net,
var_dim=n_outputs,
rescale_factors=np.array([1]),
devices="gpu",
devices="cpu",
model_id="flame_net",
)

Expand Down Expand Up @@ -171,7 +171,7 @@ def test_symbolic_buitin_functions(self):
assert all([isinstance(item, torch.Tensor) for item in residual(input_data)])

def test_symbolic_operator_ode(self):
for token in ["sin", "cos", "sqrt", "tanh"]:
for token in ["sin", "cos", "sqrt", "tanh", "cosh", "sech", "coth", "sinh"]:
f = f"D(u, t) - alpha*{token}(u)"

input_labels = ["t"]
Expand Down
Loading