Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
complex-calculator
Advanced tools
This is a calculator that performs computations related to complex numbers and polynomials.
This is a node module that can deal with complex numbers and complex polynomial functions.
npm install complex-calculator
There are two classes exposed by this module, ComplexNumber
and Polynomial
.
ComplexNumber
classThe constructor takes a real number and an imaginary number as arguments.
import {ComplexNumber} from 'complex-calculator';
const a = new ComplexNumber(-1, 1);
const b = new ComplexNumber(1, 1);
console.log(a.textVersion()) // -1 + i
console.log(b.textVersion()) // 1 + i
console.log(a.times(b).textVersion()) // -2
console.log(a.plus(b).textVersion()) // 2i
real
(Number): the real part of the complex number.
imaginary
(Number): the imaginary part of the complex number.
conjugate
: returns the complex conjugate of the number.
display
: logs the text version of the number and returns the number (for debugging in the middle of long computations).
mod
: returns the modulus (or size) of the complex number.
polar
: returns the polar representation of the complex number. This is its own data structure with its own API.
pow
: raises the complex number to a real exponent.
plus
: performs complex number addition.
textVersion
: returns a string that is formatted the way that the complex number would be written longhand.
times
: performs complex number multiplication.
PolarNumber
classThis class is not directly exposed, but rather it is accessed via the ComplexNumber class.
import {ComplexNumber} from 'complex-calculator';
const a = new ComplexNumber(-1, 1);
const b = new ComplexNumber(1, 1);
console.log(a.polar().textVersion()) // 1.4142135623730951e^(0.75πi)
console.log(a.polar().times(b.polar()).textVersion()) // 2.0000000000000004e^(1πi) <- Thanks, floating point arithmetic in base 2 :(
console.log(a.polar().times(b.polar()).rectangular().textVersion()) // -2
r
(Number): the distance from the origin to the complex number.
th
(Number): a numerical representation of the polar angle that is formed by the complex number. Multiplying this number by π will result in the radian measurement of the angle. More directly, it is the fraction of a half rotation. This will always be a number such that 0 <= a.th && a.th < 2
.
display
: logs the textVersion of the polar number and returns the polar number.
pow
: raises the polar number to a real exponent.
rectangular
: returns the complex number that corresponds to the polar number.
textVersion
: returns a string of the form re^(θi)
where r
is the modulus of the polar number and θ
is the angle of the polar number in radians.
times
: multiplies the polar number by another polar number.
Polynomial
classimport {Polynomial, ComplexNumber} from 'complex-calculator';
const p = new Polynomial([-1, 0, 1]);
console.log(p.textVersion()) // -1 + x^2
console.log(p.factor().textVersion()) // (x + 1)(x - 1)
const a = new ComplexNumber(-2, 4);
const q = new Polynomial([a, [1, 2], 1]);
console.log(q.textVersion()) // (-2 + 4i) + (1 + 2i)x + x^2
console.log(q.factor().textVersion()) // (x + 2)(x + -1 + 2i)
coefficients
(ComplexNumber[]): the list of coefficients of the polynomial. The index of each element of the list corresponds to the order of the term for that coefficient.
degree
(Number): the largest power that appears in any of the terms of the polynomial.
display
: logs the textVersion of the polynomial and returns the polynomial.
divide
: divides the polynomial by another polynomial and returns just the quotient (similar to the way that integer division works in c based languages).
division
[sic]: divides the polynomial by another polynomial. The result is represented as a quotient and a remainder.
_.isEqual(p.division(q), {quotient: r, remainder: s})
exactly when
_.isEqual(p, q.times(r).plus(s))
evaluate
: returns the output of the polynomial function for a given input.
expand
: returns the polynomial (used for consistency when using factor
).
factor
: returns a factored representation of the polynomial if the polynomial's degree is less than or equal to 3.
plus
: adds the polynomial function to another polynomial function.
push
: adds another term to the polynomial with an order one higher than the current degree of the polynomial.
undefined
remainder
: divides the polynomial by another polynomial and returns just the remainder (similar to the modulus of two integers).
textVersion
: a string representing the way that a person would write the polynomial long hand. Uses "^" to denote exponentiation.
times
: multiplies the polynomial function by another polynomial function.
Factored
classThis is another class that is only indirectly exposed.
factors
(ComplexNumber[]): this is a slight misnomer in a mathematical sense. The first element in this array is the coefficient of the largest order term of the polynomial. The remaining values are the input values of the factored polynomial that will result in an output of 0.
display
: logs the textVersion of the factored polynomial and returns the factored polynomial.
expand
: returns the polynomial that the factored polynomial came from.
textVersion
: a string representing the polynomial as a product of binomials. If the factored polynomial has factors
[a0,a1,...,an], then the string will look like a0(x + -a1)...(x + -an).
Note that a0 is not a factor. It was merely convenient to use the 0th element of the factors
array for this value.
1: If the argument is an array, it will be interpreted as [<real part>, <imaginary part>]
.
2: If the argument is an array, it will be interpreted as [<modulus>, <angle>]
where the angle will be the th
attribute of the polar number.
3: If the argument is an array, it will be interpreted as new Polynomial(argument)
.
4: If the argument is a Number, it will be interpreted as new ComplexNumber(argument, 0)
.
FAQs
This is a calculator that performs computations related to complex numbers and polynomials.
We found that complex-calculator 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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.