Declare implementations and dependencies via standard interfaces for operations #8

Merged
glen merged 20 commits from approach4.6 into main 2023-01-22 01:34:57 +00:00
14 changed files with 64 additions and 64 deletions
Showing only changes of commit a0b21181e6 - Show all commits

View File

@ -1,10 +1,10 @@
import {Complex} from './type.js'
import type {
Dependencies, OpType, OpReturns, RealType, ZeroType
Dependencies, Signature, Returns, RealType, ZeroType
} from '../interfaces/type.js'
declare module "../interfaces/type" {
interface Operations<T> {
interface Signatures<T> {
// TODO: Make Dispatcher collapse operations that match
// after removing any `_...` suffixes; the following should be
// additional dispatches for add and divide, not separate
@ -15,33 +15,33 @@ declare module "../interfaces/type" {
}
export const add =
<T>(dep: Dependencies<'add' | 'complex', T>): OpType<'add', Complex<T>> =>
<T>(dep: Dependencies<'add' | 'complex', T>): Signature<'add', Complex<T>> =>
(w, z) => dep.complex(dep.add(w.re, z.re), dep.add(w.im, z.im))
export const add_real =
<T>(dep: Dependencies<'add_real' | 'complex', T>):
OpType<'add_real', Complex<T>> =>
Signature<'add_real', Complex<T>> =>
(z, r) => dep.complex(dep.add_real(z.re, r), z.im)
export const unaryMinus =
<T>(dep: Dependencies<'unaryMinus' | 'complex', T>):
OpType<'unaryMinus', Complex<T>> =>
Signature<'unaryMinus', Complex<T>> =>
z => dep.complex(dep.unaryMinus(z.re), dep.unaryMinus(z.im))
export const conj =
<T>(dep: Dependencies<'unaryMinus' | 'conj' | 'complex', T>):
OpType<'conj', Complex<T>> =>
Signature<'conj', Complex<T>> =>
z => dep.complex(dep.conj(z.re), dep.unaryMinus(z.im))
export const subtract =
<T>(dep: Dependencies<'subtract' | 'complex', T>):
OpType<'subtract', Complex<T>> =>
Signature<'subtract', Complex<T>> =>
(w, z) => dep.complex(dep.subtract(w.re, z.re), dep.subtract(w.im, z.im))
export const multiply =
<T>(dep: Dependencies<
'add' | 'subtract' | 'multiply' | 'conj' | 'complex', T>):
OpType<'multiply', Complex<T>> =>
Signature<'multiply', Complex<T>> =>
(w, z) => {
const mult = dep.multiply
const realpart = dep.subtract(
@ -53,23 +53,23 @@ export const multiply =
export const absquare =
<T>(dep: Dependencies<'absquare', T>
& Dependencies<'add', OpReturns<'absquare', T>>):
OpType<'absquare', Complex<T>> =>
& Dependencies<'add', Returns<'absquare', T>>):
Signature<'absquare', Complex<T>> =>
z => dep.add(dep.absquare(z.re), dep.absquare(z.im))
export const divideByReal =
<T>(dep: Dependencies<'divide_real' | 'complex', T>):
OpType<'divide_real', Complex<T>> =>
Signature<'divide_real', Complex<T>> =>
(z, r) => dep.complex(dep.divide_real(z.re, r), dep.divide_real(z.im, r))
export const reciprocal =
<T>(dep: Dependencies<'conj' | 'absquare' | 'divide_real', Complex<T>>):
OpType<'reciprocal', Complex<T>> =>
Signature<'reciprocal', Complex<T>> =>
z => dep.divide_real(dep.conj(z), dep.absquare(z))
export const divide =
<T>(dep: Dependencies<'multiply' | 'reciprocal', Complex<T>>):
OpType<'divide', Complex<T>> =>
Signature<'divide', Complex<T>> =>
(w, z) => dep.multiply(w, dep.reciprocal(z))
// The dependencies are slightly tricky here, because there are three types
@ -82,8 +82,8 @@ export const sqrt =
RealType<T>>
& Dependencies<'zero' | 'add_real' | 'complex', T>
& Dependencies<'absquare' | 're' | 'divide_real', Complex<T>>
& {add_complex_real: OpType<'add_real', Complex<T>>}):
OpType<'sqrt', Complex<T>> =>
& {add_complex_real: Signature<'add_real', Complex<T>>}):
Signature<'sqrt', Complex<T>> =>
z => {
const myabs = dep.conservativeSqrt(dep.absquare(z))
const r = dep.re(z)

View File

@ -1,9 +1,9 @@
import {Complex} from './type.js'
import type {Dependencies, OpType} from '../interfaces/type.js'
import type {Dependencies, Signature} from '../interfaces/type.js'
export const isReal =
<T>(dep: Dependencies<'add' | 'equal' | 'isReal', T>):
OpType<'isReal', Complex<T>> =>
Signature<'isReal', Complex<T>> =>
z => dep.isReal(z.re) && dep.equal(z.re, dep.add(z.re, z.im))
export const isSquare: OpType<'isSquare', Complex<any>> = z => true // FIXME: not correct for Complex<bigint> once we get there
export const isSquare: Signature<'isSquare', Complex<any>> = z => true // FIXME: not correct for Complex<bigint> once we get there

View File

@ -1,6 +1,6 @@
import {Complex} from './type.js'
import {Dependencies, OpType} from '../interfaces/type.js'
import {Dependencies, Signature} from '../interfaces/type.js'
export const equal =
<T>(dep: Dependencies<'equal', T>): OpType<'equal', Complex<T>> =>
<T>(dep: Dependencies<'equal', T>): Signature<'equal', Complex<T>> =>
(w, z) => dep.equal(w.re, z.re) && dep.equal(w.im, z.im)

View File

@ -1,6 +1,6 @@
import {joinTypes, typeOfDependency} from '../core/Dispatcher.js'
import type {
ZeroType, OneType, NaNType, Dependencies, OpType, OpReturns
ZeroType, OneType, NaNType, Dependencies, Signature, Returns
} from '../interfaces/type.js'
export type Complex<T> = { re: T; im: T; }
@ -31,33 +31,33 @@ declare module "../interfaces/type" {
} : never
}
interface Operations<T> {
interface Signatures<T> {
complex: {params: [T] | [T,T], returns: Complex<T>}
}
}
export const complex =
<T>(dep: Dependencies<'zero', T>): OpType<'complex', T> =>
<T>(dep: Dependencies<'zero', T>): Signature<'complex', T> =>
(a, b) => ({re: a, im: b || dep.zero(a)})
export const zero =
<T>(dep: Dependencies<'zero', T>
& Dependencies<'complex', OpReturns<'zero', T>>):
OpType<'zero', Complex<T>> =>
& Dependencies<'complex', Returns<'zero', T>>):
Signature<'zero', Complex<T>> =>
z => dep.complex(dep.zero(z.re), dep.zero(z.im))
export const one =
<T>(dep: Dependencies<'one' | 'zero', T>
& Dependencies<'complex', OpReturns<'one' | 'zero', T>>):
OpType<'one', Complex<T>> =>
& Dependencies<'complex', Returns<'one' | 'zero', T>>):
Signature<'one', Complex<T>> =>
z => dep.complex(dep.one(z.re), dep.zero(z.im))
export const nan =
<T>(dep: Dependencies<'nan', T>
& Dependencies<'complex', OpReturns<'nan', T>>):
OpType<'nan', Complex<T>> =>
& Dependencies<'complex', Returns<'nan', T>>):
Signature<'nan', Complex<T>> =>
z => dep.complex(dep.nan(z.re), dep.nan(z.im))
export const re =
<T>(dep: Dependencies<'re', T>): OpType<'re', Complex<T>> =>
<T>(dep: Dependencies<'re', T>): Signature<'re', Complex<T>> =>
z => dep.re(z.re)

View File

@ -1,5 +1,5 @@
import type {Dependencies, OpType} from '../interfaces/type.js'
import type {Dependencies, Signature} from '../interfaces/type.js'
export const square =
<T>(dep: Dependencies<'multiply', T>): OpType<'square', T> =>
<T>(dep: Dependencies<'multiply', T>): Signature<'square', T> =>
z => dep.multiply(z, z)

View File

@ -1,5 +1,5 @@
import {Dependencies, OpType} from '../interfaces/type.js'
import {Dependencies, Signature} from '../interfaces/type.js'
export const unequal =
<T>(dep: Dependencies<'equal', T>): OpType<'unequal', T> =>
<T>(dep: Dependencies<'equal', T>): Signature<'unequal', T> =>
(x, y) => !dep.equal(x, y)

View File

@ -4,7 +4,7 @@ import type {RealType} from './type.js'
type UnaryOperator<T> = {params: [T], returns: T}
type BinaryOperator<T> = {params: [T, T], returns: T}
declare module "./type" {
interface Operations<T> {
interface Signatures<T> {
add: BinaryOperator<T>
unaryMinus: UnaryOperator<T>
conj: UnaryOperator<T>

View File

@ -2,7 +2,7 @@
// section; otherwise it is ignored.
export type UnaryPredicate<T> = {params: [T], returns: boolean}
declare module "./type" {
interface Operations<T> {
interface Signatures<T> {
isReal: UnaryPredicate<T>
isSquare: UnaryPredicate<T>
}

View File

@ -2,7 +2,7 @@
// section; otherwise it is ignored.
export type BinaryPredicate<T> = {params: [T, T], returns: boolean}
declare module "./type" {
interface Operations<T> {
interface Signatures<T> {
equal: BinaryPredicate<T>
unequal: BinaryPredicate<T>
}

View File

@ -57,7 +57,7 @@ export type RealType<T> = ALookup<T, 'real'>
* and it records the name of the operation as 're' also by virtue of the
* key 're' in the interface.
****/
export interface Operations<T> {
export interface Signatures<T> {
zero: {params: [T], returns: ZeroType<T>}
one: {params: [T], returns: OneType<T>}
// nan needs to be able to operate on its own output for everything
@ -66,9 +66,9 @@ export interface Operations<T> {
re: {params: [T], returns: RealType<T>}
}
type OpKey = keyof Operations<unknown>
type SignatureKey = keyof Signatures<unknown>
export type OpReturns<Name extends OpKey, T> = Operations<T>[Name]['returns']
export type OpType<Name extends OpKey, T> =
(...args: Operations<T>[Name]['params']) => OpReturns<Name, T>
export type Dependencies<Name extends OpKey, T> = {[K in Name]: OpType<K, T>}
export type Returns<Name extends SignatureKey, T> = Signatures<T>[Name]['returns']
export type Signature<Name extends SignatureKey, T> =
(...args: Signatures<T>[Name]['params']) => Returns<Name, T>
export type Dependencies<Name extends SignatureKey, T> = {[K in Name]: Signature<K, T>}

View File

@ -1,21 +1,21 @@
import type {configDependency} from '../core/Config.js'
import type {Dependencies, OpType} from '../interfaces/type.js'
import type {Dependencies, Signature} from '../interfaces/type.js'
export const add: OpType<'add', number> = (a, b) => a + b
export const unaryMinus: OpType<'unaryMinus', number> = a => -a
export const conj: OpType<'conj', number> = a => a
export const subtract: OpType<'subtract', number> = (a, b) => a - b
export const multiply: OpType<'multiply', number> = (a, b) => a * b
export const absquare: OpType<'absquare', number> = a => a * a
export const reciprocal: OpType<'reciprocal', number> = a => 1 / a
export const divide: OpType<'divide', number> = (a, b) => a / b
export const add: Signature<'add', number> = (a, b) => a + b
export const unaryMinus: Signature<'unaryMinus', number> = a => -a
export const conj: Signature<'conj', number> = a => a
export const subtract: Signature<'subtract', number> = (a, b) => a - b
export const multiply: Signature<'multiply', number> = (a, b) => a * b
export const absquare: Signature<'absquare', number> = a => a * a
export const reciprocal: Signature<'reciprocal', number> = a => 1 / a
export const divide: Signature<'divide', number> = (a, b) => a / b
const basicSqrt = a => isNaN(a) ? NaN : Math.sqrt(a)
export const conservativeSqrt: OpType<'conservativeSqrt', number> = basicSqrt
export const conservativeSqrt: Signature<'conservativeSqrt', number> = basicSqrt
export const sqrt =
(dep: configDependency & Dependencies<'complex', number>):
OpType<'sqrt', number> => {
Signature<'sqrt', number> => {
if (dep.config.predictable || !dep.complex) return basicSqrt
return a => {
if (isNaN(a)) return NaN

View File

@ -1,4 +1,4 @@
import type {OpType} from '../interfaces/type.js'
import type {Signature} from '../interfaces/type.js'
export const isReal: OpType<'isReal', number> = (a) => true
export const isSquare: OpType<'isSquare', number> = (a) => a >= 0
export const isReal: Signature<'isReal', number> = (a) => true
export const isSquare: Signature<'isSquare', number> = (a) => a >= 0

View File

@ -1,10 +1,10 @@
import {configDependency} from '../core/Config.js'
import {OpType} from '../interfaces/type.js'
import {Signature} from '../interfaces/type.js'
const DBL_EPSILON = Number.EPSILON || 2.2204460492503130808472633361816E-16
export const equal =
(dep: configDependency): OpType<'equal', number> =>
(dep: configDependency): Signature<'equal', number> =>
(x, y) => {
const eps = dep.config.epsilon
if (eps === null || eps === undefined) return x === y

View File

@ -1,4 +1,4 @@
import type { OpType } from '../interfaces/type.js'
import type { Signature } from '../interfaces/type.js'
export const number_type = {
before: ['Complex'],
@ -19,7 +19,7 @@ declare module "../interfaces/type" {
}
// I don't like the redundancy of repeating 'zero'; any way to eliminate that?
export const zero: OpType<'zero', number> = (a) => 0
export const one: OpType<'one', number> = (a) => 1
export const nan: OpType<'nan', number> = (a) => NaN
export const re: OpType<'re', number> = (a) => a
export const zero: Signature<'zero', number> = (a) => 0
export const one: Signature<'one', number> = (a) => 1
export const nan: Signature<'nan', number> = (a) => NaN
export const re: Signature<'re', number> = (a) => a