## 102 Reputation

8 years, 209 days

## MaplePrimes Activity

### These are replies submitted by Honigmelone

thanks for your answer. Even though the optimisation works this way, g(x) is not inlcuded in the function to optimze.

## @acer  Thanks for the exhaustive a...

Thanks for the exhaustive answer! This has solved the question.

## @Carl Love  I don't have a str...

I don't have a strict 1D input here, but wanted to focus on the 1D problem first.

Nevertheless I should have excluded these additionall points from the question.

## @acer I can't quite recall why I us...

@acer

I can't quite recall why I used eval(alist,1), but for me the problem persists when I dont use it, or even redifine the list as clist. I fiddeled a bit more, and the only way to get rid of the assumptions seems to be to manually redefine everything.

In the following code snipped I define funa, funb, and func as well as alist, blist and clist before and after making assumptions on x and y, as well as after removing them again:

 >
 > restart:
 >
 > # test removing assumptions
 > # define first function before assuming
 > funa:=x+y
 (1)
 > Maple_proc := codegen[makeproc](funa,(ListTools[Flatten]([eval(alist,1)])));
 (2)
 > #place variables in a list
 > alist:=[x,y];
 (3)
 >
 > # make some assumptions
 > assume(x,real);assume(y,real);
 > x,y
 (4)
 > #place variables in a list
 > blist:=[x,y];
 (5)
 > # define second function after assuming
 > funb := x+y;
 (6)
 >
 > # make cool interferences with assumptions...
 > # clear assumptions
 > # try 1 from https://www.mapleprimes.com/questions/207601-Remove-Assumptions-
 > nms:=convert(indets(alist,name),list); nmsS:=convert~(nms,string); L:=StringTools:-Substitute~(nmsS,"~",""); #Removing "~" L1:=parse~(L); S:=nms=~L1; Expr:=subs(S,expr);
 (7)
 > hasassumptions(x)
 (8)
 > # try 2
 > nops(alist)
 (9)
 > for i from 1 to nops(alist) do parse(cat(StringTools[Substitute]~(alist[i],"~",""),":='",StringTools[Substitute]~(alist[i],"~",""),"'")); end do;
 (10)
 > # check if assumtions are there
 > hasassumptions(x)
 (11)
 > #try 3 https://www.mapleprimes.com/questions/39555-Unassume
 > for u in alist do `property/object`[u]:=evaln(`property/object`[u]); `property/OrigName`[u]:=evaln(`property/OrigName`[u]); end do;
 (12)
 > hasassumptions(x)
 (13)
 > hasassumptions(y)
 (14)
 > alist;blist;
 (15)
 > hasassumptions(eval(alist,1));hasassumptions(eval(blist,1))
 (16)
 > hasassumptions(alist);hasassumptions(blist);
 (17)
 > hasassumptions(funa);hasassumptions(funb);
 (18)
 > # define third function after automated removal of assumptions
 > func:=x+y
 (19)
 > clist:=[x,y]
 (20)
 > hasassumptions(func);hasassumptions(clist);
 (21)
 > # test if assumtions are present after usage of makeproc
 > Maple_proc := codegen[makeproc](funa,(ListTools[Flatten]([alist]))); #fail
 (22)
 > Maple_proc := codegen[makeproc](funb,(ListTools[Flatten]([blist]))); #fail
 (23)
 > Maple_proc := codegen[makeproc](func,(ListTools[Flatten]([clist]))); #fail
 (24)
 > Maple_proc := codegen[makeproc](funa,([x,y])); #fail
 (25)
 > # try 4
 > # manual unevaluation of x and y
 > x:='x';y:='y'
 (26)
 > # define list and function
 > fund := x+y
 (27)
 > dlist:=[x,y]
 (28)
 > Maple_proc := codegen[makeproc](funa,(ListTools[Flatten]([alist]))); #succes
 (29)
 > Maple_proc := codegen[makeproc](funb,(ListTools[Flatten]([blist]))); #fail
 (30)
 > Maple_proc := codegen[makeproc](func,(ListTools[Flatten]([clist]))); #fail
 (31)
 > Maple_proc := codegen[makeproc](fund,dlist); #success
 (32)
 >
 >
 >

## @John May Thanks, removing the ticks aro...

@John May Thanks, removing the ticks around the expand did the trick.

## Thanks!...

@Markiyan Hirnyk @also

Thanks very much! That worked for me. Since I don't really understand what numeric does, I will flag the fist reply as answer.

## found the problem...

hey tomleslie,

thanks for looking into the issue. The problem was indeed in my long script. It happes that I use a variable called "a" there. When this variable is loaded before any rule using the name "a" is echoed or applied behavior becomes strange. See the attached script for reference:

 > restart;
 > rule:=abs(''a''::algebraic)^2=''a''^2;
 (1)
 > someotherrule:=abs(''w''::algebraic)^2=''w''^2;
 (2)
 > # applyrule(rule,abs(x)^2); #uncommenting ether of these will solve the problem
 > # rule;
 > applyrule(someotherrule,abs(x)^2); #applying a rule without "a" has no influence
 (3)
 > a:=1; #a loaded before echoing rule
 (4)
 > rule:=abs(''a''::algebraic)^2=''a''^2;
 (5)
 > rule; #strange behavior
 (6)
 > applyrule(rule,abs(x)^2); #cannot work as rule is diffrent from what has been defined.
 (7)
 >

## thanks!...

Thanks, that was what I was looking for!

## Thanks...

Thanks, I will try that!

## even better...

thanks, I appreciate all improvement suggestions for clean code:)

Just for the record: I found that even though assign also works with tables, it only works there once:

 > restart;
 > av:=;
 (1)
 > assign(av(1),3);
 > assign(av(1),5);
 > av1;                          #works with vectors
 (2)
 > atv:=table([atv1,atv2,atv3]);
 (3)
 > atvv:=convert(convert(atv,list),Vector);
 (4)
 > assign(atvv(1),3);
 > assign(atvv(1),5);
 > atv[1];                          #works when converting the table to vector before assigning
 (5)
 > at:=table([at1,at2,at3]);
 (6)
 > assign(at[1],3);
 > assign(at[1],5);
 > at[1];      #expected result: 5
 (7)
 >

A workaround to this is to convert the table to a vector for assigning values to the tables variables. My guess is that the tables become updated when assigning, so that the link to its variable is lost.

## Thanks!...

Hey,

thank you for your second answer. To be honest I don't really understand how this function works, I am still very new at maple. But it does what it shall do. I love how you assume inside your funtion, but don't carry your assumptions to the output. That will definitely be useful later on.

However, please understand that I marked Carl Loves post as answer to this question, because his solution is a bit easier to understand.

## Thanks again!...

Hey, thanks again! I would have never guessed to use a second rule. Now it works like a charm and does exactly what I wanted it to do. In order to be able to use the rule on Vectors I had to convert a bit:

 > restart:
 > f_symbol:=:
 > rule:= abs(''a''::algebraic)*abs(1,''a''::algebraic)= ''a'':
 > rule2:= abs('expand'(-''a'')::algebraic)*abs(1, ''a''::algebraic)= ''a'':
 > applyrule([rule,rule2],f_symbol); #expected result: a(t)-b
 (1)
 > convert(applyrule([rule,rule2],convert(f_symbol,list)),Vector);
 (2)
 >
 >

## Thanks, how to apply to terms with funct...

Hey,

thanks, that is exactly what I asked for. However I want to apply this to an expression with functions in it. I don't really understand why it does not work, as a(t)-b is also algebraic. I already tried freeze with no success:

 > restart:
 > f_symbol:=abs(a-b)*abs(1,a-b):
 > f_fun:=abs(a(t)-b)*abs(1,a(t)-b);
 (1)
 > f_subs:=subs(a(t)=freeze(a(t)),f_fun);
 (2)
 > rule:= abs(''a''::algebraic)*abs(1,''a''::algebraic)= ''a'':
 > applyrule(rule,f_fun); #expected result: a(t)-b
 (3)
 > applyrule(rule,f_subs);
 (4)
 > thaw(%);               #expected result: a(t)-b
 (5)
 > applyrule(rule,f_symbol);        #works
 (6)
 > type(''a(t)-b'',algebraic);
 (7)
 >
 >