Skip to content

Commit

Permalink
refactor: forbenius norm implementation and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Pranavchiku committed Aug 1, 2024
1 parent 94d1264 commit b0742ba
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 43 deletions.
30 changes: 18 additions & 12 deletions lib/node_modules/@stdlib/lapack/base/dlange/lib/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
var isRowMajor = require( '@stdlib/ndarray/base/assert/is-row-major' );
var dlassq = require( '@stdlib/lapack/base/dlassq' ).ndarray;
var lowercase = require( '@stdlib/string/base/lowercase' );
var dnrm2 = require( '@stdlib/blas/base/dnrm2' ).ndarray;
var sqrt = require( '@stdlib/math/base/special/sqrt' );
var Float64Array = require( '@stdlib/array/float64' );
var abs = require( '@stdlib/math/base/special/abs' );
var min = require( '@stdlib/math/base/special/min' );
var isnan = require( '@stdlib/assert/is-nan' );
Expand Down Expand Up @@ -192,27 +192,34 @@ function infNorm( M, N, A, strideA1, strideA2, offsetA, work, strideW, offsetW )
* @param {integer} strideA1 - stride of the first dimension of `A`
* @param {integer} strideA2 - stride of the second dimension of `A`
* @param {PositiveInteger} offsetA - starting index for `A`
* @param {Float64Array} work - workspace array
* @param {integer} strideW - stride length for `work`
* @param {PositiveInteger} offsetW - starting index for `work`
* @returns {number} Frobenius norm
*
* @example
* var Float64Array = require( '@stdlib/array/float64' );
*
* var A = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] );
* var work = new Float64Array( 2 );
*
* var out = frobeniusNorm( 2, 2, A, 2, 1, 0 );
* var out = frobeniusNorm( 2, 2, A, 2, 1, 0, work, 1, 0 );
* // returns 5.477225575051661
*/
function frobeniusNorm( M, N, A, strideA1, strideA2, offsetA ) {
function frobeniusNorm( M, N, A, strideA1, strideA2, offsetA, work, strideW, offsetW ) {
var value;
var out;
var j;

// Find normF( A )
out = new Float64Array( [ 0.0, 1.0 ] );
if ( M === 1 ) {
return dnrm2( N, A, strideA2, offsetA, strideA1 );
}
work[ offsetW ] = 0.0;
work[ offsetW + strideW ] = 1.0;
for ( j = 0; j < N; j++ ) {
out = dlassq( M, A, strideA1, offsetA + ( j * strideA2 ), out[ 0 ], out[ 1 ], out, 1, 0 );
work = dlassq( M, A, strideA1, offsetA + ( j * strideA2 ), work[ offsetW ], work[ offsetW + strideW ], work, strideW, offsetW );
}
value = out[ 0 ] * sqrt( out[ 1 ] );
value = work[ offsetW ] * sqrt( work[ offsetW + strideW ] );
return value;
}

Expand Down Expand Up @@ -246,7 +253,6 @@ function frobeniusNorm( M, N, A, strideA1, strideA2, offsetA ) {
*/
function dlange( norm, M, N, A, strideA1, strideA2, offsetA, work, strideW, offsetW ) {
var order;
var value;

norm = lowercase( norm );
if ( isRowMajor( [ strideA1, strideA2 ] ) ) {
Expand All @@ -256,8 +262,9 @@ function dlange( norm, M, N, A, strideA1, strideA2, offsetA, work, strideW, offs
}

if ( min( M, N ) === 0.0 ) {
value = 0.0;
} else if ( norm === 'm' ) {
return 0.0;
}
if ( norm === 'm' ) {
return maxAbsNorm( order, M, N, A, strideA1, strideA2, offsetA );
}
if ( norm === 'o' || norm === '1' ) {
Expand All @@ -267,9 +274,8 @@ function dlange( norm, M, N, A, strideA1, strideA2, offsetA, work, strideW, offs
return infNorm( M, N, A, strideA1, strideA2, offsetA, work, strideW, offsetW );
}
if ( norm === 'f' ) {
return frobeniusNorm( M, N, A, strideA1, strideA2, offsetA );
return frobeniusNorm( M, N, A, strideA1, strideA2, offsetA, work, strideW, offsetW );
}
return value;
}


Expand Down
34 changes: 3 additions & 31 deletions lib/node_modules/@stdlib/lapack/base/dlange/test/test.dlange.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ tape( 'the function returns maximum absolute value of elements of a square real

out = dlange( 'row-major', 'M', 2, 2, A, 2, work );
expected = 4.0;

t.strictEqual( out, expected, 'returns expected value' );
t.end();
});
Expand All @@ -93,7 +92,6 @@ tape( 'the function returns maximum absolute value of elements of a real matrix

out = dlange( 'row-major', 'M', 2, 3, A, 3, work );
expected = 6.0;

t.strictEqual( out, expected, 'returns expected value' );
t.end();
});
Expand All @@ -109,7 +107,6 @@ tape( 'the function returns maximum absolute value of elements of a square real

out = dlange( 'column-major', 'M', 2, 2, A, 2, work );
expected = 4.0;

t.strictEqual( out, expected, 'returns expected value' );
t.end();
});
Expand All @@ -125,7 +122,6 @@ tape( 'the function returns maximum absolute value of elements of a real matrix

out = dlange( 'column-major', 'M', 3, 2, A, 2, work );
expected = 6.0;

t.strictEqual( out, expected, 'returns expected value' );
t.end();
});
Expand All @@ -141,15 +137,12 @@ tape( 'the function returns the one norm of a square real matrix ( row-major )',

out = dlange( 'row-major', '1', 2, 2, A, 2, work );
expected = 6.0;

t.strictEqual( out, expected, 'returns expected value' );

out = dlange( 'row-major', 'O', 2, 2, A, 2, work );

t.strictEqual( out, expected, 'returns expected value' );

out = dlange( 'row-major', 'o', 2, 2, A, 2, work );

t.strictEqual( out, expected, 'returns expected value' );
t.end();
});
Expand All @@ -165,15 +158,12 @@ tape( 'the function returns the one norm of a real matrix ( row-major )', functi

out = dlange( 'row-major', '1', 2, 3, A, 3, work );
expected = 9.0;

t.strictEqual( out, expected, 'returns expected value' );

out = dlange( 'row-major', 'O', 2, 3, A, 3, work );

t.strictEqual( out, expected, 'returns expected value' );

out = dlange( 'row-major', 'o', 2, 3, A, 3, work );

t.strictEqual( out, expected, 'returns expected value' );
t.end();
});
Expand All @@ -189,15 +179,12 @@ tape( 'the function returns the one norm of a square real matrix ( column-major

out = dlange( 'column-major', '1', 2, 2, A, 2, work );
expected = 6.0;

t.strictEqual( out, expected, 'returns expected value' );

out = dlange( 'column-major', 'O', 2, 2, A, 2, work );

t.strictEqual( out, expected, 'returns expected value' );

out = dlange( 'column-major', 'o', 2, 2, A, 2, work );

t.strictEqual( out, expected, 'returns expected value' );
t.end();
});
Expand All @@ -213,15 +200,12 @@ tape( 'the function returns the one norm of a real matrix ( column-major )', fun

out = dlange( 'column-major', '1', 3, 2, A, 2, work );
expected = 10.0;

t.strictEqual( out, expected, 'returns expected value' );

out = dlange( 'column-major', 'O', 3, 2, A, 2, work );

t.strictEqual( out, expected, 'returns expected value' );

out = dlange( 'column-major', 'o', 3, 2, A, 2, work );

t.strictEqual( out, expected, 'returns expected value' );
t.end();
});
Expand All @@ -237,11 +221,9 @@ tape( 'the function returns the infinity norm of a square real matrix ( row-majo

out = dlange( 'row-major', 'I', 2, 2, A, 2, work );
expected = 7.0;

t.strictEqual( out, expected, 'returns expected value' );

out = dlange( 'row-major', 'i', 2, 2, A, 2, work );

t.strictEqual( out, expected, 'returns expected value' );
t.end();
});
Expand All @@ -257,11 +239,9 @@ tape( 'the function returns the infinity norm of a real matrix ( row-major )', f

out = dlange( 'row-major', 'I', 2, 3, A, 3, work );
expected = 15.0;

t.strictEqual( out, expected, 'returns expected value' );

out = dlange( 'row-major', 'i', 2, 3, A, 3, work );

t.strictEqual( out, expected, 'returns expected value' );
t.end();
});
Expand All @@ -277,11 +257,9 @@ tape( 'the function returns the infinity norm of a square real matrix ( column-m

out = dlange( 'column-major', 'I', 2, 2, A, 2, work );
expected = 7.0;

t.strictEqual( out, expected, 'returns expected value' );

out = dlange( 'column-major', 'i', 2, 2, A, 2, work );

t.strictEqual( out, expected, 'returns expected value' );
t.end();
});
Expand All @@ -297,11 +275,9 @@ tape( 'the function returns the infinity norm of a real matrix ( column-major )'

out = dlange( 'column-major', 'I', 3, 2, A, 2, work );
expected = 9.0;

t.strictEqual( out, expected, 'returns expected value' );

out = dlange( 'column-major', 'i', 3, 2, A, 2, work );

t.strictEqual( out, expected, 'returns expected value' );
t.end();
});
Expand All @@ -317,11 +293,13 @@ tape( 'the function returns the Frobenius norm of a square real matrix ( row-maj

out = dlange( 'row-major', 'F', 2, 2, A, 2, work );
expected = 5.477225575051661;

t.strictEqual( out, expected, 'returns expected value' );

out = dlange( 'row-major', 'f', 2, 2, A, 2, work );
t.strictEqual( out, expected, 'returns expected value' );

work = new Float64Array( 1 );
out = dlange( 'row-major', 'F', 1, 4, A, 4, work );
t.strictEqual( out, expected, 'returns expected value' );
t.end();
});
Expand All @@ -337,11 +315,9 @@ tape( 'the function returns the Frobenius norm of a real matrix ( row-major )',

out = dlange( 'row-major', 'F', 2, 3, A, 3, work );
expected = 9.539392014169456;

t.strictEqual( out, expected, 'returns expected value' );

out = dlange( 'row-major', 'f', 2, 3, A, 3, work );

t.strictEqual( out, expected, 'returns expected value' );
t.end();
});
Expand All @@ -357,11 +333,9 @@ tape( 'the function returns the Frobenius norm of a square real matrix ( column-

out = dlange( 'column-major', 'F', 2, 2, A, 2, work );
expected = 5.477225575051661;

t.strictEqual( out, expected, 'returns expected value' );

out = dlange( 'column-major', 'f', 2, 2, A, 2, work );

t.strictEqual( out, expected, 'returns expected value' );
t.end();
});
Expand All @@ -377,11 +351,9 @@ tape( 'the function returns the Frobenius norm of a real matrix ( column-major )

out = dlange( 'column-major', 'F', 3, 2, A, 2, work );
expected = 7.681145747868608;

t.strictEqual( out, expected, 'returns expected value' );

out = dlange( 'column-major', 'f', 3, 2, A, 2, work );

t.strictEqual( out, expected, 'returns expected value' );
t.end();
});

0 comments on commit b0742ba

Please sign in to comment.