public void Add(SubExpression expression) { if (this.Expressions.Count != 0) { Alternatives alternative = this; alternative.Literal = string.Concat(alternative.Literal, "|", expression.Literal); } else { this.Start = expression.Start; this.Literal = expression.Literal; } this.End = expression.End; this.Expressions.Add(expression); }
public override object Evaluate() { return((Complex)MathStats.SampleStandardDeviation(SubExpression.EvaluateAsCMatrix())); }
public override object Evaluate() { return((Complex)(SubExpression.EvaluateAsComplex().Im)); }
private void Add(SubExpression subExpression) { // Find an existing subfilter to OR with SubExpression candidate = this.SubExpressions.FirstOrDefault((s) => subExpression.CanOr(s)); // If can or, replace the existing one with the OR of the two if (candidate != null) { this.SubExpressions.Remove(candidate); this.SubExpressions.Add(subExpression.Or(candidate)); } else { // Can't OR, add the new one to the list this.SubExpressions.Add(subExpression); } }
// Or on subfilters really only exists for the names parameter, which can have multiple values internal SubExpression Or(SubExpression other) { if (!this.CanOr(other)) { return null; } SubExpression result = this.Clone(); if (result.Names != null) { result.Names.UnionWith(other.Names); } return result; }
public override object Evaluate() { return(Complex.Vers(SubExpression.EvaluateAsComplex())); }
public override object Evaluate() { return((Complex)MathStats.Mean(SubExpression.EvaluateAsCMatrix())); }
public override object Evaluate() { return(MathStats.SumOfSquares(SubExpression.EvaluateAsCMatrix())); }
public override double Evaluate() { return(Math.Tanh(SubExpression.Evaluate())); }
public override double Evaluate() { return(-SubExpression.Evaluate()); }
public override string ToString() { return("isIRI(" + SubExpression.ToString() + ")"); }
/// <summary> /// Parse une sous-expression de la forme : /// 5.machin(haha, truc).bidule.chose[6].truc(hoho) /// <param name="tokens"> /// Liste de jetons, en particulier : /// - Noun, Number /// - ParenthesisGroup, OperandGroup /// - Dot /// </param> /// </summary> static SubExpression ParseSubExpression(TokenList tokens, GlobalContext mainContext) { tokens = tokens.Copy(); SubExpression exp = new SubExpression(); exp.Parts = new List <SubExpressionPart>(); // Si on a un new, on ne vérifie pas qu'on ait un nom de type de cette manière : // on parse la SubExpression (donc new <Nom.Du.Type> () ) directement. if (tokens.First().Type != TokenType.New) { // Déja on vérifie qu'il y ait un nom de type avant, pour voir // si on appelle une méthode statique. bool containsTypeName = false; // Permet de savoir à quel jeton démarrer lors du prochain parsing : // on skip les jetons correspondant au type. int lastTypeTokenId = 0; int id = 0; string lastName = null; StringBuilder name = new StringBuilder(); foreach (Token token in tokens) { id++; if (token.Type == TokenType.Dot) { name.Append("."); } else if (token.Type == TokenType.Noun) { name.Append(((InfoToken)token).Content); } else if (token.Type == TokenType.GenericParametersGroup) { name.Append("`" + ((TokenContainer)token).Tokens.Count); } else if (token.Type == TokenType.ParenthesisGroup) { break; } bool isType = ReflectionUtils.IsTypeName(mainContext.LoadedAssemblies, mainContext.LoadedNamespaces, name.ToString()); if (isType) { lastTypeTokenId = id; containsTypeName = true; lastName = name.ToString(); // on finit pas au cas où on ait un NestedType. } } // On skip les token du type. // TODO : ça bugue s'il n'y a pas de points. if (containsTypeName) { SubExpressionPart part = new SubExpressionPart(lastName, SubExpressionPart.ExpTypes.ConstantTypeName); exp.Parts.Add(part); // On enlève ce nom de type pour le prochain parsing : if (tokens.Count >= lastTypeTokenId + 1) { tokens.RemoveRange(0, lastTypeTokenId + 1); } else { // l'expression est déja terminée. return(exp); } } } else // new { // Dans ce cas, la sub expression part du type s'étend jusqu'à la prochaine // parenthèse. // Liste de jetons représentant l'expression new TokenList newExprList = new TokenList(); bool isOK = false; // On parcours la liste de jetons originale en // supprimant les objets que l'on va parser. while (true && tokens.Count != 0) { if (tokens[0].Type == TokenType.ParenthesisGroup) { newExprList.Add(tokens[0]); tokens.RemoveAt(0); isOK = true; break; } newExprList.Add(tokens[0]); tokens.RemoveAt(0); } exp.Parts.Add(ParseSubExpressionPart(newExprList, mainContext)); } TokenList tempTokens = new TokenList(); foreach (Token token in tokens) { if (token.Type != TokenType.Dot) { tempTokens.Add(token); } else if (tempTokens.Count != 0) { SubExpressionPart part = ParseSubExpressionPart(tempTokens, mainContext); tempTokens.Clear(); exp.Parts.Add(part); } } // Car à la fin il n'y a pas de point. if (tempTokens.Count != 0) { SubExpressionPart part = ParseSubExpressionPart(tempTokens, mainContext); tempTokens.Clear(); exp.Parts.Add(part); } return(exp); }
private IExpression GetExpression(SubExpression s) { var subs = s.GetSubExpressions(); if (subs == null) { return(null); } if (subs.Length == 1) { if (!subs[0].ExpressionString.HasMathSymbols()) { if (subs[0].ExpressionString.All(char.IsLetter)) { if (!Variables.ContainsKey(subs[0].ExpressionString)) { Variables.Add(subs[0].ExpressionString, 0); } return(new VariableValueExpression(subs[0].ExpressionString, Variables)); } return(new StaticValueExpression(subs[0].ExpressionString)); } else { var ct = GetCalcType(subs[0].ExpressionString[0]); if (ct != null) { return(new StaticCalcExpression(ct.Value)); } else { throw new InvalidExpressionException(subs[0].ExpressionString); } } } var es = new List <IExpression>(); for (var i = 0; i < subs.Length; i++) { var ex = GetExpression(subs[i]); if (ex != null) { es.Add(ex); } } if (es.Count > 0) { if (es.Count < 3) { throw new InvalidExpressionException(s.ExpressionString); } if (es.Any(e => e is StaticCalcExpression)) { do { var index = IndexOfCalc(es); var calc = (StaticCalcExpression)es[index]; var v1 = es[index - 1]; var v2 = es[index + 1]; var e = new CalcExpression(v1, v2, calc.Type); es.RemoveRange(index - 1, 3); es.Insert(index - 1, e); } while (es.Count > 1); } return(es.First()); } return(null); }
public SubExpression(string s, SubExpression parent) { Parent = parent; ExpressionString = s; }
public static double ReverseEvaluate(Expression exp, double val) { if (exp is ConstantExpression) { ConstantExpression tExp = exp as ConstantExpression; return((exp as ConstantExpression).Value); } else if (exp is VariableExpression) { return(val); } else if (exp is AddExpression) { AddExpression nExp = (AddExpression)exp; if (nExp.OperandB.HasVariable) { return(ReverseEvaluate(nExp.OperandB, val - ReverseEvaluate(nExp.OperandA, val))); } else if (nExp.OperandA.HasVariable) { return(ReverseEvaluate(nExp.OperandA, val - ReverseEvaluate(nExp.OperandB, val))); } else { return(Evaluate(nExp, val)); } } else if (exp is SubExpression) { SubExpression nExp = (SubExpression)exp; if (nExp.OperandB.HasVariable) { return(ReverseEvaluate(nExp.OperandB, ReverseEvaluate(nExp.OperandA, val) - val)); } else if (nExp.OperandA.HasVariable) { return(ReverseEvaluate(nExp.OperandA, val + ReverseEvaluate(nExp.OperandB, val))); } else { return(Evaluate(nExp, val)); } } else if (exp is MulExpression) { MulExpression nExp = (MulExpression)exp; if (nExp.OperandB.HasVariable) { return(ReverseEvaluate(nExp.OperandB, val / ReverseEvaluate(nExp.OperandA, val))); } else if (nExp.OperandA.HasVariable) { return(ReverseEvaluate(nExp.OperandA, val / ReverseEvaluate(nExp.OperandB, val))); } else { return(Evaluate(nExp, val)); } } else if (exp is NegExpression) { NegExpression nExp = (NegExpression)exp; return(-ReverseEvaluate(nExp.Value, val)); } else if (exp is DivExpression) { DivExpression nExp = (DivExpression)exp; if (nExp.OperandB.HasVariable) { return(ReverseEvaluate(nExp.OperandB, ReverseEvaluate(nExp.OperandA, val) / val)); } else if (nExp.OperandA.HasVariable) { return(ReverseEvaluate(nExp.OperandA, val * ReverseEvaluate(nExp.OperandB, val))); } else { return(Evaluate(nExp, val)); } } throw new NotSupportedException(); }
public override object Evaluate() { return(MathStats.PopulationSkewness(SubExpression.EvaluateAsCMatrix())); }
public override object Evaluate() { return(MathStats.SampleKurtosis(SubExpression.EvaluateAsCMatrix())); }
public void ErrorEvaluateTests(string expression) { var subExpression = new SubExpression(); Assert.Throws(typeof(ArithmeticException), () => subExpression.Evaluate(expression)); }
public override object Evaluate() { return(MathStats.Product(SubExpression.EvaluateAsCMatrix())); }
public void IsMatchTests(string expression, bool expectedResult) { bool result = SubExpression.IsMatch(expression); Assert.That(result, Is.EqualTo(expectedResult)); }
public override object Evaluate() { return((Complex)MathStats.HarmonicMean(SubExpression.EvaluateAsExpandableDoubleArray())); }
public override object Evaluate() { CPolynomial poly = SubExpression.EvaluateAsCPolynomial(); return(new CMatrix(poly.Antiderivative().ToArray())); }
// AND is mainly used to combine subfilters with non-overlapping parameters. Overlapping parameters must match internal SubExpression And(SubExpression other) { SubExpression result = this.Clone(); // If other has a TimeGrain constraint if (other.TimeGrain != null) { // AND this has a TimeGrain constraint if (result.TimeGrain != null) { // Then they must be the same if (result.TimeGrain != other.TimeGrain) { return null; } } else { // Other has one, but this doesn't simply add the constraint result.TimeGrain = other.TimeGrain; } } // If other has a StartTime constraint if (other.StartTime != null) { // AND this has a StartTime constraint if (result.StartTime != null) { // Then they must be the same if (result.StartTime != other.StartTime) { return null; } } else { // Other has one, but this doesn't simply add the constraint result.StartTime = other.StartTime; } } // If other has a EndTime constraint if (other.EndTime != null) { // AND this has a EndTime constraint if (result.EndTime != null) { // Then they must be the same if (result.EndTime != other.EndTime) { return null; } } else { // Other has one, but this doesn't simply add the constraint result.EndTime = other.EndTime; } } // If other has a Names constraint if (other.Names != null) { // Take other's constraint in the absence of one on this, otherwise take the intersection if (result.Names == null) { result.Names = new HashSet<string>(other.Names); } else { result.Names.IntersectWith(other.Names); } } return result; }
public override object Evaluate() { IList <Complex> roots = SubExpression.EvaluateAsComplexVector(); return(new CMatrix(CPolynomial.FromRoots(roots).ToArray())); }
// Since each subfilter represents a conjunction, ORs are only allowed on subfilters with the same parameters set internal bool CanOr(SubExpression other) { // All the parameters must be equal in order or OR, except Names, which must either be both null or neither null return this.TimeGrain == other.TimeGrain && this.StartTime == other.StartTime && this.EndTime == other.EndTime && (this.Names == null) == (other.Names == null); }
public override object Evaluate() { CPolynomial poly = SubExpression.EvaluateAsCPolynomial(); return(new CMatrix(poly.Roots())); }
public override object Evaluate() { return((Complex)MathStats.PopulationVariance(SubExpression.EvaluateAsCMatrix())); }
public override void VisitPostOrder(Expression exp) { x86Register reg = GetFreeRegister(exp); if (exp is ConstantExpression) { insts.Add(new x86Instruction() { OpCode = x86OpCode.MOV, Operands = new Ix86Operand[] { new x86RegisterOperand() { Register = reg }, new x86ImmediateOperand() { Immediate = (exp as ConstantExpression).Value } } }); SetRegister(exp, reg); } else if (exp is VariableExpression) { insts.AddRange(args(reg)); SetRegister(exp, reg); } else if (exp is AddExpression) { AddExpression _exp = exp as AddExpression; insts.Add(new x86Instruction() { OpCode = x86OpCode.ADD, Operands = new Ix86Operand[] { new x86RegisterOperand() { Register = GetRegister(_exp.OperandA) }, new x86RegisterOperand() { Register = GetRegister(_exp.OperandB) } } }); SetRegister(exp, GetRegister(_exp.OperandA)); ReleaseRegister(_exp.OperandB); } else if (exp is SubExpression) { SubExpression _exp = exp as SubExpression; insts.Add(new x86Instruction() { OpCode = x86OpCode.SUB, Operands = new Ix86Operand[] { new x86RegisterOperand() { Register = GetRegister(_exp.OperandA) }, new x86RegisterOperand() { Register = GetRegister(_exp.OperandB) } } }); SetRegister(exp, GetRegister(_exp.OperandA)); ReleaseRegister(_exp.OperandB); } else if (exp is MulExpression) { throw new NotSupportedException(); } else if (exp is DivExpression) { throw new NotSupportedException(); } else if (exp is NegExpression) { NegExpression _exp = exp as NegExpression; insts.Add(new x86Instruction() { OpCode = x86OpCode.NEG, Operands = new Ix86Operand[] { new x86RegisterOperand() { Register = GetRegister(_exp.Value) } } }); SetRegister(exp, GetRegister(_exp.Value)); } else if (exp is InvExpression) { InvExpression _exp = exp as InvExpression; insts.Add(new x86Instruction() { OpCode = x86OpCode.NOT, Operands = new Ix86Operand[] { new x86RegisterOperand() { Register = GetRegister(_exp.Value) } } }); SetRegister(exp, GetRegister(_exp.Value)); } else if (exp is XorExpression) { XorExpression _exp = exp as XorExpression; insts.Add(new x86Instruction() { OpCode = x86OpCode.XOR, Operands = new Ix86Operand[] { new x86RegisterOperand() { Register = GetRegister(_exp.OperandA) }, new x86RegisterOperand() { Register = GetRegister(_exp.OperandB) } } }); SetRegister(exp, GetRegister(_exp.OperandA)); ReleaseRegister(_exp.OperandB); } }