Search code examples
proceduremaple

Defining a functional operator's "result" via an expression stored in a variable is not working


I am struggling to figure out why the following two ways of defining a simple functional operator (https://www.maplesoft.com/support/help/Maple/view.aspx?path=operators/functional&term=-%3E) are not equivalent. I have added a simple example below. How do I define the "result" via an expression stored in a variable as in the latter example? I am new to Maple.

# This works fine
f := (x, y) -> y^2 + x^2;
f(1, 2); 

# This does not work when I store the "result" expression in a variable first                              
temp := x^2 + y^2;
f := (x, y) -> temp;
f(1, 2);

Solution

  • This is a common source of confusion.

    In your second example the formal parameters x and y of your procedure have no connection to the names x and y present in the expression assigned temp.

    The usual way to accomplish the second example is to use the unapply command.

    temp := x^2 + y^2;
    
                    2    2
           temp := x  + y 
    
    f := unapply(temp, [x,y]);
    
        f := proc (x, y) options operator, arrow; y^2+x^2 end proc
    
    f(1, 2);
    
              5
    

    One more general (and also more advanced) approach is to substitute into a template with the expression(s) containing the matching names. Eg,

    g := subs(__dummy=temp,
          (x,y)->__dummy);
    
        g := proc (x, y) options operator, arrow; y^2+x^2 end proc
    
    g(1, 2);
    
              5