Commit 6cbb44f4 authored by Daniel Brown's avatar Daniel Brown Committed by Sean Leavey
Browse files

more symbol additions for sympy conversion

parent 1915b653
......@@ -43,6 +43,7 @@ PYFUNCTION_MAP = {
"abs": operator.abs,
"neg": operator.neg,
"pos": operator.pos,
"pow": operator.pow,
"conj": np.conj,
"real": np.real,
"imag": np.imag,
......@@ -69,6 +70,7 @@ FUNCTIONS = {
"abs": lambda x: Operation("abs", operator.abs, x),
"neg": lambda x: Operation("neg", operator.neg, x),
"pos": lambda x: Operation("pos", operator.pos, x),
"pow": lambda x: Operation("pow", operator.pow, x),
"conj": lambda x: Operation("conj", np.conj, x),
"real": lambda x: Operation("real", np.real, x),
"imag": lambda x: Operation("imag", np.imag, x),
......@@ -108,7 +110,15 @@ op_repr = {
def finesse2sympy(expr, iter_num=0):
It might be common for this this function to throw a NotImplementedError.
This function maps, by hand, various operator and numpy functions to sympy.
If you come across this error, you'll need to update the if-statement to
include the missing operations. Over time this should get fixed for most
use cases.
import sympy
from finesse.parameter import ParameterRef
iter_num += 1
......@@ -122,6 +132,10 @@ def finesse2sympy(expr, iter_num=0):
op = sympy.Mul
elif expr.op == operator.add:
op = sympy.Add
elif expr.op == operator.truediv:
op = lambda a, b: sympy.Mul(a, sympy.Pow(b, -1))
elif expr.op == operator.pow:
op = sympy.Pow
elif expr.op == operator.sub:
op = lambda x,y: sympy.Add(x,-y)
elif expr.op == np.conj:
......@@ -132,13 +146,15 @@ def finesse2sympy(expr, iter_num=0):
op = sympy.exp
elif expr.op == np.sqrt:
op = sympy.sqrt
elif expr.op == operator.abs:
op = sympy.Abs
elif expr.op == operator.neg:
op = lambda x: sympy.Mul(-1, x)
raise Exception(f"undefined Operation {expr.op} in {expr}")
raise NotImplementedError(f"undefined Operation {expr.op} in {expr}")
return op(*sympy_args)
raise Exception(f'{expr} undefined')
raise NotImplementedError(f'{expr} undefined')
def sympy2finesse(expr, symbol_dict={}, iter_num=0):
import sympy
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment