Javascript中的元素操作 [英] Element-wise Operations In Javascript

查看:76
本文介绍了Javascript中的元素操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一些物理模拟,当然涉及矢量。这对我来说变得非常困难,因为据我所知,javascript不支持这样的任何内容...

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.

感谢所有的帮助。

推荐答案

查看西尔维斯特。我想这可能就是你要找的东西。

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

但如果你想自己实现这些对象,那么做一个更多的OOP方法可能会更好。 JavaScript是一种基于原型的语言,因此它与其他OOP语言略有不同,但它仍然很容易实现自己的原型。

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.

类似于:

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 */ }

然后像这样使用它们:

var a = new Vector([1,2,3]);
var b = new Vector([5,0,1]);

var result = a.add(b)
result.items // [6,2,4]

或者如果你愿意,你也可以用一些函数扩展Array类

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 */ };

并使用

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

希望这可能会给你一个起点,使你的代码更具可读性。

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

另一个注意事项:不幸的是,在这种情况下,JavaScript没有支持操作重载,所以你不能做像 a + b 这样的整洁的东西。您必须执行类似 a.add(b)的操作。但只要你返回一个合适的对象,你就可以将方法链接在一起。喜欢:

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。所呈现的代码可能有点关闭,我只是把它打成了我的头顶,所以更像是pseduocode:)

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 :)

这篇关于Javascript中的元素操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆