/// <summary> /// \/ * value operation is called gradient /// gradient over scalar field generate a vector /// </summary> /// <param name="value"></param> /// <returns></returns> public override QsValue MultiplyOperation(QsValue value) { var fscalar = value as QsScalar; if (!Object.ReferenceEquals(fscalar, null)) { // Here we will multiply the nabla \/ * with @function if (!Object.ReferenceEquals(fscalar.FunctionQuantity, null)) { var f = fscalar.FunctionQuantity.Value; string[] prms = f.ParametersNames; SymbolicVariable fsv = f.ToSymbolicVariable(); QsVector GradientResult = new QsVector(); // we loop through the symbolic body and differentiate it with respect to the function parameters. // then accumulate the foreach (string prm in prms) { GradientResult.AddComponent(fsv.Differentiate(prm).ToQuantity().ToScalar()); } return(GradientResult); } } if (value is QsVector) { return(this.DelVector.MultiplyVector((QsVector)value)); } throw new NotImplementedException(@"Multiplication of \/ * " + value.GetType().Name + " Not implemented yet"); }
/// <summary> /// Differentiate operation for function. /// </summary> /// <param name="value">object of <see cref="QsScalar"/> that hold <see cref="AnyQuantity<SymbolicVariable>"/></param> /// <returns></returns> public override QsValue DifferentiateOperation(QsValue value) { QsScalar sval = (QsScalar)value; if (sval.ScalarType == ScalarTypes.SymbolicQuantity) { var dsv = sval.SymbolicQuantity.Value; string fname = "_"; string WholeFunction = string.Empty; if (this.FunctionBodyToken[0].TokenClassType == typeof(CurlyBracketGroupToken)) { // vector differentiation // take every term in the vector and differentiate it var vcs = QsVar.VectorComponents(this.FunctionBodyToken[0]); StringBuilder sc = new StringBuilder(); sc.Append(fname + "(" + RemoveRedundantParameters(this.ParametersNames) + ") = "); sc.Append("{ "); foreach (var c in vcs) { SymbolicVariable nsv = SymbolicVariable.Parse(c); int times = (int)dsv.SymbolPower; while (times > 0) { nsv = nsv.Differentiate(dsv.Symbol); times--; } sc.Append(nsv.ToString()); sc.Append(" "); } sc.Append("}"); WholeFunction = sc.ToString(); } else { SymbolicVariable nsv = ToSymbolicVariable(); int times = (int)dsv.SymbolPower; while (times > 0) { nsv = nsv.Differentiate(dsv.Symbol); times--; } WholeFunction = fname + "(" + RemoveRedundantParameters(this.ParametersNames) + ") = " + nsv.ToString(); } return(QsFunction.ParseFunction(QsEvaluator.CurrentEvaluator, WholeFunction)); } else { return(base.DifferentiateOperation(value)); } }