Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/example4/example4.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
v4 = latexexpr.Variable('v4',4.56)

def printExpr(e1,e2=''):
print('$$' + str(e1) + r'\ \ \ \ \ \ \ \ \ ' + str(e2) + '$$\n')
print(f'$${str(e1)}' + r'\ \ \ \ \ \ \ \ \ ' + str(e2) + '$$\n')

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function printExpr refactored with the following changes:


# simplify
print('\n\nsimplify')
Expand Down
88 changes: 49 additions & 39 deletions latexexpr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,11 @@ def strResult(self, format='', exponent=0):
"""
if self.isSymbolic():
return self.strSymbolic()
f = format if format else self.format
f = format or self.format
e = exponent if exponent != 0 else self.exponent
result = self.value
if e == 0:
if result < 0.:
return r'\left( %s \right)' % f % result
return '%s' % f % result
return r'\left( %s \right)' % f % result if result < 0. else f'{f}' % result
Comment on lines -215 to +219

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Variable.strResult refactored with the following changes:

val = self.value*math.pow(10, -e)
if self.value < 0.:
return r'\left( %s %s \right)' % (f % val, '\cdot 10^{%d}' % e)
Expand Down Expand Up @@ -293,8 +291,8 @@ def __str__(self):
F
"""
if self.isSymbolic():
return '%s' % (self.name)
return '%s = %s' % (self.name, self.strResultWithUnit())
return f'{self.name}'
return f'{self.name} = {self.strResultWithUnit()}'
Comment on lines -296 to +295

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Variable.__str__ refactored with the following changes:


def toLaTeXVariable(self, name, what='float', command='def'):
r"""Returns latex expression converting receiver to LaTeX variable using \def, \newcommand, or \renewcommand LaTeX command
Expand All @@ -317,10 +315,20 @@ def toLaTeXVariable(self, name, what='float', command='def'):
"""
what = what.lower()
whats = ('float', 'str', 'valunit', 'all', 'subst')
if not what in whats:
raise LaTeXExpressionError('%s not in %s' % (what, whats))
val = self.value if what == 'float' else self.strResult() if what == 'str' else self.strResultWithUnit(
) if what == 'valunit' else str(self) if what == 'all' or what == 'subst' else None
if what not in whats:
raise LaTeXExpressionError(f'{what} not in {whats}')
val = (
self.value
if what == 'float'
else self.strResult()
if what == 'str'
else self.strResultWithUnit()
if what == 'valunit'
else str(self)
if what in ['all', 'subst']
else None
)

Comment on lines -320 to +331

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Variable.toLaTeXVariable refactored with the following changes:

return toLaTeXVariable(name, val, command)

def toLaTeXVariableFloat(self, name, command='def'):
Expand Down Expand Up @@ -452,9 +460,11 @@ class Operation(object):
exponent = 0 # see :py:attr:`Variable.exponent`

def __init__(self, type, *args):
if not type in _supportedOperations:
raise LaTeXExpressionError('operation %s not in supported operations %s' % (
type, str(_supportedOperations)))
if type not in _supportedOperations:
raise LaTeXExpressionError(
f'operation {type} not in supported operations {str(_supportedOperations)}'
)

Comment on lines -455 to +467

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Operation.__init__ refactored with the following changes:

self.type = type
self.args = self.__checkArgs(args)
self.format = '%g'
Expand All @@ -471,7 +481,9 @@ def __checkArgs(self, args):
ret.append(Variable('%g' % a, a, format='%g'))
else:
raise TypeError(
"wrong argunemt type (%s) in Operation constructor" % a.__class__.__name__)
f"wrong argunemt type ({a.__class__.__name__}) in Operation constructor"
)

Comment on lines +474 to +486

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Operation.__checkArgs refactored with the following changes:

return ret

def __str(self, what):
Expand All @@ -495,11 +507,11 @@ def __str(self, what):
v0 = getattr(a[0], what)()
v1 = getattr(a[1], what)()
if t == _SUB:
return r'%s - %s' % (v0, v1)
return f'{v0} - {v1}'
if t == _DIV:
return r'\frac{ %s }{ %s }' % (v0, v1)
if t == _DIV2:
return r'%s / %s' % (v0, v1)
return f'{v0} / {v1}'
Comment on lines -498 to +514

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Operation.__str refactored with the following changes:

if t == _POW:
return r'{ %s }^{ %s }' % (v0, v1)
if t == _ROOT:
Expand All @@ -520,7 +532,7 @@ def __str(self, what):
if t == _ABS:
return r'\left| %s \right|' % v
if t == _SQR:
return r'%s^2' % v
return f'{v}^2'
if t == _SQRT:
return r'\sqrt{ %s }' % v
if t == _SIN:
Expand Down Expand Up @@ -552,8 +564,9 @@ def __str(self, what):
if _DEBUG:
print(t)
raise LaTeXExpressionError(t)
raise LaTeXExpressionError('operation %s not in supported operations %s' % (
self.type, str(_supportedOperations)))
raise LaTeXExpressionError(
f'operation {self.type} not in supported operations {str(_supportedOperations)}'
)

def strSymbolic(self):
r"""Returns string of symbolic representation of receiver
Expand Down Expand Up @@ -606,12 +619,10 @@ def strResult(self, format='', exponent=0):
if self.isSymbolic():
return self.strSymbolic()
r = float(self)
f = format if format else self.format
f = format or self.format
e = exponent if exponent != 0 else self.exponent
if e == 0:
if r < 0.:
return r'\left( %s \right)' % f % r
return '%s' % f % r
return r'\left( %s \right)' % f % r if r < 0. else f'{f}' % r
Comment on lines -609 to +625

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Operation.strResult refactored with the following changes:

val = r*math.pow(10, -e)
if r < 0.:
return r'\left( %s %s \right)' % (f % val, '\cdot 10^{%d}' % e)
Expand Down Expand Up @@ -707,8 +718,9 @@ def result(self):
if _DEBUG:
print(t)
raise LaTeXExpressionError(t)
raise LaTeXExpressionError('operation %s not in supported operations %s' % (
self.type, str(_supportedOperations)))
raise LaTeXExpressionError(
f'operation {self.type} not in supported operations {str(_supportedOperations)}'
)
Comment on lines -710 to +723

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Operation.result refactored with the following changes:


def __float__(self):
"""Returns numeric result of the receiver
Expand Down Expand Up @@ -758,7 +770,7 @@ def __str__(self):
"""
if self.isSymbolic():
return self.strSymbolic()
return '%s = %s' % (self.strSymbolic(), self.strSubstituted())
return f'{self.strSymbolic()} = {self.strSubstituted()}'
Comment on lines -761 to +773

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Operation.__str__ refactored with the following changes:


def toVariable(self, newName='', **kw):
"""Returns new Variable instance with attributes copied from receiver
Expand Down Expand Up @@ -1129,12 +1141,10 @@ def strResult(self, format='', exponent=0):
if self.isSymbolic():
return self.operation.strSubstituted()
r = float(self)
f = format if format else self.format
f = format or self.format
e = exponent if exponent != 0 else self.exponent
if e == 0:
if r < 0:
return r'\left(%s\right)' % f % r
return '%s' % f % r
return r'\left(%s\right)' % f % r if r < 0 else f'{f}' % r
Comment on lines -1132 to +1147

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Expression.strResult refactored with the following changes:

val = float(self)*math.pow(10, -e)
if r < 0:
return r'\left( %s %s \right)' % (f % val, '\cdot 10^{%d}' % e)
Expand Down Expand Up @@ -1229,8 +1239,8 @@ def __str__(self):
E_2 = \frac{ {a_{22}} + {F} }{ {F} } = \frac{ 3.45 + 5.87693 }{ { 434 \cdot 10^{-2} } } = 2.14906 \ \mathrm{mm}
"""
if self.isSymbolic():
return '%s = %s' % (self.name, self.operation)
return '%s = %s = %s' % (self.name, self.operation, self.strResultWithUnit())
return f'{self.name} = {self.operation}'
return f'{self.name} = {self.operation} = {self.strResultWithUnit()}'
Comment on lines -1232 to +1243

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Expression.__str__ refactored with the following changes:


def toLaTeXVariable(self, name, what='float', command='def'):
r"""Returns latex expression converting receiver to LaTeX variable using \def, \newcommand, or \renewcommand LaTeX command
Expand Down Expand Up @@ -1260,8 +1270,8 @@ def toLaTeXVariable(self, name, what='float', command='def'):
"""
what = what.lower()
whats = ('float', 'str', 'valunit', 'symb', 'subst', 'all')
if not what in whats:
raise LaTeXExpressionError('%s not in %s' % (what, whats))
if what not in whats:
raise LaTeXExpressionError(f'{what} not in {whats}')
Comment on lines -1263 to +1274

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Expression.toLaTeXVariable refactored with the following changes:

val = float(self) if what == 'float' else self.strResult() if what == 'str' else self.strResultWithUnit() if what == 'valunit' else self.strSymbolic(
) if what == 'symb' else self.strSubstituted() if what == 'subst' else str(self) if what == 'all' else None
return toLaTeXVariable(name, val, command)
Expand Down Expand Up @@ -1386,7 +1396,7 @@ def toLaTeXVariable(name, what, command='def'):
"""
if command == 'def':
return r'\def\%s{%s}' % (name, what)
elif command == 'newcommand' or command == 'renewcommand':
elif command in ['newcommand', 'renewcommand']:
Comment on lines -1389 to +1399

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function toLaTeXVariable refactored with the following changes:

return r'\%s{\%s}{%s}' % (command, name, what)
else:
raise LaTeXExpressionError(
Expand Down Expand Up @@ -1436,9 +1446,9 @@ def toLaTeXVariable(name, what, command='def'):
print(v8)

v3 = Variable('F', 4.34, 'kN', exponent=-2)
print(str(v3))
print(v3)
v8 = Variable('F', None, 'kN')
print(str(v8))
print(v8)
Comment on lines -1439 to +1451

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 1439-1551 refactored with the following changes:


v1 = Variable('a_{22}', 3.45, 'mm')
print(v1.strSymbolic())
Expand Down Expand Up @@ -1488,7 +1498,7 @@ def toLaTeXVariable(name, what, command='def'):
v2 = Variable('F', 5.876934835, 'kN')
v3 = Variable('F', 4.34, 'kN', exponent=-2)
o3 = (v1+v2)/v3
print(str(o3))
print(o3)

v1 = Variable('a_{22}', 3.45, 'mm')
v2 = Variable('F', 5.876934835, 'kN')
Expand Down Expand Up @@ -1548,7 +1558,7 @@ def toLaTeXVariable(name, what, command='def'):
v2 = Variable('F', 5.876934835, 'kN')
v3 = Variable('F', 4.34, 'kN', exponent=-2)
e2 = Expression('E_2', (v1+v2)/v3, 'mm')
print(str(e2))
print(e2)

v1 = Variable('a_{22}', 3.45, 'mm')
v2 = Variable('F', 5.876934835, 'kN')
Expand Down
27 changes: 8 additions & 19 deletions latexexpr/sympy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def _operation2sympy(arg, varMap=None, substituteFloats=True):
if isinstance(arg, latexexpr.Expression):
return _operation2sympy(arg.operation, varMap, sf)
if not isinstance(arg, latexexpr.Operation):
raise TypeError("TODO " + str(type(arg)) + str(arg))
raise TypeError(f"TODO {str(type(arg))}{str(arg)}")

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _operation2sympy refactored with the following changes:

t = arg.type
if t in latexexpr._supportedOperationsN:
if t == latexexpr._ADD:
Expand All @@ -94,7 +94,7 @@ def _operation2sympy(arg, varMap=None, substituteFloats=True):
if t == latexexpr._SUB:
sympyOp, args = sympy.Add, (_o2s(
a[0], varMap, sf), sympy.Mul(-1, _o2s(a[1], varMap, sf)))
elif t == latexexpr._DIV or t == latexexpr._DIV2:
elif t in [latexexpr._DIV, latexexpr._DIV2]:
sympyOp, args = sympy.Mul, (_o2s(
a[0], varMap, sf), sympy.power.Pow(_o2s(a[1], varMap, sf), -1))
elif t == latexexpr._POW:
Expand Down Expand Up @@ -172,11 +172,6 @@ def _sympy2operation(sympyExpr, varMap):
return -args[1]
if isinstance(args[1], latexexpr.Variable) and args[1].name == '-1':
return -args[0]
elif len(args) == 2 and isinstance(args[1], latexexpr.Operation) and args[1].type == latexexpr._DIV:
if args[1].args[0].value == 1.:
return args[0] / args[1].args[1]
if all(a.type == latexexpr._LN for a in (args[0], args[1].args[0])):
return latexexpr._LOG(args[0], args[0].args[1])
Comment on lines -175 to -179

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _sympy2operation refactored with the following changes:

for i, a in enumerate(args):
t = a.type if isinstance(a, latexexpr.Operation) else a.operation.type if isinstance(
a, latexexpr.Expression) else None
Expand Down Expand Up @@ -273,8 +268,7 @@ def simplify(arg, substituteFloats=False, **kw):
s, lVars = _operation2sympy(arg, substituteFloats=substituteFloats)
s = sympy.simplify(s, **kw)
return _sympy2operation(s, lVars)
raise TypeError("Unsupported type (%s) for simplify" %
(arg.__class__.__name__))
raise TypeError(f"Unsupported type ({arg.__class__.__name__}) for simplify")
Comment on lines -276 to +271

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function simplify refactored with the following changes:



latexexpr.Expression.simplify = lambda self, substituteFloats=False, **kw: _setOperation(
Expand Down Expand Up @@ -316,8 +310,7 @@ def expand(arg, substituteFloats=False, **kw):
s, lVars = _operation2sympy(arg, substituteFloats=substituteFloats)
s = sympy.expand(s, **kw)
return _sympy2operation(s, lVars)
raise TypeError("Unsupported type (%s) for expand" %
(arg.__class__.__name__))
raise TypeError(f"Unsupported type ({arg.__class__.__name__}) for expand")
Comment on lines -319 to +313

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function expand refactored with the following changes:



latexexpr.Expression.expand = lambda self, substituteFloats=False, **kw: _setOperation(
Expand Down Expand Up @@ -358,8 +351,7 @@ def factor(arg, substituteFloats=False, **kw):
s, lVars = _operation2sympy(arg, substituteFloats=substituteFloats)
s = sympy.factor(s, **kw)
return _sympy2operation(s, lVars)
raise TypeError("Unsupported type (%s) for factor" %
(arg.__class__.__name__))
raise TypeError(f"Unsupported type ({arg.__class__.__name__}) for factor")
Comment on lines -361 to +354

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function factor refactored with the following changes:



latexexpr.Expression.factor = lambda self, substituteFloats=False, **kw: _setOperation(
Expand Down Expand Up @@ -402,8 +394,7 @@ def collect(arg, syms, substituteFloats=False, **kw):
sympy.Symbol(s.name) for s in syms]
s = sympy.collect(s, syms, **kw)
return _sympy2operation(s, lVars)
raise TypeError("Unsupported type (%s) for collect" %
(arg.__class__.__name__))
raise TypeError(f"Unsupported type ({arg.__class__.__name__}) for collect")
Comment on lines -405 to +397

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function collect refactored with the following changes:



latexexpr.Expression.collect = lambda self, syms, substituteFloats=False, **kw: _setOperation(
Expand Down Expand Up @@ -447,8 +438,7 @@ def cancel(arg, substituteFloats=False, **kw):
s, lVars = _operation2sympy(arg, substituteFloats=substituteFloats)
s = sympy.cancel(s, **kw)
return _sympy2operation(s, lVars)
raise TypeError("Unsupported type (%s) for cancel" %
(arg.__class__.__name__))
raise TypeError(f"Unsupported type ({arg.__class__.__name__}) for cancel")
Comment on lines -450 to +441

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function cancel refactored with the following changes:



latexexpr.Expression.cancel = lambda self, substituteFloats=False, **kw: _setOperation(
Expand Down Expand Up @@ -484,8 +474,7 @@ def apart(arg, substituteFloats=False, **kw):
s, lVars = _operation2sympy(arg, substituteFloats=substituteFloats)
s = sympy.apart(s, **kw)
return _sympy2operation(s, lVars)
raise TypeError("Unsupported type (%s) for apart" %
(arg.__class__.__name__))
raise TypeError(f"Unsupported type ({arg.__class__.__name__}) for apart")
Comment on lines -487 to +477

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function apart refactored with the following changes:



latexexpr.Expression.apart = lambda self, substituteFloats=False, **kw: _setOperation(
Expand Down