Git Product home page Git Product logo

Comments (3)

DirkToewe avatar DirkToewe commented on May 24, 2024

Only a small part of nd4js supports custom data types:

  • opt (optimization) submodule does not support custom data types
  • la (linear algebra) submodule does not support custom data types (except for matmul)
  • N-ary operations and factory method should be data type agnostic and should work fine

Below, You will find a few code snippets that show how to use nd4js with bignumber.js. I have also created a RunKit Notebook here so You can play around with the code more easily. Hope this helps.

Factory Methods

All the factory methods should work:

const nd = require("nd4js"),
  BigNum = require('bignumber.js');

const A = nd.tabulate([3,3], (i,j) => BigNum(11+10*i+j));
console.log('A:\n' + A);

const B = nd.array(
  [[BigNum(1), BigNum(2), BigNum(3)],
   [BigNum(4), BigNum(5), BigNum(6)],
   [BigNum(7), BigNum(8), BigNum(9)]]
);
console.log('B:\n' + B);

N-ary Operations

Unary, Binary, Ternary, ... operations all should work with any data type:

console.log('2*A=\n' + A.mapElems(a => a.times(2)) );

console.log('A+B=\n' + nd.zip_elems([A,B], (a,b) => a.plus(b)) );

Matmul

In order to use matrix multiplication, You will have to override the default scalar addition and multiplication in nd.math:

const default_add = nd.math.add,
      default_mul = nd.math.mul;
// override addition
nd.math.add = (x,y) => {
  if( x instanceof BigNum ||
      y instanceof BigNum ) {
    if( !(x instanceof BigNum) ) x = BigNum(x);
    return x.plus(y);
  }
  else
    return default_add(x,y);
}
// override multiplication
nd.math.mul = (x,y) => {
  if( x instanceof BigNum ||
      y instanceof BigNum ) {
    if( !(x instanceof BigNum) ) x = BigNum(x);
    return x.times(y);
  }
  else
    return default_mul(x,y);
}

console.log('AB=\n' + nd.la.matmul2(A,B));

from nd4js.

iammadab avatar iammadab commented on May 24, 2024

Great, I can work with this. This helps. Thanks.

from nd4js.

DirkToewe avatar DirkToewe commented on May 24, 2024

Happy to be of help. If You have any more questions, experience any issues or have any feature requests, feel free to (re)open this issue or a new one.

from nd4js.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.