Skip to content

Commit

Permalink
feat(complex): Complex 2.0 (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
babiabeo authored Jan 11, 2025
1 parent 4b23a12 commit ce80181
Show file tree
Hide file tree
Showing 45 changed files with 1,338 additions and 1,558 deletions.
18 changes: 9 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
os:
- ubuntu-22.04
- windows-2022
- macOS-12
- macOS-14

steps:
- name: Setup repo
Expand All @@ -32,7 +32,7 @@ jobs:
bun-version: ${{ matrix.bun }}

- name: Install packages
run: bunx jsr add @cross/test @std/assert@^0.226.0
run: bunx jsr add @cross/test @std/assert

- name: Run tests canary
run: bun test
Expand All @@ -47,19 +47,19 @@ jobs:
os:
- ubuntu-22.04
- windows-2022
- macOS-12
- macOS-14

steps:
- name: Setup repo
uses: actions/checkout@v4

- name: Setup Deno
uses: denoland/setup-deno@v1
uses: denoland/setup-deno@v2
with:
deno-version: ${{ matrix.deno }}

- name: Install packages
run: deno add @cross/test @std/assert@^0.226.0
run: deno add jsr:@cross/test jsr:@std/assert

- name: Run tests
run: deno task test:deno
Expand All @@ -70,11 +70,11 @@ jobs:
strategy:
fail-fast: false
matrix:
node: [21.x]
node: [22.x]
os:
- ubuntu-22.04
- windows-2022
- macOS-12
- macOS-14

steps:
- name: Setup repo
Expand All @@ -101,9 +101,9 @@ jobs:
uses: actions/checkout@v4

- name: Setup Deno
uses: denoland/setup-deno@v1
uses: denoland/setup-deno@v2
with:
deno-version: canary

- name: Check linting
run: deno task lint
run: deno task lint
9 changes: 6 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
.vscode
deno.lock
docs
package.json

# lock files
package.json
package-lock.json
deno.lock
bun.lockb
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2024 David (babiabeo)
Copyright (c) 2025 David (babiabeo)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -18,4 +18,4 @@ 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.
SOFTWARE.
92 changes: 41 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
# complex
# `@babia/complex`

[![JSR](https://jsr.io/badges/@babia/complex)](https://jsr.io/@babia/complex)
[![CI](https://github.com/babiabeo/complex/actions/workflows/ci.yml/badge.svg)](https://github.com/babiabeo/complex/actions/workflows/ci.yml)

A package provides implementation of complex numbers and mathematical functions
for complex numbers.
A package providing the implementation of complex numbers and mathematical
functions.

#### Install

```sh
# deno
deno add jsr:@babia/complex

# node.js
npx jsr add @babia/complex
yarn dlx jsr add @babia/complex
pnpm dlx jsr add @babia/complex

# bun
bunx jsr add @babia/complex
```

## What is a complex number?

A complex number is an extension of the real numbers. It combines both real and
imaginary components. It can be expressed in form `a + bi`, where:
A complex number is an extension of the real number. It combines both real and
imaginary components. It can be expressed in the standard form `a + bi`, where:

- `a`: the real part
- `b`: the imaginary part
Expand All @@ -19,82 +34,57 @@ A real number can be regarded as a complex number `a + 0i`, whose the imaginary
part is `0`. A purely imaginary number is a complex number `0 + bi`, whose the
real part is `0`.

## `Complex` class
## `complex` class

This package provides an implementation of complex numbers through the `Complex`
This package provides an implementation of complex number through the `complex`
class. It has methods to perform basic operations on complex numbers:

```ts
const cmplx1 = new Complex(3, 1); // 3 + i
const cmplx2 = new Complex(2, 9); // 2 + 9i
const cmplx1 = new complex(3, 1); // 3 + i
const cmplx2 = new complex(2, 9); // 2 + 9i

cmplx1.add(cmplx2); // (3 + i) + (2 + 9i) = (5 + 10i)

// Also works with real numbers
cmplx1.add(3); // (3 + i) + 3 = (3 + i) + (3 + 0i) = (6 + i)
cmplx1.add(3); // (3 + i) + 3 = (6 + i)
```

Methods `conj()`, `abs()`, `phase()` return the conjugate, absolute value, and
argument of the complex number respectively:
Methods `abs()`, `conj()`, `phase()` return the absolute value, the conjugate,
and the argument of the complex number respectively:

```ts
cmplx2.abs(); // 9.2195445
cmplx2.conj(); // (2 - 9i)
cmplx2.abs(); // ≈ 9.219544457292889 = Math.sqrt(85)
cmplx2.phase(); // 1.3521273809209546
cmplx2.phase(); // 1.3521274
```

## `cmplx` function

For convenience, the `cmplx` function was added in `v1.1.0` to help create
complex numbers more easily.
The `cmplx` function was added in `v1.1.0` to simplify the creation of complex
numbers.

```ts
cmplx(2, 3); // 2 + 3i
```

Unlike the `Complex` class, the `cmplx` function requires the first argument
(the real part).
Unlike `complex` class, the `cmplx` function requires the first argument (the
real part).

```ts
cmplx(0); // 0 + 0i
```

## `ComplexMath`
## `cmath`

Like [`Math`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math), `ComplexMath` provides basic mathematics functionality for
complex numbers.
Besides `complex`, there is also `cmath` which is a collection of mathematical
functions for complex numbers:

- Exponential and logarithm functions
- Power and logarithm functions: `exp`, `log`, `log10`, `sqrt`
- Hyperbolic functions: `sinh`, `cosh`, `tanh`, `asinh`, `acosh`, `atanh`
- Trigonometric functions: `sin`, `cos`, `tan`, `asin`, `acos`, `atan`

```ts
ComplexMath.exp();
ComplexMath.log();
// ...
```

- Hyperbolic functions:

```ts
ComplexMath.sinh();
ComplexMath.atanh();
// ...
```

- Power functions:

```ts
ComplexMath.sqrt();
ComplexMath.pow();
// ...
```

- Trigonometric functions:

```ts
ComplexMath.asin();
ComplexMath.tan();
// ...
```
> [!NOTE]
> Do not confuse with `<cmath>` header in C++
## Related

Expand Down
16 changes: 10 additions & 6 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,30 @@
"name": "@babia/complex",
"version": "1.1.1",
"exports": {
".": "./mod.ts",
".": "./src/mod.ts",
"./math": "./src/math/mod.ts"
},
"fmt": {
"indentWidth": 4
},
"imports": {
"@cross/test": "jsr:@cross/test@^0.0.9",
"@std/assert": "jsr:@std/assert@^0.225.3",
"@cross/test": "jsr:@cross/test@^0.0.10",
"@std/assert": "jsr:@std/assert@^1.0.10",
"@babia/complex": "./mod.ts"
},
"exclude": [".vscode", "docs", ".github", "README.md", "LICENSE"],
"exclude": [
"docs",
".github",
"LICENSE"
],
"tasks": {
"check": "deno publish --dry-run --allow-dirty",
"lint": "deno fmt --check && deno lint",
"test:bun": "bun test --bail",
"test:deno": "deno test --trace-leaks --doc --parallel --clean",
"test:deno": "deno test --trace-leaks --parallel --clean src",
"test:node": "echo '{ \"type\": \"module\" }' > package.json && npx --yes tsx --test",
"test": "deno task test:bun && deno task test:deno && deno task test:node",
"ok": "deno task lint && deno task test && deno task check ",
"doc:view": "deno doc --html --name=\"@babia/complex\" ./mod.ts"
"doc:view": "deno doc --html --name=\"@babia/complex\" ./src/mod.ts"
}
}
55 changes: 0 additions & 55 deletions mod.ts

This file was deleted.

Loading

0 comments on commit ce80181

Please sign in to comment.