Maple function if elif else


  • administrators

    To get the if else if else, statement in Maple T.A. you can use the Maple functionality.

    #Algorithm
    
    # generate a random number, from 0 to 9 inclusive, to test the conditions
    $x=rint(10); 
    $TA=maple("
    if ($x< 3) then
    $x
    elif ($x>3 and $x<7) then
    0
    else
    -$x
    end if
    "); 
    

    If you want to return a number of values from the if statement you can use lists and switch statements to achieve this

    #Algorithm
    
    # generate a random number, from 0 to 9 inclusive, to test the conditions
    $x=rint(10); 
    $TA=maple("
    if ($x< 3) then
    [$x,sin($x)]
    elif ($x>3 and $x<7) then
    [$x,cos($x)]
    else
    [$x,tan($x)]
    end if
    ");
    $val1=switch(0,$TA);
    $val2=switch(1,$TA);
    

    Here is the sort of thing that gets returned

    As you can see there is an implicit parse that occurs with the switch statement. Be careful of this, it causes a lot of confusion. To get around this you need to convert the returned statements to strings. Then the first parse just returns the statement you expect.

    Here's how you do it.

    #Algorithm
    
    # generate a random number, from 0 to 9 inclusive, to test the conditions
    $x=rint(10); 
    $TA=maple("
    if ($x< 3) then
    [$x,convert(sin($x),string)]
    elif ($x>3 and $x<7) then
    [$x,convert(cos($x),string)]
    else
    [$x,convert(tan($x),string)]
    end if
    ");
    $val1=switch(0,$TA);
    $val2=switch(1,$TA);

    This results in


Log in to reply