Code Monkey home page Code Monkey logo

phasor.js's Introduction

Phasor.js

Coverage Status

Phasor.js is a wasm (via Rust) implementation of complex numbers that strives to yield correct results.

Example

import {i, rect} from 'phasor.js';

i(Math.PI).exp().add(rect(1)); // approximately rect(0);

Motivation

Phasor.js was originally developed as part of Steady to provide the core complex algebra primitives required in the implementation of algorithms that solve electrical circuits.

While it wasn't Steady's goal to implement complex numbers from scratch, circumstances proved it necessary given the fact that, at the time (Feb 2018), no alternative library available on NPM was able to correctly handle edge cases such as complex numbers with infinite magnitude.

For example, all four alternatives tested[a][b][c][d] compute ∞i × -∞i to NaN instead of and three of them also generally fail to prevent overflow/underflow even in trivial expressions such as 1E200i / 1E200i and 1E-200i / 1E-200i.

Phasor.js on the other hand successfully passes all of the following assertions.

i(Infinity).mul(i(-Infinity)).ulpsEq(rect(Infinity));
i(1E200).div(i(1E200)).ulpsEq(rect(1));
i(1E-200).div(i(1E-200)).ulpsEq(rect(1));

In 2020, Phasor.js was re-implemented from scratch in Rust.

API Reference

rect(re, im = 0)

Constructs a complex number given it's real and imaginary parts.

polar(mag, ang = 0)

Constructs a complex number given it's magnitude and angle.

i(im = 0)

Constructs a purely imaginary number.

Example:

i(42).ulpsEq(rect(0, 42));

p.real()

Extracts the real part of a complex number.

Example:

rect(3).real() === 3;
rect(3, 4).real() === 3;

p.imag()

Extracts the imaginary part of a complex number.

Example:

rect(3).real() === 0;
rect(3, 4).imag() === 4;

p.norm()

Extracts the magnitude of a complex number.

Example:

rect(3, 4).norm() === 5;

p.angle()

Extracts the angle of a complex number.

Example:

rect(3, 4).angle() === Math.atan2(4, 3);

absDiffEq(c1, c2, e = Number.EPSILON)

Compares two complex numbers for approximate equality, optionally taking a positive residue.

relativeEq(c1, c2, e = Number.EPSILON, rel = Number.EPSILON)

Compares two complex numbers for approximate equality, optionally taking a positive residue and a maximum relative distance.

ulpsEq(c1, c2, e = Number.EPSILON, ulps = 4)

Compares two complex numbers for approximate equality, optionally taking a positive residue and the maximum distance of Units in the Last Place.

p.add(q)

Computes the addition of two complex numbers.

Example:

rect(3).add(rect(0, 4)).ulpsEq(rect(3, 4));

p.sub(q)

Computes the subtraction of two complex numbers.

Example:

rect(3).sub(rect(0, 4)).ulpsEq(rect(3, -4));

p.mul(q)

Computes the multiplication of two complex numbers.

Example:

rect(3).mul(rect(0, 4)).ulpsEq(i(12));

p.div(q)

Computes the division of two complex numbers.

Example:

rect(3).div(rect(0, 4)).ulpsEq(i(-0.75));

p.neg()

Computes the opposite of a complex number.

Example:

rect(3, 4).neg().ulpsEq(rect(-3, -4));

p.conj()

Computes the conjugate of a complex number.

Example:

rect(3, 4).conj().ulpsEq(rect(3, -4));

p.recip()

Computes the reciprocal of a complex number.

Example:

rect(3, 4).recip().ulpsEq(rect(3 / 25, -4 / 25));

p.exp()

Computes the exponential of a complex number.

Example:

rect(3, 4).exp().ulpsEq(polar(Math.exp(3), 4));

p.ln()

Computes the principal natural logarithm of a complex number.

Example:

rect(3, 4).ln().ulpsEq(rect(Math.log(5), Math.atan(4 / 3)));

p.log()

Computes the principal logarithm of a complex number to an arbitrary base.

Example:

rect(3, 4).log(10).ulpsEq(rect(Math.log10(5), Math.atan(4 / 3) / Math.log(10)));

p.sinh()

Computes the hyperbolic sine of a complex number.

Example:

const p = rect(3, 4);
const q = polar(
    -Math.hypot(Math.sinh(3), Math.sin(4)),
    Math.atan2(Math.tan(4), Math.tanh(3)),
);

p.sinh().ulpsEq(q);

p.cosh()

Computes the hyperbolic cosine of a complex number.

Example:

const p = rect(3, 4);
const q = polar(
    -Math.hypot(Math.sinh(3), Math.cos(4)),
    Math.atan2(Math.tan(4), 1 / Math.tanh(3)),
);

p.cosh().ulpsEq(q);

p.isNaN()

Returns true if either the imaginary or real part (or both) of a complex number is NaN.

Example:

console.assert(!i(0).isNaN());
console.assert(!i(1).isNaN());
console.assert(!i(1E-315).isNaN())
console.assert(!i(Infinity).isNaN());
console.assert(i(NaN).isNaN());

p.isInfinite()

Returns true if the magnitude of a complex number is Infinity.

Example:

console.assert(!i(0).isInfinite());
console.assert(!i(1).isInfinite());
console.assert(!i(1E-315).isInfinite())
console.assert(i(Infinity).isInfinite());
console.assert(!i(NaN).isInfinite());

p.isFinite()

Returns true if a complex number is neither NaN nor infinite.

Example:

console.assert(i(0).isFinite());
console.assert(i(1).isFinite());
console.assert(i(1E-315).isFinite())
console.assert(!i(Infinity).isFinite());
console.assert(!i(NaN).isFinite());

p.isZero()

Returns true if the magnitude of a complex number is zero.

Example:

console.assert(i(0).isZero());
console.assert(!i(1).isZero());
console.assert(!i(1E-315).isZero())
console.assert(!i(Infinity).isZero());
console.assert(!i(NaN).isZero());

p.isSubnormal()

Returns true if the magnitude of a complex number is subnormal.

Example:

console.assert(!i(0).isSubnormal());
console.assert(!i(1).isSubnormal());
console.assert(i(1E-315).isSubnormal())
console.assert(!i(Infinity).isSubnormal());
console.assert(!i(NaN).isSubnormal());

p.isNormal()

Returns true if a complex number is not NaN, infinite, zero, or subnormal.

Example:

console.assert(!i(0).isNormal());
console.assert(i(1).isNormal());
console.assert(!i(1E-315).isNormal())
console.assert(!i(Infinity).isNormal());
console.assert(!i(NaN).isNormal());

p.isReal()

Returns true if a complex number is purely real.

Example:

console.assert(rect(3).isReal());
console.assert(!rect(0, 4).isReal());
console.assert(!rect(3, 4).isReal());

p.isImaginary()

Returns true if a complex number is purely imaginary.

Example:

console.assert(!rect(3).isImaginary());
console.assert(rect(0, 4).isImaginary());
console.assert(!rect(3, 4).isImaginary());

Under the Hood

Complex numbers are represented under the hood by their magnitude and the tangent of its angle, which might seem unusual, but it's one of the main reasons why Phasor.js is able to yield numerically correct results.

The tangent of an angle has many advantages from the point of view of numerical algorithms than angles expressed in radians, the most important one being the fact that it makes it possible to implement most algorithms, in particular the four fundamental operations (add, sub, mul and div), without relying on any trigonometric function, which are highly non-linear and a major source of numerical error.

Phasor.js essentially takes advantage of the following two trigonometric identities that rely on the less non-linear square root function:

cos(tan(α)) ≡ 1 / sqrt(1 + α^2)
sin(tan(α)) ≡ sign(α) / sqrt(1 + 1 / α^2)

These identities are particularly convenient, because those square roots in the denominators can take advantage of the hypot algorithm, which efficiently avoids unnecessary over/underflows.

phasor.js's People

Contributors

brunocodutra avatar dependabot[bot] avatar github-actions[bot] avatar greenkeeper[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

phasor.js's Issues

An in-range update of babel7 is breaking the build 🚨


☝️ Important announcement: Greenkeeper will be saying goodbye 👋 and passing the torch to Snyk on June 3rd, 2020! Find out how to migrate to Snyk and more at greenkeeper.io


There have been updates to the babel7 monorepo:

    • The devDependency @babel/core was updated from 7.8.7 to 7.9.0.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

This monorepo update includes releases of one or more dependencies which all belong to the babel7 group definition.

babel7 is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • coverage/coveralls: First build on greenkeeper/monorepo.babel7-20200322111342 at 100.0% (Details).
  • continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of ts-jest is breaking the build 🚨

The devDependency ts-jest was updated from 23.10.0 to 23.10.1.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

ts-jest is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push: The Travis CI build failed (Details).

Commits

The new version differs by 13 commits.

  • d9c5b45 Merge pull request #743 from huafu/release/23.10.1
  • e4a3a09 chore(release): 23.10.1
  • ab94359 Merge pull request #742 from huafu/fix-740-no-js-compilation-with-allow-js
  • a844fd4 Merge branch 'master' into fix-740-no-js-compilation-with-allow-js
  • 18dced1 Merge pull request #741 from huafu/e2e-weird-deep-paths
  • 9e7d6a0 test(config): adds test related to allowJs
  • 374dca1 fix(compile): js files were never transpiled thru TS
  • 70fd9af ci(cache): removes some paths from the caching
  • c12dfff fix(windows): normalize paths
  • 0141098 test(e2e): deep paths and coverage
  • 6ccbff3 Merge pull request #736 from huafu/detect-import-and-throw-if-none
  • a2a4be2 fix(config): warn instead of forcing ESM interoperability
  • 21644da Merge pull request #735 from huafu/master

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of @types/jest is breaking the build 🚨

The devDependency @types/jest was updated from 24.0.15 to 24.0.16.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

@types/jest is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of webpack is breaking the build 🚨

The devDependency webpack was updated from 4.35.3 to 4.36.0.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

webpack is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).

Release Notes for v4.36.0

Features

  • SourceMapDevToolPlugin append option now supports the default placeholders in addition to [url]
  • Arrays in resolve and parser options (Rule and Loader API) support backreferences with "..." when overriding options.
Commits

The new version differs by 42 commits.

  • 95d21bb 4.36.0
  • aa1216c Merge pull request #9422 from webpack/feature/dot-dot-dot-merge
  • b3ec775 improve merging of resolve and parsing options
  • 53a5ae2 Merge pull request #9419 from vankop/remove-valid-jsdoc-rule
  • ab75240 Merge pull request #9413 from webpack/dependabot/npm_and_yarn/ajv-6.10.2
  • 0bdabf4 Merge pull request #9418 from webpack/dependabot/npm_and_yarn/eslint-plugin-jsdoc-15.5.2
  • f207cdc remove valid jsdoc rule in favour of eslint-plugin-jsdoc
  • 31333a6 chore(deps-dev): bump eslint-plugin-jsdoc from 15.3.9 to 15.5.2
  • 036adf0 Merge pull request #9417 from webpack/dependabot/npm_and_yarn/eslint-plugin-jest-22.8.0
  • 37d4480 Merge pull request #9411 from webpack/dependabot/npm_and_yarn/simple-git-1.121.0
  • ce2a183 chore(deps-dev): bump eslint-plugin-jest from 22.7.2 to 22.8.0
  • 0beeb7e Merge pull request #9391 from vankop/create-hash-typescript
  • bf1a24a #9391 resolve super call discussion
  • bd7d95b #9391 resolve discussions, AbstractMethodError
  • 4190638 chore(deps): bump ajv from 6.10.1 to 6.10.2

There are 42 commits in total.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of webpack is breaking the build 🚨

Version 4.17.3 of webpack was just published.

Branch Build failing 🚨
Dependency webpack
Current Version 4.17.2
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

webpack is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).
  • coverage/coveralls: First build on greenkeeper/webpack-4.17.3 at 100.0% (Details).

Release Notes v4.17.3

Bugfixes

  • Fix exit code when multiple CLIs are installed
  • No longer recommend installing webpack-command, but still support it when installed
Commits

The new version differs by 7 commits.

  • ee27d36 4.17.3
  • 4430524 Merge pull request #7966 from webpack/refactor-remove-webpack-command-from-clis
  • b717aad Show only webpack-cli in the list
  • c5eab67 Merge pull request #8001 from webpack/bugfix/exit-code
  • 943aa6b Fix exit code when multiple CLIs are installed
  • 691cc94 Spelling
  • 898462d refactor: remove webpack-command from CLIs

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

RUSTSEC-2022-0054: wee_alloc is Unmaintained

wee_alloc is Unmaintained

Details
Status unmaintained
Package wee_alloc
Version 0.4.5
URL rustwasm/wee_alloc#107
Date 2022-05-11

Two of the maintainers have indicated that the crate may not be maintained.

The crate has open issues including memory leaks and may not be suitable for production use.

It may be best to switch to the default Rust standard allocator on wasm32 targets.

Last release seems to have been three years ago.

Possible Alternative(s)

The below list has not been vetted in any way and may or may not contain alternatives;

Honorable Mention(s)

The below may serve to educate on potential future alternatives:

See advisory page for additional details.

An in-range update of @types/jest is breaking the build 🚨

The devDependency @types/jest was updated from 24.0.19 to 24.0.20.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

@types/jest is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push: The Travis CI build failed (Details).

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.