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.
– Luiz Felipe
I edited the question to include the snippet
– Lucas
How did you include the library? Used some
<script src>
with a CDN, for example? Or running on Node.js?– Luiz Felipe
I’m running on Ode.js
– Lucas
You installed the library using
npm
? Is using therequire
? Still missing details in your question.– Luiz Felipe
I’m not using the
require
. Sorry, I am a complete Javascript Newbie. This moduleMath
works in the browser, but to use the Node I need to import? How do I import?– Lucas
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).– Luiz Felipe