What is cwise-compiler?
The cwise-compiler npm package is a tool for compiling high-performance, multidimensional array operations in JavaScript. It allows users to define custom operations on arrays that are optimized for performance, making it suitable for scientific computing, image processing, and other tasks that require efficient manipulation of large datasets.
What are cwise-compiler's main functionalities?
Element-wise Operations
This feature allows you to perform element-wise operations on arrays. In the example, the `add` function adds a scalar value to each element of the input array `b` and stores the result in array `a`.
const cwise = require('cwise-compiler');
const add = cwise({
args: ['array', 'array', 'scalar'],
body: function(a, b, c) {
a = b + c;
}
});
const a = [1, 2, 3];
const b = [4, 5, 6];
add(a, b, 10);
console.log(a); // Output: [14, 15, 16]
Custom Reduction Operations
This feature allows you to define custom reduction operations. In the example, the `sum` function calculates the sum of all elements in the input array `a`.
const cwise = require('cwise-compiler');
const sum = cwise({
args: ['array'],
pre: function() {
this.sum = 0;
},
body: function(a) {
this.sum += a;
},
post: function() {
return this.sum;
}
});
const a = [1, 2, 3, 4];
const result = sum(a);
console.log(result); // Output: 10
Conditional Operations
This feature allows you to perform conditional operations on arrays. In the example, the `threshold` function sets elements of the input array `a` to 0 if they are less than the scalar threshold value `t`.
const cwise = require('cwise-compiler');
const threshold = cwise({
args: ['array', 'scalar'],
body: function(a, t) {
if (a < t) {
a = 0;
}
}
});
const a = [1, 5, 3, 7];
threshold(a, 4);
console.log(a); // Output: [0, 5, 0, 7]
Other packages similar to cwise-compiler
ndarray
The ndarray package provides a multidimensional array implementation for JavaScript. It offers a wide range of utilities for manipulating arrays, including slicing, reshaping, and broadcasting. While cwise-compiler focuses on compiling custom operations for performance, ndarray provides a more general-purpose array manipulation toolkit.
numjs
Numjs is a JavaScript library inspired by NumPy, providing similar functionalities for array manipulation and mathematical operations. It is designed to be easy to use and integrates well with other JavaScript libraries. Compared to cwise-compiler, numjs offers a higher-level API and is more focused on ease of use rather than performance optimization.
mathjs
Mathjs is an extensive mathematics library for JavaScript and Node.js. It includes a wide range of mathematical functions and supports complex numbers, matrices, and units. While cwise-compiler is specialized for high-performance array operations, mathjs provides a broader set of mathematical tools and is suitable for a wider range of applications.
cwise-compiler
Just the compiler from cwise. You can call this directly if you know what you are doing and want to skip calling cwise-parser and including esprima. This is only recommended in extreme cases though. Otherwise you should stick to the default interface in cwise and not mess around with this craziness.
Install
npm install cwise-compiler
require("cwise-compiler")(procedure)
Compiles a cwise procedure for the given procedure. The object procedure must have the following fields:
args
An array of argument types (as in cwise)pre
A parsed pre functionbody
A parsed body functionpost
A parsed post functionfuncName
Name of the functionblockSize
Block size to generatedebug
Debug mode flag
Credits
(c) 2013 Mikola Lysenko. MIT License