Skip to content

Commit

Permalink
Format complex values
Browse files Browse the repository at this point in the history
  • Loading branch information
mph- committed Nov 16, 2024
1 parent 2189a81 commit 8d57f7c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lcapy/labelmaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def _format_expr(self, expr):
def _format_value_units(self, value, units, style):

expr = Expr(value, cache=False)
if not expr.is_constant or units == '':
if not expr.is_constant:
return expr.latex_math()

return value_formatter(style=style).latex_math(expr, units)
Expand Down
17 changes: 14 additions & 3 deletions lcapy/valueformatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ def __init__(self, trim=True, hundreds=False, fmt=''):

def _do(self, expr, unit, aslatex):

if expr.is_complex:
jstr = '\mathrm{j}' if aslatex else 'j'

rexpr = expr.real
iexpr = expr.imag
if rexpr == 0:
return jstr + self._do1(iexpr, unit, aslatex)
return self._do1(rexpr, '', aslatex) + ' + ' + jstr + self._do1(iexpr, unit, aslatex)
return self._do1(expr, unit, aslatex)

def _do1(self, expr, unit, aslatex):

prefixes = ('f', 'p', 'n', 'u', 'm', '', 'k', 'M', 'G', 'T')

# FIXME for complex values
Expand Down Expand Up @@ -143,9 +155,8 @@ def _fmt(self, valstr, unit='', prefix='', aslatex=True):

class SciValueFormatter(ValueFormatter):

def _do(self, expr, unit, aslatex):
def _do1(self, expr, unit, aslatex):

# FIXME for complex values
value = expr.value

fmt = '%%.%dE' % (self.num_digits - 1)
Expand Down Expand Up @@ -177,7 +188,7 @@ def _do(self, expr, unit, aslatex):

class RatfunValueFormatter(ValueFormatter):

def _do(self, expr, unit, aslatex):
def _do1(self, expr, unit, aslatex):

if not aslatex:
return str(expr) + ' ' + unit
Expand Down

0 comments on commit 8d57f7c

Please sign in to comment.