refactor: rename absquare to normsq and add norm
This commit is contained in:
parent
edfba089e3
commit
ec97b0e20a
7 changed files with 32 additions and 20 deletions
|
@ -11,7 +11,7 @@ describe('BooleanT Type', () => {
|
|||
})
|
||||
it('autoconverts to number type', () => {
|
||||
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.divide(false, true), 0)
|
||||
assert.strictEqual(math.cbrt(true), 1)
|
||||
|
|
|
@ -9,10 +9,17 @@ const CplxNum = Complex(NumberT)
|
|||
const {full} = ReturnTyping
|
||||
|
||||
describe('complex arithmetic operations', () => {
|
||||
it('computes absquare of complex numbers', () => {
|
||||
assert.strictEqual(math.absquare(cplx(3, 4)), 25)
|
||||
assert.strictEqual(math.absquare(cplx(cplx(2, 3), cplx(4,5))), 54)
|
||||
assert.strictEqual(math.absquare(cplx(true, true)), 2)
|
||||
it('computes norm-square of complex numbers', () => {
|
||||
assert.strictEqual(math.normsq(cplx(3, 4)), 25)
|
||||
assert.strictEqual(math.normsq(cplx(cplx(2, 3), cplx(4, 5))), 54)
|
||||
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', () => {
|
||||
const z = cplx(3, 4)
|
||||
|
|
|
@ -7,11 +7,11 @@ import {ReturnsAs} from '#generic/helpers.js'
|
|||
|
||||
const {conservative, full, free} = ReturnTyping
|
||||
|
||||
export const absquare = match(Complex, (math, C, strategy) => {
|
||||
const compAbsq = math.absquare.resolve(C.Component, full)
|
||||
const R = compAbsq.returns
|
||||
export const normsq = match(Complex, (math, C, strategy) => {
|
||||
const compNormsq = math.normsq.resolve(C.Component, full)
|
||||
const R = compNormsq.returns
|
||||
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')
|
||||
|
@ -38,12 +38,12 @@ export const divide = [
|
|||
|
||||
export const invert = match(Complex, (math, C, strategy) => {
|
||||
const conj = math.conj.resolve(C, full)
|
||||
const norm = math.absquare.resolve(C, full)
|
||||
const div = math.divide.resolve([C.Component, norm.returns], full)
|
||||
const normsq = math.normsq.resolve(C, full)
|
||||
const div = math.divide.resolve([C.Component, normsq.returns], full)
|
||||
const cplx = maybeComplex(math, strategy, div.returns, div.returns)
|
||||
return ReturnsAs(cplx, z => {
|
||||
const c = conj(z)
|
||||
const d = norm(z)
|
||||
const d = normsq(z)
|
||||
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 {match, Any} from '#core/TypePatterns.js'
|
||||
|
||||
export const abs = match(Any, (math, T) => {
|
||||
const absq = math.absquare.resolve(T)
|
||||
const sqrt = math.sqrt.resolve(absq.returns, ReturnTyping.conservative)
|
||||
return ReturnsAs(sqrt, t => sqrt(absq(t)))
|
||||
export const norm = match(Any, (math, T) => {
|
||||
const normsq = math.normsq.resolve(T)
|
||||
const sqrt = math.sqrt.resolve(normsq.returns, ReturnTyping.conservative)
|
||||
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 square = match(Any, (math, T, strategy) => {
|
||||
const mult = math.multiply.resolve([T, T], strategy)
|
||||
|
|
|
@ -5,7 +5,8 @@ import {ReturnTyping} from '#core/Type.js'
|
|||
describe('number arithmetic', () => {
|
||||
it('supports basic operations', () => {
|
||||
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.divide(6, 4), 1.5)
|
||||
assert.strictEqual(math.cbrt(-8), -2)
|
||||
|
|
|
@ -7,7 +7,8 @@ import {Complex} from '#complex/Complex.js'
|
|||
const {conservative, full} = ReturnTyping
|
||||
|
||||
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 divide = plain((a, b) => a / b)
|
||||
export const cbrt = plain(a => {
|
||||
|
|
|
@ -39,7 +39,7 @@ export const indistinguishable = [
|
|||
return Returns(V, (v, e, [[rT, aT]]) => v.map(f => {
|
||||
return math.indistinguishable(f, e, rT, aT)
|
||||
}))
|
||||
}
|
||||
}
|
||||
const same = math.indistinguishable.resolve([VComp, E, RT, AT])
|
||||
return Returns(
|
||||
Vector(same.returns),
|
||||
|
@ -54,7 +54,7 @@ export const indistinguishable = [
|
|||
return Returns(V, (e, v) => v.map(f => {
|
||||
return math.indistinguishable.resolve([E, math.typeOf(f)])(e, f)
|
||||
}))
|
||||
}
|
||||
}
|
||||
const same = math.indistinguishable.resolve([E, VComp])
|
||||
return Returns(
|
||||
Vector(same.returns), (e, v) => v.map(f => same(e, f)))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue