convert code to type aliases

This commit is contained in:
Jos de Jong 2022-12-23 13:52:56 +01:00
parent 04024a2a8d
commit 60ce6212b4
13 changed files with 169 additions and 115 deletions

3
.gitignore vendored
View File

@ -129,6 +129,9 @@ dist
# Stores VSCode versions used for testing VSCode extensions # Stores VSCode versions used for testing VSCode extensions
.vscode-test .vscode-test
# Webstiorm
.idea
# yarn v2 # yarn v2
.yarn/cache .yarn/cache
.yarn/unplugged .yarn/unplugged

View File

@ -1,3 +1,6 @@
import * as Complex from './native.js' import * as Complex from './native.js'
import * as complex from './arithmetic.js'
export { complex }
export {Complex} export {Complex}

View File

@ -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 = export const add =
<T>(dep: { <T>(dep: {
add: (a: T, b: T) => T add: FnAdd<T>
}) => }): FnAdd<Complex<T>> =>
(w: Complex<T>, z: Complex<T>): Complex<T> => (w, z) => complex_binary(dep.add(w.re, z.re), dep.add(w.im, z.im))
complex_binary(dep.add(w.re, z.re), dep.add(w.im, z.im))
export const addReal = export const addReal =
<T>(dep: { <T>(dep: {
addReal: (a: T, b: T) => T addReal: FnAddReal<T, T>
}) => }): FnAddReal<Complex<T>, T> =>
(z: Complex<T>, r: T): Complex<T> => (z, r) => complex_binary(dep.addReal(z.re, r), z.im)
complex_binary(dep.addReal(z.re, r), z.im)
export const unaryMinus = export const unaryMinus =
<T>(dep: { <T>(dep: {
unaryMinus: (z: T) => T unaryMinus: FnUnaryMinus<T>
}) => }): FnUnaryMinus<Complex<T>> =>
(z: Complex<T>): Complex<T> => (z) => complex_binary(dep.unaryMinus(z.re), dep.unaryMinus(z.im))
complex_binary(dep.unaryMinus(z.re), dep.unaryMinus(z.im))
export const conj = export const conj =
<T>(dep: { <T>(dep: {
unaryMinus: (z: T) => T, unaryMinus: FnUnaryMinus<T>,
conj: (z: T) => T conj: FnConj<T>
}) => }) : FnConj<Complex<T>> =>
(z: Complex<T>): Complex<T> => (z) => complex_binary(dep.conj(z.re), dep.unaryMinus(z.im))
complex_binary(dep.conj(z.re), dep.unaryMinus(z.im))
export const subtract = export const subtract =
<T>(dep: { <T>(dep: {
subtract: (a: T, b: T) => T subtract: FnSubtract<T>
}) => }): FnSubtract<Complex<T>> =>
(w: Complex<T>, z: Complex<T>): Complex<T> => (w, z) => complex_binary(dep.subtract(w.re, z.re), dep.subtract(w.im, z.im))
complex_binary(dep.subtract(w.re, z.re), dep.subtract(w.im, z.im))
export const multiply = export const multiply =
<T>(dep: { <T>(dep: {
add: (a: T, b: T) => T, add: FnAdd<T>,
subtract: (a: T, b: T) => T, subtract: FnSubtract<T>,
multiply: (a: T, b: T) => T, multiply: FnMultiply<T>,
conj: (z: T) => T conj: FnConj<T>
}) => }) =>
(w: Complex<T>, z: Complex<T>): Complex<T> => { (w, z) => {
const mult = dep.multiply const mult = dep.multiply
const realpart = dep.subtract(mult(w.re, z.re), mult(dep.conj(w.im), z.im)) 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)) 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 = export const absquare =
<T>(dep: { <T>(dep: {
add: (a: T, b: T) => T, add: FnAdd<T>,
absquare: (z: T) => T absquare: FnAbsSquare<T, T>
}) => }): FnAbsSquare<Complex<T>, T> =>
(z: Complex<T>): T => dep.add(dep.absquare(z.re), dep.absquare(z.im)) (z) => dep.add(dep.absquare(z.re), dep.absquare(z.im))
export const divideByReal = export const divideByReal =
<T>(dep: { <T>(dep: {
divideByReal: (a: T, b: T) => T divideByReal: FnDivideByReal<T, T>
}) => }): FnDivideByReal<Complex<T>, T> =>
(z: Complex<T>, r: T) => (z, r) => complex_binary(dep.divideByReal(z.re, r), dep.divideByReal(z.im, r))
complex_binary(dep.divideByReal(z.re, r), dep.divideByReal(z.im, r))
export const reciprocal = export const reciprocal =
<T>(dep: { <T>(dep: {
conj: (z: Complex<T>) => Complex<T>, conj: FnConj<Complex<T>>,
absquare: (z: Complex<T>) => T, absquare: FnAbsSquare<Complex<T>, T>,
divideByReal: (a: Complex<T>, b: T) => Complex<T>, divideByReal: FnDivideByReal<Complex<T>, T>
zero: (z: T) => T, }): FnReciprocal<Complex<T>> =>
}) => (z) => dep.divideByReal(dep.conj(z), dep.absquare(z))
(z: Complex<T>): Complex<T> => dep.divideByReal(dep.conj(z), dep.absquare(z))
export const divide = export const divide =
<T>(dep: { <T>(dep: {
multiply: (a: Complex<T>, b: Complex<T>) => Complex<T>, multiply: FnMultiply<Complex<T>>,
reciprocal: (z: Complex<T>) => Complex<T>, reciprocal: FnReciprocal<Complex<T>>,
}) => }): FnDivide<Complex<T>> =>
(w: Complex<T>, z: Complex<T>) => dep.multiply(w, dep.reciprocal(z)) (w, z) => dep.multiply(w, dep.reciprocal(z))
export const complexSqrt = export const complexSqrt =
<T>(dep: { <T>(dep: {
conservativeSqrt: (a: T) => T, conservativeSqrt: FnConservativeSqrt<T>,
isSquare: (a: T) => boolean, isSquare: FnIsSquare<T>,
complex: (a: T) => Complex<T>, complex: FnComplexUnary<T>,
unaryMinus: (a: T) => T, unaryMinus: FnUnaryMinus<T>,
zero: (a: T) => T, zero: FnZero<T>,
nan: (a: Complex<T>) => Complex<T> nan: FnNaN<Complex<T>>
}) => }) =>
(r: T): Complex<T> => { (r) => {
if (dep.isSquare(r)) return dep.complex(dep.conservativeSqrt(r)) if (dep.isSquare(r)) return dep.complex(dep.conservativeSqrt(r))
const negative = dep.unaryMinus(r) const negative = dep.unaryMinus(r)
if (dep.isSquare(negative)) { if (dep.isSquare(negative)) {
@ -104,17 +107,16 @@ export const complexSqrt =
export const sqrt = export const sqrt =
<T>(dep: { <T>(dep: {
isReal: (z: Complex<T>) => boolean, isReal: FnIsReal<Complex<T>>,
complexSqrt: (a: T) => Complex<T>, complexSqrt: FnSqrt<T>,
conservativeSqrt: (a: T) => T, conservativeSqrt: FnConservativeSqrt<T>,
absquare: (a: Complex<T>) => T, absquare: FnAbsSquare<Complex<T>, T>,
addReal: (a: Complex<T>, b: T) => Complex<T>, addReal: FnAddReal<Complex<T>, T>,
divideByReal: (a: Complex<T>, b: T) => Complex<T>, divideByReal: FnDivideByReal<Complex<T>, T>,
add: (a: T, b: T) => T, add: FnAdd<T>,
re: (a: Complex<T>) => T, re: FnRe<Complex<T>, T>,
}) => }) =>
(z: Complex<T>) => { (z: Complex<T>): Complex<T> | T => {
if (dep.isReal(z)) return dep.complexSqrt(z.re) if (dep.isReal(z)) return dep.complexSqrt(z.re)
const myabs = dep.conservativeSqrt(dep.absquare(z)) const myabs = dep.conservativeSqrt(dep.absquare(z))
const num = dep.addReal(z, myabs) const num = dep.addReal(z, myabs)

View File

@ -1,12 +1,14 @@
import { Complex } from './type.js' import { Complex } from './type.js'
import {FnEqual} from '../interfaces/relational'
import {FnAdd, FnIsReal, FnIsSquare} from '../interfaces/arithmetic'
export const isReal = export const isReal =
<T>(dep: { <T>(dep: {
equal: (a: T, b: T) => boolean, equal: FnEqual<T>,
add: (a: T, b: T) => T, add: FnAdd<T>,
isReal: (z: T) => boolean isReal: FnIsReal<T>
}) => }): FnIsReal<Complex<T>> =>
(z: Complex<T>) => dep.isReal(z.re) && dep.equal(z.re, dep.add(z.re, z.im)) (z) => dep.isReal(z.re) && dep.equal(z.re, dep.add(z.re, z.im))
export const isSquare = 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

View File

@ -1,7 +1,8 @@
import { Complex } from './type.js' import { Complex } from './type.js'
import {FnEqual} from '../interfaces/relational'
export const equal = export const equal =
<T>(dep: { <T>(dep: {
equal: (a: T, b: T) => boolean equal: FnEqual<T>
}) => }): FnEqual<Complex<T>> =>
(w: Complex<T>, z: Complex<T>): boolean => dep.equal(w.re, z.re) && dep.equal(w.im, z.im) (w, z) => dep.equal(w.re, z.re) && dep.equal(w.im, z.im)

View File

@ -1,6 +1,7 @@
import { import {
joinTypes, typeOfDependency, Dependency, joinTypes, typeOfDependency, Dependency,
} from '../core/Dispatcher.js' } from '../core/Dispatcher.js'
import type {FnNaN, FnOne, FnRe, FnZero} from '../interfaces/arithmetic.js'
export type Complex<T> = { re: T; im: T; } 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 }) => test: <T>(dep: { testT: (z: unknown) => z is T }) =>
(z: unknown): z is Complex<T> => (z: unknown): z is Complex<T> =>
typeof z === 'object' && z != null && 're' in z && 'im' in z 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) => infer: (dep: typeOfDependency) =>
(z: Complex<unknown>) => joinTypes(dep.typeOf(z.re), dep.typeOf(z.im)), (z: Complex<unknown>) => joinTypes(dep.typeOf(z.re), dep.typeOf(z.im)),
from: { from: {
@ -19,36 +20,39 @@ export const Complex_type = {
} }
} }
export type FnComplexUnary<T> = (t: T) => Complex<T>
export const complex_unary = export const complex_unary =
<T>(dep: { <T>(dep: {
zero: (z: T) => Complex<T> zero: FnZero<T>
}) => }): FnComplexUnary<T> =>
(t: T) => ({ re: t, im: dep.zero(t) }) (t) => ({ re: t, im: dep.zero(t) })
export const complex_binary = export type FnComplexBinary<T> = (re: T, im: T) => Complex<T>
<T>(t: T, u: T): Complex<T> => ({ re: t, im: u })
export const complex_binary = <T>(t: T, u: T): Complex<T> => ({ re: t, im: u })
export const zero = export const zero =
<T>(dep: { <T>(dep: {
zero: (z: T) => T zero: FnZero<T>
}) => }): FnZero<Complex<T>> =>
(z: Complex<T>): Complex<T> => complex_binary(dep.zero(z.re), dep.zero(z.im)) (z) => complex_binary(dep.zero(z.re), dep.zero(z.im))
export const one = export const one =
<T>(dep: { <T>(dep: {
zero: (z: T) => T, zero: FnZero<T>,
one: (z: T) => T one: FnOne<T>
}) => }): FnOne<Complex<T>> =>
(z: Complex<T>): Complex<T> => complex_binary(dep.one(z.re), dep.zero(z.im)) (z) => complex_binary(dep.one(z.re), dep.zero(z.im))
export const nan = export const nan =
<T>(dep: { <T>(dep: {
nan: (z: T) => T nan: FnNaN<T>
}) => }): FnNaN<Complex<T>> =>
(z: Complex<T>): Complex<T> => complex_binary(dep.nan(z.re), dep.nan(z.im)) (z) => complex_binary(dep.nan(z.re), dep.nan(z.im))
export const re = export const re =
<T>(dep: { <T>(dep: {
re: (z: T) => T re: FnRe<T,T>
}) => }): FnRe<Complex<T>, T> =>
(z: Complex<T>): T => dep.re(z.re) (z) => dep.re(z.re)

View File

@ -1,5 +1,7 @@
import type { FnMultiply, FnSquare } from "../interfaces/arithmetic"
export const square = export const square =
<T>(dep: { <T>(dep: {
multiply: (x: T, y: T) => T multiply: FnMultiply<T>
}) => }): FnSquare<T> =>
(z: T): T => dep.multiply(z, z) (z) => dep.multiply(z, z)

View 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

View File

@ -0,0 +1,3 @@
export type FnEqual<T> = (a: T, b: T) => boolean
export type FnUnequal<T> = (a: T, b: T) => boolean

View File

@ -1,24 +1,25 @@
import { Config } from '../core/Config.js' 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 addReal = add
export const unaryMinus = (a: number): number => -a export const unaryMinus: FnUnaryMinus<number> = (a) => -a
export const conj = (a: number): number => a export const conj: FnConj<number> = (a) => a
export const subtract = (a: number, b: number): number => a - b export const subtract: FnSubtract<number> = (a, b) => a - b
export const multiply = (a: number, b: number): number => a * b export const multiply: FnMultiply<number> = (a, b) => a * b
export const absquare = (a: number): number => a * a export const absquare: FnAbsSquare<number, number> = (a) => a * a
export const reciprocal = (a: number): number => 1 / a export const reciprocal: FnReciprocal<number> = (a) => 1 / a
export const divide = (a: number, b: number): number => a / b export const divide: FnDivide<number> = (a, b) => a / b
export const divideByReal = divide 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 = export const sqrt =
(dep: { (dep: {
config: Config, config: Config,
complex: (re: number, im: number) => Complex<number> complex: FnComplexBinary<number>
}): (a: number) => number | Complex<number> => { }): FnSqrt<number> => {
if (dep.config.predictable || !dep.complex) return conservativeSqrt if (dep.config.predictable || !dep.complex) return conservativeSqrt
return a => { return a => {
if (isNaN(a)) return NaN if (isNaN(a)) return NaN

View File

@ -1,2 +1,4 @@
export const isReal = (a: number) : boolean => true import type { FnIsReal, FnIsSquare } from "../interfaces/arithmetic"
export const isSquare = (a: number) : boolean => a >= 0
export const isReal: FnIsReal<number> = (a) => true
export const isSquare: FnIsSquare<number> = (a) => a >= 0

View File

@ -1,11 +1,12 @@
import { Config } from '../core/Config.js' import { Config } from '../core/Config.js'
import type { FnEqual, FnUnequal } from '../interfaces/relational.js'
const DBL_EPSILON = Number.EPSILON || 2.2204460492503130808472633361816E-16 const DBL_EPSILON = Number.EPSILON || 2.2204460492503130808472633361816E-16
export const equal = export const equal =
(dep: { (dep: {
config: Config config: Config
}) => (x: number, y: number): boolean => { }): FnEqual<number> => (x, y) => {
const eps = dep.config.epsilon const eps = dep.config.epsilon
if (eps === null || eps === undefined) return x === y if (eps === null || eps === undefined) return x === y
if (x === y) return true if (x === y) return true
@ -21,6 +22,6 @@ export const equal =
} }
export const unequal = (dep: { export const unequal = (dep: {
equal: (x: number, y: number) => boolean equal: FnEqual<number>
}) => }): FnUnequal<number> =>
(x: number, y: number): boolean => !dep.equal(x, y) (x, y) => !dep.equal(x, y)

View File

@ -1,10 +1,12 @@
import type { FnNaN, FnOne, FnZero, FnRe } from "../interfaces/arithmetic"
export const number_type = { export const number_type = {
before: ['Complex'], before: ['Complex'],
test: (n: unknown): n is number => typeof n === 'number', test: (n: unknown): n is number => typeof n === 'number',
from: { string: (s: string) => +s } from: { string: (s: string) => +s }
} }
export const zero = (a: number): number => 0 export const zero: FnZero<number> = (a) => 0
export const one = (a: number): number => 1 export const one: FnOne<number> = (a) => 1
export const nan = (a: number): number => NaN export const nan: FnNaN<number> = (a) => NaN
export const re = (a: number): number => a export const re: FnRe<number, number> = (a) => a