npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@stdlib/ndarray-base-function-object

v0.2.2

Published

C APIs for creating and managing ndarray function objects.

Downloads

10

Readme

Function Object

NPM version Build Status Coverage Status

C APIs for creating and managing ndarray function objects.

Installation

npm install @stdlib/ndarray-base-function-object

Usage

var headerDir = require( '@stdlib/ndarray-base-function-object' );

headerDir

Absolute file path for the directory containing header files for C APIs.

var dir = headerDir;
// returns <string>

Examples

var headerDir = require( '@stdlib/ndarray-base-function-object' );

console.log( headerDir );

C APIs

Usage

#include "stdlib/ndarray/base/function_object.h"

ndarrayFunctionObject

Structure for grouping ndarray function information.

struct ndarrayFunctionObject {
    // ndarray function name:
    const char *name;

    // Number of input ndarrays:
    int32_t nin;

    // Number of output ndarrays:
    int32_t nout;

    // Total number of ndarray arguments (nin + nout):
    int32_t narrays;

    // Array containing ndarray functions:
    ndarrayFcn *functions;

    // Number of ndarray functions:
    int32_t nfunctions;

    // Array of type "numbers" (as enumerated elsewhere), where the total number of types equals `narrays * nfunctions` and where each set of `narrays` consecutive types (non-overlapping) corresponds to the set of ndarray argument types for a corresponding ndarray function:
    int32_t *types;

    // Array of void pointers corresponding to the "data" (e.g., callbacks) which should be passed to a respective ndarray function (note: the number of pointers should match the number of ndarray functions):
    void **data;
};

ndarrayFcn

Function pointer type for an ndarray function.

typedef int8_t (*ndarrayFcn)( struct ndarray *arrays[], void *data );

An ndarrayFcn function should accept the following arguments:

  • arrays: [in] struct ndarray** array containing pointers to input and output ndarrays.
  • data: [in] void* function data (e.g., a callback).

An ndarrayFcn function should return a status code (with 0 indicating success).

stdlib_ndarray_function_allocate( _name, nin, nout, _functions, nfunctions, _types, _data[] )

Returns a pointer to a dynamically allocated ndarray function object.

#include "stdlib/ndarray/base/unary.h"
#include "stdlib/ndarray/dtypes.h"
#include <stdlib.h>
#include <stdio.h>

// Define the function(s) we want to apply to ndarrays:
double scale( const double x ) {
    return x * 10.0;
}

// Define a function name:
const char name[] = "unary_ndarray_function";

// Define a list of ndarray functions (in this case, as the function to be applied accepts doubles, we only use ndarray functions which handle doubles as function arguments and, for the purposes of this example, we assume that the output ndarray is always a double-precision floating-point number array):
ndarrayFcn functions[] = {
    stdlib_ndarray_d_d,
    stdlib_ndarray_f_f_as_d_d,
    stdlib_ndarray_u_d_as_d_d,
    stdlib_ndarray_i_d_as_d_d,
    stdlib_ndarray_t_d_as_d_d,
    stdlib_ndarray_k_d_as_d_d,
    stdlib_ndarray_b_d_as_d_d,
    stdlib_ndarray_s_d_as_d_d
};

// Define the **ndarray** argument types for each ndarray function:
int32_t types[] = {
    STDLIB_NDARRAY_FLOAT64, STDLIB_NDARRAY_FLOAT64,
    STDLIB_NDARRAY_FLOAT32, STDLIB_NDARRAY_FLOAT64,
    STDLIB_NDARRAY_UINT32, STDLIB_NDARRAY_FLOAT64,
    STDLIB_NDARRAY_INT32, STDLIB_NDARRAY_FLOAT64,
    STDLIB_NDARRAY_UINT16, STDLIB_NDARRAY_FLOAT64,
    STDLIB_NDARRAY_INT16, STDLIB_NDARRAY_FLOAT64,
    STDLIB_NDARRAY_UINT8, STDLIB_NDARRAY_FLOAT64,
    STDLIB_NDARRAY_INT8, STDLIB_NDARRAY_FLOAT64
};

// Define a list of ndarray function "data" (in this case, callbacks):
void *data[] = {
    (void *)scale,
    (void *)scale,
    (void *)scale,
    (void *)scale,
    (void *)scale,
    (void *)scale,
    (void *)scale,
    (void *)scale
};

// Create a new ndarray function object:
struct ndarrayFunctionObject *obj = stdlib_ndarray_function_allocate( name, 1, 1, functions, 8, types, data );
if ( obj == NULL ) {
    fprintf( stderr, "Error allocating memory.\n" );
    exit( 1 );
}

// Free allocated memory:
stdlib_ndarray_function_free( obj );

The function accepts the following arguments:

  • name: [in] char* ndarray function name.
  • nin: [in] int32_t number of input ndarrays.
  • nout: [in] int32_t number of output ndarrays.
  • functions: [in] ndarrayFcn* array containing ndarray functions.
  • nfunctions: [in] int32_t number of ndarray functions.
  • types: [in] int32_t* array of type "numbers", where the total number of types equals (nin+nout)*nfunctions and where each set of nin+nout consecutive types (non-overlapping) corresponds to the set of ndarray argument types for a corresponding ndarray function.
  • data: [in] void* array of void pointers corresponding to the "data" (e.g., callbacks) which should be passed to a respective ndarray function.
struct ndarrayFunctionObject * stdlib_ndarray_function_allocate( const char *name, int32_t nin, int32_t nout, ndarrayFcn *functions, int32_t nfunctions, int32_t *types, void *data[] )

The function returns a pointer to a dynamically allocated ndarray function or, if unable to allocate memory, a null pointer. The user is responsible for freeing the allocated memory.

stdlib_ndarray_function_free( *obj )

Frees an ndarray function object's allocated memory.

#include "stdlib/ndarray/base/unary.h"
#include "stdlib/ndarray/dtypes.h"
#include <stdlib.h>
#include <stdio.h>

// Define the function(s) we want to apply to ndarrays:
double scale( const double x ) {
    return x * 10.0;
}

// Define a function name:
const char name[] = "unary_ndarray_function";

// Define a list of ndarray functions:
ndarrayFcn functions[] = {
    stdlib_ndarray_d_d
};

// Define the **ndarray** argument types for each ndarray function:
int32_t types[] = {
    STDLIB_NDARRAY_FLOAT64, STDLIB_NDARRAY_FLOAT64
};

// Define a list of ndarray function "data" (in this case, callbacks):
void *data[] = {
    (void *)scale
};

// Create a new ndarray function object:
struct ndarrayFunctionObject *obj = stdlib_ndarray_function_allocate( name, 1, 1, functions, 1, types, data );
if ( obj == NULL ) {
    fprintf( stderr, "Error allocating memory.\n" );
    exit( 1 );
}

// ...

// Free allocated memory:
stdlib_ndarray_function_free( obj );

The function accepts the following arguments:

  • obj: [in] ndarrayFunctionObject* ndarray function object.
void stdlib_ndarray_function_free( struct ndarrayFunctionObject *obj )

stdlib_ndarray_function_dispatch_index_of( _obj, _types )

Returns the first index of a function whose signature satisfies a provided list of array types.

#include "stdlib/ndarray/base/unary.h"
#include "stdlib/ndarray/dtypes.h"
#include <stdlib.h>
#include <stdio.h>

// Define the function(s) we want to apply to ndarrays:
double scale( const double x ) {
    return x * 10.0;
}

// ...

// Define a function name:
const char name[] = "unary_ndarray_function";

// Define a list of ndarray functions:
ndarrayFcn functions[] = {
    stdlib_ndarray_d_d,
    stdlib_ndarray_f_f_as_d_d
};

// Define the **ndarray** argument types for each ndarray function:
int32_t types[] = {
    STDLIB_NDARRAY_FLOAT64, STDLIB_NDARRAY_FLOAT64,
    STDLIB_NDARRAY_FLOAT32, STDLIB_NDARRAY_FLOAT64
};

// Define a list of ndarray function "data" (in this case, callbacks):
void *data[] = {
    (void *)scale,
    (void *)scale
};

// Create a new ndarray function object:
struct ndarrayFunctionObject *obj = stdlib_ndarray_function_allocate( name, 1, 1, functions, 2, types, data );
if ( obj == NULL ) {
    fprintf( stderr, "Error allocating memory.\n" );
    exit( 1 );
}

// ...

// Define a list of types on which to dispatch:
int32_t itypes[] = {
    STDLIB_NDARRAY_FLOAT32, STDLIB_NDARRAY_FLOAT64
};

// Find a function satisfying the list of types:
int64_t idx = stdlib_ndarray_function_dispatch_index_of( obj, itypes );
if ( idx < 0 ) {
    fprintf( stderr, "Unable to find function.\n" );
    exit( 1 );
}

// ...

// Free allocated memory:
stdlib_ndarray_function_free( obj );

The function accepts the following arguments:

  • obj: [in] ndarrayFunctionObject* ndarray function object.
  • types: [in] int32_t* list of array types on which to dispatch.
int64_t stdlib_ndarray_function_dispatch_index_of( const struct ndarrayFunctionObject *obj, const int32_t *types )

If a function is found, the function returns the index of the function, and the function returns -1 if unable to find a function.


Notice

This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.

For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.

Community

Chat


License

See LICENSE.

Copyright

Copyright © 2016-2024. The Stdlib Authors.