def _array2string(self, prefix=''): # Mimic numpy >=1.12 array2string, in which structured arrays are # typeset taking into account all printoptions. # TODO: in final numpy 1.12, the scalar case should work as well; # see https://github.com/numpy/numpy/issues/8172 values = self._values if NUMPY_LT_1_12 or self.isscalar: # Mimic StructureFormat from numpy >=1.12 assuming float-only data. from numpy.core.arrayprint import FloatFormat opts = np.get_printoptions() format_functions = [ FloatFormat(np.atleast_1d(values[component]).ravel(), precision=opts['precision'], suppress_small=opts['suppress']) for component in self.components ] def fmt(x): return '({})'.format(', '.join( format_function(field) for field, format_function in zip(x, format_functions))) # Before 1.12, structures arrays were set as "numpystr", # so that is the formmater we need to replace. formatter = {'numpystr': fmt} else: fmt = repr formatter = {} return np.array2string(values, formatter=formatter, style=fmt, separator=', ', prefix=prefix)
def tostring(arr, max_line_width=75, precision=8, suppress_small=False, separator=' ', array_output=0): r""" Returns a textual representation of a number or field of numbers. Each dimension is indicated by a pair of matching square brackets (`[]`), within which each subset of the field is output. The orientation of the dimensions is as follows: the last (rightmost) dimension is always horizontal, so that the frequent rank-1 fields use a minimum of screen real-estate. The next-to-last dimesnion is displayed vertically if present and any earlier dimension is displayed with additional bracket divisions. :Parameters: - `max\_line\_width`: the maximum number of characters used in a single line. Default is `sys.output_line_width` or 77. - `precision`: the number of digits after the decimal point. Default is `sys.float_output_precision` or 8. - `suppress_small`: whether small values should be suppressed (and output as `0`). Default is `sys.float_output_suppress_small` or `false`. - `separator`: what character string to place between two numbers. - `array_output`: Format output for an `eval`. Only used if `arr` is a `Numeric` `array`. >>> from fipy import Variable >>> print tostring(Variable((1,0,11.2345)), precision=1) [ 1. 0. 11.2] >>> print tostring(array((1,2)), precision=5) [1 2] >>> print tostring(array((1.12345,2.79)), precision=2) [ 1.12 2.79] >>> print tostring(1) 1 >>> print tostring(array(1)) 1 >>> print tostring(array([1.23345]), precision=2) [ 1.23] >>> print tostring(array([1]), precision=2) [1] >>> print tostring(1.123456, precision=2) 1.12 >>> print tostring(array(1.123456), precision=3) 1.123 """ if _isPhysical(arr): return arr.tostring(max_line_width=max_line_width, precision=precision, suppress_small=suppress_small, separator=separator) elif isinstance(arr, NUMERIX.ndarray) and arr.shape != (): return NUMERIX.array2string(arr, precision=precision, max_line_width=max_line_width, suppress_small=suppress_small, separator=separator) elif isFloat(arr): try: ## this is for numpy 1.0.4 and above ## why has the interface changed again? from numpy.core.arrayprint import FloatFormat return FloatFormat(NUMERIX.array((arr, )), precision, suppress_small)(arr).strip() except: from numpy.core.arrayprint import _floatFormat, _formatFloat return _formatFloat(arr, format='%%1.%df' % precision) elif isInt(arr): try: ## this is for numpy 1.7 and above ## why has the interface changed again? from numpy.core.arrayprint import IntegerFormat return IntegerFormat(NUMERIX.array((arr, )))(arr).strip() except: from numpy.core.arrayprint import _formatInteger return _formatInteger(arr, format='%d') else: raise TypeError, 'cannot convert ' + str(arr) + ' to string'