@@ -529,44 +529,55 @@ export const createDenseMatrixClass = /* #__PURE__ */ factory(name, dependencies
529529 * Applies a callback function to a reference to each element of the matrix
530530 * @memberof DenseMatrix
531531 * @param {Function } callback The callback function is invoked with three
532- * parameters: an array, an integer index to that
533- * array, and the Matrix being traversed.
532+ * parameters: the array containing the element,
533+ * the index of the element within that array (as an integer),
534+ * and for non unarry callbacks copy of the current index (as an array of integers).
534535 */
535536 DenseMatrix . prototype . _forEach = function ( callback ) {
536- const me = this
537- const s = me . size ( )
538- const maxDepth = s . length - 1
537+ const isUnary = callback . length === 2 // callback has 2 parameters: value, index
538+ const maxDepth = this . _size . length - 1
539539
540- if ( maxDepth < 0 ) {
540+ if ( maxDepth < 0 ) return
541+
542+ if ( isUnary ) {
543+ iterateUnary ( this . _data )
541544 return
542545 }
543546
544547 if ( maxDepth === 0 ) {
545- const thisSize = s [ 0 ]
546- for ( let i = 0 ; i < thisSize ; i ++ ) {
547- callback ( me . _data , i , [ i ] )
548+ for ( let i = 0 ; i < this . _data . length ; i ++ ) {
549+ callback ( this . _data , i , [ i ] )
548550 }
549551 return
550552 }
551553
552- const index = Array ( s . length )
554+ const index = new Array ( maxDepth + 1 )
553555
554- function recurse ( data , depth ) {
555- const thisSize = s [ depth ]
556+ iterate ( this . _data )
557+ function iterate ( data , depth = 0 ) {
556558 if ( depth < maxDepth ) {
557- for ( let i = 0 ; i < thisSize ; i ++ ) {
559+ for ( let i = 0 ; i < data . length ; i ++ ) {
558560 index [ depth ] = i
559- recurse ( data [ i ] , depth + 1 )
561+ iterate ( data [ i ] , depth + 1 )
560562 }
561563 } else {
562- for ( let i = 0 ; i < thisSize ; i ++ ) {
564+ for ( let i = 0 ; i < data . length ; i ++ ) {
563565 index [ depth ] = i
564566 callback ( data , i , index . slice ( ) )
565567 }
566568 }
567569 }
568-
569- recurse ( me . _data , 0 )
570+ function iterateUnary ( data , depth = 0 ) {
571+ if ( depth < maxDepth ) {
572+ for ( let i = 0 ; i < data . length ; i ++ ) {
573+ iterateUnary ( data [ i ] , depth + 1 )
574+ }
575+ } else {
576+ for ( let i = 0 ; i < data . length ; i ++ ) {
577+ callback ( data , i )
578+ }
579+ }
580+ }
570581 }
571582
572583 /**
@@ -576,17 +587,21 @@ export const createDenseMatrixClass = /* #__PURE__ */ factory(name, dependencies
576587 * @param {Function } callback The callback function is invoked with three
577588 * parameters: the value of the element, the index
578589 * of the element, and the Matrix being traversed.
590+ * @param {boolean } skipZeros If true, the callback function is invoked only for non-zero entries
591+ * @param {boolean } isUnary If true, the callback function is invoked with one parameter
579592 *
580593 * @return {DenseMatrix } matrix
581594 */
582- DenseMatrix . prototype . map = function ( callback ) {
595+ DenseMatrix . prototype . map = function ( callback , skipZeros = false , isUnary = false ) {
583596 const me = this
584597 const result = new DenseMatrix ( me )
585- const fastCallback = optimizeCallback ( callback , me . _data , 'map' )
598+ const fastCallback = optimizeCallback ( callback , me . _data , 'map' , isUnary )
586599
587- result . _forEach ( function ( arr , i , index ) {
588- arr [ i ] = fastCallback ( arr [ i ] , index , me )
589- } )
600+ const applyCallback = isUnary || fastCallback . isUnary
601+ ? ( arr , i ) => { arr [ i ] = fastCallback . fn ( arr [ i ] ) }
602+ : ( arr , i , index ) => { arr [ i ] = fastCallback . fn ( arr [ i ] , index , me ) }
603+
604+ result . _forEach ( applyCallback )
590605
591606 return result
592607 }
@@ -597,13 +612,18 @@ export const createDenseMatrixClass = /* #__PURE__ */ factory(name, dependencies
597612 * @param {Function } callback The callback function is invoked with three
598613 * parameters: the value of the element, the index
599614 * of the element, and the Matrix being traversed.
615+ * @param {boolean } skipZeros If true, the callback function is invoked only for non-zero entries
616+ * @param {boolean } isUnary If true, the callback function is invoked with one parameter
600617 */
601- DenseMatrix . prototype . forEach = function ( callback ) {
618+ DenseMatrix . prototype . forEach = function ( callback , skipZeros = false , isUnary = false ) {
602619 const me = this
603- const fastCallback = optimizeCallback ( callback , me . _data , 'map' )
604- me . _forEach ( function ( arr , i , index ) {
605- fastCallback ( arr [ i ] , index , me )
606- } )
620+ const fastCallback = optimizeCallback ( callback , me . _data , 'map' , isUnary )
621+
622+ const applyCallback = isUnary || fastCallback . isUnary
623+ ? ( arr , i ) => { fastCallback . fn ( arr [ i ] ) }
624+ : ( arr , i , index ) => { fastCallback . fn ( arr [ i ] , index , me ) }
625+
626+ me . _forEach ( applyCallback )
607627 }
608628
609629 /**
0 commit comments