Skip to content

docs: improve README examples of ndarray/base namespace #1742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions lib/node_modules/@stdlib/ndarray/base/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

@license Apache-2.0

Copyright (c) 2018 The Stdlib Authors.
Copyright (c) 2024 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.
Expand Down Expand Up @@ -163,10 +163,46 @@ The namespace contains the following sub-namespaces:
<!-- eslint no-undef: "error" -->

```javascript
var objectKeys = require( '@stdlib/utils/keys' );
var Float64Array = require( '@stdlib/array/float64' );
var linspace = require( '@stdlib/array/linspace' );
var ddot = require( '@stdlib/blas/base/ddot' );
var sin = require( '@stdlib/math/base/special/sin' );
var cos = require( '@stdlib/math/base/special/cos' );
var PI = require( '@stdlib/constants/float64/pi' );
var ns = require( '@stdlib/ndarray/base' );

console.log( objectKeys( ns ) );
var resultData;
var yStrides;
var xStrides;
var xShape;
var yData;
var xData;
var y;
var x;
var i;

xData = linspace( 0, 2 * PI, 100 );
yData = linspace( 0, PI, 100 );
x = ns.ndarray( 'float64', xData, [ 100 ], [ 1 ], 0, 'row-major' );
y = ns.ndarray( 'float64', yData, [ 100 ], [ 1 ], 0, 'row-major' );

// Apply sin function to each element of x and cos function to each element of y
for ( i = 0; i < x.shape[ 0 ]; i++ ) {
x.set( i, sin( x.get( i ) ) );
y.set( i, cos( y.get( i ) ) );
}

resultData = new Float64Array( 1 );

xShape = x.shape[ 0 ];
xStrides = x.strides[ 0 ];
yStrides = y.strides[ 0 ];

// Calculate dot product of x and y
resultData[ 0 ] = ddot( xShape, x.data, xStrides, y.data, yStrides );

// Logs 42.00632598024892
console.log( resultData[ 0 ] );
```

</section>
Expand Down
42 changes: 39 additions & 3 deletions lib/node_modules/@stdlib/ndarray/base/examples/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
* Copyright (c) 2024 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.
Expand All @@ -18,7 +18,43 @@

'use strict';

var objectKeys = require( '@stdlib/utils/keys' );
var Float64Array = require( '@stdlib/array/float64' );
var linspace = require( '@stdlib/array/linspace' );
var ddot = require( '@stdlib/blas/base/ddot' );
var sin = require( '@stdlib/math/base/special/sin' );
var cos = require( '@stdlib/math/base/special/cos' );
var PI = require( '@stdlib/constants/float64/pi' );
var ns = require( './../lib' );

console.log( objectKeys( ns ) );
var resultData;
var yStrides;
var xStrides;
var xShape;
var yData;
var xData;
var y;
var x;
var i;

xData = linspace( 0, 2 * PI, 100 );
yData = linspace( 0, PI, 100 );
x = ns.ndarray( 'float64', xData, [ 100 ], [ 1 ], 0, 'row-major' );
y = ns.ndarray( 'float64', yData, [ 100 ], [ 1 ], 0, 'row-major' );

// Apply sin function to each element of x and cos function to each element of y
for ( i = 0; i < x.shape[ 0 ]; i++ ) {
x.set( i, sin( x.get( i ) ) );
y.set( i, cos( y.get( i ) ) );
}

resultData = new Float64Array( 1 );

xShape = x.shape[ 0 ];
xStrides = x.strides[ 0 ];
yStrides = y.strides[ 0 ];

// Calculate dot product of x and y
resultData[ 0 ] = ddot( xShape, x.data, xStrides, y.data, yStrides );

// Logs 42.00632598024892
console.log( resultData[ 0 ] );