Skip to content

Commit

Permalink
Merge pull request #525 from rimjhimittal/arc_func
Browse files Browse the repository at this point in the history
added arctan, arccos and arcsin functions and an example file using them
  • Loading branch information
pgleeson authored Mar 31, 2024
2 parents 34d657c + 24fac95 commit 1c2ee26
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
46 changes: 40 additions & 6 deletions src/modeci_mdf/functions/standard.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
# are important, do not remove even though they appear unused.
import math
import numpy

import types
import re

"""
A dict that stores all registered MDF functions.
Expand Down Expand Up @@ -69,12 +70,24 @@ def create_python_expression(expression_string: str = None) -> str:
function expression in python
"""

for func in ["exp", "sin", "cos", "tan", "sinh", "cosh", "tanh"]:
if "numpy." + func not in expression_string:
functions = [
"arctan",
"arcsin",
"arccos",
"exp",
"sin",
"cos",
"tan",
"sinh",
"cosh",
"tanh",
]
functions_sorted = sorted(functions, key=len, reverse=True)
for func in functions_sorted:
pattern = r"\b" + re.escape(func) + r"\b"
replacement = "numpy." + func
expression_string = re.sub(pattern, replacement, expression_string)

expression_string = expression_string.replace(
"%s(" % func, "numpy.%s(" % func
)
for func in ["maximum"]:
expression_string = expression_string.replace("%s(" % func, "numpy.%s(" % func)
"""for func in ["max"]:
Expand Down Expand Up @@ -213,6 +226,27 @@ def add_public_functions_from_module(module, module_alias: str = None):
expression_string="(%s * slope + intercept)" % (STANDARD_ARG_0),
)

add_mdf_function(
"arctan",
description="Inverse tangent function",
arguments=[STANDARD_ARG_0, "scale"],
expression_string="scale * arctan(%s)" % (STANDARD_ARG_0),
)

add_mdf_function(
"arcsin",
description="Inverse sine function",
arguments=[STANDARD_ARG_0, "scale"],
expression_string="scale * arcsin(%s)" % (STANDARD_ARG_0),
)

add_mdf_function(
"arccos",
description="Inverse cosine function",
arguments=[STANDARD_ARG_0, "scale"],
expression_string="scale * arccos(%s)" % (STANDARD_ARG_0),
)

add_mdf_function(
"logistic",
description="Logistic function",
Expand Down
6 changes: 6 additions & 0 deletions tests/test_mdf_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
),
("Relu", {"A": 1}, 1),
("Relu", {"A": -1}, 0),
("arctan", {"variable0": 1, "scale": 1}, math.atan(1)),
("arctan", {"variable0": 0, "scale": 2}, 2 * math.atan(0)),
("arcsin", {"variable0": 0.5, "scale": 1}, math.asin(0.5)),
("arcsin", {"variable0": 0, "scale": 2}, 2 * math.asin(0)),
("arccos", {"variable0": 0.5, "scale": 1}, math.acos(0.5)),
("arccos", {"variable0": 0, "scale": 2}, 2 * math.acos(0)),
],
)
def test_std_functions(name, expected_result, parameters):
Expand Down

0 comments on commit 1c2ee26

Please sign in to comment.