tostring.js (3955B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // MODULES // 22 23 var replace = require( '@stdlib/string/replace' ); 24 var real = require( '@stdlib/complex/real' ); 25 var imag = require( '@stdlib/complex/imag' ); 26 27 28 // VARIABLES // 29 30 var CTORS = { 31 'int8': 'new Int8Array( [ {{data}} ] )', 32 'uint8': 'new Uint8Array( [ {{data}} ] )', 33 'uint8c': 'new Uint8ClampedArray( [ {{data}} ] )', 34 'int16': 'new Int16Array( [ {{data}} ] )', 35 'uint16': 'new Uint16Array( [ {{data}} ] )', 36 'int32': 'new Int32Array( [ {{data}} ] )', 37 'uint32': 'new Uint32Array( [ {{data}} ] )', 38 'float32': 'new Float32Array( [ {{data}} ] )', 39 'float64': 'new Float64Array( [ {{data}} ] )', 40 'generic': '[ {{data}} ]', 41 'binary': 'new Buffer( [ {{data}} ] )', 42 'complex64': 'new Complex64Array( [ {{data}} ] )', 43 'complex128': 'new Complex128Array( [ {{data}} ] )' 44 }; 45 46 47 // MAIN // 48 49 /** 50 * Serializes an ndarray as a string. 51 * 52 * ## Notes 53 * 54 * - The method does **not** serialize data outside of the buffer region defined by the array configuration. 55 * 56 * @private 57 * @returns {string} string representation 58 */ 59 function toString() { // eslint-disable-line stdlib/no-redeclare 60 /* eslint-disable no-invalid-this */ 61 var buffer; 62 var ndims; 63 var ctor; 64 var str; 65 var dt; 66 var v; 67 var i; 68 69 ndims = this._shape.length; 70 dt = this._dtype; 71 72 // Function to invoke to create an ndarray: 73 str = 'ndarray( \''+dt+'\', '; 74 75 // Data buffer parameter... 76 buffer = ''; 77 if ( this._length <= 100 ) { 78 if ( dt === 'complex64' || dt === 'complex128' ) { 79 for ( i = 0; i < this._length; i++ ) { 80 v = this.iget( i ); 81 buffer += real( v ) + ', ' + imag( v ); 82 if ( i < this._length-1 ) { 83 buffer += ', '; 84 } 85 } 86 } else { 87 for ( i = 0; i < this._length; i++ ) { 88 buffer += this.iget( i ); 89 if ( i < this._length-1 ) { 90 buffer += ', '; 91 } 92 } 93 } 94 } else { 95 // First three values... 96 if ( dt === 'complex64' || dt === 'complex128' ) { 97 for ( i = 0; i < 3; i++ ) { 98 v = this.iget( i ); 99 buffer += real( v ) + ', ' + imag( v ); 100 if ( i < 2 ) { 101 buffer += ', '; 102 } 103 } 104 } else { 105 for ( i = 0; i < 3; i++ ) { 106 buffer += this.iget( i ); 107 if ( i < 2 ) { 108 buffer += ', '; 109 } 110 } 111 } 112 buffer += ', ..., '; 113 114 // Last three values... 115 if ( dt === 'complex64' || dt === 'complex128' ) { 116 for ( i = 2; i >= 0; i-- ) { 117 v = this.iget( this._length-1-i ); 118 buffer += real( v ) + ', ' + imag( v ); 119 if ( i > 0 ) { 120 buffer += ', '; 121 } 122 } 123 } else { 124 for ( i = 2; i >= 0; i-- ) { 125 buffer += this.iget( this._length-1-i ); 126 if ( i > 0 ) { 127 buffer += ', '; 128 } 129 } 130 } 131 } 132 ctor = CTORS[ this.dtype ]; 133 str += replace( ctor, '{{data}}', buffer ); 134 str += ', '; 135 136 // Array shape... 137 if ( ndims === 0 ) { 138 str += '[]'; 139 } else { 140 str += '[ ' + this._shape.join( ', ' ) + ' ]'; 141 } 142 str += ', '; 143 144 // Stride array... 145 str += '[ '; 146 if ( ndims === 0 ) { 147 str += '0'; 148 } else { 149 for ( i = 0; i < ndims; i++ ) { 150 if ( this._strides[ i ] < 0 ) { 151 str += -this._strides[ i ]; 152 } else { 153 str += this._strides[ i ]; 154 } 155 if ( i < ndims-1 ) { 156 str += ', '; 157 } 158 } 159 } 160 str += ' ]'; 161 str += ', '; 162 163 // Buffer offset: 164 str += '0'; 165 str += ', '; 166 167 // Order: 168 str += '\'' + this._order + '\''; 169 170 // Close the function call: 171 str += ' )'; 172 return str; 173 174 /* eslint-enable no-invalid-this */ 175 } 176 177 178 // EXPORTS // 179 180 module.exports = toString;