Preben Alsholm

13613 Reputation

22 Badges

19 years, 236 days

MaplePrimes Activity


These are replies submitted by Preben Alsholm

Surely the uses of map and `~` have some overlap.
But their respective domains of operation are different.
Here are two examples where `~` doesn't work:
 

map(simplify,sqrt(a^2)+sqrt(b^2)) assuming a>0,b<0;
f~([a,b],[a,b,c]); #error
map2(f,[a,b],[a,b,c]);

 

@nm On purpose I didn't do that becase it won't have any effect in RootOF.
Notice this:
 

S4:=singular(1/(cos(x)*sin(x)*Psi(1/x)),x);
subs(_Z=ZZZZZ,{S4}); # The substitution is made yes, but:
%; # back to _Z
RootOf(sin(x),x); # Look at the result

For more look at ?RootOf in particular the examples.

_Z is protected exactly because of its use here:
Try assigning to _Z as in _Z:=1234;
Also try:
 

restart;
local _Z;
RootOf(sin(x),x);

Notice the global version :-_Z in RootOf

@nm Here is a somewhat more automated approach. As of now it only handles N,NN, and Z.
Notice that assign is only used once.

restart;
#S4:=singular(1/(cos(x)*sin(x))+Psi(1/x),x);
S4:=singular(1/(cos(x))+Psi(1/x),x); 
idts:=indets([S4],`local`);
S:=convert~(idts,string);
idts;

resZ,resNoZ:=selectremove(s->evalb(s[2]="Z"),S);
resN,resNoN:=selectremove(s->evalb(s[2]="N"),resNoZ);
resNN,resNoNN:=selectremove(s->evalb(s[2..3]="NN"),resN);
numZ:=numelems(resZ);
numNN:=numelems(resNN);
numN:=numelems(resNoNN);
`union`(idts[1..numN]=~N,idts[numN+1..numNN]=~NN,idts[numN+numNN+1..numZ+numNN]=~Z) ;
assign(%);
S4;


 

@nm I did something that in your case will look like this:

restart;

S4:=singular(1/(cos(x)*sin(x))+Psi(1/x),x);
idts:=indets([S4],`local`);

idts[1]=~NN;

assign(%);

idts;

idts[-2..-1]=~Z; # Don't use _Z

assign(%);

idts;

S4;

 

@Axel Vogt Yes indeed. I was looking into that myself. I was first thinking of making use of StringTools.
But turning names into strings, manipulating them, and then parsing makes the variables global.
But here is another ad hoc example:
 

restart;
S1:=singular(Psi(1/x)*1/sin(x));
S2:=singular(Psi(1/x)*x/sin(x));
S3:=singular(Psi(1/x)*x^2/sin(x));
idts:=indets(`union`(S1,S2,S3),`local`);
idts[1..3]=~NN;
assign(%);
idts;
idts[-3..-1]=~Z; # Don't use _Z
assign(%);
idts;
`union`(S1,S2,S3);

To my point about not using StringTools here is the obstacle:
In my original idts above (before any use of assign) you can try:
 

idts:=indets(`union`(S1,S2,S3),`local`);
S:=convert~(idts,string);
parse~(S);
select(type,%,`local`); #Empty

 

@Carl Love I quite agree. I don't get those warnings though. My setting for warnlevel is 3 (the default).
I wonder why nm gets them and I don't.

convert(179.9087476,'units','arcdeg','rad');

 

@nm You can submit a bug report directly from MaplePrimes:
Go to the top of the page just under the MaplePrimes heading.
There you find the horizontal list starting with Questions and ending with More.
Click on More and you will see "Submit Software Change Request".
Software Change Request (SCR) is a euphemism for bug report.

Are you able to do anything at all to begin with? Like just typing 2+2; and getting 4 when you press enter.
If not it might be a license issue. You could talk to the person at your school who takes care of such matters.

@abert Maybe another example can throw some light on this.
 

restart;
q:=proc() local z; z end proc;
q(); # returns an 'escaped' local
%-z; # z - z
z1:=q();
z1;
addressof(z1);
addressof(z);

You see that there are two different names: the global z and the escaped local also printed as z.
You could continue with this:
 

z2:=q();
addressof(z2);
z-z1-z2; # z-z-z

The idea of creating local names for use in some context is actually exemplified in a very old and undocumented procedure called `tools/gensym` ("generate (local) symbol"):
 

showstat(`tools/gensym`);

There is correspondingly this one:
 

showstat(`tools/genglobal`);

Thus if you continue with this:
 

z3:=`tools/gensym`(z);
z-z1-z2-z3; # z-z-z-z
z4:=`tools/genglobal`(z1);
z-z1-z2-z3-z4; # z-z-z-z-z0

Looking at the addresses of the global z1 and the loval it evalutes to:
 

addressof('z1');
addressof(z1);

They are different.

@nm Tho see the procedure set interface(verboseproc=3);
It responds with 1, which is the default.
Now do eval(applyrule),  print(applyrule), or lprint(eval(applyrule)).
The latter is easier to copy, Ctrl+c.
Dump that on an input line call it something (not applyrule).
Make the changes I suggested above.

@Carl Love I tried this on nm's other example rule0 :=A::anything + B::anything = A + 1/2*B:

restart;
rule1:=A::nonunit(anything) + B::nonunit(anything) = A + 1/2*B
applyrule(rule1,x+y);

This never finishes. We can make it finish by making some simple changes in applyrule as I have pointed out above.
Let ap be the name of that procedure, which has a third optional argument count::nonnegint:=100.
 

ap(rule1,x+y,1);  # x+y/2
ap(rule1,x+y,2);  # x+y/4
ap(rule1,x+y,16); # x + 1/65536*y
etc,etc

 

@nm Wrapping works in your rule0 example:

restart;
applyrule(f(A::anything)+f(B::anything) = f(A)+1/2*f(B),f(A)+f(B));
eval(%,f=(x->x)); # A+B/2

 

@nm Since you can in fact look at the procedure I did that and allowed myself to change its input to this:
 

ap:=proc (_rules, expr,count::nonnegint:=100)

Notice the default for count. Then I introduced a local variable j. The 'while' do loop I changed to
 

j:=0;
    while answer <> i and j<count do 
     j:=j+1;
et cetera

Then I did:
 

rule0:=A::anything+B::anything = A+1/2*B;
ap(rule0,A+B,1); # Result 1/2*A+B
ap(rule0,A+B,5); # Result1/16*A + 1/2*B
ap(rule0,A+B); # Result 1/633825300114114700748351602688*A + 1/2*B
### You wanted A+1/2*B
### Now your first rule (the_rule)
rule1:=A::anything+B::anything = A*B;
ap(rule1,A+B,1); # B
ap(rule1,A+B,2); # 0

applyrule is copyrighted so I don't bring the rest.
So making applyrule stop is easy, but it won't help in these cases.

First 11 12 13 14 15 16 17 Last Page 13 of 229