From 2b373a1384a8489c042a5e48287074bef40522fc Mon Sep 17 00:00:00 2001 From: Peter Pinch Date: Wed, 31 Jan 2018 09:43:01 -0500 Subject: [PATCH] update functions to work in complex plane --- common/lib/calc/calc/calc.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/common/lib/calc/calc/calc.py b/common/lib/calc/calc/calc.py index 611b84ce44..9024b9fc6d 100644 --- a/common/lib/calc/calc/calc.py +++ b/common/lib/calc/calc/calc.py @@ -30,6 +30,11 @@ from pyparsing import ( import functions +# Functions available by default +# We use scimath variants which give complex results when needed. For example: +# np.sqrt(-4+0j) = 2j +# np.sqrt(-4) = nan, but +# np.lib.scimath.sqrt(-4) = 2j DEFAULT_FUNCTIONS = { 'sin': numpy.sin, 'cos': numpy.cos, @@ -37,13 +42,13 @@ DEFAULT_FUNCTIONS = { 'sec': functions.sec, 'csc': functions.csc, 'cot': functions.cot, - 'sqrt': numpy.sqrt, - 'log10': numpy.log10, - 'log2': numpy.log2, - 'ln': numpy.log, + 'sqrt': numpy.lib.scimath.sqrt, + 'log10': numpy.lib.scimath.log10, + 'log2': numpy.lib.scimath.log2, + 'ln': numpy.lib.scimath.log, 'exp': numpy.exp, - 'arccos': numpy.arccos, - 'arcsin': numpy.arcsin, + 'arccos': numpy.lib.scimath.arccos, + 'arcsin': numpy.lib.scimath.arcsin, 'arctan': numpy.arctan, 'arcsec': functions.arcsec, 'arccsc': functions.arccsc, @@ -59,11 +64,12 @@ DEFAULT_FUNCTIONS = { 'coth': functions.coth, 'arcsinh': numpy.arcsinh, 'arccosh': numpy.arccosh, - 'arctanh': numpy.arctanh, + 'arctanh': numpy.lib.scimath.arctanh, 'arcsech': functions.arcsech, 'arccsch': functions.arccsch, 'arccoth': functions.arccoth } + DEFAULT_VARIABLES = { 'i': numpy.complex(0, 1), 'j': numpy.complex(0, 1),