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

tensor-contraction

v0.2.0

Published

implements tensor contraction on a single mixed tensor

Downloads

24

Readme

tensor-contraction

implements tensor contraction on a single mixed tensor

Install

With npm do

npm install tensor-contraction --save

Usage

Signature is (addition, indicesPair, tensorDim, tensorData) where

  • addition is a function that defines the scalar operator used
  • indicesPair is an array of two elements that indicates which indices will be used for contraction
  • tensorDim is an array that defines tensor indices set
  • tensorData is an array that defines the tensor components

It returns the contractedTensorData array given by the tensors contraction.

It throws a TypeError: Contraction indices does not have the same dimension. See indices pair check example.

Examples

All code in the examples below is intended to be contained into a single file.

Let's use common real addition.

var tensorContraction = require('tensor-contraction')

function addition (a, b) { return a + b }

var contraction = tensorContraction.bind(null, addition)

Matrix trace

The trace of a matrix is the sum of the components in its diagonal. It is the simplest example of tensor contraction. In the following example, the trace is 1 + 5 + 9 = 15

contraction([0, 1], [3, 3], [1, 2, 3,
                             4, 5, 6,
                             7, 8, 9]).should.be.eql(15)

Indices pair check

It is required that indices chosen for contraction have the same dimension. The following example

contraction([0, 1], [3, 2], [1, 2, 3,
                             4, 5, 6])

will throw with message

Contraction indices does not have the same dimension: 0-th index = 3 but 1-th index = 2.

Order 3

Consider a tensor of order 3 and contract pairing first and last index. A multidimensional array t is used to point out which component corresponds to a given combination of indices, but keep in mind that tensors are always given as a monodimensional array. Note that contracted tensor is a vector with two elements, where:

  1. the first one is given by the sum of the tensor components with middle index equal to 0 and with first and last index equals;
  2. the second one is given by the sum of tensor components with middle index equal to 1 and with first and last index equals.
var t = [[[0, 1], [2, 3]], [[4, 5], [6, 7]]] // t[0][0][0] = 0
                                             // t[0][0][1] = 1
                                             // t[0][1][0] = 2
                                             // t[0][1][1] = 3
                                             // t[1][0][0] = 4
                                             // t[1][0][1] = 5
                                             // t[1][1][0] = 6
                                             // t[1][1][1] = 7

contraction([0, 2], [2, 2, 2], [t[0][0][0], t[0][0][1], t[0][1][0], t[0][1][1],  // [5, 9] = [0 + 5, 2 + 7] =
                                t[1][0][0], t[1][0][1], t[1][1][0], t[1][1][1]]) //        = [t[0][0][0] + t[1][0][1],
                                                                                 //           t[0][1][0] + t[1][1][1]]

Follows calculation of other 2 possible contractions, where the tensorData is given by explicit numeric values.

contraction([0, 1], [2, 2, 2], [0, 1, 2, 3, 4, 5, 6, 7]) // [6, 8]

contraction([1, 2], [2, 2, 2], [0, 1, 2, 3, 4, 5, 6, 7]) // [3, 11]

License

MIT