Skip to content

Commit

Permalink
tests and clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
benibela committed Feb 4, 2022
1 parent 3499e30 commit 7009dee
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 125 deletions.
78 changes: 39 additions & 39 deletions gui/js/dagitty.js
Original file line number Diff line number Diff line change
Expand Up @@ -2779,14 +2779,6 @@ var GraphAnalyzer = {
//console.log(toponodes[i].id + " " + toponodes[j].id + " -> " +treksum)
}

function mulMasked(x, y){
return x*y
}
function addMasked(x, y){

return x+y
}//*/
var maskedEval = {"add": addMasked, "mul": mulMasked}
function simulateNumeric(variableValue){
var i
var j
Expand All @@ -2796,7 +2788,7 @@ var GraphAnalyzer = {
var evaled = {}
for (i = 0; i < n; i++ )
for (j = i; j < n; j++ ) {
evaled[sigma[i][j].toString()] = sigmaexpand[i][j].evalNumeric(inputs, maskedEval)
evaled[sigma[i][j].toString()] = sigmaexpand[i][j].evalToBigInt(inputs)
}
//console.log(evaled)
return evaled
Expand Down Expand Up @@ -2865,7 +2857,7 @@ var GraphAnalyzer = {
function isZeroSigmaPoly(p){
//console.log("isZeroSigmaPoly")
for (var i=0;i<simulated.length;i++)
if (p.evalNumeric(simulated[i], maskedEval) != 0)
if (p.evalToBigInt(simulated[i]) != 0)
return false
var q = p.eval(sigmaevalobj)
return q.isZero()
Expand Down Expand Up @@ -3000,14 +2992,14 @@ var GraphAnalyzer = {
var t = fastp.t()
if (!s) return isZeroSigmaPoly( ADD(MUL(a,p,p), MUL(b,p,r), MUL(c,r, r) ) )

a = a.evalNumeric(simulated[i], maskedEval)
b = b.evalNumeric(simulated[i], maskedEval)
c = c.evalNumeric(simulated[i], maskedEval)
p = p.evalNumeric(simulated[i], maskedEval)
q = q.evalNumeric(simulated[i], maskedEval)
r = r.evalNumeric(simulated[i], maskedEval)
s = s.evalNumeric(simulated[i], maskedEval)
t = t.evalNumeric(simulated[i], maskedEval)
a = a.evalToBigInt(simulated[i])
b = b.evalToBigInt(simulated[i])
c = c.evalToBigInt(simulated[i])
p = p.evalToBigInt(simulated[i])
q = q.evalToBigInt(simulated[i])
r = r.evalToBigInt(simulated[i])
s = s.evalToBigInt(simulated[i])
t = t.evalToBigInt(simulated[i])

var app_aqqs_bpr_bqst_crr_ctts = (a*p*p) + (a*q*q*s) + (b*p*r) + (b*q*s*t) + (c*r*r) + (c*t*t*s)
var minus_2apq_bpt_bqr_2crt = - ((2n*a*p*q) + (b*p*t) + (b*q*r) + (2n* c*r*t))
Expand Down Expand Up @@ -5437,7 +5429,7 @@ MPoly.prototype.toString = function(formatting){
formatting = formatting ? formatting : {}
var PLUS = "PLUS" in formatting ? formatting.PLUS : " + "
var SUB = "SUB" in formatting ? formatting.SUB : " - "
var TIMES = "TIMES" in formatting ? formatting.TIMES : "*"
var TIMES = "TIMES" in formatting ? formatting.TIMES : " "
var POWER = "POWER" in formatting ? formatting.POWER : "^"
if (this.length == 0) return "0"
return this.map(function(term, i){
Expand All @@ -5451,7 +5443,7 @@ MPoly.prototype.toString = function(formatting){
}
if (factor != 1 || term.length == 1)
temp.push(factor.toString())
for (var i=1; i < term.length; i+=2) {
for (i=1; i < term.length; i+=2) {
var exp = term[i+1] == 1 ? "" : POWER + term[i+1]
temp.push(MPolyHelper.variableToString(term[i]) + exp)
}
Expand All @@ -5467,8 +5459,8 @@ MPoly.prototype.eval = function(insert){
var replacements
if (_.isArray(insert)) replacements = insert
else {
var replacements = new Array(MPolyHelper.variableCount + 1)
for (p in insert)
replacements = new Array(MPolyHelper.variableCount + 1)
for (var p in insert)
replacements[MPolyHelper.variableFromString(p, true)] = insert[p]
}

Expand Down Expand Up @@ -5498,27 +5490,35 @@ MPoly.prototype.eval = function(insert){
else newproduct = keptpoly
if (newsum !== null) newsum = newsum.add(newproduct)
else newsum = newproduct
// console.log(newsum)
}
if (newsum === null) newsum = MPoly.zero
return newsum
}
MPoly.prototype.evalNumeric = function(insert, options){
MPoly.prototype.evalToBigInt = function(insert, options){
var newoptions = {}
if (!options) options = {}
newoptions.number = "number" in options ? options.number : function(x){return BigInt(x)}
if ("add" in options) newoptions.add = options.add
if ("mul" in options) newoptions.mul = options.mul
return this.evalToNumber(insert, newoptions)
}
MPoly.prototype.evalToNumber = function(insert, options){
var replacements
if (_.isArray(insert)) replacements = insert
else {
var replacements = new Array(MPolyHelper.variableCount + 1)
for (p in insert)
replacements = new Array(MPolyHelper.variableCount + 1)
for (var p in insert)
replacements[MPolyHelper.variableFromString(p, true)] = insert[p]
}
if (!options) options = {}
var NUMBER = "number" in options ? options.number : function(x){return x}
var MUL = "mul" in options ? options.mul : function(x,y){return x*y}
var ADD = "add" in options ? options.add : function(x,y){return x+y}

var newsum = 0n
var newsum = NUMBER(0)
for (var i=0;i<this.length;i++) {
var old = this[i]
var newproduct = BigInt(old[0])
var newproduct = NUMBER(old[0])
for (var j=1;j<old.length;j+=2) {
if (old[j] in replacements) {
var replacement = replacements[old[j]]
Expand All @@ -5527,7 +5527,6 @@ MPoly.prototype.evalNumeric = function(insert, options){
} else throw "No value given for " + MPolyHelper.variableToString(old[j])
}
newsum = ADD(newsum, newproduct)
// console.log(newsum)
}
return newsum
}
Expand Down Expand Up @@ -5558,16 +5557,16 @@ MPoly.prototype.evalNumeric = function(insert, options){
}*/
MPoly.mul = function(){
switch (arguments.length) {
case 0: return MPoly.one
case 1: return arguments[0]
default: return _.reduce(arguments, function(a,b){ return a.mul(b) })
case 0: return MPoly.one
case 1: return arguments[0]
default: return _.reduce(arguments, function(a,b){ return a.mul(b) })
}
}
MPoly.add = function(){
switch (arguments.length) {
case 0: return MPoly.zero
case 1: return arguments[0]
default: return _.reduce(arguments, function(a,b){ return a.add(b) })
case 0: return MPoly.zero
case 1: return arguments[0]
default: return _.reduce(arguments, function(a,b){ return a.add(b) })
}
}

Expand Down Expand Up @@ -5604,15 +5603,16 @@ var MPolyHelper = {
if (a[0] > b[0]) return 1
return 0
})
for (var i = 1; i < monos.length; i++)
var i
for (i = 1; i < monos.length; i++)
if (monos[i - 1][0] == monos[i][0]) {
monos[i - 1][1] += monos[i][1]
monos.splice(i,1)
i--
}
var resa = new Array(monos.length*2 + 1)
resa[0] = factor
for (var i=0;i < monos.length; i++) {
for (i=0;i < monos.length; i++) {
if ( /[+*^-]/.test(monos[i][0]) ) throw "Monomials cannot contain math symbols. Separate monomials by space or *"
resa[2*i + 1] = MPolyHelper.variableFromString(monos[i][0])
resa[2*i + 2] = monos[i][1]
Expand Down Expand Up @@ -5685,10 +5685,10 @@ var MPolyHelper = {
return res //all subterms are normalized
//normalize subterms after i
p = res
var res = MPolyHelper.createEmptyMPoly(p.length)
res = MPolyHelper.createEmptyMPoly(p.length)
var j = 0
for (; j < i; j++) res[j] = p[j]
for (; i < p.length && (p[i].length == 0 || p[i][0] == 0); i++) {}
for (; i < p.length && (p[i].length == 0 || p[i][0] == 0); i++) {/**/}
//if (i < p.length) { res[j] = p[i]; j++; i++ }
for (; i < p.length; i++)
if (j >= 1 && MPolyHelper.compareVariableOrder(res[j - 1], p[i]) == 0) {
Expand Down
28 changes: 10 additions & 18 deletions jslib/graph/GraphAnalyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1794,14 +1794,6 @@ var GraphAnalyzer = {
//console.log(toponodes[i].id + " " + toponodes[j].id + " -> " +treksum)
}

function mulMasked(x, y){
return x*y
}
function addMasked(x, y){

return x+y
}//*/
var maskedEval = {"add": addMasked, "mul": mulMasked}
function simulateNumeric(variableValue){
var i
var j
Expand All @@ -1811,7 +1803,7 @@ var GraphAnalyzer = {
var evaled = {}
for (i = 0; i < n; i++ )
for (j = i; j < n; j++ ) {
evaled[sigma[i][j].toString()] = sigmaexpand[i][j].evalNumeric(inputs, maskedEval)
evaled[sigma[i][j].toString()] = sigmaexpand[i][j].evalToBigInt(inputs)
}
//console.log(evaled)
return evaled
Expand Down Expand Up @@ -1880,7 +1872,7 @@ var GraphAnalyzer = {
function isZeroSigmaPoly(p){
//console.log("isZeroSigmaPoly")
for (var i=0;i<simulated.length;i++)
if (p.evalNumeric(simulated[i], maskedEval) != 0)
if (p.evalToBigInt(simulated[i]) != 0)
return false
var q = p.eval(sigmaevalobj)
return q.isZero()
Expand Down Expand Up @@ -2015,14 +2007,14 @@ var GraphAnalyzer = {
var t = fastp.t()
if (!s) return isZeroSigmaPoly( ADD(MUL(a,p,p), MUL(b,p,r), MUL(c,r, r) ) )

a = a.evalNumeric(simulated[i], maskedEval)
b = b.evalNumeric(simulated[i], maskedEval)
c = c.evalNumeric(simulated[i], maskedEval)
p = p.evalNumeric(simulated[i], maskedEval)
q = q.evalNumeric(simulated[i], maskedEval)
r = r.evalNumeric(simulated[i], maskedEval)
s = s.evalNumeric(simulated[i], maskedEval)
t = t.evalNumeric(simulated[i], maskedEval)
a = a.evalToBigInt(simulated[i])
b = b.evalToBigInt(simulated[i])
c = c.evalToBigInt(simulated[i])
p = p.evalToBigInt(simulated[i])
q = q.evalToBigInt(simulated[i])
r = r.evalToBigInt(simulated[i])
s = s.evalToBigInt(simulated[i])
t = t.evalToBigInt(simulated[i])

var app_aqqs_bpr_bqst_crr_ctts = (a*p*p) + (a*q*q*s) + (b*p*r) + (b*q*s*t) + (c*r*r) + (c*t*t*s)
var minus_2apq_bpt_bqr_2crt = - ((2n*a*p*q) + (b*p*t) + (b*q*r) + (2n* c*r*t))
Expand Down
50 changes: 29 additions & 21 deletions jslib/graph/MPolynomials.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ MPoly.prototype.toString = function(formatting){
formatting = formatting ? formatting : {}
var PLUS = "PLUS" in formatting ? formatting.PLUS : " + "
var SUB = "SUB" in formatting ? formatting.SUB : " - "
var TIMES = "TIMES" in formatting ? formatting.TIMES : "*"
var TIMES = "TIMES" in formatting ? formatting.TIMES : " "
var POWER = "POWER" in formatting ? formatting.POWER : "^"
if (this.length == 0) return "0"
return this.map(function(term, i){
Expand All @@ -100,7 +100,7 @@ MPoly.prototype.toString = function(formatting){
}
if (factor != 1 || term.length == 1)
temp.push(factor.toString())
for (var i=1; i < term.length; i+=2) {
for (i=1; i < term.length; i+=2) {
var exp = term[i+1] == 1 ? "" : POWER + term[i+1]
temp.push(MPolyHelper.variableToString(term[i]) + exp)
}
Expand All @@ -116,8 +116,8 @@ MPoly.prototype.eval = function(insert){
var replacements
if (_.isArray(insert)) replacements = insert
else {
var replacements = new Array(MPolyHelper.variableCount + 1)
for (p in insert)
replacements = new Array(MPolyHelper.variableCount + 1)
for (var p in insert)
replacements[MPolyHelper.variableFromString(p, true)] = insert[p]
}

Expand Down Expand Up @@ -147,27 +147,35 @@ MPoly.prototype.eval = function(insert){
else newproduct = keptpoly
if (newsum !== null) newsum = newsum.add(newproduct)
else newsum = newproduct
// console.log(newsum)
}
if (newsum === null) newsum = MPoly.zero
return newsum
}
MPoly.prototype.evalNumeric = function(insert, options){
MPoly.prototype.evalToBigInt = function(insert, options){
var newoptions = {}
if (!options) options = {}
newoptions.number = "number" in options ? options.number : function(x){return BigInt(x)}
if ("add" in options) newoptions.add = options.add
if ("mul" in options) newoptions.mul = options.mul
return this.evalToNumber(insert, newoptions)
}
MPoly.prototype.evalToNumber = function(insert, options){
var replacements
if (_.isArray(insert)) replacements = insert
else {
var replacements = new Array(MPolyHelper.variableCount + 1)
for (p in insert)
replacements = new Array(MPolyHelper.variableCount + 1)
for (var p in insert)
replacements[MPolyHelper.variableFromString(p, true)] = insert[p]
}
if (!options) options = {}
var NUMBER = "number" in options ? options.number : function(x){return x}
var MUL = "mul" in options ? options.mul : function(x,y){return x*y}
var ADD = "add" in options ? options.add : function(x,y){return x+y}

var newsum = 0n
var newsum = NUMBER(0)
for (var i=0;i<this.length;i++) {
var old = this[i]
var newproduct = BigInt(old[0])
var newproduct = NUMBER(old[0])
for (var j=1;j<old.length;j+=2) {
if (old[j] in replacements) {
var replacement = replacements[old[j]]
Expand All @@ -176,7 +184,6 @@ MPoly.prototype.evalNumeric = function(insert, options){
} else throw "No value given for " + MPolyHelper.variableToString(old[j])
}
newsum = ADD(newsum, newproduct)
// console.log(newsum)
}
return newsum
}
Expand Down Expand Up @@ -207,16 +214,16 @@ MPoly.prototype.evalNumeric = function(insert, options){
}*/
MPoly.mul = function(){
switch (arguments.length) {
case 0: return MPoly.one
case 1: return arguments[0]
default: return _.reduce(arguments, function(a,b){ return a.mul(b) })
case 0: return MPoly.one
case 1: return arguments[0]
default: return _.reduce(arguments, function(a,b){ return a.mul(b) })
}
}
MPoly.add = function(){
switch (arguments.length) {
case 0: return MPoly.zero
case 1: return arguments[0]
default: return _.reduce(arguments, function(a,b){ return a.add(b) })
case 0: return MPoly.zero
case 1: return arguments[0]
default: return _.reduce(arguments, function(a,b){ return a.add(b) })
}
}

Expand Down Expand Up @@ -253,15 +260,16 @@ var MPolyHelper = {
if (a[0] > b[0]) return 1
return 0
})
for (var i = 1; i < monos.length; i++)
var i
for (i = 1; i < monos.length; i++)
if (monos[i - 1][0] == monos[i][0]) {
monos[i - 1][1] += monos[i][1]
monos.splice(i,1)
i--
}
var resa = new Array(monos.length*2 + 1)
resa[0] = factor
for (var i=0;i < monos.length; i++) {
for (i=0;i < monos.length; i++) {
if ( /[+*^-]/.test(monos[i][0]) ) throw "Monomials cannot contain math symbols. Separate monomials by space or *"
resa[2*i + 1] = MPolyHelper.variableFromString(monos[i][0])
resa[2*i + 2] = monos[i][1]
Expand Down Expand Up @@ -334,10 +342,10 @@ var MPolyHelper = {
return res //all subterms are normalized
//normalize subterms after i
p = res
var res = MPolyHelper.createEmptyMPoly(p.length)
res = MPolyHelper.createEmptyMPoly(p.length)
var j = 0
for (; j < i; j++) res[j] = p[j]
for (; i < p.length && (p[i].length == 0 || p[i][0] == 0); i++) {}
for (; i < p.length && (p[i].length == 0 || p[i][0] == 0); i++) {/**/}
//if (i < p.length) { res[j] = p[i]; j++; i++ }
for (; i < p.length; i++)
if (j >= 1 && MPolyHelper.compareVariableOrder(res[j - 1], p[i]) == 0) {
Expand Down
Loading

0 comments on commit 7009dee

Please sign in to comment.