printArray
Prints javascript arrays with easy 1D, 2D and rebroadcast API.
How to use
The API is simple, just aprint(array, numOfDigits, [shapex, shapey])
.
Leaving out [shapex, shapey] will print 1D row vectors if array is 1D or
2D arrays if array is 2D. Default number of digits is 5 if numOfDigits
is left out.
var aprint = //////////////////////////////////////////////////var ex = "Example 1: Vectors printed as vector"var dum = 2 3 4 5 6 7 7 8 9 10 11 12//////////////////////////////////////////////////var digits = 8ex = "Example 2: 1D array printed as nxm array"//////////////////////////////////////////////////ex = "Example 3: 1D array printed as mxn array"//////////////////////////////////////////////////ex = "Example 4: 2D array printed as 2D array"digits = 4var dum2 = 1 2 3 4 5 6 2 3 4 5 6 7 3 4 5 6 7 8 4 5 6 7 8 9//////////////////////////////////////////////////ex = "Example 5: 2D array printed row-wise as 1D row vector"//////////////////////////////////////////////////ex = "Example 6: 2D array printed as column vector"//////////////////////////////////////////////////ex = "Example 7: 2D array broadcast into different shape"//////////////////////////////////////////////////ex = "Example 8: Default handling of negative numbers with extra spacing"dum2 = //////////////////////////////////////////////////ex = "Example 9: 2D array broadcast as wrong dimensions"try catch err console // Helper funcs ////////////////////////////////// { console console console} { var i result = fori = 0; i < array2Dlength; i++ resulti = array2Di return result}
which outputs
----------------------Example 1: Vectors printed as vector 1.414 1.732 2.000 2.236 2.449 2.645 2.645 2.828 3.000 3.162 3.316 3.464----------------------Example 2: 1D array printed as nxm array 1.414213 1.732050 2.0000002.236068 2.449489 2.6457512.645751 2.828427 3.0000003.162277 3.316624 3.464101----------------------Example 3: 1D array printed as mxn array 1.414213 1.732050 2.000000 2.2360682.449489 2.645751 2.645751 2.8284273.000000 3.162277 3.316624 3.464101----------------------Example 4: 2D array printed as 2D array 1.00 2.00 3.00 4.00 5.00 6.002.00 3.00 4.00 5.00 6.00 7.003.00 4.00 5.00 6.00 7.00 8.004.00 5.00 6.00 7.00 8.00 9.00----------------------Example 5: 2D array printed row-wise as 1D row vector 1.002.003.004.005.006.002.003.004.005.006.007.003.004.005.006.007.008.004.005.006.007.008.009.00----------------------Example 6: 2D array printed as column vector 1.00 2.00 3.00 4.00 5.00 6.00 2.00 3.00 4.00 5.00 6.00 7.00 3.00 4.00 5.00 6.00 7.00 8.00 4.00 5.00 6.00 7.00 8.00 9.00----------------------Example 7: 2D array broadcast into different shape 1.00 2.00 3.00 4.005.00 6.00 2.00 3.004.00 5.00 6.00 7.003.00 4.00 5.00 6.007.00 8.00 4.00 5.006.00 7.00 8.00 9.00----------------------Example 8: Default handling of negative numbers with extra spacing 0.54 -0.41 -0.98 -0.65 0.28 0.96 -0.41 -0.98-0.65 0.28 0.96 0.75-0.98 -0.65 0.28 0.96 0.75 -0.14 -0.65 0.28 0.96 0.75 -0.14 -0.91----------------------Example 9: 2D array broadcast as wrong dimensions Error: wrong dimensions
License
MIT
Copyright (C)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.