Skip to content

Commit

Permalink
Fixed bugs and added support for some functions
Browse files Browse the repository at this point in the history
  • Loading branch information
aryan26roy committed Sep 4, 2023
1 parent e0244fd commit 2612ef3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
24 changes: 15 additions & 9 deletions src/formulate/AST.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def __str__(self):
return "{0}({1})".format(str(self.sign), self.operand)

def unary_to_ufunc(self, sign):
signmap = {"~": "um.invert", "!": "not"}
signmap = {"~": "np.invert", "!": "np.logical_not"}
return signmap[str(sign)]

def to_python(self):
Expand Down Expand Up @@ -84,8 +84,8 @@ def __str__(self):

def binary_to_ufunc(self, sign):
sign_mapping = {
"&": "um.bitwise_or",
"|": "um.bitwise_or",
"&": "np.bitwise_and",
"|": "np.bitwise_or",
"&&": "and",
"||": "or",
}
Expand Down Expand Up @@ -138,11 +138,8 @@ def __str__(self):
return "{0}[{1}]".format(str(self.var), ",".join(str(x) for x in self.paren))

def to_python(self):
slices = ""
for elem in self.paren:
slices += "[" + elem.to_python() + "]"
pycode = "(" + str(self.var.to_python()) + slices + ")"
return pycode
temp_str = [ "," + elem.to_python() for elem in self.paren]
return "(" + str(self.var.to_python()) + "[:" + "".join(temp_str)+"]" + ")"


@dataclass
Expand Down Expand Up @@ -179,7 +176,16 @@ def __str__(self):
self.function,
", ".join(str(x) for x in self.arguments),
)


def to_python(self):
print(str(self.function))
match str(self.function):
case "pi":
return "np.pi"
case "e":
return "np.exp(1)"
case _ :
raise ValueError("Not a valid function!")

# come-up with a shared notation for functions
# make a to_python function
5 changes: 3 additions & 2 deletions src/formulate/toast.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@
}

FUNC_MAPPING = {
"MATH::PI": "pi",
"LENGTH$": "no_of_entries",
"MATH::PI": "pi", #np.pi
"MATH::E": "e",
"LENGTH$": "no_of_entries", #
"ITERATION$": "current_iteration",
"SUM$": "sum",
"MIN$": "min",
Expand Down

0 comments on commit 2612ef3

Please sign in to comment.