convert code to type aliases
This commit is contained in:
parent
04024a2a8d
commit
60ce6212b4
3
.gitignore
vendored
3
.gitignore
vendored
@ -129,6 +129,9 @@ dist
|
||||
# Stores VSCode versions used for testing VSCode extensions
|
||||
.vscode-test
|
||||
|
||||
# Webstiorm
|
||||
.idea
|
||||
|
||||
# yarn v2
|
||||
.yarn/cache
|
||||
.yarn/unplugged
|
||||
|
@ -1,3 +1,6 @@
|
||||
import * as Complex from './native.js'
|
||||
import * as complex from './arithmetic.js'
|
||||
|
||||
export { complex }
|
||||
|
||||
export {Complex}
|
||||
|
@ -1,49 +1,54 @@
|
||||
import { Complex, complex_binary } from './type.js'
|
||||
import {Complex, complex_binary, FnComplexUnary} from './type.js'
|
||||
import type {
|
||||
FnAbsSquare,
|
||||
FnAdd,
|
||||
FnAddReal,
|
||||
FnConj, FnConservativeSqrt, FnDivide,
|
||||
FnDivideByReal, FnIsReal, FnIsSquare,
|
||||
FnMultiply, FnNaN, FnRe, FnReciprocal, FnSqrt,
|
||||
FnSubtract,
|
||||
FnUnaryMinus, FnZero
|
||||
} from '../interfaces/arithmetic'
|
||||
|
||||
export const add =
|
||||
<T>(dep: {
|
||||
add: (a: T, b: T) => T
|
||||
}) =>
|
||||
(w: Complex<T>, z: Complex<T>): Complex<T> =>
|
||||
complex_binary(dep.add(w.re, z.re), dep.add(w.im, z.im))
|
||||
add: FnAdd<T>
|
||||
}): FnAdd<Complex<T>> =>
|
||||
(w, z) => complex_binary(dep.add(w.re, z.re), dep.add(w.im, z.im))
|
||||
|
||||
export const addReal =
|
||||
<T>(dep: {
|
||||
addReal: (a: T, b: T) => T
|
||||
}) =>
|
||||
(z: Complex<T>, r: T): Complex<T> =>
|
||||
complex_binary(dep.addReal(z.re, r), z.im)
|
||||
addReal: FnAddReal<T, T>
|
||||
}): FnAddReal<Complex<T>, T> =>
|
||||
(z, r) => complex_binary(dep.addReal(z.re, r), z.im)
|
||||
|
||||
export const unaryMinus =
|
||||
<T>(dep: {
|
||||
unaryMinus: (z: T) => T
|
||||
}) =>
|
||||
(z: Complex<T>): Complex<T> =>
|
||||
complex_binary(dep.unaryMinus(z.re), dep.unaryMinus(z.im))
|
||||
unaryMinus: FnUnaryMinus<T>
|
||||
}): FnUnaryMinus<Complex<T>> =>
|
||||
(z) => complex_binary(dep.unaryMinus(z.re), dep.unaryMinus(z.im))
|
||||
|
||||
export const conj =
|
||||
<T>(dep: {
|
||||
unaryMinus: (z: T) => T,
|
||||
conj: (z: T) => T
|
||||
}) =>
|
||||
(z: Complex<T>): Complex<T> =>
|
||||
complex_binary(dep.conj(z.re), dep.unaryMinus(z.im))
|
||||
unaryMinus: FnUnaryMinus<T>,
|
||||
conj: FnConj<T>
|
||||
}) : FnConj<Complex<T>> =>
|
||||
(z) => complex_binary(dep.conj(z.re), dep.unaryMinus(z.im))
|
||||
|
||||
export const subtract =
|
||||
<T>(dep: {
|
||||
subtract: (a: T, b: T) => T
|
||||
}) =>
|
||||
(w: Complex<T>, z: Complex<T>): Complex<T> =>
|
||||
complex_binary(dep.subtract(w.re, z.re), dep.subtract(w.im, z.im))
|
||||
subtract: FnSubtract<T>
|
||||
}): FnSubtract<Complex<T>> =>
|
||||
(w, z) => complex_binary(dep.subtract(w.re, z.re), dep.subtract(w.im, z.im))
|
||||
|
||||
export const multiply =
|
||||
<T>(dep: {
|
||||
add: (a: T, b: T) => T,
|
||||
subtract: (a: T, b: T) => T,
|
||||
multiply: (a: T, b: T) => T,
|
||||
conj: (z: T) => T
|
||||
add: FnAdd<T>,
|
||||
subtract: FnSubtract<T>,
|
||||
multiply: FnMultiply<T>,
|
||||
conj: FnConj<T>
|
||||
}) =>
|
||||
(w: Complex<T>, z: Complex<T>): Complex<T> => {
|
||||
(w, z) => {
|
||||
const mult = dep.multiply
|
||||
const realpart = dep.subtract(mult(w.re, z.re), mult(dep.conj(w.im), z.im))
|
||||
const imagpart = dep.add(mult(dep.conj(w.re), z.im), mult(w.im, z.re))
|
||||
@ -52,44 +57,42 @@ export const multiply =
|
||||
|
||||
export const absquare =
|
||||
<T>(dep: {
|
||||
add: (a: T, b: T) => T,
|
||||
absquare: (z: T) => T
|
||||
}) =>
|
||||
(z: Complex<T>): T => dep.add(dep.absquare(z.re), dep.absquare(z.im))
|
||||
add: FnAdd<T>,
|
||||
absquare: FnAbsSquare<T, T>
|
||||
}): FnAbsSquare<Complex<T>, T> =>
|
||||
(z) => dep.add(dep.absquare(z.re), dep.absquare(z.im))
|
||||
|
||||
export const divideByReal =
|
||||
<T>(dep: {
|
||||
divideByReal: (a: T, b: T) => T
|
||||
}) =>
|
||||
(z: Complex<T>, r: T) =>
|
||||
complex_binary(dep.divideByReal(z.re, r), dep.divideByReal(z.im, r))
|
||||
divideByReal: FnDivideByReal<T, T>
|
||||
}): FnDivideByReal<Complex<T>, T> =>
|
||||
(z, r) => complex_binary(dep.divideByReal(z.re, r), dep.divideByReal(z.im, r))
|
||||
|
||||
export const reciprocal =
|
||||
<T>(dep: {
|
||||
conj: (z: Complex<T>) => Complex<T>,
|
||||
absquare: (z: Complex<T>) => T,
|
||||
divideByReal: (a: Complex<T>, b: T) => Complex<T>,
|
||||
zero: (z: T) => T,
|
||||
}) =>
|
||||
(z: Complex<T>): Complex<T> => dep.divideByReal(dep.conj(z), dep.absquare(z))
|
||||
conj: FnConj<Complex<T>>,
|
||||
absquare: FnAbsSquare<Complex<T>, T>,
|
||||
divideByReal: FnDivideByReal<Complex<T>, T>
|
||||
}): FnReciprocal<Complex<T>> =>
|
||||
(z) => dep.divideByReal(dep.conj(z), dep.absquare(z))
|
||||
|
||||
export const divide =
|
||||
<T>(dep: {
|
||||
multiply: (a: Complex<T>, b: Complex<T>) => Complex<T>,
|
||||
reciprocal: (z: Complex<T>) => Complex<T>,
|
||||
}) =>
|
||||
(w: Complex<T>, z: Complex<T>) => dep.multiply(w, dep.reciprocal(z))
|
||||
multiply: FnMultiply<Complex<T>>,
|
||||
reciprocal: FnReciprocal<Complex<T>>,
|
||||
}): FnDivide<Complex<T>> =>
|
||||
(w, z) => dep.multiply(w, dep.reciprocal(z))
|
||||
|
||||
export const complexSqrt =
|
||||
<T>(dep: {
|
||||
conservativeSqrt: (a: T) => T,
|
||||
isSquare: (a: T) => boolean,
|
||||
complex: (a: T) => Complex<T>,
|
||||
unaryMinus: (a: T) => T,
|
||||
zero: (a: T) => T,
|
||||
nan: (a: Complex<T>) => Complex<T>
|
||||
conservativeSqrt: FnConservativeSqrt<T>,
|
||||
isSquare: FnIsSquare<T>,
|
||||
complex: FnComplexUnary<T>,
|
||||
unaryMinus: FnUnaryMinus<T>,
|
||||
zero: FnZero<T>,
|
||||
nan: FnNaN<Complex<T>>
|
||||
}) =>
|
||||
(r: T): Complex<T> => {
|
||||
(r) => {
|
||||
if (dep.isSquare(r)) return dep.complex(dep.conservativeSqrt(r))
|
||||
const negative = dep.unaryMinus(r)
|
||||
if (dep.isSquare(negative)) {
|
||||
@ -104,17 +107,16 @@ export const complexSqrt =
|
||||
|
||||
export const sqrt =
|
||||
<T>(dep: {
|
||||
isReal: (z: Complex<T>) => boolean,
|
||||
complexSqrt: (a: T) => Complex<T>,
|
||||
conservativeSqrt: (a: T) => T,
|
||||
absquare: (a: Complex<T>) => T,
|
||||
addReal: (a: Complex<T>, b: T) => Complex<T>,
|
||||
divideByReal: (a: Complex<T>, b: T) => Complex<T>,
|
||||
add: (a: T, b: T) => T,
|
||||
re: (a: Complex<T>) => T,
|
||||
|
||||
isReal: FnIsReal<Complex<T>>,
|
||||
complexSqrt: FnSqrt<T>,
|
||||
conservativeSqrt: FnConservativeSqrt<T>,
|
||||
absquare: FnAbsSquare<Complex<T>, T>,
|
||||
addReal: FnAddReal<Complex<T>, T>,
|
||||
divideByReal: FnDivideByReal<Complex<T>, T>,
|
||||
add: FnAdd<T>,
|
||||
re: FnRe<Complex<T>, T>,
|
||||
}) =>
|
||||
(z: Complex<T>) => {
|
||||
(z: Complex<T>): Complex<T> | T => {
|
||||
if (dep.isReal(z)) return dep.complexSqrt(z.re)
|
||||
const myabs = dep.conservativeSqrt(dep.absquare(z))
|
||||
const num = dep.addReal(z, myabs)
|
||||
|
@ -1,12 +1,14 @@
|
||||
import { Complex } from './type.js'
|
||||
import {FnEqual} from '../interfaces/relational'
|
||||
import {FnAdd, FnIsReal, FnIsSquare} from '../interfaces/arithmetic'
|
||||
|
||||
export const isReal =
|
||||
<T>(dep: {
|
||||
equal: (a: T, b: T) => boolean,
|
||||
add: (a: T, b: T) => T,
|
||||
isReal: (z: T) => boolean
|
||||
}) =>
|
||||
(z: Complex<T>) => dep.isReal(z.re) && dep.equal(z.re, dep.add(z.re, z.im))
|
||||
equal: FnEqual<T>,
|
||||
add: FnAdd<T>,
|
||||
isReal: FnIsReal<T>
|
||||
}): FnIsReal<Complex<T>> =>
|
||||
(z) => dep.isReal(z.re) && dep.equal(z.re, dep.add(z.re, z.im))
|
||||
|
||||
export const isSquare =
|
||||
<T>(z: Complex<T>) => true // FIXME: not correct for Complex<bigint> once we get there
|
||||
<T>(): FnIsSquare<Complex<T>> => (z) => true // FIXME: not correct for Complex<bigint> once we get there
|
||||
|
@ -1,7 +1,8 @@
|
||||
import { Complex } from './type.js'
|
||||
import {FnEqual} from '../interfaces/relational'
|
||||
|
||||
export const equal =
|
||||
<T>(dep: {
|
||||
equal: (a: T, b: T) => boolean
|
||||
}) =>
|
||||
(w: Complex<T>, z: Complex<T>): boolean => dep.equal(w.re, z.re) && dep.equal(w.im, z.im)
|
||||
equal: FnEqual<T>
|
||||
}): FnEqual<Complex<T>> =>
|
||||
(w, z) => dep.equal(w.re, z.re) && dep.equal(w.im, z.im)
|
||||
|
@ -1,6 +1,7 @@
|
||||
import {
|
||||
joinTypes, typeOfDependency, Dependency,
|
||||
} from '../core/Dispatcher.js'
|
||||
import type {FnNaN, FnOne, FnRe, FnZero} from '../interfaces/arithmetic.js'
|
||||
|
||||
export type Complex<T> = { re: T; im: T; }
|
||||
|
||||
@ -8,7 +9,7 @@ export const Complex_type = {
|
||||
test: <T>(dep: { testT: (z: unknown) => z is T }) =>
|
||||
(z: unknown): z is Complex<T> =>
|
||||
typeof z === 'object' && z != null && 're' in z && 'im' in z
|
||||
&& dep.testT(z.re) && dep.testT(z.im),
|
||||
&& dep.testT(z['re']) && dep.testT(z['im']),
|
||||
infer: (dep: typeOfDependency) =>
|
||||
(z: Complex<unknown>) => joinTypes(dep.typeOf(z.re), dep.typeOf(z.im)),
|
||||
from: {
|
||||
@ -19,36 +20,39 @@ export const Complex_type = {
|
||||
}
|
||||
}
|
||||
|
||||
export type FnComplexUnary<T> = (t: T) => Complex<T>
|
||||
|
||||
export const complex_unary =
|
||||
<T>(dep: {
|
||||
zero: (z: T) => Complex<T>
|
||||
}) =>
|
||||
(t: T) => ({ re: t, im: dep.zero(t) })
|
||||
zero: FnZero<T>
|
||||
}): FnComplexUnary<T> =>
|
||||
(t) => ({ re: t, im: dep.zero(t) })
|
||||
|
||||
export const complex_binary =
|
||||
<T>(t: T, u: T): Complex<T> => ({ re: t, im: u })
|
||||
export type FnComplexBinary<T> = (re: T, im: T) => Complex<T>
|
||||
|
||||
export const complex_binary = <T>(t: T, u: T): Complex<T> => ({ re: t, im: u })
|
||||
|
||||
export const zero =
|
||||
<T>(dep: {
|
||||
zero: (z: T) => T
|
||||
}) =>
|
||||
(z: Complex<T>): Complex<T> => complex_binary(dep.zero(z.re), dep.zero(z.im))
|
||||
zero: FnZero<T>
|
||||
}): FnZero<Complex<T>> =>
|
||||
(z) => complex_binary(dep.zero(z.re), dep.zero(z.im))
|
||||
|
||||
export const one =
|
||||
<T>(dep: {
|
||||
zero: (z: T) => T,
|
||||
one: (z: T) => T
|
||||
}) =>
|
||||
(z: Complex<T>): Complex<T> => complex_binary(dep.one(z.re), dep.zero(z.im))
|
||||
zero: FnZero<T>,
|
||||
one: FnOne<T>
|
||||
}): FnOne<Complex<T>> =>
|
||||
(z) => complex_binary(dep.one(z.re), dep.zero(z.im))
|
||||
|
||||
export const nan =
|
||||
<T>(dep: {
|
||||
nan: (z: T) => T
|
||||
}) =>
|
||||
(z: Complex<T>): Complex<T> => complex_binary(dep.nan(z.re), dep.nan(z.im))
|
||||
nan: FnNaN<T>
|
||||
}): FnNaN<Complex<T>> =>
|
||||
(z) => complex_binary(dep.nan(z.re), dep.nan(z.im))
|
||||
|
||||
export const re =
|
||||
<T>(dep: {
|
||||
re: (z: T) => T
|
||||
}) =>
|
||||
(z: Complex<T>): T => dep.re(z.re)
|
||||
re: FnRe<T,T>
|
||||
}): FnRe<Complex<T>, T> =>
|
||||
(z) => dep.re(z.re)
|
||||
|
@ -1,5 +1,7 @@
|
||||
import type { FnMultiply, FnSquare } from "../interfaces/arithmetic"
|
||||
|
||||
export const square =
|
||||
<T>(dep: {
|
||||
multiply: (x: T, y: T) => T
|
||||
}) =>
|
||||
(z: T): T => dep.multiply(z, z)
|
||||
multiply: FnMultiply<T>
|
||||
}): FnSquare<T> =>
|
||||
(z) => dep.multiply(z, z)
|
||||
|
28
src/interfaces/arithmetic.ts
Normal file
28
src/interfaces/arithmetic.ts
Normal file
@ -0,0 +1,28 @@
|
||||
// shared interfaces
|
||||
|
||||
import { Complex } from "../Complex/type"
|
||||
|
||||
// Note: right now I've added an 'Fn*' prefix,
|
||||
// so it is clear that the type hold a function type definition
|
||||
// This is not necessary though, it is just a naming convention.
|
||||
export type FnAdd<T> = (a: T, b: T) => T
|
||||
export type FnAddReal<T, U> = (a: T, b: U) => T
|
||||
export type FnUnaryMinus<T> = (a: T) => T
|
||||
export type FnConj<T> = (a: T) => T
|
||||
export type FnSubtract<T> = (a: T, b: T) => T
|
||||
export type FnMultiply<T> = (a: T, b: T) => T
|
||||
export type FnAbsSquare<T, U> = (a: T) => U
|
||||
export type FnReciprocal<T> = (a: T) => T
|
||||
export type FnDivide<T> = (a: T, b: T) => T
|
||||
export type FnDivideByReal<T, U> = (a: T, b: U) => T
|
||||
export type FnConservativeSqrt<T> = (a: T) => T
|
||||
export type FnSqrt<T> = (a: T) => T | Complex<T>
|
||||
export type FnSquare<T> = (z: T) => T
|
||||
|
||||
export type FnIsReal<T> = (a: T) => boolean
|
||||
export type FnIsSquare<T> = (a: T) => boolean
|
||||
|
||||
export type FnZero<T> = (a: T) => T
|
||||
export type FnOne<T> = (a: T) => T
|
||||
export type FnNaN<T> = (a: T) => T
|
||||
export type FnRe<T, U> = (a: T) => U
|
3
src/interfaces/relational.ts
Normal file
3
src/interfaces/relational.ts
Normal file
@ -0,0 +1,3 @@
|
||||
|
||||
export type FnEqual<T> = (a: T, b: T) => boolean
|
||||
export type FnUnequal<T> = (a: T, b: T) => boolean
|
@ -1,24 +1,25 @@
|
||||
import { Config } from '../core/Config.js'
|
||||
import type { Complex } from '../Complex/type.js'
|
||||
import type { FnComplexBinary } from '../Complex/type.js'
|
||||
import { FnAdd, FnConj, FnSubtract, FnUnaryMinus, FnMultiply, FnAbsSquare, FnReciprocal, FnDivide, FnConservativeSqrt, FnSqrt } from '../interfaces/arithmetic.js'
|
||||
|
||||
export const add = (a: number, b: number): number => a + b
|
||||
export const add: FnAdd<number> = (a, b) => a + b
|
||||
export const addReal = add
|
||||
export const unaryMinus = (a: number): number => -a
|
||||
export const conj = (a: number): number => a
|
||||
export const subtract = (a: number, b: number): number => a - b
|
||||
export const multiply = (a: number, b: number): number => a * b
|
||||
export const absquare = (a: number): number => a * a
|
||||
export const reciprocal = (a: number): number => 1 / a
|
||||
export const divide = (a: number, b: number): number => a / b
|
||||
export const unaryMinus: FnUnaryMinus<number> = (a) => -a
|
||||
export const conj: FnConj<number> = (a) => a
|
||||
export const subtract: FnSubtract<number> = (a, b) => a - b
|
||||
export const multiply: FnMultiply<number> = (a, b) => a * b
|
||||
export const absquare: FnAbsSquare<number, number> = (a) => a * a
|
||||
export const reciprocal: FnReciprocal<number> = (a) => 1 / a
|
||||
export const divide: FnDivide<number> = (a, b) => a / b
|
||||
export const divideByReal = divide
|
||||
|
||||
export const conservativeSqrt = (a: number): number => isNaN(a) ? NaN : Math.sqrt(a)
|
||||
export const conservativeSqrt: FnConservativeSqrt<number> = (a) => isNaN(a) ? NaN : Math.sqrt(a)
|
||||
|
||||
export const sqrt =
|
||||
(dep: {
|
||||
config: Config,
|
||||
complex: (re: number, im: number) => Complex<number>
|
||||
}): (a: number) => number | Complex<number> => {
|
||||
complex: FnComplexBinary<number>
|
||||
}): FnSqrt<number> => {
|
||||
if (dep.config.predictable || !dep.complex) return conservativeSqrt
|
||||
return a => {
|
||||
if (isNaN(a)) return NaN
|
||||
|
@ -1,2 +1,4 @@
|
||||
export const isReal = (a: number) : boolean => true
|
||||
export const isSquare = (a: number) : boolean => a >= 0
|
||||
import type { FnIsReal, FnIsSquare } from "../interfaces/arithmetic"
|
||||
|
||||
export const isReal: FnIsReal<number> = (a) => true
|
||||
export const isSquare: FnIsSquare<number> = (a) => a >= 0
|
||||
|
@ -1,11 +1,12 @@
|
||||
import { Config } from '../core/Config.js'
|
||||
import type { FnEqual, FnUnequal } from '../interfaces/relational.js'
|
||||
|
||||
const DBL_EPSILON = Number.EPSILON || 2.2204460492503130808472633361816E-16
|
||||
|
||||
export const equal =
|
||||
(dep: {
|
||||
config: Config
|
||||
}) => (x: number, y: number): boolean => {
|
||||
}): FnEqual<number> => (x, y) => {
|
||||
const eps = dep.config.epsilon
|
||||
if (eps === null || eps === undefined) return x === y
|
||||
if (x === y) return true
|
||||
@ -21,6 +22,6 @@ export const equal =
|
||||
}
|
||||
|
||||
export const unequal = (dep: {
|
||||
equal: (x: number, y: number) => boolean
|
||||
}) =>
|
||||
(x: number, y: number): boolean => !dep.equal(x, y)
|
||||
equal: FnEqual<number>
|
||||
}): FnUnequal<number> =>
|
||||
(x, y) => !dep.equal(x, y)
|
||||
|
@ -1,10 +1,12 @@
|
||||
import type { FnNaN, FnOne, FnZero, FnRe } from "../interfaces/arithmetic"
|
||||
|
||||
export const number_type = {
|
||||
before: ['Complex'],
|
||||
test: (n: unknown): n is number => typeof n === 'number',
|
||||
from: { string: (s: string) => +s }
|
||||
}
|
||||
|
||||
export const zero = (a: number): number => 0
|
||||
export const one = (a: number): number => 1
|
||||
export const nan = (a: number): number => NaN
|
||||
export const re = (a: number): number => a
|
||||
export const zero: FnZero<number> = (a) => 0
|
||||
export const one: FnOne<number> = (a) => 1
|
||||
export const nan: FnNaN<number> = (a) => NaN
|
||||
export const re: FnRe<number, number> = (a) => a
|
||||
|
Loading…
Reference in New Issue
Block a user