Pergunta

I downloaded a Maple proc which returns a Vector (say, v) of expressions and where each individual expression is in terms of other Vectors (p, a) with symbolic entries. For example:

> v := myProc();
> v[1];
p[2] + a[1]
> v[2];
p[5] + a[3] + sqrt(a[1])
...

I'd like to be able to evaluate the expressions in Vector 'v' after it is generated by assigning numerical values to Vectors 'p' and 'a' however if I define Vector 'a' and 'p' as follows:

a := Vector(3,1):  
p := Vector(5,2):

I get results in which one Vector's values are reassigned but the other Vector's values are not:

> v[1]; 
p[2] + 1
> v[2];
p[5] + 1 + sqrt(1)

Any insight as to the nature of this issue would be appreciated. I've been going through the Maple files corresponding to this proc to attempt to assign values to 'p' and 'a' before Vector 'v' returns the expressions, but this has been relatively unsuccessful, as I am relatively new to Maple and the numerous subprocs in the main proc seem to ultimately require symbolic Vectors to successfully return Vector 'v'.

Foi útil?

Solução

The p[i] in the entries in the first Vector returned by xearm are so-called escaped locals. As such they have a different address from entries of your global p Vector, which is why they fail to evaluate as you expected.

You can get around this like as follows. Replace,

v:=f[1];

by,

v:=f[1];
v:=convert(v,`global`);

The a[i] and b[i] in the first result returned by xearm appear to be indexed into the global names a and b, and don't have the same issue as do the p[i].

Using the properly working example from my earlier answer, here is a problematic version which exhibits similar behavior.

restart:

myProc:=proc()
       local w, p;
       w:=Vector(2);
       w[1]:=p[2] + a[1];
       w[2]:=p[5] + a[3] + sqrt(a[1]);
       return w;
    end proc:

v:=myProc():

v[1];
                             p[2] + a[1]

v[2];
                                         (1/2)
                       p[5] + a[3] + a[1]     

a:=Vector(3,1):
p:=Vector(5,2):

v[1];
                              p[2] + 1

v[2];
                              p[5] + 2

v:=convert(v,`global`):

v;
                                 [3]
                                 [ ]
                                 [4]

Outras dicas

Are you completely sure that p has been successfully assigned as the result of say the call Vector(5,2)?

The following works, for me, as expected.

restart:

myProc:=proc()
           local w;
           w:=Vector(2);
           w[1]:=p[2] + a[1];
           w[2]:=p[5] + a[3] + sqrt(a[1]);
           return w;
        end proc:

v:=myProc():

v[1];
                             p[2] + a[1]

v[2];
                                         (1/2)
                       p[5] + a[3] + a[1]     

a:=Vector(3,1):
p:=Vector(5,2):

v[1];
                                  3

v[2];
                                  4

I can think of some tricky ways to write myProc so that it behaves as you describe, but they are mostly quite contrived. For example, I could place two pairs of unevaluation quotes (two pairs of single left quotes) around the instances of p[5] and p[2] inside the body of myProc.

What do you get if immediately after querying v[2] (and getting your unexpected output involving unevaluated indexed references to p) you then issue the command,

%;

?

What do you get if, at the problematic point, you issue just the command,

p;

?

What do you get if, at the problematic point, you issue the command,

map(eval,v);

?

Are you at liberty to give a URL to the source of myProc?

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top