Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
ndarray-ops
Advanced tools
The ndarray-ops npm package provides a collection of high-performance operations for manipulating ndarrays (N-dimensional arrays). It is designed to work efficiently with large datasets and supports a variety of mathematical and logical operations.
Element-wise Operations
This feature allows you to perform element-wise operations such as addition, subtraction, multiplication, and division on ndarrays. The code sample demonstrates how to add two 2x2 matrices element-wise.
const ndarray = require('ndarray');
const ops = require('ndarray-ops');
let a = ndarray(new Float32Array([1, 2, 3, 4]), [2, 2]);
let b = ndarray(new Float32Array([5, 6, 7, 8]), [2, 2]);
let c = ndarray(new Float32Array(4), [2, 2]);
ops.add(c, a, b);
console.log(c.data); // Output: Float32Array [ 6, 8, 10, 12 ]
Scalar Operations
This feature allows you to perform operations involving a scalar value and an ndarray. The code sample demonstrates how to multiply each element of a 2x2 matrix by a scalar value (2).
const ndarray = require('ndarray');
const ops = require('ndarray-ops');
let a = ndarray(new Float32Array([1, 2, 3, 4]), [2, 2]);
ops.mulseq(a, 2);
console.log(a.data); // Output: Float32Array [ 2, 4, 6, 8 ]
Logical Operations
This feature allows you to perform logical operations such as less than, greater than, and equal to on ndarrays. The code sample demonstrates how to compare two 2x2 matrices element-wise to check if elements in the first matrix are less than those in the second matrix.
const ndarray = require('ndarray');
const ops = require('ndarray-ops');
let a = ndarray(new Float32Array([1, 2, 3, 4]), [2, 2]);
let b = ndarray(new Float32Array([4, 3, 2, 1]), [2, 2]);
let c = ndarray(new Float32Array(4), [2, 2]);
ops.lt(c, a, b);
console.log(c.data); // Output: Float32Array [ 1, 1, 0, 0 ]
Math.js is an extensive math library for JavaScript and Node.js. It provides a wide range of mathematical functions and supports multi-dimensional arrays. Compared to ndarray-ops, math.js offers a broader set of functionalities but may not be as optimized for performance in specific ndarray operations.
Numeric is a library for numerical computations in JavaScript. It provides support for matrix and vector operations, similar to ndarray-ops. However, ndarray-ops is more specialized for high-performance operations on ndarrays, while Numeric offers a more general approach to numerical computations.
Ndarray is a module for creating and manipulating N-dimensional arrays in JavaScript. While it provides the basic structure for ndarrays, it does not include the extensive set of operations that ndarray-ops offers. Ndarray-ops can be seen as a complementary package to ndarray, providing the necessary operations to manipulate the arrays efficiently.
A collection of common mathematical operations for ndarrays. Implemented using cwise
First, install the library using npm:
npm install ndarray-ops
Then you can import the library by doing:
var ops = require("ndarray-ops")
Then you can use the functions as in the following example:
//First, import libraries
var ndarray = require("ndarray")
, ops = require("ndarray-ops")
//Next, create some arrays
var a = ndarray(new Float32Array(128*128))
, b = ndarray(new Float32Array(128*128))
, c = ndarray(new Float32Array(128*128))
//Initialize b with some random numbers:
ops.random(b)
//Set c to a constant 1
ops.assigns(c, 1.0)
//Add b and c, store result in a:
ops.add(a, b, c)
//Multiply a by 0.5 in place
ops.mulseq(a, 0.5)
//Print some statistics about a:
console.log(
"inf(a) = ", ops.inf(a),
"sup(a) = ", ops.sup(a),
"argmin(a) = ", ops.argmin(a),
"argmax(a) = ", ops.argmax(a),
"norm1(a) = ", ops.norm1(a))
This library implements component-wise operations for all of the operators and Math.* functions in JS, along with a few commonly used aggregate operations. Most of the functions in the library work by applying some symmetric binary operator to a pair of arrays. You call them like this:
ops.add(dest, arg1, arg2)
Which translates into code that works (approximately) like this:
for(var i=0; i<dest.shape[0]; ++i) {
dest[i] = arg1[i] + arg2[i]
}
It is up to you to specify where the result gets store. This library does not create new arrays for you to avoid performing expensive intermediate allocations. There are also a few other variations:
ops.addeq(dest, arg1)
Operators with the -eq suffix perform an assignment.
for(var i=0; i<dest.shape[0]; ++i) {
dest[i] += arg1[i]
}
ops.adds(dest, arg1, 1.0)
The -s suffix denotes scalar/broadcast operations; so the above would translate to:
for(var i=0; i<dest.shape[0]; ++i) {
dest[i] = arg1[i] + 1.0
}
ops.addseq(dest, 1.0)
The -seq suffix is basically the combination of the above, and translates to:
for(var i=0; i<dest.shape[0]; ++i) {
dest[i] += 1.0
}
The following operators follow this rule:
+
-
*
/
%
&
&
^
<<
>>
>>>
<
>
<=
>=
===
!==
&&
||
Math.max
Math.min
There are a few corner cases that follow slightly different rules. These can be grouped using the following general categories:
There are two assignment operators:
op.assign(dest, src)
copies one array into another, while op.assigns(dest, val)
broadcasts a scalar to all elements of an array.
Nullary operators only take on argument for the array they are assigning to, and don't have any variations. Currently there is only one of these:
Math.random()
Unary operators have one of two forms, they can be written as either:
op.abs(dest, arg)
Or:
op.abseq(dest)
The former version sets dest = |arg|, while in the latter the operation is applied in place. ndarray-ops exposes the following unary operators:
!
~
-
1.0/
Math.abs
Math.acos
Math.asin
Math.atan
Math.ceil
Math.cos
Math.exp
Math.floor
Math.log
Math.round
Math.sin
Math.sqrt
Math.tan
There are also a few non-symmetric binary operators. These operators have an extra suffix op
which flips the order of the arguments. There are only two of these:
Finally, there are aggregate operators that take an array as input and compute some aggregate result or summary. These functions don't have any special suffixes and all of them take a single array as input.
(c) 2013 Mikola Lysenko. MIT License
FAQs
Common operations for ndarray arrays
The npm package ndarray-ops receives a total of 136,442 weekly downloads. As such, ndarray-ops popularity was classified as popular.
We found that ndarray-ops demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.