# Changeset 14539

Ignore:
Timestamp:
01/04/17 13:58:05 (5 years ago)
Message:

#2650: extended simplifier to pass all new unit tests for factors and binary factors

Location:
branches/symbreg-factors-2650
Files:
3 edited

### Legend:

Unmodified
 r14535 } else if(IsBinFactor(node)) { var binFactor = node as BinaryFactorVariableTreeNode; return MakeBinFactor(binFactor.Symbol, binFactor.VariableName, binFactor.VariableValue, Math.Cos(binFactor.Weight)); // cos(0) = 1 see similar case for Exp(binfactor) return MakeSum(MakeBinFactor(binFactor.Symbol, binFactor.VariableName, binFactor.VariableValue, Math.Cos(binFactor.Weight) - 1), MakeConstant(1.0)); } else { var cosNode = cosineSymbol.CreateTreeNode(); return MakeFactor(factNode.Symbol, factNode.VariableName, factNode.Weights.Select(w => Math.Exp(w))); } else if(IsBinFactor(node)) { // exp( binfactor w val=a) = if(val=a) exp(w) else exp(0) = binfactor( (exp(w) - 1) val a) + 1 var binFactor = node as BinaryFactorVariableTreeNode; return MakeBinFactor(binFactor.Symbol, binFactor.VariableName, binFactor.VariableValue, Math.Exp(binFactor.Weight)); return MakeSum(MakeBinFactor(binFactor.Symbol, binFactor.VariableName, binFactor.VariableValue, Math.Exp(binFactor.Weight) - 1), MakeConstant(1.0)); } else if(IsLog(node)) { return node.GetSubtree(0); var factNode = node as FactorVariableTreeNode; return MakeFactor(factNode.Symbol, factNode.VariableName, factNode.Weights.Select(w => Math.Log(w))); } else if(IsBinFactor(node)) { var binFactor = node as BinaryFactorVariableTreeNode; return MakeBinFactor(binFactor.Symbol, binFactor.VariableName, binFactor.VariableValue, Math.Log(binFactor.Weight)); } else if(IsExp(node)) { return node.GetSubtree(0); var node1 = b as FactorVariableTreeNode; return MakeFactor(node0.Symbol, node0.VariableName, node0.Weights.Zip(node1.Weights, (u, v) => u / v)); } else if(IsFactor(a) && IsBinFactor(b) && ((IVariableTreeNode)a).VariableName == ((IVariableTreeNode)b).VariableName) { var node0 = a as FactorVariableTreeNode; var node1 = b as BinaryFactorVariableTreeNode; var varValues = node0.Symbol.GetVariableValues(node0.VariableName).ToArray(); var wi = Array.IndexOf(varValues, node1.VariableValue); if(wi < 0) throw new ArgumentException(); var newWeighs = new double[varValues.Length]; node0.Weights.CopyTo(newWeighs, 0); for(int i = 0; i < newWeighs.Length; i++) if(wi == i) newWeighs[i] /= node1.Weight; else newWeighs[i] /= 0.0; return MakeFactor(node0.Symbol, node0.VariableName, newWeighs); } else if(IsFactor(a)) { return MakeFraction(MakeConstant(1.0), MakeProduct(b, Invert(a))); var node1 = b as FactorVariableTreeNode; return MakeFactor(node0.Symbol, node0.VariableName, node0.Weights.Zip(node1.Weights, (u, v) => u + v)); } else if(IsBinFactor(a) && IsFactor(b)) { return MakeSum(b, a); } else if(IsFactor(a) && IsBinFactor(b) && ((IVariableTreeNode)a).VariableName == ((IVariableTreeNode)b).VariableName) { var node0 = a as FactorVariableTreeNode; var node1 = b as BinaryFactorVariableTreeNode; var varValues = node0.Symbol.GetVariableValues(node0.VariableName).ToArray(); var wi = Array.IndexOf(varValues, node1.VariableValue); if(wi < 0) throw new ArgumentException(); var newWeighs = new double[varValues.Length]; node0.Weights.CopyTo(newWeighs, 0); newWeighs[wi] += node1.Weight; return MakeFactor(node0.Symbol, node0.VariableName, newWeighs); } else if(IsAddition(a) && IsAddition(b)) { // merge additions var node1 = b as ConstantTreeNode; return MakeBinFactor(node0.Symbol, node0.VariableName, node0.VariableValue, node0.Weight * node1.Value); } else if(IsBinFactor(a) && IsFactor(b)) { return MakeProduct(b, a); } else if(IsFactor(a) && IsBinFactor(b) && ((IVariableTreeNode)a).VariableName == ((IVariableTreeNode)b).VariableName) { var node0 = a as FactorVariableTreeNode; var node1 = b as BinaryFactorVariableTreeNode; var varValues = node0.Symbol.GetVariableValues(node0.VariableName).ToArray(); var wi = Array.IndexOf(varValues, node1.VariableValue); if(wi < 0) throw new ArgumentException(); return MakeBinFactor(node1.Symbol, node1.VariableName, node1.VariableValue, node1.Weight * node0.Weights[wi]); } else if(IsConstant(b) && ((ConstantTreeNode)b).Value.IsAlmost(1.0)) { // $* 1.0 =>$