Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 | 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 382x 382x 65x 65x 317x 382x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 400x 400x 400x 400x 400x 400x 400x 400x 104x 400x 296x 296x 400x 400x 136x 136x 136x 136x 264x 400x 72x 72x 18x 18x 54x 54x 72x 2x 72x 2x 2x 2x 2x 2x 2x 52x 48x 48x 54x 54x 54x 192x 400x 64x 64x 64x 64x 128x 400x 13x 13x 13x 13x 3x 3x 10x 10x 10x 2x 10x 2x 2x 2x 2x 2x 2x 8x 4x 4x 10x 10x 10x 115x 400x 29x 29x 29x 86x 400x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 1272x 1272x 1272x 1272x 1272x 1272x 1272x 1272x 1272x 1272x 1272x 1272x 1272x 1272x 1272x 1272x 1272x 1x 1x 1x 1271x 1271x 1271x 1272x 68x 5x 5x 63x 68x 30x 30x 9x 9x 1203x 1203x 1203x 1268x 211x 211x 33x 1x 1x 32x 32x 178x 193x 30x 30x 12x 12x 17x 17x 148x 189x 15x 1x 1x 15x 13x 13x 1x 1x 1x 133x 189x 5x 5x 128x 189x 38x 38x 16x 16x 992x 992x 992x 1263x 310x 310x 57x 24x 24x 33x 57x 5x 5x 28x 57x 1x 1x 27x 27x 253x 310x 99x 3x 3x 96x 99x 27x 27x 42x 42x 154x 271x 63x 63x 271x 48x 48x 43x 271x 3x 3x 3x 40x 310x 1x 1x 39x 271x 2x 2x 37x 37x 37x 37x 682x 682x 682x 1259x 292x 292x 61x 24x 24x 61x 15x 15x 61x 9x 9x 13x 13x 231x 292x 55x 55x 292x 55x 55x 121x 292x 13x 12x 12x 1x 1x 1x 108x 292x 32x 3x 3x 29x 32x 24x 24x 5x 5x 5x 76x 292x 53x 53x 292x 9x 9x 14x 14x 14x 14x 390x 390x 390x 1259x 260x 55x 55x 260x 55x 55x 150x 260x 58x 26x 26x 32x 32x 32x 58x 3x 3x 29x 58x 24x 24x 5x 5x 5x 92x 260x 54x 54x 260x 15x 15x 260x 9x 9x 14x 14x 14x 130x 130x 130x 1259x 33x 33x 1259x 33x 33x 1259x 13x 13x 1259x 13x 13x 1259x 15x 15x 1259x 9x 9x 14x 14x 14x 1272x 14x 14x 14x 14x 14x | /**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* eslint-disable max-len */
'use strict';
// MODULES //
var isNonNegativeIntegerArray = require( '@stdlib/assert/is-nonnegative-integer-array' ).primitives;
var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive;
var isIterableLike = require( '@stdlib/assert/is-iterable-like' );
var isCollection = require( '@stdlib/assert/is-collection' );
var isArrayBuffer = require( '@stdlib/assert/is-arraybuffer' );
var isPlainObject = require( '@stdlib/assert/is-plain-object' );
var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
var isComplexDataType = require( '@stdlib/ndarray/base/assert/is-complex-floating-point-data-type' );
var isGenericDataType = require( '@stdlib/ndarray/base/assert/is-generic-data-type' );
var isBinaryDataType = require( '@stdlib/ndarray/base/assert/is-binary-data-type' );
var isDataType = require( '@stdlib/ndarray/base/assert/is-data-type' );
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
var flatten2d = require( '@stdlib/array/base/flatten2d' );
var slice = require( '@stdlib/array/base/slice' );
var arrayShape = require( '@stdlib/array/shape' );
var typedarray = require( '@stdlib/array/typed' );
var iterator2array = require( '@stdlib/array/from-iterator' );
var buffer = require( '@stdlib/ndarray/base/buffer' );
var bytesPerElement = require( '@stdlib/ndarray/base/bytes-per-element' );
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
var numel = require( '@stdlib/ndarray/base/numel' );
var ndarray = require( '@stdlib/ndarray/ctor' );
var defaults = require( '@stdlib/ndarray/defaults' );
var array2buffer = require( '@stdlib/buffer/from-array' );
var arraybuffer2ndarray = require( '@stdlib/ndarray/from-arraybuffer' );
var ITERATOR_SYMBOL = require( '@stdlib/symbol/iterator' );
var format = require( '@stdlib/string/format' );
// VARIABLES //
var DEFAULT_DTYPE = defaults.get( 'dtypes.default' );
var DEFAULT_ORDER = defaults.get( 'order' );
// FUNCTIONS //
/**
* Resolves the order of the output matrix.
*
* @private
* @param {*} options - options argument
* @returns {string} order
*/
function resolveOrder( options ) {
if ( hasOwnProp( options, 'order' ) ) {
return options.order;
}
return DEFAULT_ORDER;
}
/**
* Returns a matrix having a specified data type.
*
* @private
* @param {(NonNegativeIntegerArray|Collection|ArrayBuffer|Iterable|Options)} arg - shape, array-like object, ArrayBuffer, iterable, or options object
* @param {*} dtype - data type
* @param {(Options|null)} options - function options
* @param {boolean} [options.readonly=false] - boolean indicating whether to return a read-only ndarray
* @param {string} [options.mode='throw'] - specifies how to handle indices which exceed ndarray dimensions
* @param {StringArray} [options.submode=[options.mode]] - specifies how to handle subscripts which exceed array dimensions on a per dimension basis
* @returns {(ndarray|null)} two-dimensional ndarray
*
* @example
* var getDType = require( '@stdlib/ndarray/dtype' );
* var getShape = require( '@stdlib/ndarray/shape' );
*
* var v = matrixWithDType( [ 2, 5 ], 'float64', {} );
* // returns <ndarray>
*
* var sh = getShape( v );
* // returns [ 2, 5 ]
*
* var dt = String( getDType( v ) );
* // returns 'float64'
*
* @example
* var getDType = require( '@stdlib/ndarray/dtype' );
* var getShape = require( '@stdlib/ndarray/shape' );
*
* var v = matrixWithDType( [ [ 1, 2 ], [ 3, 4 ] ], 'generic', {} );
* // returns <ndarray>
*
* var sh = getShape( v );
* // returns [ 2, 2 ]
*
* var dt = String( getDType( v ) );
* // returns 'generic'
*
* @example
* var getDType = require( '@stdlib/ndarray/dtype' );
* var getShape = require( '@stdlib/ndarray/shape' );
* var ArrayBuffer = require( '@stdlib/array/buffer' );
*
* var buf = new ArrayBuffer( 32 );
* var v = matrixWithDType( buf, 'float64', {} );
* // returns <ndarray>
*
* var sh = getShape( v );
* // returns [ 1, 4 ]
*
* var dt = String( getDType( v ) );
* // returns 'float64'
*
* @example
* var getDType = require( '@stdlib/ndarray/dtype' );
* var getShape = require( '@stdlib/ndarray/shape' );
*
* var v = matrixWithDType( {}, 'float64', null );
* // returns <ndarray>
*
* var sh = getShape( v );
* // returns [ 0, 0 ]
*
* var dt = String( getDType( v ) );
* // returns 'float64'
*
* @example
* var getDType = require( '@stdlib/ndarray/dtype' );
* var getShape = require( '@stdlib/ndarray/shape' );
* var array2iterator = require( '@stdlib/array/to-iterator' );
*
* var v = matrixWithDType( array2iterator( [ [ 1, 2 ], [ 3, 4 ] ] ), 'generic', {} );
* // returns <ndarray>
*
* var sh = getShape( v );
* // returns [ 2, 2 ]
*
* var dt = String( getDType( v ) );
* // returns 'generic'
*/
function matrixWithDType( arg, dtype, options ) {
var opts;
var buf;
var ord;
var sh;
// Note: in all of the following, we delegate option validation to the ndarray constructor...
if ( options === null ) {
opts = {};
} else {
opts = options;
}
// Case: matrix( shape )
if ( isNonNegativeIntegerArray( arg ) && arg.length === 2 ) {
buf = buffer( dtype, numel( arg ) );
ord = resolveOrder( opts );
return new ndarray( dtype, buf, arg, shape2strides( arg, ord ), 0, ord, opts );
}
// Case: matrix( collection )
if ( isCollection( arg ) ) {
sh = arrayShape( arg );
if ( sh.length < 2 ) {
return null;
}
sh = slice( sh, 0, 2 );
buf = flatten2d( arg, sh, false );
if ( isBinaryDataType( dtype ) ) {
buf = array2buffer( buf ); // note: we assume that `buf` is an array of octets
} else if ( isComplexDataType( dtype ) ) {
// Sniff the first value in order to determine if we've been provided a nested array containing interleaved real and imaginary components...
if ( isNumber( buf[ 0 ] ) ) {
// As each real and imaginary component pair corresponds to a single complex number, we need to update the ndarray shape:
sh[ sh.length-1 ] /= 2;
}
buf = typedarray( buf, dtype );
} else if ( !isGenericDataType( dtype ) ) {
buf = typedarray( buf, dtype );
}
ord = resolveOrder( opts );
return new ndarray( dtype, buf, sh, shape2strides( sh, ord ), 0, ord, opts );
}
// Case: matrix( ArrayBuffer )
if ( isArrayBuffer( arg ) ) {
sh = [ 1, arg.byteLength/bytesPerElement( dtype ) ];
ord = resolveOrder( opts );
return arraybuffer2ndarray( dtype, arg, sh, shape2strides( sh, ord ), 0, ord, opts );
}
// Case: matrix( Iterable )
if ( isIterableLike( arg ) ) {
buf = arg[ ITERATOR_SYMBOL ]();
buf = iterator2array( buf );
sh = arrayShape( buf );
if ( sh.length < 2 ) {
return null;
}
sh = slice( sh, 0, 2 );
buf = flatten2d( buf, sh, false );
if ( isBinaryDataType( dtype ) ) {
buf = array2buffer( buf ); // note: we assume that `buf` is an array of octets
} else if ( isComplexDataType( dtype ) ) {
// Sniff the first value in order to determine if we've materialized a nested array containing interleaved real and imaginary components...
if ( isNumber( buf[ 0 ] ) ) {
// As each real and imaginary component pair corresponds to a single complex number, we need to update the ndarray shape:
sh[ sh.length-1 ] /= 2;
}
buf = typedarray( buf, dtype );
} else if ( !isGenericDataType( dtype ) ) {
buf = typedarray( buf, dtype );
}
ord = resolveOrder( opts );
return new ndarray( dtype, buf, sh, shape2strides( sh, ord ), 0, ord, opts );
}
// Case: matrix( options )
if ( options === null && isPlainObject( arg ) ) {
buf = buffer( dtype, 0 );
return new ndarray( dtype, buf, [ 0, 0 ], [ 0, 0 ], 0, resolveOrder( arg ), arg );
}
return null;
}
// MAIN //
/**
* Creates a matrix (i.e., a two-dimensional ndarray).
*
* ## Notes
*
* - This API is intended to match the conventions of `@stdlib/array/typed`, which has a similar signature.
*
* @param {(NonNegativeInteger|NonNegativeIntegerArray|ArrayLikeObject<Collection>|ArrayBuffer|Iterable)} [arg0] - number of rows, shape, nested array, ArrayBuffer, or iterable
* @param {NonNegativeInteger} [arg1] - number of columns or byte offset
* @param {(NonNegativeInteger|NonNegativeIntegerArray)} [arg2] - number of rows or shape
* @param {NonNegativeInteger} [arg3] - number of columns
* @param {*} [dtype='float64'] - data type
* @param {Options} [options] - function options
* @param {boolean} [options.readonly=false] - boolean indicating whether to return a read-only matrix
* @param {string} [options.mode='throw'] - specifies how to handle indices which exceed matrix dimensions
* @param {StringArray} [options.submode=[options.mode]] - specifies how to handle subscripts which exceed array dimensions on a per dimension basis
* @param {string} [options.order='row-major'] - memory layout (either row-major or column-major)
* @throws {TypeError} first argument must be either a shape, array-like object, ArrayBuffer, iterable, data type, or options object
* @throws {TypeError} must provide valid options
* @returns {ndarray} two-dimensional ndarray
*
* @example
* var getDType = require( '@stdlib/ndarray/dtype' );
* var getShape = require( '@stdlib/ndarray/shape' );
*
* var arr = matrix();
* // returns <ndarray>
*
* var sh = getShape( arr );
* // returns [ 0, 0 ]
*
* var dt = String( getDType( arr ) );
* // returns 'float64'
*
* @example
* var getDType = require( '@stdlib/ndarray/dtype' );
* var getShape = require( '@stdlib/ndarray/shape' );
*
* var arr = matrix( 2, 2 );
* // returns <ndarray>
*
* var sh = getShape( arr );
* // returns [ 2, 2 ]
*
* var dt = String( getDType( arr ) );
* // returns 'float64'
*
* @example
* var getDType = require( '@stdlib/ndarray/dtype' );
* var getShape = require( '@stdlib/ndarray/shape' );
*
* var arr = matrix( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] );
* // returns <ndarray>
*
* var sh = getShape( arr );
* // returns [ 2, 2 ]
*
* var dt = String( getDType( arr ) );
* // returns 'float64'
*
* @example
* var getDType = require( '@stdlib/ndarray/dtype' );
* var getShape = require( '@stdlib/ndarray/shape' );
*
* var arr = matrix( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], 'float32' );
* // returns <ndarray>
*
* var sh = getShape( arr );
* // returns [ 2, 2 ]
*
* var dt = String( getDType( arr ) );
* // returns 'float32'
*
* @example
* var getDType = require( '@stdlib/ndarray/dtype' );
* var getShape = require( '@stdlib/ndarray/shape' );
* var ArrayBuffer = require( '@stdlib/array/buffer' );
*
* var buf = new ArrayBuffer( 32 );
* var arr = matrix( buf );
* // returns <ndarray>
*
* var sh = getShape( arr );
* // returns [ 1, 4 ]
*
* var dt = String( getDType( arr ) );
* // returns 'float64'
*
* @example
* var getDType = require( '@stdlib/ndarray/dtype' );
* var getShape = require( '@stdlib/ndarray/shape' );
* var ArrayBuffer = require( '@stdlib/array/buffer' );
*
* var buf = new ArrayBuffer( 32 );
* var arr = matrix( buf, 16 );
* // returns <ndarray>
*
* var sh = getShape( arr );
* // returns [ 1, 2 ]
*
* var dt = String( getDType( arr ) );
* // returns 'float64'
*
* @example
* var getDType = require( '@stdlib/ndarray/dtype' );
* var getShape = require( '@stdlib/ndarray/shape' );
* var ArrayBuffer = require( '@stdlib/array/buffer' );
*
* var buf = new ArrayBuffer( 64 );
* var arr = matrix( buf, 16, 2, 1 );
* // returns <ndarray>
*
* var sh = getShape( arr );
* // returns [ 2, 1 ]
*
* var dt = String( getDType( arr ) );
* // returns 'float64'
*/
function matrix() { // eslint-disable-line max-statements
var nargs;
var arg0;
var arg1;
var arg2;
var arg3;
var arg4;
var arg5;
var buf;
var ord;
var out;
var sh;
nargs = arguments.length;
// Case: matrix()
if ( nargs === 0 ) {
buf = buffer( DEFAULT_DTYPE, 0 );
return new ndarray( DEFAULT_DTYPE, buf, [ 0, 0 ], [ 0, 0 ], 0, DEFAULT_ORDER );
}
arg0 = arguments[ 0 ];
// Case: matrix( arg0 );
if ( nargs === 1 ) {
if ( isDataType( arg0 ) ) {
return matrixWithDType( [ 0, 0 ], arg0, null );
}
out = matrixWithDType( arg0, DEFAULT_DTYPE, null );
if ( out === null ) {
throw new TypeError( format( 'invalid argument. Must provide a shape, ArrayBuffer, array-like object, iterable, data type, or options object. Value: `%s`.', arg0 ) );
}
return out;
}
arg1 = arguments[ 1 ];
// Case: matrix( arg0, arg1 )
if ( nargs === 2 ) {
// Case: matrix( dtype, options )
if ( isDataType( arg0 ) ) {
if ( arg1 === null ) {
throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', arg1 ) );
}
return matrixWithDType( [ 0, 0 ], arg0, arg1 );
}
// Case: matrix( arg0, dtype )
if ( isDataType( arg1 ) ) {
out = matrixWithDType( arg0, arg1, null );
if ( out === null ) {
throw new TypeError( format( 'invalid argument. First argument must be a shape, ArrayBuffer, array-like object, or iterable. Value: `%s`.', arg0 ) );
}
return out;
}
// Case: matrix( M, N ) || matrix( ArrayBuffer, byteOffset )
if ( isNonNegativeInteger( arg1 ) ) {
if ( isNonNegativeInteger( arg0 ) ) {
return matrixWithDType( [ arg0, arg1 ], DEFAULT_DTYPE, null );
}
if ( !isArrayBuffer( arg0 ) ) {
throw new TypeError( format( 'invalid argument. First argument must be an ArrayBuffer. Value: `%s`.', arg0 ) );
}
sh = [ 1, (arg0.byteLength-arg1)/bytesPerElement( DEFAULT_DTYPE ) ];
return arraybuffer2ndarray( DEFAULT_DTYPE, arg0, sh, shape2strides( sh, DEFAULT_ORDER ), arg1, DEFAULT_ORDER );
}
// Case: matrix( arg0, options )
if ( arg1 === null ) {
throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', arg1 ) );
}
out = matrixWithDType( arg0, DEFAULT_DTYPE, arg1 );
if ( out === null ) {
throw new TypeError( format( 'invalid argument. First argument must be a shape, ArrayBuffer, array-like object, or iterable. Value: `%s`.', arg0 ) );
}
return out;
}
arg2 = arguments[ 2 ];
// Case: matrix( arg0, arg1, arg2 )
if ( nargs === 3 ) {
// Case: matrix( M, N, arg2 )
if ( isNonNegativeInteger( arg0 ) ) {
if ( !isNonNegativeInteger( arg1 ) ) {
throw new TypeError( format( 'invalid argument. Second argument must be a nonnegative integer. Value: `%s`.', arg1 ) );
}
// Case: matrix( M, N, dtype )
if ( isDataType( arg2 ) ) {
return matrixWithDType( [ arg0, arg1 ], arg2, null );
}
// Case: matrix( M, N, options )
if ( arg2 === null ) {
throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', arg2 ) );
}
return matrixWithDType( [ arg0, arg1 ], DEFAULT_DTYPE, arg2 );
}
// Case: matrix( arg0, dtype, options )
if ( isDataType( arg1 ) ) {
if ( arg2 === null ) {
throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', arg2 ) );
}
out = matrixWithDType( arg0, arg1, arg2 );
if ( out === null ) {
throw new TypeError( format( 'invalid argument. First argument must be a shape, ArrayBuffer, array-like object, or iterable. Value: `%s`.', arg0 ) );
}
return out;
}
// Case: matrix( ArrayBuffer, byteOffset, arg2 )
if ( !isArrayBuffer( arg0 ) ) {
throw new TypeError( format( 'invalid argument. First argument must be an ArrayBuffer. Value: `%s`.', arg0 ) );
}
if ( !isNonNegativeInteger( arg1 ) ) {
throw new TypeError( format( 'invalid argument. Byte offset must be a nonnegative integer. Value: `%s`.', arg1 ) );
}
// Case: matrix( ArrayBuffer, byteOffset, dtype )
if ( isDataType( arg2 ) ) {
sh = [ 1, (arg0.byteLength-arg1)/bytesPerElement( arg2 ) ];
return arraybuffer2ndarray( arg2, arg0, sh, shape2strides( sh, DEFAULT_ORDER ), arg1, DEFAULT_ORDER );
}
// Case: matrix( ArrayBuffer, byteOffset, shape )
if ( isNonNegativeIntegerArray( arg2 ) && arg2.length === 2 ) {
return arraybuffer2ndarray( DEFAULT_DTYPE, arg0, arg2, shape2strides( arg2, DEFAULT_ORDER ), arg1, DEFAULT_ORDER );
}
// Case: matrix( ArrayBuffer, byteOffset, options )
if ( arg2 === null ) {
throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', arg2 ) );
}
sh = [ 1, (arg0.byteLength-arg1)/bytesPerElement( DEFAULT_DTYPE ) ];
ord = resolveOrder( arg2 );
return arraybuffer2ndarray( DEFAULT_DTYPE, arg0, sh, shape2strides( sh, ord ), arg1, ord, arg2 );
}
arg3 = arguments[ 3 ];
// Case: matrix( arg0, arg1, arg2, arg3 )
if ( nargs === 4 ) {
// Case: matrix( M, N, dtype, options )
if ( isNonNegativeInteger( arg0 ) ) {
if ( !isNonNegativeInteger( arg1 ) ) {
throw new TypeError( format( 'invalid argument. Second argument must be a nonnegative integer. Value: `%s`.', arg1 ) );
}
if ( !isDataType( arg2 ) ) {
throw new TypeError( format( 'invalid argument. Third argument must be a recognized/supported data type. Value: `%s`.', arg2 ) );
}
if ( !isPlainObject( arg3 ) ) {
throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', arg3 ) );
}
return matrixWithDType( [ arg0, arg1 ], arg2, arg3 );
}
// Case: matrix( ArrayBuffer, byteOffset, arg2, arg3 )
if ( !isArrayBuffer( arg0 ) ) {
throw new TypeError( format( 'invalid argument. First argument must be an ArrayBuffer. Value: `%s`.', arg0 ) );
}
if ( !isNonNegativeInteger( arg1 ) ) {
throw new TypeError( format( 'invalid argument. Byte offset must be a nonnegative integer. Value: `%s`.', arg1 ) );
}
// Case: matrix( ArrayBuffer, byteOffset, M, N )
if ( isNonNegativeInteger( arg2 ) ) {
if ( !isNonNegativeInteger( arg3 ) ) {
throw new TypeError( format( 'invalid argument. Fourth argument must be a nonnegative integer. Value: `%s`.', arg3 ) );
}
sh = [ arg2, arg3 ];
return arraybuffer2ndarray( DEFAULT_DTYPE, arg0, sh, shape2strides( sh, DEFAULT_ORDER ), arg1, DEFAULT_ORDER );
}
// Case: matrix( ArrayBuffer, byteOffset, shape, dtype )
if ( isNonNegativeIntegerArray( arg2 ) && arg2.length === 2 ) {
if ( isDataType( arg3 ) ) {
return arraybuffer2ndarray( arg3, arg0, arg2, shape2strides( arg2, DEFAULT_ORDER ), arg1, DEFAULT_ORDER );
}
// Case: matrix( ArrayBuffer, byteOffset, shape, options )
if ( !isPlainObject( arg3 ) ) {
throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', arg3 ) );
}
ord = resolveOrder( arg3 );
return arraybuffer2ndarray( DEFAULT_DTYPE, arg0, arg2, shape2strides( arg2, ord ), arg1, ord, arg3 );
}
// Case: matrix( ArrayBuffer, byteOffset, dtype, options )
if ( !isDataType( arg2 ) ) {
throw new TypeError( format( 'invalid argument. Third argument must be a recognized/supported data type. Value: `%s`.', arg2 ) );
}
if ( !isPlainObject( arg3 ) ) {
throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', arg3 ) );
}
sh = [ 1, (arg0.byteLength-arg1)/bytesPerElement( arg2 ) ];
ord = resolveOrder( arg3 );
return arraybuffer2ndarray( arg2, arg0, sh, shape2strides( sh, ord ), arg1, ord, arg3 );
}
arg4 = arguments[ 4 ];
// Case: matrix( ArrayBuffer, byteOffset, arg2, arg3, arg4 )
if ( nargs === 5 ) {
if ( !isArrayBuffer( arg0 ) ) {
throw new TypeError( format( 'invalid argument. First argument must be an ArrayBuffer. Value: `%s`.', arg0 ) );
}
if ( !isNonNegativeInteger( arg1 ) ) {
throw new TypeError( format( 'invalid argument. Second argument must be a nonnegative integer. Value: `%s`.', arg1 ) );
}
// Case: matrix( ArrayBuffer, byteOffset, M, N, arg4 )
if ( isNonNegativeInteger( arg2 ) ) {
if ( !isNonNegativeInteger( arg3 ) ) {
throw new TypeError( format( 'invalid argument. Fourth argument must be a nonnegative integer. Value: `%s`.', arg3 ) );
}
sh = [ arg2, arg3 ];
// Case: matrix( ArrayBuffer, byteOffset, M, N, dtype )
if ( isDataType( arg4 ) ) {
return arraybuffer2ndarray( arg4, arg0, sh, shape2strides( sh, DEFAULT_ORDER ), arg1, DEFAULT_ORDER );
}
// Case: matrix( ArrayBuffer, byteOffset, M, N, options )
if ( !isPlainObject( arg4 ) ) {
throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', arg4 ) );
}
ord = resolveOrder( arg4 );
return arraybuffer2ndarray( DEFAULT_DTYPE, arg0, sh, shape2strides( sh, ord ), arg1, ord, arg4 );
}
// Case: matrix( ArrayBuffer, byteOffset, shape, dtype, options )
if ( !isNonNegativeIntegerArray( arg2 ) || arg2.length !== 2 ) {
throw new TypeError( format( 'invalid argument. Third argument must be a valid shape. Value: `%s`.', arg2 ) );
}
if ( !isDataType( arg3 ) ) {
throw new TypeError( format( 'invalid argument. Fourth argument must be a recognized/supported data type. Value: `%s`.', arg3 ) );
}
if ( !isPlainObject( arg4 ) ) {
throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', arg4 ) );
}
ord = resolveOrder( arg4 );
return arraybuffer2ndarray( arg3, arg0, arg2, shape2strides( arg2, ord ), arg1, ord, arg4 );
}
arg5 = arguments[ 5 ];
// Case: matrix( ArrayBuffer, byteOffset, M, N, dtype, options )
if ( !isArrayBuffer( arg0 ) ) {
throw new TypeError( format( 'invalid argument. First argument must be an ArrayBuffer. Value: `%s`.', arg0 ) );
}
if ( !isNonNegativeInteger( arg1 ) ) {
throw new TypeError( format( 'invalid argument. Second argument must be a nonnegative integer. Value: `%s`.', arg1 ) );
}
if ( !isNonNegativeInteger( arg2 ) ) {
throw new TypeError( format( 'invalid argument. Third argument must be a nonnegative integer. Value: `%s`.', arg2 ) );
}
if ( !isNonNegativeInteger( arg3 ) ) {
throw new TypeError( format( 'invalid argument. Fourth argument must be a nonnegative integer. Value: `%s`.', arg3 ) );
}
if ( !isDataType( arg4 ) ) {
throw new TypeError( format( 'invalid argument. Fifth argument must be a recognized/supported data type. Value: `%s`.', arg4 ) );
}
if ( !isPlainObject( arg5 ) ) {
throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', arg5 ) );
}
sh = [ arg2, arg3 ];
ord = resolveOrder( arg5 );
return arraybuffer2ndarray( arg4, arg0, sh, shape2strides( sh, ord ), arg1, ord, arg5 );
}
// EXPORTS //
module.exports = matrix;
|