feat: Implement Vector type #28
7 changed files with 32 additions and 20 deletions
|
@ -11,7 +11,7 @@ describe('BooleanT Type', () => {
|
||||||
})
|
})
|
||||||
it('autoconverts to number type', () => {
|
it('autoconverts to number type', () => {
|
||||||
assert.strictEqual(math.abs(false), 0)
|
assert.strictEqual(math.abs(false), 0)
|
||||||
assert.strictEqual(math.absquare(true), 1)
|
assert.strictEqual(math.normsq(true), 1)
|
||||||
assert.strictEqual(math.add(true, true), 2)
|
assert.strictEqual(math.add(true, true), 2)
|
||||||
assert.strictEqual(math.divide(false, true), 0)
|
assert.strictEqual(math.divide(false, true), 0)
|
||||||
assert.strictEqual(math.cbrt(true), 1)
|
assert.strictEqual(math.cbrt(true), 1)
|
||||||
|
|
|
@ -9,10 +9,17 @@ const CplxNum = Complex(NumberT)
|
||||||
const {full} = ReturnTyping
|
const {full} = ReturnTyping
|
||||||
|
|
||||||
describe('complex arithmetic operations', () => {
|
describe('complex arithmetic operations', () => {
|
||||||
it('computes absquare of complex numbers', () => {
|
it('computes norm-square of complex numbers', () => {
|
||||||
assert.strictEqual(math.absquare(cplx(3, 4)), 25)
|
assert.strictEqual(math.normsq(cplx(3, 4)), 25)
|
||||||
assert.strictEqual(math.absquare(cplx(cplx(2, 3), cplx(4,5))), 54)
|
assert.strictEqual(math.normsq(cplx(cplx(2, 3), cplx(4, 5))), 54)
|
||||||
assert.strictEqual(math.absquare(cplx(true, true)), 2)
|
assert.strictEqual(math.normsq(cplx(true, true)), 2)
|
||||||
|
})
|
||||||
|
it('computes norm-magnitude of complex numbers', () => {
|
||||||
|
assert.strictEqual(math.norm(cplx(-3, 4)), 5)
|
||||||
|
assert.strictEqual(
|
||||||
|
math.norm(cplx(cplx(2, -3), cplx(-4, 5))), Math.sqrt(54))
|
||||||
|
const boolnorm = math.norm(cplx(true, true))
|
||||||
|
assert(math.equal(boolnorm * boolnorm, 2))
|
||||||
})
|
})
|
||||||
it('adds complex numbers', () => {
|
it('adds complex numbers', () => {
|
||||||
const z = cplx(3, 4)
|
const z = cplx(3, 4)
|
||||||
|
|
|
@ -7,11 +7,11 @@ import {ReturnsAs} from '#generic/helpers.js'
|
||||||
|
|
||||||
const {conservative, full, free} = ReturnTyping
|
const {conservative, full, free} = ReturnTyping
|
||||||
|
|
||||||
export const absquare = match(Complex, (math, C, strategy) => {
|
export const normsq = match(Complex, (math, C, strategy) => {
|
||||||
const compAbsq = math.absquare.resolve(C.Component, full)
|
const compNormsq = math.normsq.resolve(C.Component, full)
|
||||||
const R = compAbsq.returns
|
const R = compNormsq.returns
|
||||||
const add = math.add.resolve([R,R], strategy)
|
const add = math.add.resolve([R,R], strategy)
|
||||||
return ReturnsAs(add, z => add(compAbsq(z.re), compAbsq(z.im)))
|
return ReturnsAs(add, z => add(compNormsq(z.re), compNormsq(z.im)))
|
||||||
})
|
})
|
||||||
|
|
||||||
export const add = promoteBinary('add')
|
export const add = promoteBinary('add')
|
||||||
|
@ -38,12 +38,12 @@ export const divide = [
|
||||||
|
|
||||||
export const invert = match(Complex, (math, C, strategy) => {
|
export const invert = match(Complex, (math, C, strategy) => {
|
||||||
const conj = math.conj.resolve(C, full)
|
const conj = math.conj.resolve(C, full)
|
||||||
const norm = math.absquare.resolve(C, full)
|
const normsq = math.normsq.resolve(C, full)
|
||||||
const div = math.divide.resolve([C.Component, norm.returns], full)
|
const div = math.divide.resolve([C.Component, normsq.returns], full)
|
||||||
const cplx = maybeComplex(math, strategy, div.returns, div.returns)
|
const cplx = maybeComplex(math, strategy, div.returns, div.returns)
|
||||||
return ReturnsAs(cplx, z => {
|
return ReturnsAs(cplx, z => {
|
||||||
const c = conj(z)
|
const c = conj(z)
|
||||||
const d = norm(z)
|
const d = normsq(z)
|
||||||
return cplx(div(c.re, d), div(c.im, d))
|
return cplx(div(c.re, d), div(c.im, d))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -3,11 +3,14 @@ import {ReturnsAs} from './helpers.js'
|
||||||
import {Returns, ReturnTyping} from '#core/Type.js'
|
import {Returns, ReturnTyping} from '#core/Type.js'
|
||||||
import {match, Any} from '#core/TypePatterns.js'
|
import {match, Any} from '#core/TypePatterns.js'
|
||||||
|
|
||||||
export const abs = match(Any, (math, T) => {
|
export const norm = match(Any, (math, T) => {
|
||||||
const absq = math.absquare.resolve(T)
|
const normsq = math.normsq.resolve(T)
|
||||||
const sqrt = math.sqrt.resolve(absq.returns, ReturnTyping.conservative)
|
const sqrt = math.sqrt.resolve(normsq.returns, ReturnTyping.conservative)
|
||||||
return ReturnsAs(sqrt, t => sqrt(absq(t)))
|
return ReturnsAs(sqrt, t => sqrt(normsq(t)))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
export const abs = norm // coincide for most types (scalars)
|
||||||
|
|
||||||
export const conj = match(Any, (_math, T) => Returns(T, t => t))
|
export const conj = match(Any, (_math, T) => Returns(T, t => t))
|
||||||
export const square = match(Any, (math, T, strategy) => {
|
export const square = match(Any, (math, T, strategy) => {
|
||||||
const mult = math.multiply.resolve([T, T], strategy)
|
const mult = math.multiply.resolve([T, T], strategy)
|
||||||
|
|
|
@ -5,7 +5,8 @@ import {ReturnTyping} from '#core/Type.js'
|
||||||
describe('number arithmetic', () => {
|
describe('number arithmetic', () => {
|
||||||
it('supports basic operations', () => {
|
it('supports basic operations', () => {
|
||||||
assert.strictEqual(math.abs(-Infinity), Infinity)
|
assert.strictEqual(math.abs(-Infinity), Infinity)
|
||||||
assert.strictEqual(math.absquare(-2), 4)
|
assert(isNaN(math.norm(NaN)))
|
||||||
|
assert.strictEqual(math.normsq(-2), 4)
|
||||||
assert.strictEqual(math.add(2, 2), 4)
|
assert.strictEqual(math.add(2, 2), 4)
|
||||||
assert.strictEqual(math.divide(6, 4), 1.5)
|
assert.strictEqual(math.divide(6, 4), 1.5)
|
||||||
assert.strictEqual(math.cbrt(-8), -2)
|
assert.strictEqual(math.cbrt(-8), -2)
|
||||||
|
|
|
@ -7,7 +7,8 @@ import {Complex} from '#complex/Complex.js'
|
||||||
const {conservative, full} = ReturnTyping
|
const {conservative, full} = ReturnTyping
|
||||||
|
|
||||||
export const abs = plain(Math.abs)
|
export const abs = plain(Math.abs)
|
||||||
export const absquare = plain(a => a*a)
|
export const norm = abs
|
||||||
|
export const normsq = plain(a => a*a)
|
||||||
export const add = plain((a, b) => a + b)
|
export const add = plain((a, b) => a + b)
|
||||||
export const divide = plain((a, b) => a / b)
|
export const divide = plain((a, b) => a / b)
|
||||||
export const cbrt = plain(a => {
|
export const cbrt = plain(a => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue