
Product
Secure Your AI-Generated Code with Socket MCP
Socket MCP brings real-time security checks to AI-generated code, helping developers catch risky dependencies before they enter the codebase.
Complex is a additional Type to deal with Complex Numbers in JavaScript. It provides several methods to add, multiply numbers as well as calculate the magnitude and angle in the complex plane.
You can get this package with NPM:
npm install Complex
var Complex = require('Complex');
console.log(new Complex(3, 4).abs()); // 5
Complex can be built for the browser with wrapup or other tools that can generate browser JS from Node packages.
Testing is done with Mocha and Expect.js:
# install dependencies
npm install
# run the tests in node
./node_modules/.bin/mocha test/Complex.js
or testing in the browser:
# install dependencies
npm install
# run a small node server
node ./test/server.js
# run tests
google-chrome http://localhost:3000
var z = new Complex(real im);
A in line function like Number.from.
var z = Complex.from(real[, im]);
Or
1+4i
var z = Complex.from(2, 4);
var z = Complex.from(5);
var z = Complex.from('2+5i');
Creates a complex instance from a polar representation: r*e^(phi*i) = r (cos(phi) + i sin(phi))
var z = Complex.fromPolar(r, phi);
A instance of the imaginary unit i
var i = Complex.i;
A instance for the real number 1
var one = Complex.one;
Sets the real and imaginary properties a and b from a + bi
myComplex.fromRect(real, im);
Sets the a and b in a + bi
from a polar representation.
myComplex.fromPolar(r, phi);
Sets the precision of the numbers. Similar to Number.prototype.toPrecision. Useful befor printing the number with the toString method.
myComplex.toPrecision(k);
Formats a number using fixed-point notation. Similar to Number.prototype.toFixed. Useful before printing the number with the toString method.
myComplex.toFixed(k);
Finalizes the instance. The number will not change and any other method call will return a new instance. Very useful when a complex instance should stay constant. For example the Complex.i variable is a finalized instance.
myComplex.finalize();
Calculates the magnitude of the complex number
myComplex.magnitude();
Calculates the angle with respect to the real axis, in radians.
myComplex.angle();
Calculates the conjugate of the complex number (multiplies the imaginary part with -1)
myComplex.conjugate();
Negates the number (multiplies both the real and imaginary part with -1)
myComplex.negate();
Multiplies the number with a real or complex number
myComplex.multiply(z);
Divides the number by a real or complex number
myComplex.divide(z);
Adds a real or complex number
myComplex.add(z);
Subtracts a real or complex number
myComplex.subtract(z);
Returns the base to the exponent
myComplex.pow(z);
Returns the square root
myComplex.sqrt();
Returns the natural logarithm (base E
)
myComplex.log([k]);
ln(z) = ln|z| + arg(z)
) where arg(z) can return the same for different angles (every 2*pi), with this argument you can define which answer is requiredCalculates the e^z
where the base is E
and the exponential the complex number.
myComplex.exp();
Calculates the sine of the complex number
myComplex.sin();
Calculates the cosine of the complex number
myComplex.cos();
Calculates the tangent of the complex number
myComplex.tan();
Calculates the hyperbolic sine of the complex number
myComplex.sinh();
Calculates the hyperbolic cosine of the complex number
myComplex.cosh();
Calculates the hyperbolic tangent of the complex number
myComplex.tanh();
Returns a new Complex instance with the same real and imaginary properties
myComplex.clone();
Returns a string representation of the complex number
myComplex.toString();
new Complex(1, 2).toString(); // 1+2i
new Complex(0, 1).toString(); // i
new Complex(4, 0).toString(); // 4
new Complex(1, 1).toString(); // 1+i
'my Complex Number is: ' + (new Complex(3, 5)); // 'my Complex Number is: 3+5i
Checks if the real and imaginary components are equal to the passed in compelex components.
myComplex.equals(z);
new Complex(1, 4).equals(new Complex(1, 4)); // true
new Complex(1, 4).equals(new Complex(1, 3)); // false
Copyright (c) 2014 Arian Stolwijk
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
Do calculations with Complex numbers
The npm package complex receives a total of 173 weekly downloads. As such, complex popularity was classified as not popular.
We found that complex demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket MCP brings real-time security checks to AI-generated code, helping developers catch risky dependencies before they enter the codebase.
Security News
As vulnerability data bottlenecks grow, the federal government is formally investigating NIST’s handling of the National Vulnerability Database.
Research
Security News
Socket’s Threat Research Team has uncovered 60 npm packages using post-install scripts to silently exfiltrate hostnames, IP addresses, DNS servers, and user directories to a Discord-controlled endpoint.