Thuận Nguyễn
Thuận Nguyễn

Reputation: 31

How to make a proc that aply for all variables in maple

for example supposed I want to program a proc that plus 1 to all results that is a real numbers in my program. Is there a way to do that ?

Upvotes: 2

Views: 67

Answers (1)

maplemathmatt
maplemathmatt

Reputation: 176

You can use anames('user') to obtain top-level, user-defined variables, and then select those that are real-valued. In the code below, calling incrementReals() will increment all such variables:

restart;

a, b, c := 3, 1 - I, 0.5;

# Procedure to increment by 1 all user-defined variables that are of type 'realcons'.
incrementReals := proc()
    local A := select( u -> type( eval(u), 'realcons' ), [ anames( 'user' ) ] ): 
    local B := map( u -> eval(u) + 1, A ):
    assign( A =~ B );  
    return NULL:
end proc:

# Increment.
incrementReals();

# Confirm that a and b were incremented, but b was not.
'a' = a, 'b' = b, 'c' = c; # a = 4, b = 1 - I, c = 1.5

Upvotes: 1

Related Questions