Compare commits

...

7 Commits

Author SHA1 Message Date
40c7cd36af fix and test absquare for quaternion 2022-12-23 17:16:21 +01:00
04024a2a8d fix a TS issue 2022-12-23 12:22:41 +01:00
cbd1719227 experiment: convert all implementations to plain types 2022-12-23 11:27:39 +01:00
8c06c8f36e feat: Add generic operation square and numeric unequal (#4)
Co-authored-by: Jos de Jong <wjosdejong@gmail.com>
Co-authored-by: Glen Whitney <glen@studioinfinity.org>
Reviewed-on: #4
2022-12-22 16:12:36 +00:00
fbec410c42 feat: Implement complex arithmetic through sqrt
Together with any auxiliary functions needed for that goal. Also
  strives to ensure the same functions are being defined for
  number and for `Complex<T>`.
2022-12-22 00:14:58 -05:00
d55776655f refactor: Convenience type operator for specifying concrete signatures 2022-12-21 11:41:25 -05:00
1eb73be2fa refactor: entirely new scheme for specifying return types 2022-12-21 00:18:42 -05:00
17 changed files with 352 additions and 67 deletions

3
.gitignore vendored
View File

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

View File

@ -1,8 +1,3 @@
import {ForType} from '../core/Dispatcher.js'
import * as Complex from './native.js'
export {Complex}
declare module "../core/Dispatcher" {
interface ImplementationTypes extends ForType<'Complex', typeof Complex> {}
}

127
src/Complex/arithmetic.ts Normal file
View File

@ -0,0 +1,127 @@
import { Complex, complex_binary } from './type.js'
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))
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)
export const unaryMinus =
<T>(dep: {
unaryMinus: (z: T) => T
}) =>
(z: Complex<T>): Complex<T> =>
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))
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))
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
}) =>
(w: Complex<T>, z: Complex<T>): Complex<T> => {
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))
return complex_binary(realpart, imagpart)
}
export const absquare =
<T, U>(dep: {
add: (a: U, b: U) => U,
absquare: (z: T) => U
}) =>
(z: Complex<T>): U => 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))
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))
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))
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>
}) =>
(r: T): Complex<T> => {
if (dep.isSquare(r)) return dep.complex(dep.conservativeSqrt(r))
const negative = dep.unaryMinus(r)
if (dep.isSquare(negative)) {
return complex_binary(
dep.zero(r), dep.conservativeSqrt(negative))
}
// neither the real number or its negative is a square; could happen
// for example with bigint. So there is no square root. So we have to
// return the NaN of the type.
return dep.nan(dep.complex(r))
}
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,
}) =>
(z: Complex<T>) => {
if (dep.isReal(z)) return dep.complexSqrt(z.re)
const myabs = dep.conservativeSqrt(dep.absquare(z))
const num = dep.addReal(z, myabs)
const r = dep.re(z)
const denomsq = dep.add(dep.add(myabs, myabs), dep.add(r, r))
const denom = dep.conservativeSqrt(denomsq)
return dep.divideByReal(num, denom)
}
export const conservativeSqrt = sqrt

12
src/Complex/predicate.ts Normal file
View File

@ -0,0 +1,12 @@
import { Complex } from './type.js'
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))
export const isSquare =
<T>(z: Complex<T>) => true // FIXME: not correct for Complex<bigint> once we get there

View File

@ -0,0 +1,7 @@
import { Complex } from './type.js'
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)

View File

@ -1,22 +1,54 @@
import {joinTypes, typeOfDependency, Dependency} from '../core/Dispatcher.js'
import {
joinTypes, typeOfDependency, Dependency,
} from '../core/Dispatcher.js'
export type Complex<T> = {re: T; im: T;}
export type Complex<T> = { re: T; im: T; }
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> =>
typeof z === 'object' && '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),
infer: (dep: typeOfDependency) =>
(z: Complex<unknown>) => joinTypes(dep.typeOf(z.re), dep.typeOf(z.im)),
from: {
T: <T>(dep: Dependency<'zero', [T]>) => (t: T) =>
({re: t, im: dep.zero(t)}),
Complex: <U,T>(dep: {convert: (from: U) => T}) =>
(z: Complex<U>) => ({re: dep.convert(z.re), im: dep.convert(z.im)})
({ re: t, im: dep.zero(t) }),
Complex: <U, T>(dep: { convert: (from: U) => T }) =>
(z: Complex<U>) => ({ re: dep.convert(z.re), im: dep.convert(z.im) })
}
}
export const complex_unary = <T>(dep: Dependency<'zero', [T]>) =>
(t: T) => ({re: t, im: dep.zero(t)})
export const complex_binary = <T>(t: T, u: T) => ({re: t, im: u})
export const complex_unary =
<T>(dep: {
zero: (z: T) => Complex<T>
}) =>
(t: T) => ({ re: t, im: dep.zero(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))
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))
export const nan =
<T>(dep: {
nan: (z: T) => T
}) =>
(z: Complex<T>): Complex<T> => 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)

View File

@ -1,2 +1,3 @@
export * from './numbers/all.js'
export * from './Complex/all.js'
export * from './generic/all.js'

View File

@ -1,4 +1,5 @@
export type Config = {
epsilon: number
predictable: boolean
}

View File

@ -9,16 +9,62 @@
type TypeName = string
type Parameter = TypeName
type Signature = Parameter[]
type InputSignature = Parameter[]
type DependenciesType = Record<string, Function>
export interface ImplementationTypes {}
export type typeOfDependency = {typeOf: (x: unknown) => TypeName}
// Helper for collecting implementations
// (Really just suffixes the type name onto the keys of exports)
export type ForType<T extends string, Exports> = keyof Exports extends string
? {[K in keyof Exports as `${K}_${T}`]: Exports[K]}
: never
// All of the implementations must publish descriptions of their
// return types into the following interface, using the format
// described just below:
export interface ReturnTypes<Params> {}
/*****
To describe one implementation for a hypothetical operation `foo`, there
should be a property of the interface whose name starts with `foo` and whose
next character, if any, is an underscore. The type of this property
must be the return type of that implementation when Params matches the
parameter types of the implementation, and `never` otherwise.
Thus to describe an implementation that takes a number and a string and
returns a boolean, for example, you could write
```
declare module "Dispatcher" {
interface ReturnTypes<Params> {
foo_example: Params extends [number, string] ? boolean : never
}
}
```
If there is another, generic implementation that takes one argument
of any type and returns a Vector of that type, you can say
```
...
foo_generic: Params extends [infer T] ? Vector<T> : never
...
```
In practice, each subdirectory corresponding to a type, like Complex,
defines an interface, like `ComplexReturn<Params>` for the implementations
in that subdirectory, which can mostly be defined without suffixes because
there's typically just a single implementation within that domain.
Then the module responsible for collating all of the implementations for
that type inserts all of the properties of that interface into `ReturnTypes`
suitably suffixed to avoid collisions.
One might think that simply defining an implementation for `foo`
of type `(n: number, s: string) => boolean` would provide all of the same
information as the type of the key `foo_example` in the ReturnTypes
interface above, but in practice TypeScript has challenges in extracting
types relating to functions. (In particular, there is no
way to get the specialized return type of a generic function when it is
called on aguments whose specific types match the generic parameters.)
Hence the need for this additional mechanism to specify return types, in
a way readily suited for TypeScript type computations.
*****/
// Helpers for specifying signatures
// A basic signature with concrete types
export type Signature<CandidateParams, ActualParams, Returns> =
CandidateParams extends ActualParams ? Returns : never
//dummy implementation for now
export function joinTypes(a: TypeName, b: TypeName) {
@ -26,27 +72,27 @@ export function joinTypes(a: TypeName, b: TypeName) {
return 'any'
}
/**
* Build up to Dependency type lookup
*/
type DependenciesType = Record<string, Function>
// Used to filter keys that match a given operation name
type BeginsWith<Name extends string> = Name | `${Name}_${string}`
type FinalShape<FuncType> =
FuncType extends (arg: DependenciesType) => Function
? ReturnType<FuncType> : FuncType
// Look up the return type of an implementation based on its name
// and the parameters it takes
export type ImpReturns<Name extends string, Params> =
{[K in keyof ReturnTypes<Params>]: K extends BeginsWith<Name>
? ReturnTypes<Params>[K] : never}[keyof ReturnTypes<Params>]
type BeginsWith<Name extends string> = `${Name}${string}`
type DependencyTypes<Ob, Name extends string, Params extends unknown[]> =
{[K in keyof Ob]: K extends BeginsWith<Name>
? FinalShape<Ob[K]> extends (...args: Params) => any
? FinalShape<Ob[K]>
: never
: never}
// The type of an implementation (with dependencies satisfied,
// based on its name and the parameters it takes
export type ImpType<Name extends string, Params extends unknown[]> =
(...args: Params) => ImpReturns<Name, Params>
// The type of a dependency on an implementation based on its name
// and the parameters it takes (just a simple object with one property
// named the same as the operation, of value type equal to the type of
// that implementation. These can be `&`ed together in case of multiple
// dependencies:
export type Dependency<Name extends string, Params extends unknown[]> =
{[N in Name]:
DependencyTypes<ImplementationTypes, N, Params>[keyof ImplementationTypes]}
{[N in Name]: ImpType<N, Params>}
// Now types used in the Dispatcher class itself
@ -64,9 +110,9 @@ type SpecificationsGroup = Record<string, SpecObject>
export class Dispatcher {
installSpecification(
name: string,
signature: Signature,
signature: InputSignature,
returns: TypeName,
dependencies: Record<string, Signature>,
dependencies: Record<string, InputSignature>,
behavior: Function // possible todo: constrain this type based
// on the signature, return type, and dependencies. Not sure if
// that's really possible, though.

3
src/generic/all.ts Normal file
View File

@ -0,0 +1,3 @@
import * as generic from './arithmetic.js'
export { generic }

View File

@ -0,0 +1,5 @@
export const square =
<T>(dep: {
multiply: (x: T, y: T) => T
}) =>
(z: T): T => dep.multiply(z, z)

View File

@ -2,3 +2,22 @@ import {Dispatcher} from './core/Dispatcher.js'
import * as Specifications from './all.js'
export default new Dispatcher(Specifications)
// Test https://github.com/josdejong/pocomath/issues/1#issuecomment-1364056151
import {Complex} from './Complex/type.js'
import {absquare as absquare_complex} from './Complex/arithmetic.js'
const mockRealAdd = (a: number, b: number) => a+b
const mockComplexAbsquare = (z: Complex<number>) => z.re*z.re + z.im*z.im
const quatAbsquare = absquare_complex({
add: mockRealAdd,
absquare: mockComplexAbsquare
})
const myabs = quatAbsquare({re: {re: 0, im: 1}, im: {re:2, im: 3}})
const typeTest: typeof myabs = 7 // check myabs is just a number
console.log('Result is', myabs)

View File

@ -1,8 +1,3 @@
import {ForType} from '../core/Dispatcher.js'
import * as numbers from './native.js'
export {numbers}
declare module "../core/Dispatcher" {
interface ImplementationTypes extends ForType<'numbers', typeof numbers> {}
}

View File

@ -1,18 +1,26 @@
import {configDependency} from '../core/Config.js'
import {Dependency} from '../core/Dispatcher.js'
import { Config } from '../core/Config.js'
import type { Complex } from '../Complex/type.js'
export const add = (a: number, b: number): number => 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 divideByReal = divide
export const conservativeSqrt = (a: number): number => isNaN(a) ? NaN : Math.sqrt(a)
export const add = (a: number, b: number) => a + b
export const unaryMinus = (a: number) => -a
export const subtract = (a: number, b: number) => a - b
export const multiply = (a: number, b: number) => a * b
export const divide = (a: number, b: number) => a / b
export const sqrt =
(dep: configDependency
& Dependency<'complex', [number, number]>) => {
if (dep.config.predictable || !dep.complex) {
return (a: number) => isNaN(a) ? NaN : Math.sqrt(a)
}
return (a: number) => {
(dep: {
config: Config,
complex: (re: number, im: number) => Complex<number>
}): (a: number) => number | Complex<number> => {
if (dep.config.predictable || !dep.complex) return conservativeSqrt
return a => {
if (isNaN(a)) return NaN
if (a >= 0) return Math.sqrt(a)
return dep.complex(0, Math.sqrt(unaryMinus(a)))

2
src/numbers/predicate.ts Normal file
View File

@ -0,0 +1,2 @@
export const isReal = (a: number) : boolean => true
export const isSquare = (a: number) : boolean => a >= 0

26
src/numbers/relational.ts Normal file
View File

@ -0,0 +1,26 @@
import { Config } from '../core/Config.js'
const DBL_EPSILON = Number.EPSILON || 2.2204460492503130808472633361816E-16
export const equal =
(dep: {
config: Config
}) => (x: number, y: number): boolean => {
const eps = dep.config.epsilon
if (eps === null || eps === undefined) return x === y
if (x === y) return true
if (isNaN(x) || isNaN(y)) return false
if (isFinite(x) && isFinite(y)) {
const diff = Math.abs(x - y)
if (diff < DBL_EPSILON) return true
return diff <= Math.max(Math.abs(x), Math.abs(y)) * eps
}
return false
}
export const unequal = (dep: {
equal: (x: number, y: number) => boolean
}) =>
(x: number, y: number): boolean => !dep.equal(x, y)

View File

@ -1,7 +1,10 @@
export const number_type = {
before: ['Complex'],
test: (n: unknown): n is number => typeof n === 'number',
from: {string: s => +s}
from: { string: (s: string) => +s }
}
export const zero = (a: number) => 0
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