Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Element-wise Operations In Javascript

Tags:

I'm doing some physics simulations which of course involve vectors. This has become very difficult for me because to the best of my knowledge javascript doesn't support anything like this...

#with the aid of numpy >>> a = np.array([1,2,3]) >>> b = np.array([9,2,7]) >>> a+b array([10,  4, 10]) 

I've been able to work around this limitation by defining functions that will achieve the same thing, but my formulas end up looking like this:

add(x, add( mult(v,dt), mult(mult( a(x), .5), Math.pow(dt,2)))) 

So my question is whether there are better ways to achieve this functionality, whether they be features of the language I am unaware of, libraries that address this issue, or more effective ways to deal with it.

Thanks for the help all.

like image 510
danem Avatar asked Aug 21 '11 01:08

danem


People also ask

What is '$' in JavaScript?

Updated on July 03, 2019. The dollar sign ($) and the underscore (_) characters are JavaScript identifiers, which just means that they identify an object in the same way a name would. The objects they identify include things such as variables, functions, properties, events, and objects.

What does 3 dots mean in JavaScript?

(three dots in JavaScript) is called the Spread Syntax or Spread Operator. This allows an iterable such as an array expression or string to be expanded or an object expression to be expanded wherever placed.

How many array methods are there in JavaScript?

In JavaScript, arrays have three static methods.

What is the correct way to write a JavaScript array?

The correct way to write a JavaScript array var txt = new Array("arr ","kim","jim"). JavaScript arrays are used to store multiple values in a single variable. Using an array literal is the easiest way to create a JavaScript Array.


2 Answers

we can use the map function to add array elements:

function addvector(a,b){     return a.map((e,i) => e + b[i]); } addvector([2,3,4],[4,7,90]) # returns [6,10,94] 
like image 56
Varn K Avatar answered Sep 25 '22 18:09

Varn K


Check out Sylvester. I think it might be what you are looking for.

But if you wanted to implement the objects yourself, then it might be better to do a more OOP approach. JavaScript is a prototype-based language, so it different a little bit from other OOP languages, but its still pretty easy to implement your own prototypes.

Something like:

Vector = function(items) {     this.items = items }  Vector.prototype.add = function(other) {     var result = []     for(var i = 0; i < this.items; i++) {         result.push( this.items[i] + other.items[i])     }      return new Vector(result); }  Vector.prototype.subtract = function(other) { /* code to subtract */ } Vector.prototype.multiply = function(other) { /* code to multiply */ } 

And then use them like this:

var a = new Vector([1,2,3]); var b = new Vector([5,0,1]);  var result = a.add(b) result.items // [6,2,4] 

Or if you wanted to, you could also extend the Array class with some functions with

Array.prototype.vectorAdd = function(other) { /* code to add another array as a vector */ }; 

And call that using

[1,2,3].vectorAdd([5,0,1]) 

Hopefully, that might give you a starting point to make your code a little more readable.

Just another note: Unfortunately in this case, JavaScript doesn't support operation overloading so you can't do neat stuff like a+b. You'll have to do something like a.add(b). but as long you return an appropriate object you can chain methods together. Like:

a.add(b).multiply(c).subtract(d); 

ps. the presented code might be a little "off", I just typed it up off the top of my head, so treat it more like pseduocode :)

like image 20
J. Holmes Avatar answered Sep 24 '22 18:09

J. Holmes