feat(polynomialRoot) #57

Merged
glen merged 4 commits from polynomialRoot into main 2022-12-01 17:47:21 +00:00
4 changed files with 116 additions and 73 deletions
Showing only changes of commit de52c041e5 - Show all commits

View File

@ -9,9 +9,11 @@ export {conjugate} from './conjugate.mjs'
export {equalTT} from './equalTT.mjs'
export {gcd} from './gcd.mjs'
export {invert} from './invert.mjs'
export {isReal} from './isReal.mjs'
export {isZero} from './isZero.mjs'
export {multiply} from './multiply.mjs'
export {negate} from './negate.mjs'
export {polynomialRoot} from './polynomialRoot.mjs'
export {quaternion} from './quaternion.mjs'
export {quotient} from './quotient.mjs'
export {roundquotient} from './roundquotient.mjs'

View File

@ -87,6 +87,7 @@ function substituteInSignature(signature, parameter, type) {
return sig.replaceAll(pattern, type)
}
const UniversalType = 'ground' // name for a type that matches anything
let lastWhatToDo = null // used in an infinite descent check
export default class PocomathInstance {
@ -128,12 +129,13 @@ export default class PocomathInstance {
// its onMismatch function, below:
this._metaTyped = typed.create()
this._metaTyped.clear()
this._metaTyped.addTypes([{name: UniversalType, test: () => true}])
// And these are the meta bindings: (I think we don't need separate
// invalidation for them as they are only accessed through a main call.)
this._meta = {} // The resulting typed-functions
this._metaTFimps = {} // and their implementations
const me = this
const myTyped = this._typed
this._typed.onMismatch = (name, args, sigs) => {
if (me._invalid.has(name)) {
// rebuild implementation and try again
@ -523,6 +525,10 @@ export default class PocomathInstance {
}
}
}
// Need to metafy ground types
if (type === base) {
this._metafy(type)
}
// update the typeOf function
const imp = {}
imp[type] = {uses: new Set(), does: () => Returns('string', () => type)}
@ -640,7 +646,7 @@ export default class PocomathInstance {
}
// install the "base type" in the meta universe:
let beforeType = 'any'
let beforeType = UniversalType
for (const other of spec.before || []) {
if (other in this.templates) {
beforeType = other
@ -648,6 +654,13 @@ export default class PocomathInstance {
}
}
this._metaTyped.addTypes([{name: base, test: spec.base}], beforeType)
// Add conversions to the base type:
if (spec.from && spec.from[theTemplateParam]) {
for (const ground of this._metafiedTypes) {
this._metaTyped.addConversion(
{from: ground, to: base, convert: spec.from[theTemplateParam]})
}
}
this._instantiationsOf[base] = new Set()
// update the typeOf function
@ -881,7 +894,7 @@ export default class PocomathInstance {
basesig, theTemplateParam, '')
if (testsig === basesig) {
// that is not also top-level
for (const templateType of typeListOfSignature(basesig)) {
for (let templateType of typeListOfSignature(basesig)) {
if (templateType.slice(0,3) === '...') {
templateType = templateType.slice(3)
}
@ -908,7 +921,7 @@ export default class PocomathInstance {
let baseSignature = rawSignature.replaceAll(templateCall, '')
/* Any remaining template params are top-level */
const signature = substituteInSignature(
baseSignature, theTemplateParam, 'any')
baseSignature, theTemplateParam, UniversalType)
const hasTopLevel = (signature !== baseSignature)
if (!ubType && hasTopLevel) {
for (const othersig in imps) {
@ -939,7 +952,6 @@ export default class PocomathInstance {
}
}
}
for (const instType of instantiationSet) {
this._instantiateTemplateImplementation(name, rawSignature, instType)
}
@ -1018,9 +1030,10 @@ export default class PocomathInstance {
usedConversions = true
instantiateFor = self.joinTypes(argTypes, usedConversions)
if (instantiateFor === 'any') {
let argDisplay = args.map(toString).join(', ')
throw TypeError(
`In call to ${name}, no type unifies arguments `
+ args.toString() + '; of types ' + argTypes.toString()
+ argDisplay + '; of types ' + argTypes.toString()
+ '; note each consecutive pair must unify to a '
+ 'supertype of at least one of them')
}
@ -1042,7 +1055,9 @@ export default class PocomathInstance {
for (j = 0; j < parTypes.length; ++j) {
if (wantTypes[j] !== parTypes[j] && parTypes[j].includes('<')) {
// actually used the param and is a template
self._ensureTemplateTypes(parTypes[j], instantiateFor)
const strippedType = parTypes[j].substr(
parTypes[j].lastIndexOf('.') + 1)
self._ensureTemplateTypes(strippedType, instantiateFor)
}
}
@ -1117,11 +1132,13 @@ export default class PocomathInstance {
if (Object.keys(tf_imps).length > 0) {
tf = this._typed(name, tf_imps)
tf.fromInstance = this
tf.isMeta = false
}
let metaTF
if (Object.keys(meta_imps).length > 0) {
metaTF = this._metaTyped(name, meta_imps)
metaTF.fromInstance = this
metaTF.isMeta = true
}
this._meta[name] = metaTF
@ -1325,6 +1342,14 @@ export default class PocomathInstance {
if (destination && needsig) {
destination = this.resolve(func, needsig)
}
if (!destination) {
// Unresolved reference. This is allowed so that
// you can bundle up just some portions of the library,
// but let's warn.
console.log(
'WARNING: No definition found for dependency',
dep, 'needed by a function with signature', signature)
}
refs[dep] = destination
}
}
@ -1364,8 +1389,16 @@ export default class PocomathInstance {
}
_correctPartialSelfRefs(name, imps) {
let sawDeferral = true
while (sawDeferral) {
// We might generate some new partial self references in resolving
// the previously existing ones, so looping through the signatures
// once is not enough; we have to keep looping until there are no
// more deferrals
sawDeferral = false
for (const aSignature in imps) {
if (!(imps[aSignature].deferred)) continue
sawDeferral = true
const deferral = imps[aSignature]
const part_self_references = deferral.psr
const corrected_self_references = []
@ -1436,6 +1469,7 @@ export default class PocomathInstance {
imps[aSignature].fromInstance = deferral.fromInstance
}
}
}
/* This function analyzes the template and makes sure the
* instantiations of it for type and all prior types of type are present
@ -1550,7 +1584,7 @@ export default class PocomathInstance {
const otherTypeList = typeListOfSignature(otherSig)
if (typeList.length !== otherTypeList.length) continue
let allMatch = true
let paramBound = 'any'
let paramBound = UniversalType
for (let k = 0; k < typeList.length; ++k) {
let myType = typeList[k]
let otherType = otherTypeList[k]
@ -1577,6 +1611,7 @@ export default class PocomathInstance {
theTemplateParam, paramBound)
}
if (otherType === 'any') continue
if (otherType === UniversalType) continue
if (myType === otherType) continue
if (otherType in this.Templates) {
const [myBase] = splitTemplate(myType)
@ -1608,6 +1643,7 @@ export default class PocomathInstance {
typedFunction = this[name]
}
const haveTF = this._typed.isTypedFunction(typedFunction)
&& !(typedFunction.isMeta)
if (haveTF) {
// First try a direct match
let result

View File

@ -45,6 +45,11 @@ describe('complex', () => {
assert.ok(!(math.equal(math.complex(45n, 3n), 45n)))
})
it('tests for reality', () => {
assert.ok(math.isReal(math.complex(3, 0)))
assert.ok(!(math.isReal(math.complex(3, 2))))
})
it('computes gcd', () => {
assert.deepStrictEqual(
math.gcd(math.complex(53n, 56n), math.complex(47n, -13n)),