如何在 JavaScript 中使用虚数进行计算?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15399340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to calculate with imaginary numbers in JavaScript?
提问by u283863
Recently, I am trying to calculate using some equations that involve the imaginary number i
in them. However, unlike e
or π
, there isn't any methods or native functions that will return i
. A quick searchin Google didn't get me any answer. Any ideas on how to achieve it?
最近,我正在尝试使用一些涉及虚数的方程进行计算i
。但是,与e
或不同的π
是,没有任何方法或本机函数会返回i
。一个快速搜索在谷歌没有得到我任何答复。关于如何实现它的任何想法?
function imaginary(){
return {
rational: this,
imaginary: "2i" //magic code that does this
};
};
Number.prototype.imaginary = imaginary;
采纳答案by mbeckish
Assuming you really want complex numbers, and not just the imaginary component:
假设您真的想要复数,而不仅仅是虚部:
I would model a complex number just as you would model a 2D point, i.e. a pair of numbers.
我会模拟一个复数,就像你模拟一个二维点,即一对数字。
Just as a point has x and y components, so a complex number has real and imaginary components. Both components can just be modeled with ordinary numeric types (int, float, etc.)
正如一个点有 x 和 y 分量一样,复数也有实部和虚部。两个组件都可以用普通的数字类型(int、float 等)建模
However, you will need to define new functionality for all of the mathematical operations.
但是,您需要为所有数学运算定义新功能。
Addition and subtraction of complex numbers works the same way as addition and subtraction of points - add the separate components to each other, don't mix them. For example:
复数的加法和减法与点的加法和减法的工作方式相同 - 将单独的组件彼此相加,不要混合它们。例如:
(3+2i)+(5+4i) = (8+6i)
(3+2i)+(5+4i) = (8+6i)
Multiplication works just like you learned in algebra when multiplying (a+b)*(c+d) = (ac+ad+bc+bd).
乘法就像你在代数中学到的乘法 (a+b)*(c+d) = (ac+ad+bc+bd) 一样。
Except now you also have to remember that i*i = -1. So:
除了现在你还必须记住 i*i = -1。所以:
(a+bi)*(c+di) = (ac+adi+bci+bdii) = (ac-bd) + (ad+bc)i
(a+bi)*(c+di) = (ac+adi+bci+bdii) = (ac-bd) + (ad+bc)i
For division and exponentiation, see http://en.wikipedia.org/wiki/Complex_number
对于除法和幂运算,请参阅http://en.wikipedia.org/wiki/Complex_number
回答by Jos de Jong
The math.js library supports complex numbers, matrices, and more. The library is compatible with JavaScript's built-in Math library, so quite easy to use.
math.js 库支持复数、矩阵等。该库与 JavaScript 的内置 Math 库兼容,因此非常易于使用。
You can just do things like:
您可以执行以下操作:
math.i; // i
math.sqrt(-4) // 2i
var a = math.complex('2 + 3i'); // 2 + 3i
var b = math.complex(4, 5); // 4 + 5i
math.add(a, b); // 6 + 8i
math.multiply(a, b); // -7 + 22i
math.eval('e^(pi*i) + 1'); // ~0
// etc...
Edit: note that math.js comes with an expression parser, which makes it more convenient to work with complex values and mathematical expressions:
编辑:请注意 math.js 带有一个表达式解析器,它可以更方便地处理复杂的值和数学表达式:
math.eval('(2 + 3i) * (4 + 5i)'); // -7 + 22i
回答by ebram khalil
I'm not math expert but I tried to search with another term and I got different results.
我不是数学专家,但我尝试用另一个术语进行搜索,但得到了不同的结果。
Check these:
检查这些:
I hope this helps.
我希望这有帮助。