Can anyone help fix this DDMathParser new function?

血红的双手。 提交于 2019-12-11 11:23:57

问题


I'm registering a summation function that is sum(expression,initial,final). The expression contains the independent variable x and the initial and final are integers. For example, sum(2x,2,4) should return 2*2+3*2+4*2=18. However, I have trouble substituting x in the expression for every value between initial and final. Here are my code and it doesn't work. Can anyone tell me how to fix it?

//sum(expr (dependent on x),initial,final)
[evaluator registerFunction:^DDExpression *(NSArray *args, NSDictionary *vars, DDMathEvaluator *eval, NSError *__autoreleasing *error) {
    DDExpression *final = nil;
    if([args count] == 3){
        DDExpression *expr = [args objectAtIndex:0];
        DDExpression *start = [args objectAtIndex:1];
        DDExpression *end = [args objectAtIndex:2];
        //test whether start and end are integers
        double startDouble = [[start evaluateWithSubstitutions:nil evaluator:evaluator error:nil] doubleValue];
        double endDouble = [[end evaluateWithSubstitutions:nil evaluator:evaluator error:nil] doubleValue];
        int startInt = (int)startDouble;
        int endInt = (int)endDouble;
        if (((startDouble-startInt)>0.9999 || (startDouble-startInt)<0.0001) && ((endDouble-endInt)>0.9999 || (endDouble-endInt)<0.0001)) {
            //start and end are integers
            double resultSum = 0;
            for (int i=startInt; i<=endInt; i++) {
                NSNumber *xValue = [NSNumber numberWithInt:i];
                NSDictionary *substitution = [NSDictionary dictionaryWithObjectsAndKeys:xValue,@"x", nil];
                NSNumber *valueOfExpr = [expr evaluateWithSubstitutions:substitution evaluator:evaluator error:nil];
                resultSum = resultSum + [valueOfExpr doubleValue];
            }
            final = [DDExpression numberExpressionWithNumber:[NSNumber numberWithDouble:resultSum]];
        }
    }
    return final;
}forName:@"sum"];

回答1:


DDMathParser author here.

There are two things wrong, and one thing suboptimal.

  1. Wrong - You can't call this function "sum". "sum" is a built-in function, and built-in functions cannot be overridden.
  2. Wrong - The first parameter of your function is: "2x". That's fine, except that x is being interpreted as a function, but inside your for loop you're treating x as if it were a variable. You either need to fundamentally alter how your function works, or use the form 2 * $x (or just 2$x) as the first parameter to your function.
  3. Suboptimal - Instead of getting the doubleValue off the evaluated arguments, just get the integerValue instead and avoid the whole double-to-int conversion.

Here's how I'd clean up your function (although you should probably do a bit more in the way of filling in the error out parameter):

[evaluator registerFunction:^DDExpression *(NSArray *args, NSDictionary *vars, DDMathEvaluator *eval, NSError *__autoreleasing *error) {
    DDExpression *final = nil;
    if ([args count] == 3) {
        DDExpression *expr = [args objectAtIndex:0];
        DDExpression *start = [args objectAtIndex:1];
        DDExpression *end = [args objectAtIndex:2];
        //get the start and end values
        NSInteger startValue = [[eval evaluateExpression:start withSubstitutions:vars error:error] integerValue];
        NSInteger endValue = [[eval evaluateExpression:end withSubstitutions:vars error:error] integerValue];

        double sum = 0;
        for (NSInteger i = startValue; i <= endValue; i++) {
            NSDictionary *substitution = @{@"x": @(i)};
            NSNumber *value = [eval evaluateExpression:expr withSubstitutions:substitution error:error];
            sum += [value doubleValue];
        }
        final = [DDExpression numberExpressionWithNumber:@(sum)];
    }
    return final;
} forName:@"sum2"];

With this, you can do sum2(2*$x, 2, 4) and it will evaluate to 18.



来源:https://stackoverflow.com/questions/15717995/can-anyone-help-fix-this-ddmathparser-new-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!