Honigmelone

102 Reputation

8 Badges

8 years, 237 days

MaplePrimes Activity


These are replies submitted by Honigmelone

@radaar 

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 answer! This has solved the question.

@Carl Love 

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 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

x+y

(1)

 Maple_proc := codegen[makeproc](funa,(ListTools[Flatten]([eval(alist,1)])));

proc (alist) x+y end proc

(2)

#place variables in a list

alist:=[x,y];

[x, y]

(3)

 

# make some assumptions

assume(x,real);assume(y,real);

x,y

x, y

(4)

#place variables in a list

blist:=[x,y];

[x, y]

(5)

# define second function after assuming

funb := x+y;

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);

[x, y]

 

["x~", "y~"]

 

["x", "y"]

 

[x, y]

 

[x = x, y = y]

 

expr

(7)

hasassumptions(x)

true

(8)

# try 2

nops(alist)

2

(9)

for i from 1 to nops(alist) do
parse(cat(StringTools[Substitute]~(alist[i],"~",""),":='",StringTools[Substitute]~(alist[i],"~",""),"'"));
end do;

'x'

 

'y'

(10)

# check if assumtions are there

hasassumptions(x)

true

(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;

`property/object`[x]

 

`property/OrigName`[x]

 

`property/object`[y]

 

`property/OrigName`[y]

(12)

hasassumptions(x)

false

(13)

hasassumptions(y)

false

(14)

alist;blist;

[x, y]

 

[x, y]

(15)

hasassumptions(eval(alist,1));hasassumptions(eval(blist,1))

false

 

false

(16)

hasassumptions(alist);hasassumptions(blist);

false

 

false

(17)

hasassumptions(funa);hasassumptions(funb);

false

 

false

(18)

# define third function after automated removal of assumptions

func:=x+y

x+y

(19)

clist:=[x,y]

[x, y]

(20)

hasassumptions(func);hasassumptions(clist);

false

 

false

(21)

# test if assumtions are present after usage of makeproc

 Maple_proc := codegen[makeproc](funa,(ListTools[Flatten]([alist]))); #fail

proc (x, y) x+y end proc

(22)

 Maple_proc := codegen[makeproc](funb,(ListTools[Flatten]([blist]))); #fail

proc (x, y) x+y end proc

(23)

 Maple_proc := codegen[makeproc](func,(ListTools[Flatten]([clist]))); #fail

proc (x, y) x+y end proc

(24)

 Maple_proc := codegen[makeproc](funa,([x,y])); #fail

proc (x, y) x+y end proc

(25)

# try 4

# manual unevaluation of x and y

x:='x';y:='y'

x

 

y

(26)

# define list and function

fund := x+y

x+y

(27)

dlist:=[x,y]

[x, y]

(28)

 Maple_proc := codegen[makeproc](funa,(ListTools[Flatten]([alist]))); #succes

proc (x, y) x+y end proc

(29)

 Maple_proc := codegen[makeproc](funb,(ListTools[Flatten]([blist]))); #fail

proc (x, y) x+y end proc

(30)

 Maple_proc := codegen[makeproc](func,(ListTools[Flatten]([clist]))); #fail

proc (x, y) x+y end proc

(31)

 Maple_proc := codegen[makeproc](fund,dlist); #success

proc (x, y) x+y end proc

(32)

 

 

 


 

Download testunassuming.mw

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

@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.

@tomleslie 

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;

abs('a'::algebraic)^2 = 'a'^2

(1)

someotherrule:=abs(''w''::algebraic)^2=''w''^2;

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

x^2

(3)

a:=1; #a loaded before echoing rule

1

(4)

rule:=abs(''a''::algebraic)^2=''a''^2;

abs('a'::algebraic)^2 = 'a'^2

(5)

rule; #strange behavior

a::algebraic^2 = a^2

(6)

applyrule(rule,abs(x)^2); #cannot work as rule is diffrent from what has been defined.

abs(x)^2

(7)

 


Download applyrule_3.mw

@Preben Alsholm 

Thanks, that was what I was looking for!

@Preben Alsholm 

Thanks, I will try that!

@acer 

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

@Kitonum 

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


restart;

av:=<av1,av2,av3>;

av := Vector(3, {(1) = av1, (2) = av2, (3) = av3})

(1)

assign(av(1),3);

assign(av(1),5);

av1;                          #works with vectors

5

(2)

atv:=table([atv1,atv2,atv3]);

table( [( 1 ) = atv1, ( 2 ) = atv2, ( 3 ) = atv3 ] )

(3)

atvv:=convert(convert(atv,list),Vector);

atvv := Vector(3, {(1) = atv1, (2) = atv2, (3) = atv3})

(4)

assign(atvv(1),3);

assign(atvv(1),5);    

atv[1];                          #works when converting the table to vector before assigning

5

(5)

at:=table([at1,at2,at3]);

table( [( 1 ) = at1, ( 2 ) = at2, ( 3 ) = at3 ] )

(6)

assign(at[1],3);

assign(at[1],5);

Error, invalid left hand side in assignment

 

at[1];      #expected result: 5

3

(7)

 


Download assign_table.mw

 

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.

@Christian Wolinski 

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.

@Carl Love 

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:=<abs(a-b)*abs(1,a-b),abs(b-c)*abs(1,b-c)>:  

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

Vector(2, {(1) = abs(a-b)*abs(1, a-b), (2) = abs(b-c)*abs(1, b-c)})

(1)

convert(applyrule([rule,rule2],convert(f_symbol,list)),Vector);

Vector(2, {(1) = a-b, (2) = b-c})

(2)

 

 


Download simplify_abs.mw

@Carl Love 

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);

abs(-a(t)+b)*abs(1, a(t)-b)

(1)

f_subs:=subs(a(t)=freeze(a(t)),f_fun);

abs(-`freeze/R0`+b)*abs(1, `freeze/R0`-b)

(2)

rule:= abs(''a''::algebraic)*abs(1,''a''::algebraic)= ''a'':

applyrule(rule,f_fun); #expected result: a(t)-b

abs(-a(t)+b)*abs(1, a(t)-b)

(3)

applyrule(rule,f_subs);

abs(-`freeze/R0`+b)*abs(1, `freeze/R0`-b)

(4)

thaw(%);               #expected result: a(t)-b

abs(-a(t)+b)*abs(1, a(t)-b)

(5)

applyrule(rule,f_symbol);        #works

a-b

(6)

type(''a(t)-b'',algebraic);

true

(7)

 

 


Download simplify_abs.mw

 

@Thomas Richard 

hey, thanks for your solution! The warning to the symbolic options is pretty scary, but since I only have real variables and functions I don't have to worry.

1 2 Page 1 of 2