Javascript 中的元素操作 [英] Element-wise Operations In Javascript
问题描述
我正在做一些物理模拟,其中当然涉及向量.这对我来说变得非常困难,因为据我所知,javascript 不支持这样的东西......
#借助numpy>>>a = np.array([1,2,3])>>>b = np.array([9,2,7])>>>a+b数组([10, 4, 10])
我已经能够通过定义实现相同目的的函数来解决这个限制,但我的公式最终看起来像这样:
add(x, add( mult(v,dt), mult(mult( a(x), .5), Math.pow(dt,2))))
所以我的问题是是否有更好的方法来实现这个功能,它们是我不知道的语言的特性,解决这个问题的库,或者更有效的方法来处理它.
感谢大家的帮助.
查看 Sylvester.我想这可能就是你要找的.p>
但是如果您想自己实现对象,那么最好采用更多的 OOP 方法.JavaScript 是一种基于原型的语言,因此它与其他 OOP 语言略有不同,但它仍然很容易实现您自己的原型.
类似于:
Vector = function(items) {this.items = 物品}Vector.prototype.add = 函数(其他){变量结果 = []for(var i = 0; i < this.items; i++) {result.push( this.items[i] + other.items[i])}返回新向量(结果);}Vector.prototype.subtract = function(other) {/* 减法代码 */}Vector.prototype.multiply = function(other) {/* 乘法代码 */}
然后像这样使用它们:
var a = new Vector([1,2,3]);var b = new Vector([5,0,1]);var 结果 = a.add(b)result.items//[6,2,4]
或者,如果你愿意,你也可以使用一些函数扩展 Array 类
Array.prototype.vectorAdd = function(other) {/* 添加另一个数组作为向量的代码 */};
并调用它使用
[1,2,3].vectorAdd([5,0,1])
希望这可以为您提供一个起点,让您的代码更具可读性.
另外一个注意事项:不幸的是,在这种情况下,JavaScript 不支持操作重载,因此您不能做像 a+b
这样的整洁的东西.您必须执行类似 a.add(b)
的操作.但是只要你返回一个合适的对象,你就可以将方法链接在一起.喜欢:
a.add(b).multiply(c).subtract(d);
ps.所呈现的代码可能有点偏离",我只是从头顶上输入它,所以更像对待伪代码 :)
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.
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 :)
这篇关于Javascript 中的元素操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!