How to get the product to scale two vectors in Javascript?

Asked

Viewed 108 times

1

In python, if I have two vectors, I can get the scalar product using numpy. Following example:

x=np.array([2,4,6])                                                                                                                                                                
y=np.array([1,3,5])                                                                                                                                                                
np.dot(x,y)                                                                                                                                                                        

Returns:

44

I’m looking for an equivalent function in Javascript. I found a call Math of.. But unfortunately, I’m not able to use this function. I tried:

let a=[2,4,6];
let b=[1,3,5];
console.log(math.dot(a,b))

That returns the error:

ReferenceError: math is not defined

I tried to write Math capital:

let a=[2,4,6];
let b=[1,3,5];
console.log(Math.dot(a,b))

But returned the error:

TypeError: Math.dot is not a function

What am I doing wrong here? How can I get a product to scale in Javascript?

  • Try to reproduce the error here, please? A suggestion is to use the code snippet.

  • I edited the question to include the snippet

  • How did you include the library? Used some <script src> with a CDN, for example? Or running on Node.js?

  • I’m running on Ode.js

  • You installed the library using npm? Is using the require? Still missing details in your question.

  • I’m not using the require. Sorry, I am a complete Javascript Newbie. This module Math works in the browser, but to use the Node I need to import? How do I import?

  • 1

    No need to apologize!! : ) You can access the "module" Math because he is a global object built-in Javascript. Already the library in question (mathjs) which you referred to is not "native", so you need to import it in some way. I’m just trying to understand what you did (because the question currently needs more details).

Show 2 more comments

2 answers

4


If you are on Node.js[Commentary], install the library through a package manager. In Node.js, the most common package managers are npm (which is already installed by default with Node.js) and Yarn, which can be installed separately.

To install a library using Node.js, from the root folder of your project, run the command:

npm install <nome da biblioteca>

In your case, you should do npm install mathjs, in accordance with documentation points. You can check the library page mathjs on the npm website here.

Once you have installed the library, you can use the require to import it:

const math = require('mathjs');

let a = [2,4,6];
let b = [1,3,5];
console.log(math.dot(a, b)); //=> 44

Note that unlike Cdns (commonly used in browsers for importing libraries), in Node.js objects are not exposed globally. So you should import the library you want to explicitly use.

The require of Node.js follows the Commonjs pattern. There is also Ecmascript modules, which can be used as an alternative "syntax" to Commonjs in newer versions of Node.js.

As a curiosity, if you are in Node.js version that supports, you can use the syntax of Ecmascript modules:

import * as math from 'mathjs';

let a = [2,4,6];
let b = [1,3,5];
console.log(math.dot(a, b)); //=> 44

Learn more about the Commonjs in the context of Node.js. E on the Ecmascript modules.

2

The math.js works perfect and returns the same result:

let a=[2,4,6];
let b=[1,3,5];
console.log(math.dot(a,b))
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/9.0.0/math.js"></script>

By the message not added the lib properly, can download or use the link to a Cdn on the internet and add in the tag <script>. See here some Cdn links you can use: https://cdnjs.com/libraries/mathjs

If you are going to use Node or a script on the server, you need to install the package, the instructions are also on the Math page: https://www.npmjs.com/package/mathjs

You can install with the npm, using the command: npm install mathjs

  • My difficulty is in the same import part. I was using the module Math no matter, but now I discovered that it is built-in and so I was able to use it. This other module I need to import. I just need to understand the import process now. The line <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/9.0.0/math.js"></script> is in an HTML or the file itself js? I’m spinning the js in the Node

  • yes, the tag script should be on the same page where is the javascript code that will use Math :)

  • and how do I import using only the file js on the Node?

  • 1

    ah calm there, will use it in Ode? then no use install on the page, you will need to install the package. Here on the Math page you have the instructions: https://www.npmjs.com/package/mathjs To help I’ll put the answer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.