Refactrored poly2str and poly2hstr

master
Per A Brodtkorb 8 years ago
parent dfae5bf3d7
commit 23e3c5f29b

@ -442,8 +442,8 @@ class LevelCrossings(PlotData):
max_L = max(L)
maxi = max(abs(r_[min_L, max_L])) * epsilon
mini = -maxi
u = linspace(mini, maxi, 101)
nu = 101
u = linspace(mini, maxi, nu)
G = cdfnorm(u) # (1 + erf(u / sqrt(2))) / 2
G = G * (1 - G)
@ -451,7 +451,7 @@ class LevelCrossings(PlotData):
factor1 = 1. / sqrt(1 - x ** 2)
factor2 = 1. / (1 + x)
integral = zeros(u.shape, dtype=float)
for i in range(len(integral)):
for i in range(nu):
y = factor1 * exp(-u[i] * u[i] * factor2)
integral[i] = trapz(y, x)
# end

@ -629,11 +629,44 @@ def poly2hstr(p, variable='x'):
>>> import wafo.polynomial as wp
>>> wp.poly2hstr([1, 1, 2], 's' )
'(s + 1)*s + 2'
>>> wp.poly2hstr([2, 1, 2, 1], 's' )
'((2*s + 1)*s + 2)*s + 1'
>>> wp.poly2hstr([2, 0, 2, 1], 's' )
'(2*s**2 + 2)*s + 1'
See also
--------
poly2str
"""
def _append_coef(s, coef, expon, var):
# Is it the first term?
isfirst = s == ''
# Add sign, but we don't need a leading plus-sign.
if isfirst:
if coef < 0:
s = '-' # Unary minus.
else:
sgn = '-' if coef < 0 else '+'
s = '{0:s} {1:s} '.format(s, sgn)
# We need the coefficient only if it is different from 1 or -1 or
# when it is the constant term.
needcoef = ((abs(coef) != 1) | (expon == 0) & isfirst) | 1 - isfirst
# Append the coefficient if it is different from one or when it is
# the constant term.
if needcoef:
s = '{0:s}{1:.20g}'.format(s, abs(coef))
# We need the variable except in the constant term.
needvar = expon != 0
# Append variable if necessary.
if needvar:
# Append a multiplication sign if necessary.
if needcoef:
if 1 - isfirst:
s = '({0:s})'.format(s)
s = '{0:s}*'.format(s)
s = '{0:s}{1:s}'.format(s, var)
return s
var = variable
coefs = polytrim(np.atleast_1d(p))
@ -649,44 +682,9 @@ def poly2hstr(p, variable='x'):
else:
# Append exponent if necessary.
if ix > 1:
exponstr = '%.0f' % ix
s = '%s**%s' % (s, exponstr)
s = '{0:s}**{1:d}'.format(s, ix)
ix = 1
# Is it the first term?
isfirst = s == ''
# We need the coefficient only if it is different from 1 or -1 or
# when it is the constant term.
needcoef = ((abs(coef) != 1) |
(expon == 0) & isfirst) | 1 - isfirst
# We need the variable except in the constant term.
needvar = (expon != 0)
# Add sign, but we don't need a leading plus-sign.
if isfirst:
if coef < 0:
s = '-' # % Unary minus.
else:
if coef < 0:
s = '%s - ' % s # % Binary minus (subtraction).
else:
s = '%s + ' % s # % Binary plus (addition).
# Append the coefficient if it is different from one or when it is
# the constant term.
if needcoef:
coefstr = '%.20g' % abs(coef)
s = '%s%s' % (s, coefstr)
# Append variable if necessary.
if needvar:
# Append a multiplication sign if necessary.
if needcoef:
if 1 - isfirst:
s = '(%s)' % s
s = '%s*' % s
s = '%s%s' % (s, var)
s = _append_coef(s, coef, expon, var)
# Now treat the special case where the polynomial is zero.
if s == '':
@ -720,52 +718,68 @@ def poly2str(p, variable='x'):
>>> import wafo.polynomial as wp
>>> wp.poly2str([1, 1, 2], 's' )
's**2 + s + 2'
>>> wp.poly2str([2, 1, 2, 0, 1], 's' )
'2*s**4 + s**3 + 2*s**2 + 1'
"""
thestr = "0"
var = variable
# Remove leading zeros
coeffs = polytrim(np.atleast_1d(p))
N = len(coeffs) - 1
for k in range(N+1):
coefstr = '%.4g' % abs(coeffs[k])
if coefstr[-4:] == '0000':
coefstr = coefstr[:-5]
power = (N - k)
if power == 0:
def _coefstr_0(coefstr, k):
if coefstr != '0':
newstr = '%s' % (coefstr,)
newstr = '{0:s}'.format(coefstr)
else:
newstr = '0' if k == 0 else ''
elif power == 1:
return newstr
def _coefstr_1(coefstr, var):
if coefstr == '0':
newstr = ''
elif coefstr in ['b', '1']:
newstr = var
else:
newstr = '%s*%s' % (coefstr, var)
else:
newstr = '{0:s}*{1:s}'.format(coefstr, var)
return newstr
def _coefstr_n(coefstr, var, power):
if coefstr == '0':
newstr = ''
elif coefstr in ['b', '1']:
newstr = '%s**%d' % (var, power,)
newstr = '{0:s}**{1:d}'.format(var, power)
else:
newstr = '%s*%s**%d' % (coefstr, var, power)
newstr = '{0:s}*{1:s}**{2:d}'.format(coefstr, var, power)
return newstr
def _add_strings(thestr, newstr, ck, k):
if k > 0:
if newstr != '':
if coeffs[k] < 0:
thestr = "%s - %s" % (thestr, newstr)
else:
thestr = "%s + %s" % (thestr, newstr)
elif (k == 0) and (newstr != '') and (coeffs[k] < 0):
thestr = "-%s" % (newstr,)
sgn = '-' if ck < 0 else '+'
thestr = "{0:s} {1:s} {2:s}".format(thestr, sgn, newstr)
elif (k == 0) and (newstr != '') and (ck < 0):
thestr = "-{0:s}".format(newstr)
else:
thestr = newstr
return thestr
thestr = "0"
var = variable
# Remove leading zeros
coeffs = polytrim(np.atleast_1d(p))
N = len(coeffs) - 1
for k, ck in enumerate(coeffs):
coefstr = '%.4g' % abs(ck)
if coefstr[-4:] == '0000':
coefstr = coefstr[:-5]
power = (N - k)
if power == 0:
newstr = _coefstr_0(coefstr, k)
elif power == 1:
newstr = _coefstr_1(coefstr, var)
else:
newstr = _coefstr_n(coefstr, var, power)
thestr = _add_strings(thestr, newstr, ck, k)
return thestr
def polyshift(py, a=-1, b=1):
"""

Loading…
Cancel
Save