MCPcopy Create free account
hub / github.com/numpy/numpy / _generate_string

Method _generate_string

numpy/polynomial/_polybase.py:360–396  ·  view source on GitHub ↗

Generate the full string representation of the polynomial, using ``term_method`` to generate each polynomial term.

(self, term_method)

Source from the content-addressed store, hash-verified

358 return self._generate_string(self._str_term_ascii)
359
360 def _generate_string(self, term_method):
361 """
362 Generate the full string representation of the polynomial, using
363 ``term_method`` to generate each polynomial term.
364 """
365 # Get configuration for line breaks
366 linewidth = np.get_printoptions().get('linewidth', 75)
367 if linewidth < 1:
368 linewidth = 1
369 out = pu.format_float(self.coef[0])
370 for i, coef in enumerate(self.coef[1:]):
371 out += " "
372 power = str(i + 1)
373 # Polynomial coefficient
374 # The coefficient array can be an object array with elements that
375 # will raise a TypeError with >= 0 (e.g. strings or Python
376 # complex). In this case, represent the coefficient as-is.
377 try:
378 if coef >= 0:
379 next_term = f"+ " + pu.format_float(coef, parens=True)
380 else:
381 next_term = f"- " + pu.format_float(-coef, parens=True)
382 except TypeError:
383 next_term = f"+ {coef}"
384 # Polynomial term
385 next_term += term_method(power, self.symbol)
386 # Length of the current line with next term added
387 line_len = len(out.split('\n')[-1]) + len(next_term)
388 # If not the last term in the polynomial, it will be two
389 # characters longer due to the +/- with the next term
390 if i < len(self.coef[1:]) - 1:
391 line_len += 2
392 # Handle linebreaking
393 if line_len >= linewidth:
394 next_term = next_term.replace(" ", "\n", 1)
395 out += next_term
396 return out
397
398 @classmethod
399 def _str_term_unicode(cls, i, arg_str):

Callers 2

__format__Method · 0.95
__str__Method · 0.95

Calls 3

replaceMethod · 0.80
getMethod · 0.45
splitMethod · 0.45

Tested by

no test coverage detected